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