Nav2 Navigation Stack - humble  humble
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 void reset()
77  {
78  if (reset_bins_ || item_count_ > 0) {
79  item_bins_.clear();
80  item_count_ = 0;
81  }
82  iter_ = last_insert_iter_ = item_bins_.end();
83  }
84 
90  void enqueue(const double priority, item_t item)
91  {
92  // We keep track of the last priority we inserted. If this items priority
93  // matches the previous insertion we can avoid searching through all the
94  // bins.
95  if (last_insert_iter_ == item_bins_.end() || last_insert_iter_->first != priority) {
96  last_insert_iter_ = item_bins_.find(priority);
97 
98  // If not found, create a new bin
99  if (last_insert_iter_ == item_bins_.end()) {
100  auto map_item = std::make_pair(priority, std::move(std::vector<item_t>()));
101 
102  // Inserts an item if it doesn't exist. Returns an iterator to the item
103  // whether it existed or was inserted.
104  std::pair<ItemMapIterator, bool> insert_result = item_bins_.insert(std::move(map_item));
105  last_insert_iter_ = insert_result.first;
106  }
107  }
108 
109  // Add the item to the vector for this map key
110  last_insert_iter_->second.push_back(item);
111  item_count_++;
112 
113  // Use short circuiting to check if we want to update the iterator
114  if (iter_ == item_bins_.end() || priority < iter_->first) {
115  iter_ = last_insert_iter_;
116  }
117  }
118 
125  bool isEmpty()
126  {
127  return item_count_ == 0;
128  }
129 
134  item_t & front()
135  {
136  if (iter_ == item_bins_.end()) {
137  throw std::out_of_range("front() called on empty costmap_queue::MapBasedQueue!");
138  }
139 
140  return iter_->second.back();
141  }
142 
146  void pop()
147  {
148  if (iter_ != item_bins_.end() && !iter_->second.empty()) {
149  iter_->second.pop_back();
150  item_count_--;
151  }
152 
153  auto not_empty = [](const typename ItemMap::value_type & key_val) {
154  return !key_val.second.empty();
155  };
156  iter_ = std::find_if(iter_, item_bins_.end(), not_empty);
157  }
158 
159 protected:
160  using ItemMap = std::map<double, std::vector<item_t>>;
161  using ItemMapIterator = typename ItemMap::iterator;
162 
163  bool reset_bins_;
164 
165  ItemMap item_bins_;
166  unsigned int item_count_;
167  ItemMapIterator iter_;
168  ItemMapIterator last_insert_iter_;
169 };
170 } // namespace costmap_queue
171 
172 #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.
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.