src/hugo/bin_heap.h
changeset 539 fb261e3a9a0f
parent 491 4804c967543d
child 542 69bde1d90c04
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/hugo/bin_heap.h	Thu May 06 13:21:24 2004 +0000
     1.3 @@ -0,0 +1,246 @@
     1.4 +// -*- C++ -*- //
     1.5 +
     1.6 +/* FIXME: Copyright ... 
     1.7 + *
     1.8 + * This implementation is heavily based on STL's heap functions and
     1.9 + * the similar class by Alpar Juttner in IKTA...
    1.10 + */
    1.11 +
    1.12 +/******
    1.13 + *
    1.14 + * BinHeap<ItemType, PrioType, ItemIntMap, [PrioCompare]>
    1.15 + *
    1.16 + * Ez az osztaly item-prioritas parok tarolasara alkalmas binaris kupacot
    1.17 + * valosit meg.
    1.18 + * A kupacban legfolul mindig az a par talalhato, amiben a prioritas a
    1.19 + * legkisebb. (Gondolj a Dijkstra pont-tavolsag kupacara; igazabol ahhoz
    1.20 + * lett keszitve...)
    1.21 + *
    1.22 + * Megjegyzes: a kupacos temakorokben a prioritast kulcsnak szoktak nevezni,
    1.23 + * de mivel ez zavaro tud lenni a property-map-es kulcs-ertek szohasznalata
    1.24 + * miatt, megprobaltunk valami semleges elnevezeseket kitalalni.
    1.25 + *
    1.26 + * A hasznalatahoz szukseg van egy irhato/olvashato property_map-re, ami
    1.27 + * az itemekhez egy int-et tud tarolni (ezzel tudom megkeresni az illeto
    1.28 + * elemet a kupacban a csokkentes es hasonlo muveletekhez).
    1.29 + * A map-re csak referenciat tarol, ugy hogy a kupac elete folyan a map-nek
    1.30 + * is elnie kell. (???)
    1.31 + *
    1.32 + * Ketfele modon hasznalhato:
    1.33 + * Lusta mod:
    1.34 + * set(Item, Prio) metodussal pakolunk a kupacba,
    1.35 + * aztan o majd eldonti, hogy ez az elem mar benne van-e es ha igen, akkor
    1.36 + * csokkentettunk-e rajta, vagy noveltunk.
    1.37 + * Ehhez nagyon fontos, hogy az atadott property map inicializalva legyen
    1.38 + * minden szobajovo kulcs ertekre, -1 -es ertekkel!
    1.39 + * Es ilyen esetben a kulcsokrol lekerdezheto az allapotuk a state metodussal:
    1.40 + * (nem jart meg a kupacban PRE_HEAP=-1, epp a kupacban van IN_HEAP=0,
    1.41 + *  mar kikerult a kupacbol POST_HEAP=-2).
    1.42 + * Szoval ebben a modban a kupac nagyjabol hasznalhato property_map-kent, csak
    1.43 + * meg meg tudja mondani a "legkisebb" prioritasu elemet. De csak nagyjabol,
    1.44 + * hiszen a kupacbol kikerult elemeknek elvesz az ertekuk...
    1.45 + *
    1.46 + * Kozvetlen mod:
    1.47 + * push(Item, Prio) metodussal belerakunk a kupacba (ha az illeto kulcs mar
    1.48 + * benn volt, akkor gaz).
    1.49 + * increase/decrease(Item i, Prio new_prio) metodusokkal lehet
    1.50 + * novelni/csokkenteni az illeto elemhez tartozo prioritast. (Ha nem volt
    1.51 + * megbenne a kupacban az illeto elem, vagy nem abba az iranyba valtoztattad
    1.52 + * az erteket, amerre mondtad -- gaz).
    1.53 + *
    1.54 + * Termeszetesen a fenti ket modot ertelemszeruen lehet keverni.
    1.55 + * Ja es mindig nagyon gaz, ha belepiszkalsz a map-be, amit a kupac
    1.56 + * hasznal. :-))
    1.57 + *
    1.58 + *
    1.59 + * Bocs, most faradt vagyok, majd egyszer leforditom. (Misi)
    1.60 + *
    1.61 + */
    1.62 +
    1.63 +
    1.64 +#ifndef BIN_HEAP_HH
    1.65 +#define BIN_HEAP_HH
    1.66 +
    1.67 +///\ingroup auxdat
    1.68 +///\file
    1.69 +///\brief Binary Heap implementation.
    1.70 +
    1.71 +#include <vector>
    1.72 +#include <utility>
    1.73 +#include <functional>
    1.74 +
    1.75 +namespace hugo {
    1.76 +
    1.77 +  /// \addtogroup auxdat
    1.78 +  /// @{
    1.79 +
    1.80 +   /// A Binary Heap implementation.
    1.81 +  template <typename Item, typename Prio, typename ItemIntMap,
    1.82 +	    typename Compare = std::less<Prio> >
    1.83 +  class BinHeap {
    1.84 +
    1.85 +  public:
    1.86 +    typedef Item                             ItemType;
    1.87 +    // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
    1.88 +    typedef Prio                             PrioType;
    1.89 +    typedef std::pair<ItemType,PrioType>     PairType;
    1.90 +    typedef ItemIntMap                       ItemIntMapType;
    1.91 +    typedef Compare                          PrioCompare;
    1.92 +
    1.93 +    /**
    1.94 +     * Each Item element have a state associated to it. It may be "in heap",
    1.95 +     * "pre heap" or "post heap". The later two are indifferent from the
    1.96 +     * heap's point of view, but may be useful to the user.
    1.97 +     *
    1.98 +     * The ItemIntMap _should_ be initialized in such way, that it maps
    1.99 +     * PRE_HEAP (-1) to any element to be put in the heap...
   1.100 +     */
   1.101 +    ///\todo it is used nowhere
   1.102 +    ///
   1.103 +    enum state_enum {
   1.104 +      IN_HEAP = 0,
   1.105 +      PRE_HEAP = -1,
   1.106 +      POST_HEAP = -2
   1.107 +    };
   1.108 +
   1.109 +  private:
   1.110 +    std::vector<PairType> data;
   1.111 +    Compare comp;
   1.112 +    // FIXME: jo ez igy???
   1.113 +    ItemIntMap &iim;
   1.114 +
   1.115 +  public:
   1.116 +    BinHeap(ItemIntMap &_iim) : iim(_iim) {}
   1.117 +    BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
   1.118 +
   1.119 +
   1.120 +    int size() const { return data.size(); }
   1.121 +    bool empty() const { return data.empty(); }
   1.122 +
   1.123 +  private:
   1.124 +    static int parent(int i) { return (i-1)/2; }
   1.125 +    static int second_child(int i) { return 2*i+2; }
   1.126 +    bool less(const PairType &p1, const PairType &p2) const {
   1.127 +      return comp(p1.second, p2.second);
   1.128 +    }
   1.129 +
   1.130 +    int bubble_up(int hole, PairType p);
   1.131 +    int bubble_down(int hole, PairType p, int length);
   1.132 +
   1.133 +    void move(const PairType &p, int i) {
   1.134 +      data[i] = p;
   1.135 +      iim.set(p.first, i);
   1.136 +    }
   1.137 +
   1.138 +    void rmidx(int h) {
   1.139 +      int n = data.size()-1;
   1.140 +      if( h>=0 && h<=n ) {
   1.141 +	iim.set(data[h].first, POST_HEAP);
   1.142 +	if ( h<n ) {
   1.143 +	  bubble_down(h, data[n], n);
   1.144 +	}
   1.145 +	data.pop_back();
   1.146 +      }
   1.147 +    }
   1.148 +
   1.149 +  public:
   1.150 +    void push(const PairType &p) {
   1.151 +      int n = data.size();
   1.152 +      data.resize(n+1);
   1.153 +      bubble_up(n, p);
   1.154 +    }
   1.155 +    void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
   1.156 +
   1.157 +    Item top() const {
   1.158 +      return data[0].first;
   1.159 +    }
   1.160 +    /// Returns the prio of the top element of the heap.
   1.161 +    Prio prio() const {
   1.162 +      return data[0].second;
   1.163 +    }
   1.164 +
   1.165 +    void pop() {
   1.166 +      rmidx(0);
   1.167 +    }
   1.168 +
   1.169 +    void erase(const Item &i) {
   1.170 +      rmidx(iim[i]);
   1.171 +    }
   1.172 +
   1.173 +    Prio operator[](const Item &i) const {
   1.174 +      int idx = iim[i];
   1.175 +      return data[idx].second;
   1.176 +    }
   1.177 +
   1.178 +    void set(const Item &i, const Prio &p) {
   1.179 +      int idx = iim[i];
   1.180 +      if( idx < 0 ) {
   1.181 +	push(i,p);
   1.182 +      }
   1.183 +      else if( comp(p, data[idx].second) ) {
   1.184 +	bubble_up(idx, PairType(i,p));
   1.185 +      }
   1.186 +      else {
   1.187 +	bubble_down(idx, PairType(i,p), data.size());
   1.188 +      }
   1.189 +    }
   1.190 +
   1.191 +    void decrease(const Item &i, const Prio &p) {
   1.192 +      int idx = iim[i];
   1.193 +      bubble_up(idx, PairType(i,p));
   1.194 +    }
   1.195 +    void increase(const Item &i, const Prio &p) {
   1.196 +      int idx = iim[i];
   1.197 +      bubble_down(idx, PairType(i,p), data.size());
   1.198 +    }
   1.199 +
   1.200 +    state_enum state(const Item &i) const {
   1.201 +      int s = iim[i];
   1.202 +      if( s>=0 )
   1.203 +	s=0;
   1.204 +      return state_enum(s);
   1.205 +    }
   1.206 +
   1.207 +  }; // class BinHeap
   1.208 +
   1.209 +  
   1.210 +  template <typename K, typename V, typename M, typename C>
   1.211 +  int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
   1.212 +    int par = parent(hole);
   1.213 +    while( hole>0 && less(p,data[par]) ) {
   1.214 +      move(data[par],hole);
   1.215 +      hole = par;
   1.216 +      par = parent(hole);
   1.217 +    }
   1.218 +    move(p, hole);
   1.219 +    return hole;
   1.220 +  }
   1.221 +
   1.222 +  template <typename K, typename V, typename M, typename C>
   1.223 +  int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
   1.224 +    int child = second_child(hole);
   1.225 +    while(child < length) {
   1.226 +      if( less(data[child-1], data[child]) ) {
   1.227 +	--child;
   1.228 +      }
   1.229 +      if( !less(data[child], p) )
   1.230 +	goto ok;
   1.231 +      move(data[child], hole);
   1.232 +      hole = child;
   1.233 +      child = second_child(hole);
   1.234 +    }
   1.235 +    child--;
   1.236 +    if( child<length && less(data[child], p) ) {
   1.237 +      move(data[child], hole);
   1.238 +      hole=child;
   1.239 +    }
   1.240 +  ok:
   1.241 +    move(p, hole);
   1.242 +    return hole;
   1.243 +  }
   1.244 +
   1.245 +  ///@}
   1.246 +
   1.247 +} // namespace hugo
   1.248 +
   1.249 +#endif // BIN_HEAP_HH