1.1 --- a/lemon/bin_heap.h Sun Aug 02 12:40:20 2009 +0200
1.2 +++ b/lemon/bin_heap.h Fri Sep 25 09:13:03 2009 +0200
1.3 @@ -19,9 +19,9 @@
1.4 #ifndef LEMON_BIN_HEAP_H
1.5 #define LEMON_BIN_HEAP_H
1.6
1.7 -///\ingroup auxdat
1.8 +///\ingroup heaps
1.9 ///\file
1.10 -///\brief Binary Heap implementation.
1.11 +///\brief Binary heap implementation.
1.12
1.13 #include <vector>
1.14 #include <utility>
1.15 @@ -29,45 +29,41 @@
1.16
1.17 namespace lemon {
1.18
1.19 - ///\ingroup auxdat
1.20 + /// \ingroup heaps
1.21 ///
1.22 - ///\brief A Binary Heap implementation.
1.23 + /// \brief Binary heap data structure.
1.24 ///
1.25 - ///This class implements the \e binary \e heap data structure.
1.26 - ///
1.27 - ///A \e heap is a data structure for storing items with specified values
1.28 - ///called \e priorities in such a way that finding the item with minimum
1.29 - ///priority is efficient. \c Comp specifies the ordering of the priorities.
1.30 - ///In a heap one can change the priority of an item, add or erase an
1.31 - ///item, etc.
1.32 + /// This class implements the \e binary \e heap data structure.
1.33 + /// It fully conforms to the \ref concepts::Heap "heap concept".
1.34 ///
1.35 - ///\tparam PR Type of the priority of the items.
1.36 - ///\tparam IM A read and writable item map with int values, used internally
1.37 - ///to handle the cross references.
1.38 - ///\tparam Comp A functor class for the ordering of the priorities.
1.39 - ///The default is \c std::less<PR>.
1.40 - ///
1.41 - ///\sa FibHeap
1.42 - ///\sa Dijkstra
1.43 - template <typename PR, typename IM, typename Comp = std::less<PR> >
1.44 + /// \tparam PR Type of the priorities of the items.
1.45 + /// \tparam IM A read-writable item map with \c int values, used
1.46 + /// internally to handle the cross references.
1.47 + /// \tparam CMP A functor class for comparing the priorities.
1.48 + /// The default is \c std::less<PR>.
1.49 +#ifdef DOXYGEN
1.50 + template <typename PR, typename IM, typename CMP>
1.51 +#else
1.52 + template <typename PR, typename IM, typename CMP = std::less<PR> >
1.53 +#endif
1.54 class BinHeap {
1.55 + public:
1.56
1.57 - public:
1.58 - ///\e
1.59 + /// Type of the item-int map.
1.60 typedef IM ItemIntMap;
1.61 - ///\e
1.62 + /// Type of the priorities.
1.63 typedef PR Prio;
1.64 - ///\e
1.65 + /// Type of the items stored in the heap.
1.66 typedef typename ItemIntMap::Key Item;
1.67 - ///\e
1.68 + /// Type of the item-priority pairs.
1.69 typedef std::pair<Item,Prio> Pair;
1.70 - ///\e
1.71 - typedef Comp Compare;
1.72 + /// Functor type for comparing the priorities.
1.73 + typedef CMP Compare;
1.74
1.75 - /// \brief Type to represent the items states.
1.76 + /// \brief Type to represent the states of the items.
1.77 ///
1.78 - /// Each Item element have a state associated to it. It may be "in heap",
1.79 - /// "pre heap" or "post heap". The latter two are indifferent from the
1.80 + /// Each item has a state associated to it. It can be "in heap",
1.81 + /// "pre-heap" or "post-heap". The latter two are indifferent from the
1.82 /// heap's point of view, but may be useful to the user.
1.83 ///
1.84 /// The item-int map must be initialized in such way that it assigns
1.85 @@ -84,42 +80,43 @@
1.86 ItemIntMap &_iim;
1.87
1.88 public:
1.89 - /// \brief The constructor.
1.90 +
1.91 + /// \brief Constructor.
1.92 ///
1.93 - /// The constructor.
1.94 - /// \param map should be given to the constructor, since it is used
1.95 - /// internally to handle the cross references. The value of the map
1.96 - /// must be \c PRE_HEAP (<tt>-1</tt>) for every item.
1.97 + /// Constructor.
1.98 + /// \param map A map that assigns \c int values to the items.
1.99 + /// It is used internally to handle the cross references.
1.100 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.101 explicit BinHeap(ItemIntMap &map) : _iim(map) {}
1.102
1.103 - /// \brief The constructor.
1.104 + /// \brief Constructor.
1.105 ///
1.106 - /// The constructor.
1.107 - /// \param map should be given to the constructor, since it is used
1.108 - /// internally to handle the cross references. The value of the map
1.109 - /// should be PRE_HEAP (-1) for each element.
1.110 - ///
1.111 - /// \param comp The comparator function object.
1.112 + /// Constructor.
1.113 + /// \param map A map that assigns \c int values to the items.
1.114 + /// It is used internally to handle the cross references.
1.115 + /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
1.116 + /// \param comp The function object used for comparing the priorities.
1.117 BinHeap(ItemIntMap &map, const Compare &comp)
1.118 : _iim(map), _comp(comp) {}
1.119
1.120
1.121 - /// The number of items stored in the heap.
1.122 + /// \brief The number of items stored in the heap.
1.123 ///
1.124 - /// \brief Returns the number of items stored in the heap.
1.125 + /// This function returns the number of items stored in the heap.
1.126 int size() const { return _data.size(); }
1.127
1.128 - /// \brief Checks if the heap stores no items.
1.129 + /// \brief Check if the heap is empty.
1.130 ///
1.131 - /// Returns \c true if and only if the heap stores no items.
1.132 + /// This function returns \c true if the heap is empty.
1.133 bool empty() const { return _data.empty(); }
1.134
1.135 - /// \brief Make empty this heap.
1.136 + /// \brief Make the heap empty.
1.137 ///
1.138 - /// Make empty this heap. It does not change the cross reference map.
1.139 - /// If you want to reuse what is not surely empty you should first clear
1.140 - /// the heap and after that you should set the cross reference map for
1.141 - /// each item to \c PRE_HEAP.
1.142 + /// This functon makes the heap empty.
1.143 + /// It does not change the cross reference map. If you want to reuse
1.144 + /// a heap that is not surely empty, you should first clear it and
1.145 + /// then you should set the cross reference map to \c PRE_HEAP
1.146 + /// for each item.
1.147 void clear() {
1.148 _data.clear();
1.149 }
1.150 @@ -127,12 +124,12 @@
1.151 private:
1.152 static int parent(int i) { return (i-1)/2; }
1.153
1.154 - static int second_child(int i) { return 2*i+2; }
1.155 + static int secondChild(int i) { return 2*i+2; }
1.156 bool less(const Pair &p1, const Pair &p2) const {
1.157 return _comp(p1.second, p2.second);
1.158 }
1.159
1.160 - int bubble_up(int hole, Pair p) {
1.161 + int bubbleUp(int hole, Pair p) {
1.162 int par = parent(hole);
1.163 while( hole>0 && less(p,_data[par]) ) {
1.164 move(_data[par],hole);
1.165 @@ -143,8 +140,8 @@
1.166 return hole;
1.167 }
1.168
1.169 - int bubble_down(int hole, Pair p, int length) {
1.170 - int child = second_child(hole);
1.171 + int bubbleDown(int hole, Pair p, int length) {
1.172 + int child = secondChild(hole);
1.173 while(child < length) {
1.174 if( less(_data[child-1], _data[child]) ) {
1.175 --child;
1.176 @@ -153,7 +150,7 @@
1.177 goto ok;
1.178 move(_data[child], hole);
1.179 hole = child;
1.180 - child = second_child(hole);
1.181 + child = secondChild(hole);
1.182 }
1.183 child--;
1.184 if( child<length && less(_data[child], p) ) {
1.185 @@ -171,87 +168,91 @@
1.186 }
1.187
1.188 public:
1.189 +
1.190 /// \brief Insert a pair of item and priority into the heap.
1.191 ///
1.192 - /// Adds \c p.first to the heap with priority \c p.second.
1.193 + /// This function inserts \c p.first to the heap with priority
1.194 + /// \c p.second.
1.195 /// \param p The pair to insert.
1.196 + /// \pre \c p.first must not be stored in the heap.
1.197 void push(const Pair &p) {
1.198 int n = _data.size();
1.199 _data.resize(n+1);
1.200 - bubble_up(n, p);
1.201 + bubbleUp(n, p);
1.202 }
1.203
1.204 - /// \brief Insert an item into the heap with the given heap.
1.205 + /// \brief Insert an item into the heap with the given priority.
1.206 ///
1.207 - /// Adds \c i to the heap with priority \c p.
1.208 + /// This function inserts the given item into the heap with the
1.209 + /// given priority.
1.210 /// \param i The item to insert.
1.211 /// \param p The priority of the item.
1.212 + /// \pre \e i must not be stored in the heap.
1.213 void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
1.214
1.215 - /// \brief Returns the item with minimum priority relative to \c Compare.
1.216 + /// \brief Return the item having minimum priority.
1.217 ///
1.218 - /// This method returns the item with minimum priority relative to \c
1.219 - /// Compare.
1.220 - /// \pre The heap must be nonempty.
1.221 + /// This function returns the item having minimum priority.
1.222 + /// \pre The heap must be non-empty.
1.223 Item top() const {
1.224 return _data[0].first;
1.225 }
1.226
1.227 - /// \brief Returns the minimum priority relative to \c Compare.
1.228 + /// \brief The minimum priority.
1.229 ///
1.230 - /// It returns the minimum priority relative to \c Compare.
1.231 - /// \pre The heap must be nonempty.
1.232 + /// This function returns the minimum priority.
1.233 + /// \pre The heap must be non-empty.
1.234 Prio prio() const {
1.235 return _data[0].second;
1.236 }
1.237
1.238 - /// \brief Deletes the item with minimum priority relative to \c Compare.
1.239 + /// \brief Remove the item having minimum priority.
1.240 ///
1.241 - /// This method deletes the item with minimum priority relative to \c
1.242 - /// Compare from the heap.
1.243 + /// This function removes the item having minimum priority.
1.244 /// \pre The heap must be non-empty.
1.245 void pop() {
1.246 int n = _data.size()-1;
1.247 _iim.set(_data[0].first, POST_HEAP);
1.248 if (n > 0) {
1.249 - bubble_down(0, _data[n], n);
1.250 + bubbleDown(0, _data[n], n);
1.251 }
1.252 _data.pop_back();
1.253 }
1.254
1.255 - /// \brief Deletes \c i from the heap.
1.256 + /// \brief Remove the given item from the heap.
1.257 ///
1.258 - /// This method deletes item \c i from the heap.
1.259 - /// \param i The item to erase.
1.260 - /// \pre The item should be in the heap.
1.261 + /// This function removes the given item from the heap if it is
1.262 + /// already stored.
1.263 + /// \param i The item to delete.
1.264 + /// \pre \e i must be in the heap.
1.265 void erase(const Item &i) {
1.266 int h = _iim[i];
1.267 int n = _data.size()-1;
1.268 _iim.set(_data[h].first, POST_HEAP);
1.269 if( h < n ) {
1.270 - if ( bubble_up(h, _data[n]) == h) {
1.271 - bubble_down(h, _data[n], n);
1.272 + if ( bubbleUp(h, _data[n]) == h) {
1.273 + bubbleDown(h, _data[n], n);
1.274 }
1.275 }
1.276 _data.pop_back();
1.277 }
1.278
1.279 -
1.280 - /// \brief Returns the priority of \c i.
1.281 + /// \brief The priority of the given item.
1.282 ///
1.283 - /// This function returns the priority of item \c i.
1.284 + /// This function returns the priority of the given item.
1.285 /// \param i The item.
1.286 - /// \pre \c i must be in the heap.
1.287 + /// \pre \e i must be in the heap.
1.288 Prio operator[](const Item &i) const {
1.289 int idx = _iim[i];
1.290 return _data[idx].second;
1.291 }
1.292
1.293 - /// \brief \c i gets to the heap with priority \c p independently
1.294 - /// if \c i was already there.
1.295 + /// \brief Set the priority of an item or insert it, if it is
1.296 + /// not stored in the heap.
1.297 ///
1.298 - /// This method calls \ref push(\c i, \c p) if \c i is not stored
1.299 - /// in the heap and sets the priority of \c i to \c p otherwise.
1.300 + /// This method sets the priority of the given item if it is
1.301 + /// already stored in the heap. Otherwise it inserts the given
1.302 + /// item into the heap with the given priority.
1.303 /// \param i The item.
1.304 /// \param p The priority.
1.305 void set(const Item &i, const Prio &p) {
1.306 @@ -260,44 +261,42 @@
1.307 push(i,p);
1.308 }
1.309 else if( _comp(p, _data[idx].second) ) {
1.310 - bubble_up(idx, Pair(i,p));
1.311 + bubbleUp(idx, Pair(i,p));
1.312 }
1.313 else {
1.314 - bubble_down(idx, Pair(i,p), _data.size());
1.315 + bubbleDown(idx, Pair(i,p), _data.size());
1.316 }
1.317 }
1.318
1.319 - /// \brief Decreases the priority of \c i to \c p.
1.320 + /// \brief Decrease the priority of an item to the given value.
1.321 ///
1.322 - /// This method decreases the priority of item \c i to \c p.
1.323 + /// This function decreases the priority of an item to the given value.
1.324 /// \param i The item.
1.325 /// \param p The priority.
1.326 - /// \pre \c i must be stored in the heap with priority at least \c
1.327 - /// p relative to \c Compare.
1.328 + /// \pre \e i must be stored in the heap with priority at least \e p.
1.329 void decrease(const Item &i, const Prio &p) {
1.330 int idx = _iim[i];
1.331 - bubble_up(idx, Pair(i,p));
1.332 + bubbleUp(idx, Pair(i,p));
1.333 }
1.334
1.335 - /// \brief Increases the priority of \c i to \c p.
1.336 + /// \brief Increase the priority of an item to the given value.
1.337 ///
1.338 - /// This method sets the priority of item \c i to \c p.
1.339 + /// This function increases the priority of an item to the given value.
1.340 /// \param i The item.
1.341 /// \param p The priority.
1.342 - /// \pre \c i must be stored in the heap with priority at most \c
1.343 - /// p relative to \c Compare.
1.344 + /// \pre \e i must be stored in the heap with priority at most \e p.
1.345 void increase(const Item &i, const Prio &p) {
1.346 int idx = _iim[i];
1.347 - bubble_down(idx, Pair(i,p), _data.size());
1.348 + bubbleDown(idx, Pair(i,p), _data.size());
1.349 }
1.350
1.351 - /// \brief Returns if \c item is in, has already been in, or has
1.352 - /// never been in the heap.
1.353 + /// \brief Return the state of an item.
1.354 ///
1.355 - /// This method returns PRE_HEAP if \c item has never been in the
1.356 - /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
1.357 - /// otherwise. In the latter case it is possible that \c item will
1.358 - /// get back to the heap again.
1.359 + /// This method returns \c PRE_HEAP if the given item has never
1.360 + /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
1.361 + /// and \c POST_HEAP otherwise.
1.362 + /// In the latter case it is possible that the item will get back
1.363 + /// to the heap again.
1.364 /// \param i The item.
1.365 State state(const Item &i) const {
1.366 int s = _iim[i];
1.367 @@ -306,11 +305,11 @@
1.368 return State(s);
1.369 }
1.370
1.371 - /// \brief Sets the state of the \c item in the heap.
1.372 + /// \brief Set the state of an item in the heap.
1.373 ///
1.374 - /// Sets the state of the \c item in the heap. It can be used to
1.375 - /// manually clear the heap when it is important to achive the
1.376 - /// better time complexity.
1.377 + /// This function sets the state of the given item in the heap.
1.378 + /// It can be used to manually clear the heap when it is important
1.379 + /// to achive better time complexity.
1.380 /// \param i The item.
1.381 /// \param st The state. It should not be \c IN_HEAP.
1.382 void state(const Item& i, State st) {
1.383 @@ -327,12 +326,13 @@
1.384 }
1.385 }
1.386
1.387 - /// \brief Replaces an item in the heap.
1.388 + /// \brief Replace an item in the heap.
1.389 ///
1.390 - /// The \c i item is replaced with \c j item. The \c i item should
1.391 - /// be in the heap, while the \c j should be out of the heap. The
1.392 - /// \c i item will out of the heap and \c j will be in the heap
1.393 - /// with the same prioriority as prevoiusly the \c i item.
1.394 + /// This function replaces item \c i with item \c j.
1.395 + /// Item \c i must be in the heap, while \c j must be out of the heap.
1.396 + /// After calling this method, item \c i will be out of the
1.397 + /// heap and \c j will be in the heap with the same prioriority
1.398 + /// as item \c i had before.
1.399 void replace(const Item& i, const Item& j) {
1.400 int idx = _iim[i];
1.401 _iim.set(i, _iim[j]);