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