Changeset 2547:f393a8162688 in lemon-0.x for lemon/bin_heap.h
- Timestamp:
- 12/27/07 14:40:16 (16 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3424
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/bin_heap.h
r2546 r2547 40 40 ///one can change the priority of an item, add or erase an item, etc. 41 41 /// 42 ///\param Prio Type of the priority of the items.43 ///\param ItemIntMap A read and writable Item int map, used internally42 ///\param _Prio Type of the priority of the items. 43 ///\param _ItemIntMap A read and writable Item int map, used internally 44 44 ///to handle the cross references. 45 ///\param Compare A class for the ordering of the priorities. The46 ///default is \c std::less< Prio>.45 ///\param _Compare A class for the ordering of the priorities. The 46 ///default is \c std::less<_Prio>. 47 47 /// 48 48 ///\sa FibHeap 49 49 ///\sa Dijkstra 50 template <typename Prio, typenameItemIntMap,51 typename Compare = std::less<Prio> >50 template <typename _Prio, typename _ItemIntMap, 51 typename _Compare = std::less<_Prio> > 52 52 class BinHeap { 53 53 54 54 public: 55 typedef typename ItemIntMap::Key ItemType;56 typedef Prio PrioType;57 typedef std::pair<ItemType,PrioType> PairType;58 typedef ItemIntMap ItemIntMapType;59 typedef Compare PrioCompare;55 typedef _ItemIntMap ItemIntMap; 56 typedef _Prio Prio; 57 typedef typename ItemIntMap::Key Item; 58 typedef std::pair<Item,Prio> Pair; 59 typedef _Compare Compare; 60 60 61 61 /// \brief Type to represent the items states. … … 67 67 /// The ItemIntMap \e should be initialized in such way that it maps 68 68 /// PRE_HEAP (-1) to any element to be put in the heap... 69 enum state_enum{69 enum State { 70 70 IN_HEAP = 0, 71 71 PRE_HEAP = -1, … … 74 74 75 75 private: 76 std::vector<Pair Type> data;76 std::vector<Pair> data; 77 77 Compare comp; 78 78 ItemIntMap &iim; … … 123 123 124 124 static int second_child(int i) { return 2*i+2; } 125 bool less(const Pair Type &p1, const PairType&p2) const {125 bool less(const Pair &p1, const Pair &p2) const { 126 126 return comp(p1.second, p2.second); 127 127 } 128 128 129 int bubble_up(int hole, Pair Typep) {129 int bubble_up(int hole, Pair p) { 130 130 int par = parent(hole); 131 131 while( hole>0 && less(p,data[par]) ) { … … 138 138 } 139 139 140 int bubble_down(int hole, Pair Typep, int length) {140 int bubble_down(int hole, Pair p, int length) { 141 141 int child = second_child(hole); 142 142 while(child < length) { … … 160 160 } 161 161 162 void move(const Pair Type&p, int i) {162 void move(const Pair &p, int i) { 163 163 data[i] = p; 164 164 iim.set(p.first, i); … … 170 170 /// Adds \c p.first to the heap with priority \c p.second. 171 171 /// \param p The pair to insert. 172 void push(const Pair Type&p) {172 void push(const Pair &p) { 173 173 int n = data.size(); 174 174 data.resize(n+1); … … 181 181 /// \param i The item to insert. 182 182 /// \param p The priority of the item. 183 void push(const Item Type &i, const Prio &p) { push(PairType(i,p)); }183 void push(const Item &i, const Prio &p) { push(Pair(i,p)); } 184 184 185 185 /// \brief Returns the item with minimum priority relative to \c Compare. … … 188 188 /// Compare. 189 189 /// \pre The heap must be nonempty. 190 Item Typetop() const {190 Item top() const { 191 191 return data[0].first; 192 192 } … … 219 219 /// \param i The item to erase. 220 220 /// \pre The item should be in the heap. 221 void erase(const Item Type&i) {221 void erase(const Item &i) { 222 222 int h = iim[i]; 223 223 int n = data.size()-1; … … 237 237 /// \pre \c i must be in the heap. 238 238 /// \param i The item. 239 Prio operator[](const Item Type&i) const {239 Prio operator[](const Item &i) const { 240 240 int idx = iim[i]; 241 241 return data[idx].second; … … 249 249 /// \param i The item. 250 250 /// \param p The priority. 251 void set(const Item Type&i, const Prio &p) {251 void set(const Item &i, const Prio &p) { 252 252 int idx = iim[i]; 253 253 if( idx < 0 ) { … … 255 255 } 256 256 else if( comp(p, data[idx].second) ) { 257 bubble_up(idx, Pair Type(i,p));257 bubble_up(idx, Pair(i,p)); 258 258 } 259 259 else { 260 bubble_down(idx, Pair Type(i,p), data.size());260 bubble_down(idx, Pair(i,p), data.size()); 261 261 } 262 262 } … … 269 269 /// \param i The item. 270 270 /// \param p The priority. 271 void decrease(const Item Type&i, const Prio &p) {271 void decrease(const Item &i, const Prio &p) { 272 272 int idx = iim[i]; 273 bubble_up(idx, Pair Type(i,p));273 bubble_up(idx, Pair(i,p)); 274 274 } 275 275 … … 281 281 /// \param i The item. 282 282 /// \param p The priority. 283 void increase(const Item Type&i, const Prio &p) {283 void increase(const Item &i, const Prio &p) { 284 284 int idx = iim[i]; 285 bubble_down(idx, Pair Type(i,p), data.size());285 bubble_down(idx, Pair(i,p), data.size()); 286 286 } 287 287 … … 294 294 /// get back to the heap again. 295 295 /// \param i The item. 296 state_enum state(const ItemType&i) const {296 State state(const Item &i) const { 297 297 int s = iim[i]; 298 298 if( s>=0 ) 299 299 s=0; 300 return state_enum(s);300 return State(s); 301 301 } 302 302 … … 308 308 /// \param i The item. 309 309 /// \param st The state. It should not be \c IN_HEAP. 310 void state(const Item Type& i, state_enumst) {310 void state(const Item& i, State st) { 311 311 switch (st) { 312 312 case POST_HEAP:
Note: See TracChangeset
for help on using the changeset viewer.