bool map problems solved.
(now operator[] gives back 'std::vector<T>::reference' rather that 'T&')
3 *template <typename Item,
6 * typename Compare = std::less<Prio> >
10 *FibHeap(ItemIntMap), FibHeap(ItemIntMap, Compare)
14 *int size() : returns the number of elements in the heap
16 *bool empty() : true iff size()=0
18 *void set(Item, Prio) : calls push(Item, Prio) if Item is not
19 * in the heap, and calls decrease/increase(Item, Prio) otherwise
21 *void push(Item, Prio) : pushes Item to the heap with priority Prio. Item
22 * mustn't be in the heap.
24 *Item top() : returns the Item with least Prio.
25 * Must be called only if heap is nonempty.
27 *Prio prio() : returns the least Prio
28 * Must be called only if heap is nonempty.
30 *Prio get(Item) : returns Prio of Item
31 * Must be called only if Item is in heap.
33 *void pop() : deletes the Item with least Prio
35 *void erase(Item) : deletes Item from the heap if it was already there
37 *void decrease(Item, P) : decreases prio of Item to P.
38 * Item must be in the heap with prio at least P.
40 *void increase(Item, P) : sets prio of Item to P.
42 *state_enum state(Item) : returns PRE_HEAP if Item has not been in the
43 * heap until now, IN_HEAP if it is in the heap at the moment, and
44 * POST_HEAP otherwise. In the latter case it is possible that Item
45 * will get back to the heap again.
47 *In Fibonacci heaps, increase and erase are not efficient, in case of
48 *many calls to these operations, it is better to use bin_heap.
60 template <typename Item, typename Prio, typename ItemIntMap,
61 typename Compare = std::less<Prio> >
65 typedef Prio PrioType;
69 std::vector<store> container;
83 FibHeap(ItemIntMap &_iimap) : minimum(), iimap(_iimap), num_items() {}
84 FibHeap(ItemIntMap &_iimap, const Compare &_comp) : minimum(),
85 iimap(_iimap), comp(_comp), num_items() {}
93 bool empty() const { return num_items==0; }
96 void set (Item const it, PrioType const value) {
98 if ( i >= 0 && container[i].in ) {
99 if ( comp(value, container[i].prio) ) decrease(it, value);
100 if ( comp(container[i].prio, value) ) increase(it, value);
101 } else push(it, value);
105 void push (Item const it, PrioType const value) {
108 int s=container.size();
112 container.push_back(st);
115 container[i].parent=container[i].child=-1;
116 container[i].degree=0;
117 container[i].in=true;
118 container[i].marked=false;
122 container[container[minimum].right_neighbor].left_neighbor=i;
123 container[i].right_neighbor=container[minimum].right_neighbor;
124 container[minimum].right_neighbor=i;
125 container[i].left_neighbor=minimum;
126 if ( comp( value, container[minimum].prio) ) minimum=i;
128 container[i].right_neighbor=container[i].left_neighbor=i;
131 container[i].prio=value;
137 return container[minimum].name;
141 PrioType prio() const {
142 return container[minimum].prio;
148 PrioType& operator[](const Item& it) const {
149 return container[iimap.get(it)].prio;
152 const PrioType& operator[](const Item& it) const {
153 return container[iimap.get(it)].prio;
156 const PrioType get(const Item& it) const {
157 return container[iimap.get(it)].prio;
163 /*The first case is that there are only one root.*/
164 if ( container[minimum].left_neighbor==minimum ) {
165 container[minimum].in=false;
166 if ( container[minimum].degree!=0 ) {
167 makeroot(container[minimum].child);
168 minimum=container[minimum].child;
172 int right=container[minimum].right_neighbor;
174 container[minimum].in=false;
175 if ( container[minimum].degree > 0 ) {
176 int left=container[minimum].left_neighbor;
177 int child=container[minimum].child;
178 int last_child=container[child].left_neighbor;
182 container[left].right_neighbor=child;
183 container[child].left_neighbor=left;
184 container[right].left_neighbor=last_child;
185 container[last_child].right_neighbor=right;
189 } // the case where there are more roots
194 void erase (const Item& it) {
197 if ( i >= 0 && container[i].in ) {
198 if ( container[i].parent!=-1 ) {
199 int p=container[i].parent;
203 minimum=i; //As if its prio would be -infinity
209 void decrease (Item it, PrioType const value) {
211 container[i].prio=value;
212 int p=container[i].parent;
214 if ( p!=-1 && comp(value, container[p].prio) ) {
218 if ( comp(value, container[minimum].prio) ) minimum=i;
222 void increase (Item it, PrioType const value) {
228 state_enum state(const Item &it) const {
231 if ( container[i].in ) i=0;
234 return state_enum(i);
242 int maxdeg=int( floor( 2.08*log(double(container.size()))))+1;
244 std::vector<int> A(maxdeg,-1);
247 *Recall that now minimum does not point to the minimum prio element.
248 *We set minimum to this during balance().
250 int anchor=container[minimum].left_neighbor;
256 if ( anchor==active ) end=true;
257 int d=container[active].degree;
258 next=container[active].right_neighbor;
261 if( comp(container[active].prio, container[A[d]].prio) ) {
274 while ( container[minimum].parent >=0 ) minimum=container[minimum].parent;
278 if ( comp(container[s].prio, container[minimum].prio) ) minimum=s;
279 s=container[s].right_neighbor;
284 void makeroot (int c) {
287 container[s].parent=-1;
288 s=container[s].right_neighbor;
293 void cut (int a, int b) {
295 *Replacing a from the children of b.
297 --container[b].degree;
299 if ( container[b].degree !=0 ) {
300 int child=container[b].child;
302 container[b].child=container[child].right_neighbor;
307 /*Lacing a to the roots.*/
308 int right=container[minimum].right_neighbor;
309 container[minimum].right_neighbor=a;
310 container[a].left_neighbor=minimum;
311 container[a].right_neighbor=right;
312 container[right].left_neighbor=a;
314 container[a].parent=-1;
315 container[a].marked=false;
321 if ( container[a].parent!=-1 ) {
322 int p=container[a].parent;
324 if ( container[a].marked==false ) container[a].marked=true;
333 void fuse (int a, int b) {
336 /*Lacing b under a.*/
337 container[b].parent=a;
339 if (container[a].degree==0) {
340 container[b].left_neighbor=b;
341 container[b].right_neighbor=b;
342 container[a].child=b;
344 int child=container[a].child;
345 int last_child=container[child].left_neighbor;
346 container[child].left_neighbor=b;
347 container[b].right_neighbor=child;
348 container[last_child].right_neighbor=b;
349 container[b].left_neighbor=last_child;
352 ++container[a].degree;
354 container[b].marked=false;
359 *It is invoked only if a has siblings.
361 void unlace (int a) {
362 int leftn=container[a].left_neighbor;
363 int rightn=container[a].right_neighbor;
364 container[leftn].right_neighbor=rightn;
365 container[rightn].left_neighbor=leftn;
370 friend class FibHeap;
382 store() : parent(-1), child(-1), degree(), marked(false), in(true) {}