23 #ifndef TURI_MUTABLE_PRIORITY_QUEUE_HPP 24 #define TURI_MUTABLE_PRIORITY_QUEUE_HPP 29 #include <boost/unordered_map.hpp> 64 template <
typename T,
typename Priority>
82 std::vector<heap_element>
heap;
88 size_t left(
size_t i)
const {
104 return heap[i].second;
108 bool less(
size_t i,
size_t j) {
109 return heap[i].second < heap[j].second;
113 void swap(
size_t i,
size_t j) {
114 std::swap(heap[i], heap[j]);
115 index_map[heap[i].first] = i;
116 index_map[heap[j].first] = j;
125 if ((l <= s) &&
less(i, l))
127 if ((r <= s) &&
less(largest, r))
138 : heap(1,
std::make_pair(T(), Priority())) { }
151 return heap.size() - 1;
161 return index_map.count(item) > 0;
165 void push(T item, Priority priority) {
166 heap.push_back(std::make_pair(item, priority));
176 const std::pair<T, Priority>&
top()
const {
185 std::pair<T, Priority>
pop() {
187 heap_element
top = heap[1];
191 index_map.erase(top.first);
196 Priority
get(T item)
const {
197 typename index_map_type::const_iterator iter = index_map.find(item);
198 assert(iter != index_map.end());
199 size_t i = iter->second;
200 return heap[i].second;
214 typename index_map_type::const_iterator iter = index_map.find(item);
215 assert(iter != index_map.end());
217 size_t i = iter->second;
218 heap[i].second = priority;
232 typename index_map_type::const_iterator iter = index_map.find(item);
233 if(iter != index_map.end()) {
235 size_t i = iter->second;
236 heap[i].second = priority;
244 push(item, priority);
257 typename index_map_type::const_iterator iter = index_map.find(item);
258 if(iter != index_map.end()) {
260 size_t i = iter->second;
261 heap[i].second = std::max(priority, heap[i].second);
272 push(item, priority);
286 typename index_map_type::const_iterator iter = index_map.find(item);
287 if(iter != index_map.end()) {
289 size_t i = iter->second;
290 heap[i].second = priority + heap[i].second;
301 push(item, priority);
308 const std::vector<heap_element>&
values()
const {
315 heap.push_back(std::make_pair(T(), Priority()));
320 bool remove(T item) {
322 typename index_map_type::iterator iter = index_map.find(item);
325 if(iter != index_map.end()) {
326 size_t i = iter->second;
331 index_map.erase(iter);
564 #endif // #ifndef TURI_MUTABLE_PRIORITY_QUEUE_HPP boost::unordered_map< T, size_t > index_map_type
The storage type of the index map.
bool less(size_t i, size_t j)
Compares the priorities at two heap locations.
void clear()
Clears all the values (equivalent to stl clear)
const std::pair< T, Priority > & top() const
Accesses the item with maximum priority in the queue.
void swap(size_t i, size_t j)
Swaps the heap locations of two elements.
const std::vector< heap_element > & values() const
Returns the values (key-priority pairs) in the priority queue.
bool insert_max(T item, Priority priority)
index_map_type index_map
The map used to map from items to indexes in the heap.
size_t parent(size_t i) const
Returns the index of the parent of the supplied index.
void update(T item, Priority priority)
void push(T item, Priority priority)
Enqueues a new item in the queue.
bool insert_cumulative(T item, Priority priority)
Priority priority_at(size_t i)
Extracts the priority at a heap location.
void push_or_update(T item, Priority priority)
size_t left(size_t i) const
Returns the index of the left child of the supplied index.
std::pair< T, Priority > pop()
std::pair< T, Priority > heap_element
An element of the heap.
bool empty() const
Returns true iff the queue is empty.
std::vector< heap_element > heap
The heap used to store the elements. The first element is unused.
bool contains(const T &item) const
Returns true if the queue contains the given value.
size_t right(size_t i) const
Returns the index of the right child of the supplied index.
mutable_queue()
Default constructor.
Priority operator[](T item) const
Returns the priority associated with a key.
size_t size() const
Returns the number of elements in the heap.
void heapify(size_t i)
The traditional heapify function.