src/include/bin_heap.hh
author klao
Tue, 27 Jan 2004 21:23:33 +0000
changeset 39 28b0d751d29f
parent 37 e0e41f9e2be5
child 41 67f73b15855d
permissions -rw-r--r--
Alap leiras a BinHeap -rol

BinHeap::state() befejezese
     1 /* FIXME: Copyright ... 
     2  *
     3  * This implementation is heavily based on STL's heap functions and
     4  * the similar class by Alpar Juttner in IKTA...
     5  */
     6 
     7 /******
     8  *
     9  * BinHeap<KeyType, ValueType, KeyIntMap, [ValueCompare]>
    10  *
    11  * Ez az osztaly kulcs-ertek parok tarolasara alkalmas binaris kupacot
    12  * valosit meg.
    13  * A kupacban legfolul mindig az a par talalhato, amiben az _ertek_ a
    14  * legkisebb. (Gondolj a Dijkstra pont-tavolsag kupacara; igazabol ahhoz
    15  * lett keszitve...)
    16  *
    17  * Megjegyzes: egy kicsit gyanus nekem, hogy a kupacos temakorben nem
    18  * azt hivjak kulcsnak, amit most en annak nevezek. :) En olyan 
    19  * property_map -os ertelemben hasznalom.
    20  *
    21  * A hasznalatahoz szukseg van egy irhato/olvashato property_map-re, ami
    22  * a kulcsokhoz egy int-et tud tarolni (ezzel tudom megkeresni az illeto
    23  * elemet a kupacban a csokkentes es hasonlo muveletekhez).
    24  * A map-re csak referenciat tarol, ugy hogy a kupac elete folyan a map-nek
    25  * is elnie kell. (???)
    26  *
    27  * Ketfele modon hasznalhato:
    28  * Lusta mod:
    29  * put(Key, Value) metodussal pakolunk a kupacba,
    30  * aztan o majd eldonti, hogy ez az elem mar benne van-e es ha igen, akkor
    31  * csokkentettunk-e rajta, vagy noveltunk.
    32  * Ehhez nagyon fontos, hogy az atadott property map inicializalva legyen
    33  * minden szobajovo kulcs ertekre, -1 -es ertekkel!
    34  * Es ilyen esetben a kulcsokrol lekerdezheto az allapotuk a state metodussal:
    35  * (nem jart meg a kupacban PRE_HEAP=-1, epp a kupacban van IN_HEAP=0,
    36  *  mar kikerult a kupacbol POST_HEAP=-2).
    37  * Szoval ebben a modban a kupac nagyjabol hasznalhato property_map-kent, csak
    38  * meg meg tudja mondani a "legkisebb" erteku elemet. De csak nagyjabol,
    39  * hiszen a kupacbol kikerult elemeknek elvesz az ertekuk...
    40  *
    41  * Kozvetlen mod:
    42  * push(Key, Value) metodussal belerakunk a kupacba (ha az illeto kulcs mar
    43  * benn volt, akkor gaz).
    44  * increase/decrease(Key k, Value new_value) metodusokkal lehet
    45  * novelni/csokkenteni az illeto kulcshoz tartozo erteket. (Ha nem volt meg
    46  * benne a kupacban az illeto kulcs, vagy nem abba az iranyba valtoztattad
    47  * az erteket, amerre mondtad -- gaz).
    48  *
    49  * Termeszetesen a fenti ket modot ertelemszeruen lehet keverni.
    50  * Ja es mindig nagyon gaz, ha belepiszkalsz a map-be, amit a kupac
    51  * hasznal. :-))
    52  *
    53  *
    54  * Bocs, most faradt vagyok, majd egyszer leforditom. (Misi)
    55  *
    56  */
    57 
    58 
    59 #ifndef BIN_HEAP_HH
    60 #define BIN_HEAP_HH
    61 
    62 #include <vector>
    63 #include <utility>
    64 #include <functional>
    65 
    66 namespace marci {
    67 
    68   template <typename Key, typename Val, typename KeyIntMap,
    69 	    typename Compare = std::less<Val> >
    70   class BinHeap {
    71 
    72   public:
    73     typedef Key	             KeyType;
    74     // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
    75     typedef Val              ValueType;
    76     typedef std::pair<KeyType,ValueType>     PairType;
    77     typedef KeyIntMap        KeyIntMapType;
    78     typedef Compare          ValueCompare;
    79 
    80     /**
    81      * Each Key element have a state associated to it. It may be "in heap",
    82      * "pre heap" or "post heap". The later two are indifferent from the
    83      * heap's point of view, but may be useful to the user.
    84      *
    85      * The KeyIntMap _should_ be initialized in such way, that it maps
    86      * PRE_HEAP (-1) to any element to be put in the heap...
    87      */
    88     enum state_enum {
    89       IN_HEAP = 0,
    90       PRE_HEAP = -1,
    91       POST_HEAP = -2
    92     };
    93 
    94   private:
    95     std::vector<PairType> data;
    96     Compare comp;
    97     // FIXME: jo ez igy???
    98     KeyIntMap &kim;
    99 
   100   public:
   101     BinHeap(KeyIntMap &_kim) : kim(_kim) {}
   102     BinHeap(KeyIntMap &_kim, const Compare &_comp) : comp(_comp), kim(_kim) {}
   103 
   104 
   105     int size() const { return data.size(); }
   106     bool empty() const { return size() == 0; }
   107 
   108   private:
   109     static int parent(int i) { return (i-1)/2; }
   110     static int second_child(int i) { return 2*i+2; }
   111     bool less(const PairType &p1, const PairType &p2) {
   112       return comp(p1.second, p2.second);
   113     }
   114 
   115     int bubble_up(int hole, PairType p);
   116     int bubble_down(int hole, PairType p, int length);
   117 
   118     void move(const PairType &p, int i) {
   119       data[i] = p;
   120       kim.put(p.first, i);
   121     }
   122 
   123   public:
   124     void push(const PairType &p) {
   125       int n = data.size();
   126       data.resize(n+1);
   127       bubble_up(n, p);
   128     }
   129     void push(const Key &k, const Val &v) { push(PairType(k,v)); }
   130 
   131     Key top() const {
   132       // FIXME: test size>0 ?
   133       return data[0].first;
   134     }
   135     Val topValue() const {
   136       // FIXME: test size>0 ?
   137       return data[0].second;
   138     }
   139 
   140     void pop() {
   141       int n = data.size()-1;
   142       if( n>=0 ) {
   143 	kim.put(data[0].first, POST_HEAP);
   144 	if ( n>0 ) {
   145 	  bubble_down(0, data[n], n);
   146 	}
   147 	data.pop_back();
   148       }
   149     }
   150 
   151     const Val get(const Key &k) const {
   152       int idx = kim.get(k);
   153       return data[idx].second;
   154     }
   155     void put(const Key &k, const Val &v) {
   156       int idx = kim.get(k);
   157       if( idx < 0 ) {
   158 	push(k,v);
   159       }
   160       else if( comp(v, data[idx].second) ) {
   161 	bubble_up(idx, PairType(k,v));
   162       }
   163       else {
   164 	bubble_down(idx, PairType(k,v), data.size());
   165       }
   166     }
   167 
   168     void decrease(const Key &k, const Val &v) {
   169       int idx = kim.get(k);
   170       bubble_up(idx, PairType(k,v));
   171     }
   172     void increase(const Key &k, const Val &v) {
   173       int idx = kim.get(k);
   174       bubble_down(idx, PairType(k,v), data.size());
   175     }
   176 
   177     state_enum state(const Key &k) const {
   178       int s = kim.get(k);
   179       if( s>=0 )
   180 	s=0;
   181       return state_enum(s);
   182     }
   183 
   184   }; // class BinHeap
   185 
   186   
   187   template <typename K, typename V, typename M, typename C>
   188   int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
   189     int par = parent(hole);
   190     while( hole>0 && less(p,data[par]) ) {
   191       move(data[par],hole);
   192       hole = par;
   193       par = parent(hole);
   194     }
   195     move(p, hole);
   196     return hole;
   197   }
   198 
   199   template <typename K, typename V, typename M, typename C>
   200   int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
   201     int child = second_child(hole);
   202     while(child < length) {
   203       if( less(data[child-1], data[child]) ) {
   204 	--child;
   205       }
   206       if( !less(data[child], p) )
   207 	goto ok;
   208       move(data[child], hole);
   209       hole = child;
   210       child = second_child(hole);
   211     }
   212     child--;
   213     if( child<length && less(data[child], p) ) {
   214       move(data[child], hole);
   215       hole=child;
   216     }
   217   ok:
   218     move(p, hole);
   219     return hole;
   220   }
   221 
   222 } // namespace marci
   223 
   224 #endif // BIN_HEAP_HH