lemon/bin_heap.h
changeset 606 c5fd2d996909
parent 463 88ed40ad0d4f
child 631 33c6b6e755cd
     1.1 --- a/lemon/bin_heap.h	Thu Mar 05 10:13:20 2009 +0000
     1.2 +++ b/lemon/bin_heap.h	Sun Mar 29 23:08:20 2009 +0200
     1.3 @@ -33,35 +33,36 @@
     1.4    ///
     1.5    ///\brief A Binary Heap implementation.
     1.6    ///
     1.7 -  ///This class implements the \e binary \e heap data structure. A \e heap
     1.8 -  ///is a data structure for storing items with specified values called \e
     1.9 -  ///priorities in such a way that finding the item with minimum priority is
    1.10 -  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
    1.11 -  ///one can change the priority of an item, add or erase an item, etc.
    1.12 +  ///This class implements the \e binary \e heap data structure. 
    1.13 +  /// 
    1.14 +  ///A \e heap is a data structure for storing items with specified values
    1.15 +  ///called \e priorities in such a way that finding the item with minimum
    1.16 +  ///priority is efficient. \c Comp specifies the ordering of the priorities.
    1.17 +  ///In a heap one can change the priority of an item, add or erase an
    1.18 +  ///item, etc.
    1.19    ///
    1.20 -  ///\tparam _Prio Type of the priority of the items.
    1.21 -  ///\tparam _ItemIntMap A read and writable Item int map, used internally
    1.22 +  ///\tparam PR Type of the priority of the items.
    1.23 +  ///\tparam IM A read and writable item map with int values, used internally
    1.24    ///to handle the cross references.
    1.25 -  ///\tparam _Compare A class for the ordering of the priorities. The
    1.26 -  ///default is \c std::less<_Prio>.
    1.27 +  ///\tparam Comp A functor class for the ordering of the priorities.
    1.28 +  ///The default is \c std::less<PR>.
    1.29    ///
    1.30    ///\sa FibHeap
    1.31    ///\sa Dijkstra
    1.32 -  template <typename _Prio, typename _ItemIntMap,
    1.33 -            typename _Compare = std::less<_Prio> >
    1.34 +  template <typename PR, typename IM, typename Comp = std::less<PR> >
    1.35    class BinHeap {
    1.36  
    1.37    public:
    1.38      ///\e
    1.39 -    typedef _ItemIntMap ItemIntMap;
    1.40 +    typedef IM ItemIntMap;
    1.41      ///\e
    1.42 -    typedef _Prio Prio;
    1.43 +    typedef PR Prio;
    1.44      ///\e
    1.45      typedef typename ItemIntMap::Key Item;
    1.46      ///\e
    1.47      typedef std::pair<Item,Prio> Pair;
    1.48      ///\e
    1.49 -    typedef _Compare Compare;
    1.50 +    typedef Comp Compare;
    1.51  
    1.52      /// \brief Type to represent the items states.
    1.53      ///
    1.54 @@ -69,49 +70,49 @@
    1.55      /// "pre heap" or "post heap". The latter two are indifferent from the
    1.56      /// heap's point of view, but may be useful to the user.
    1.57      ///
    1.58 -    /// The ItemIntMap \e should be initialized in such way that it maps
    1.59 -    /// PRE_HEAP (-1) to any element to be put in the heap...
    1.60 +    /// The item-int map must be initialized in such way that it assigns
    1.61 +    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
    1.62      enum State {
    1.63 -      IN_HEAP = 0,
    1.64 -      PRE_HEAP = -1,
    1.65 -      POST_HEAP = -2
    1.66 +      IN_HEAP = 0,    ///< \e
    1.67 +      PRE_HEAP = -1,  ///< \e
    1.68 +      POST_HEAP = -2  ///< \e
    1.69      };
    1.70  
    1.71    private:
    1.72 -    std::vector<Pair> data;
    1.73 -    Compare comp;
    1.74 -    ItemIntMap &iim;
    1.75 +    std::vector<Pair> _data;
    1.76 +    Compare _comp;
    1.77 +    ItemIntMap &_iim;
    1.78  
    1.79    public:
    1.80      /// \brief The constructor.
    1.81      ///
    1.82      /// The constructor.
    1.83 -    /// \param _iim should be given to the constructor, since it is used
    1.84 +    /// \param map should be given to the constructor, since it is used
    1.85      /// internally to handle the cross references. The value of the map
    1.86 -    /// should be PRE_HEAP (-1) for each element.
    1.87 -    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
    1.88 +    /// must be \c PRE_HEAP (<tt>-1</tt>) for every item.
    1.89 +    explicit BinHeap(ItemIntMap &map) : _iim(map) {}
    1.90  
    1.91      /// \brief The constructor.
    1.92      ///
    1.93      /// The constructor.
    1.94 -    /// \param _iim should be given to the constructor, since it is used
    1.95 +    /// \param map should be given to the constructor, since it is used
    1.96      /// internally to handle the cross references. The value of the map
    1.97      /// should be PRE_HEAP (-1) for each element.
    1.98      ///
    1.99 -    /// \param _comp The comparator function object.
   1.100 -    BinHeap(ItemIntMap &_iim, const Compare &_comp)
   1.101 -      : iim(_iim), comp(_comp) {}
   1.102 +    /// \param comp The comparator function object.
   1.103 +    BinHeap(ItemIntMap &map, const Compare &comp)
   1.104 +      : _iim(map), _comp(comp) {}
   1.105  
   1.106  
   1.107      /// The number of items stored in the heap.
   1.108      ///
   1.109      /// \brief Returns the number of items stored in the heap.
   1.110 -    int size() const { return data.size(); }
   1.111 +    int size() const { return _data.size(); }
   1.112  
   1.113      /// \brief Checks if the heap stores no items.
   1.114      ///
   1.115      /// Returns \c true if and only if the heap stores no items.
   1.116 -    bool empty() const { return data.empty(); }
   1.117 +    bool empty() const { return _data.empty(); }
   1.118  
   1.119      /// \brief Make empty this heap.
   1.120      ///
   1.121 @@ -120,7 +121,7 @@
   1.122      /// the heap and after that you should set the cross reference map for
   1.123      /// each item to \c PRE_HEAP.
   1.124      void clear() {
   1.125 -      data.clear();
   1.126 +      _data.clear();
   1.127      }
   1.128  
   1.129    private:
   1.130 @@ -128,13 +129,13 @@
   1.131  
   1.132      static int second_child(int i) { return 2*i+2; }
   1.133      bool less(const Pair &p1, const Pair &p2) const {
   1.134 -      return comp(p1.second, p2.second);
   1.135 +      return _comp(p1.second, p2.second);
   1.136      }
   1.137  
   1.138      int bubble_up(int hole, Pair p) {
   1.139        int par = parent(hole);
   1.140 -      while( hole>0 && less(p,data[par]) ) {
   1.141 -        move(data[par],hole);
   1.142 +      while( hole>0 && less(p,_data[par]) ) {
   1.143 +        move(_data[par],hole);
   1.144          hole = par;
   1.145          par = parent(hole);
   1.146        }
   1.147 @@ -145,18 +146,18 @@
   1.148      int bubble_down(int hole, Pair p, int length) {
   1.149        int child = second_child(hole);
   1.150        while(child < length) {
   1.151 -        if( less(data[child-1], data[child]) ) {
   1.152 +        if( less(_data[child-1], _data[child]) ) {
   1.153            --child;
   1.154          }
   1.155 -        if( !less(data[child], p) )
   1.156 +        if( !less(_data[child], p) )
   1.157            goto ok;
   1.158 -        move(data[child], hole);
   1.159 +        move(_data[child], hole);
   1.160          hole = child;
   1.161          child = second_child(hole);
   1.162        }
   1.163        child--;
   1.164 -      if( child<length && less(data[child], p) ) {
   1.165 -        move(data[child], hole);
   1.166 +      if( child<length && less(_data[child], p) ) {
   1.167 +        move(_data[child], hole);
   1.168          hole=child;
   1.169        }
   1.170      ok:
   1.171 @@ -165,8 +166,8 @@
   1.172      }
   1.173  
   1.174      void move(const Pair &p, int i) {
   1.175 -      data[i] = p;
   1.176 -      iim.set(p.first, i);
   1.177 +      _data[i] = p;
   1.178 +      _iim.set(p.first, i);
   1.179      }
   1.180  
   1.181    public:
   1.182 @@ -175,8 +176,8 @@
   1.183      /// Adds \c p.first to the heap with priority \c p.second.
   1.184      /// \param p The pair to insert.
   1.185      void push(const Pair &p) {
   1.186 -      int n = data.size();
   1.187 -      data.resize(n+1);
   1.188 +      int n = _data.size();
   1.189 +      _data.resize(n+1);
   1.190        bubble_up(n, p);
   1.191      }
   1.192  
   1.193 @@ -193,7 +194,7 @@
   1.194      /// Compare.
   1.195      /// \pre The heap must be nonempty.
   1.196      Item top() const {
   1.197 -      return data[0].first;
   1.198 +      return _data[0].first;
   1.199      }
   1.200  
   1.201      /// \brief Returns the minimum priority relative to \c Compare.
   1.202 @@ -201,7 +202,7 @@
   1.203      /// It returns the minimum priority relative to \c Compare.
   1.204      /// \pre The heap must be nonempty.
   1.205      Prio prio() const {
   1.206 -      return data[0].second;
   1.207 +      return _data[0].second;
   1.208      }
   1.209  
   1.210      /// \brief Deletes the item with minimum priority relative to \c Compare.
   1.211 @@ -210,12 +211,12 @@
   1.212      /// Compare from the heap.
   1.213      /// \pre The heap must be non-empty.
   1.214      void pop() {
   1.215 -      int n = data.size()-1;
   1.216 -      iim.set(data[0].first, POST_HEAP);
   1.217 +      int n = _data.size()-1;
   1.218 +      _iim.set(_data[0].first, POST_HEAP);
   1.219        if (n > 0) {
   1.220 -        bubble_down(0, data[n], n);
   1.221 +        bubble_down(0, _data[n], n);
   1.222        }
   1.223 -      data.pop_back();
   1.224 +      _data.pop_back();
   1.225      }
   1.226  
   1.227      /// \brief Deletes \c i from the heap.
   1.228 @@ -224,26 +225,26 @@
   1.229      /// \param i The item to erase.
   1.230      /// \pre The item should be in the heap.
   1.231      void erase(const Item &i) {
   1.232 -      int h = iim[i];
   1.233 -      int n = data.size()-1;
   1.234 -      iim.set(data[h].first, POST_HEAP);
   1.235 +      int h = _iim[i];
   1.236 +      int n = _data.size()-1;
   1.237 +      _iim.set(_data[h].first, POST_HEAP);
   1.238        if( h < n ) {
   1.239 -        if ( bubble_up(h, data[n]) == h) {
   1.240 -          bubble_down(h, data[n], n);
   1.241 +        if ( bubble_up(h, _data[n]) == h) {
   1.242 +          bubble_down(h, _data[n], n);
   1.243          }
   1.244        }
   1.245 -      data.pop_back();
   1.246 +      _data.pop_back();
   1.247      }
   1.248  
   1.249  
   1.250      /// \brief Returns the priority of \c i.
   1.251      ///
   1.252      /// This function returns the priority of item \c i.
   1.253 +    /// \param i The item.
   1.254      /// \pre \c i must be in the heap.
   1.255 -    /// \param i The item.
   1.256      Prio operator[](const Item &i) const {
   1.257 -      int idx = iim[i];
   1.258 -      return data[idx].second;
   1.259 +      int idx = _iim[i];
   1.260 +      return _data[idx].second;
   1.261      }
   1.262  
   1.263      /// \brief \c i gets to the heap with priority \c p independently
   1.264 @@ -254,40 +255,40 @@
   1.265      /// \param i The item.
   1.266      /// \param p The priority.
   1.267      void set(const Item &i, const Prio &p) {
   1.268 -      int idx = iim[i];
   1.269 +      int idx = _iim[i];
   1.270        if( idx < 0 ) {
   1.271          push(i,p);
   1.272        }
   1.273 -      else if( comp(p, data[idx].second) ) {
   1.274 +      else if( _comp(p, _data[idx].second) ) {
   1.275          bubble_up(idx, Pair(i,p));
   1.276        }
   1.277        else {
   1.278 -        bubble_down(idx, Pair(i,p), data.size());
   1.279 +        bubble_down(idx, Pair(i,p), _data.size());
   1.280        }
   1.281      }
   1.282  
   1.283      /// \brief Decreases the priority of \c i to \c p.
   1.284      ///
   1.285      /// This method decreases the priority of item \c i to \c p.
   1.286 +    /// \param i The item.
   1.287 +    /// \param p The priority.
   1.288      /// \pre \c i must be stored in the heap with priority at least \c
   1.289      /// p relative to \c Compare.
   1.290 -    /// \param i The item.
   1.291 -    /// \param p The priority.
   1.292      void decrease(const Item &i, const Prio &p) {
   1.293 -      int idx = iim[i];
   1.294 +      int idx = _iim[i];
   1.295        bubble_up(idx, Pair(i,p));
   1.296      }
   1.297  
   1.298      /// \brief Increases the priority of \c i to \c p.
   1.299      ///
   1.300      /// This method sets the priority of item \c i to \c p.
   1.301 +    /// \param i The item.
   1.302 +    /// \param p The priority.
   1.303      /// \pre \c i must be stored in the heap with priority at most \c
   1.304      /// p relative to \c Compare.
   1.305 -    /// \param i The item.
   1.306 -    /// \param p The priority.
   1.307      void increase(const Item &i, const Prio &p) {
   1.308 -      int idx = iim[i];
   1.309 -      bubble_down(idx, Pair(i,p), data.size());
   1.310 +      int idx = _iim[i];
   1.311 +      bubble_down(idx, Pair(i,p), _data.size());
   1.312      }
   1.313  
   1.314      /// \brief Returns if \c item is in, has already been in, or has
   1.315 @@ -299,7 +300,7 @@
   1.316      /// get back to the heap again.
   1.317      /// \param i The item.
   1.318      State state(const Item &i) const {
   1.319 -      int s = iim[i];
   1.320 +      int s = _iim[i];
   1.321        if( s>=0 )
   1.322          s=0;
   1.323        return State(s);
   1.324 @@ -319,7 +320,7 @@
   1.325          if (state(i) == IN_HEAP) {
   1.326            erase(i);
   1.327          }
   1.328 -        iim[i] = st;
   1.329 +        _iim[i] = st;
   1.330          break;
   1.331        case IN_HEAP:
   1.332          break;
   1.333 @@ -333,10 +334,10 @@
   1.334      /// \c i item will out of the heap and \c j will be in the heap
   1.335      /// with the same prioriority as prevoiusly the \c i item.
   1.336      void replace(const Item& i, const Item& j) {
   1.337 -      int idx = iim[i];
   1.338 -      iim.set(i, iim[j]);
   1.339 -      iim.set(j, idx);
   1.340 -      data[idx].first = j;
   1.341 +      int idx = _iim[i];
   1.342 +      _iim.set(i, _iim[j]);
   1.343 +      _iim.set(j, idx);
   1.344 +      _data[idx].first = j;
   1.345      }
   1.346  
   1.347    }; // class BinHeap