805:b31e13 »
765:703ebf »
760:4ac304 »
756:0747f3 »
748:d1a922 »
743:c9b9da »
740:7bda78 »
738:9e54e3 »
733:7439dc »
765:703ebf »
760:4ac304 »
756:0747f3 »
748:d1a922 »
743:c9b9da »
740:7bda78 »
738:9e54e3 »
733:7439dc »
0
4
0
... | ... |
@@ -35,7 +35,7 @@ |
35 | 35 |
/// |
36 |
///This class implements the \e binary \e heap data structure. |
|
37 |
/// |
|
36 |
///This class implements the \e binary \e heap data structure. |
|
37 |
/// |
|
38 | 38 |
///A \e heap is a data structure for storing items with specified values |
39 | 39 |
///called \e priorities in such a way that finding the item with minimum |
40 |
///priority is efficient. \c |
|
40 |
///priority is efficient. \c CMP specifies the ordering of the priorities. |
|
41 | 41 |
///In a heap one can change the priority of an item, add or erase an |
... | ... |
@@ -46,3 +46,3 @@ |
46 | 46 |
///to handle the cross references. |
47 |
///\tparam |
|
47 |
///\tparam CMP A functor class for the ordering of the priorities. |
|
48 | 48 |
///The default is \c std::less<PR>. |
... | ... |
@@ -51,3 +51,3 @@ |
51 | 51 |
///\sa Dijkstra |
52 |
template <typename PR, typename IM, typename |
|
52 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
|
53 | 53 |
class BinHeap { |
... | ... |
@@ -64,3 +64,3 @@ |
64 | 64 |
///\e |
65 |
typedef |
|
65 |
typedef CMP Compare; |
|
66 | 66 |
... | ... |
@@ -33,3 +33,3 @@ |
33 | 33 |
|
34 |
template <bool |
|
34 |
template <bool MIN> |
|
35 | 35 |
struct DirectionTraits { |
... | ... |
@@ -67,7 +67,8 @@ |
67 | 67 |
/// |
68 |
/// \param |
|
68 |
/// \param IM A read and write Item int map, used internally |
|
69 | 69 |
/// to handle the cross references. |
70 |
/// \param minimize If the given parameter is true then the heap gives back |
|
71 |
/// the lowest priority. |
|
72 |
|
|
70 |
/// \param MIN If the given parameter is false then instead of the |
|
71 |
/// minimum value the maximum can be retrivied with the top() and |
|
72 |
/// prio() member functions. |
|
73 |
template <typename IM, bool MIN = true> |
|
73 | 74 |
class BucketHeap { |
... | ... |
@@ -76,3 +77,3 @@ |
76 | 77 |
/// \e |
77 |
typedef typename |
|
78 |
typedef typename IM::Key Item; |
|
78 | 79 |
/// \e |
... | ... |
@@ -82,3 +83,3 @@ |
82 | 83 |
/// \e |
83 |
typedef |
|
84 |
typedef IM ItemIntMap; |
|
84 | 85 |
|
... | ... |
@@ -86,3 +87,3 @@ |
86 | 87 |
|
87 |
typedef _bucket_heap_bits::DirectionTraits< |
|
88 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
|
88 | 89 |
|
... | ... |
@@ -96,8 +97,8 @@ |
96 | 97 |
/// |
97 |
/// The ItemIntMap \e should be initialized in such way that it maps |
|
98 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
|
98 |
/// The item-int map must be initialized in such way that it assigns |
|
99 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
|
99 | 100 |
enum State { |
100 |
IN_HEAP = 0, |
|
101 |
PRE_HEAP = -1, |
|
102 |
|
|
101 |
IN_HEAP = 0, ///< = 0. |
|
102 |
PRE_HEAP = -1, ///< = -1. |
|
103 |
POST_HEAP = -2 ///< = -2. |
|
103 | 104 |
}; |
... | ... |
@@ -108,6 +109,6 @@ |
108 | 109 |
/// The constructor. |
109 |
/// \param |
|
110 |
/// \param map should be given to the constructor, since it is used |
|
110 | 111 |
/// internally to handle the cross references. The value of the map |
111 | 112 |
/// should be PRE_HEAP (-1) for each element. |
112 |
explicit BucketHeap(ItemIntMap & |
|
113 |
explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} |
|
113 | 114 |
|
... | ... |
@@ -116,3 +117,3 @@ |
116 | 117 |
/// \brief Returns the number of items stored in the heap. |
117 |
int size() const { return |
|
118 |
int size() const { return _data.size(); } |
|
118 | 119 |
|
... | ... |
@@ -121,3 +122,3 @@ |
121 | 122 |
/// Returns \c true if and only if the heap stores no items. |
122 |
bool empty() const { return |
|
123 |
bool empty() const { return _data.empty(); } |
|
123 | 124 |
|
... | ... |
@@ -130,3 +131,3 @@ |
130 | 131 |
void clear() { |
131 |
|
|
132 |
_data.clear(); _first.clear(); _minimum = 0; |
|
132 | 133 |
} |
... | ... |
@@ -136,15 +137,15 @@ |
136 | 137 |
void relocate_last(int idx) { |
137 |
if (idx + 1 < int(data.size())) { |
|
138 |
data[idx] = data.back(); |
|
139 |
if (data[idx].prev != -1) { |
|
140 |
data[data[idx].prev].next = idx; |
|
138 |
if (idx + 1 < int(_data.size())) { |
|
139 |
_data[idx] = _data.back(); |
|
140 |
if (_data[idx].prev != -1) { |
|
141 |
_data[_data[idx].prev].next = idx; |
|
141 | 142 |
} else { |
142 |
|
|
143 |
_first[_data[idx].value] = idx; |
|
143 | 144 |
} |
144 |
if (data[idx].next != -1) { |
|
145 |
data[data[idx].next].prev = idx; |
|
145 |
if (_data[idx].next != -1) { |
|
146 |
_data[_data[idx].next].prev = idx; |
|
146 | 147 |
} |
147 |
|
|
148 |
_iim[_data[idx].item] = idx; |
|
148 | 149 |
} |
149 |
|
|
150 |
_data.pop_back(); |
|
150 | 151 |
} |
... | ... |
@@ -152,9 +153,9 @@ |
152 | 153 |
void unlace(int idx) { |
153 |
if (data[idx].prev != -1) { |
|
154 |
data[data[idx].prev].next = data[idx].next; |
|
154 |
if (_data[idx].prev != -1) { |
|
155 |
_data[_data[idx].prev].next = _data[idx].next; |
|
155 | 156 |
} else { |
156 |
|
|
157 |
_first[_data[idx].value] = _data[idx].next; |
|
157 | 158 |
} |
158 |
if (data[idx].next != -1) { |
|
159 |
data[data[idx].next].prev = data[idx].prev; |
|
159 |
if (_data[idx].next != -1) { |
|
160 |
_data[_data[idx].next].prev = _data[idx].prev; |
|
160 | 161 |
} |
... | ... |
@@ -163,11 +164,11 @@ |
163 | 164 |
void lace(int idx) { |
164 |
if (int(first.size()) <= data[idx].value) { |
|
165 |
first.resize(data[idx].value + 1, -1); |
|
165 |
if (int(_first.size()) <= _data[idx].value) { |
|
166 |
_first.resize(_data[idx].value + 1, -1); |
|
166 | 167 |
} |
167 |
data[idx].next = first[data[idx].value]; |
|
168 |
if (data[idx].next != -1) { |
|
169 |
|
|
168 |
_data[idx].next = _first[_data[idx].value]; |
|
169 |
if (_data[idx].next != -1) { |
|
170 |
_data[_data[idx].next].prev = idx; |
|
170 | 171 |
} |
171 |
first[data[idx].value] = idx; |
|
172 |
data[idx].prev = -1; |
|
172 |
_first[_data[idx].value] = idx; |
|
173 |
_data[idx].prev = -1; |
|
173 | 174 |
} |
... | ... |
@@ -189,8 +190,8 @@ |
189 | 190 |
void push(const Item &i, const Prio &p) { |
190 |
int idx = data.size(); |
|
191 |
index[i] = idx; |
|
192 |
|
|
191 |
int idx = _data.size(); |
|
192 |
_iim[i] = idx; |
|
193 |
_data.push_back(BucketItem(i, p)); |
|
193 | 194 |
lace(idx); |
194 |
if (Direction::less(p, minimal)) { |
|
195 |
minimal = p; |
|
195 |
if (Direction::less(p, _minimum)) { |
|
196 |
_minimum = p; |
|
196 | 197 |
} |
... | ... |
@@ -203,6 +204,6 @@ |
203 | 204 |
Item top() const { |
204 |
while (first[minimal] == -1) { |
|
205 |
Direction::increase(minimal); |
|
205 |
while (_first[_minimum] == -1) { |
|
206 |
Direction::increase(_minimum); |
|
206 | 207 |
} |
207 |
return |
|
208 |
return _data[_first[_minimum]].item; |
|
208 | 209 |
} |
... | ... |
@@ -214,6 +215,6 @@ |
214 | 215 |
Prio prio() const { |
215 |
while (first[minimal] == -1) { |
|
216 |
Direction::increase(minimal); |
|
216 |
while (_first[_minimum] == -1) { |
|
217 |
Direction::increase(_minimum); |
|
217 | 218 |
} |
218 |
return |
|
219 |
return _minimum; |
|
219 | 220 |
} |
... | ... |
@@ -225,7 +226,7 @@ |
225 | 226 |
void pop() { |
226 |
while (first[minimal] == -1) { |
|
227 |
Direction::increase(minimal); |
|
227 |
while (_first[_minimum] == -1) { |
|
228 |
Direction::increase(_minimum); |
|
228 | 229 |
} |
229 |
int idx = first[minimal]; |
|
230 |
index[data[idx].item] = -2; |
|
230 |
int idx = _first[_minimum]; |
|
231 |
_iim[_data[idx].item] = -2; |
|
231 | 232 |
unlace(idx); |
... | ... |
@@ -240,4 +241,4 @@ |
240 | 241 |
void erase(const Item &i) { |
241 |
int idx = index[i]; |
|
242 |
index[data[idx].item] = -2; |
|
242 |
int idx = _iim[i]; |
|
243 |
_iim[_data[idx].item] = -2; |
|
243 | 244 |
unlace(idx); |
... | ... |
@@ -253,4 +254,4 @@ |
253 | 254 |
Prio operator[](const Item &i) const { |
254 |
int idx = index[i]; |
|
255 |
return data[idx].value; |
|
255 |
int idx = _iim[i]; |
|
256 |
return _data[idx].value; |
|
256 | 257 |
} |
... | ... |
@@ -265,6 +266,6 @@ |
265 | 266 |
void set(const Item &i, const Prio &p) { |
266 |
int idx = |
|
267 |
int idx = _iim[i]; |
|
267 | 268 |
if (idx < 0) { |
268 | 269 |
push(i, p); |
269 |
} else if (Direction::less(p, |
|
270 |
} else if (Direction::less(p, _data[idx].value)) { |
|
270 | 271 |
decrease(i, p); |
... | ... |
@@ -283,7 +284,7 @@ |
283 | 284 |
void decrease(const Item &i, const Prio &p) { |
284 |
int idx = |
|
285 |
int idx = _iim[i]; |
|
285 | 286 |
unlace(idx); |
286 |
data[idx].value = p; |
|
287 |
if (Direction::less(p, minimal)) { |
|
288 |
|
|
287 |
_data[idx].value = p; |
|
288 |
if (Direction::less(p, _minimum)) { |
|
289 |
_minimum = p; |
|
289 | 290 |
} |
... | ... |
@@ -300,5 +301,5 @@ |
300 | 301 |
void increase(const Item &i, const Prio &p) { |
301 |
int idx = |
|
302 |
int idx = _iim[i]; |
|
302 | 303 |
unlace(idx); |
303 |
|
|
304 |
_data[idx].value = p; |
|
304 | 305 |
lace(idx); |
... | ... |
@@ -315,3 +316,3 @@ |
315 | 316 |
State state(const Item &i) const { |
316 |
int idx = |
|
317 |
int idx = _iim[i]; |
|
317 | 318 |
if (idx >= 0) idx = 0; |
... | ... |
@@ -334,3 +335,3 @@ |
334 | 335 |
} |
335 |
|
|
336 |
_iim[i] = st; |
|
336 | 337 |
break; |
... | ... |
@@ -353,6 +354,6 @@ |
353 | 354 |
|
354 |
ItemIntMap& index; |
|
355 |
std::vector<int> first; |
|
356 |
std::vector<BucketItem> data; |
|
357 |
mutable int minimal; |
|
355 |
ItemIntMap& _iim; |
|
356 |
std::vector<int> _first; |
|
357 |
std::vector<BucketItem> _data; |
|
358 |
mutable int _minimum; |
|
358 | 359 |
|
... | ... |
@@ -372,9 +373,10 @@ |
372 | 373 |
/// |
373 |
/// \param |
|
374 |
/// \param IM A read and write Item int map, used internally |
|
374 | 375 |
/// to handle the cross references. |
375 |
/// \param minimize If the given parameter is true then the heap gives back |
|
376 |
/// the lowest priority. |
|
376 |
/// \param MIN If the given parameter is false then instead of the |
|
377 |
/// minimum value the maximum can be retrivied with the top() and |
|
378 |
/// prio() member functions. |
|
377 | 379 |
/// |
378 | 380 |
/// \sa BucketHeap |
379 |
template <typename |
|
381 |
template <typename IM, bool MIN = true > |
|
380 | 382 |
class SimpleBucketHeap { |
... | ... |
@@ -382,6 +384,6 @@ |
382 | 384 |
public: |
383 |
typedef typename |
|
385 |
typedef typename IM::Key Item; |
|
384 | 386 |
typedef int Prio; |
385 | 387 |
typedef std::pair<Item, Prio> Pair; |
386 |
typedef |
|
388 |
typedef IM ItemIntMap; |
|
387 | 389 |
|
... | ... |
@@ -389,3 +391,3 @@ |
389 | 391 |
|
390 |
typedef _bucket_heap_bits::DirectionTraits< |
|
392 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
|
391 | 393 |
|
... | ... |
@@ -399,8 +401,8 @@ |
399 | 401 |
/// |
400 |
/// The ItemIntMap \e should be initialized in such way that it maps |
|
401 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
|
402 |
/// The item-int map must be initialized in such way that it assigns |
|
403 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
|
402 | 404 |
enum State { |
403 |
IN_HEAP = 0, |
|
404 |
PRE_HEAP = -1, |
|
405 |
|
|
405 |
IN_HEAP = 0, ///< = 0. |
|
406 |
PRE_HEAP = -1, ///< = -1. |
|
407 |
POST_HEAP = -2 ///< = -2. |
|
406 | 408 |
}; |
... | ... |
@@ -412,7 +414,7 @@ |
412 | 414 |
/// The constructor. |
413 |
/// \param |
|
415 |
/// \param map should be given to the constructor, since it is used |
|
414 | 416 |
/// internally to handle the cross references. The value of the map |
415 | 417 |
/// should be PRE_HEAP (-1) for each element. |
416 |
explicit SimpleBucketHeap(ItemIntMap &_index) |
|
417 |
: index(_index), free(-1), num(0), minimal(0) {} |
|
418 |
explicit SimpleBucketHeap(ItemIntMap &map) |
|
419 |
: _iim(map), _free(-1), _num(0), _minimum(0) {} |
|
418 | 420 |
|
... | ... |
@@ -421,3 +423,3 @@ |
421 | 423 |
/// The number of items stored in the heap. |
422 |
int size() const { return |
|
424 |
int size() const { return _num; } |
|
423 | 425 |
|
... | ... |
@@ -426,3 +428,3 @@ |
426 | 428 |
/// Returns \c true if and only if the heap stores no items. |
427 |
bool empty() const { return |
|
429 |
bool empty() const { return _num == 0; } |
|
428 | 430 |
|
... | ... |
@@ -435,3 +437,3 @@ |
435 | 437 |
void clear() { |
436 |
|
|
438 |
_data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; |
|
437 | 439 |
} |
... | ... |
@@ -453,18 +455,18 @@ |
453 | 455 |
int idx; |
454 |
if (free == -1) { |
|
455 |
idx = data.size(); |
|
456 |
|
|
456 |
if (_free == -1) { |
|
457 |
idx = _data.size(); |
|
458 |
_data.push_back(BucketItem(i)); |
|
457 | 459 |
} else { |
458 |
idx = free; |
|
459 |
free = data[idx].next; |
|
460 |
|
|
460 |
idx = _free; |
|
461 |
_free = _data[idx].next; |
|
462 |
_data[idx].item = i; |
|
461 | 463 |
} |
462 |
index[i] = idx; |
|
463 |
if (p >= int(first.size())) first.resize(p + 1, -1); |
|
464 |
data[idx].next = first[p]; |
|
465 |
first[p] = idx; |
|
466 |
if (Direction::less(p, minimal)) { |
|
467 |
minimal = p; |
|
464 |
_iim[i] = idx; |
|
465 |
if (p >= int(_first.size())) _first.resize(p + 1, -1); |
|
466 |
_data[idx].next = _first[p]; |
|
467 |
_first[p] = idx; |
|
468 |
if (Direction::less(p, _minimum)) { |
|
469 |
_minimum = p; |
|
468 | 470 |
} |
469 |
++ |
|
471 |
++_num; |
|
470 | 472 |
} |
... | ... |
@@ -476,6 +478,6 @@ |
476 | 478 |
Item top() const { |
477 |
while (first[minimal] == -1) { |
|
478 |
Direction::increase(minimal); |
|
479 |
while (_first[_minimum] == -1) { |
|
480 |
Direction::increase(_minimum); |
|
479 | 481 |
} |
480 |
return |
|
482 |
return _data[_first[_minimum]].item; |
|
481 | 483 |
} |
... | ... |
@@ -487,6 +489,6 @@ |
487 | 489 |
Prio prio() const { |
488 |
while (first[minimal] == -1) { |
|
489 |
Direction::increase(minimal); |
|
490 |
while (_first[_minimum] == -1) { |
|
491 |
Direction::increase(_minimum); |
|
490 | 492 |
} |
491 |
return |
|
493 |
return _minimum; |
|
492 | 494 |
} |
... | ... |
@@ -498,11 +500,11 @@ |
498 | 500 |
void pop() { |
499 |
while (first[minimal] == -1) { |
|
500 |
Direction::increase(minimal); |
|
501 |
while (_first[_minimum] == -1) { |
|
502 |
Direction::increase(_minimum); |
|
501 | 503 |
} |
502 |
int idx = first[minimal]; |
|
503 |
index[data[idx].item] = -2; |
|
504 |
first[minimal] = data[idx].next; |
|
505 |
data[idx].next = free; |
|
506 |
free = idx; |
|
507 |
--num; |
|
504 |
int idx = _first[_minimum]; |
|
505 |
_iim[_data[idx].item] = -2; |
|
506 |
_first[_minimum] = _data[idx].next; |
|
507 |
_data[idx].next = _free; |
|
508 |
_free = idx; |
|
509 |
--_num; |
|
508 | 510 |
} |
... | ... |
@@ -518,9 +520,9 @@ |
518 | 520 |
Prio operator[](const Item &i) const { |
519 |
for (int k = 0; k < first.size(); ++k) { |
|
520 |
int idx = first[k]; |
|
521 |
for (int k = 0; k < _first.size(); ++k) { |
|
522 |
int idx = _first[k]; |
|
521 | 523 |
while (idx != -1) { |
522 |
if ( |
|
524 |
if (_data[idx].item == i) { |
|
523 | 525 |
return k; |
524 | 526 |
} |
525 |
idx = |
|
527 |
idx = _data[idx].next; |
|
526 | 528 |
} |
... | ... |
@@ -539,3 +541,3 @@ |
539 | 541 |
State state(const Item &i) const { |
540 |
int idx = |
|
542 |
int idx = _iim[i]; |
|
541 | 543 |
if (idx >= 0) idx = 0; |
... | ... |
@@ -554,7 +556,7 @@ |
554 | 556 |
|
555 |
ItemIntMap& index; |
|
556 |
std::vector<int> first; |
|
557 |
std::vector<BucketItem> data; |
|
558 |
int free, num; |
|
559 |
|
|
557 |
ItemIntMap& _iim; |
|
558 |
std::vector<int> _first; |
|
559 |
std::vector<BucketItem> _data; |
|
560 |
int _free, _num; |
|
561 |
mutable int _minimum; |
|
560 | 562 |
... | ... |
@@ -38,3 +38,3 @@ |
38 | 38 |
///priorities in such a way that finding the item with minimum priority is |
39 |
///efficient. \c |
|
39 |
///efficient. \c CMP specifies the ordering of the priorities. In a heap |
|
40 | 40 |
///one can change the priority of an item, add or erase an item, etc. |
... | ... |
@@ -45,7 +45,7 @@ |
45 | 45 |
/// |
46 |
///\param _Prio Type of the priority of the items. |
|
47 |
///\param _ItemIntMap A read and writable Item int map, used internally |
|
46 |
///\param PRIO Type of the priority of the items. |
|
47 |
///\param IM A read and writable Item int map, used internally |
|
48 | 48 |
///to handle the cross references. |
49 |
///\param _Compare A class for the ordering of the priorities. The |
|
50 |
///default is \c std::less<_Prio>. |
|
49 |
///\param CMP A class for the ordering of the priorities. The |
|
50 |
///default is \c std::less<PRIO>. |
|
51 | 51 |
/// |
... | ... |
@@ -54,9 +54,5 @@ |
54 | 54 |
#ifdef DOXYGEN |
55 |
template <typename _Prio, |
|
56 |
typename _ItemIntMap, |
|
57 |
|
|
55 |
template <typename PRIO, typename IM, typename CMP> |
|
58 | 56 |
#else |
59 |
template <typename _Prio, |
|
60 |
typename _ItemIntMap, |
|
61 |
|
|
57 |
template <typename PRIO, typename IM, typename CMP = std::less<PRIO> > |
|
62 | 58 |
#endif |
... | ... |
@@ -65,5 +61,5 @@ |
65 | 61 |
///\e |
66 |
typedef |
|
62 |
typedef IM ItemIntMap; |
|
67 | 63 |
///\e |
68 |
typedef |
|
64 |
typedef PRIO Prio; |
|
69 | 65 |
///\e |
... | ... |
@@ -73,22 +69,27 @@ |
73 | 69 |
///\e |
74 |
typedef |
|
70 |
typedef CMP Compare; |
|
75 | 71 |
|
76 | 72 |
private: |
77 |
class |
|
73 |
class Store; |
|
78 | 74 |
|
79 |
std::vector<store> container; |
|
80 |
int minimum; |
|
81 |
ItemIntMap &iimap; |
|
82 |
Compare comp; |
|
83 |
|
|
75 |
std::vector<Store> _data; |
|
76 |
int _minimum; |
|
77 |
ItemIntMap &_iim; |
|
78 |
Compare _comp; |
|
79 |
int _num; |
|
84 | 80 |
|
85 | 81 |
public: |
86 |
|
|
82 |
|
|
83 |
/// \brief Type to represent the items states. |
|
84 |
/// |
|
85 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
86 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
87 |
/// heap's point of view, but may be useful to the user. |
|
88 |
/// |
|
89 |
/// The item-int map must be initialized in such way that it assigns |
|
90 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
|
87 | 91 |
enum State { |
88 |
///The node is in the heap |
|
89 |
IN_HEAP = 0, |
|
90 |
///The node has never been in the heap |
|
91 |
PRE_HEAP = -1, |
|
92 |
///The node was in the heap but it got out of it |
|
93 |
POST_HEAP = -2 |
|
92 |
IN_HEAP = 0, ///< = 0. |
|
93 |
PRE_HEAP = -1, ///< = -1. |
|
94 |
POST_HEAP = -2 ///< = -2. |
|
94 | 95 |
}; |
... | ... |
@@ -97,6 +98,6 @@ |
97 | 98 |
/// |
98 |
/// \c |
|
99 |
/// \c map should be given to the constructor, since it is |
|
99 | 100 |
/// used internally to handle the cross references. |
100 |
explicit FibHeap(ItemIntMap &_iimap) |
|
101 |
: minimum(0), iimap(_iimap), num_items() {} |
|
101 |
explicit FibHeap(ItemIntMap &map) |
|
102 |
: _minimum(0), _iim(map), _num() {} |
|
102 | 103 |
|
... | ... |
@@ -104,7 +105,7 @@ |
104 | 105 |
/// |
105 |
/// \c _iimap should be given to the constructor, since it is used |
|
106 |
/// internally to handle the cross references. \c _comp is an |
|
106 |
/// \c map should be given to the constructor, since it is used |
|
107 |
/// internally to handle the cross references. \c comp is an |
|
107 | 108 |
/// object for ordering of the priorities. |
108 |
FibHeap(ItemIntMap &_iimap, const Compare &_comp) |
|
109 |
: minimum(0), iimap(_iimap), comp(_comp), num_items() {} |
|
109 |
FibHeap(ItemIntMap &map, const Compare &comp) |
|
110 |
: _minimum(0), _iim(map), _comp(comp), _num() {} |
|
110 | 111 |
|
... | ... |
@@ -113,3 +114,3 @@ |
113 | 114 |
/// Returns the number of items stored in the heap. |
114 |
int size() const { return |
|
115 |
int size() const { return _num; } |
|
115 | 116 |
|
... | ... |
@@ -118,3 +119,3 @@ |
118 | 119 |
/// Returns \c true if and only if the heap stores no items. |
119 |
bool empty() const { return |
|
120 |
bool empty() const { return _num==0; } |
|
120 | 121 |
|
... | ... |
@@ -127,3 +128,3 @@ |
127 | 128 |
void clear() { |
128 |
|
|
129 |
_data.clear(); _minimum = 0; _num = 0; |
|
129 | 130 |
} |
... | ... |
@@ -137,6 +138,6 @@ |
137 | 138 |
void set (const Item& item, const Prio& value) { |
138 |
int i=iimap[item]; |
|
139 |
if ( i >= 0 && container[i].in ) { |
|
140 |
if ( comp(value, container[i].prio) ) decrease(item, value); |
|
141 |
if ( comp(container[i].prio, value) ) increase(item, value); |
|
139 |
int i=_iim[item]; |
|
140 |
if ( i >= 0 && _data[i].in ) { |
|
141 |
if ( _comp(value, _data[i].prio) ) decrease(item, value); |
|
142 |
if ( _comp(_data[i].prio, value) ) increase(item, value); |
|
142 | 143 |
} else push(item, value); |
... | ... |
@@ -149,29 +150,29 @@ |
149 | 150 |
void push (const Item& item, const Prio& value) { |
150 |
int i= |
|
151 |
int i=_iim[item]; |
|
151 | 152 |
if ( i < 0 ) { |
152 |
int s=container.size(); |
|
153 |
iimap.set( item, s ); |
|
154 |
|
|
153 |
int s=_data.size(); |
|
154 |
_iim.set( item, s ); |
|
155 |
Store st; |
|
155 | 156 |
st.name=item; |
156 |
|
|
157 |
_data.push_back(st); |
|
157 | 158 |
i=s; |
158 | 159 |
} else { |
159 |
container[i].parent=container[i].child=-1; |
|
160 |
container[i].degree=0; |
|
161 |
container[i].in=true; |
|
162 |
container[i].marked=false; |
|
160 |
_data[i].parent=_data[i].child=-1; |
|
161 |
_data[i].degree=0; |
|
162 |
_data[i].in=true; |
|
163 |
_data[i].marked=false; |
|
163 | 164 |
} |
164 | 165 |
|
165 |
if ( num_items ) { |
|
166 |
container[container[minimum].right_neighbor].left_neighbor=i; |
|
167 |
container[i].right_neighbor=container[minimum].right_neighbor; |
|
168 |
container[minimum].right_neighbor=i; |
|
169 |
container[i].left_neighbor=minimum; |
|
170 |
if ( comp( value, container[minimum].prio) ) minimum=i; |
|
166 |
if ( _num ) { |
|
167 |
_data[_data[_minimum].right_neighbor].left_neighbor=i; |
|
168 |
_data[i].right_neighbor=_data[_minimum].right_neighbor; |
|
169 |
_data[_minimum].right_neighbor=i; |
|
170 |
_data[i].left_neighbor=_minimum; |
|
171 |
if ( _comp( value, _data[_minimum].prio) ) _minimum=i; |
|
171 | 172 |
} else { |
172 |
container[i].right_neighbor=container[i].left_neighbor=i; |
|
173 |
minimum=i; |
|
173 |
_data[i].right_neighbor=_data[i].left_neighbor=i; |
|
174 |
_minimum=i; |
|
174 | 175 |
} |
175 |
container[i].prio=value; |
|
176 |
++num_items; |
|
176 |
_data[i].prio=value; |
|
177 |
++_num; |
|
177 | 178 |
} |
... | ... |
@@ -183,3 +184,3 @@ |
183 | 184 |
/// \pre The heap must be nonempty. |
184 |
Item top() const { return |
|
185 |
Item top() const { return _data[_minimum].name; } |
|
185 | 186 |
|
... | ... |
@@ -189,3 +190,3 @@ |
189 | 190 |
/// \pre The heap must be nonempty. |
190 |
const Prio& prio() const { return |
|
191 |
const Prio& prio() const { return _data[_minimum].prio; } |
|
191 | 192 |
|
... | ... |
@@ -196,3 +197,3 @@ |
196 | 197 |
const Prio& operator[](const Item& item) const { |
197 |
return |
|
198 |
return _data[_iim[item]].prio; |
|
198 | 199 |
} |
... | ... |
@@ -206,7 +207,7 @@ |
206 | 207 |
/*The first case is that there are only one root.*/ |
207 |
if ( container[minimum].left_neighbor==minimum ) { |
|
208 |
container[minimum].in=false; |
|
209 |
if ( container[minimum].degree!=0 ) { |
|
210 |
makeroot(container[minimum].child); |
|
211 |
|
|
208 |
if ( _data[_minimum].left_neighbor==_minimum ) { |
|
209 |
_data[_minimum].in=false; |
|
210 |
if ( _data[_minimum].degree!=0 ) { |
|
211 |
makeroot(_data[_minimum].child); |
|
212 |
_minimum=_data[_minimum].child; |
|
212 | 213 |
balance(); |
... | ... |
@@ -214,9 +215,9 @@ |
214 | 215 |
} else { |
215 |
int right=container[minimum].right_neighbor; |
|
216 |
unlace(minimum); |
|
217 |
container[minimum].in=false; |
|
218 |
if ( container[minimum].degree > 0 ) { |
|
219 |
int left=container[minimum].left_neighbor; |
|
220 |
int child=container[minimum].child; |
|
221 |
|
|
216 |
int right=_data[_minimum].right_neighbor; |
|
217 |
unlace(_minimum); |
|
218 |
_data[_minimum].in=false; |
|
219 |
if ( _data[_minimum].degree > 0 ) { |
|
220 |
int left=_data[_minimum].left_neighbor; |
|
221 |
int child=_data[_minimum].child; |
|
222 |
int last_child=_data[child].left_neighbor; |
|
222 | 223 |
|
... | ... |
@@ -224,11 +225,11 @@ |
224 | 225 |
|
225 |
container[left].right_neighbor=child; |
|
226 |
container[child].left_neighbor=left; |
|
227 |
container[right].left_neighbor=last_child; |
|
228 |
container[last_child].right_neighbor=right; |
|
226 |
_data[left].right_neighbor=child; |
|
227 |
_data[child].left_neighbor=left; |
|
228 |
_data[right].left_neighbor=last_child; |
|
229 |
_data[last_child].right_neighbor=right; |
|
229 | 230 |
} |
230 |
|
|
231 |
_minimum=right; |
|
231 | 232 |
balance(); |
232 | 233 |
} // the case where there are more roots |
233 |
-- |
|
234 |
--_num; |
|
234 | 235 |
} |
... | ... |
@@ -240,7 +241,7 @@ |
240 | 241 |
void erase (const Item& item) { |
241 |
int i= |
|
242 |
int i=_iim[item]; |
|
242 | 243 |
|
243 |
if ( i >= 0 && container[i].in ) { |
|
244 |
if ( container[i].parent!=-1 ) { |
|
245 |
|
|
244 |
if ( i >= 0 && _data[i].in ) { |
|
245 |
if ( _data[i].parent!=-1 ) { |
|
246 |
int p=_data[i].parent; |
|
246 | 247 |
cut(i,p); |
... | ... |
@@ -248,3 +249,3 @@ |
248 | 249 |
} |
249 |
|
|
250 |
_minimum=i; //As if its prio would be -infinity |
|
250 | 251 |
pop(); |
... | ... |
@@ -259,7 +260,7 @@ |
259 | 260 |
void decrease (Item item, const Prio& value) { |
260 |
int i=iimap[item]; |
|
261 |
container[i].prio=value; |
|
262 |
int |
|
261 |
int i=_iim[item]; |
|
262 |
_data[i].prio=value; |
|
263 |
int p=_data[i].parent; |
|
263 | 264 |
|
264 |
if ( p!=-1 && |
|
265 |
if ( p!=-1 && _comp(value, _data[p].prio) ) { |
|
265 | 266 |
cut(i,p); |
... | ... |
@@ -267,3 +268,3 @@ |
267 | 268 |
} |
268 |
if ( |
|
269 |
if ( _comp(value, _data[_minimum].prio) ) _minimum=i; |
|
269 | 270 |
} |
... | ... |
@@ -291,5 +292,5 @@ |
291 | 292 |
State state(const Item &item) const { |
292 |
int i= |
|
293 |
int i=_iim[item]; |
|
293 | 294 |
if( i>=0 ) { |
294 |
if ( |
|
295 |
if ( _data[i].in ) i=0; |
|
295 | 296 |
else i=-2; |
... | ... |
@@ -303,3 +304,3 @@ |
303 | 304 |
/// manually clear the heap when it is important to achive the |
304 |
/// better time |
|
305 |
/// better time _complexity. |
|
305 | 306 |
/// \param i The item. |
... | ... |
@@ -313,3 +314,3 @@ |
313 | 314 |
} |
314 |
|
|
315 |
_iim[i] = st; |
|
315 | 316 |
break; |
... | ... |
@@ -324,3 +325,3 @@ |
324 | 325 |
|
325 |
int maxdeg=int( std::floor( 2.08*log(double( |
|
326 |
int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1; |
|
326 | 327 |
|
... | ... |
@@ -332,4 +333,4 @@ |
332 | 333 |
*/ |
333 |
int anchor=container[minimum].left_neighbor; |
|
334 |
int next=minimum; |
|
334 |
int anchor=_data[_minimum].left_neighbor; |
|
335 |
int next=_minimum; |
|
335 | 336 |
bool end=false; |
... | ... |
@@ -339,7 +340,7 @@ |
339 | 340 |
if ( anchor==active ) end=true; |
340 |
int d=container[active].degree; |
|
341 |
next=container[active].right_neighbor; |
|
341 |
int d=_data[active].degree; |
|
342 |
next=_data[active].right_neighbor; |
|
342 | 343 |
|
343 | 344 |
while (A[d]!=-1) { |
344 |
if( |
|
345 |
if( _comp(_data[active].prio, _data[A[d]].prio) ) { |
|
345 | 346 |
fuse(active,A[d]); |
... | ... |
@@ -356,9 +357,9 @@ |
356 | 357 |
|
357 |
while ( container[minimum].parent >=0 ) |
|
358 |
minimum=container[minimum].parent; |
|
359 |
int s=minimum; |
|
360 |
int m=minimum; |
|
358 |
while ( _data[_minimum].parent >=0 ) |
|
359 |
_minimum=_data[_minimum].parent; |
|
360 |
int s=_minimum; |
|
361 |
int m=_minimum; |
|
361 | 362 |
do { |
362 |
if ( comp(container[s].prio, container[minimum].prio) ) minimum=s; |
|
363 |
s=container[s].right_neighbor; |
|
363 |
if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s; |
|
364 |
s=_data[s].right_neighbor; |
|
364 | 365 |
} while ( s != m ); |
... | ... |
@@ -369,4 +370,4 @@ |
369 | 370 |
do { |
370 |
container[s].parent=-1; |
|
371 |
s=container[s].right_neighbor; |
|
371 |
_data[s].parent=-1; |
|
372 |
s=_data[s].right_neighbor; |
|
372 | 373 |
} while ( s != c ); |
... | ... |
@@ -378,8 +379,8 @@ |
378 | 379 |
*/ |
379 |
-- |
|
380 |
--_data[b].degree; |
|
380 | 381 |
|
381 |
if ( container[b].degree !=0 ) { |
|
382 |
int child=container[b].child; |
|
382 |
if ( _data[b].degree !=0 ) { |
|
383 |
int child=_data[b].child; |
|
383 | 384 |
if ( child==a ) |
384 |
|
|
385 |
_data[b].child=_data[child].right_neighbor; |
|
385 | 386 |
unlace(a); |
... | ... |
@@ -389,10 +390,10 @@ |
389 | 390 |
/*Lacing a to the roots.*/ |
390 |
int right=container[minimum].right_neighbor; |
|
391 |
container[minimum].right_neighbor=a; |
|
392 |
container[a].left_neighbor=minimum; |
|
393 |
container[a].right_neighbor=right; |
|
394 |
|
|
391 |
int right=_data[_minimum].right_neighbor; |
|
392 |
_data[_minimum].right_neighbor=a; |
|
393 |
_data[a].left_neighbor=_minimum; |
|
394 |
_data[a].right_neighbor=right; |
|
395 |
_data[right].left_neighbor=a; |
|
395 | 396 |
|
396 |
container[a].parent=-1; |
|
397 |
container[a].marked=false; |
|
397 |
_data[a].parent=-1; |
|
398 |
_data[a].marked=false; |
|
398 | 399 |
} |
... | ... |
@@ -400,6 +401,6 @@ |
400 | 401 |
void cascade(int a) { |
401 |
if ( container[a].parent!=-1 ) { |
|
402 |
int p=container[a].parent; |
|
402 |
if ( _data[a].parent!=-1 ) { |
|
403 |
int p=_data[a].parent; |
|
403 | 404 |
|
404 |
if ( |
|
405 |
if ( _data[a].marked==false ) _data[a].marked=true; |
|
405 | 406 |
else { |
... | ... |
@@ -415,20 +416,20 @@ |
415 | 416 |
/*Lacing b under a.*/ |
416 |
|
|
417 |
_data[b].parent=a; |
|
417 | 418 |
|
418 |
if (container[a].degree==0) { |
|
419 |
container[b].left_neighbor=b; |
|
420 |
container[b].right_neighbor=b; |
|
421 |
container[a].child=b; |
|
419 |
if (_data[a].degree==0) { |
|
420 |
_data[b].left_neighbor=b; |
|
421 |
_data[b].right_neighbor=b; |
|
422 |
_data[a].child=b; |
|
422 | 423 |
} else { |
423 |
int child=container[a].child; |
|
424 |
int last_child=container[child].left_neighbor; |
|
425 |
container[child].left_neighbor=b; |
|
426 |
container[b].right_neighbor=child; |
|
427 |
container[last_child].right_neighbor=b; |
|
428 |
container[b].left_neighbor=last_child; |
|
424 |
int child=_data[a].child; |
|
425 |
int last_child=_data[child].left_neighbor; |
|
426 |
_data[child].left_neighbor=b; |
|
427 |
_data[b].right_neighbor=child; |
|
428 |
_data[last_child].right_neighbor=b; |
|
429 |
_data[b].left_neighbor=last_child; |
|
429 | 430 |
} |
430 | 431 |
|
431 |
++ |
|
432 |
++_data[a].degree; |
|
432 | 433 |
|
433 |
|
|
434 |
_data[b].marked=false; |
|
434 | 435 |
} |
... | ... |
@@ -439,6 +440,6 @@ |
439 | 440 |
void unlace(int a) { |
440 |
int leftn=container[a].left_neighbor; |
|
441 |
int rightn=container[a].right_neighbor; |
|
442 |
container[leftn].right_neighbor=rightn; |
|
443 |
container[rightn].left_neighbor=leftn; |
|
441 |
int leftn=_data[a].left_neighbor; |
|
442 |
int rightn=_data[a].right_neighbor; |
|
443 |
_data[leftn].right_neighbor=rightn; |
|
444 |
_data[rightn].left_neighbor=leftn; |
|
444 | 445 |
} |
... | ... |
@@ -446,3 +447,3 @@ |
446 | 447 |
|
447 |
class |
|
448 |
class Store { |
|
448 | 449 |
friend class FibHeap; |
... | ... |
@@ -459,3 +460,3 @@ |
459 | 460 |
|
460 |
|
|
461 |
Store() : parent(-1), child(-1), degree(), marked(false), in(true) {} |
|
461 | 462 |
}; |
... | ... |
@@ -43,3 +43,3 @@ |
43 | 43 |
/// |
44 |
/// \param |
|
44 |
/// \param IM A read and writable Item int map, used internally |
|
45 | 45 |
/// to handle the cross references. |
... | ... |
@@ -48,3 +48,3 @@ |
48 | 48 |
/// \see Dijkstra |
49 |
template <typename |
|
49 |
template <typename IM> |
|
50 | 50 |
class RadixHeap { |
... | ... |
@@ -52,5 +52,5 @@ |
52 | 52 |
public: |
53 |
typedef typename |
|
53 |
typedef typename IM::Key Item; |
|
54 | 54 |
typedef int Prio; |
55 |
typedef |
|
55 |
typedef IM ItemIntMap; |
|
56 | 56 |
|
... | ... |
@@ -101,3 +101,3 @@ |
101 | 101 |
|
102 |
ItemIntMap & |
|
102 |
ItemIntMap &_iim; |
|
103 | 103 |
|
... | ... |
@@ -109,3 +109,3 @@ |
109 | 109 |
/// |
110 |
/// \param |
|
110 |
/// \param map It should be given to the constructor, since it is used |
|
111 | 111 |
/// internally to handle the cross references. The value of the map |
... | ... |
@@ -115,4 +115,4 @@ |
115 | 115 |
/// \param capacity It determines the initial capacity of the heap. |
116 |
RadixHeap(ItemIntMap &_iim, int minimal = 0, int capacity = 0) |
|
117 |
: iim(_iim) { |
|
116 |
RadixHeap(ItemIntMap &map, int minimal = 0, int capacity = 0) |
|
117 |
: _iim(map) { |
|
118 | 118 |
boxes.push_back(RadixBox(minimal, 1)); |
... | ... |
@@ -270,3 +270,3 @@ |
270 | 270 |
} |
271 |
|
|
271 |
_iim[data[index].item] = index; |
|
272 | 272 |
} |
... | ... |
@@ -284,3 +284,3 @@ |
284 | 284 |
int n = data.size(); |
285 |
|
|
285 |
_iim.set(i, n); |
|
286 | 286 |
data.push_back(RadixItem(i, p)); |
... | ... |
@@ -318,3 +318,3 @@ |
318 | 318 |
int index = boxes[0].first; |
319 |
|
|
319 |
_iim[data[index].item] = POST_HEAP; |
|
320 | 320 |
remove(index); |
... | ... |
@@ -329,4 +329,4 @@ |
329 | 329 |
void erase(const Item &i) { |
330 |
int index = iim[i]; |
|
331 |
iim[i] = POST_HEAP; |
|
330 |
int index = _iim[i]; |
|
331 |
_iim[i] = POST_HEAP; |
|
332 | 332 |
remove(index); |
... | ... |
@@ -341,3 +341,3 @@ |
341 | 341 |
Prio operator[](const Item &i) const { |
342 |
int idx = |
|
342 |
int idx = _iim[i]; |
|
343 | 343 |
return data[idx].prio; |
... | ... |
@@ -354,3 +354,3 @@ |
354 | 354 |
void set(const Item &i, const Prio &p) { |
355 |
int idx = |
|
355 |
int idx = _iim[i]; |
|
356 | 356 |
if( idx < 0 ) { |
... | ... |
@@ -376,3 +376,3 @@ |
376 | 376 |
void decrease(const Item &i, const Prio &p) { |
377 |
int idx = |
|
377 |
int idx = _iim[i]; |
|
378 | 378 |
data[idx].prio = p; |
... | ... |
@@ -388,3 +388,3 @@ |
388 | 388 |
void increase(const Item &i, const Prio &p) { |
389 |
int idx = |
|
389 |
int idx = _iim[i]; |
|
390 | 390 |
data[idx].prio = p; |
... | ... |
@@ -402,3 +402,3 @@ |
402 | 402 |
State state(const Item &i) const { |
403 |
int s = |
|
403 |
int s = _iim[i]; |
|
404 | 404 |
if( s >= 0 ) s = 0; |
... | ... |
@@ -421,3 +421,3 @@ |
421 | 421 |
} |
422 |
|
|
422 |
_iim[i] = st; |
|
423 | 423 |
break; |
0 comments (0 inline)