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!
|
hegyi@776
|
36 |
* Es ilyen esetben a kulcsokrol lekerdezheto az allapotuk a state
|
hegyi@776
|
37 |
metodussal:
|
klao@39
|
38 |
* (nem jart meg a kupacban PRE_HEAP=-1, epp a kupacban van IN_HEAP=0,
|
klao@39
|
39 |
* mar kikerult a kupacbol POST_HEAP=-2).
|
klao@39
|
40 |
* Szoval ebben a modban a kupac nagyjabol hasznalhato property_map-kent, csak
|
klao@172
|
41 |
* meg meg tudja mondani a "legkisebb" prioritasu elemet. De csak nagyjabol,
|
klao@39
|
42 |
* hiszen a kupacbol kikerult elemeknek elvesz az ertekuk...
|
klao@39
|
43 |
*
|
klao@39
|
44 |
* Kozvetlen mod:
|
klao@172
|
45 |
* push(Item, Prio) metodussal belerakunk a kupacba (ha az illeto kulcs mar
|
klao@39
|
46 |
* benn volt, akkor gaz).
|
klao@172
|
47 |
* increase/decrease(Item i, Prio new_prio) metodusokkal lehet
|
klao@172
|
48 |
* novelni/csokkenteni az illeto elemhez tartozo prioritast. (Ha nem volt
|
klao@172
|
49 |
* megbenne a kupacban az illeto elem, vagy nem abba az iranyba valtoztattad
|
klao@39
|
50 |
* az erteket, amerre mondtad -- gaz).
|
klao@39
|
51 |
*
|
klao@39
|
52 |
* Termeszetesen a fenti ket modot ertelemszeruen lehet keverni.
|
klao@39
|
53 |
* Ja es mindig nagyon gaz, ha belepiszkalsz a map-be, amit a kupac
|
klao@39
|
54 |
* hasznal. :-))
|
klao@39
|
55 |
*
|
klao@39
|
56 |
*
|
klao@39
|
57 |
* Bocs, most faradt vagyok, majd egyszer leforditom. (Misi)
|
klao@39
|
58 |
*
|
klao@39
|
59 |
*/
|
klao@39
|
60 |
|
klao@39
|
61 |
|
ladanyi@542
|
62 |
#ifndef HUGO_BIN_HEAP_H
|
ladanyi@542
|
63 |
#define HUGO_BIN_HEAP_H
|
klao@37
|
64 |
|
klao@491
|
65 |
///\ingroup auxdat
|
klao@274
|
66 |
///\file
|
klao@274
|
67 |
///\brief Binary Heap implementation.
|
klao@274
|
68 |
|
klao@37
|
69 |
#include <vector>
|
klao@37
|
70 |
#include <utility>
|
klao@37
|
71 |
#include <functional>
|
klao@37
|
72 |
|
alpar@114
|
73 |
namespace hugo {
|
klao@37
|
74 |
|
alpar@430
|
75 |
/// \addtogroup auxdat
|
alpar@430
|
76 |
/// @{
|
alpar@430
|
77 |
|
alpar@430
|
78 |
/// A Binary Heap implementation.
|
klao@172
|
79 |
template <typename Item, typename Prio, typename ItemIntMap,
|
klao@172
|
80 |
typename Compare = std::less<Prio> >
|
klao@37
|
81 |
class BinHeap {
|
klao@37
|
82 |
|
klao@37
|
83 |
public:
|
klao@172
|
84 |
typedef Item ItemType;
|
klao@37
|
85 |
// FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot...
|
klao@172
|
86 |
typedef Prio PrioType;
|
klao@172
|
87 |
typedef std::pair<ItemType,PrioType> PairType;
|
klao@172
|
88 |
typedef ItemIntMap ItemIntMapType;
|
klao@172
|
89 |
typedef Compare PrioCompare;
|
klao@37
|
90 |
|
klao@37
|
91 |
/**
|
klao@172
|
92 |
* Each Item element have a state associated to it. It may be "in heap",
|
klao@37
|
93 |
* "pre heap" or "post heap". The later two are indifferent from the
|
klao@37
|
94 |
* heap's point of view, but may be useful to the user.
|
klao@37
|
95 |
*
|
klao@172
|
96 |
* The ItemIntMap _should_ be initialized in such way, that it maps
|
klao@37
|
97 |
* PRE_HEAP (-1) to any element to be put in the heap...
|
klao@37
|
98 |
*/
|
klao@274
|
99 |
///\todo it is used nowhere
|
klao@274
|
100 |
///
|
klao@39
|
101 |
enum state_enum {
|
klao@37
|
102 |
IN_HEAP = 0,
|
klao@37
|
103 |
PRE_HEAP = -1,
|
klao@37
|
104 |
POST_HEAP = -2
|
klao@37
|
105 |
};
|
klao@37
|
106 |
|
klao@37
|
107 |
private:
|
klao@37
|
108 |
std::vector<PairType> data;
|
klao@37
|
109 |
Compare comp;
|
klao@37
|
110 |
// FIXME: jo ez igy???
|
klao@172
|
111 |
ItemIntMap &iim;
|
klao@37
|
112 |
|
klao@37
|
113 |
public:
|
klao@172
|
114 |
BinHeap(ItemIntMap &_iim) : iim(_iim) {}
|
klao@172
|
115 |
BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {}
|
klao@37
|
116 |
|
klao@37
|
117 |
|
klao@37
|
118 |
int size() const { return data.size(); }
|
klao@41
|
119 |
bool empty() const { return data.empty(); }
|
klao@37
|
120 |
|
klao@37
|
121 |
private:
|
klao@37
|
122 |
static int parent(int i) { return (i-1)/2; }
|
klao@37
|
123 |
static int second_child(int i) { return 2*i+2; }
|
klao@214
|
124 |
bool less(const PairType &p1, const PairType &p2) const {
|
klao@37
|
125 |
return comp(p1.second, p2.second);
|
klao@37
|
126 |
}
|
klao@37
|
127 |
|
klao@37
|
128 |
int bubble_up(int hole, PairType p);
|
klao@37
|
129 |
int bubble_down(int hole, PairType p, int length);
|
klao@37
|
130 |
|
klao@37
|
131 |
void move(const PairType &p, int i) {
|
klao@37
|
132 |
data[i] = p;
|
klao@172
|
133 |
iim.set(p.first, i);
|
klao@37
|
134 |
}
|
klao@37
|
135 |
|
klao@41
|
136 |
void rmidx(int h) {
|
klao@41
|
137 |
int n = data.size()-1;
|
klao@41
|
138 |
if( h>=0 && h<=n ) {
|
klao@172
|
139 |
iim.set(data[h].first, POST_HEAP);
|
klao@41
|
140 |
if ( h<n ) {
|
klao@41
|
141 |
bubble_down(h, data[n], n);
|
klao@41
|
142 |
}
|
klao@41
|
143 |
data.pop_back();
|
klao@41
|
144 |
}
|
klao@41
|
145 |
}
|
klao@41
|
146 |
|
klao@37
|
147 |
public:
|
klao@37
|
148 |
void push(const PairType &p) {
|
klao@37
|
149 |
int n = data.size();
|
klao@37
|
150 |
data.resize(n+1);
|
klao@37
|
151 |
bubble_up(n, p);
|
klao@37
|
152 |
}
|
klao@172
|
153 |
void push(const Item &i, const Prio &p) { push(PairType(i,p)); }
|
klao@37
|
154 |
|
klao@172
|
155 |
Item top() const {
|
klao@37
|
156 |
return data[0].first;
|
klao@37
|
157 |
}
|
klao@274
|
158 |
/// Returns the prio of the top element of the heap.
|
klao@274
|
159 |
Prio prio() const {
|
klao@37
|
160 |
return data[0].second;
|
klao@37
|
161 |
}
|
klao@37
|
162 |
|
klao@37
|
163 |
void pop() {
|
klao@41
|
164 |
rmidx(0);
|
klao@41
|
165 |
}
|
klao@41
|
166 |
|
klao@172
|
167 |
void erase(const Item &i) {
|
jacint@221
|
168 |
rmidx(iim[i]);
|
klao@37
|
169 |
}
|
klao@37
|
170 |
|
klao@274
|
171 |
Prio operator[](const Item &i) const {
|
jacint@221
|
172 |
int idx = iim[i];
|
klao@37
|
173 |
return data[idx].second;
|
klao@37
|
174 |
}
|
klao@274
|
175 |
|
klao@172
|
176 |
void set(const Item &i, const Prio &p) {
|
jacint@221
|
177 |
int idx = iim[i];
|
klao@37
|
178 |
if( idx < 0 ) {
|
klao@172
|
179 |
push(i,p);
|
klao@37
|
180 |
}
|
klao@172
|
181 |
else if( comp(p, data[idx].second) ) {
|
klao@172
|
182 |
bubble_up(idx, PairType(i,p));
|
klao@37
|
183 |
}
|
klao@37
|
184 |
else {
|
klao@172
|
185 |
bubble_down(idx, PairType(i,p), data.size());
|
klao@37
|
186 |
}
|
klao@37
|
187 |
}
|
klao@37
|
188 |
|
klao@172
|
189 |
void decrease(const Item &i, const Prio &p) {
|
jacint@221
|
190 |
int idx = iim[i];
|
klao@172
|
191 |
bubble_up(idx, PairType(i,p));
|
klao@37
|
192 |
}
|
klao@172
|
193 |
void increase(const Item &i, const Prio &p) {
|
jacint@221
|
194 |
int idx = iim[i];
|
klao@172
|
195 |
bubble_down(idx, PairType(i,p), data.size());
|
klao@37
|
196 |
}
|
klao@37
|
197 |
|
klao@172
|
198 |
state_enum state(const Item &i) const {
|
jacint@221
|
199 |
int s = iim[i];
|
klao@39
|
200 |
if( s>=0 )
|
klao@39
|
201 |
s=0;
|
klao@39
|
202 |
return state_enum(s);
|
klao@39
|
203 |
}
|
klao@39
|
204 |
|
klao@37
|
205 |
}; // class BinHeap
|
klao@37
|
206 |
|
klao@37
|
207 |
|
klao@37
|
208 |
template <typename K, typename V, typename M, typename C>
|
klao@37
|
209 |
int BinHeap<K,V,M,C>::bubble_up(int hole, PairType p) {
|
klao@37
|
210 |
int par = parent(hole);
|
klao@37
|
211 |
while( hole>0 && less(p,data[par]) ) {
|
klao@37
|
212 |
move(data[par],hole);
|
klao@37
|
213 |
hole = par;
|
klao@37
|
214 |
par = parent(hole);
|
klao@37
|
215 |
}
|
klao@37
|
216 |
move(p, hole);
|
klao@37
|
217 |
return hole;
|
klao@37
|
218 |
}
|
klao@37
|
219 |
|
klao@37
|
220 |
template <typename K, typename V, typename M, typename C>
|
klao@37
|
221 |
int BinHeap<K,V,M,C>::bubble_down(int hole, PairType p, int length) {
|
klao@37
|
222 |
int child = second_child(hole);
|
klao@37
|
223 |
while(child < length) {
|
klao@37
|
224 |
if( less(data[child-1], data[child]) ) {
|
klao@37
|
225 |
--child;
|
klao@37
|
226 |
}
|
klao@37
|
227 |
if( !less(data[child], p) )
|
klao@37
|
228 |
goto ok;
|
klao@37
|
229 |
move(data[child], hole);
|
klao@37
|
230 |
hole = child;
|
klao@37
|
231 |
child = second_child(hole);
|
klao@37
|
232 |
}
|
klao@37
|
233 |
child--;
|
klao@37
|
234 |
if( child<length && less(data[child], p) ) {
|
klao@37
|
235 |
move(data[child], hole);
|
klao@37
|
236 |
hole=child;
|
klao@37
|
237 |
}
|
klao@37
|
238 |
ok:
|
klao@37
|
239 |
move(p, hole);
|
klao@37
|
240 |
return hole;
|
klao@37
|
241 |
}
|
klao@37
|
242 |
|
alpar@430
|
243 |
///@}
|
alpar@430
|
244 |
|
alpar@114
|
245 |
} // namespace hugo
|
klao@37
|
246 |
|
marci@901
|
247 |
#endif // HUGO_BIN_HEAP_H
|