Changes in lemon/bin_heap.h [631:33c6b6e755cd:209:765619b7cbb2] in lemon
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/bin_heap.h
r631 r209 3 3 * This file is a part of LEMON, a generic C++ optimization library. 4 4 * 5 * Copyright (C) 2003-200 95 * Copyright (C) 2003-2008 6 6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 7 7 * (Egervary Research Group on Combinatorial Optimization, EGRES). … … 34 34 ///\brief A Binary Heap implementation. 35 35 /// 36 ///This class implements the \e binary \e heap data structure. 37 /// 38 ///A \e heap is a data structure for storing items with specified values 39 ///called \e priorities in such a way that finding the item with minimum 40 ///priority is efficient. \c Comp specifies the ordering of the priorities. 41 ///In a heap one can change the priority of an item, add or erase an 42 ///item, etc. 36 ///This class implements the \e binary \e heap data structure. A \e heap 37 ///is a data structure for storing items with specified values called \e 38 ///priorities in such a way that finding the item with minimum priority is 39 ///efficient. \c Compare specifies the ordering of the priorities. In a heap 40 ///one can change the priority of an item, add or erase an item, etc. 43 41 /// 44 ///\tparam PRType of the priority of the items.45 ///\tparam IM A read and writable item map with int values, used internally42 ///\tparam _Prio Type of the priority of the items. 43 ///\tparam _ItemIntMap A read and writable Item int map, used internally 46 44 ///to handle the cross references. 47 ///\tparam Comp A functor class for the ordering of the priorities.48 /// The default is \c std::less<PR>.45 ///\tparam _Compare A class for the ordering of the priorities. The 46 ///default is \c std::less<_Prio>. 49 47 /// 50 48 ///\sa FibHeap 51 49 ///\sa Dijkstra 52 template <typename PR, typename IM, typename Comp = std::less<PR> > 50 template <typename _Prio, typename _ItemIntMap, 51 typename _Compare = std::less<_Prio> > 53 52 class BinHeap { 54 53 55 54 public: 56 55 ///\e 57 typedef IMItemIntMap;58 ///\e 59 typedef PRPrio;56 typedef _ItemIntMap ItemIntMap; 57 ///\e 58 typedef _Prio Prio; 60 59 ///\e 61 60 typedef typename ItemIntMap::Key Item; … … 63 62 typedef std::pair<Item,Prio> Pair; 64 63 ///\e 65 typedef CompCompare;64 typedef _Compare Compare; 66 65 67 66 /// \brief Type to represent the items states. … … 71 70 /// heap's point of view, but may be useful to the user. 72 71 /// 73 /// The item-int map must be initialized in such way that it assigns74 /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.72 /// The ItemIntMap \e should be initialized in such way that it maps 73 /// PRE_HEAP (-1) to any element to be put in the heap... 75 74 enum State { 76 IN_HEAP = 0, ///< = 0.77 PRE_HEAP = -1, ///< = -1.78 POST_HEAP = -2 ///< = -2.75 IN_HEAP = 0, 76 PRE_HEAP = -1, 77 POST_HEAP = -2 79 78 }; 80 79 81 80 private: 82 std::vector<Pair> _data;83 Compare _comp;84 ItemIntMap & _iim;81 std::vector<Pair> data; 82 Compare comp; 83 ItemIntMap &iim; 85 84 86 85 public: … … 88 87 /// 89 88 /// The constructor. 90 /// \param map should be given to the constructor, since it is used 91 /// internally to handle the cross references. The value of the map 92 /// must be \c PRE_HEAP (<tt>-1</tt>) for every item. 93 explicit BinHeap(ItemIntMap &map) : _iim(map) {} 94 95 /// \brief The constructor. 96 /// 97 /// The constructor. 98 /// \param map should be given to the constructor, since it is used 89 /// \param _iim should be given to the constructor, since it is used 99 90 /// internally to handle the cross references. The value of the map 100 91 /// should be PRE_HEAP (-1) for each element. 101 /// 102 /// \param comp The comparator function object. 103 BinHeap(ItemIntMap &map, const Compare &comp) 104 : _iim(map), _comp(comp) {} 92 explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {} 93 94 /// \brief The constructor. 95 /// 96 /// The constructor. 97 /// \param _iim should be given to the constructor, since it is used 98 /// internally to handle the cross references. The value of the map 99 /// should be PRE_HEAP (-1) for each element. 100 /// 101 /// \param _comp The comparator function object. 102 BinHeap(ItemIntMap &_iim, const Compare &_comp) 103 : iim(_iim), comp(_comp) {} 105 104 106 105 … … 108 107 /// 109 108 /// \brief Returns the number of items stored in the heap. 110 int size() const { return _data.size(); }109 int size() const { return data.size(); } 111 110 112 111 /// \brief Checks if the heap stores no items. 113 112 /// 114 113 /// Returns \c true if and only if the heap stores no items. 115 bool empty() const { return _data.empty(); }114 bool empty() const { return data.empty(); } 116 115 117 116 /// \brief Make empty this heap. … … 122 121 /// each item to \c PRE_HEAP. 123 122 void clear() { 124 _data.clear();123 data.clear(); 125 124 } 126 125 … … 130 129 static int second_child(int i) { return 2*i+2; } 131 130 bool less(const Pair &p1, const Pair &p2) const { 132 return _comp(p1.second, p2.second);131 return comp(p1.second, p2.second); 133 132 } 134 133 135 134 int bubble_up(int hole, Pair p) { 136 135 int par = parent(hole); 137 while( hole>0 && less(p, _data[par]) ) {138 move( _data[par],hole);136 while( hole>0 && less(p,data[par]) ) { 137 move(data[par],hole); 139 138 hole = par; 140 139 par = parent(hole); … … 147 146 int child = second_child(hole); 148 147 while(child < length) { 149 if( less( _data[child-1], _data[child]) ) {148 if( less(data[child-1], data[child]) ) { 150 149 --child; 151 150 } 152 if( !less( _data[child], p) )151 if( !less(data[child], p) ) 153 152 goto ok; 154 move( _data[child], hole);153 move(data[child], hole); 155 154 hole = child; 156 155 child = second_child(hole); 157 156 } 158 157 child--; 159 if( child<length && less( _data[child], p) ) {160 move( _data[child], hole);158 if( child<length && less(data[child], p) ) { 159 move(data[child], hole); 161 160 hole=child; 162 161 } … … 167 166 168 167 void move(const Pair &p, int i) { 169 _data[i] = p;170 _iim.set(p.first, i);168 data[i] = p; 169 iim.set(p.first, i); 171 170 } 172 171 … … 177 176 /// \param p The pair to insert. 178 177 void push(const Pair &p) { 179 int n = _data.size();180 _data.resize(n+1);178 int n = data.size(); 179 data.resize(n+1); 181 180 bubble_up(n, p); 182 181 } … … 195 194 /// \pre The heap must be nonempty. 196 195 Item top() const { 197 return _data[0].first;196 return data[0].first; 198 197 } 199 198 … … 203 202 /// \pre The heap must be nonempty. 204 203 Prio prio() const { 205 return _data[0].second;204 return data[0].second; 206 205 } 207 206 … … 212 211 /// \pre The heap must be non-empty. 213 212 void pop() { 214 int n = _data.size()-1;215 _iim.set(_data[0].first, POST_HEAP);213 int n = data.size()-1; 214 iim.set(data[0].first, POST_HEAP); 216 215 if (n > 0) { 217 bubble_down(0, _data[n], n);218 } 219 _data.pop_back();216 bubble_down(0, data[n], n); 217 } 218 data.pop_back(); 220 219 } 221 220 … … 226 225 /// \pre The item should be in the heap. 227 226 void erase(const Item &i) { 228 int h = _iim[i];229 int n = _data.size()-1;230 _iim.set(_data[h].first, POST_HEAP);227 int h = iim[i]; 228 int n = data.size()-1; 229 iim.set(data[h].first, POST_HEAP); 231 230 if( h < n ) { 232 if ( bubble_up(h, _data[n]) == h) {233 bubble_down(h, _data[n], n);231 if ( bubble_up(h, data[n]) == h) { 232 bubble_down(h, data[n], n); 234 233 } 235 234 } 236 _data.pop_back();235 data.pop_back(); 237 236 } 238 237 … … 241 240 /// 242 241 /// This function returns the priority of item \c i. 243 /// \param i The item.244 242 /// \pre \c i must be in the heap. 243 /// \param i The item. 245 244 Prio operator[](const Item &i) const { 246 int idx = _iim[i];247 return _data[idx].second;245 int idx = iim[i]; 246 return data[idx].second; 248 247 } 249 248 … … 256 255 /// \param p The priority. 257 256 void set(const Item &i, const Prio &p) { 258 int idx = _iim[i];257 int idx = iim[i]; 259 258 if( idx < 0 ) { 260 259 push(i,p); 261 260 } 262 else if( _comp(p, _data[idx].second) ) {261 else if( comp(p, data[idx].second) ) { 263 262 bubble_up(idx, Pair(i,p)); 264 263 } 265 264 else { 266 bubble_down(idx, Pair(i,p), _data.size());265 bubble_down(idx, Pair(i,p), data.size()); 267 266 } 268 267 } … … 271 270 /// 272 271 /// This method decreases the priority of item \c i to \c p. 273 /// \param i The item.274 /// \param p The priority.275 272 /// \pre \c i must be stored in the heap with priority at least \c 276 273 /// p relative to \c Compare. 274 /// \param i The item. 275 /// \param p The priority. 277 276 void decrease(const Item &i, const Prio &p) { 278 int idx = _iim[i];277 int idx = iim[i]; 279 278 bubble_up(idx, Pair(i,p)); 280 279 } … … 283 282 /// 284 283 /// This method sets the priority of item \c i to \c p. 285 /// \param i The item.286 /// \param p The priority.287 284 /// \pre \c i must be stored in the heap with priority at most \c 288 285 /// p relative to \c Compare. 286 /// \param i The item. 287 /// \param p The priority. 289 288 void increase(const Item &i, const Prio &p) { 290 int idx = _iim[i];291 bubble_down(idx, Pair(i,p), _data.size());289 int idx = iim[i]; 290 bubble_down(idx, Pair(i,p), data.size()); 292 291 } 293 292 … … 301 300 /// \param i The item. 302 301 State state(const Item &i) const { 303 int s = _iim[i];302 int s = iim[i]; 304 303 if( s>=0 ) 305 304 s=0; … … 321 320 erase(i); 322 321 } 323 _iim[i] = st;322 iim[i] = st; 324 323 break; 325 324 case IN_HEAP: … … 335 334 /// with the same prioriority as prevoiusly the \c i item. 336 335 void replace(const Item& i, const Item& j) { 337 int idx = _iim[i];338 _iim.set(i, _iim[j]);339 _iim.set(j, idx);340 _data[idx].first = j;336 int idx = iim[i]; 337 iim.set(i, iim[j]); 338 iim.set(j, idx); 339 data[idx].first = j; 341 340 } 342 341
Note: See TracChangeset
for help on using the changeset viewer.