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