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