Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

bin_heap.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * src/lemon/bin_heap.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Combinatorial Optimization Research Group, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_BIN_HEAP_H
00018 #define LEMON_BIN_HEAP_H
00019 
00024 
00025 #include <vector>
00026 #include <utility>
00027 #include <functional>
00028 
00029 namespace lemon {
00030 
00033 
00035   
00040   template <typename Item, typename Prio, typename ItemIntMap,
00041             typename Compare = std::less<Prio> >
00042   class BinHeap {
00043 
00044   public:
00045     typedef Item                             ItemType;
00046     // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
00047     typedef Prio                             PrioType;
00048     typedef std::pair<ItemType,PrioType>     PairType;
00049     typedef ItemIntMap                       ItemIntMapType;
00050     typedef Compare                          PrioCompare;
00051 
00060 
00061 
00062     enum state_enum {
00063       IN_HEAP = 0,
00064       PRE_HEAP = -1,
00065       POST_HEAP = -2
00066     };
00067 
00068   private:
00069     std::vector<PairType> data;
00070     Compare comp;
00071     // FIXME: jo ez igy???
00072     ItemIntMap &iim;
00073 
00074   public:
00076     BinHeap(ItemIntMap &_iim) : iim(_iim) {}
00078     BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
00079 
00080 
00082     int size() const { return data.size(); }
00084     bool empty() const { return data.empty(); }
00085 
00086   private:
00087     static int parent(int i) { return (i-1)/2; }
00088     static int second_child(int i) { return 2*i+2; }
00089     bool less(const PairType &p1, const PairType &p2) const {
00090       return comp(p1.second, p2.second);
00091     }
00092 
00093     int bubble_up(int hole, PairType p);
00094     int bubble_down(int hole, PairType p, int length);
00095 
00096     void move(const PairType &p, int i) {
00097       data[i] = p;
00098       iim.set(p.first, i);
00099     }
00100 
00101     void rmidx(int h) {
00102       int n = data.size()-1;
00103       if( h>=0 && h<=n ) {
00104         iim.set(data[h].first, POST_HEAP);
00105         if ( h<n ) {
00106           bubble_down(h, data[n], n);
00107         }
00108         data.pop_back();
00109       }
00110     }
00111 
00112   public:
00114     void push(const PairType &p) {
00115       int n = data.size();
00116       data.resize(n+1);
00117       bubble_up(n, p);
00118     }
00120     void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
00121 
00123     Item top() const {
00124       return data[0].first;
00125     }
00127     Prio prio() const {
00128       return data[0].second;
00129     }
00130 
00132     void pop() {
00133       rmidx(0);
00134     }
00135 
00137     void erase(const Item &i) {
00138       rmidx(iim[i]);
00139     }
00140 
00142     Prio operator[](const Item &i) const {
00143       int idx = iim[i];
00144       return data[idx].second;
00145     }
00146 
00148     void set(const Item &i, const Prio &p) {
00149       int idx = iim[i];
00150       if( idx < 0 ) {
00151         push(i,p);
00152       }
00153       else if( comp(p, data[idx].second) ) {
00154         bubble_up(idx, PairType(i,p));
00155       }
00156       else {
00157         bubble_down(idx, PairType(i,p), data.size());
00158       }
00159     }
00160 
00162     void decrease(const Item &i, const Prio &p) {
00163       int idx = iim[i];
00164       bubble_up(idx, PairType(i,p));
00165     }
00167     void increase(const Item &i, const Prio &p) {
00168       int idx = iim[i];
00169       bubble_down(idx, PairType(i,p), data.size());
00170     }
00171 
00173     state_enum state(const Item &i) const {
00174       int s = iim[i];
00175       if( s>=0 )
00176         s=0;
00177       return state_enum(s);
00178     }
00179 
00180   }; // class BinHeap
00181 
00182   
00183   template <typename K, typename V, typename M, typename C>
00184   int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
00185     int par = parent(hole);
00186     while( hole>0 && less(p,data[par]) ) {
00187       move(data[par],hole);
00188       hole = par;
00189       par = parent(hole);
00190     }
00191     move(p, hole);
00192     return hole;
00193   }
00194 
00195   template <typename K, typename V, typename M, typename C>
00196   int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
00197     int child = second_child(hole);
00198     while(child < length) {
00199       if( less(data[child-1], data[child]) ) {
00200         --child;
00201       }
00202       if( !less(data[child], p) )
00203         goto ok;
00204       move(data[child], hole);
00205       hole = child;
00206       child = second_child(hole);
00207     }
00208     child--;
00209     if( child<length && less(data[child], p) ) {
00210       move(data[child], hole);
00211       hole=child;
00212     }
00213   ok:
00214     move(p, hole);
00215     return hole;
00216   }
00217 
00219 
00220 } // namespace lemon
00221 
00222 #endif // LEMON_BIN_HEAP_H

Generated on Sat Mar 19 10:58:39 2005 for LEMON by  doxygen 1.4.1