From 8a9558f06f10e4f6e03294277bf593a2dba37c70 Mon Sep 17 00:00:00 2001 From: Jeremiah Willcock Date: Wed, 4 Feb 2009 21:01:27 +0000 Subject: [PATCH] Added ability to check whether an element is in the heap, and do conditional insert/update operations based on that [SVN r51021] --- include/boost/graph/detail/d_ary_heap.hpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/boost/graph/detail/d_ary_heap.hpp b/include/boost/graph/detail/d_ary_heap.hpp index 6a249046..ffbaa47c 100644 --- a/include/boost/graph/detail/d_ary_heap.hpp +++ b/include/boost/graph/detail/d_ary_heap.hpp @@ -104,6 +104,7 @@ namespace boost { } void pop() { + put(index_in_heap, data[0], (size_type)(-1)); data[0] = data.back(); put(index_in_heap, data[0], 0); data.pop_back(); @@ -120,6 +121,21 @@ namespace boost { verify_heap(); } + bool contains(const Value& v) const { + return (index != (size_type)(-1)); + } + + void push_or_update(const Value& v) { /* insert if not present, else update */ + size_type index = get(index_in_heap, v); + if (index == (size_type)(-1)) { + index = data.size(); + data.push_back(v); + put(index_in_heap, v, index); + } + preserve_heap_property_up(index); + verify_heap(); + } + private: Compare compare; Container data; @@ -211,6 +227,7 @@ namespace boost { // From the root, swap elements (each one with its smallest child) if there // are any parent-child pairs that violate the heap property void preserve_heap_property_down() { + if (data.empty()) return; size_type index = 0; Value currently_being_moved = data[0]; distance_type currently_being_moved_dist =