/** * Updates the priority associated with a item in the queue. This * function fails if the item is not already present. */ void update(T item, Priority priority) { // Verify that the item is currently in the queue typename index_map_type::const_iterator iter = index_map.find(item); assert(iter != index_map.end()); // If it is already present update the priority size_t i = iter->second; heap[i].second = priority; while ((i > 1) && (priority_at(parent(i)) < priority)) { swap(i, parent(i)); i = parent(i); } heapify(i); }
//! Remove an item from the queue bool remove(T item) { // Ensure that the element is in the queue typename index_map_type::iterator iter = index_map.find(item); // only if the element is present in the first place do we need // remove it if(iter != index_map.end()) { size_t i = iter->second; swap(i, size()); heap.pop_back(); heapify(i); // erase the element from the index map index_map.erase(iter); return true; } return false; }
/** * If item is already in the queue, sets its priority to the sum * of the old priority and the new one. If the item is not in the queue, * adds it to the queue. * * returns true if the item was already present */ bool insert_cumulative(T item, Priority priority) { // determine if the item is already in the queue typename index_map_type::const_iterator iter = index_map.find(item); if(iter != index_map.end()) { // already present // If it is already present update the priority size_t i = iter->second; heap[i].second = priority + heap[i].second; // If the priority went up move the priority until its greater // than its parent while ((i > 1) && (priority_at(parent(i)) <= priority)) { swap(i, parent(i)); i = parent(i); } // Trickle down if necessary heapify(i); // This should not be necessary return false; } else { // not already present so simply add push(item, priority); return true; } } // end of insert cumulative
//! Returns the weight associated with a key Priority get(T item) const { typename index_map_type::const_iterator iter = index_map.find(item); assert(iter != index_map.end()); size_t i = iter->second; return heap[i].second; }