gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge #419 to branch 1.1
0 7 3
merge 1.1
5 files changed with 1535 insertions and 10 deletions:
↑ Collapse diff ↑
Ignore white space 16 line context
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
Ignore white space 16 line context
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

	
Ignore white space 16 line context
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
Ignore white space 16 line context
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};${COIN_OSI_VOL_LIBRARY};${COIN_VOL_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
Ignore white space 16 line context
... ...
@@ -54,16 +54,17 @@
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 \
... ...
@@ -71,16 +72,17 @@
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 \
... ...
@@ -93,16 +95,17 @@
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 \
Ignore white space 16 line context
... ...
@@ -28,46 +28,46 @@
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 Comp specifies the ordering of the priorities.
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 Comp A functor class for the ordering of the priorities.
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 Comp = std::less<PR> >
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 Comp Compare;
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
Ignore white space 16 line context
... ...
@@ -44,16 +44,18 @@
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

	
... ...
@@ -186,16 +188,18 @@
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

	
Ignore white space 16 line context
... ...
@@ -177,17 +177,18 @@
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
        void constraints() {
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;
Ignore white space 16 line context
... ...
@@ -60,16 +60,17 @@
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}
... ...
@@ -101,16 +102,17 @@
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}
Ignore white space 16 line context
... ...
@@ -26,16 +26,19 @@
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);
... ...
@@ -178,10 +181,44 @@
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)