0
7
3
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2009 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BUCKET_HEAP_H |
|
20 |
#define LEMON_BUCKET_HEAP_H |
|
21 |
|
|
22 |
///\ingroup auxdat |
|
23 |
///\file |
|
24 |
///\brief Bucket Heap implementation. |
|
25 |
|
|
26 |
#include <vector> |
|
27 |
#include <utility> |
|
28 |
#include <functional> |
|
29 |
|
|
30 |
namespace lemon { |
|
31 |
|
|
32 |
namespace _bucket_heap_bits { |
|
33 |
|
|
34 |
template <bool MIN> |
|
35 |
struct DirectionTraits { |
|
36 |
static bool less(int left, int right) { |
|
37 |
return left < right; |
|
38 |
} |
|
39 |
static void increase(int& value) { |
|
40 |
++value; |
|
41 |
} |
|
42 |
}; |
|
43 |
|
|
44 |
template <> |
|
45 |
struct DirectionTraits<false> { |
|
46 |
static bool less(int left, int right) { |
|
47 |
return left > right; |
|
48 |
} |
|
49 |
static void increase(int& value) { |
|
50 |
--value; |
|
51 |
} |
|
52 |
}; |
|
53 |
|
|
54 |
} |
|
55 |
|
|
56 |
/// \ingroup auxdat |
|
57 |
/// |
|
58 |
/// \brief A Bucket Heap implementation. |
|
59 |
/// |
|
60 |
/// This class implements the \e bucket \e heap data structure. A \e heap |
|
61 |
/// is a data structure for storing items with specified values called \e |
|
62 |
/// priorities in such a way that finding the item with minimum priority is |
|
63 |
/// efficient. The bucket heap is very simple implementation, it can store |
|
64 |
/// only integer priorities and it stores for each priority in the |
|
65 |
/// \f$ [0..C) \f$ range a list of items. So it should be used only when |
|
66 |
/// the priorities are small. It is not intended to use as dijkstra heap. |
|
67 |
/// |
|
68 |
/// \param IM A read and write Item int map, used internally |
|
69 |
/// to handle the cross references. |
|
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> |
|
74 |
class BucketHeap { |
|
75 |
|
|
76 |
public: |
|
77 |
/// \e |
|
78 |
typedef typename IM::Key Item; |
|
79 |
/// \e |
|
80 |
typedef int Prio; |
|
81 |
/// \e |
|
82 |
typedef std::pair<Item, Prio> Pair; |
|
83 |
/// \e |
|
84 |
typedef IM ItemIntMap; |
|
85 |
|
|
86 |
private: |
|
87 |
|
|
88 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
|
89 |
|
|
90 |
public: |
|
91 |
|
|
92 |
/// \brief Type to represent the items states. |
|
93 |
/// |
|
94 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
95 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
96 |
/// heap's point of view, but may be useful to the user. |
|
97 |
/// |
|
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. |
|
100 |
enum State { |
|
101 |
IN_HEAP = 0, ///< = 0. |
|
102 |
PRE_HEAP = -1, ///< = -1. |
|
103 |
POST_HEAP = -2 ///< = -2. |
|
104 |
}; |
|
105 |
|
|
106 |
public: |
|
107 |
/// \brief The constructor. |
|
108 |
/// |
|
109 |
/// The constructor. |
|
110 |
/// \param map should be given to the constructor, since it is used |
|
111 |
/// internally to handle the cross references. The value of the map |
|
112 |
/// should be PRE_HEAP (-1) for each element. |
|
113 |
explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} |
|
114 |
|
|
115 |
/// The number of items stored in the heap. |
|
116 |
/// |
|
117 |
/// \brief Returns the number of items stored in the heap. |
|
118 |
int size() const { return _data.size(); } |
|
119 |
|
|
120 |
/// \brief Checks if the heap stores no items. |
|
121 |
/// |
|
122 |
/// Returns \c true if and only if the heap stores no items. |
|
123 |
bool empty() const { return _data.empty(); } |
|
124 |
|
|
125 |
/// \brief Make empty this heap. |
|
126 |
/// |
|
127 |
/// Make empty this heap. It does not change the cross reference |
|
128 |
/// map. If you want to reuse a heap what is not surely empty you |
|
129 |
/// should first clear the heap and after that you should set the |
|
130 |
/// cross reference map for each item to \c PRE_HEAP. |
|
131 |
void clear() { |
|
132 |
_data.clear(); _first.clear(); _minimum = 0; |
|
133 |
} |
|
134 |
|
|
135 |
private: |
|
136 |
|
|
137 |
void relocate_last(int 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; |
|
142 |
} else { |
|
143 |
_first[_data[idx].value] = idx; |
|
144 |
} |
|
145 |
if (_data[idx].next != -1) { |
|
146 |
_data[_data[idx].next].prev = idx; |
|
147 |
} |
|
148 |
_iim[_data[idx].item] = idx; |
|
149 |
} |
|
150 |
_data.pop_back(); |
|
151 |
} |
|
152 |
|
|
153 |
void unlace(int idx) { |
|
154 |
if (_data[idx].prev != -1) { |
|
155 |
_data[_data[idx].prev].next = _data[idx].next; |
|
156 |
} else { |
|
157 |
_first[_data[idx].value] = _data[idx].next; |
|
158 |
} |
|
159 |
if (_data[idx].next != -1) { |
|
160 |
_data[_data[idx].next].prev = _data[idx].prev; |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
void lace(int idx) { |
|
165 |
if (int(_first.size()) <= _data[idx].value) { |
|
166 |
_first.resize(_data[idx].value + 1, -1); |
|
167 |
} |
|
168 |
_data[idx].next = _first[_data[idx].value]; |
|
169 |
if (_data[idx].next != -1) { |
|
170 |
_data[_data[idx].next].prev = idx; |
|
171 |
} |
|
172 |
_first[_data[idx].value] = idx; |
|
173 |
_data[idx].prev = -1; |
|
174 |
} |
|
175 |
|
|
176 |
public: |
|
177 |
/// \brief Insert a pair of item and priority into the heap. |
|
178 |
/// |
|
179 |
/// Adds \c p.first to the heap with priority \c p.second. |
|
180 |
/// \param p The pair to insert. |
|
181 |
void push(const Pair& p) { |
|
182 |
push(p.first, p.second); |
|
183 |
} |
|
184 |
|
|
185 |
/// \brief Insert an item into the heap with the given priority. |
|
186 |
/// |
|
187 |
/// Adds \c i to the heap with priority \c p. |
|
188 |
/// \param i The item to insert. |
|
189 |
/// \param p The priority of the item. |
|
190 |
void push(const Item &i, const Prio &p) { |
|
191 |
int idx = _data.size(); |
|
192 |
_iim[i] = idx; |
|
193 |
_data.push_back(BucketItem(i, p)); |
|
194 |
lace(idx); |
|
195 |
if (Direction::less(p, _minimum)) { |
|
196 |
_minimum = p; |
|
197 |
} |
|
198 |
} |
|
199 |
|
|
200 |
/// \brief Returns the item with minimum priority. |
|
201 |
/// |
|
202 |
/// This method returns the item with minimum priority. |
|
203 |
/// \pre The heap must be nonempty. |
|
204 |
Item top() const { |
|
205 |
while (_first[_minimum] == -1) { |
|
206 |
Direction::increase(_minimum); |
|
207 |
} |
|
208 |
return _data[_first[_minimum]].item; |
|
209 |
} |
|
210 |
|
|
211 |
/// \brief Returns the minimum priority. |
|
212 |
/// |
|
213 |
/// It returns the minimum priority. |
|
214 |
/// \pre The heap must be nonempty. |
|
215 |
Prio prio() const { |
|
216 |
while (_first[_minimum] == -1) { |
|
217 |
Direction::increase(_minimum); |
|
218 |
} |
|
219 |
return _minimum; |
|
220 |
} |
|
221 |
|
|
222 |
/// \brief Deletes the item with minimum priority. |
|
223 |
/// |
|
224 |
/// This method deletes the item with minimum priority from the heap. |
|
225 |
/// \pre The heap must be non-empty. |
|
226 |
void pop() { |
|
227 |
while (_first[_minimum] == -1) { |
|
228 |
Direction::increase(_minimum); |
|
229 |
} |
|
230 |
int idx = _first[_minimum]; |
|
231 |
_iim[_data[idx].item] = -2; |
|
232 |
unlace(idx); |
|
233 |
relocate_last(idx); |
|
234 |
} |
|
235 |
|
|
236 |
/// \brief Deletes \c i from the heap. |
|
237 |
/// |
|
238 |
/// This method deletes item \c i from the heap, if \c i was |
|
239 |
/// already stored in the heap. |
|
240 |
/// \param i The item to erase. |
|
241 |
void erase(const Item &i) { |
|
242 |
int idx = _iim[i]; |
|
243 |
_iim[_data[idx].item] = -2; |
|
244 |
unlace(idx); |
|
245 |
relocate_last(idx); |
|
246 |
} |
|
247 |
|
|
248 |
|
|
249 |
/// \brief Returns the priority of \c i. |
|
250 |
/// |
|
251 |
/// This function returns the priority of item \c i. |
|
252 |
/// \pre \c i must be in the heap. |
|
253 |
/// \param i The item. |
|
254 |
Prio operator[](const Item &i) const { |
|
255 |
int idx = _iim[i]; |
|
256 |
return _data[idx].value; |
|
257 |
} |
|
258 |
|
|
259 |
/// \brief \c i gets to the heap with priority \c p independently |
|
260 |
/// if \c i was already there. |
|
261 |
/// |
|
262 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
|
263 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
|
264 |
/// \param i The item. |
|
265 |
/// \param p The priority. |
|
266 |
void set(const Item &i, const Prio &p) { |
|
267 |
int idx = _iim[i]; |
|
268 |
if (idx < 0) { |
|
269 |
push(i, p); |
|
270 |
} else if (Direction::less(p, _data[idx].value)) { |
|
271 |
decrease(i, p); |
|
272 |
} else { |
|
273 |
increase(i, p); |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
/// \brief Decreases the priority of \c i to \c p. |
|
278 |
/// |
|
279 |
/// This method decreases the priority of item \c i to \c p. |
|
280 |
/// \pre \c i must be stored in the heap with priority at least \c |
|
281 |
/// p relative to \c Compare. |
|
282 |
/// \param i The item. |
|
283 |
/// \param p The priority. |
|
284 |
void decrease(const Item &i, const Prio &p) { |
|
285 |
int idx = _iim[i]; |
|
286 |
unlace(idx); |
|
287 |
_data[idx].value = p; |
|
288 |
if (Direction::less(p, _minimum)) { |
|
289 |
_minimum = p; |
|
290 |
} |
|
291 |
lace(idx); |
|
292 |
} |
|
293 |
|
|
294 |
/// \brief Increases the priority of \c i to \c p. |
|
295 |
/// |
|
296 |
/// This method sets the priority of item \c i to \c p. |
|
297 |
/// \pre \c i must be stored in the heap with priority at most \c |
|
298 |
/// p relative to \c Compare. |
|
299 |
/// \param i The item. |
|
300 |
/// \param p The priority. |
|
301 |
void increase(const Item &i, const Prio &p) { |
|
302 |
int idx = _iim[i]; |
|
303 |
unlace(idx); |
|
304 |
_data[idx].value = p; |
|
305 |
lace(idx); |
|
306 |
} |
|
307 |
|
|
308 |
/// \brief Returns if \c item is in, has already been in, or has |
|
309 |
/// never been in the heap. |
|
310 |
/// |
|
311 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
312 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
313 |
/// otherwise. In the latter case it is possible that \c item will |
|
314 |
/// get back to the heap again. |
|
315 |
/// \param i The item. |
|
316 |
State state(const Item &i) const { |
|
317 |
int idx = _iim[i]; |
|
318 |
if (idx >= 0) idx = 0; |
|
319 |
return State(idx); |
|
320 |
} |
|
321 |
|
|
322 |
/// \brief Sets the state of the \c item in the heap. |
|
323 |
/// |
|
324 |
/// Sets the state of the \c item in the heap. It can be used to |
|
325 |
/// manually clear the heap when it is important to achive the |
|
326 |
/// better time complexity. |
|
327 |
/// \param i The item. |
|
328 |
/// \param st The state. It should not be \c IN_HEAP. |
|
329 |
void state(const Item& i, State st) { |
|
330 |
switch (st) { |
|
331 |
case POST_HEAP: |
|
332 |
case PRE_HEAP: |
|
333 |
if (state(i) == IN_HEAP) { |
|
334 |
erase(i); |
|
335 |
} |
|
336 |
_iim[i] = st; |
|
337 |
break; |
|
338 |
case IN_HEAP: |
|
339 |
break; |
|
340 |
} |
|
341 |
} |
|
342 |
|
|
343 |
private: |
|
344 |
|
|
345 |
struct BucketItem { |
|
346 |
BucketItem(const Item& _item, int _value) |
|
347 |
: item(_item), value(_value) {} |
|
348 |
|
|
349 |
Item item; |
|
350 |
int value; |
|
351 |
|
|
352 |
int prev, next; |
|
353 |
}; |
|
354 |
|
|
355 |
ItemIntMap& _iim; |
|
356 |
std::vector<int> _first; |
|
357 |
std::vector<BucketItem> _data; |
|
358 |
mutable int _minimum; |
|
359 |
|
|
360 |
}; // class BucketHeap |
|
361 |
|
|
362 |
/// \ingroup auxdat |
|
363 |
/// |
|
364 |
/// \brief A Simplified Bucket Heap implementation. |
|
365 |
/// |
|
366 |
/// This class implements a simplified \e bucket \e heap data |
|
367 |
/// structure. It does not provide some functionality but it faster |
|
368 |
/// and simplier data structure than the BucketHeap. The main |
|
369 |
/// difference is that the BucketHeap stores for every key a double |
|
370 |
/// linked list while this class stores just simple lists. In the |
|
371 |
/// other way it does not support erasing each elements just the |
|
372 |
/// minimal and it does not supports key increasing, decreasing. |
|
373 |
/// |
|
374 |
/// \param IM A read and write Item int map, used internally |
|
375 |
/// to handle the cross references. |
|
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. |
|
379 |
/// |
|
380 |
/// \sa BucketHeap |
|
381 |
template <typename IM, bool MIN = true > |
|
382 |
class SimpleBucketHeap { |
|
383 |
|
|
384 |
public: |
|
385 |
typedef typename IM::Key Item; |
|
386 |
typedef int Prio; |
|
387 |
typedef std::pair<Item, Prio> Pair; |
|
388 |
typedef IM ItemIntMap; |
|
389 |
|
|
390 |
private: |
|
391 |
|
|
392 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
|
393 |
|
|
394 |
public: |
|
395 |
|
|
396 |
/// \brief Type to represent the items states. |
|
397 |
/// |
|
398 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
399 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
400 |
/// heap's point of view, but may be useful to the user. |
|
401 |
/// |
|
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. |
|
404 |
enum State { |
|
405 |
IN_HEAP = 0, ///< = 0. |
|
406 |
PRE_HEAP = -1, ///< = -1. |
|
407 |
POST_HEAP = -2 ///< = -2. |
|
408 |
}; |
|
409 |
|
|
410 |
public: |
|
411 |
|
|
412 |
/// \brief The constructor. |
|
413 |
/// |
|
414 |
/// The constructor. |
|
415 |
/// \param map should be given to the constructor, since it is used |
|
416 |
/// internally to handle the cross references. The value of the map |
|
417 |
/// should be PRE_HEAP (-1) for each element. |
|
418 |
explicit SimpleBucketHeap(ItemIntMap &map) |
|
419 |
: _iim(map), _free(-1), _num(0), _minimum(0) {} |
|
420 |
|
|
421 |
/// \brief Returns the number of items stored in the heap. |
|
422 |
/// |
|
423 |
/// The number of items stored in the heap. |
|
424 |
int size() const { return _num; } |
|
425 |
|
|
426 |
/// \brief Checks if the heap stores no items. |
|
427 |
/// |
|
428 |
/// Returns \c true if and only if the heap stores no items. |
|
429 |
bool empty() const { return _num == 0; } |
|
430 |
|
|
431 |
/// \brief Make empty this heap. |
|
432 |
/// |
|
433 |
/// Make empty this heap. It does not change the cross reference |
|
434 |
/// map. If you want to reuse a heap what is not surely empty you |
|
435 |
/// should first clear the heap and after that you should set the |
|
436 |
/// cross reference map for each item to \c PRE_HEAP. |
|
437 |
void clear() { |
|
438 |
_data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; |
|
439 |
} |
|
440 |
|
|
441 |
/// \brief Insert a pair of item and priority into the heap. |
|
442 |
/// |
|
443 |
/// Adds \c p.first to the heap with priority \c p.second. |
|
444 |
/// \param p The pair to insert. |
|
445 |
void push(const Pair& p) { |
|
446 |
push(p.first, p.second); |
|
447 |
} |
|
448 |
|
|
449 |
/// \brief Insert an item into the heap with the given priority. |
|
450 |
/// |
|
451 |
/// Adds \c i to the heap with priority \c p. |
|
452 |
/// \param i The item to insert. |
|
453 |
/// \param p The priority of the item. |
|
454 |
void push(const Item &i, const Prio &p) { |
|
455 |
int idx; |
|
456 |
if (_free == -1) { |
|
457 |
idx = _data.size(); |
|
458 |
_data.push_back(BucketItem(i)); |
|
459 |
} else { |
|
460 |
idx = _free; |
|
461 |
_free = _data[idx].next; |
|
462 |
_data[idx].item = i; |
|
463 |
} |
|
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; |
|
470 |
} |
|
471 |
++_num; |
|
472 |
} |
|
473 |
|
|
474 |
/// \brief Returns the item with minimum priority. |
|
475 |
/// |
|
476 |
/// This method returns the item with minimum priority. |
|
477 |
/// \pre The heap must be nonempty. |
|
478 |
Item top() const { |
|
479 |
while (_first[_minimum] == -1) { |
|
480 |
Direction::increase(_minimum); |
|
481 |
} |
|
482 |
return _data[_first[_minimum]].item; |
|
483 |
} |
|
484 |
|
|
485 |
/// \brief Returns the minimum priority. |
|
486 |
/// |
|
487 |
/// It returns the minimum priority. |
|
488 |
/// \pre The heap must be nonempty. |
|
489 |
Prio prio() const { |
|
490 |
while (_first[_minimum] == -1) { |
|
491 |
Direction::increase(_minimum); |
|
492 |
} |
|
493 |
return _minimum; |
|
494 |
} |
|
495 |
|
|
496 |
/// \brief Deletes the item with minimum priority. |
|
497 |
/// |
|
498 |
/// This method deletes the item with minimum priority from the heap. |
|
499 |
/// \pre The heap must be non-empty. |
|
500 |
void pop() { |
|
501 |
while (_first[_minimum] == -1) { |
|
502 |
Direction::increase(_minimum); |
|
503 |
} |
|
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; |
|
510 |
} |
|
511 |
|
|
512 |
/// \brief Returns the priority of \c i. |
|
513 |
/// |
|
514 |
/// This function returns the priority of item \c i. |
|
515 |
/// \warning This operator is not a constant time function |
|
516 |
/// because it scans the whole data structure to find the proper |
|
517 |
/// value. |
|
518 |
/// \pre \c i must be in the heap. |
|
519 |
/// \param i The item. |
|
520 |
Prio operator[](const Item &i) const { |
|
521 |
for (int k = 0; k < _first.size(); ++k) { |
|
522 |
int idx = _first[k]; |
|
523 |
while (idx != -1) { |
|
524 |
if (_data[idx].item == i) { |
|
525 |
return k; |
|
526 |
} |
|
527 |
idx = _data[idx].next; |
|
528 |
} |
|
529 |
} |
|
530 |
return -1; |
|
531 |
} |
|
532 |
|
|
533 |
/// \brief Returns if \c item is in, has already been in, or has |
|
534 |
/// never been in the heap. |
|
535 |
/// |
|
536 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
537 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
538 |
/// otherwise. In the latter case it is possible that \c item will |
|
539 |
/// get back to the heap again. |
|
540 |
/// \param i The item. |
|
541 |
State state(const Item &i) const { |
|
542 |
int idx = _iim[i]; |
|
543 |
if (idx >= 0) idx = 0; |
|
544 |
return State(idx); |
|
545 |
} |
|
546 |
|
|
547 |
private: |
|
548 |
|
|
549 |
struct BucketItem { |
|
550 |
BucketItem(const Item& _item) |
|
551 |
: item(_item) {} |
|
552 |
|
|
553 |
Item item; |
|
554 |
int next; |
|
555 |
}; |
|
556 |
|
|
557 |
ItemIntMap& _iim; |
|
558 |
std::vector<int> _first; |
|
559 |
std::vector<BucketItem> _data; |
|
560 |
int _free, _num; |
|
561 |
mutable int _minimum; |
|
562 |
|
|
563 |
}; // class SimpleBucketHeap |
|
564 |
|
|
565 |
} |
|
566 |
|
|
567 |
#endif |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2009 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_FIB_HEAP_H |
|
20 |
#define LEMON_FIB_HEAP_H |
|
21 |
|
|
22 |
///\file |
|
23 |
///\ingroup auxdat |
|
24 |
///\brief Fibonacci Heap implementation. |
|
25 |
|
|
26 |
#include <vector> |
|
27 |
#include <functional> |
|
28 |
#include <lemon/math.h> |
|
29 |
|
|
30 |
namespace lemon { |
|
31 |
|
|
32 |
/// \ingroup auxdat |
|
33 |
/// |
|
34 |
///\brief Fibonacci Heap. |
|
35 |
/// |
|
36 |
///This class implements the \e Fibonacci \e heap data structure. A \e heap |
|
37 |
///is a data structure for storing items with specified values called \e |
|
38 |
///priorities in such a way that finding the item with minimum priority is |
|
39 |
///efficient. \c CMP specifies the ordering of the priorities. In a heap |
|
40 |
///one can change the priority of an item, add or erase an item, etc. |
|
41 |
/// |
|
42 |
///The methods \ref increase and \ref erase are not efficient in a Fibonacci |
|
43 |
///heap. In case of many calls to these operations, it is better to use a |
|
44 |
///\ref BinHeap "binary heap". |
|
45 |
/// |
|
46 |
///\param PRIO Type of the priority of the items. |
|
47 |
///\param IM A read and writable Item int map, used internally |
|
48 |
///to handle the cross references. |
|
49 |
///\param CMP A class for the ordering of the priorities. The |
|
50 |
///default is \c std::less<PRIO>. |
|
51 |
/// |
|
52 |
///\sa BinHeap |
|
53 |
///\sa Dijkstra |
|
54 |
#ifdef DOXYGEN |
|
55 |
template <typename PRIO, typename IM, typename CMP> |
|
56 |
#else |
|
57 |
template <typename PRIO, typename IM, typename CMP = std::less<PRIO> > |
|
58 |
#endif |
|
59 |
class FibHeap { |
|
60 |
public: |
|
61 |
///\e |
|
62 |
typedef IM ItemIntMap; |
|
63 |
///\e |
|
64 |
typedef PRIO Prio; |
|
65 |
///\e |
|
66 |
typedef typename ItemIntMap::Key Item; |
|
67 |
///\e |
|
68 |
typedef std::pair<Item,Prio> Pair; |
|
69 |
///\e |
|
70 |
typedef CMP Compare; |
|
71 |
|
|
72 |
private: |
|
73 |
class Store; |
|
74 |
|
|
75 |
std::vector<Store> _data; |
|
76 |
int _minimum; |
|
77 |
ItemIntMap &_iim; |
|
78 |
Compare _comp; |
|
79 |
int _num; |
|
80 |
|
|
81 |
public: |
|
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. |
|
91 |
enum State { |
|
92 |
IN_HEAP = 0, ///< = 0. |
|
93 |
PRE_HEAP = -1, ///< = -1. |
|
94 |
POST_HEAP = -2 ///< = -2. |
|
95 |
}; |
|
96 |
|
|
97 |
/// \brief The constructor |
|
98 |
/// |
|
99 |
/// \c map should be given to the constructor, since it is |
|
100 |
/// used internally to handle the cross references. |
|
101 |
explicit FibHeap(ItemIntMap &map) |
|
102 |
: _minimum(0), _iim(map), _num() {} |
|
103 |
|
|
104 |
/// \brief The constructor |
|
105 |
/// |
|
106 |
/// \c map should be given to the constructor, since it is used |
|
107 |
/// internally to handle the cross references. \c comp is an |
|
108 |
/// object for ordering of the priorities. |
|
109 |
FibHeap(ItemIntMap &map, const Compare &comp) |
|
110 |
: _minimum(0), _iim(map), _comp(comp), _num() {} |
|
111 |
|
|
112 |
/// \brief The number of items stored in the heap. |
|
113 |
/// |
|
114 |
/// Returns the number of items stored in the heap. |
|
115 |
int size() const { return _num; } |
|
116 |
|
|
117 |
/// \brief Checks if the heap stores no items. |
|
118 |
/// |
|
119 |
/// Returns \c true if and only if the heap stores no items. |
|
120 |
bool empty() const { return _num==0; } |
|
121 |
|
|
122 |
/// \brief Make empty this heap. |
|
123 |
/// |
|
124 |
/// Make empty this heap. It does not change the cross reference |
|
125 |
/// map. If you want to reuse a heap what is not surely empty you |
|
126 |
/// should first clear the heap and after that you should set the |
|
127 |
/// cross reference map for each item to \c PRE_HEAP. |
|
128 |
void clear() { |
|
129 |
_data.clear(); _minimum = 0; _num = 0; |
|
130 |
} |
|
131 |
|
|
132 |
/// \brief \c item gets to the heap with priority \c value independently |
|
133 |
/// if \c item was already there. |
|
134 |
/// |
|
135 |
/// This method calls \ref push(\c item, \c value) if \c item is not |
|
136 |
/// stored in the heap and it calls \ref decrease(\c item, \c value) or |
|
137 |
/// \ref increase(\c item, \c value) otherwise. |
|
138 |
void set (const Item& item, const Prio& 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); |
|
143 |
} else push(item, value); |
|
144 |
} |
|
145 |
|
|
146 |
/// \brief Adds \c item to the heap with priority \c value. |
|
147 |
/// |
|
148 |
/// Adds \c item to the heap with priority \c value. |
|
149 |
/// \pre \c item must not be stored in the heap. |
|
150 |
void push (const Item& item, const Prio& value) { |
|
151 |
int i=_iim[item]; |
|
152 |
if ( i < 0 ) { |
|
153 |
int s=_data.size(); |
|
154 |
_iim.set( item, s ); |
|
155 |
Store st; |
|
156 |
st.name=item; |
|
157 |
_data.push_back(st); |
|
158 |
i=s; |
|
159 |
} else { |
|
160 |
_data[i].parent=_data[i].child=-1; |
|
161 |
_data[i].degree=0; |
|
162 |
_data[i].in=true; |
|
163 |
_data[i].marked=false; |
|
164 |
} |
|
165 |
|
|
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; |
|
172 |
} else { |
|
173 |
_data[i].right_neighbor=_data[i].left_neighbor=i; |
|
174 |
_minimum=i; |
|
175 |
} |
|
176 |
_data[i].prio=value; |
|
177 |
++_num; |
|
178 |
} |
|
179 |
|
|
180 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
|
181 |
/// |
|
182 |
/// This method returns the item with minimum priority relative to \c |
|
183 |
/// Compare. |
|
184 |
/// \pre The heap must be nonempty. |
|
185 |
Item top() const { return _data[_minimum].name; } |
|
186 |
|
|
187 |
/// \brief Returns the minimum priority relative to \c Compare. |
|
188 |
/// |
|
189 |
/// It returns the minimum priority relative to \c Compare. |
|
190 |
/// \pre The heap must be nonempty. |
|
191 |
const Prio& prio() const { return _data[_minimum].prio; } |
|
192 |
|
|
193 |
/// \brief Returns the priority of \c item. |
|
194 |
/// |
|
195 |
/// It returns the priority of \c item. |
|
196 |
/// \pre \c item must be in the heap. |
|
197 |
const Prio& operator[](const Item& item) const { |
|
198 |
return _data[_iim[item]].prio; |
|
199 |
} |
|
200 |
|
|
201 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
|
202 |
/// |
|
203 |
/// This method deletes the item with minimum priority relative to \c |
|
204 |
/// Compare from the heap. |
|
205 |
/// \pre The heap must be non-empty. |
|
206 |
void pop() { |
|
207 |
/*The first case is that there are only one root.*/ |
|
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; |
|
213 |
balance(); |
|
214 |
} |
|
215 |
} else { |
|
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; |
|
223 |
|
|
224 |
makeroot(child); |
|
225 |
|
|
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; |
|
230 |
} |
|
231 |
_minimum=right; |
|
232 |
balance(); |
|
233 |
} // the case where there are more roots |
|
234 |
--_num; |
|
235 |
} |
|
236 |
|
|
237 |
/// \brief Deletes \c item from the heap. |
|
238 |
/// |
|
239 |
/// This method deletes \c item from the heap, if \c item was already |
|
240 |
/// stored in the heap. It is quite inefficient in Fibonacci heaps. |
|
241 |
void erase (const Item& item) { |
|
242 |
int i=_iim[item]; |
|
243 |
|
|
244 |
if ( i >= 0 && _data[i].in ) { |
|
245 |
if ( _data[i].parent!=-1 ) { |
|
246 |
int p=_data[i].parent; |
|
247 |
cut(i,p); |
|
248 |
cascade(p); |
|
249 |
} |
|
250 |
_minimum=i; //As if its prio would be -infinity |
|
251 |
pop(); |
|
252 |
} |
|
253 |
} |
|
254 |
|
|
255 |
/// \brief Decreases the priority of \c item to \c value. |
|
256 |
/// |
|
257 |
/// This method decreases the priority of \c item to \c value. |
|
258 |
/// \pre \c item must be stored in the heap with priority at least \c |
|
259 |
/// value relative to \c Compare. |
|
260 |
void decrease (Item item, const Prio& value) { |
|
261 |
int i=_iim[item]; |
|
262 |
_data[i].prio=value; |
|
263 |
int p=_data[i].parent; |
|
264 |
|
|
265 |
if ( p!=-1 && _comp(value, _data[p].prio) ) { |
|
266 |
cut(i,p); |
|
267 |
cascade(p); |
|
268 |
} |
|
269 |
if ( _comp(value, _data[_minimum].prio) ) _minimum=i; |
|
270 |
} |
|
271 |
|
|
272 |
/// \brief Increases the priority of \c item to \c value. |
|
273 |
/// |
|
274 |
/// This method sets the priority of \c item to \c value. Though |
|
275 |
/// there is no precondition on the priority of \c item, this |
|
276 |
/// method should be used only if it is indeed necessary to increase |
|
277 |
/// (relative to \c Compare) the priority of \c item, because this |
|
278 |
/// method is inefficient. |
|
279 |
void increase (Item item, const Prio& value) { |
|
280 |
erase(item); |
|
281 |
push(item, value); |
|
282 |
} |
|
283 |
|
|
284 |
|
|
285 |
/// \brief Returns if \c item is in, has already been in, or has never |
|
286 |
/// been in the heap. |
|
287 |
/// |
|
288 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
289 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
290 |
/// otherwise. In the latter case it is possible that \c item will |
|
291 |
/// get back to the heap again. |
|
292 |
State state(const Item &item) const { |
|
293 |
int i=_iim[item]; |
|
294 |
if( i>=0 ) { |
|
295 |
if ( _data[i].in ) i=0; |
|
296 |
else i=-2; |
|
297 |
} |
|
298 |
return State(i); |
|
299 |
} |
|
300 |
|
|
301 |
/// \brief Sets the state of the \c item in the heap. |
|
302 |
/// |
|
303 |
/// Sets the state of the \c item in the heap. It can be used to |
|
304 |
/// manually clear the heap when it is important to achive the |
|
305 |
/// better time _complexity. |
|
306 |
/// \param i The item. |
|
307 |
/// \param st The state. It should not be \c IN_HEAP. |
|
308 |
void state(const Item& i, State st) { |
|
309 |
switch (st) { |
|
310 |
case POST_HEAP: |
|
311 |
case PRE_HEAP: |
|
312 |
if (state(i) == IN_HEAP) { |
|
313 |
erase(i); |
|
314 |
} |
|
315 |
_iim[i] = st; |
|
316 |
break; |
|
317 |
case IN_HEAP: |
|
318 |
break; |
|
319 |
} |
|
320 |
} |
|
321 |
|
|
322 |
private: |
|
323 |
|
|
324 |
void balance() { |
|
325 |
|
|
326 |
int maxdeg=int( std::floor( 2.08*log(double(_data.size()))))+1; |
|
327 |
|
|
328 |
std::vector<int> A(maxdeg,-1); |
|
329 |
|
|
330 |
/* |
|
331 |
*Recall that now minimum does not point to the minimum prio element. |
|
332 |
*We set minimum to this during balance(). |
|
333 |
*/ |
|
334 |
int anchor=_data[_minimum].left_neighbor; |
|
335 |
int next=_minimum; |
|
336 |
bool end=false; |
|
337 |
|
|
338 |
do { |
|
339 |
int active=next; |
|
340 |
if ( anchor==active ) end=true; |
|
341 |
int d=_data[active].degree; |
|
342 |
next=_data[active].right_neighbor; |
|
343 |
|
|
344 |
while (A[d]!=-1) { |
|
345 |
if( _comp(_data[active].prio, _data[A[d]].prio) ) { |
|
346 |
fuse(active,A[d]); |
|
347 |
} else { |
|
348 |
fuse(A[d],active); |
|
349 |
active=A[d]; |
|
350 |
} |
|
351 |
A[d]=-1; |
|
352 |
++d; |
|
353 |
} |
|
354 |
A[d]=active; |
|
355 |
} while ( !end ); |
|
356 |
|
|
357 |
|
|
358 |
while ( _data[_minimum].parent >=0 ) |
|
359 |
_minimum=_data[_minimum].parent; |
|
360 |
int s=_minimum; |
|
361 |
int m=_minimum; |
|
362 |
do { |
|
363 |
if ( _comp(_data[s].prio, _data[_minimum].prio) ) _minimum=s; |
|
364 |
s=_data[s].right_neighbor; |
|
365 |
} while ( s != m ); |
|
366 |
} |
|
367 |
|
|
368 |
void makeroot(int c) { |
|
369 |
int s=c; |
|
370 |
do { |
|
371 |
_data[s].parent=-1; |
|
372 |
s=_data[s].right_neighbor; |
|
373 |
} while ( s != c ); |
|
374 |
} |
|
375 |
|
|
376 |
void cut(int a, int b) { |
|
377 |
/* |
|
378 |
*Replacing a from the children of b. |
|
379 |
*/ |
|
380 |
--_data[b].degree; |
|
381 |
|
|
382 |
if ( _data[b].degree !=0 ) { |
|
383 |
int child=_data[b].child; |
|
384 |
if ( child==a ) |
|
385 |
_data[b].child=_data[child].right_neighbor; |
|
386 |
unlace(a); |
|
387 |
} |
|
388 |
|
|
389 |
|
|
390 |
/*Lacing a to the roots.*/ |
|
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; |
|
396 |
|
|
397 |
_data[a].parent=-1; |
|
398 |
_data[a].marked=false; |
|
399 |
} |
|
400 |
|
|
401 |
void cascade(int a) { |
|
402 |
if ( _data[a].parent!=-1 ) { |
|
403 |
int p=_data[a].parent; |
|
404 |
|
|
405 |
if ( _data[a].marked==false ) _data[a].marked=true; |
|
406 |
else { |
|
407 |
cut(a,p); |
|
408 |
cascade(p); |
|
409 |
} |
|
410 |
} |
|
411 |
} |
|
412 |
|
|
413 |
void fuse(int a, int b) { |
|
414 |
unlace(b); |
|
415 |
|
|
416 |
/*Lacing b under a.*/ |
|
417 |
_data[b].parent=a; |
|
418 |
|
|
419 |
if (_data[a].degree==0) { |
|
420 |
_data[b].left_neighbor=b; |
|
421 |
_data[b].right_neighbor=b; |
|
422 |
_data[a].child=b; |
|
423 |
} else { |
|
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; |
|
430 |
} |
|
431 |
|
|
432 |
++_data[a].degree; |
|
433 |
|
|
434 |
_data[b].marked=false; |
|
435 |
} |
|
436 |
|
|
437 |
/* |
|
438 |
*It is invoked only if a has siblings. |
|
439 |
*/ |
|
440 |
void unlace(int a) { |
|
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; |
|
445 |
} |
|
446 |
|
|
447 |
|
|
448 |
class Store { |
|
449 |
friend class FibHeap; |
|
450 |
|
|
451 |
Item name; |
|
452 |
int parent; |
|
453 |
int left_neighbor; |
|
454 |
int right_neighbor; |
|
455 |
int child; |
|
456 |
int degree; |
|
457 |
bool marked; |
|
458 |
bool in; |
|
459 |
Prio prio; |
|
460 |
|
|
461 |
Store() : parent(-1), child(-1), degree(), marked(false), in(true) {} |
|
462 |
}; |
|
463 |
}; |
|
464 |
|
|
465 |
} //namespace lemon |
|
466 |
|
|
467 |
#endif //LEMON_FIB_HEAP_H |
|
468 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2009 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_RADIX_HEAP_H |
|
20 |
#define LEMON_RADIX_HEAP_H |
|
21 |
|
|
22 |
///\ingroup auxdat |
|
23 |
///\file |
|
24 |
///\brief Radix Heap implementation. |
|
25 |
|
|
26 |
#include <vector> |
|
27 |
#include <lemon/error.h> |
|
28 |
|
|
29 |
namespace lemon { |
|
30 |
|
|
31 |
|
|
32 |
/// \ingroup auxdata |
|
33 |
/// |
|
34 |
/// \brief A Radix Heap implementation. |
|
35 |
/// |
|
36 |
/// This class implements the \e radix \e heap data structure. A \e heap |
|
37 |
/// is a data structure for storing items with specified values called \e |
|
38 |
/// priorities in such a way that finding the item with minimum priority is |
|
39 |
/// efficient. This heap type can store only items with \e int priority. |
|
40 |
/// In a heap one can change the priority of an item, add or erase an |
|
41 |
/// item, but the priority cannot be decreased under the last removed |
|
42 |
/// item's priority. |
|
43 |
/// |
|
44 |
/// \param IM A read and writable Item int map, used internally |
|
45 |
/// to handle the cross references. |
|
46 |
/// |
|
47 |
/// \see BinHeap |
|
48 |
/// \see Dijkstra |
|
49 |
template <typename IM> |
|
50 |
class RadixHeap { |
|
51 |
|
|
52 |
public: |
|
53 |
typedef typename IM::Key Item; |
|
54 |
typedef int Prio; |
|
55 |
typedef IM ItemIntMap; |
|
56 |
|
|
57 |
/// \brief Exception thrown by RadixHeap. |
|
58 |
/// |
|
59 |
/// This Exception is thrown when a smaller priority |
|
60 |
/// is inserted into the \e RadixHeap then the last time erased. |
|
61 |
/// \see RadixHeap |
|
62 |
|
|
63 |
class UnderFlowPriorityError : public Exception { |
|
64 |
public: |
|
65 |
virtual const char* what() const throw() { |
|
66 |
return "lemon::RadixHeap::UnderFlowPriorityError"; |
|
67 |
} |
|
68 |
}; |
|
69 |
|
|
70 |
/// \brief Type to represent the items states. |
|
71 |
/// |
|
72 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
73 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
74 |
/// heap's point of view, but may be useful to the user. |
|
75 |
/// |
|
76 |
/// The ItemIntMap \e should be initialized in such way that it maps |
|
77 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
|
78 |
enum State { |
|
79 |
IN_HEAP = 0, |
|
80 |
PRE_HEAP = -1, |
|
81 |
POST_HEAP = -2 |
|
82 |
}; |
|
83 |
|
|
84 |
private: |
|
85 |
|
|
86 |
struct RadixItem { |
|
87 |
int prev, next, box; |
|
88 |
Item item; |
|
89 |
int prio; |
|
90 |
RadixItem(Item _item, int _prio) : item(_item), prio(_prio) {} |
|
91 |
}; |
|
92 |
|
|
93 |
struct RadixBox { |
|
94 |
int first; |
|
95 |
int min, size; |
|
96 |
RadixBox(int _min, int _size) : first(-1), min(_min), size(_size) {} |
|
97 |
}; |
|
98 |
|
|
99 |
std::vector<RadixItem> data; |
|
100 |
std::vector<RadixBox> boxes; |
|
101 |
|
|
102 |
ItemIntMap &_iim; |
|
103 |
|
|
104 |
|
|
105 |
public: |
|
106 |
/// \brief The constructor. |
|
107 |
/// |
|
108 |
/// The constructor. |
|
109 |
/// |
|
110 |
/// \param map It should be given to the constructor, since it is used |
|
111 |
/// internally to handle the cross references. The value of the map |
|
112 |
/// should be PRE_HEAP (-1) for each element. |
|
113 |
/// |
|
114 |
/// \param minimal The initial minimal value of the heap. |
|
115 |
/// \param capacity It determines the initial capacity of the heap. |
|
116 |
RadixHeap(ItemIntMap &map, int minimal = 0, int capacity = 0) |
|
117 |
: _iim(map) { |
|
118 |
boxes.push_back(RadixBox(minimal, 1)); |
|
119 |
boxes.push_back(RadixBox(minimal + 1, 1)); |
|
120 |
while (lower(boxes.size() - 1, capacity + minimal - 1)) { |
|
121 |
extend(); |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
/// The number of items stored in the heap. |
|
126 |
/// |
|
127 |
/// \brief Returns the number of items stored in the heap. |
|
128 |
int size() const { return data.size(); } |
|
129 |
/// \brief Checks if the heap stores no items. |
|
130 |
/// |
|
131 |
/// Returns \c true if and only if the heap stores no items. |
|
132 |
bool empty() const { return data.empty(); } |
|
133 |
|
|
134 |
/// \brief Make empty this heap. |
|
135 |
/// |
|
136 |
/// Make empty this heap. It does not change the cross reference |
|
137 |
/// map. If you want to reuse a heap what is not surely empty you |
|
138 |
/// should first clear the heap and after that you should set the |
|
139 |
/// cross reference map for each item to \c PRE_HEAP. |
|
140 |
void clear(int minimal = 0, int capacity = 0) { |
|
141 |
data.clear(); boxes.clear(); |
|
142 |
boxes.push_back(RadixBox(minimal, 1)); |
|
143 |
boxes.push_back(RadixBox(minimal + 1, 1)); |
|
144 |
while (lower(boxes.size() - 1, capacity + minimal - 1)) { |
|
145 |
extend(); |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
private: |
|
150 |
|
|
151 |
bool upper(int box, Prio pr) { |
|
152 |
return pr < boxes[box].min; |
|
153 |
} |
|
154 |
|
|
155 |
bool lower(int box, Prio pr) { |
|
156 |
return pr >= boxes[box].min + boxes[box].size; |
|
157 |
} |
|
158 |
|
|
159 |
/// \brief Remove item from the box list. |
|
160 |
void remove(int index) { |
|
161 |
if (data[index].prev >= 0) { |
|
162 |
data[data[index].prev].next = data[index].next; |
|
163 |
} else { |
|
164 |
boxes[data[index].box].first = data[index].next; |
|
165 |
} |
|
166 |
if (data[index].next >= 0) { |
|
167 |
data[data[index].next].prev = data[index].prev; |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
/// \brief Insert item into the box list. |
|
172 |
void insert(int box, int index) { |
|
173 |
if (boxes[box].first == -1) { |
|
174 |
boxes[box].first = index; |
|
175 |
data[index].next = data[index].prev = -1; |
|
176 |
} else { |
|
177 |
data[index].next = boxes[box].first; |
|
178 |
data[boxes[box].first].prev = index; |
|
179 |
data[index].prev = -1; |
|
180 |
boxes[box].first = index; |
|
181 |
} |
|
182 |
data[index].box = box; |
|
183 |
} |
|
184 |
|
|
185 |
/// \brief Add a new box to the box list. |
|
186 |
void extend() { |
|
187 |
int min = boxes.back().min + boxes.back().size; |
|
188 |
int bs = 2 * boxes.back().size; |
|
189 |
boxes.push_back(RadixBox(min, bs)); |
|
190 |
} |
|
191 |
|
|
192 |
/// \brief Move an item up into the proper box. |
|
193 |
void bubble_up(int index) { |
|
194 |
if (!lower(data[index].box, data[index].prio)) return; |
|
195 |
remove(index); |
|
196 |
int box = findUp(data[index].box, data[index].prio); |
|
197 |
insert(box, index); |
|
198 |
} |
|
199 |
|
|
200 |
/// \brief Find up the proper box for the item with the given prio. |
|
201 |
int findUp(int start, int pr) { |
|
202 |
while (lower(start, pr)) { |
|
203 |
if (++start == int(boxes.size())) { |
|
204 |
extend(); |
|
205 |
} |
|
206 |
} |
|
207 |
return start; |
|
208 |
} |
|
209 |
|
|
210 |
/// \brief Move an item down into the proper box. |
|
211 |
void bubble_down(int index) { |
|
212 |
if (!upper(data[index].box, data[index].prio)) return; |
|
213 |
remove(index); |
|
214 |
int box = findDown(data[index].box, data[index].prio); |
|
215 |
insert(box, index); |
|
216 |
} |
|
217 |
|
|
218 |
/// \brief Find up the proper box for the item with the given prio. |
|
219 |
int findDown(int start, int pr) { |
|
220 |
while (upper(start, pr)) { |
|
221 |
if (--start < 0) throw UnderFlowPriorityError(); |
|
222 |
} |
|
223 |
return start; |
|
224 |
} |
|
225 |
|
|
226 |
/// \brief Find the first not empty box. |
|
227 |
int findFirst() { |
|
228 |
int first = 0; |
|
229 |
while (boxes[first].first == -1) ++first; |
|
230 |
return first; |
|
231 |
} |
|
232 |
|
|
233 |
/// \brief Gives back the minimal prio of the box. |
|
234 |
int minValue(int box) { |
|
235 |
int min = data[boxes[box].first].prio; |
|
236 |
for (int k = boxes[box].first; k != -1; k = data[k].next) { |
|
237 |
if (data[k].prio < min) min = data[k].prio; |
|
238 |
} |
|
239 |
return min; |
|
240 |
} |
|
241 |
|
|
242 |
/// \brief Rearrange the items of the heap and makes the |
|
243 |
/// first box not empty. |
|
244 |
void moveDown() { |
|
245 |
int box = findFirst(); |
|
246 |
if (box == 0) return; |
|
247 |
int min = minValue(box); |
|
248 |
for (int i = 0; i <= box; ++i) { |
|
249 |
boxes[i].min = min; |
|
250 |
min += boxes[i].size; |
|
251 |
} |
|
252 |
int curr = boxes[box].first, next; |
|
253 |
while (curr != -1) { |
|
254 |
next = data[curr].next; |
|
255 |
bubble_down(curr); |
|
256 |
curr = next; |
|
257 |
} |
|
258 |
} |
|
259 |
|
|
260 |
void relocate_last(int index) { |
|
261 |
if (index != int(data.size()) - 1) { |
|
262 |
data[index] = data.back(); |
|
263 |
if (data[index].prev != -1) { |
|
264 |
data[data[index].prev].next = index; |
|
265 |
} else { |
|
266 |
boxes[data[index].box].first = index; |
|
267 |
} |
|
268 |
if (data[index].next != -1) { |
|
269 |
data[data[index].next].prev = index; |
|
270 |
} |
|
271 |
_iim[data[index].item] = index; |
|
272 |
} |
|
273 |
data.pop_back(); |
|
274 |
} |
|
275 |
|
|
276 |
public: |
|
277 |
|
|
278 |
/// \brief Insert an item into the heap with the given priority. |
|
279 |
/// |
|
280 |
/// Adds \c i to the heap with priority \c p. |
|
281 |
/// \param i The item to insert. |
|
282 |
/// \param p The priority of the item. |
|
283 |
void push(const Item &i, const Prio &p) { |
|
284 |
int n = data.size(); |
|
285 |
_iim.set(i, n); |
|
286 |
data.push_back(RadixItem(i, p)); |
|
287 |
while (lower(boxes.size() - 1, p)) { |
|
288 |
extend(); |
|
289 |
} |
|
290 |
int box = findDown(boxes.size() - 1, p); |
|
291 |
insert(box, n); |
|
292 |
} |
|
293 |
|
|
294 |
/// \brief Returns the item with minimum priority. |
|
295 |
/// |
|
296 |
/// This method returns the item with minimum priority. |
|
297 |
/// \pre The heap must be nonempty. |
|
298 |
Item top() const { |
|
299 |
const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown(); |
|
300 |
return data[boxes[0].first].item; |
|
301 |
} |
|
302 |
|
|
303 |
/// \brief Returns the minimum priority. |
|
304 |
/// |
|
305 |
/// It returns the minimum priority. |
|
306 |
/// \pre The heap must be nonempty. |
|
307 |
Prio prio() const { |
|
308 |
const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown(); |
|
309 |
return data[boxes[0].first].prio; |
|
310 |
} |
|
311 |
|
|
312 |
/// \brief Deletes the item with minimum priority. |
|
313 |
/// |
|
314 |
/// This method deletes the item with minimum priority. |
|
315 |
/// \pre The heap must be non-empty. |
|
316 |
void pop() { |
|
317 |
moveDown(); |
|
318 |
int index = boxes[0].first; |
|
319 |
_iim[data[index].item] = POST_HEAP; |
|
320 |
remove(index); |
|
321 |
relocate_last(index); |
|
322 |
} |
|
323 |
|
|
324 |
/// \brief Deletes \c i from the heap. |
|
325 |
/// |
|
326 |
/// This method deletes item \c i from the heap, if \c i was |
|
327 |
/// already stored in the heap. |
|
328 |
/// \param i The item to erase. |
|
329 |
void erase(const Item &i) { |
|
330 |
int index = _iim[i]; |
|
331 |
_iim[i] = POST_HEAP; |
|
332 |
remove(index); |
|
333 |
relocate_last(index); |
|
334 |
} |
|
335 |
|
|
336 |
/// \brief Returns the priority of \c i. |
|
337 |
/// |
|
338 |
/// This function returns the priority of item \c i. |
|
339 |
/// \pre \c i must be in the heap. |
|
340 |
/// \param i The item. |
|
341 |
Prio operator[](const Item &i) const { |
|
342 |
int idx = _iim[i]; |
|
343 |
return data[idx].prio; |
|
344 |
} |
|
345 |
|
|
346 |
/// \brief \c i gets to the heap with priority \c p independently |
|
347 |
/// if \c i was already there. |
|
348 |
/// |
|
349 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
|
350 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
|
351 |
/// It may throw an \e UnderFlowPriorityException. |
|
352 |
/// \param i The item. |
|
353 |
/// \param p The priority. |
|
354 |
void set(const Item &i, const Prio &p) { |
|
355 |
int idx = _iim[i]; |
|
356 |
if( idx < 0 ) { |
|
357 |
push(i, p); |
|
358 |
} |
|
359 |
else if( p >= data[idx].prio ) { |
|
360 |
data[idx].prio = p; |
|
361 |
bubble_up(idx); |
|
362 |
} else { |
|
363 |
data[idx].prio = p; |
|
364 |
bubble_down(idx); |
|
365 |
} |
|
366 |
} |
|
367 |
|
|
368 |
|
|
369 |
/// \brief Decreases the priority of \c i to \c p. |
|
370 |
/// |
|
371 |
/// This method decreases the priority of item \c i to \c p. |
|
372 |
/// \pre \c i must be stored in the heap with priority at least \c p, and |
|
373 |
/// \c should be greater or equal to the last removed item's priority. |
|
374 |
/// \param i The item. |
|
375 |
/// \param p The priority. |
|
376 |
void decrease(const Item &i, const Prio &p) { |
|
377 |
int idx = _iim[i]; |
|
378 |
data[idx].prio = p; |
|
379 |
bubble_down(idx); |
|
380 |
} |
|
381 |
|
|
382 |
/// \brief Increases the priority of \c i to \c p. |
|
383 |
/// |
|
384 |
/// This method sets the priority of item \c i to \c p. |
|
385 |
/// \pre \c i must be stored in the heap with priority at most \c p |
|
386 |
/// \param i The item. |
|
387 |
/// \param p The priority. |
|
388 |
void increase(const Item &i, const Prio &p) { |
|
389 |
int idx = _iim[i]; |
|
390 |
data[idx].prio = p; |
|
391 |
bubble_up(idx); |
|
392 |
} |
|
393 |
|
|
394 |
/// \brief Returns if \c item is in, has already been in, or has |
|
395 |
/// never been in the heap. |
|
396 |
/// |
|
397 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
398 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
399 |
/// otherwise. In the latter case it is possible that \c item will |
|
400 |
/// get back to the heap again. |
|
401 |
/// \param i The item. |
|
402 |
State state(const Item &i) const { |
|
403 |
int s = _iim[i]; |
|
404 |
if( s >= 0 ) s = 0; |
|
405 |
return State(s); |
|
406 |
} |
|
407 |
|
|
408 |
/// \brief Sets the state of the \c item in the heap. |
|
409 |
/// |
|
410 |
/// Sets the state of the \c item in the heap. It can be used to |
|
411 |
/// manually clear the heap when it is important to achive the |
|
412 |
/// better time complexity. |
|
413 |
/// \param i The item. |
|
414 |
/// \param st The state. It should not be \c IN_HEAP. |
|
415 |
void state(const Item& i, State st) { |
|
416 |
switch (st) { |
|
417 |
case POST_HEAP: |
|
418 |
case PRE_HEAP: |
|
419 |
if (state(i) == IN_HEAP) { |
|
420 |
erase(i); |
|
421 |
} |
|
422 |
_iim[i] = st; |
|
423 |
break; |
|
424 |
case IN_HEAP: |
|
425 |
break; |
|
426 |
} |
|
427 |
} |
|
428 |
|
|
429 |
}; // class RadixHeap |
|
430 |
|
|
431 |
} // namespace lemon |
|
432 |
|
|
433 |
#endif // LEMON_RADIX_HEAP_H |
1 | 1 |
SET(COIN_ROOT_DIR "" CACHE PATH "COIN root directory") |
2 | 2 |
|
3 | 3 |
FIND_PATH(COIN_INCLUDE_DIR coin/CoinUtilsConfig.h |
4 | 4 |
HINTS ${COIN_ROOT_DIR}/include |
5 | 5 |
) |
6 | 6 |
FIND_LIBRARY(COIN_CBC_LIBRARY |
7 | 7 |
NAMES Cbc libCbc |
8 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
8 | 9 |
HINTS ${COIN_ROOT_DIR}/lib |
9 | 10 |
) |
10 | 11 |
FIND_LIBRARY(COIN_CBC_SOLVER_LIBRARY |
11 | 12 |
NAMES CbcSolver libCbcSolver |
13 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
12 | 14 |
HINTS ${COIN_ROOT_DIR}/lib |
13 | 15 |
) |
14 | 16 |
FIND_LIBRARY(COIN_CGL_LIBRARY |
15 | 17 |
NAMES Cgl libCgl |
18 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
16 | 19 |
HINTS ${COIN_ROOT_DIR}/lib |
17 | 20 |
) |
18 | 21 |
FIND_LIBRARY(COIN_CLP_LIBRARY |
19 | 22 |
NAMES Clp libClp |
23 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
20 | 24 |
HINTS ${COIN_ROOT_DIR}/lib |
21 | 25 |
) |
22 | 26 |
FIND_LIBRARY(COIN_COIN_UTILS_LIBRARY |
23 | 27 |
NAMES CoinUtils libCoinUtils |
28 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
24 | 29 |
HINTS ${COIN_ROOT_DIR}/lib |
25 | 30 |
) |
26 | 31 |
FIND_LIBRARY(COIN_OSI_LIBRARY |
27 | 32 |
NAMES Osi libOsi |
33 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
28 | 34 |
HINTS ${COIN_ROOT_DIR}/lib |
29 | 35 |
) |
30 | 36 |
FIND_LIBRARY(COIN_OSI_CBC_LIBRARY |
31 | 37 |
NAMES OsiCbc libOsiCbc |
38 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
32 | 39 |
HINTS ${COIN_ROOT_DIR}/lib |
33 | 40 |
) |
34 | 41 |
FIND_LIBRARY(COIN_OSI_CLP_LIBRARY |
35 | 42 |
NAMES OsiClp libOsiClp |
43 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
36 | 44 |
HINTS ${COIN_ROOT_DIR}/lib |
37 | 45 |
) |
38 | 46 |
FIND_LIBRARY(COIN_OSI_VOL_LIBRARY |
39 | 47 |
NAMES OsiVol libOsiVol |
48 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
40 | 49 |
HINTS ${COIN_ROOT_DIR}/lib |
41 | 50 |
) |
42 | 51 |
FIND_LIBRARY(COIN_VOL_LIBRARY |
43 | 52 |
NAMES Vol libVol |
53 |
HINTS ${COIN_ROOT_DIR}/lib/coin |
|
44 | 54 |
HINTS ${COIN_ROOT_DIR}/lib |
45 | 55 |
) |
46 | 56 |
|
47 | 57 |
INCLUDE(FindPackageHandleStandardArgs) |
48 | 58 |
FIND_PACKAGE_HANDLE_STANDARD_ARGS(COIN DEFAULT_MSG |
49 | 59 |
COIN_INCLUDE_DIR |
50 | 60 |
COIN_CBC_LIBRARY |
51 | 61 |
COIN_CBC_SOLVER_LIBRARY |
52 | 62 |
COIN_CGL_LIBRARY |
53 | 63 |
COIN_CLP_LIBRARY |
54 | 64 |
COIN_COIN_UTILS_LIBRARY |
55 | 65 |
COIN_OSI_LIBRARY |
56 | 66 |
COIN_OSI_CBC_LIBRARY |
57 | 67 |
COIN_OSI_CLP_LIBRARY |
58 |
COIN_OSI_VOL_LIBRARY |
|
59 |
COIN_VOL_LIBRARY |
|
68 |
# COIN_OSI_VOL_LIBRARY |
|
69 |
# COIN_VOL_LIBRARY |
|
60 | 70 |
) |
61 | 71 |
|
62 | 72 |
IF(COIN_FOUND) |
63 | 73 |
SET(COIN_INCLUDE_DIRS ${COIN_INCLUDE_DIR}) |
64 |
SET(COIN_LIBRARIES "${COIN_CBC_LIBRARY};${COIN_CBC_SOLVER_LIBRARY};${COIN_CGL_LIBRARY};${COIN_CLP_LIBRARY};${COIN_COIN_UTILS_LIBRARY};${COIN_OSI_LIBRARY};${COIN_OSI_CBC_LIBRARY};${COIN_OSI_CLP_LIBRARY} |
|
74 |
SET(COIN_LIBRARIES "${COIN_CBC_LIBRARY};${COIN_CBC_SOLVER_LIBRARY};${COIN_CGL_LIBRARY};${COIN_CLP_LIBRARY};${COIN_COIN_UTILS_LIBRARY};${COIN_OSI_LIBRARY};${COIN_OSI_CBC_LIBRARY};${COIN_OSI_CLP_LIBRARY}") |
|
65 | 75 |
SET(COIN_CLP_LIBRARIES "${COIN_CLP_LIBRARY};${COIN_COIN_UTILS_LIBRARY}") |
66 | 76 |
SET(COIN_CBC_LIBRARIES ${COIN_LIBRARIES}) |
67 | 77 |
ENDIF(COIN_FOUND) |
68 | 78 |
|
69 | 79 |
MARK_AS_ADVANCED( |
70 | 80 |
COIN_INCLUDE_DIR |
71 | 81 |
COIN_CBC_LIBRARY |
72 | 82 |
COIN_CBC_SOLVER_LIBRARY |
73 | 83 |
COIN_CGL_LIBRARY |
74 | 84 |
COIN_CLP_LIBRARY |
75 | 85 |
COIN_COIN_UTILS_LIBRARY |
76 | 86 |
COIN_OSI_LIBRARY |
77 | 87 |
COIN_OSI_CBC_LIBRARY |
78 | 88 |
COIN_OSI_CLP_LIBRARY |
79 | 89 |
COIN_OSI_VOL_LIBRARY |
80 | 90 |
COIN_VOL_LIBRARY |
81 | 91 |
) |
82 | 92 |
|
83 | 93 |
IF(COIN_FOUND) |
84 | 94 |
SET(LEMON_HAVE_LP TRUE) |
85 | 95 |
SET(LEMON_HAVE_MIP TRUE) |
86 | 96 |
SET(LEMON_HAVE_CLP TRUE) |
87 | 97 |
SET(LEMON_HAVE_CBC TRUE) |
88 | 98 |
ENDIF(COIN_FOUND) |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
lemon/lemon.pc.in \ |
3 | 3 |
lemon/CMakeLists.txt \ |
4 | 4 |
lemon/config.h.cmake |
5 | 5 |
|
6 | 6 |
pkgconfig_DATA += lemon/lemon.pc |
7 | 7 |
|
8 | 8 |
lib_LTLIBRARIES += lemon/libemon.la |
9 | 9 |
|
10 | 10 |
lemon_libemon_la_SOURCES = \ |
11 | 11 |
lemon/arg_parser.cc \ |
12 | 12 |
lemon/base.cc \ |
13 | 13 |
lemon/color.cc \ |
14 | 14 |
lemon/lp_base.cc \ |
15 | 15 |
lemon/lp_skeleton.cc \ |
16 | 16 |
lemon/random.cc \ |
17 | 17 |
lemon/bits/windows.cc |
18 | 18 |
|
19 | 19 |
nodist_lemon_HEADERS = lemon/config.h |
20 | 20 |
|
21 | 21 |
lemon_libemon_la_CXXFLAGS = \ |
22 | 22 |
$(AM_CXXFLAGS) \ |
23 | 23 |
$(GLPK_CFLAGS) \ |
24 | 24 |
$(CPLEX_CFLAGS) \ |
25 | 25 |
$(SOPLEX_CXXFLAGS) \ |
26 | 26 |
$(CLP_CXXFLAGS) \ |
27 | 27 |
$(CBC_CXXFLAGS) |
28 | 28 |
|
29 | 29 |
lemon_libemon_la_LDFLAGS = \ |
30 | 30 |
$(GLPK_LIBS) \ |
31 | 31 |
$(CPLEX_LIBS) \ |
32 | 32 |
$(SOPLEX_LIBS) \ |
33 | 33 |
$(CLP_LIBS) \ |
34 | 34 |
$(CBC_LIBS) |
35 | 35 |
|
36 | 36 |
if HAVE_GLPK |
37 | 37 |
lemon_libemon_la_SOURCES += lemon/glpk.cc |
38 | 38 |
endif |
39 | 39 |
|
40 | 40 |
if HAVE_CPLEX |
41 | 41 |
lemon_libemon_la_SOURCES += lemon/cplex.cc |
42 | 42 |
endif |
43 | 43 |
|
44 | 44 |
if HAVE_SOPLEX |
45 | 45 |
lemon_libemon_la_SOURCES += lemon/soplex.cc |
46 | 46 |
endif |
47 | 47 |
|
48 | 48 |
if HAVE_CLP |
49 | 49 |
lemon_libemon_la_SOURCES += lemon/clp.cc |
50 | 50 |
endif |
51 | 51 |
|
52 | 52 |
if HAVE_CBC |
53 | 53 |
lemon_libemon_la_SOURCES += lemon/cbc.cc |
54 | 54 |
endif |
55 | 55 |
|
56 | 56 |
lemon_HEADERS += \ |
57 | 57 |
lemon/adaptors.h \ |
58 | 58 |
lemon/arg_parser.h \ |
59 | 59 |
lemon/assert.h \ |
60 | 60 |
lemon/bfs.h \ |
61 | 61 |
lemon/bin_heap.h \ |
62 |
lemon/bucket_heap.h \ |
|
62 | 63 |
lemon/cbc.h \ |
63 | 64 |
lemon/circulation.h \ |
64 | 65 |
lemon/clp.h \ |
65 | 66 |
lemon/color.h \ |
66 | 67 |
lemon/concept_check.h \ |
67 | 68 |
lemon/connectivity.h \ |
68 | 69 |
lemon/counter.h \ |
69 | 70 |
lemon/core.h \ |
70 | 71 |
lemon/cplex.h \ |
71 | 72 |
lemon/dfs.h \ |
72 | 73 |
lemon/dijkstra.h \ |
73 | 74 |
lemon/dim2.h \ |
74 | 75 |
lemon/dimacs.h \ |
75 | 76 |
lemon/edge_set.h \ |
76 | 77 |
lemon/elevator.h \ |
77 | 78 |
lemon/error.h \ |
78 | 79 |
lemon/euler.h \ |
80 |
lemon/fib_heap.h \ |
|
79 | 81 |
lemon/full_graph.h \ |
80 | 82 |
lemon/glpk.h \ |
81 | 83 |
lemon/gomory_hu.h \ |
82 | 84 |
lemon/graph_to_eps.h \ |
83 | 85 |
lemon/grid_graph.h \ |
84 | 86 |
lemon/hypercube_graph.h \ |
85 | 87 |
lemon/kruskal.h \ |
86 | 88 |
lemon/hao_orlin.h \ |
87 | 89 |
lemon/lgf_reader.h \ |
88 | 90 |
lemon/lgf_writer.h \ |
89 | 91 |
lemon/list_graph.h \ |
90 | 92 |
lemon/lp.h \ |
91 | 93 |
lemon/lp_base.h \ |
92 | 94 |
lemon/lp_skeleton.h \ |
93 | 95 |
lemon/maps.h \ |
94 | 96 |
lemon/matching.h \ |
95 | 97 |
lemon/math.h \ |
96 | 98 |
lemon/min_cost_arborescence.h \ |
97 | 99 |
lemon/nauty_reader.h \ |
98 | 100 |
lemon/network_simplex.h \ |
99 | 101 |
lemon/path.h \ |
100 | 102 |
lemon/preflow.h \ |
103 |
lemon/radix_heap.h \ |
|
101 | 104 |
lemon/radix_sort.h \ |
102 | 105 |
lemon/random.h \ |
103 | 106 |
lemon/smart_graph.h \ |
104 | 107 |
lemon/soplex.h \ |
105 | 108 |
lemon/suurballe.h \ |
106 | 109 |
lemon/time_measure.h \ |
107 | 110 |
lemon/tolerance.h \ |
108 | 111 |
lemon/unionfind.h \ |
109 | 112 |
lemon/bits/windows.h |
110 | 113 |
|
111 | 114 |
bits_HEADERS += \ |
112 | 115 |
lemon/bits/alteration_notifier.h \ |
113 | 116 |
lemon/bits/array_map.h \ |
114 | 117 |
lemon/bits/bezier.h \ |
115 | 118 |
lemon/bits/default_map.h \ |
116 | 119 |
lemon/bits/edge_set_extender.h \ |
117 | 120 |
lemon/bits/enable_if.h \ |
118 | 121 |
lemon/bits/graph_adaptor_extender.h \ |
119 | 122 |
lemon/bits/graph_extender.h \ |
120 | 123 |
lemon/bits/map_extender.h \ |
121 | 124 |
lemon/bits/path_dump.h \ |
122 | 125 |
lemon/bits/solver_bits.h \ |
123 | 126 |
lemon/bits/traits.h \ |
124 | 127 |
lemon/bits/variant.h \ |
125 | 128 |
lemon/bits/vector_map.h |
126 | 129 |
|
127 | 130 |
concept_HEADERS += \ |
128 | 131 |
lemon/concepts/digraph.h \ |
129 | 132 |
lemon/concepts/graph.h \ |
130 | 133 |
lemon/concepts/graph_components.h \ |
131 | 134 |
lemon/concepts/heap.h \ |
132 | 135 |
lemon/concepts/maps.h \ |
133 | 136 |
lemon/concepts/path.h |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Binary Heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
///\ingroup auxdat |
33 | 33 |
/// |
34 | 34 |
///\brief A Binary Heap implementation. |
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 |
42 | 42 |
///item, etc. |
43 | 43 |
/// |
44 | 44 |
///\tparam PR Type of the priority of the items. |
45 | 45 |
///\tparam IM A read and writable item map with int values, used internally |
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>. |
49 | 49 |
/// |
50 | 50 |
///\sa FibHeap |
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 { |
54 | 54 |
|
55 | 55 |
public: |
56 | 56 |
///\e |
57 | 57 |
typedef IM ItemIntMap; |
58 | 58 |
///\e |
59 | 59 |
typedef PR Prio; |
60 | 60 |
///\e |
61 | 61 |
typedef typename ItemIntMap::Key Item; |
62 | 62 |
///\e |
63 | 63 |
typedef std::pair<Item,Prio> Pair; |
64 | 64 |
///\e |
65 |
typedef |
|
65 |
typedef CMP Compare; |
|
66 | 66 |
|
67 | 67 |
/// \brief Type to represent the items states. |
68 | 68 |
/// |
69 | 69 |
/// Each Item element have a state associated to it. It may be "in heap", |
70 | 70 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
71 | 71 |
/// heap's point of view, but may be useful to the user. |
72 | 72 |
/// |
73 | 73 |
/// The item-int map must be initialized in such way that it assigns |
74 | 74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
75 | 75 |
enum State { |
76 | 76 |
IN_HEAP = 0, ///< = 0. |
77 | 77 |
PRE_HEAP = -1, ///< = -1. |
78 | 78 |
POST_HEAP = -2 ///< = -2. |
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
private: |
82 | 82 |
std::vector<Pair> _data; |
83 | 83 |
Compare _comp; |
84 | 84 |
ItemIntMap &_iim; |
85 | 85 |
|
86 | 86 |
public: |
87 | 87 |
/// \brief The constructor. |
88 | 88 |
/// |
89 | 89 |
/// The constructor. |
90 | 90 |
/// \param map should be given to the constructor, since it is used |
91 | 91 |
/// internally to handle the cross references. The value of the map |
92 | 92 |
/// must be \c PRE_HEAP (<tt>-1</tt>) for every item. |
93 | 93 |
explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
94 | 94 |
|
95 | 95 |
/// \brief The constructor. |
96 | 96 |
/// |
97 | 97 |
/// The constructor. |
98 | 98 |
/// \param map should be given to the constructor, since it is used |
99 | 99 |
/// internally to handle the cross references. The value of the map |
100 | 100 |
/// should be PRE_HEAP (-1) for each element. |
101 | 101 |
/// |
102 | 102 |
/// \param comp The comparator function object. |
103 | 103 |
BinHeap(ItemIntMap &map, const Compare &comp) |
104 | 104 |
: _iim(map), _comp(comp) {} |
105 | 105 |
|
106 | 106 |
|
107 | 107 |
/// The number of items stored in the heap. |
108 | 108 |
/// |
109 | 109 |
/// \brief Returns the number of items stored in the heap. |
110 | 110 |
int size() const { return _data.size(); } |
111 | 111 |
|
112 | 112 |
/// \brief Checks if the heap stores no items. |
113 | 113 |
/// |
114 | 114 |
/// Returns \c true if and only if the heap stores no items. |
115 | 115 |
bool empty() const { return _data.empty(); } |
116 | 116 |
|
117 | 117 |
/// \brief Make empty this heap. |
118 | 118 |
/// |
119 | 119 |
/// Make empty this heap. It does not change the cross reference map. |
120 | 120 |
/// If you want to reuse what is not surely empty you should first clear |
121 | 121 |
/// the heap and after that you should set the cross reference map for |
122 | 122 |
/// each item to \c PRE_HEAP. |
123 | 123 |
void clear() { |
124 | 124 |
_data.clear(); |
125 | 125 |
} |
126 | 126 |
|
127 | 127 |
private: |
128 | 128 |
static int parent(int i) { return (i-1)/2; } |
129 | 129 |
|
130 | 130 |
static int second_child(int i) { return 2*i+2; } |
131 | 131 |
bool less(const Pair &p1, const Pair &p2) const { |
132 | 132 |
return _comp(p1.second, p2.second); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
int bubble_up(int hole, Pair p) { |
136 | 136 |
int par = parent(hole); |
137 | 137 |
while( hole>0 && less(p,_data[par]) ) { |
138 | 138 |
move(_data[par],hole); |
139 | 139 |
hole = par; |
140 | 140 |
par = parent(hole); |
141 | 141 |
} |
142 | 142 |
move(p, hole); |
143 | 143 |
return hole; |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
int bubble_down(int hole, Pair p, int length) { |
147 | 147 |
int child = second_child(hole); |
148 | 148 |
while(child < length) { |
149 | 149 |
if( less(_data[child-1], _data[child]) ) { |
150 | 150 |
--child; |
151 | 151 |
} |
152 | 152 |
if( !less(_data[child], p) ) |
153 | 153 |
goto ok; |
154 | 154 |
move(_data[child], hole); |
155 | 155 |
hole = child; |
156 | 156 |
child = second_child(hole); |
157 | 157 |
} |
158 | 158 |
child--; |
159 | 159 |
if( child<length && less(_data[child], p) ) { |
160 | 160 |
move(_data[child], hole); |
161 | 161 |
hole=child; |
162 | 162 |
} |
163 | 163 |
ok: |
164 | 164 |
move(p, hole); |
165 | 165 |
return hole; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
void move(const Pair &p, int i) { |
169 | 169 |
_data[i] = p; |
170 | 170 |
_iim.set(p.first, i); |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
public: |
174 | 174 |
/// \brief Insert a pair of item and priority into the heap. |
175 | 175 |
/// |
176 | 176 |
/// Adds \c p.first to the heap with priority \c p.second. |
177 | 177 |
/// \param p The pair to insert. |
178 | 178 |
void push(const Pair &p) { |
179 | 179 |
int n = _data.size(); |
180 | 180 |
_data.resize(n+1); |
181 | 181 |
bubble_up(n, p); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
/// \brief Insert an item into the heap with the given heap. |
185 | 185 |
/// |
186 | 186 |
/// Adds \c i to the heap with priority \c p. |
187 | 187 |
/// \param i The item to insert. |
188 | 188 |
/// \param p The priority of the item. |
189 | 189 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
190 | 190 |
|
191 | 191 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
192 | 192 |
/// |
193 | 193 |
/// This method returns the item with minimum priority relative to \c |
194 | 194 |
/// Compare. |
195 | 195 |
/// \pre The heap must be nonempty. |
196 | 196 |
Item top() const { |
197 | 197 |
return _data[0].first; |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
/// \brief Returns the minimum priority relative to \c Compare. |
201 | 201 |
/// |
202 | 202 |
/// It returns the minimum priority relative to \c Compare. |
203 | 203 |
/// \pre The heap must be nonempty. |
204 | 204 |
Prio prio() const { |
205 | 205 |
return _data[0].second; |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
209 | 209 |
/// |
210 | 210 |
/// This method deletes the item with minimum priority relative to \c |
211 | 211 |
/// Compare from the heap. |
212 | 212 |
/// \pre The heap must be non-empty. |
213 | 213 |
void pop() { |
214 | 214 |
int n = _data.size()-1; |
215 | 215 |
_iim.set(_data[0].first, POST_HEAP); |
216 | 216 |
if (n > 0) { |
217 | 217 |
bubble_down(0, _data[n], n); |
218 | 218 |
} |
219 | 219 |
_data.pop_back(); |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
/// \brief Deletes \c i from the heap. |
223 | 223 |
/// |
224 | 224 |
/// This method deletes item \c i from the heap. |
225 | 225 |
/// \param i The item to erase. |
226 | 226 |
/// \pre The item should be in the heap. |
227 | 227 |
void erase(const Item &i) { |
228 | 228 |
int h = _iim[i]; |
229 | 229 |
int n = _data.size()-1; |
230 | 230 |
_iim.set(_data[h].first, POST_HEAP); |
231 | 231 |
if( h < n ) { |
232 | 232 |
if ( bubble_up(h, _data[n]) == h) { |
233 | 233 |
bubble_down(h, _data[n], n); |
234 | 234 |
} |
235 | 235 |
} |
236 | 236 |
_data.pop_back(); |
237 | 237 |
} |
238 | 238 |
|
239 | 239 |
|
240 | 240 |
/// \brief Returns the priority of \c i. |
241 | 241 |
/// |
242 | 242 |
/// This function returns the priority of item \c i. |
243 | 243 |
/// \param i The item. |
244 | 244 |
/// \pre \c i must be in the heap. |
245 | 245 |
Prio operator[](const Item &i) const { |
246 | 246 |
int idx = _iim[i]; |
247 | 247 |
return _data[idx].second; |
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
/// \brief \c i gets to the heap with priority \c p independently |
251 | 251 |
/// if \c i was already there. |
252 | 252 |
/// |
253 | 253 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
254 | 254 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
255 | 255 |
/// \param i The item. |
256 | 256 |
/// \param p The priority. |
257 | 257 |
void set(const Item &i, const Prio &p) { |
258 | 258 |
int idx = _iim[i]; |
259 | 259 |
if( idx < 0 ) { |
260 | 260 |
push(i,p); |
261 | 261 |
} |
262 | 262 |
else if( _comp(p, _data[idx].second) ) { |
263 | 263 |
bubble_up(idx, Pair(i,p)); |
264 | 264 |
} |
265 | 265 |
else { |
266 | 266 |
bubble_down(idx, Pair(i,p), _data.size()); |
267 | 267 |
} |
268 | 268 |
} |
269 | 269 |
|
270 | 270 |
/// \brief Decreases the priority of \c i to \c p. |
271 | 271 |
/// |
272 | 272 |
/// This method decreases the priority of item \c i to \c p. |
273 | 273 |
/// \param i The item. |
274 | 274 |
/// \param p The priority. |
275 | 275 |
/// \pre \c i must be stored in the heap with priority at least \c |
276 | 276 |
/// p relative to \c Compare. |
277 | 277 |
void decrease(const Item &i, const Prio &p) { |
278 | 278 |
int idx = _iim[i]; |
279 | 279 |
bubble_up(idx, Pair(i,p)); |
280 | 280 |
} |
281 | 281 |
|
282 | 282 |
/// \brief Increases the priority of \c i to \c p. |
283 | 283 |
/// |
284 | 284 |
/// This method sets the priority of item \c i to \c p. |
285 | 285 |
/// \param i The item. |
286 | 286 |
/// \param p The priority. |
287 | 287 |
/// \pre \c i must be stored in the heap with priority at most \c |
288 | 288 |
/// p relative to \c Compare. |
289 | 289 |
void increase(const Item &i, const Prio &p) { |
290 | 290 |
int idx = _iim[i]; |
291 | 291 |
bubble_down(idx, Pair(i,p), _data.size()); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
/// \brief Returns if \c item is in, has already been in, or has |
295 | 295 |
/// never been in the heap. |
296 | 296 |
/// |
297 | 297 |
/// This method returns PRE_HEAP if \c item has never been in the |
298 | 298 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
299 | 299 |
/// otherwise. In the latter case it is possible that \c item will |
300 | 300 |
/// get back to the heap again. |
301 | 301 |
/// \param i The item. |
302 | 302 |
State state(const Item &i) const { |
303 | 303 |
int s = _iim[i]; |
304 | 304 |
if( s>=0 ) |
305 | 305 |
s=0; |
306 | 306 |
return State(s); |
307 | 307 |
} |
308 | 308 |
|
309 | 309 |
/// \brief Sets the state of the \c item in the heap. |
310 | 310 |
/// |
311 | 311 |
/// Sets the state of the \c item in the heap. It can be used to |
312 | 312 |
/// manually clear the heap when it is important to achive the |
313 | 313 |
/// better time complexity. |
314 | 314 |
/// \param i The item. |
315 | 315 |
/// \param st The state. It should not be \c IN_HEAP. |
316 | 316 |
void state(const Item& i, State st) { |
317 | 317 |
switch (st) { |
318 | 318 |
case POST_HEAP: |
319 | 319 |
case PRE_HEAP: |
320 | 320 |
if (state(i) == IN_HEAP) { |
321 | 321 |
erase(i); |
322 | 322 |
} |
323 | 323 |
_iim[i] = st; |
324 | 324 |
break; |
325 | 325 |
case IN_HEAP: |
326 | 326 |
break; |
327 | 327 |
} |
328 | 328 |
} |
329 | 329 |
|
330 | 330 |
/// \brief Replaces an item in the heap. |
331 | 331 |
/// |
332 | 332 |
/// The \c i item is replaced with \c j item. The \c i item should |
333 | 333 |
/// be in the heap, while the \c j should be out of the heap. The |
334 | 334 |
/// \c i item will out of the heap and \c j will be in the heap |
335 | 335 |
/// with the same prioriority as prevoiusly the \c i item. |
336 | 336 |
void replace(const Item& i, const Item& j) { |
337 | 337 |
int idx = _iim[i]; |
338 | 338 |
_iim.set(i, _iim[j]); |
339 | 339 |
_iim.set(j, idx); |
340 | 340 |
_data[idx].first = j; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
}; // class BinHeap |
344 | 344 |
|
345 | 345 |
} // namespace lemon |
346 | 346 |
|
347 | 347 |
#endif // LEMON_BIN_HEAP_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_MAP_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_MAP_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
|
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
//\file |
30 | 30 |
//\brief Extenders for iterable maps. |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
// \ingroup graphbits |
35 | 35 |
// |
36 | 36 |
// \brief Extender for maps |
37 | 37 |
template <typename _Map> |
38 | 38 |
class MapExtender : public _Map { |
39 | 39 |
typedef _Map Parent; |
40 | 40 |
typedef typename Parent::GraphType GraphType; |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef MapExtender Map; |
45 | 45 |
typedef typename Parent::Key Item; |
46 | 46 |
|
47 | 47 |
typedef typename Parent::Key Key; |
48 | 48 |
typedef typename Parent::Value Value; |
49 | 49 |
typedef typename Parent::Reference Reference; |
50 | 50 |
typedef typename Parent::ConstReference ConstReference; |
51 | 51 |
|
52 |
typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
|
53 |
|
|
52 | 54 |
class MapIt; |
53 | 55 |
class ConstMapIt; |
54 | 56 |
|
55 | 57 |
friend class MapIt; |
56 | 58 |
friend class ConstMapIt; |
57 | 59 |
|
58 | 60 |
public: |
59 | 61 |
|
60 | 62 |
MapExtender(const GraphType& graph) |
61 | 63 |
: Parent(graph) {} |
62 | 64 |
|
63 | 65 |
MapExtender(const GraphType& graph, const Value& value) |
64 | 66 |
: Parent(graph, value) {} |
65 | 67 |
|
66 | 68 |
private: |
67 | 69 |
MapExtender& operator=(const MapExtender& cmap) { |
68 | 70 |
return operator=<MapExtender>(cmap); |
69 | 71 |
} |
70 | 72 |
|
71 | 73 |
template <typename CMap> |
72 | 74 |
MapExtender& operator=(const CMap& cmap) { |
73 | 75 |
Parent::operator=(cmap); |
74 | 76 |
return *this; |
75 | 77 |
} |
76 | 78 |
|
77 | 79 |
public: |
78 | 80 |
class MapIt : public Item { |
79 | 81 |
typedef Item Parent; |
80 | 82 |
|
81 | 83 |
public: |
82 | 84 |
|
83 | 85 |
typedef typename Map::Value Value; |
84 | 86 |
|
85 | 87 |
MapIt() : map(NULL) {} |
86 | 88 |
|
87 | 89 |
MapIt(Invalid i) : Parent(i), map(NULL) {} |
88 | 90 |
|
89 | 91 |
explicit MapIt(Map& _map) : map(&_map) { |
90 | 92 |
map->notifier()->first(*this); |
91 | 93 |
} |
92 | 94 |
|
93 | 95 |
MapIt(const Map& _map, const Item& item) |
94 | 96 |
: Parent(item), map(&_map) {} |
95 | 97 |
|
96 | 98 |
MapIt& operator++() { |
97 | 99 |
map->notifier()->next(*this); |
98 | 100 |
return *this; |
99 | 101 |
} |
100 | 102 |
|
101 | 103 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
102 | 104 |
return (*map)[*this]; |
103 | 105 |
} |
104 | 106 |
|
105 | 107 |
typename MapTraits<Map>::ReturnValue operator*() { |
106 | 108 |
return (*map)[*this]; |
107 | 109 |
} |
108 | 110 |
|
109 | 111 |
void set(const Value& value) { |
110 | 112 |
map->set(*this, value); |
111 | 113 |
} |
112 | 114 |
|
113 | 115 |
protected: |
114 | 116 |
Map* map; |
115 | 117 |
|
116 | 118 |
}; |
117 | 119 |
|
118 | 120 |
class ConstMapIt : public Item { |
119 | 121 |
typedef Item Parent; |
120 | 122 |
|
121 | 123 |
public: |
122 | 124 |
|
123 | 125 |
typedef typename Map::Value Value; |
124 | 126 |
|
125 | 127 |
ConstMapIt() : map(NULL) {} |
126 | 128 |
|
127 | 129 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) {} |
128 | 130 |
|
129 | 131 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
130 | 132 |
map->notifier()->first(*this); |
131 | 133 |
} |
132 | 134 |
|
133 | 135 |
ConstMapIt(const Map& _map, const Item& item) |
134 | 136 |
: Parent(item), map(_map) {} |
135 | 137 |
|
136 | 138 |
ConstMapIt& operator++() { |
137 | 139 |
map->notifier()->next(*this); |
138 | 140 |
return *this; |
139 | 141 |
} |
140 | 142 |
|
141 | 143 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
142 | 144 |
return map[*this]; |
143 | 145 |
} |
144 | 146 |
|
145 | 147 |
protected: |
146 | 148 |
const Map* map; |
147 | 149 |
}; |
148 | 150 |
|
149 | 151 |
class ItemIt : public Item { |
150 | 152 |
typedef Item Parent; |
151 | 153 |
|
152 | 154 |
public: |
153 | 155 |
ItemIt() : map(NULL) {} |
154 | 156 |
|
155 | 157 |
|
156 | 158 |
ItemIt(Invalid i) : Parent(i), map(NULL) {} |
157 | 159 |
|
158 | 160 |
explicit ItemIt(Map& _map) : map(&_map) { |
159 | 161 |
map->notifier()->first(*this); |
160 | 162 |
} |
161 | 163 |
|
162 | 164 |
ItemIt(const Map& _map, const Item& item) |
163 | 165 |
: Parent(item), map(&_map) {} |
164 | 166 |
|
165 | 167 |
ItemIt& operator++() { |
166 | 168 |
map->notifier()->next(*this); |
167 | 169 |
return *this; |
168 | 170 |
} |
169 | 171 |
|
170 | 172 |
protected: |
171 | 173 |
const Map* map; |
172 | 174 |
|
173 | 175 |
}; |
174 | 176 |
}; |
175 | 177 |
|
176 | 178 |
// \ingroup graphbits |
177 | 179 |
// |
178 | 180 |
// \brief Extender for maps which use a subset of the items. |
179 | 181 |
template <typename _Graph, typename _Map> |
180 | 182 |
class SubMapExtender : public _Map { |
181 | 183 |
typedef _Map Parent; |
182 | 184 |
typedef _Graph GraphType; |
183 | 185 |
|
184 | 186 |
public: |
185 | 187 |
|
186 | 188 |
typedef SubMapExtender Map; |
187 | 189 |
typedef typename Parent::Key Item; |
188 | 190 |
|
189 | 191 |
typedef typename Parent::Key Key; |
190 | 192 |
typedef typename Parent::Value Value; |
191 | 193 |
typedef typename Parent::Reference Reference; |
192 | 194 |
typedef typename Parent::ConstReference ConstReference; |
193 | 195 |
|
196 |
typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
|
197 |
|
|
194 | 198 |
class MapIt; |
195 | 199 |
class ConstMapIt; |
196 | 200 |
|
197 | 201 |
friend class MapIt; |
198 | 202 |
friend class ConstMapIt; |
199 | 203 |
|
200 | 204 |
public: |
201 | 205 |
|
202 | 206 |
SubMapExtender(const GraphType& _graph) |
203 | 207 |
: Parent(_graph), graph(_graph) {} |
204 | 208 |
|
205 | 209 |
SubMapExtender(const GraphType& _graph, const Value& _value) |
206 | 210 |
: Parent(_graph, _value), graph(_graph) {} |
207 | 211 |
|
208 | 212 |
private: |
209 | 213 |
SubMapExtender& operator=(const SubMapExtender& cmap) { |
210 | 214 |
return operator=<MapExtender>(cmap); |
211 | 215 |
} |
212 | 216 |
|
213 | 217 |
template <typename CMap> |
214 | 218 |
SubMapExtender& operator=(const CMap& cmap) { |
215 | 219 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
216 | 220 |
Item it; |
217 | 221 |
for (graph.first(it); it != INVALID; graph.next(it)) { |
218 | 222 |
Parent::set(it, cmap[it]); |
219 | 223 |
} |
220 | 224 |
return *this; |
221 | 225 |
} |
222 | 226 |
|
223 | 227 |
public: |
224 | 228 |
class MapIt : public Item { |
225 | 229 |
typedef Item Parent; |
226 | 230 |
|
227 | 231 |
public: |
228 | 232 |
typedef typename Map::Value Value; |
229 | 233 |
|
230 | 234 |
MapIt() : map(NULL) {} |
231 | 235 |
|
232 | 236 |
MapIt(Invalid i) : Parent(i), map(NULL) { } |
233 | 237 |
|
234 | 238 |
explicit MapIt(Map& _map) : map(&_map) { |
235 | 239 |
map->graph.first(*this); |
236 | 240 |
} |
237 | 241 |
|
238 | 242 |
MapIt(const Map& _map, const Item& item) |
239 | 243 |
: Parent(item), map(&_map) {} |
240 | 244 |
|
241 | 245 |
MapIt& operator++() { |
242 | 246 |
map->graph.next(*this); |
243 | 247 |
return *this; |
244 | 248 |
} |
245 | 249 |
|
246 | 250 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
247 | 251 |
return (*map)[*this]; |
248 | 252 |
} |
249 | 253 |
|
250 | 254 |
typename MapTraits<Map>::ReturnValue operator*() { |
251 | 255 |
return (*map)[*this]; |
252 | 256 |
} |
253 | 257 |
|
254 | 258 |
void set(const Value& value) { |
255 | 259 |
map->set(*this, value); |
256 | 260 |
} |
257 | 261 |
|
258 | 262 |
protected: |
259 | 263 |
Map* map; |
260 | 264 |
|
261 | 265 |
}; |
262 | 266 |
|
263 | 267 |
class ConstMapIt : public Item { |
264 | 268 |
typedef Item Parent; |
265 | 269 |
|
266 | 270 |
public: |
267 | 271 |
|
268 | 272 |
typedef typename Map::Value Value; |
269 | 273 |
|
270 | 274 |
ConstMapIt() : map(NULL) {} |
271 | 275 |
|
272 | 276 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) { } |
273 | 277 |
|
274 | 278 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
275 | 279 |
map->graph.first(*this); |
276 | 280 |
} |
277 | 281 |
|
278 | 282 |
ConstMapIt(const Map& _map, const Item& item) |
279 | 283 |
: Parent(item), map(&_map) {} |
280 | 284 |
|
281 | 285 |
ConstMapIt& operator++() { |
282 | 286 |
map->graph.next(*this); |
283 | 287 |
return *this; |
284 | 288 |
} |
285 | 289 |
|
286 | 290 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
287 | 291 |
return (*map)[*this]; |
288 | 292 |
} |
289 | 293 |
|
290 | 294 |
protected: |
291 | 295 |
const Map* map; |
292 | 296 |
}; |
293 | 297 |
|
294 | 298 |
class ItemIt : public Item { |
295 | 299 |
typedef Item Parent; |
296 | 300 |
|
297 | 301 |
public: |
298 | 302 |
ItemIt() : map(NULL) {} |
299 | 303 |
|
300 | 304 |
|
301 | 305 |
ItemIt(Invalid i) : Parent(i), map(NULL) { } |
302 | 306 |
|
303 | 307 |
explicit ItemIt(Map& _map) : map(&_map) { |
304 | 308 |
map->graph.first(*this); |
305 | 309 |
} |
306 | 310 |
|
307 | 311 |
ItemIt(const Map& _map, const Item& item) |
308 | 312 |
: Parent(item), map(&_map) {} |
309 | 313 |
|
310 | 314 |
ItemIt& operator++() { |
311 | 315 |
map->graph.next(*this); |
312 | 316 |
return *this; |
313 | 317 |
} |
314 | 318 |
|
315 | 319 |
protected: |
316 | 320 |
const Map* map; |
317 | 321 |
|
318 | 322 |
}; |
319 | 323 |
|
320 | 324 |
private: |
321 | 325 |
|
322 | 326 |
const GraphType& graph; |
323 | 327 |
|
324 | 328 |
}; |
325 | 329 |
|
326 | 330 |
} |
327 | 331 |
|
328 | 332 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONCEPTS_MAPS_H |
20 | 20 |
#define LEMON_CONCEPTS_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
///\ingroup map_concepts |
26 | 26 |
///\file |
27 | 27 |
///\brief The concept of maps. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup map_concepts |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// Readable map concept |
37 | 37 |
|
38 | 38 |
/// Readable map concept. |
39 | 39 |
/// |
40 | 40 |
template<typename K, typename T> |
41 | 41 |
class ReadMap |
42 | 42 |
{ |
43 | 43 |
public: |
44 | 44 |
/// The key type of the map. |
45 | 45 |
typedef K Key; |
46 | 46 |
/// \brief The value type of the map. |
47 | 47 |
/// (The type of objects associated with the keys). |
48 | 48 |
typedef T Value; |
49 | 49 |
|
50 | 50 |
/// Returns the value associated with the given key. |
51 | 51 |
Value operator[](const Key &) const { |
52 | 52 |
return *static_cast<Value *>(0); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
template<typename _ReadMap> |
56 | 56 |
struct Constraints { |
57 | 57 |
void constraints() { |
58 | 58 |
Value val = m[key]; |
59 | 59 |
val = m[key]; |
60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
61 | 61 |
own_val = m[own_key]; |
62 | 62 |
|
63 | 63 |
ignore_unused_variable_warning(key); |
64 | 64 |
ignore_unused_variable_warning(val); |
65 | 65 |
ignore_unused_variable_warning(own_key); |
66 | 66 |
ignore_unused_variable_warning(own_val); |
67 | 67 |
} |
68 | 68 |
const Key& key; |
69 | 69 |
const typename _ReadMap::Key& own_key; |
70 | 70 |
const _ReadMap& m; |
71 | 71 |
}; |
72 | 72 |
|
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
/// Writable map concept |
77 | 77 |
|
78 | 78 |
/// Writable map concept. |
79 | 79 |
/// |
80 | 80 |
template<typename K, typename T> |
81 | 81 |
class WriteMap |
82 | 82 |
{ |
83 | 83 |
public: |
84 | 84 |
/// The key type of the map. |
85 | 85 |
typedef K Key; |
86 | 86 |
/// \brief The value type of the map. |
87 | 87 |
/// (The type of objects associated with the keys). |
88 | 88 |
typedef T Value; |
89 | 89 |
|
90 | 90 |
/// Sets the value associated with the given key. |
91 | 91 |
void set(const Key &, const Value &) {} |
92 | 92 |
|
93 | 93 |
/// Default constructor. |
94 | 94 |
WriteMap() {} |
95 | 95 |
|
96 | 96 |
template <typename _WriteMap> |
97 | 97 |
struct Constraints { |
98 | 98 |
void constraints() { |
99 | 99 |
m.set(key, val); |
100 | 100 |
m.set(own_key, own_val); |
101 | 101 |
|
102 | 102 |
ignore_unused_variable_warning(key); |
103 | 103 |
ignore_unused_variable_warning(val); |
104 | 104 |
ignore_unused_variable_warning(own_key); |
105 | 105 |
ignore_unused_variable_warning(own_val); |
106 | 106 |
} |
107 | 107 |
const Key& key; |
108 | 108 |
const Value& val; |
109 | 109 |
const typename _WriteMap::Key& own_key; |
110 | 110 |
const typename _WriteMap::Value& own_val; |
111 | 111 |
_WriteMap& m; |
112 | 112 |
}; |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// Read/writable map concept |
116 | 116 |
|
117 | 117 |
/// Read/writable map concept. |
118 | 118 |
/// |
119 | 119 |
template<typename K, typename T> |
120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
121 | 121 |
public WriteMap<K,T> |
122 | 122 |
{ |
123 | 123 |
public: |
124 | 124 |
/// The key type of the map. |
125 | 125 |
typedef K Key; |
126 | 126 |
/// \brief The value type of the map. |
127 | 127 |
/// (The type of objects associated with the keys). |
128 | 128 |
typedef T Value; |
129 | 129 |
|
130 | 130 |
/// Returns the value associated with the given key. |
131 | 131 |
Value operator[](const Key &) const { |
132 | 132 |
return *static_cast<Value *>(0); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
/// Sets the value associated with the given key. |
136 | 136 |
void set(const Key &, const Value &) {} |
137 | 137 |
|
138 | 138 |
template<typename _ReadWriteMap> |
139 | 139 |
struct Constraints { |
140 | 140 |
void constraints() { |
141 | 141 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
142 | 142 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
143 | 143 |
} |
144 | 144 |
}; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
|
148 | 148 |
/// Dereferable map concept |
149 | 149 |
|
150 | 150 |
/// Dereferable map concept. |
151 | 151 |
/// |
152 | 152 |
template<typename K, typename T, typename R, typename CR> |
153 | 153 |
class ReferenceMap : public ReadWriteMap<K,T> |
154 | 154 |
{ |
155 | 155 |
public: |
156 | 156 |
/// Tag for reference maps. |
157 | 157 |
typedef True ReferenceMapTag; |
158 | 158 |
/// The key type of the map. |
159 | 159 |
typedef K Key; |
160 | 160 |
/// \brief The value type of the map. |
161 | 161 |
/// (The type of objects associated with the keys). |
162 | 162 |
typedef T Value; |
163 | 163 |
/// The reference type of the map. |
164 | 164 |
typedef R Reference; |
165 | 165 |
/// The const reference type of the map. |
166 | 166 |
typedef CR ConstReference; |
167 | 167 |
|
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
/// Returns a reference to the value associated with the given key. |
171 | 171 |
Reference operator[](const Key &) { |
172 | 172 |
return *static_cast<Value *>(0); |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
/// Returns a const reference to the value associated with the given key. |
176 | 176 |
ConstReference operator[](const Key &) const { |
177 | 177 |
return *static_cast<Value *>(0); |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
/// Sets the value associated with the given key. |
181 | 181 |
void set(const Key &k,const Value &t) { operator[](k)=t; } |
182 | 182 |
|
183 | 183 |
template<typename _ReferenceMap> |
184 | 184 |
struct Constraints { |
185 |
|
|
185 |
typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type |
|
186 |
constraints() { |
|
186 | 187 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
187 | 188 |
ref = m[key]; |
188 | 189 |
m[key] = val; |
189 | 190 |
m[key] = ref; |
190 | 191 |
m[key] = cref; |
191 | 192 |
own_ref = m[own_key]; |
192 | 193 |
m[own_key] = own_val; |
193 | 194 |
m[own_key] = own_ref; |
194 | 195 |
m[own_key] = own_cref; |
195 | 196 |
m[key] = m[own_key]; |
196 | 197 |
m[own_key] = m[key]; |
197 | 198 |
} |
198 | 199 |
const Key& key; |
199 | 200 |
Value& val; |
200 | 201 |
Reference ref; |
201 | 202 |
ConstReference cref; |
202 | 203 |
const typename _ReferenceMap::Key& own_key; |
203 | 204 |
typename _ReferenceMap::Value& own_val; |
204 | 205 |
typename _ReferenceMap::Reference own_ref; |
205 | 206 |
typename _ReferenceMap::ConstReference own_cref; |
206 | 207 |
_ReferenceMap& m; |
207 | 208 |
}; |
208 | 209 |
}; |
209 | 210 |
|
210 | 211 |
// @} |
211 | 212 |
|
212 | 213 |
} //namespace concepts |
213 | 214 |
|
214 | 215 |
} //namespace lemon |
215 | 216 |
|
216 | 217 |
#endif |
1 | 1 |
INCLUDE_DIRECTORIES( |
2 | 2 |
${PROJECT_SOURCE_DIR} |
3 | 3 |
${PROJECT_BINARY_DIR} |
4 | 4 |
) |
5 | 5 |
|
6 | 6 |
LINK_DIRECTORIES( |
7 | 7 |
${PROJECT_BINARY_DIR}/lemon |
8 | 8 |
) |
9 | 9 |
|
10 | 10 |
SET(TEST_WITH_VALGRIND "NO" CACHE STRING |
11 | 11 |
"Run the test with valgrind (YES/NO).") |
12 | 12 |
SET(VALGRIND_FLAGS "" CACHE STRING "Valgrind flags used by the tests.") |
13 | 13 |
|
14 | 14 |
SET(TESTS |
15 | 15 |
adaptors_test |
16 | 16 |
bfs_test |
17 | 17 |
circulation_test |
18 | 18 |
connectivity_test |
19 | 19 |
counter_test |
20 | 20 |
dfs_test |
21 | 21 |
digraph_test |
22 | 22 |
dijkstra_test |
23 | 23 |
dim_test |
24 | 24 |
edge_set_test |
25 | 25 |
error_test |
26 | 26 |
euler_test |
27 | 27 |
gomory_hu_test |
28 | 28 |
graph_copy_test |
29 | 29 |
graph_test |
30 | 30 |
graph_utils_test |
31 | 31 |
hao_orlin_test |
32 | 32 |
heap_test |
33 | 33 |
kruskal_test |
34 | 34 |
maps_test |
35 | 35 |
matching_test |
36 | 36 |
min_cost_arborescence_test |
37 | 37 |
min_cost_flow_test |
38 | 38 |
path_test |
39 | 39 |
preflow_test |
40 | 40 |
radix_sort_test |
41 | 41 |
random_test |
42 | 42 |
suurballe_test |
43 | 43 |
time_measure_test |
44 | 44 |
unionfind_test |
45 | 45 |
) |
46 | 46 |
|
47 | 47 |
IF(LEMON_HAVE_LP) |
48 | 48 |
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer") |
49 | 49 |
ADD_EXECUTABLE(lp_test lp_test.cc) |
50 | 50 |
ELSE() |
51 | 51 |
ADD_EXECUTABLE(lp_test EXCLUDE_FROM_ALL lp_test.cc) |
52 | 52 |
ENDIF() |
53 | 53 |
|
54 | 54 |
SET(LP_TEST_LIBS lemon) |
55 | 55 |
|
56 | 56 |
IF(LEMON_HAVE_GLPK) |
57 | 57 |
SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${GLPK_LIBRARIES}) |
58 | 58 |
ENDIF() |
59 | 59 |
IF(LEMON_HAVE_CPLEX) |
60 | 60 |
SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${CPLEX_LIBRARIES}) |
61 | 61 |
ENDIF() |
62 | 62 |
IF(LEMON_HAVE_CLP) |
63 | 63 |
SET(LP_TEST_LIBS ${LP_TEST_LIBS} ${COIN_CLP_LIBRARIES}) |
64 | 64 |
ENDIF() |
65 | 65 |
|
66 | 66 |
TARGET_LINK_LIBRARIES(lp_test ${LP_TEST_LIBS}) |
67 | 67 |
ADD_TEST(lp_test lp_test) |
68 |
ADD_DEPENDENCIES(check lp_test) |
|
68 | 69 |
|
69 | 70 |
IF(WIN32 AND LEMON_HAVE_GLPK) |
70 | 71 |
GET_TARGET_PROPERTY(TARGET_LOC lp_test LOCATION) |
71 | 72 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
72 | 73 |
ADD_CUSTOM_COMMAND(TARGET lp_test POST_BUILD |
73 | 74 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/glpk.dll ${TARGET_PATH} |
74 | 75 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/libltdl3.dll ${TARGET_PATH} |
75 | 76 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/zlib1.dll ${TARGET_PATH} |
76 | 77 |
) |
77 | 78 |
ENDIF() |
78 | 79 |
|
79 | 80 |
IF(WIN32 AND LEMON_HAVE_CPLEX) |
80 | 81 |
GET_TARGET_PROPERTY(TARGET_LOC lp_test LOCATION) |
81 | 82 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
82 | 83 |
ADD_CUSTOM_COMMAND(TARGET lp_test POST_BUILD |
83 | 84 |
COMMAND ${CMAKE_COMMAND} -E copy ${CPLEX_BIN_DIR}/cplex91.dll ${TARGET_PATH} |
84 | 85 |
) |
85 | 86 |
ENDIF() |
86 | 87 |
ENDIF() |
87 | 88 |
|
88 | 89 |
IF(LEMON_HAVE_MIP) |
89 | 90 |
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer") |
90 | 91 |
ADD_EXECUTABLE(mip_test mip_test.cc) |
91 | 92 |
ELSE() |
92 | 93 |
ADD_EXECUTABLE(mip_test EXCLUDE_FROM_ALL mip_test.cc) |
93 | 94 |
ENDIF() |
94 | 95 |
|
95 | 96 |
SET(MIP_TEST_LIBS lemon) |
96 | 97 |
|
97 | 98 |
IF(LEMON_HAVE_GLPK) |
98 | 99 |
SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${GLPK_LIBRARIES}) |
99 | 100 |
ENDIF() |
100 | 101 |
IF(LEMON_HAVE_CPLEX) |
101 | 102 |
SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${CPLEX_LIBRARIES}) |
102 | 103 |
ENDIF() |
103 | 104 |
IF(LEMON_HAVE_CBC) |
104 | 105 |
SET(MIP_TEST_LIBS ${MIP_TEST_LIBS} ${COIN_CBC_LIBRARIES}) |
105 | 106 |
ENDIF() |
106 | 107 |
|
107 | 108 |
TARGET_LINK_LIBRARIES(mip_test ${MIP_TEST_LIBS}) |
108 | 109 |
ADD_TEST(mip_test mip_test) |
110 |
ADD_DEPENDENCIES(check mip_test) |
|
109 | 111 |
|
110 | 112 |
IF(WIN32 AND LEMON_HAVE_GLPK) |
111 | 113 |
GET_TARGET_PROPERTY(TARGET_LOC mip_test LOCATION) |
112 | 114 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
113 | 115 |
ADD_CUSTOM_COMMAND(TARGET mip_test POST_BUILD |
114 | 116 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/glpk.dll ${TARGET_PATH} |
115 | 117 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/libltdl3.dll ${TARGET_PATH} |
116 | 118 |
COMMAND ${CMAKE_COMMAND} -E copy ${GLPK_BIN_DIR}/zlib1.dll ${TARGET_PATH} |
117 | 119 |
) |
118 | 120 |
ENDIF() |
119 | 121 |
|
120 | 122 |
IF(WIN32 AND LEMON_HAVE_CPLEX) |
121 | 123 |
GET_TARGET_PROPERTY(TARGET_LOC mip_test LOCATION) |
122 | 124 |
GET_FILENAME_COMPONENT(TARGET_PATH ${TARGET_LOC} PATH) |
123 | 125 |
ADD_CUSTOM_COMMAND(TARGET mip_test POST_BUILD |
124 | 126 |
COMMAND ${CMAKE_COMMAND} -E copy ${CPLEX_BIN_DIR}/cplex91.dll ${TARGET_PATH} |
125 | 127 |
) |
126 | 128 |
ENDIF() |
127 | 129 |
ENDIF() |
128 | 130 |
|
129 | 131 |
FOREACH(TEST_NAME ${TESTS}) |
130 | 132 |
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer") |
131 | 133 |
ADD_EXECUTABLE(${TEST_NAME} ${TEST_NAME}.cc) |
132 | 134 |
ELSE() |
133 | 135 |
ADD_EXECUTABLE(${TEST_NAME} EXCLUDE_FROM_ALL ${TEST_NAME}.cc) |
134 | 136 |
ENDIF() |
135 | 137 |
TARGET_LINK_LIBRARIES(${TEST_NAME} lemon) |
136 | 138 |
IF(TEST_WITH_VALGRIND) |
137 | 139 |
ADD_TEST(${TEST_NAME} |
138 | 140 |
valgrind --error-exitcode=1 ${VALGRIND_FLAGS} |
139 | 141 |
${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME} ) |
140 | 142 |
ELSE() |
141 | 143 |
ADD_TEST(${TEST_NAME} ${TEST_NAME}) |
142 | 144 |
ENDIF() |
143 | 145 |
ADD_DEPENDENCIES(check ${TEST_NAME}) |
144 | 146 |
ENDFOREACH() |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <iostream> |
20 | 20 |
#include <fstream> |
21 | 21 |
#include <string> |
22 | 22 |
#include <vector> |
23 | 23 |
|
24 | 24 |
#include <lemon/concept_check.h> |
25 | 25 |
#include <lemon/concepts/heap.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/smart_graph.h> |
28 | 28 |
|
29 | 29 |
#include <lemon/lgf_reader.h> |
30 | 30 |
#include <lemon/dijkstra.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bin_heap.h> |
34 |
#include <lemon/fib_heap.h> |
|
35 |
#include <lemon/radix_heap.h> |
|
36 |
#include <lemon/bucket_heap.h> |
|
34 | 37 |
|
35 | 38 |
#include "test_tools.h" |
36 | 39 |
|
37 | 40 |
using namespace lemon; |
38 | 41 |
using namespace lemon::concepts; |
39 | 42 |
|
40 | 43 |
typedef ListDigraph Digraph; |
41 | 44 |
DIGRAPH_TYPEDEFS(Digraph); |
42 | 45 |
|
43 | 46 |
char test_lgf[] = |
44 | 47 |
"@nodes\n" |
45 | 48 |
"label\n" |
46 | 49 |
"0\n" |
47 | 50 |
"1\n" |
48 | 51 |
"2\n" |
49 | 52 |
"3\n" |
50 | 53 |
"4\n" |
51 | 54 |
"5\n" |
52 | 55 |
"6\n" |
53 | 56 |
"7\n" |
54 | 57 |
"8\n" |
55 | 58 |
"9\n" |
56 | 59 |
"@arcs\n" |
57 | 60 |
" label capacity\n" |
58 | 61 |
"0 5 0 94\n" |
59 | 62 |
"3 9 1 11\n" |
60 | 63 |
"8 7 2 83\n" |
61 | 64 |
"1 2 3 94\n" |
62 | 65 |
"5 7 4 35\n" |
63 | 66 |
"7 4 5 84\n" |
64 | 67 |
"9 5 6 38\n" |
65 | 68 |
"0 4 7 96\n" |
66 | 69 |
"6 7 8 6\n" |
67 | 70 |
"3 1 9 27\n" |
68 | 71 |
"5 2 10 77\n" |
69 | 72 |
"5 6 11 69\n" |
70 | 73 |
"6 5 12 41\n" |
71 | 74 |
"4 6 13 70\n" |
72 | 75 |
"3 2 14 45\n" |
73 | 76 |
"7 9 15 93\n" |
74 | 77 |
"5 9 16 50\n" |
75 | 78 |
"9 0 17 94\n" |
76 | 79 |
"9 6 18 67\n" |
77 | 80 |
"0 9 19 86\n" |
78 | 81 |
"@attributes\n" |
79 | 82 |
"source 3\n"; |
80 | 83 |
|
81 | 84 |
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34}; |
82 | 85 |
int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37}; |
83 | 86 |
|
84 | 87 |
int test_len = sizeof(test_seq) / sizeof(test_seq[0]); |
85 | 88 |
|
86 | 89 |
template <typename Heap> |
87 | 90 |
void heapSortTest() { |
88 | 91 |
RangeMap<int> map(test_len, -1); |
89 | 92 |
|
90 | 93 |
Heap heap(map); |
91 | 94 |
|
92 | 95 |
std::vector<int> v(test_len); |
93 | 96 |
|
94 | 97 |
for (int i = 0; i < test_len; ++i) { |
95 | 98 |
v[i] = test_seq[i]; |
96 | 99 |
heap.push(i, v[i]); |
97 | 100 |
} |
98 | 101 |
std::sort(v.begin(), v.end()); |
99 | 102 |
for (int i = 0; i < test_len; ++i) { |
100 | 103 |
check(v[i] == heap.prio() ,"Wrong order in heap sort."); |
101 | 104 |
heap.pop(); |
102 | 105 |
} |
103 | 106 |
} |
104 | 107 |
|
105 | 108 |
template <typename Heap> |
106 | 109 |
void heapIncreaseTest() { |
107 | 110 |
RangeMap<int> map(test_len, -1); |
108 | 111 |
|
109 | 112 |
Heap heap(map); |
110 | 113 |
|
111 | 114 |
std::vector<int> v(test_len); |
112 | 115 |
|
113 | 116 |
for (int i = 0; i < test_len; ++i) { |
114 | 117 |
v[i] = test_seq[i]; |
115 | 118 |
heap.push(i, v[i]); |
116 | 119 |
} |
117 | 120 |
for (int i = 0; i < test_len; ++i) { |
118 | 121 |
v[i] += test_inc[i]; |
119 | 122 |
heap.increase(i, v[i]); |
120 | 123 |
} |
121 | 124 |
std::sort(v.begin(), v.end()); |
122 | 125 |
for (int i = 0; i < test_len; ++i) { |
123 | 126 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test."); |
124 | 127 |
heap.pop(); |
125 | 128 |
} |
126 | 129 |
} |
127 | 130 |
|
128 | 131 |
|
129 | 132 |
|
130 | 133 |
template <typename Heap> |
131 | 134 |
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length, |
132 | 135 |
Node source) { |
133 | 136 |
|
134 | 137 |
typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>:: |
135 | 138 |
Create dijkstra(digraph, length); |
136 | 139 |
|
137 | 140 |
dijkstra.run(source); |
138 | 141 |
|
139 | 142 |
for(ArcIt a(digraph); a != INVALID; ++a) { |
140 | 143 |
Node s = digraph.source(a); |
141 | 144 |
Node t = digraph.target(a); |
142 | 145 |
if (dijkstra.reached(s)) { |
143 | 146 |
check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a], |
144 | 147 |
"Error in a shortest path tree!"); |
145 | 148 |
} |
146 | 149 |
} |
147 | 150 |
|
148 | 151 |
for(NodeIt n(digraph); n != INVALID; ++n) { |
149 | 152 |
if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) { |
150 | 153 |
Arc a = dijkstra.predArc(n); |
151 | 154 |
Node s = digraph.source(a); |
152 | 155 |
check( dijkstra.dist(n) - dijkstra.dist(s) == length[a], |
153 | 156 |
"Error in a shortest path tree!"); |
154 | 157 |
} |
155 | 158 |
} |
156 | 159 |
|
157 | 160 |
} |
158 | 161 |
|
159 | 162 |
int main() { |
160 | 163 |
|
161 | 164 |
typedef int Item; |
162 | 165 |
typedef int Prio; |
163 | 166 |
typedef RangeMap<int> ItemIntMap; |
164 | 167 |
|
165 | 168 |
Digraph digraph; |
166 | 169 |
IntArcMap length(digraph); |
167 | 170 |
Node source; |
168 | 171 |
|
169 | 172 |
std::istringstream input(test_lgf); |
170 | 173 |
digraphReader(digraph, input). |
171 | 174 |
arcMap("capacity", length). |
172 | 175 |
node("source", source). |
173 | 176 |
run(); |
174 | 177 |
|
175 | 178 |
{ |
176 | 179 |
typedef BinHeap<Prio, ItemIntMap> IntHeap; |
177 | 180 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
178 | 181 |
heapSortTest<IntHeap>(); |
179 | 182 |
heapIncreaseTest<IntHeap>(); |
180 | 183 |
|
181 | 184 |
typedef BinHeap<Prio, IntNodeMap > NodeHeap; |
182 | 185 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
183 | 186 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
184 | 187 |
} |
185 | 188 |
|
189 |
{ |
|
190 |
typedef FibHeap<Prio, ItemIntMap> IntHeap; |
|
191 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
192 |
heapSortTest<IntHeap>(); |
|
193 |
heapIncreaseTest<IntHeap>(); |
|
194 |
|
|
195 |
typedef FibHeap<Prio, IntNodeMap > NodeHeap; |
|
196 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
|
197 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
|
198 |
} |
|
199 |
|
|
200 |
{ |
|
201 |
typedef RadixHeap<ItemIntMap> IntHeap; |
|
202 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
203 |
heapSortTest<IntHeap>(); |
|
204 |
heapIncreaseTest<IntHeap>(); |
|
205 |
|
|
206 |
typedef RadixHeap<IntNodeMap > NodeHeap; |
|
207 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
|
208 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
|
209 |
} |
|
210 |
|
|
211 |
{ |
|
212 |
typedef BucketHeap<ItemIntMap> IntHeap; |
|
213 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
|
214 |
heapSortTest<IntHeap>(); |
|
215 |
heapIncreaseTest<IntHeap>(); |
|
216 |
|
|
217 |
typedef BucketHeap<IntNodeMap > NodeHeap; |
|
218 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
|
219 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
|
220 |
} |
|
221 |
|
|
222 |
|
|
186 | 223 |
return 0; |
187 | 224 |
} |
0 comments (0 inline)