Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
map_based_queue.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef COSTMAP_QUEUE__MAP_BASED_QUEUE_HPP_
36 #define COSTMAP_QUEUE__MAP_BASED_QUEUE_HPP_
37 
38 #include <algorithm>
39 #include <map>
40 #include <stdexcept>
41 #include <utility>
42 #include <vector>
43 
44 namespace costmap_queue
45 {
60 template<class item_t>
62 {
63 public:
67  explicit MapBasedQueue(bool reset_bins = true)
68  : reset_bins_(reset_bins), item_count_(0)
69  {
70  reset();
71  }
72 
76  virtual ~MapBasedQueue() = default;
77 
81  virtual void reset()
82  {
83  if (reset_bins_ || item_count_ > 0) {
84  item_bins_.clear();
85  item_count_ = 0;
86  }
87  iter_ = last_insert_iter_ = item_bins_.end();
88  }
89 
95  void enqueue(const double priority, item_t item)
96  {
97  // We keep track of the last priority we inserted. If this items priority
98  // matches the previous insertion we can avoid searching through all the
99  // bins.
100  if (last_insert_iter_ == item_bins_.end() || last_insert_iter_->first != priority) {
101  last_insert_iter_ = item_bins_.find(priority);
102 
103  // If not found, create a new bin
104  if (last_insert_iter_ == item_bins_.end()) {
105  auto map_item = std::make_pair(priority, std::move(std::vector<item_t>()));
106 
107  // Inserts an item if it doesn't exist. Returns an iterator to the item
108  // whether it existed or was inserted.
109  std::pair<ItemMapIterator, bool> insert_result = item_bins_.insert(std::move(map_item));
110  last_insert_iter_ = insert_result.first;
111  }
112  }
113 
114  // Add the item to the vector for this map key
115  last_insert_iter_->second.push_back(item);
116  item_count_++;
117 
118  // Use short circuiting to check if we want to update the iterator
119  if (iter_ == item_bins_.end() || priority < iter_->first) {
120  iter_ = last_insert_iter_;
121  }
122  }
123 
130  bool isEmpty()
131  {
132  return item_count_ == 0;
133  }
134 
139  item_t & front()
140  {
141  if (iter_ == item_bins_.end()) {
142  throw std::out_of_range("front() called on empty costmap_queue::MapBasedQueue!");
143  }
144 
145  return iter_->second.back();
146  }
147 
151  void pop()
152  {
153  if (iter_ != item_bins_.end() && !iter_->second.empty()) {
154  iter_->second.pop_back();
155  item_count_--;
156  }
157 
158  auto not_empty = [](const typename ItemMap::value_type & key_val) {
159  return !key_val.second.empty();
160  };
161  iter_ = std::find_if(iter_, item_bins_.end(), not_empty);
162  }
163 
164 protected:
165  using ItemMap = std::map<double, std::vector<item_t>>;
166  using ItemMapIterator = typename ItemMap::iterator;
167 
168  bool reset_bins_;
169 
170  ItemMap item_bins_;
171  unsigned int item_count_;
172  ItemMapIterator iter_;
173  ItemMapIterator last_insert_iter_;
174 };
175 } // namespace costmap_queue
176 
177 #endif // COSTMAP_QUEUE__MAP_BASED_QUEUE_HPP_
Templatized interface for a priority queue.
bool isEmpty()
Check to see if there is anything in the queue.
MapBasedQueue(bool reset_bins=true)
Default Constructor.
virtual void reset()
Clear the queue.
virtual ~MapBasedQueue()=default
Default virtual Destructor.
item_t & front()
Return the item at the front of the queue.
void enqueue(const double priority, item_t item)
Add a new item to the queue with a set priority.
void pop()
Remove (and destroy) the item at the front of the queue.