map_based_queue.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2017, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #ifndef COSTMAP_QUEUE_MAP_BASED_QUEUE_H
00036 #define COSTMAP_QUEUE_MAP_BASED_QUEUE_H
00037 
00038 #include <algorithm>
00039 #include <map>
00040 #include <stdexcept>
00041 #include <utility>
00042 #include <vector>
00043 
00044 namespace costmap_queue
00045 {
00060 template <class item_t>
00061 class MapBasedQueue
00062 {
00063 public:
00067   explicit MapBasedQueue(bool reset_bins = true) : reset_bins_(reset_bins), item_count_(0)
00068   {
00069     reset();
00070   }
00071 
00075   virtual void reset()
00076   {
00077     if (reset_bins_ || item_count_ > 0)
00078     {
00079       item_bins_.clear();
00080       item_count_ = 0;
00081     }
00082     iter_ = last_insert_iter_ = item_bins_.end();
00083   }
00084 
00090   void enqueue(const double priority, item_t item)
00091   {
00092     // We keep track of the last priority we inserted. If this items priority matches the previous insertion
00093     // we can avoid searching through all the bins.
00094     if (last_insert_iter_ == item_bins_.end() || last_insert_iter_->first != priority)
00095     {
00096       last_insert_iter_ = item_bins_.find(priority);
00097 
00098       // If not found, create a new bin
00099       if (last_insert_iter_ == item_bins_.end())
00100       {
00101         auto map_item = std::make_pair(priority, std::move(std::vector<item_t>()));
00102 
00103         // Inserts an item if it doesn't exist. Returns an iterator to the item whether it existed or was inserted.
00104         std::pair<ItemMapIterator, bool> insert_result = item_bins_.insert(std::move(map_item));
00105         last_insert_iter_ = insert_result.first;
00106       }
00107     }
00108 
00109     // Add the item to the vector for this map key
00110     last_insert_iter_->second.push_back(item);
00111     item_count_++;
00112 
00113     // Use short circuiting to check if we want to update the iterator
00114     if (iter_ == item_bins_.end() || priority < iter_->first)
00115     {
00116       iter_ = last_insert_iter_;
00117     }
00118   }
00119 
00126   bool isEmpty()
00127   {
00128     return item_count_ == 0;
00129   }
00130 
00135   item_t& front()
00136   {
00137     if (iter_ == item_bins_.end())
00138     {
00139       throw std::out_of_range("front() called on empty costmap_queue::MapBasedQueue!");
00140     }
00141 
00142     return iter_->second.back();
00143   }
00144 
00148   void pop()
00149   {
00150     if (iter_ != item_bins_.end() && !iter_->second.empty())
00151     {
00152       iter_->second.pop_back();
00153       item_count_--;
00154     }
00155 
00156     auto not_empty = [](const typename ItemMap::value_type& key_val) { return !key_val.second.empty(); };
00157     iter_ = std::find_if(iter_, item_bins_.end(), not_empty);
00158   }
00159 
00160 protected:
00161   using ItemMap = std::map<double, std::vector<item_t>>;
00162   using ItemMapIterator = typename ItemMap::iterator;
00163 
00164   bool reset_bins_;
00165 
00166   ItemMap item_bins_;
00167   unsigned int item_count_;
00168   ItemMapIterator iter_;
00169   ItemMapIterator last_insert_iter_;
00170 };
00171 }  // namespace costmap_queue
00172 
00173 #endif  // COSTMAP_QUEUE_MAP_BASED_QUEUE_H


costmap_queue
Author(s):
autogenerated on Wed Jun 26 2019 20:09:33