16 #ifndef LOW_PRIORITY_QUEUE
17 #define LOW_PRIORITY_QUEUE
22 #warning TODO: exchange the list of value_type's to a list of Elements and write a custom iterator class returning value_type on dereference
27 template<
class Element,
class Priority =
size_t>
31 typedef typename std::pair<Element, Priority> value_type;
33 #if GCC_VERSION >= 40900
34 typedef typename list<value_type>::const_iterator const_iterator;
36 typedef typename list<value_type>::iterator const_iterator;
41 typedef typename list<value_type>::iterator iterator;
42 typedef typename std::map<Priority, list<value_type> > PrioToList;
43 typedef typename PrioToList::iterator PrioToListIter;
44 PrioToList prio_to_list;
52 prio_to_list(lpq.prio_to_list)
58 return prio_to_list.empty();
62 const_iterator
insert(
const value_type& value)
64 const Priority& prio = value.second;
66 const PrioToListIter prio_it = prio_to_list.DEFAULT_EMPLACE(prio).first;
67 list<value_type>& vlist = prio_it->second;
68 vlist.push_back(value);
69 DEBUG5(cout <<
"inserted item of priority "<<vlist.back().second<<endl);
70 return std::prev(vlist.end());
74 void erase(
const const_iterator& it)
76 const Priority& prio = it->second;
79 const typename PrioToList::iterator p_iter = prio_to_list.find(prio);
80 assert(p_iter != prio_to_list.end());
81 list<value_type>& vlist = p_iter->second;
84 if(vlist.empty()) prio_to_list.erase(p_iter);
90 const Priority& old_prio = it->second;
92 if(old_prio != new_prio){
93 const typename PrioToList::iterator old_list = prio_to_list.find(old_prio);
94 assert(old_list != prio_to_list.end());
97 const typename PrioToList::iterator new_list = prio_to_list.DEFAULT_EMPLACE(new_prio).first;
98 list<value_type>& vlist = new_list->second;
100 vlist.splice(vlist.end(), old_list->second, it);
101 vlist.back().second = new_prio;
103 if(old_list->second.empty()) prio_to_list.erase(old_list);
121 assert(!prio_to_list.empty());
122 assert(!prio_to_list.begin()->second.empty());
123 return prio_to_list.begin()->second.front();
130 list<value_type>& vlist = prio_to_list.begin()->second;
132 if(vlist.empty()) prio_to_list.erase(prio_to_list.begin());
138 assert(!prio_to_list.empty());
139 assert(!prio_to_list.rbegin()->second.empty());
140 return prio_to_list.rbegin()->second.back();
147 list<value_type>& vlist = prio_to_list.rbegin()->second;
149 if(vlist.empty()) prio_to_list.erase(prio_to_list.rbegin());
a low-priority queue of Elements with Priorities
Definition: low_priority_queue.hpp:28
Definition: low_priority_queue.hpp:23
void increment_priority(const const_iterator &it)
increment the priority of an item, given as iterator (see change_priority())
Definition: low_priority_queue.hpp:113
const bool empty() const
return whether the low-priority queue is empty
Definition: low_priority_queue.hpp:56
void change_priority(const const_iterator &it, const Priority &new_prio)
change the priority of an item in the low-priority queue
Definition: low_priority_queue.hpp:88
void decrement_priority(const const_iterator &it)
decrement the priority of an item, given as iterator (see change_priority())
Definition: low_priority_queue.hpp:108
void pop_max()
remove the max of the low-priority queue
Definition: low_priority_queue.hpp:143
low_priority_queue()
construct an empty low-priority queue
Definition: low_priority_queue.hpp:48
const value_type & get_max() const
return the max of the low-priority queue - this is an item of maximum priority
Definition: low_priority_queue.hpp:136
const_iterator insert(const value_type &value)
insert an element into the low-priority queue, returning a const_iterator to the newly inserted item ...
Definition: low_priority_queue.hpp:62
const value_type & get_min() const
return the min of the low-priority queue - this is any item of minimum priority
Definition: low_priority_queue.hpp:119
void erase(const const_iterator &it)
erase an element (given as iterator) from the low-priority queue
Definition: low_priority_queue.hpp:74
low_priority_queue(const low_priority_queue< Element, Priority > &lpq)
copy construct a low-priority queue
Definition: low_priority_queue.hpp:51
void pop_min()
remove the min of the low-priority queue
Definition: low_priority_queue.hpp:126