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