Apollo  6.0
Open source self driving car software
indexed_list.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
21 #pragma once
22 
23 #include <unordered_map>
24 #include <vector>
25 
26 #include <boost/thread/shared_mutex.hpp>
27 
28 #include "cyber/common/log.h"
30 
31 namespace apollo {
32 namespace planning {
33 
34 template <typename I, typename T>
35 class IndexedList {
36  public:
45  T* Add(const I id, const T& object) {
46  auto obs = Find(id);
47  if (obs) {
48  AWARN << "object " << id << " is already in container";
49  *obs = object;
50  return obs;
51  } else {
52  object_dict_.insert({id, object});
53  auto* ptr = &object_dict_.at(id);
54  object_list_.push_back(ptr);
55  return ptr;
56  }
57  }
58 
65  T* Find(const I id) {
66  return apollo::common::util::FindOrNull(object_dict_, id);
67  }
68 
75  const T* Find(const I id) const {
76  return apollo::common::util::FindOrNull(object_dict_, id);
77  }
78 
83  const std::vector<const T*>& Items() const { return object_list_; }
84 
89  const std::unordered_map<I, T>& Dict() const { return object_dict_; }
90 
95  this->object_list_.clear();
96  this->object_dict_.clear();
97  for (const auto& item : other.Dict()) {
98  Add(item.first, item.second);
99  }
100  return *this;
101  }
102 
103  private:
104  std::vector<const T*> object_list_;
105  std::unordered_map<I, T> object_dict_;
106 };
107 
108 template <typename I, typename T>
109 class ThreadSafeIndexedList : public IndexedList<I, T> {
110  public:
111  T* Add(const I id, const T& object) {
112  boost::unique_lock<boost::shared_mutex> writer_lock(mutex_);
113  return IndexedList<I, T>::Add(id, object);
114  }
115 
116  T* Find(const I id) {
117  boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
118  return IndexedList<I, T>::Find(id);
119  }
120 
121  std::vector<const T*> Items() const {
122  boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
123  return IndexedList<I, T>::Items();
124  }
125 
126  private:
127  mutable boost::shared_mutex mutex_;
128 };
129 
130 } // namespace planning
131 } // namespace apollo
T * Find(const I id)
Definition: indexed_list.h:116
const std::vector< const T * > & Items() const
List all the items in the container.
Definition: indexed_list.h:83
const T * Find(const I id) const
Find object by id in the container.
Definition: indexed_list.h:75
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Planning module main class. It processes GPS and IMU as input, to generate planning info...
IndexedList & operator=(const IndexedList &other)
Copy the container with objects.
Definition: indexed_list.h:94
Some map util functions.
T * Add(const I id, const T &object)
copy object into the container. If the id is already exist, overwrite the object in the container...
Definition: indexed_list.h:45
Definition: indexed_list.h:35
const std::unordered_map< I, T > & Dict() const
List all the items in the container.
Definition: indexed_list.h:89
Definition: indexed_list.h:109
T * Find(const I id)
Find object by id in the container.
Definition: indexed_list.h:65
#define AWARN
Definition: log.h:43
T * Add(const I id, const T &object)
Definition: indexed_list.h:111
std::vector< const T * > Items() const
Definition: indexed_list.h:121