gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Merge
0 5 3
merge default
2 files changed with 2605 insertions and 13 deletions:
↑ Collapse diff ↑
Ignore white space 6 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 1536 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 6 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 6 line context
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/list_graph.h \
94 96
	lemon/maps.h \
95 97
	lemon/matching.h \
96 98
	lemon/math.h \
97 99
	lemon/min_cost_arborescence.h \
98 100
	lemon/nauty_reader.h \
99 101
	lemon/network_simplex.h \
100 102
	lemon/path.h \
101 103
	lemon/preflow.h \
104
	lemon/radix_heap.h \
102 105
	lemon/radix_sort.h \
103 106
	lemon/random.h \
104 107
	lemon/smart_graph.h \
105 108
	lemon/soplex.h \
106 109
	lemon/suurballe.h \
107 110
	lemon/time_measure.h \
108 111
	lemon/tolerance.h \
109 112
	lemon/unionfind.h \
110 113
	lemon/bits/windows.h
111 114

	
112 115
bits_HEADERS += \
113 116
	lemon/bits/alteration_notifier.h \
114 117
	lemon/bits/array_map.h \
115 118
	lemon/bits/bezier.h \
116 119
	lemon/bits/default_map.h \
117 120
	lemon/bits/edge_set_extender.h \
118 121
	lemon/bits/enable_if.h \
119 122
	lemon/bits/graph_adaptor_extender.h \
120 123
	lemon/bits/graph_extender.h \
121 124
	lemon/bits/map_extender.h \
122 125
	lemon/bits/path_dump.h \
123 126
	lemon/bits/solver_bits.h \
124 127
	lemon/bits/traits.h \
125 128
	lemon/bits/variant.h \
126 129
	lemon/bits/vector_map.h
127 130

	
128 131
concept_HEADERS += \
129 132
	lemon/concepts/digraph.h \
130 133
	lemon/concepts/graph.h \
131 134
	lemon/concepts/graph_components.h \
132 135
	lemon/concepts/heap.h \
133 136
	lemon/concepts/maps.h \
134 137
	lemon/concepts/path.h
Ignore white space 6 line context
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 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
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
Ignore white space 6 line context
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_MAPS_H
20 20
#define LEMON_MAPS_H
21 21

	
22 22
#include <iterator>
23 23
#include <functional>
24 24
#include <vector>
25
#include <map>
25 26

	
26 27
#include <lemon/core.h>
27 28

	
28 29
///\file
29 30
///\ingroup maps
30 31
///\brief Miscellaneous property maps
31 32

	
32
#include <map>
33

	
34 33
namespace lemon {
35 34

	
36 35
  /// \addtogroup maps
37 36
  /// @{
38 37

	
39 38
  /// Base class of maps.
40 39

	
41 40
  /// Base class of maps. It provides the necessary type definitions
42 41
  /// required by the map %concepts.
43 42
  template<typename K, typename V>
44 43
  class MapBase {
45 44
  public:
46 45
    /// \brief The key type of the map.
47 46
    typedef K Key;
48 47
    /// \brief The value type of the map.
49 48
    /// (The type of objects associated with the keys).
50 49
    typedef V Value;
51 50
  };
52 51

	
53 52

	
54 53
  /// Null map. (a.k.a. DoNothingMap)
55 54

	
56 55
  /// This map can be used if you have to provide a map only for
57 56
  /// its type definitions, or if you have to provide a writable map,
58 57
  /// but data written to it is not required (i.e. it will be sent to
59 58
  /// <tt>/dev/null</tt>).
60 59
  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
61 60
  ///
62 61
  /// \sa ConstMap
63 62
  template<typename K, typename V>
64 63
  class NullMap : public MapBase<K, V> {
65 64
  public:
66 65
    ///\e
67 66
    typedef K Key;
68 67
    ///\e
69 68
    typedef V Value;
70 69

	
71 70
    /// Gives back a default constructed element.
72 71
    Value operator[](const Key&) const { return Value(); }
73 72
    /// Absorbs the value.
74 73
    void set(const Key&, const Value&) {}
75 74
  };
76 75

	
77 76
  /// Returns a \c NullMap class
78 77

	
79 78
  /// This function just returns a \c NullMap class.
80 79
  /// \relates NullMap
81 80
  template <typename K, typename V>
82 81
  NullMap<K, V> nullMap() {
83 82
    return NullMap<K, V>();
84 83
  }
85 84

	
86 85

	
87 86
  /// Constant map.
88 87

	
89 88
  /// This \ref concepts::ReadMap "readable map" assigns a specified
90 89
  /// value to each key.
91 90
  ///
92 91
  /// In other aspects it is equivalent to \c NullMap.
93 92
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
94 93
  /// concept, but it absorbs the data written to it.
95 94
  ///
96 95
  /// The simplest way of using this map is through the constMap()
97 96
  /// function.
98 97
  ///
99 98
  /// \sa NullMap
100 99
  /// \sa IdentityMap
101 100
  template<typename K, typename V>
102 101
  class ConstMap : public MapBase<K, V> {
103 102
  private:
104 103
    V _value;
105 104
  public:
106 105
    ///\e
107 106
    typedef K Key;
108 107
    ///\e
109 108
    typedef V Value;
110 109

	
111 110
    /// Default constructor
112 111

	
113 112
    /// Default constructor.
114 113
    /// The value of the map will be default constructed.
115 114
    ConstMap() {}
116 115

	
117 116
    /// Constructor with specified initial value
118 117

	
119 118
    /// Constructor with specified initial value.
120 119
    /// \param v The initial value of the map.
121 120
    ConstMap(const Value &v) : _value(v) {}
122 121

	
123 122
    /// Gives back the specified value.
124 123
    Value operator[](const Key&) const { return _value; }
125 124

	
126 125
    /// Absorbs the value.
127 126
    void set(const Key&, const Value&) {}
128 127

	
129 128
    /// Sets the value that is assigned to each key.
130 129
    void setAll(const Value &v) {
131 130
      _value = v;
132 131
    }
133 132

	
134 133
    template<typename V1>
135 134
    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
136 135
  };
137 136

	
138 137
  /// Returns a \c ConstMap class
139 138

	
140 139
  /// This function just returns a \c ConstMap class.
141 140
  /// \relates ConstMap
142 141
  template<typename K, typename V>
143 142
  inline ConstMap<K, V> constMap(const V &v) {
144 143
    return ConstMap<K, V>(v);
145 144
  }
146 145

	
147 146
  template<typename K, typename V>
148 147
  inline ConstMap<K, V> constMap() {
149 148
    return ConstMap<K, V>();
150 149
  }
151 150

	
152 151

	
153 152
  template<typename T, T v>
154 153
  struct Const {};
155 154

	
156 155
  /// Constant map with inlined constant value.
157 156

	
158 157
  /// This \ref concepts::ReadMap "readable map" assigns a specified
159 158
  /// value to each key.
160 159
  ///
161 160
  /// In other aspects it is equivalent to \c NullMap.
162 161
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
163 162
  /// concept, but it absorbs the data written to it.
164 163
  ///
165 164
  /// The simplest way of using this map is through the constMap()
166 165
  /// function.
167 166
  ///
168 167
  /// \sa NullMap
169 168
  /// \sa IdentityMap
170 169
  template<typename K, typename V, V v>
171 170
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
172 171
  public:
173 172
    ///\e
174 173
    typedef K Key;
175 174
    ///\e
176 175
    typedef V Value;
177 176

	
178 177
    /// Constructor.
179 178
    ConstMap() {}
180 179

	
181 180
    /// Gives back the specified value.
182 181
    Value operator[](const Key&) const { return v; }
183 182

	
184 183
    /// Absorbs the value.
185 184
    void set(const Key&, const Value&) {}
186 185
  };
187 186

	
188 187
  /// Returns a \c ConstMap class with inlined constant value
189 188

	
190 189
  /// This function just returns a \c ConstMap class with inlined
191 190
  /// constant value.
192 191
  /// \relates ConstMap
193 192
  template<typename K, typename V, V v>
194 193
  inline ConstMap<K, Const<V, v> > constMap() {
195 194
    return ConstMap<K, Const<V, v> >();
196 195
  }
197 196

	
198 197

	
199 198
  /// Identity map.
200 199

	
201 200
  /// This \ref concepts::ReadMap "read-only map" gives back the given
202 201
  /// key as value without any modification.
203 202
  ///
204 203
  /// \sa ConstMap
205 204
  template <typename T>
206 205
  class IdentityMap : public MapBase<T, T> {
207 206
  public:
208 207
    ///\e
209 208
    typedef T Key;
210 209
    ///\e
211 210
    typedef T Value;
212 211

	
213 212
    /// Gives back the given value without any modification.
214 213
    Value operator[](const Key &k) const {
215 214
      return k;
216 215
    }
217 216
  };
218 217

	
219 218
  /// Returns an \c IdentityMap class
220 219

	
221 220
  /// This function just returns an \c IdentityMap class.
222 221
  /// \relates IdentityMap
223 222
  template<typename T>
224 223
  inline IdentityMap<T> identityMap() {
225 224
    return IdentityMap<T>();
226 225
  }
227 226

	
228 227

	
229 228
  /// \brief Map for storing values for integer keys from the range
230 229
  /// <tt>[0..size-1]</tt>.
231 230
  ///
232 231
  /// This map is essentially a wrapper for \c std::vector. It assigns
233 232
  /// values to integer keys from the range <tt>[0..size-1]</tt>.
234 233
  /// It can be used with some data structures, for example
235 234
  /// \c UnionFind, \c BinHeap, when the used items are small
236 235
  /// integers. This map conforms the \ref concepts::ReferenceMap
237 236
  /// "ReferenceMap" concept.
238 237
  ///
239 238
  /// The simplest way of using this map is through the rangeMap()
240 239
  /// function.
241 240
  template <typename V>
242 241
  class RangeMap : public MapBase<int, V> {
243 242
    template <typename V1>
244 243
    friend class RangeMap;
245 244
  private:
246 245

	
247 246
    typedef std::vector<V> Vector;
248 247
    Vector _vector;
249 248

	
250 249
  public:
251 250

	
252 251
    /// Key type
253 252
    typedef int Key;
254 253
    /// Value type
255 254
    typedef V Value;
256 255
    /// Reference type
257 256
    typedef typename Vector::reference Reference;
258 257
    /// Const reference type
259 258
    typedef typename Vector::const_reference ConstReference;
260 259

	
261 260
    typedef True ReferenceMapTag;
262 261

	
263 262
  public:
264 263

	
265 264
    /// Constructor with specified default value.
266 265
    RangeMap(int size = 0, const Value &value = Value())
267 266
      : _vector(size, value) {}
268 267

	
269 268
    /// Constructs the map from an appropriate \c std::vector.
270 269
    template <typename V1>
271 270
    RangeMap(const std::vector<V1>& vector)
272 271
      : _vector(vector.begin(), vector.end()) {}
273 272

	
274 273
    /// Constructs the map from another \c RangeMap.
275 274
    template <typename V1>
276 275
    RangeMap(const RangeMap<V1> &c)
277 276
      : _vector(c._vector.begin(), c._vector.end()) {}
278 277

	
279 278
    /// Returns the size of the map.
280 279
    int size() {
281 280
      return _vector.size();
282 281
    }
283 282

	
284 283
    /// Resizes the map.
285 284

	
286 285
    /// Resizes the underlying \c std::vector container, so changes the
287 286
    /// keyset of the map.
288 287
    /// \param size The new size of the map. The new keyset will be the
289 288
    /// range <tt>[0..size-1]</tt>.
290 289
    /// \param value The default value to assign to the new keys.
291 290
    void resize(int size, const Value &value = Value()) {
292 291
      _vector.resize(size, value);
293 292
    }
294 293

	
295 294
  private:
296 295

	
297 296
    RangeMap& operator=(const RangeMap&);
298 297

	
299 298
  public:
300 299

	
301 300
    ///\e
302 301
    Reference operator[](const Key &k) {
303 302
      return _vector[k];
304 303
    }
305 304

	
306 305
    ///\e
307 306
    ConstReference operator[](const Key &k) const {
308 307
      return _vector[k];
309 308
    }
310 309

	
311 310
    ///\e
312 311
    void set(const Key &k, const Value &v) {
313 312
      _vector[k] = v;
314 313
    }
315 314
  };
316 315

	
317 316
  /// Returns a \c RangeMap class
318 317

	
319 318
  /// This function just returns a \c RangeMap class.
320 319
  /// \relates RangeMap
321 320
  template<typename V>
322 321
  inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
323 322
    return RangeMap<V>(size, value);
324 323
  }
325 324

	
326 325
  /// \brief Returns a \c RangeMap class created from an appropriate
327 326
  /// \c std::vector
328 327

	
329 328
  /// This function just returns a \c RangeMap class created from an
330 329
  /// appropriate \c std::vector.
331 330
  /// \relates RangeMap
332 331
  template<typename V>
333 332
  inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
334 333
    return RangeMap<V>(vector);
335 334
  }
336 335

	
337 336

	
338 337
  /// Map type based on \c std::map
339 338

	
340 339
  /// This map is essentially a wrapper for \c std::map with addition
341 340
  /// that you can specify a default value for the keys that are not
342 341
  /// stored actually. This value can be different from the default
343 342
  /// contructed value (i.e. \c %Value()).
344 343
  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
345 344
  /// concept.
346 345
  ///
347 346
  /// This map is useful if a default value should be assigned to most of
348 347
  /// the keys and different values should be assigned only to a few
349 348
  /// keys (i.e. the map is "sparse").
350 349
  /// The name of this type also refers to this important usage.
351 350
  ///
352 351
  /// Apart form that this map can be used in many other cases since it
353 352
  /// is based on \c std::map, which is a general associative container.
354 353
  /// However keep in mind that it is usually not as efficient as other
355 354
  /// maps.
356 355
  ///
357 356
  /// The simplest way of using this map is through the sparseMap()
358 357
  /// function.
359 358
  template <typename K, typename V, typename Comp = std::less<K> >
360 359
  class SparseMap : public MapBase<K, V> {
361 360
    template <typename K1, typename V1, typename C1>
362 361
    friend class SparseMap;
363 362
  public:
364 363

	
365 364
    /// Key type
366 365
    typedef K Key;
367 366
    /// Value type
368 367
    typedef V Value;
369 368
    /// Reference type
370 369
    typedef Value& Reference;
371 370
    /// Const reference type
372 371
    typedef const Value& ConstReference;
373 372

	
374 373
    typedef True ReferenceMapTag;
375 374

	
376 375
  private:
377 376

	
378 377
    typedef std::map<K, V, Comp> Map;
379 378
    Map _map;
380 379
    Value _value;
381 380

	
382 381
  public:
383 382

	
384 383
    /// \brief Constructor with specified default value.
385 384
    SparseMap(const Value &value = Value()) : _value(value) {}
386 385
    /// \brief Constructs the map from an appropriate \c std::map, and
387 386
    /// explicitly specifies a default value.
388 387
    template <typename V1, typename Comp1>
389 388
    SparseMap(const std::map<Key, V1, Comp1> &map,
390 389
              const Value &value = Value())
391 390
      : _map(map.begin(), map.end()), _value(value) {}
392 391

	
393 392
    /// \brief Constructs the map from another \c SparseMap.
394 393
    template<typename V1, typename Comp1>
395 394
    SparseMap(const SparseMap<Key, V1, Comp1> &c)
396 395
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
397 396

	
398 397
  private:
399 398

	
400 399
    SparseMap& operator=(const SparseMap&);
401 400

	
402 401
  public:
403 402

	
404 403
    ///\e
405 404
    Reference operator[](const Key &k) {
406 405
      typename Map::iterator it = _map.lower_bound(k);
407 406
      if (it != _map.end() && !_map.key_comp()(k, it->first))
408 407
        return it->second;
409 408
      else
410 409
        return _map.insert(it, std::make_pair(k, _value))->second;
411 410
    }
412 411

	
413 412
    ///\e
414 413
    ConstReference operator[](const Key &k) const {
415 414
      typename Map::const_iterator it = _map.find(k);
416 415
      if (it != _map.end())
417 416
        return it->second;
418 417
      else
419 418
        return _value;
420 419
    }
421 420

	
422 421
    ///\e
423 422
    void set(const Key &k, const Value &v) {
424 423
      typename Map::iterator it = _map.lower_bound(k);
425 424
      if (it != _map.end() && !_map.key_comp()(k, it->first))
426 425
        it->second = v;
427 426
      else
428 427
        _map.insert(it, std::make_pair(k, v));
429 428
    }
430 429

	
431 430
    ///\e
432 431
    void setAll(const Value &v) {
433 432
      _value = v;
434 433
      _map.clear();
435 434
    }
436 435
  };
437 436

	
438 437
  /// Returns a \c SparseMap class
439 438

	
440 439
  /// This function just returns a \c SparseMap class with specified
441 440
  /// default value.
442 441
  /// \relates SparseMap
443 442
  template<typename K, typename V, typename Compare>
444 443
  inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
445 444
    return SparseMap<K, V, Compare>(value);
446 445
  }
447 446

	
448 447
  template<typename K, typename V>
449 448
  inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
450 449
    return SparseMap<K, V, std::less<K> >(value);
451 450
  }
452 451

	
453 452
  /// \brief Returns a \c SparseMap class created from an appropriate
454 453
  /// \c std::map
455 454

	
456 455
  /// This function just returns a \c SparseMap class created from an
457 456
  /// appropriate \c std::map.
458 457
  /// \relates SparseMap
459 458
  template<typename K, typename V, typename Compare>
460 459
  inline SparseMap<K, V, Compare>
461 460
    sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
462 461
  {
463 462
    return SparseMap<K, V, Compare>(map, value);
464 463
  }
465 464

	
466 465
  /// @}
467 466

	
468 467
  /// \addtogroup map_adaptors
469 468
  /// @{
470 469

	
471 470
  /// Composition of two maps
472 471

	
473 472
  /// This \ref concepts::ReadMap "read-only map" returns the
474 473
  /// composition of two given maps. That is to say, if \c m1 is of
475 474
  /// type \c M1 and \c m2 is of \c M2, then for
476 475
  /// \code
477 476
  ///   ComposeMap<M1, M2> cm(m1,m2);
478 477
  /// \endcode
479 478
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
480 479
  ///
481 480
  /// The \c Key type of the map is inherited from \c M2 and the
482 481
  /// \c Value type is from \c M1.
483 482
  /// \c M2::Value must be convertible to \c M1::Key.
484 483
  ///
485 484
  /// The simplest way of using this map is through the composeMap()
486 485
  /// function.
487 486
  ///
488 487
  /// \sa CombineMap
489 488
  template <typename M1, typename M2>
490 489
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
491 490
    const M1 &_m1;
492 491
    const M2 &_m2;
493 492
  public:
494 493
    ///\e
495 494
    typedef typename M2::Key Key;
496 495
    ///\e
497 496
    typedef typename M1::Value Value;
498 497

	
499 498
    /// Constructor
500 499
    ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
501 500

	
502 501
    ///\e
503 502
    typename MapTraits<M1>::ConstReturnValue
504 503
    operator[](const Key &k) const { return _m1[_m2[k]]; }
505 504
  };
506 505

	
507 506
  /// Returns a \c ComposeMap class
508 507

	
509 508
  /// This function just returns a \c ComposeMap class.
510 509
  ///
511 510
  /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
512 511
  /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
513 512
  /// will be equal to <tt>m1[m2[x]]</tt>.
514 513
  ///
515 514
  /// \relates ComposeMap
516 515
  template <typename M1, typename M2>
517 516
  inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
518 517
    return ComposeMap<M1, M2>(m1, m2);
519 518
  }
520 519

	
521 520

	
522 521
  /// Combination of two maps using an STL (binary) functor.
523 522

	
524 523
  /// This \ref concepts::ReadMap "read-only map" takes two maps and a
525 524
  /// binary functor and returns the combination of the two given maps
526 525
  /// using the functor.
527 526
  /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
528 527
  /// and \c f is of \c F, then for
529 528
  /// \code
530 529
  ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
531 530
  /// \endcode
532 531
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
533 532
  ///
534 533
  /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
535 534
  /// must be convertible to \c M2::Key) and the \c Value type is \c V.
536 535
  /// \c M2::Value and \c M1::Value must be convertible to the
537 536
  /// corresponding input parameter of \c F and the return type of \c F
538 537
  /// must be convertible to \c V.
539 538
  ///
540 539
  /// The simplest way of using this map is through the combineMap()
541 540
  /// function.
542 541
  ///
543 542
  /// \sa ComposeMap
544 543
  template<typename M1, typename M2, typename F,
545 544
           typename V = typename F::result_type>
546 545
  class CombineMap : public MapBase<typename M1::Key, V> {
547 546
    const M1 &_m1;
548 547
    const M2 &_m2;
549 548
    F _f;
550 549
  public:
551 550
    ///\e
552 551
    typedef typename M1::Key Key;
553 552
    ///\e
554 553
    typedef V Value;
555 554

	
556 555
    /// Constructor
557 556
    CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
558 557
      : _m1(m1), _m2(m2), _f(f) {}
559 558
    ///\e
560 559
    Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
561 560
  };
562 561

	
563 562
  /// Returns a \c CombineMap class
564 563

	
565 564
  /// This function just returns a \c CombineMap class.
566 565
  ///
567 566
  /// For example, if \c m1 and \c m2 are both maps with \c double
568 567
  /// values, then
569 568
  /// \code
570 569
  ///   combineMap(m1,m2,std::plus<double>())
571 570
  /// \endcode
572 571
  /// is equivalent to
573 572
  /// \code
574 573
  ///   addMap(m1,m2)
575 574
  /// \endcode
576 575
  ///
577 576
  /// This function is specialized for adaptable binary function
578 577
  /// classes and C++ functions.
579 578
  ///
580 579
  /// \relates CombineMap
581 580
  template<typename M1, typename M2, typename F, typename V>
582 581
  inline CombineMap<M1, M2, F, V>
583 582
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
584 583
    return CombineMap<M1, M2, F, V>(m1,m2,f);
585 584
  }
586 585

	
587 586
  template<typename M1, typename M2, typename F>
588 587
  inline CombineMap<M1, M2, F, typename F::result_type>
589 588
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
590 589
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
591 590
  }
592 591

	
593 592
  template<typename M1, typename M2, typename K1, typename K2, typename V>
594 593
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
595 594
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
596 595
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
597 596
  }
598 597

	
599 598

	
600 599
  /// Converts an STL style (unary) functor to a map
601 600

	
602 601
  /// This \ref concepts::ReadMap "read-only map" returns the value
603 602
  /// of a given functor. Actually, it just wraps the functor and
604 603
  /// provides the \c Key and \c Value typedefs.
605 604
  ///
606 605
  /// Template parameters \c K and \c V will become its \c Key and
607 606
  /// \c Value. In most cases they have to be given explicitly because
608 607
  /// a functor typically does not provide \c argument_type and
609 608
  /// \c result_type typedefs.
610 609
  /// Parameter \c F is the type of the used functor.
611 610
  ///
612 611
  /// The simplest way of using this map is through the functorToMap()
613 612
  /// function.
614 613
  ///
615 614
  /// \sa MapToFunctor
616 615
  template<typename F,
617 616
           typename K = typename F::argument_type,
618 617
           typename V = typename F::result_type>
619 618
  class FunctorToMap : public MapBase<K, V> {
620 619
    F _f;
621 620
  public:
622 621
    ///\e
623 622
    typedef K Key;
624 623
    ///\e
625 624
    typedef V Value;
626 625

	
627 626
    /// Constructor
628 627
    FunctorToMap(const F &f = F()) : _f(f) {}
629 628
    ///\e
630 629
    Value operator[](const Key &k) const { return _f(k); }
631 630
  };
632 631

	
633 632
  /// Returns a \c FunctorToMap class
634 633

	
635 634
  /// This function just returns a \c FunctorToMap class.
636 635
  ///
637 636
  /// This function is specialized for adaptable binary function
638 637
  /// classes and C++ functions.
639 638
  ///
640 639
  /// \relates FunctorToMap
641 640
  template<typename K, typename V, typename F>
642 641
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
643 642
    return FunctorToMap<F, K, V>(f);
644 643
  }
645 644

	
646 645
  template <typename F>
647 646
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
648 647
    functorToMap(const F &f)
649 648
  {
650 649
    return FunctorToMap<F, typename F::argument_type,
651 650
      typename F::result_type>(f);
652 651
  }
653 652

	
654 653
  template <typename K, typename V>
655 654
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
656 655
    return FunctorToMap<V (*)(K), K, V>(f);
657 656
  }
658 657

	
659 658

	
660 659
  /// Converts a map to an STL style (unary) functor
661 660

	
662 661
  /// This class converts a map to an STL style (unary) functor.
663 662
  /// That is it provides an <tt>operator()</tt> to read its values.
664 663
  ///
665 664
  /// For the sake of convenience it also works as a usual
666 665
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
667 666
  /// and the \c Key and \c Value typedefs also exist.
668 667
  ///
669 668
  /// The simplest way of using this map is through the mapToFunctor()
670 669
  /// function.
671 670
  ///
672 671
  ///\sa FunctorToMap
673 672
  template <typename M>
674 673
  class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
675 674
    const M &_m;
676 675
  public:
677 676
    ///\e
678 677
    typedef typename M::Key Key;
679 678
    ///\e
680 679
    typedef typename M::Value Value;
681 680

	
682 681
    typedef typename M::Key argument_type;
683 682
    typedef typename M::Value result_type;
684 683

	
685 684
    /// Constructor
686 685
    MapToFunctor(const M &m) : _m(m) {}
687 686
    ///\e
688 687
    Value operator()(const Key &k) const { return _m[k]; }
689 688
    ///\e
690 689
    Value operator[](const Key &k) const { return _m[k]; }
691 690
  };
692 691

	
693 692
  /// Returns a \c MapToFunctor class
694 693

	
695 694
  /// This function just returns a \c MapToFunctor class.
696 695
  /// \relates MapToFunctor
697 696
  template<typename M>
698 697
  inline MapToFunctor<M> mapToFunctor(const M &m) {
699 698
    return MapToFunctor<M>(m);
700 699
  }
701 700

	
702 701

	
703 702
  /// \brief Map adaptor to convert the \c Value type of a map to
704 703
  /// another type using the default conversion.
705 704

	
706 705
  /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
707 706
  /// "readable map" to another type using the default conversion.
708 707
  /// The \c Key type of it is inherited from \c M and the \c Value
709 708
  /// type is \c V.
710 709
  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
711 710
  ///
712 711
  /// The simplest way of using this map is through the convertMap()
713 712
  /// function.
714 713
  template <typename M, typename V>
715 714
  class ConvertMap : public MapBase<typename M::Key, V> {
716 715
    const M &_m;
717 716
  public:
718 717
    ///\e
719 718
    typedef typename M::Key Key;
720 719
    ///\e
721 720
    typedef V Value;
722 721

	
723 722
    /// Constructor
724 723

	
725 724
    /// Constructor.
726 725
    /// \param m The underlying map.
727 726
    ConvertMap(const M &m) : _m(m) {}
728 727

	
729 728
    ///\e
730 729
    Value operator[](const Key &k) const { return _m[k]; }
731 730
  };
732 731

	
733 732
  /// Returns a \c ConvertMap class
734 733

	
735 734
  /// This function just returns a \c ConvertMap class.
736 735
  /// \relates ConvertMap
737 736
  template<typename V, typename M>
738 737
  inline ConvertMap<M, V> convertMap(const M &map) {
739 738
    return ConvertMap<M, V>(map);
740 739
  }
741 740

	
742 741

	
743 742
  /// Applies all map setting operations to two maps
744 743

	
745 744
  /// This map has two \ref concepts::WriteMap "writable map" parameters
746 745
  /// and each write request will be passed to both of them.
747 746
  /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
748 747
  /// operations will return the corresponding values of \c M1.
749 748
  ///
750 749
  /// The \c Key and \c Value types are inherited from \c M1.
751 750
  /// The \c Key and \c Value of \c M2 must be convertible from those
752 751
  /// of \c M1.
753 752
  ///
754 753
  /// The simplest way of using this map is through the forkMap()
755 754
  /// function.
756 755
  template<typename  M1, typename M2>
757 756
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
758 757
    M1 &_m1;
759 758
    M2 &_m2;
760 759
  public:
761 760
    ///\e
762 761
    typedef typename M1::Key Key;
763 762
    ///\e
764 763
    typedef typename M1::Value Value;
765 764

	
766 765
    /// Constructor
767 766
    ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
768 767
    /// Returns the value associated with the given key in the first map.
769 768
    Value operator[](const Key &k) const { return _m1[k]; }
770 769
    /// Sets the value associated with the given key in both maps.
771 770
    void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
772 771
  };
773 772

	
774 773
  /// Returns a \c ForkMap class
775 774

	
776 775
  /// This function just returns a \c ForkMap class.
777 776
  /// \relates ForkMap
778 777
  template <typename M1, typename M2>
779 778
  inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
780 779
    return ForkMap<M1,M2>(m1,m2);
781 780
  }
782 781

	
783 782

	
784 783
  /// Sum of two maps
785 784

	
786 785
  /// This \ref concepts::ReadMap "read-only map" returns the sum
787 786
  /// of the values of the two given maps.
788 787
  /// Its \c Key and \c Value types are inherited from \c M1.
789 788
  /// The \c Key and \c Value of \c M2 must be convertible to those of
790 789
  /// \c M1.
791 790
  ///
792 791
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
793 792
  /// \code
794 793
  ///   AddMap<M1,M2> am(m1,m2);
795 794
  /// \endcode
796 795
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
797 796
  ///
798 797
  /// The simplest way of using this map is through the addMap()
799 798
  /// function.
800 799
  ///
801 800
  /// \sa SubMap, MulMap, DivMap
... ...
@@ -1053,1762 +1052,2659 @@
1053 1052
  };
1054 1053

	
1055 1054
  /// Returns a \c ShiftMap class
1056 1055

	
1057 1056
  /// This function just returns a \c ShiftMap class.
1058 1057
  ///
1059 1058
  /// For example, if \c m is a map with \c double values and \c v is
1060 1059
  /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1061 1060
  /// <tt>m[x]+v</tt>.
1062 1061
  ///
1063 1062
  /// \relates ShiftMap
1064 1063
  template<typename M, typename C>
1065 1064
  inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1066 1065
    return ShiftMap<M, C>(m,v);
1067 1066
  }
1068 1067

	
1069 1068
  /// Returns a \c ShiftWriteMap class
1070 1069

	
1071 1070
  /// This function just returns a \c ShiftWriteMap class.
1072 1071
  ///
1073 1072
  /// For example, if \c m is a map with \c double values and \c v is
1074 1073
  /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1075 1074
  /// <tt>m[x]+v</tt>.
1076 1075
  /// Moreover it makes also possible to write the map.
1077 1076
  ///
1078 1077
  /// \relates ShiftWriteMap
1079 1078
  template<typename M, typename C>
1080 1079
  inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1081 1080
    return ShiftWriteMap<M, C>(m,v);
1082 1081
  }
1083 1082

	
1084 1083

	
1085 1084
  /// Scales a map with a constant.
1086 1085

	
1087 1086
  /// This \ref concepts::ReadMap "read-only map" returns the value of
1088 1087
  /// the given map multiplied from the left side with a constant value.
1089 1088
  /// Its \c Key and \c Value are inherited from \c M.
1090 1089
  ///
1091 1090
  /// Actually,
1092 1091
  /// \code
1093 1092
  ///   ScaleMap<M> sc(m,v);
1094 1093
  /// \endcode
1095 1094
  /// is equivalent to
1096 1095
  /// \code
1097 1096
  ///   ConstMap<M::Key, M::Value> cm(v);
1098 1097
  ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1099 1098
  /// \endcode
1100 1099
  ///
1101 1100
  /// The simplest way of using this map is through the scaleMap()
1102 1101
  /// function.
1103 1102
  ///
1104 1103
  /// \sa ScaleWriteMap
1105 1104
  template<typename M, typename C = typename M::Value>
1106 1105
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1107 1106
    const M &_m;
1108 1107
    C _v;
1109 1108
  public:
1110 1109
    ///\e
1111 1110
    typedef typename M::Key Key;
1112 1111
    ///\e
1113 1112
    typedef typename M::Value Value;
1114 1113

	
1115 1114
    /// Constructor
1116 1115

	
1117 1116
    /// Constructor.
1118 1117
    /// \param m The undelying map.
1119 1118
    /// \param v The constant value.
1120 1119
    ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1121 1120
    ///\e
1122 1121
    Value operator[](const Key &k) const { return _v*_m[k]; }
1123 1122
  };
1124 1123

	
1125 1124
  /// Scales a map with a constant (read-write version).
1126 1125

	
1127 1126
  /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1128 1127
  /// the given map multiplied from the left side with a constant value.
1129 1128
  /// Its \c Key and \c Value are inherited from \c M.
1130 1129
  /// It can also be used as write map if the \c / operator is defined
1131 1130
  /// between \c Value and \c C and the given multiplier is not zero.
1132 1131
  ///
1133 1132
  /// The simplest way of using this map is through the scaleWriteMap()
1134 1133
  /// function.
1135 1134
  ///
1136 1135
  /// \sa ScaleMap
1137 1136
  template<typename M, typename C = typename M::Value>
1138 1137
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1139 1138
    M &_m;
1140 1139
    C _v;
1141 1140
  public:
1142 1141
    ///\e
1143 1142
    typedef typename M::Key Key;
1144 1143
    ///\e
1145 1144
    typedef typename M::Value Value;
1146 1145

	
1147 1146
    /// Constructor
1148 1147

	
1149 1148
    /// Constructor.
1150 1149
    /// \param m The undelying map.
1151 1150
    /// \param v The constant value.
1152 1151
    ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1153 1152
    ///\e
1154 1153
    Value operator[](const Key &k) const { return _v*_m[k]; }
1155 1154
    ///\e
1156 1155
    void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1157 1156
  };
1158 1157

	
1159 1158
  /// Returns a \c ScaleMap class
1160 1159

	
1161 1160
  /// This function just returns a \c ScaleMap class.
1162 1161
  ///
1163 1162
  /// For example, if \c m is a map with \c double values and \c v is
1164 1163
  /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1165 1164
  /// <tt>v*m[x]</tt>.
1166 1165
  ///
1167 1166
  /// \relates ScaleMap
1168 1167
  template<typename M, typename C>
1169 1168
  inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1170 1169
    return ScaleMap<M, C>(m,v);
1171 1170
  }
1172 1171

	
1173 1172
  /// Returns a \c ScaleWriteMap class
1174 1173

	
1175 1174
  /// This function just returns a \c ScaleWriteMap class.
1176 1175
  ///
1177 1176
  /// For example, if \c m is a map with \c double values and \c v is
1178 1177
  /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1179 1178
  /// <tt>v*m[x]</tt>.
1180 1179
  /// Moreover it makes also possible to write the map.
1181 1180
  ///
1182 1181
  /// \relates ScaleWriteMap
1183 1182
  template<typename M, typename C>
1184 1183
  inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1185 1184
    return ScaleWriteMap<M, C>(m,v);
1186 1185
  }
1187 1186

	
1188 1187

	
1189 1188
  /// Negative of a map
1190 1189

	
1191 1190
  /// This \ref concepts::ReadMap "read-only map" returns the negative
1192 1191
  /// of the values of the given map (using the unary \c - operator).
1193 1192
  /// Its \c Key and \c Value are inherited from \c M.
1194 1193
  ///
1195 1194
  /// If M::Value is \c int, \c double etc., then
1196 1195
  /// \code
1197 1196
  ///   NegMap<M> neg(m);
1198 1197
  /// \endcode
1199 1198
  /// is equivalent to
1200 1199
  /// \code
1201 1200
  ///   ScaleMap<M> neg(m,-1);
1202 1201
  /// \endcode
1203 1202
  ///
1204 1203
  /// The simplest way of using this map is through the negMap()
1205 1204
  /// function.
1206 1205
  ///
1207 1206
  /// \sa NegWriteMap
1208 1207
  template<typename M>
1209 1208
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
1210 1209
    const M& _m;
1211 1210
  public:
1212 1211
    ///\e
1213 1212
    typedef typename M::Key Key;
1214 1213
    ///\e
1215 1214
    typedef typename M::Value Value;
1216 1215

	
1217 1216
    /// Constructor
1218 1217
    NegMap(const M &m) : _m(m) {}
1219 1218
    ///\e
1220 1219
    Value operator[](const Key &k) const { return -_m[k]; }
1221 1220
  };
1222 1221

	
1223 1222
  /// Negative of a map (read-write version)
1224 1223

	
1225 1224
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1226 1225
  /// negative of the values of the given map (using the unary \c -
1227 1226
  /// operator).
1228 1227
  /// Its \c Key and \c Value are inherited from \c M.
1229 1228
  /// It makes also possible to write the map.
1230 1229
  ///
1231 1230
  /// If M::Value is \c int, \c double etc., then
1232 1231
  /// \code
1233 1232
  ///   NegWriteMap<M> neg(m);
1234 1233
  /// \endcode
1235 1234
  /// is equivalent to
1236 1235
  /// \code
1237 1236
  ///   ScaleWriteMap<M> neg(m,-1);
1238 1237
  /// \endcode
1239 1238
  ///
1240 1239
  /// The simplest way of using this map is through the negWriteMap()
1241 1240
  /// function.
1242 1241
  ///
1243 1242
  /// \sa NegMap
1244 1243
  template<typename M>
1245 1244
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1246 1245
    M &_m;
1247 1246
  public:
1248 1247
    ///\e
1249 1248
    typedef typename M::Key Key;
1250 1249
    ///\e
1251 1250
    typedef typename M::Value Value;
1252 1251

	
1253 1252
    /// Constructor
1254 1253
    NegWriteMap(M &m) : _m(m) {}
1255 1254
    ///\e
1256 1255
    Value operator[](const Key &k) const { return -_m[k]; }
1257 1256
    ///\e
1258 1257
    void set(const Key &k, const Value &v) { _m.set(k, -v); }
1259 1258
  };
1260 1259

	
1261 1260
  /// Returns a \c NegMap class
1262 1261

	
1263 1262
  /// This function just returns a \c NegMap class.
1264 1263
  ///
1265 1264
  /// For example, if \c m is a map with \c double values, then
1266 1265
  /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1267 1266
  ///
1268 1267
  /// \relates NegMap
1269 1268
  template <typename M>
1270 1269
  inline NegMap<M> negMap(const M &m) {
1271 1270
    return NegMap<M>(m);
1272 1271
  }
1273 1272

	
1274 1273
  /// Returns a \c NegWriteMap class
1275 1274

	
1276 1275
  /// This function just returns a \c NegWriteMap class.
1277 1276
  ///
1278 1277
  /// For example, if \c m is a map with \c double values, then
1279 1278
  /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1280 1279
  /// Moreover it makes also possible to write the map.
1281 1280
  ///
1282 1281
  /// \relates NegWriteMap
1283 1282
  template <typename M>
1284 1283
  inline NegWriteMap<M> negWriteMap(M &m) {
1285 1284
    return NegWriteMap<M>(m);
1286 1285
  }
1287 1286

	
1288 1287

	
1289 1288
  /// Absolute value of a map
1290 1289

	
1291 1290
  /// This \ref concepts::ReadMap "read-only map" returns the absolute
1292 1291
  /// value of the values of the given map.
1293 1292
  /// Its \c Key and \c Value are inherited from \c M.
1294 1293
  /// \c Value must be comparable to \c 0 and the unary \c -
1295 1294
  /// operator must be defined for it, of course.
1296 1295
  ///
1297 1296
  /// The simplest way of using this map is through the absMap()
1298 1297
  /// function.
1299 1298
  template<typename M>
1300 1299
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1301 1300
    const M &_m;
1302 1301
  public:
1303 1302
    ///\e
1304 1303
    typedef typename M::Key Key;
1305 1304
    ///\e
1306 1305
    typedef typename M::Value Value;
1307 1306

	
1308 1307
    /// Constructor
1309 1308
    AbsMap(const M &m) : _m(m) {}
1310 1309
    ///\e
1311 1310
    Value operator[](const Key &k) const {
1312 1311
      Value tmp = _m[k];
1313 1312
      return tmp >= 0 ? tmp : -tmp;
1314 1313
    }
1315 1314

	
1316 1315
  };
1317 1316

	
1318 1317
  /// Returns an \c AbsMap class
1319 1318

	
1320 1319
  /// This function just returns an \c AbsMap class.
1321 1320
  ///
1322 1321
  /// For example, if \c m is a map with \c double values, then
1323 1322
  /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1324 1323
  /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1325 1324
  /// negative.
1326 1325
  ///
1327 1326
  /// \relates AbsMap
1328 1327
  template<typename M>
1329 1328
  inline AbsMap<M> absMap(const M &m) {
1330 1329
    return AbsMap<M>(m);
1331 1330
  }
1332 1331

	
1333 1332
  /// @}
1334 1333

	
1335 1334
  // Logical maps and map adaptors:
1336 1335

	
1337 1336
  /// \addtogroup maps
1338 1337
  /// @{
1339 1338

	
1340 1339
  /// Constant \c true map.
1341 1340

	
1342 1341
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1343 1342
  /// each key.
1344 1343
  ///
1345 1344
  /// Note that
1346 1345
  /// \code
1347 1346
  ///   TrueMap<K> tm;
1348 1347
  /// \endcode
1349 1348
  /// is equivalent to
1350 1349
  /// \code
1351 1350
  ///   ConstMap<K,bool> tm(true);
1352 1351
  /// \endcode
1353 1352
  ///
1354 1353
  /// \sa FalseMap
1355 1354
  /// \sa ConstMap
1356 1355
  template <typename K>
1357 1356
  class TrueMap : public MapBase<K, bool> {
1358 1357
  public:
1359 1358
    ///\e
1360 1359
    typedef K Key;
1361 1360
    ///\e
1362 1361
    typedef bool Value;
1363 1362

	
1364 1363
    /// Gives back \c true.
1365 1364
    Value operator[](const Key&) const { return true; }
1366 1365
  };
1367 1366

	
1368 1367
  /// Returns a \c TrueMap class
1369 1368

	
1370 1369
  /// This function just returns a \c TrueMap class.
1371 1370
  /// \relates TrueMap
1372 1371
  template<typename K>
1373 1372
  inline TrueMap<K> trueMap() {
1374 1373
    return TrueMap<K>();
1375 1374
  }
1376 1375

	
1377 1376

	
1378 1377
  /// Constant \c false map.
1379 1378

	
1380 1379
  /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1381 1380
  /// each key.
1382 1381
  ///
1383 1382
  /// Note that
1384 1383
  /// \code
1385 1384
  ///   FalseMap<K> fm;
1386 1385
  /// \endcode
1387 1386
  /// is equivalent to
1388 1387
  /// \code
1389 1388
  ///   ConstMap<K,bool> fm(false);
1390 1389
  /// \endcode
1391 1390
  ///
1392 1391
  /// \sa TrueMap
1393 1392
  /// \sa ConstMap
1394 1393
  template <typename K>
1395 1394
  class FalseMap : public MapBase<K, bool> {
1396 1395
  public:
1397 1396
    ///\e
1398 1397
    typedef K Key;
1399 1398
    ///\e
1400 1399
    typedef bool Value;
1401 1400

	
1402 1401
    /// Gives back \c false.
1403 1402
    Value operator[](const Key&) const { return false; }
1404 1403
  };
1405 1404

	
1406 1405
  /// Returns a \c FalseMap class
1407 1406

	
1408 1407
  /// This function just returns a \c FalseMap class.
1409 1408
  /// \relates FalseMap
1410 1409
  template<typename K>
1411 1410
  inline FalseMap<K> falseMap() {
1412 1411
    return FalseMap<K>();
1413 1412
  }
1414 1413

	
1415 1414
  /// @}
1416 1415

	
1417 1416
  /// \addtogroup map_adaptors
1418 1417
  /// @{
1419 1418

	
1420 1419
  /// Logical 'and' of two maps
1421 1420

	
1422 1421
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1423 1422
  /// 'and' of the values of the two given maps.
1424 1423
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1425 1424
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1426 1425
  ///
1427 1426
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1428 1427
  /// \code
1429 1428
  ///   AndMap<M1,M2> am(m1,m2);
1430 1429
  /// \endcode
1431 1430
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1432 1431
  ///
1433 1432
  /// The simplest way of using this map is through the andMap()
1434 1433
  /// function.
1435 1434
  ///
1436 1435
  /// \sa OrMap
1437 1436
  /// \sa NotMap, NotWriteMap
1438 1437
  template<typename M1, typename M2>
1439 1438
  class AndMap : public MapBase<typename M1::Key, bool> {
1440 1439
    const M1 &_m1;
1441 1440
    const M2 &_m2;
1442 1441
  public:
1443 1442
    ///\e
1444 1443
    typedef typename M1::Key Key;
1445 1444
    ///\e
1446 1445
    typedef bool Value;
1447 1446

	
1448 1447
    /// Constructor
1449 1448
    AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1450 1449
    ///\e
1451 1450
    Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1452 1451
  };
1453 1452

	
1454 1453
  /// Returns an \c AndMap class
1455 1454

	
1456 1455
  /// This function just returns an \c AndMap class.
1457 1456
  ///
1458 1457
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1459 1458
  /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1460 1459
  /// <tt>m1[x]&&m2[x]</tt>.
1461 1460
  ///
1462 1461
  /// \relates AndMap
1463 1462
  template<typename M1, typename M2>
1464 1463
  inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1465 1464
    return AndMap<M1, M2>(m1,m2);
1466 1465
  }
1467 1466

	
1468 1467

	
1469 1468
  /// Logical 'or' of two maps
1470 1469

	
1471 1470
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1472 1471
  /// 'or' of the values of the two given maps.
1473 1472
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1474 1473
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1475 1474
  ///
1476 1475
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1477 1476
  /// \code
1478 1477
  ///   OrMap<M1,M2> om(m1,m2);
1479 1478
  /// \endcode
1480 1479
  /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1481 1480
  ///
1482 1481
  /// The simplest way of using this map is through the orMap()
1483 1482
  /// function.
1484 1483
  ///
1485 1484
  /// \sa AndMap
1486 1485
  /// \sa NotMap, NotWriteMap
1487 1486
  template<typename M1, typename M2>
1488 1487
  class OrMap : public MapBase<typename M1::Key, bool> {
1489 1488
    const M1 &_m1;
1490 1489
    const M2 &_m2;
1491 1490
  public:
1492 1491
    ///\e
1493 1492
    typedef typename M1::Key Key;
1494 1493
    ///\e
1495 1494
    typedef bool Value;
1496 1495

	
1497 1496
    /// Constructor
1498 1497
    OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1499 1498
    ///\e
1500 1499
    Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1501 1500
  };
1502 1501

	
1503 1502
  /// Returns an \c OrMap class
1504 1503

	
1505 1504
  /// This function just returns an \c OrMap class.
1506 1505
  ///
1507 1506
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1508 1507
  /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1509 1508
  /// <tt>m1[x]||m2[x]</tt>.
1510 1509
  ///
1511 1510
  /// \relates OrMap
1512 1511
  template<typename M1, typename M2>
1513 1512
  inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1514 1513
    return OrMap<M1, M2>(m1,m2);
1515 1514
  }
1516 1515

	
1517 1516

	
1518 1517
  /// Logical 'not' of a map
1519 1518

	
1520 1519
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1521 1520
  /// negation of the values of the given map.
1522 1521
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1523 1522
  ///
1524 1523
  /// The simplest way of using this map is through the notMap()
1525 1524
  /// function.
1526 1525
  ///
1527 1526
  /// \sa NotWriteMap
1528 1527
  template <typename M>
1529 1528
  class NotMap : public MapBase<typename M::Key, bool> {
1530 1529
    const M &_m;
1531 1530
  public:
1532 1531
    ///\e
1533 1532
    typedef typename M::Key Key;
1534 1533
    ///\e
1535 1534
    typedef bool Value;
1536 1535

	
1537 1536
    /// Constructor
1538 1537
    NotMap(const M &m) : _m(m) {}
1539 1538
    ///\e
1540 1539
    Value operator[](const Key &k) const { return !_m[k]; }
1541 1540
  };
1542 1541

	
1543 1542
  /// Logical 'not' of a map (read-write version)
1544 1543

	
1545 1544
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1546 1545
  /// logical negation of the values of the given map.
1547 1546
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1548 1547
  /// It makes also possible to write the map. When a value is set,
1549 1548
  /// the opposite value is set to the original map.
1550 1549
  ///
1551 1550
  /// The simplest way of using this map is through the notWriteMap()
1552 1551
  /// function.
1553 1552
  ///
1554 1553
  /// \sa NotMap
1555 1554
  template <typename M>
1556 1555
  class NotWriteMap : public MapBase<typename M::Key, bool> {
1557 1556
    M &_m;
1558 1557
  public:
1559 1558
    ///\e
1560 1559
    typedef typename M::Key Key;
1561 1560
    ///\e
1562 1561
    typedef bool Value;
1563 1562

	
1564 1563
    /// Constructor
1565 1564
    NotWriteMap(M &m) : _m(m) {}
1566 1565
    ///\e
1567 1566
    Value operator[](const Key &k) const { return !_m[k]; }
1568 1567
    ///\e
1569 1568
    void set(const Key &k, bool v) { _m.set(k, !v); }
1570 1569
  };
1571 1570

	
1572 1571
  /// Returns a \c NotMap class
1573 1572

	
1574 1573
  /// This function just returns a \c NotMap class.
1575 1574
  ///
1576 1575
  /// For example, if \c m is a map with \c bool values, then
1577 1576
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1578 1577
  ///
1579 1578
  /// \relates NotMap
1580 1579
  template <typename M>
1581 1580
  inline NotMap<M> notMap(const M &m) {
1582 1581
    return NotMap<M>(m);
1583 1582
  }
1584 1583

	
1585 1584
  /// Returns a \c NotWriteMap class
1586 1585

	
1587 1586
  /// This function just returns a \c NotWriteMap class.
1588 1587
  ///
1589 1588
  /// For example, if \c m is a map with \c bool values, then
1590 1589
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1591 1590
  /// Moreover it makes also possible to write the map.
1592 1591
  ///
1593 1592
  /// \relates NotWriteMap
1594 1593
  template <typename M>
1595 1594
  inline NotWriteMap<M> notWriteMap(M &m) {
1596 1595
    return NotWriteMap<M>(m);
1597 1596
  }
1598 1597

	
1599 1598

	
1600 1599
  /// Combination of two maps using the \c == operator
1601 1600

	
1602 1601
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1603 1602
  /// the keys for which the corresponding values of the two maps are
1604 1603
  /// equal.
1605 1604
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1606 1605
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1607 1606
  ///
1608 1607
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1609 1608
  /// \code
1610 1609
  ///   EqualMap<M1,M2> em(m1,m2);
1611 1610
  /// \endcode
1612 1611
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1613 1612
  ///
1614 1613
  /// The simplest way of using this map is through the equalMap()
1615 1614
  /// function.
1616 1615
  ///
1617 1616
  /// \sa LessMap
1618 1617
  template<typename M1, typename M2>
1619 1618
  class EqualMap : public MapBase<typename M1::Key, bool> {
1620 1619
    const M1 &_m1;
1621 1620
    const M2 &_m2;
1622 1621
  public:
1623 1622
    ///\e
1624 1623
    typedef typename M1::Key Key;
1625 1624
    ///\e
1626 1625
    typedef bool Value;
1627 1626

	
1628 1627
    /// Constructor
1629 1628
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1630 1629
    ///\e
1631 1630
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1632 1631
  };
1633 1632

	
1634 1633
  /// Returns an \c EqualMap class
1635 1634

	
1636 1635
  /// This function just returns an \c EqualMap class.
1637 1636
  ///
1638 1637
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1639 1638
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1640 1639
  /// <tt>m1[x]==m2[x]</tt>.
1641 1640
  ///
1642 1641
  /// \relates EqualMap
1643 1642
  template<typename M1, typename M2>
1644 1643
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1645 1644
    return EqualMap<M1, M2>(m1,m2);
1646 1645
  }
1647 1646

	
1648 1647

	
1649 1648
  /// Combination of two maps using the \c < operator
1650 1649

	
1651 1650
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1652 1651
  /// the keys for which the corresponding value of the first map is
1653 1652
  /// less then the value of the second map.
1654 1653
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1655 1654
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1656 1655
  ///
1657 1656
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1658 1657
  /// \code
1659 1658
  ///   LessMap<M1,M2> lm(m1,m2);
1660 1659
  /// \endcode
1661 1660
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1662 1661
  ///
1663 1662
  /// The simplest way of using this map is through the lessMap()
1664 1663
  /// function.
1665 1664
  ///
1666 1665
  /// \sa EqualMap
1667 1666
  template<typename M1, typename M2>
1668 1667
  class LessMap : public MapBase<typename M1::Key, bool> {
1669 1668
    const M1 &_m1;
1670 1669
    const M2 &_m2;
1671 1670
  public:
1672 1671
    ///\e
1673 1672
    typedef typename M1::Key Key;
1674 1673
    ///\e
1675 1674
    typedef bool Value;
1676 1675

	
1677 1676
    /// Constructor
1678 1677
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1679 1678
    ///\e
1680 1679
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1681 1680
  };
1682 1681

	
1683 1682
  /// Returns an \c LessMap class
1684 1683

	
1685 1684
  /// This function just returns an \c LessMap class.
1686 1685
  ///
1687 1686
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1688 1687
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1689 1688
  /// <tt>m1[x]<m2[x]</tt>.
1690 1689
  ///
1691 1690
  /// \relates LessMap
1692 1691
  template<typename M1, typename M2>
1693 1692
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1694 1693
    return LessMap<M1, M2>(m1,m2);
1695 1694
  }
1696 1695

	
1697 1696
  namespace _maps_bits {
1698 1697

	
1699 1698
    template <typename _Iterator, typename Enable = void>
1700 1699
    struct IteratorTraits {
1701 1700
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
1702 1701
    };
1703 1702

	
1704 1703
    template <typename _Iterator>
1705 1704
    struct IteratorTraits<_Iterator,
1706 1705
      typename exists<typename _Iterator::container_type>::type>
1707 1706
    {
1708 1707
      typedef typename _Iterator::container_type::value_type Value;
1709 1708
    };
1710 1709

	
1711 1710
  }
1712 1711

	
1713 1712
  /// @}
1714 1713

	
1715 1714
  /// \addtogroup maps
1716 1715
  /// @{
1717 1716

	
1718 1717
  /// \brief Writable bool map for logging each \c true assigned element
1719 1718
  ///
1720 1719
  /// A \ref concepts::WriteMap "writable" bool map for logging
1721 1720
  /// each \c true assigned element, i.e it copies subsequently each
1722 1721
  /// keys set to \c true to the given iterator.
1723 1722
  /// The most important usage of it is storing certain nodes or arcs
1724 1723
  /// that were marked \c true by an algorithm.
1725 1724
  ///
1726 1725
  /// There are several algorithms that provide solutions through bool
1727 1726
  /// maps and most of them assign \c true at most once for each key.
1728 1727
  /// In these cases it is a natural request to store each \c true
1729 1728
  /// assigned elements (in order of the assignment), which can be
1730 1729
  /// easily done with LoggerBoolMap.
1731 1730
  ///
1732 1731
  /// The simplest way of using this map is through the loggerBoolMap()
1733 1732
  /// function.
1734 1733
  ///
1735 1734
  /// \tparam IT The type of the iterator.
1736 1735
  /// \tparam KEY The key type of the map. The default value set
1737 1736
  /// according to the iterator type should work in most cases.
1738 1737
  ///
1739 1738
  /// \note The container of the iterator must contain enough space
1740 1739
  /// for the elements or the iterator should be an inserter iterator.
1741 1740
#ifdef DOXYGEN
1742 1741
  template <typename IT, typename KEY>
1743 1742
#else
1744 1743
  template <typename IT,
1745 1744
            typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
1746 1745
#endif
1747 1746
  class LoggerBoolMap : public MapBase<KEY, bool> {
1748 1747
  public:
1749 1748

	
1750 1749
    ///\e
1751 1750
    typedef KEY Key;
1752 1751
    ///\e
1753 1752
    typedef bool Value;
1754 1753
    ///\e
1755 1754
    typedef IT Iterator;
1756 1755

	
1757 1756
    /// Constructor
1758 1757
    LoggerBoolMap(Iterator it)
1759 1758
      : _begin(it), _end(it) {}
1760 1759

	
1761 1760
    /// Gives back the given iterator set for the first key
1762 1761
    Iterator begin() const {
1763 1762
      return _begin;
1764 1763
    }
1765 1764

	
1766 1765
    /// Gives back the the 'after the last' iterator
1767 1766
    Iterator end() const {
1768 1767
      return _end;
1769 1768
    }
1770 1769

	
1771 1770
    /// The set function of the map
1772 1771
    void set(const Key& key, Value value) {
1773 1772
      if (value) {
1774 1773
        *_end++ = key;
1775 1774
      }
1776 1775
    }
1777 1776

	
1778 1777
  private:
1779 1778
    Iterator _begin;
1780 1779
    Iterator _end;
1781 1780
  };
1782 1781

	
1783 1782
  /// Returns a \c LoggerBoolMap class
1784 1783

	
1785 1784
  /// This function just returns a \c LoggerBoolMap class.
1786 1785
  ///
1787 1786
  /// The most important usage of it is storing certain nodes or arcs
1788 1787
  /// that were marked \c true by an algorithm.
1789 1788
  /// For example it makes easier to store the nodes in the processing
1790 1789
  /// order of Dfs algorithm, as the following examples show.
1791 1790
  /// \code
1792 1791
  ///   std::vector<Node> v;
1793 1792
  ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
1794 1793
  /// \endcode
1795 1794
  /// \code
1796 1795
  ///   std::vector<Node> v(countNodes(g));
1797 1796
  ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
1798 1797
  /// \endcode
1799 1798
  ///
1800 1799
  /// \note The container of the iterator must contain enough space
1801 1800
  /// for the elements or the iterator should be an inserter iterator.
1802 1801
  ///
1803 1802
  /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
1804 1803
  /// it cannot be used when a readable map is needed, for example as
1805 1804
  /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
1806 1805
  ///
1807 1806
  /// \relates LoggerBoolMap
1808 1807
  template<typename Iterator>
1809 1808
  inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
1810 1809
    return LoggerBoolMap<Iterator>(it);
1811 1810
  }
1812 1811

	
1813 1812
  /// @}
1814 1813

	
1815 1814
  /// \addtogroup graph_maps
1816 1815
  /// @{
1817 1816

	
1818 1817
  /// \brief Provides an immutable and unique id for each item in a graph.
1819 1818
  ///
1820 1819
  /// IdMap provides a unique and immutable id for each item of the
1821
  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is 
1820
  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
1822 1821
  ///  - \b unique: different items get different ids,
1823 1822
  ///  - \b immutable: the id of an item does not change (even if you
1824 1823
  ///    delete other nodes).
1825 1824
  ///
1826 1825
  /// Using this map you get access (i.e. can read) the inner id values of
1827 1826
  /// the items stored in the graph, which is returned by the \c id()
1828 1827
  /// function of the graph. This map can be inverted with its member
1829 1828
  /// class \c InverseMap or with the \c operator()() member.
1830 1829
  ///
1831 1830
  /// \tparam GR The graph type.
1832 1831
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1833 1832
  /// \c GR::Edge).
1834 1833
  ///
1835 1834
  /// \see RangeIdMap
1836 1835
  template <typename GR, typename K>
1837 1836
  class IdMap : public MapBase<K, int> {
1838 1837
  public:
1839 1838
    /// The graph type of IdMap.
1840 1839
    typedef GR Graph;
1841 1840
    typedef GR Digraph;
1842 1841
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1843 1842
    typedef K Item;
1844 1843
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
1845 1844
    typedef K Key;
1846 1845
    /// The value type of IdMap.
1847 1846
    typedef int Value;
1848 1847

	
1849 1848
    /// \brief Constructor.
1850 1849
    ///
1851 1850
    /// Constructor of the map.
1852 1851
    explicit IdMap(const Graph& graph) : _graph(&graph) {}
1853 1852

	
1854 1853
    /// \brief Gives back the \e id of the item.
1855 1854
    ///
1856 1855
    /// Gives back the immutable and unique \e id of the item.
1857 1856
    int operator[](const Item& item) const { return _graph->id(item);}
1858 1857

	
1859 1858
    /// \brief Gives back the \e item by its id.
1860 1859
    ///
1861 1860
    /// Gives back the \e item by its id.
1862 1861
    Item operator()(int id) { return _graph->fromId(id, Item()); }
1863 1862

	
1864 1863
  private:
1865 1864
    const Graph* _graph;
1866 1865

	
1867 1866
  public:
1868 1867

	
1869 1868
    /// \brief This class represents the inverse of its owner (IdMap).
1870 1869
    ///
1871 1870
    /// This class represents the inverse of its owner (IdMap).
1872 1871
    /// \see inverse()
1873 1872
    class InverseMap {
1874 1873
    public:
1875 1874

	
1876 1875
      /// \brief Constructor.
1877 1876
      ///
1878 1877
      /// Constructor for creating an id-to-item map.
1879 1878
      explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1880 1879

	
1881 1880
      /// \brief Constructor.
1882 1881
      ///
1883 1882
      /// Constructor for creating an id-to-item map.
1884 1883
      explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1885 1884

	
1886 1885
      /// \brief Gives back the given item from its id.
1887 1886
      ///
1888 1887
      /// Gives back the given item from its id.
1889 1888
      Item operator[](int id) const { return _graph->fromId(id, Item());}
1890 1889

	
1891 1890
    private:
1892 1891
      const Graph* _graph;
1893 1892
    };
1894 1893

	
1895 1894
    /// \brief Gives back the inverse of the map.
1896 1895
    ///
1897 1896
    /// Gives back the inverse of the IdMap.
1898 1897
    InverseMap inverse() const { return InverseMap(*_graph);}
1899 1898
  };
1900 1899

	
1901 1900

	
1902 1901
  /// \brief General cross reference graph map type.
1903 1902

	
1904 1903
  /// This class provides simple invertable graph maps.
1905 1904
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1906 1905
  /// and if a key is set to a new value, then stores it in the inverse map.
1907 1906
  /// The values of the map can be accessed
1908 1907
  /// with stl compatible forward iterator.
1909 1908
  ///
1910 1909
  /// This type is not reference map, so it cannot be modified with
1911 1910
  /// the subscript operator.
1912 1911
  ///
1913 1912
  /// \tparam GR The graph type.
1914 1913
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1915 1914
  /// \c GR::Edge).
1916 1915
  /// \tparam V The value type of the map.
1917 1916
  ///
1918 1917
  /// \see IterableValueMap
1919 1918
  template <typename GR, typename K, typename V>
1920 1919
  class CrossRefMap
1921 1920
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1922 1921
  private:
1923 1922

	
1924 1923
    typedef typename ItemSetTraits<GR, K>::
1925 1924
      template Map<V>::Type Map;
1926 1925

	
1927 1926
    typedef std::multimap<V, K> Container;
1928 1927
    Container _inv_map;
1929 1928

	
1930 1929
  public:
1931 1930

	
1932 1931
    /// The graph type of CrossRefMap.
1933 1932
    typedef GR Graph;
1934 1933
    typedef GR Digraph;
1935 1934
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1936 1935
    typedef K Item;
1937 1936
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1938 1937
    typedef K Key;
1939 1938
    /// The value type of CrossRefMap.
1940 1939
    typedef V Value;
1941 1940

	
1942 1941
    /// \brief Constructor.
1943 1942
    ///
1944 1943
    /// Construct a new CrossRefMap for the given graph.
1945 1944
    explicit CrossRefMap(const Graph& graph) : Map(graph) {}
1946 1945

	
1947 1946
    /// \brief Forward iterator for values.
1948 1947
    ///
1949 1948
    /// This iterator is an stl compatible forward
1950 1949
    /// iterator on the values of the map. The values can
1951 1950
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
1952 1951
    /// They are considered with multiplicity, so each value is
1953 1952
    /// traversed for each item it is assigned to.
1954 1953
    class ValueIterator
1955 1954
      : public std::iterator<std::forward_iterator_tag, Value> {
1956 1955
      friend class CrossRefMap;
1957 1956
    private:
1958 1957
      ValueIterator(typename Container::const_iterator _it)
1959 1958
        : it(_it) {}
1960 1959
    public:
1961 1960

	
1962 1961
      ValueIterator() {}
1963 1962

	
1964 1963
      ValueIterator& operator++() { ++it; return *this; }
1965 1964
      ValueIterator operator++(int) {
1966 1965
        ValueIterator tmp(*this);
1967 1966
        operator++();
1968 1967
        return tmp;
1969 1968
      }
1970 1969

	
1971 1970
      const Value& operator*() const { return it->first; }
1972 1971
      const Value* operator->() const { return &(it->first); }
1973 1972

	
1974 1973
      bool operator==(ValueIterator jt) const { return it == jt.it; }
1975 1974
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
1976 1975

	
1977 1976
    private:
1978 1977
      typename Container::const_iterator it;
1979 1978
    };
1980 1979

	
1981 1980
    /// \brief Returns an iterator to the first value.
1982 1981
    ///
1983 1982
    /// Returns an stl compatible iterator to the
1984 1983
    /// first value of the map. The values of the
1985 1984
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1986 1985
    /// range.
1987 1986
    ValueIterator beginValue() const {
1988 1987
      return ValueIterator(_inv_map.begin());
1989 1988
    }
1990 1989

	
1991 1990
    /// \brief Returns an iterator after the last value.
1992 1991
    ///
1993 1992
    /// Returns an stl compatible iterator after the
1994 1993
    /// last value of the map. The values of the
1995 1994
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
1996 1995
    /// range.
1997 1996
    ValueIterator endValue() const {
1998 1997
      return ValueIterator(_inv_map.end());
1999 1998
    }
2000 1999

	
2001 2000
    /// \brief Sets the value associated with the given key.
2002 2001
    ///
2003 2002
    /// Sets the value associated with the given key.
2004 2003
    void set(const Key& key, const Value& val) {
2005 2004
      Value oldval = Map::operator[](key);
2006 2005
      typename Container::iterator it;
2007 2006
      for (it = _inv_map.equal_range(oldval).first;
2008 2007
           it != _inv_map.equal_range(oldval).second; ++it) {
2009 2008
        if (it->second == key) {
2010 2009
          _inv_map.erase(it);
2011 2010
          break;
2012 2011
        }
2013 2012
      }
2014 2013
      _inv_map.insert(std::make_pair(val, key));
2015 2014
      Map::set(key, val);
2016 2015
    }
2017 2016

	
2018 2017
    /// \brief Returns the value associated with the given key.
2019 2018
    ///
2020 2019
    /// Returns the value associated with the given key.
2021 2020
    typename MapTraits<Map>::ConstReturnValue
2022 2021
    operator[](const Key& key) const {
2023 2022
      return Map::operator[](key);
2024 2023
    }
2025 2024

	
2026 2025
    /// \brief Gives back an item by its value.
2027 2026
    ///
2028 2027
    /// This function gives back an item that is assigned to
2029 2028
    /// the given value or \c INVALID if no such item exists.
2030 2029
    /// If there are more items with the same associated value,
2031 2030
    /// only one of them is returned.
2032 2031
    Key operator()(const Value& val) const {
2033 2032
      typename Container::const_iterator it = _inv_map.find(val);
2034 2033
      return it != _inv_map.end() ? it->second : INVALID;
2035 2034
    }
2036 2035
    
2037 2036
    /// \brief Returns the number of items with the given value.
2038 2037
    ///
2039 2038
    /// This function returns the number of items with the given value
2040 2039
    /// associated with it.
2041 2040
    int count(const Value &val) const {
2042 2041
      return _inv_map.count(val);
2043 2042
    }
2044 2043

	
2045 2044
  protected:
2046 2045

	
2047 2046
    /// \brief Erase the key from the map and the inverse map.
2048 2047
    ///
2049 2048
    /// Erase the key from the map and the inverse map. It is called by the
2050 2049
    /// \c AlterationNotifier.
2051 2050
    virtual void erase(const Key& key) {
2052 2051
      Value val = Map::operator[](key);
2053 2052
      typename Container::iterator it;
2054 2053
      for (it = _inv_map.equal_range(val).first;
2055 2054
           it != _inv_map.equal_range(val).second; ++it) {
2056 2055
        if (it->second == key) {
2057 2056
          _inv_map.erase(it);
2058 2057
          break;
2059 2058
        }
2060 2059
      }
2061 2060
      Map::erase(key);
2062 2061
    }
2063 2062

	
2064 2063
    /// \brief Erase more keys from the map and the inverse map.
2065 2064
    ///
2066 2065
    /// Erase more keys from the map and the inverse map. It is called by the
2067 2066
    /// \c AlterationNotifier.
2068 2067
    virtual void erase(const std::vector<Key>& keys) {
2069 2068
      for (int i = 0; i < int(keys.size()); ++i) {
2070 2069
        Value val = Map::operator[](keys[i]);
2071 2070
        typename Container::iterator it;
2072 2071
        for (it = _inv_map.equal_range(val).first;
2073 2072
             it != _inv_map.equal_range(val).second; ++it) {
2074 2073
          if (it->second == keys[i]) {
2075 2074
            _inv_map.erase(it);
2076 2075
            break;
2077 2076
          }
2078 2077
        }
2079 2078
      }
2080 2079
      Map::erase(keys);
2081 2080
    }
2082 2081

	
2083 2082
    /// \brief Clear the keys from the map and the inverse map.
2084 2083
    ///
2085 2084
    /// Clear the keys from the map and the inverse map. It is called by the
2086 2085
    /// \c AlterationNotifier.
2087 2086
    virtual void clear() {
2088 2087
      _inv_map.clear();
2089 2088
      Map::clear();
2090 2089
    }
2091 2090

	
2092 2091
  public:
2093 2092

	
2094 2093
    /// \brief The inverse map type.
2095 2094
    ///
2096 2095
    /// The inverse of this map. The subscript operator of the map
2097 2096
    /// gives back the item that was last assigned to the value.
2098 2097
    class InverseMap {
2099 2098
    public:
2100 2099
      /// \brief Constructor
2101 2100
      ///
2102 2101
      /// Constructor of the InverseMap.
2103 2102
      explicit InverseMap(const CrossRefMap& inverted)
2104 2103
        : _inverted(inverted) {}
2105 2104

	
2106 2105
      /// The value type of the InverseMap.
2107 2106
      typedef typename CrossRefMap::Key Value;
2108 2107
      /// The key type of the InverseMap.
2109 2108
      typedef typename CrossRefMap::Value Key;
2110 2109

	
2111 2110
      /// \brief Subscript operator.
2112 2111
      ///
2113 2112
      /// Subscript operator. It gives back an item
2114 2113
      /// that is assigned to the given value or \c INVALID
2115 2114
      /// if no such item exists.
2116 2115
      Value operator[](const Key& key) const {
2117 2116
        return _inverted(key);
2118 2117
      }
2119 2118

	
2120 2119
    private:
2121 2120
      const CrossRefMap& _inverted;
2122 2121
    };
2123 2122

	
2124 2123
    /// \brief It gives back the read-only inverse map.
2125 2124
    ///
2126 2125
    /// It gives back the read-only inverse map.
2127 2126
    InverseMap inverse() const {
2128 2127
      return InverseMap(*this);
2129 2128
    }
2130 2129

	
2131 2130
  };
2132 2131

	
2133 2132
  /// \brief Provides continuous and unique id for the
2134 2133
  /// items of a graph.
2135 2134
  ///
2136 2135
  /// RangeIdMap provides a unique and continuous
2137 2136
  /// id for each item of a given type (\c Node, \c Arc or
2138 2137
  /// \c Edge) in a graph. This id is
2139 2138
  ///  - \b unique: different items get different ids,
2140 2139
  ///  - \b continuous: the range of the ids is the set of integers
2141 2140
  ///    between 0 and \c n-1, where \c n is the number of the items of
2142 2141
  ///    this type (\c Node, \c Arc or \c Edge).
2143 2142
  ///  - So, the ids can change when deleting an item of the same type.
2144 2143
  ///
2145 2144
  /// Thus this id is not (necessarily) the same as what can get using
2146 2145
  /// the \c id() function of the graph or \ref IdMap.
2147 2146
  /// This map can be inverted with its member class \c InverseMap,
2148 2147
  /// or with the \c operator()() member.
2149 2148
  ///
2150 2149
  /// \tparam GR The graph type.
2151 2150
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2152 2151
  /// \c GR::Edge).
2153 2152
  ///
2154 2153
  /// \see IdMap
2155 2154
  template <typename GR, typename K>
2156 2155
  class RangeIdMap
2157 2156
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2158 2157

	
2159 2158
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
2160 2159

	
2161 2160
  public:
2162 2161
    /// The graph type of RangeIdMap.
2163 2162
    typedef GR Graph;
2164 2163
    typedef GR Digraph;
2165 2164
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2166 2165
    typedef K Item;
2167 2166
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2168 2167
    typedef K Key;
2169 2168
    /// The value type of RangeIdMap.
2170 2169
    typedef int Value;
2171 2170

	
2172 2171
    /// \brief Constructor.
2173 2172
    ///
2174 2173
    /// Constructor.
2175 2174
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
2176 2175
      Item it;
2177 2176
      const typename Map::Notifier* nf = Map::notifier();
2178 2177
      for (nf->first(it); it != INVALID; nf->next(it)) {
2179 2178
        Map::set(it, _inv_map.size());
2180 2179
        _inv_map.push_back(it);
2181 2180
      }
2182 2181
    }
2183 2182

	
2184 2183
  protected:
2185 2184

	
2186 2185
    /// \brief Adds a new key to the map.
2187 2186
    ///
2188 2187
    /// Add a new key to the map. It is called by the
2189 2188
    /// \c AlterationNotifier.
2190 2189
    virtual void add(const Item& item) {
2191 2190
      Map::add(item);
2192 2191
      Map::set(item, _inv_map.size());
2193 2192
      _inv_map.push_back(item);
2194 2193
    }
2195 2194

	
2196 2195
    /// \brief Add more new keys to the map.
2197 2196
    ///
2198 2197
    /// Add more new keys to the map. It is called by the
2199 2198
    /// \c AlterationNotifier.
2200 2199
    virtual void add(const std::vector<Item>& items) {
2201 2200
      Map::add(items);
2202 2201
      for (int i = 0; i < int(items.size()); ++i) {
2203 2202
        Map::set(items[i], _inv_map.size());
2204 2203
        _inv_map.push_back(items[i]);
2205 2204
      }
2206 2205
    }
2207 2206

	
2208 2207
    /// \brief Erase the key from the map.
2209 2208
    ///
2210 2209
    /// Erase the key from the map. It is called by the
2211 2210
    /// \c AlterationNotifier.
2212 2211
    virtual void erase(const Item& item) {
2213 2212
      Map::set(_inv_map.back(), Map::operator[](item));
2214 2213
      _inv_map[Map::operator[](item)] = _inv_map.back();
2215 2214
      _inv_map.pop_back();
2216 2215
      Map::erase(item);
2217 2216
    }
2218 2217

	
2219 2218
    /// \brief Erase more keys from the map.
2220 2219
    ///
2221 2220
    /// Erase more keys from the map. It is called by the
2222 2221
    /// \c AlterationNotifier.
2223 2222
    virtual void erase(const std::vector<Item>& items) {
2224 2223
      for (int i = 0; i < int(items.size()); ++i) {
2225 2224
        Map::set(_inv_map.back(), Map::operator[](items[i]));
2226 2225
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
2227 2226
        _inv_map.pop_back();
2228 2227
      }
2229 2228
      Map::erase(items);
2230 2229
    }
2231 2230

	
2232 2231
    /// \brief Build the unique map.
2233 2232
    ///
2234 2233
    /// Build the unique map. It is called by the
2235 2234
    /// \c AlterationNotifier.
2236 2235
    virtual void build() {
2237 2236
      Map::build();
2238 2237
      Item it;
2239 2238
      const typename Map::Notifier* nf = Map::notifier();
2240 2239
      for (nf->first(it); it != INVALID; nf->next(it)) {
2241 2240
        Map::set(it, _inv_map.size());
2242 2241
        _inv_map.push_back(it);
2243 2242
      }
2244 2243
    }
2245 2244

	
2246 2245
    /// \brief Clear the keys from the map.
2247 2246
    ///
2248 2247
    /// Clear the keys from the map. It is called by the
2249 2248
    /// \c AlterationNotifier.
2250 2249
    virtual void clear() {
2251 2250
      _inv_map.clear();
2252 2251
      Map::clear();
2253 2252
    }
2254 2253

	
2255 2254
  public:
2256 2255

	
2257 2256
    /// \brief Returns the maximal value plus one.
2258 2257
    ///
2259 2258
    /// Returns the maximal value plus one in the map.
2260 2259
    unsigned int size() const {
2261 2260
      return _inv_map.size();
2262 2261
    }
2263 2262

	
2264 2263
    /// \brief Swaps the position of the two items in the map.
2265 2264
    ///
2266 2265
    /// Swaps the position of the two items in the map.
2267 2266
    void swap(const Item& p, const Item& q) {
2268 2267
      int pi = Map::operator[](p);
2269 2268
      int qi = Map::operator[](q);
2270 2269
      Map::set(p, qi);
2271 2270
      _inv_map[qi] = p;
2272 2271
      Map::set(q, pi);
2273 2272
      _inv_map[pi] = q;
2274 2273
    }
2275 2274

	
2276 2275
    /// \brief Gives back the \e RangeId of the item
2277 2276
    ///
2278 2277
    /// Gives back the \e RangeId of the item.
2279 2278
    int operator[](const Item& item) const {
2280 2279
      return Map::operator[](item);
2281 2280
    }
2282 2281

	
2283 2282
    /// \brief Gives back the item belonging to a \e RangeId
2284
    /// 
2283
    ///
2285 2284
    /// Gives back the item belonging to a \e RangeId.
2286 2285
    Item operator()(int id) const {
2287 2286
      return _inv_map[id];
2288 2287
    }
2289 2288

	
2290 2289
  private:
2291 2290

	
2292 2291
    typedef std::vector<Item> Container;
2293 2292
    Container _inv_map;
2294 2293

	
2295 2294
  public:
2296 2295

	
2297 2296
    /// \brief The inverse map type of RangeIdMap.
2298 2297
    ///
2299 2298
    /// The inverse map type of RangeIdMap.
2300 2299
    class InverseMap {
2301 2300
    public:
2302 2301
      /// \brief Constructor
2303 2302
      ///
2304 2303
      /// Constructor of the InverseMap.
2305 2304
      explicit InverseMap(const RangeIdMap& inverted)
2306 2305
        : _inverted(inverted) {}
2307 2306

	
2308 2307

	
2309 2308
      /// The value type of the InverseMap.
2310 2309
      typedef typename RangeIdMap::Key Value;
2311 2310
      /// The key type of the InverseMap.
2312 2311
      typedef typename RangeIdMap::Value Key;
2313 2312

	
2314 2313
      /// \brief Subscript operator.
2315 2314
      ///
2316 2315
      /// Subscript operator. It gives back the item
2317 2316
      /// that the descriptor currently belongs to.
2318 2317
      Value operator[](const Key& key) const {
2319 2318
        return _inverted(key);
2320 2319
      }
2321 2320

	
2322 2321
      /// \brief Size of the map.
2323 2322
      ///
2324 2323
      /// Returns the size of the map.
2325 2324
      unsigned int size() const {
2326 2325
        return _inverted.size();
2327 2326
      }
2328 2327

	
2329 2328
    private:
2330 2329
      const RangeIdMap& _inverted;
2331 2330
    };
2332 2331

	
2333 2332
    /// \brief Gives back the inverse of the map.
2334 2333
    ///
2335 2334
    /// Gives back the inverse of the map.
2336 2335
    const InverseMap inverse() const {
2337 2336
      return InverseMap(*this);
2338 2337
    }
2339 2338
  };
2340 2339

	
2340
  /// \brief Dynamic iterable \c bool map.
2341
  ///
2342
  /// This class provides a special graph map type which can store a
2343
  /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
2344
  /// For both \c true and \c false values it is possible to iterate on
2345
  /// the keys.
2346
  ///
2347
  /// This type is a reference map, so it can be modified with the
2348
  /// subscription operator.
2349
  ///
2350
  /// \tparam GR The graph type.
2351
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2352
  /// \c GR::Edge).
2353
  ///
2354
  /// \see IterableIntMap, IterableValueMap
2355
  /// \see CrossRefMap
2356
  template <typename GR, typename K>
2357
  class IterableBoolMap
2358
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
2359
  private:
2360
    typedef GR Graph;
2361

	
2362
    typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
2363
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
2364

	
2365
    std::vector<K> _array;
2366
    int _sep;
2367

	
2368
  public:
2369

	
2370
    /// Indicates that the map is reference map.
2371
    typedef True ReferenceMapTag;
2372

	
2373
    /// The key type
2374
    typedef K Key;
2375
    /// The value type
2376
    typedef bool Value;
2377
    /// The const reference type.
2378
    typedef const Value& ConstReference;
2379

	
2380
  private:
2381

	
2382
    int position(const Key& key) const {
2383
      return Parent::operator[](key);
2384
    }
2385

	
2386
  public:
2387

	
2388
    /// \brief Reference to the value of the map.
2389
    ///
2390
    /// This class is similar to the \c bool type. It can be converted to
2391
    /// \c bool and it provides the same operators.
2392
    class Reference {
2393
      friend class IterableBoolMap;
2394
    private:
2395
      Reference(IterableBoolMap& map, const Key& key)
2396
        : _key(key), _map(map) {}
2397
    public:
2398

	
2399
      Reference& operator=(const Reference& value) {
2400
        _map.set(_key, static_cast<bool>(value));
2401
         return *this;
2402
      }
2403

	
2404
      operator bool() const {
2405
        return static_cast<const IterableBoolMap&>(_map)[_key];
2406
      }
2407

	
2408
      Reference& operator=(bool value) {
2409
        _map.set(_key, value);
2410
        return *this;
2411
      }
2412
      Reference& operator&=(bool value) {
2413
        _map.set(_key, _map[_key] & value);
2414
        return *this;
2415
      }
2416
      Reference& operator|=(bool value) {
2417
        _map.set(_key, _map[_key] | value);
2418
        return *this;
2419
      }
2420
      Reference& operator^=(bool value) {
2421
        _map.set(_key, _map[_key] ^ value);
2422
        return *this;
2423
      }
2424
    private:
2425
      Key _key;
2426
      IterableBoolMap& _map;
2427
    };
2428

	
2429
    /// \brief Constructor of the map with a default value.
2430
    ///
2431
    /// Constructor of the map with a default value.
2432
    explicit IterableBoolMap(const Graph& graph, bool def = false)
2433
      : Parent(graph) {
2434
      typename Parent::Notifier* nf = Parent::notifier();
2435
      Key it;
2436
      for (nf->first(it); it != INVALID; nf->next(it)) {
2437
        Parent::set(it, _array.size());
2438
        _array.push_back(it);
2439
      }
2440
      _sep = (def ? _array.size() : 0);
2441
    }
2442

	
2443
    /// \brief Const subscript operator of the map.
2444
    ///
2445
    /// Const subscript operator of the map.
2446
    bool operator[](const Key& key) const {
2447
      return position(key) < _sep;
2448
    }
2449

	
2450
    /// \brief Subscript operator of the map.
2451
    ///
2452
    /// Subscript operator of the map.
2453
    Reference operator[](const Key& key) {
2454
      return Reference(*this, key);
2455
    }
2456

	
2457
    /// \brief Set operation of the map.
2458
    ///
2459
    /// Set operation of the map.
2460
    void set(const Key& key, bool value) {
2461
      int pos = position(key);
2462
      if (value) {
2463
        if (pos < _sep) return;
2464
        Key tmp = _array[_sep];
2465
        _array[_sep] = key;
2466
        Parent::set(key, _sep);
2467
        _array[pos] = tmp;
2468
        Parent::set(tmp, pos);
2469
        ++_sep;
2470
      } else {
2471
        if (pos >= _sep) return;
2472
        --_sep;
2473
        Key tmp = _array[_sep];
2474
        _array[_sep] = key;
2475
        Parent::set(key, _sep);
2476
        _array[pos] = tmp;
2477
        Parent::set(tmp, pos);
2478
      }
2479
    }
2480

	
2481
    /// \brief Set all items.
2482
    ///
2483
    /// Set all items in the map.
2484
    /// \note Constant time operation.
2485
    void setAll(bool value) {
2486
      _sep = (value ? _array.size() : 0);
2487
    }
2488

	
2489
    /// \brief Returns the number of the keys mapped to \c true.
2490
    ///
2491
    /// Returns the number of the keys mapped to \c true.
2492
    int trueNum() const {
2493
      return _sep;
2494
    }
2495

	
2496
    /// \brief Returns the number of the keys mapped to \c false.
2497
    ///
2498
    /// Returns the number of the keys mapped to \c false.
2499
    int falseNum() const {
2500
      return _array.size() - _sep;
2501
    }
2502

	
2503
    /// \brief Iterator for the keys mapped to \c true.
2504
    ///
2505
    /// Iterator for the keys mapped to \c true. It works
2506
    /// like a graph item iterator, it can be converted to
2507
    /// the key type of the map, incremented with \c ++ operator, and
2508
    /// if the iterator leaves the last valid key, it will be equal to
2509
    /// \c INVALID.
2510
    class TrueIt : public Key {
2511
    public:
2512
      typedef Key Parent;
2513

	
2514
      /// \brief Creates an iterator.
2515
      ///
2516
      /// Creates an iterator. It iterates on the
2517
      /// keys mapped to \c true.
2518
      /// \param map The IterableBoolMap.
2519
      explicit TrueIt(const IterableBoolMap& map)
2520
        : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
2521
          _map(&map) {}
2522

	
2523
      /// \brief Invalid constructor \& conversion.
2524
      ///
2525
      /// This constructor initializes the iterator to be invalid.
2526
      /// \sa Invalid for more details.
2527
      TrueIt(Invalid) : Parent(INVALID), _map(0) {}
2528

	
2529
      /// \brief Increment operator.
2530
      ///
2531
      /// Increment operator.
2532
      TrueIt& operator++() {
2533
        int pos = _map->position(*this);
2534
        Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
2535
        return *this;
2536
      }
2537

	
2538
    private:
2539
      const IterableBoolMap* _map;
2540
    };
2541

	
2542
    /// \brief Iterator for the keys mapped to \c false.
2543
    ///
2544
    /// Iterator for the keys mapped to \c false. It works
2545
    /// like a graph item iterator, it can be converted to
2546
    /// the key type of the map, incremented with \c ++ operator, and
2547
    /// if the iterator leaves the last valid key, it will be equal to
2548
    /// \c INVALID.
2549
    class FalseIt : public Key {
2550
    public:
2551
      typedef Key Parent;
2552

	
2553
      /// \brief Creates an iterator.
2554
      ///
2555
      /// Creates an iterator. It iterates on the
2556
      /// keys mapped to \c false.
2557
      /// \param map The IterableBoolMap.
2558
      explicit FalseIt(const IterableBoolMap& map)
2559
        : Parent(map._sep < int(map._array.size()) ?
2560
                 map._array.back() : INVALID), _map(&map) {}
2561

	
2562
      /// \brief Invalid constructor \& conversion.
2563
      ///
2564
      /// This constructor initializes the iterator to be invalid.
2565
      /// \sa Invalid for more details.
2566
      FalseIt(Invalid) : Parent(INVALID), _map(0) {}
2567

	
2568
      /// \brief Increment operator.
2569
      ///
2570
      /// Increment operator.
2571
      FalseIt& operator++() {
2572
        int pos = _map->position(*this);
2573
        Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
2574
        return *this;
2575
      }
2576

	
2577
    private:
2578
      const IterableBoolMap* _map;
2579
    };
2580

	
2581
    /// \brief Iterator for the keys mapped to a given value.
2582
    ///
2583
    /// Iterator for the keys mapped to a given value. It works
2584
    /// like a graph item iterator, it can be converted to
2585
    /// the key type of the map, incremented with \c ++ operator, and
2586
    /// if the iterator leaves the last valid key, it will be equal to
2587
    /// \c INVALID.
2588
    class ItemIt : public Key {
2589
    public:
2590
      typedef Key Parent;
2591

	
2592
      /// \brief Creates an iterator with a value.
2593
      ///
2594
      /// Creates an iterator with a value. It iterates on the
2595
      /// keys mapped to the given value.
2596
      /// \param map The IterableBoolMap.
2597
      /// \param value The value.
2598
      ItemIt(const IterableBoolMap& map, bool value)
2599
        : Parent(value ? 
2600
                 (map._sep > 0 ?
2601
                  map._array[map._sep - 1] : INVALID) :
2602
                 (map._sep < int(map._array.size()) ?
2603
                  map._array.back() : INVALID)), _map(&map) {}
2604

	
2605
      /// \brief Invalid constructor \& conversion.
2606
      ///
2607
      /// This constructor initializes the iterator to be invalid.
2608
      /// \sa Invalid for more details.
2609
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2610

	
2611
      /// \brief Increment operator.
2612
      ///
2613
      /// Increment operator.
2614
      ItemIt& operator++() {
2615
        int pos = _map->position(*this);
2616
        int _sep = pos >= _map->_sep ? _map->_sep : 0;
2617
        Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
2618
        return *this;
2619
      }
2620

	
2621
    private:
2622
      const IterableBoolMap* _map;
2623
    };
2624

	
2625
  protected:
2626

	
2627
    virtual void add(const Key& key) {
2628
      Parent::add(key);
2629
      Parent::set(key, _array.size());
2630
      _array.push_back(key);
2631
    }
2632

	
2633
    virtual void add(const std::vector<Key>& keys) {
2634
      Parent::add(keys);
2635
      for (int i = 0; i < int(keys.size()); ++i) {
2636
        Parent::set(keys[i], _array.size());
2637
        _array.push_back(keys[i]);
2638
      }
2639
    }
2640

	
2641
    virtual void erase(const Key& key) {
2642
      int pos = position(key);
2643
      if (pos < _sep) {
2644
        --_sep;
2645
        Parent::set(_array[_sep], pos);
2646
        _array[pos] = _array[_sep];
2647
        Parent::set(_array.back(), _sep);
2648
        _array[_sep] = _array.back();
2649
        _array.pop_back();
2650
      } else {
2651
        Parent::set(_array.back(), pos);
2652
        _array[pos] = _array.back();
2653
        _array.pop_back();
2654
      }
2655
      Parent::erase(key);
2656
    }
2657

	
2658
    virtual void erase(const std::vector<Key>& keys) {
2659
      for (int i = 0; i < int(keys.size()); ++i) {
2660
        int pos = position(keys[i]);
2661
        if (pos < _sep) {
2662
          --_sep;
2663
          Parent::set(_array[_sep], pos);
2664
          _array[pos] = _array[_sep];
2665
          Parent::set(_array.back(), _sep);
2666
          _array[_sep] = _array.back();
2667
          _array.pop_back();
2668
        } else {
2669
          Parent::set(_array.back(), pos);
2670
          _array[pos] = _array.back();
2671
          _array.pop_back();
2672
        }
2673
      }
2674
      Parent::erase(keys);
2675
    }
2676

	
2677
    virtual void build() {
2678
      Parent::build();
2679
      typename Parent::Notifier* nf = Parent::notifier();
2680
      Key it;
2681
      for (nf->first(it); it != INVALID; nf->next(it)) {
2682
        Parent::set(it, _array.size());
2683
        _array.push_back(it);
2684
      }
2685
      _sep = 0;
2686
    }
2687

	
2688
    virtual void clear() {
2689
      _array.clear();
2690
      _sep = 0;
2691
      Parent::clear();
2692
    }
2693

	
2694
  };
2695

	
2696

	
2697
  namespace _maps_bits {
2698
    template <typename Item>
2699
    struct IterableIntMapNode {
2700
      IterableIntMapNode() : value(-1) {}
2701
      IterableIntMapNode(int _value) : value(_value) {}
2702
      Item prev, next;
2703
      int value;
2704
    };
2705
  }
2706

	
2707
  /// \brief Dynamic iterable integer map.
2708
  ///
2709
  /// This class provides a special graph map type which can store an
2710
  /// integer value for graph items (\c Node, \c Arc or \c Edge).
2711
  /// For each non-negative value it is possible to iterate on the keys
2712
  /// mapped to the value.
2713
  ///
2714
  /// This type is a reference map, so it can be modified with the
2715
  /// subscription operator.
2716
  ///
2717
  /// \note The size of the data structure depends on the largest
2718
  /// value in the map.
2719
  ///
2720
  /// \tparam GR The graph type.
2721
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2722
  /// \c GR::Edge).
2723
  ///
2724
  /// \see IterableBoolMap, IterableValueMap
2725
  /// \see CrossRefMap
2726
  template <typename GR, typename K>
2727
  class IterableIntMap
2728
    : protected ItemSetTraits<GR, K>::
2729
        template Map<_maps_bits::IterableIntMapNode<K> >::Type {
2730
  public:
2731
    typedef typename ItemSetTraits<GR, K>::
2732
      template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
2733

	
2734
    /// The key type
2735
    typedef K Key;
2736
    /// The value type
2737
    typedef int Value;
2738
    /// The graph type
2739
    typedef GR Graph;
2740

	
2741
    /// \brief Constructor of the map.
2742
    ///
2743
    /// Constructor of the map. It sets all values to -1.
2744
    explicit IterableIntMap(const Graph& graph)
2745
      : Parent(graph) {}
2746

	
2747
    /// \brief Constructor of the map with a given value.
2748
    ///
2749
    /// Constructor of the map with a given value.
2750
    explicit IterableIntMap(const Graph& graph, int value)
2751
      : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
2752
      if (value >= 0) {
2753
        for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
2754
          lace(it);
2755
        }
2756
      }
2757
    }
2758

	
2759
  private:
2760

	
2761
    void unlace(const Key& key) {
2762
      typename Parent::Value& node = Parent::operator[](key);
2763
      if (node.value < 0) return;
2764
      if (node.prev != INVALID) {
2765
        Parent::operator[](node.prev).next = node.next;
2766
      } else {
2767
        _first[node.value] = node.next;
2768
      }
2769
      if (node.next != INVALID) {
2770
        Parent::operator[](node.next).prev = node.prev;
2771
      }
2772
      while (!_first.empty() && _first.back() == INVALID) {
2773
        _first.pop_back();
2774
      }
2775
    }
2776

	
2777
    void lace(const Key& key) {
2778
      typename Parent::Value& node = Parent::operator[](key);
2779
      if (node.value < 0) return;
2780
      if (node.value >= int(_first.size())) {
2781
        _first.resize(node.value + 1, INVALID);
2782
      }
2783
      node.prev = INVALID;
2784
      node.next = _first[node.value];
2785
      if (node.next != INVALID) {
2786
        Parent::operator[](node.next).prev = key;
2787
      }
2788
      _first[node.value] = key;
2789
    }
2790

	
2791
  public:
2792

	
2793
    /// Indicates that the map is reference map.
2794
    typedef True ReferenceMapTag;
2795

	
2796
    /// \brief Reference to the value of the map.
2797
    ///
2798
    /// This class is similar to the \c int type. It can
2799
    /// be converted to \c int and it has the same operators.
2800
    class Reference {
2801
      friend class IterableIntMap;
2802
    private:
2803
      Reference(IterableIntMap& map, const Key& key)
2804
        : _key(key), _map(map) {}
2805
    public:
2806

	
2807
      Reference& operator=(const Reference& value) {
2808
        _map.set(_key, static_cast<const int&>(value));
2809
         return *this;
2810
      }
2811

	
2812
      operator const int&() const {
2813
        return static_cast<const IterableIntMap&>(_map)[_key];
2814
      }
2815

	
2816
      Reference& operator=(int value) {
2817
        _map.set(_key, value);
2818
        return *this;
2819
      }
2820
      Reference& operator++() {
2821
        _map.set(_key, _map[_key] + 1);
2822
        return *this;
2823
      }
2824
      int operator++(int) {
2825
        int value = _map[_key];
2826
        _map.set(_key, value + 1);
2827
        return value;
2828
      }
2829
      Reference& operator--() {
2830
        _map.set(_key, _map[_key] - 1);
2831
        return *this;
2832
      }
2833
      int operator--(int) {
2834
        int value = _map[_key];
2835
        _map.set(_key, value - 1);
2836
        return value;
2837
      }
2838
      Reference& operator+=(int value) {
2839
        _map.set(_key, _map[_key] + value);
2840
        return *this;
2841
      }
2842
      Reference& operator-=(int value) {
2843
        _map.set(_key, _map[_key] - value);
2844
        return *this;
2845
      }
2846
      Reference& operator*=(int value) {
2847
        _map.set(_key, _map[_key] * value);
2848
        return *this;
2849
      }
2850
      Reference& operator/=(int value) {
2851
        _map.set(_key, _map[_key] / value);
2852
        return *this;
2853
      }
2854
      Reference& operator%=(int value) {
2855
        _map.set(_key, _map[_key] % value);
2856
        return *this;
2857
      }
2858
      Reference& operator&=(int value) {
2859
        _map.set(_key, _map[_key] & value);
2860
        return *this;
2861
      }
2862
      Reference& operator|=(int value) {
2863
        _map.set(_key, _map[_key] | value);
2864
        return *this;
2865
      }
2866
      Reference& operator^=(int value) {
2867
        _map.set(_key, _map[_key] ^ value);
2868
        return *this;
2869
      }
2870
      Reference& operator<<=(int value) {
2871
        _map.set(_key, _map[_key] << value);
2872
        return *this;
2873
      }
2874
      Reference& operator>>=(int value) {
2875
        _map.set(_key, _map[_key] >> value);
2876
        return *this;
2877
      }
2878

	
2879
    private:
2880
      Key _key;
2881
      IterableIntMap& _map;
2882
    };
2883

	
2884
    /// The const reference type.
2885
    typedef const Value& ConstReference;
2886

	
2887
    /// \brief Gives back the maximal value plus one.
2888
    ///
2889
    /// Gives back the maximal value plus one.
2890
    int size() const {
2891
      return _first.size();
2892
    }
2893

	
2894
    /// \brief Set operation of the map.
2895
    ///
2896
    /// Set operation of the map.
2897
    void set(const Key& key, const Value& value) {
2898
      unlace(key);
2899
      Parent::operator[](key).value = value;
2900
      lace(key);
2901
    }
2902

	
2903
    /// \brief Const subscript operator of the map.
2904
    ///
2905
    /// Const subscript operator of the map.
2906
    const Value& operator[](const Key& key) const {
2907
      return Parent::operator[](key).value;
2908
    }
2909

	
2910
    /// \brief Subscript operator of the map.
2911
    ///
2912
    /// Subscript operator of the map.
2913
    Reference operator[](const Key& key) {
2914
      return Reference(*this, key);
2915
    }
2916

	
2917
    /// \brief Iterator for the keys with the same value.
2918
    ///
2919
    /// Iterator for the keys with the same value. It works
2920
    /// like a graph item iterator, it can be converted to
2921
    /// the item type of the map, incremented with \c ++ operator, and
2922
    /// if the iterator leaves the last valid item, it will be equal to
2923
    /// \c INVALID.
2924
    class ItemIt : public Key {
2925
    public:
2926
      typedef Key Parent;
2927

	
2928
      /// \brief Invalid constructor \& conversion.
2929
      ///
2930
      /// This constructor initializes the iterator to be invalid.
2931
      /// \sa Invalid for more details.
2932
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
2933

	
2934
      /// \brief Creates an iterator with a value.
2935
      ///
2936
      /// Creates an iterator with a value. It iterates on the
2937
      /// keys mapped to the given value.
2938
      /// \param map The IterableIntMap.
2939
      /// \param value The value.
2940
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
2941
        if (value < 0 || value >= int(_map->_first.size())) {
2942
          Parent::operator=(INVALID);
2943
        } else {
2944
          Parent::operator=(_map->_first[value]);
2945
        }
2946
      }
2947

	
2948
      /// \brief Increment operator.
2949
      ///
2950
      /// Increment operator.
2951
      ItemIt& operator++() {
2952
        Parent::operator=(_map->IterableIntMap::Parent::
2953
                          operator[](static_cast<Parent&>(*this)).next);
2954
        return *this;
2955
      }
2956

	
2957
    private:
2958
      const IterableIntMap* _map;
2959
    };
2960

	
2961
  protected:
2962

	
2963
    virtual void erase(const Key& key) {
2964
      unlace(key);
2965
      Parent::erase(key);
2966
    }
2967

	
2968
    virtual void erase(const std::vector<Key>& keys) {
2969
      for (int i = 0; i < int(keys.size()); ++i) {
2970
        unlace(keys[i]);
2971
      }
2972
      Parent::erase(keys);
2973
    }
2974

	
2975
    virtual void clear() {
2976
      _first.clear();
2977
      Parent::clear();
2978
    }
2979

	
2980
  private:
2981
    std::vector<Key> _first;
2982
  };
2983

	
2984
  namespace _maps_bits {
2985
    template <typename Item, typename Value>
2986
    struct IterableValueMapNode {
2987
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
2988
      Item prev, next;
2989
      Value value;
2990
    };
2991
  }
2992

	
2993
  /// \brief Dynamic iterable map for comparable values.
2994
  ///
2995
  /// This class provides a special graph map type which can store an
2996
  /// comparable value for graph items (\c Node, \c Arc or \c Edge).
2997
  /// For each value it is possible to iterate on the keys mapped to
2998
  /// the value.
2999
  ///
3000
  /// The map stores for each value a linked list with
3001
  /// the items which mapped to the value, and the values are stored
3002
  /// in balanced binary tree. The values of the map can be accessed
3003
  /// with stl compatible forward iterator.
3004
  ///
3005
  /// This type is not reference map, so it cannot be modified with
3006
  /// the subscription operator.
3007
  ///
3008
  /// \tparam GR The graph type.
3009
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
3010
  /// \c GR::Edge).
3011
  /// \tparam V The value type of the map. It can be any comparable
3012
  /// value type.
3013
  ///
3014
  /// \see IterableBoolMap, IterableIntMap
3015
  /// \see CrossRefMap
3016
  template <typename GR, typename K, typename V>
3017
  class IterableValueMap
3018
    : protected ItemSetTraits<GR, K>::
3019
        template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
3020
  public:
3021
    typedef typename ItemSetTraits<GR, K>::
3022
      template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
3023

	
3024
    /// The key type
3025
    typedef K Key;
3026
    /// The value type
3027
    typedef V Value;
3028
    /// The graph type
3029
    typedef GR Graph;
3030

	
3031
  public:
3032

	
3033
    /// \brief Constructor of the map with a given value.
3034
    ///
3035
    /// Constructor of the map with a given value.
3036
    explicit IterableValueMap(const Graph& graph,
3037
                              const Value& value = Value())
3038
      : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
3039
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3040
        lace(it);
3041
      }
3042
    }
3043

	
3044
  protected:
3045

	
3046
    void unlace(const Key& key) {
3047
      typename Parent::Value& node = Parent::operator[](key);
3048
      if (node.prev != INVALID) {
3049
        Parent::operator[](node.prev).next = node.next;
3050
      } else {
3051
        if (node.next != INVALID) {
3052
          _first[node.value] = node.next;
3053
        } else {
3054
          _first.erase(node.value);
3055
        }
3056
      }
3057
      if (node.next != INVALID) {
3058
        Parent::operator[](node.next).prev = node.prev;
3059
      }
3060
    }
3061

	
3062
    void lace(const Key& key) {
3063
      typename Parent::Value& node = Parent::operator[](key);
3064
      typename std::map<Value, Key>::iterator it = _first.find(node.value);
3065
      if (it == _first.end()) {
3066
        node.prev = node.next = INVALID;
3067
        _first.insert(std::make_pair(node.value, key));
3068
      } else {
3069
        node.prev = INVALID;
3070
        node.next = it->second;
3071
        if (node.next != INVALID) {
3072
          Parent::operator[](node.next).prev = key;
3073
        }
3074
        it->second = key;
3075
      }
3076
    }
3077

	
3078
  public:
3079

	
3080
    /// \brief Forward iterator for values.
3081
    ///
3082
    /// This iterator is an stl compatible forward
3083
    /// iterator on the values of the map. The values can
3084
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
3085
    class ValueIterator
3086
      : public std::iterator<std::forward_iterator_tag, Value> {
3087
      friend class IterableValueMap;
3088
    private:
3089
      ValueIterator(typename std::map<Value, Key>::const_iterator _it)
3090
        : it(_it) {}
3091
    public:
3092

	
3093
      ValueIterator() {}
3094

	
3095
      ValueIterator& operator++() { ++it; return *this; }
3096
      ValueIterator operator++(int) {
3097
        ValueIterator tmp(*this);
3098
        operator++();
3099
        return tmp;
3100
      }
3101

	
3102
      const Value& operator*() const { return it->first; }
3103
      const Value* operator->() const { return &(it->first); }
3104

	
3105
      bool operator==(ValueIterator jt) const { return it == jt.it; }
3106
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
3107

	
3108
    private:
3109
      typename std::map<Value, Key>::const_iterator it;
3110
    };
3111

	
3112
    /// \brief Returns an iterator to the first value.
3113
    ///
3114
    /// Returns an stl compatible iterator to the
3115
    /// first value of the map. The values of the
3116
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3117
    /// range.
3118
    ValueIterator beginValue() const {
3119
      return ValueIterator(_first.begin());
3120
    }
3121

	
3122
    /// \brief Returns an iterator after the last value.
3123
    ///
3124
    /// Returns an stl compatible iterator after the
3125
    /// last value of the map. The values of the
3126
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
3127
    /// range.
3128
    ValueIterator endValue() const {
3129
      return ValueIterator(_first.end());
3130
    }
3131

	
3132
    /// \brief Set operation of the map.
3133
    ///
3134
    /// Set operation of the map.
3135
    void set(const Key& key, const Value& value) {
3136
      unlace(key);
3137
      Parent::operator[](key).value = value;
3138
      lace(key);
3139
    }
3140

	
3141
    /// \brief Const subscript operator of the map.
3142
    ///
3143
    /// Const subscript operator of the map.
3144
    const Value& operator[](const Key& key) const {
3145
      return Parent::operator[](key).value;
3146
    }
3147

	
3148
    /// \brief Iterator for the keys with the same value.
3149
    ///
3150
    /// Iterator for the keys with the same value. It works
3151
    /// like a graph item iterator, it can be converted to
3152
    /// the item type of the map, incremented with \c ++ operator, and
3153
    /// if the iterator leaves the last valid item, it will be equal to
3154
    /// \c INVALID.
3155
    class ItemIt : public Key {
3156
    public:
3157
      typedef Key Parent;
3158

	
3159
      /// \brief Invalid constructor \& conversion.
3160
      ///
3161
      /// This constructor initializes the iterator to be invalid.
3162
      /// \sa Invalid for more details.
3163
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
3164

	
3165
      /// \brief Creates an iterator with a value.
3166
      ///
3167
      /// Creates an iterator with a value. It iterates on the
3168
      /// keys which have the given value.
3169
      /// \param map The IterableValueMap
3170
      /// \param value The value
3171
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
3172
        typename std::map<Value, Key>::const_iterator it =
3173
          map._first.find(value);
3174
        if (it == map._first.end()) {
3175
          Parent::operator=(INVALID);
3176
        } else {
3177
          Parent::operator=(it->second);
3178
        }
3179
      }
3180

	
3181
      /// \brief Increment operator.
3182
      ///
3183
      /// Increment Operator.
3184
      ItemIt& operator++() {
3185
        Parent::operator=(_map->IterableValueMap::Parent::
3186
                          operator[](static_cast<Parent&>(*this)).next);
3187
        return *this;
3188
      }
3189

	
3190

	
3191
    private:
3192
      const IterableValueMap* _map;
3193
    };
3194

	
3195
  protected:
3196

	
3197
    virtual void add(const Key& key) {
3198
      Parent::add(key);
3199
      unlace(key);
3200
    }
3201

	
3202
    virtual void add(const std::vector<Key>& keys) {
3203
      Parent::add(keys);
3204
      for (int i = 0; i < int(keys.size()); ++i) {
3205
        lace(keys[i]);
3206
      }
3207
    }
3208

	
3209
    virtual void erase(const Key& key) {
3210
      unlace(key);
3211
      Parent::erase(key);
3212
    }
3213

	
3214
    virtual void erase(const std::vector<Key>& keys) {
3215
      for (int i = 0; i < int(keys.size()); ++i) {
3216
        unlace(keys[i]);
3217
      }
3218
      Parent::erase(keys);
3219
    }
3220

	
3221
    virtual void build() {
3222
      Parent::build();
3223
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
3224
        lace(it);
3225
      }
3226
    }
3227

	
3228
    virtual void clear() {
3229
      _first.clear();
3230
      Parent::clear();
3231
    }
3232

	
3233
  private:
3234
    std::map<Value, Key> _first;
3235
  };
3236

	
2341 3237
  /// \brief Map of the source nodes of arcs in a digraph.
2342 3238
  ///
2343 3239
  /// SourceMap provides access for the source node of each arc in a digraph,
2344 3240
  /// which is returned by the \c source() function of the digraph.
2345 3241
  /// \tparam GR The digraph type.
2346 3242
  /// \see TargetMap
2347 3243
  template <typename GR>
2348 3244
  class SourceMap {
2349 3245
  public:
2350 3246

	
2351 3247
    ///\e
2352 3248
    typedef typename GR::Arc Key;
2353 3249
    ///\e
2354 3250
    typedef typename GR::Node Value;
2355 3251

	
2356 3252
    /// \brief Constructor
2357 3253
    ///
2358 3254
    /// Constructor.
2359 3255
    /// \param digraph The digraph that the map belongs to.
2360 3256
    explicit SourceMap(const GR& digraph) : _graph(digraph) {}
2361 3257

	
2362 3258
    /// \brief Returns the source node of the given arc.
2363 3259
    ///
2364 3260
    /// Returns the source node of the given arc.
2365 3261
    Value operator[](const Key& arc) const {
2366 3262
      return _graph.source(arc);
2367 3263
    }
2368 3264

	
2369 3265
  private:
2370 3266
    const GR& _graph;
2371 3267
  };
2372 3268

	
2373 3269
  /// \brief Returns a \c SourceMap class.
2374 3270
  ///
2375 3271
  /// This function just returns an \c SourceMap class.
2376 3272
  /// \relates SourceMap
2377 3273
  template <typename GR>
2378 3274
  inline SourceMap<GR> sourceMap(const GR& graph) {
2379 3275
    return SourceMap<GR>(graph);
2380 3276
  }
2381 3277

	
2382 3278
  /// \brief Map of the target nodes of arcs in a digraph.
2383 3279
  ///
2384 3280
  /// TargetMap provides access for the target node of each arc in a digraph,
2385 3281
  /// which is returned by the \c target() function of the digraph.
2386 3282
  /// \tparam GR The digraph type.
2387 3283
  /// \see SourceMap
2388 3284
  template <typename GR>
2389 3285
  class TargetMap {
2390 3286
  public:
2391 3287

	
2392 3288
    ///\e
2393 3289
    typedef typename GR::Arc Key;
2394 3290
    ///\e
2395 3291
    typedef typename GR::Node Value;
2396 3292

	
2397 3293
    /// \brief Constructor
2398 3294
    ///
2399 3295
    /// Constructor.
2400 3296
    /// \param digraph The digraph that the map belongs to.
2401 3297
    explicit TargetMap(const GR& digraph) : _graph(digraph) {}
2402 3298

	
2403 3299
    /// \brief Returns the target node of the given arc.
2404 3300
    ///
2405 3301
    /// Returns the target node of the given arc.
2406 3302
    Value operator[](const Key& e) const {
2407 3303
      return _graph.target(e);
2408 3304
    }
2409 3305

	
2410 3306
  private:
2411 3307
    const GR& _graph;
2412 3308
  };
2413 3309

	
2414 3310
  /// \brief Returns a \c TargetMap class.
2415 3311
  ///
2416 3312
  /// This function just returns a \c TargetMap class.
2417 3313
  /// \relates TargetMap
2418 3314
  template <typename GR>
2419 3315
  inline TargetMap<GR> targetMap(const GR& graph) {
2420 3316
    return TargetMap<GR>(graph);
2421 3317
  }
2422 3318

	
2423 3319
  /// \brief Map of the "forward" directed arc view of edges in a graph.
2424 3320
  ///
2425 3321
  /// ForwardMap provides access for the "forward" directed arc view of
2426 3322
  /// each edge in a graph, which is returned by the \c direct() function
2427 3323
  /// of the graph with \c true parameter.
2428 3324
  /// \tparam GR The graph type.
2429 3325
  /// \see BackwardMap
2430 3326
  template <typename GR>
2431 3327
  class ForwardMap {
2432 3328
  public:
2433 3329

	
2434 3330
    typedef typename GR::Arc Value;
2435 3331
    typedef typename GR::Edge Key;
2436 3332

	
2437 3333
    /// \brief Constructor
2438 3334
    ///
2439 3335
    /// Constructor.
2440 3336
    /// \param graph The graph that the map belongs to.
2441 3337
    explicit ForwardMap(const GR& graph) : _graph(graph) {}
2442 3338

	
2443 3339
    /// \brief Returns the "forward" directed arc view of the given edge.
2444 3340
    ///
2445 3341
    /// Returns the "forward" directed arc view of the given edge.
2446 3342
    Value operator[](const Key& key) const {
2447 3343
      return _graph.direct(key, true);
2448 3344
    }
2449 3345

	
2450 3346
  private:
2451 3347
    const GR& _graph;
2452 3348
  };
2453 3349

	
2454 3350
  /// \brief Returns a \c ForwardMap class.
2455 3351
  ///
2456 3352
  /// This function just returns an \c ForwardMap class.
2457 3353
  /// \relates ForwardMap
2458 3354
  template <typename GR>
2459 3355
  inline ForwardMap<GR> forwardMap(const GR& graph) {
2460 3356
    return ForwardMap<GR>(graph);
2461 3357
  }
2462 3358

	
2463 3359
  /// \brief Map of the "backward" directed arc view of edges in a graph.
2464 3360
  ///
2465 3361
  /// BackwardMap provides access for the "backward" directed arc view of
2466 3362
  /// each edge in a graph, which is returned by the \c direct() function
2467 3363
  /// of the graph with \c false parameter.
2468 3364
  /// \tparam GR The graph type.
2469 3365
  /// \see ForwardMap
2470 3366
  template <typename GR>
2471 3367
  class BackwardMap {
2472 3368
  public:
2473 3369

	
2474 3370
    typedef typename GR::Arc Value;
2475 3371
    typedef typename GR::Edge Key;
2476 3372

	
2477 3373
    /// \brief Constructor
2478 3374
    ///
2479 3375
    /// Constructor.
2480 3376
    /// \param graph The graph that the map belongs to.
2481 3377
    explicit BackwardMap(const GR& graph) : _graph(graph) {}
2482 3378

	
2483 3379
    /// \brief Returns the "backward" directed arc view of the given edge.
2484 3380
    ///
2485 3381
    /// Returns the "backward" directed arc view of the given edge.
2486 3382
    Value operator[](const Key& key) const {
2487 3383
      return _graph.direct(key, false);
2488 3384
    }
2489 3385

	
2490 3386
  private:
2491 3387
    const GR& _graph;
2492 3388
  };
2493 3389

	
2494 3390
  /// \brief Returns a \c BackwardMap class
2495 3391

	
2496 3392
  /// This function just returns a \c BackwardMap class.
2497 3393
  /// \relates BackwardMap
2498 3394
  template <typename GR>
2499 3395
  inline BackwardMap<GR> backwardMap(const GR& graph) {
2500 3396
    return BackwardMap<GR>(graph);
2501 3397
  }
2502 3398

	
2503 3399
  /// \brief Map of the in-degrees of nodes in a digraph.
2504 3400
  ///
2505 3401
  /// This map returns the in-degree of a node. Once it is constructed,
2506 3402
  /// the degrees are stored in a standard \c NodeMap, so each query is done
2507 3403
  /// in constant time. On the other hand, the values are updated automatically
2508 3404
  /// whenever the digraph changes.
2509 3405
  ///
2510
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure 
3406
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
2511 3407
  /// may provide alternative ways to modify the digraph.
2512 3408
  /// The correct behavior of InDegMap is not guarantied if these additional
2513 3409
  /// features are used. For example the functions
2514 3410
  /// \ref ListDigraph::changeSource() "changeSource()",
2515 3411
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2516 3412
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2517 3413
  /// of \ref ListDigraph will \e not update the degree values correctly.
2518 3414
  ///
2519 3415
  /// \sa OutDegMap
2520 3416
  template <typename GR>
2521 3417
  class InDegMap
2522 3418
    : protected ItemSetTraits<GR, typename GR::Arc>
2523 3419
      ::ItemNotifier::ObserverBase {
2524 3420

	
2525 3421
  public:
2526
    
3422

	
2527 3423
    /// The graph type of InDegMap
2528 3424
    typedef GR Graph;
2529 3425
    typedef GR Digraph;
2530 3426
    /// The key type
2531 3427
    typedef typename Digraph::Node Key;
2532 3428
    /// The value type
2533 3429
    typedef int Value;
2534 3430

	
2535 3431
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2536 3432
    ::ItemNotifier::ObserverBase Parent;
2537 3433

	
2538 3434
  private:
2539 3435

	
2540 3436
    class AutoNodeMap
2541 3437
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2542 3438
    public:
2543 3439

	
2544 3440
      typedef typename ItemSetTraits<Digraph, Key>::
2545 3441
      template Map<int>::Type Parent;
2546 3442

	
2547 3443
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2548 3444

	
2549 3445
      virtual void add(const Key& key) {
2550 3446
        Parent::add(key);
2551 3447
        Parent::set(key, 0);
2552 3448
      }
2553 3449

	
2554 3450
      virtual void add(const std::vector<Key>& keys) {
2555 3451
        Parent::add(keys);
2556 3452
        for (int i = 0; i < int(keys.size()); ++i) {
2557 3453
          Parent::set(keys[i], 0);
2558 3454
        }
2559 3455
      }
2560 3456

	
2561 3457
      virtual void build() {
2562 3458
        Parent::build();
2563 3459
        Key it;
2564 3460
        typename Parent::Notifier* nf = Parent::notifier();
2565 3461
        for (nf->first(it); it != INVALID; nf->next(it)) {
2566 3462
          Parent::set(it, 0);
2567 3463
        }
2568 3464
      }
2569 3465
    };
2570 3466

	
2571 3467
  public:
2572 3468

	
2573 3469
    /// \brief Constructor.
2574 3470
    ///
2575 3471
    /// Constructor for creating an in-degree map.
2576 3472
    explicit InDegMap(const Digraph& graph)
2577 3473
      : _digraph(graph), _deg(graph) {
2578 3474
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2579 3475

	
2580 3476
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2581 3477
        _deg[it] = countInArcs(_digraph, it);
2582 3478
      }
2583 3479
    }
2584 3480

	
2585 3481
    /// \brief Gives back the in-degree of a Node.
2586 3482
    ///
2587 3483
    /// Gives back the in-degree of a Node.
2588 3484
    int operator[](const Key& key) const {
2589 3485
      return _deg[key];
2590 3486
    }
2591 3487

	
2592 3488
  protected:
2593 3489

	
2594 3490
    typedef typename Digraph::Arc Arc;
2595 3491

	
2596 3492
    virtual void add(const Arc& arc) {
2597 3493
      ++_deg[_digraph.target(arc)];
2598 3494
    }
2599 3495

	
2600 3496
    virtual void add(const std::vector<Arc>& arcs) {
2601 3497
      for (int i = 0; i < int(arcs.size()); ++i) {
2602 3498
        ++_deg[_digraph.target(arcs[i])];
2603 3499
      }
2604 3500
    }
2605 3501

	
2606 3502
    virtual void erase(const Arc& arc) {
2607 3503
      --_deg[_digraph.target(arc)];
2608 3504
    }
2609 3505

	
2610 3506
    virtual void erase(const std::vector<Arc>& arcs) {
2611 3507
      for (int i = 0; i < int(arcs.size()); ++i) {
2612 3508
        --_deg[_digraph.target(arcs[i])];
2613 3509
      }
2614 3510
    }
2615 3511

	
2616 3512
    virtual void build() {
2617 3513
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2618 3514
        _deg[it] = countInArcs(_digraph, it);
2619 3515
      }
2620 3516
    }
2621 3517

	
2622 3518
    virtual void clear() {
2623 3519
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2624 3520
        _deg[it] = 0;
2625 3521
      }
2626 3522
    }
2627 3523
  private:
2628 3524

	
2629 3525
    const Digraph& _digraph;
2630 3526
    AutoNodeMap _deg;
2631 3527
  };
2632 3528

	
2633 3529
  /// \brief Map of the out-degrees of nodes in a digraph.
2634 3530
  ///
2635 3531
  /// This map returns the out-degree of a node. Once it is constructed,
2636 3532
  /// the degrees are stored in a standard \c NodeMap, so each query is done
2637 3533
  /// in constant time. On the other hand, the values are updated automatically
2638 3534
  /// whenever the digraph changes.
2639 3535
  ///
2640
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure 
3536
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
2641 3537
  /// may provide alternative ways to modify the digraph.
2642 3538
  /// The correct behavior of OutDegMap is not guarantied if these additional
2643 3539
  /// features are used. For example the functions
2644 3540
  /// \ref ListDigraph::changeSource() "changeSource()",
2645 3541
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
2646 3542
  /// \ref ListDigraph::reverseArc() "reverseArc()"
2647 3543
  /// of \ref ListDigraph will \e not update the degree values correctly.
2648 3544
  ///
2649 3545
  /// \sa InDegMap
2650 3546
  template <typename GR>
2651 3547
  class OutDegMap
2652 3548
    : protected ItemSetTraits<GR, typename GR::Arc>
2653 3549
      ::ItemNotifier::ObserverBase {
2654 3550

	
2655 3551
  public:
2656 3552

	
2657 3553
    /// The graph type of OutDegMap
2658 3554
    typedef GR Graph;
2659 3555
    typedef GR Digraph;
2660 3556
    /// The key type
2661 3557
    typedef typename Digraph::Node Key;
2662 3558
    /// The value type
2663 3559
    typedef int Value;
2664 3560

	
2665 3561
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2666 3562
    ::ItemNotifier::ObserverBase Parent;
2667 3563

	
2668 3564
  private:
2669 3565

	
2670 3566
    class AutoNodeMap
2671 3567
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
2672 3568
    public:
2673 3569

	
2674 3570
      typedef typename ItemSetTraits<Digraph, Key>::
2675 3571
      template Map<int>::Type Parent;
2676 3572

	
2677 3573
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2678 3574

	
2679 3575
      virtual void add(const Key& key) {
2680 3576
        Parent::add(key);
2681 3577
        Parent::set(key, 0);
2682 3578
      }
2683 3579
      virtual void add(const std::vector<Key>& keys) {
2684 3580
        Parent::add(keys);
2685 3581
        for (int i = 0; i < int(keys.size()); ++i) {
2686 3582
          Parent::set(keys[i], 0);
2687 3583
        }
2688 3584
      }
2689 3585
      virtual void build() {
2690 3586
        Parent::build();
2691 3587
        Key it;
2692 3588
        typename Parent::Notifier* nf = Parent::notifier();
2693 3589
        for (nf->first(it); it != INVALID; nf->next(it)) {
2694 3590
          Parent::set(it, 0);
2695 3591
        }
2696 3592
      }
2697 3593
    };
2698 3594

	
2699 3595
  public:
2700 3596

	
2701 3597
    /// \brief Constructor.
2702 3598
    ///
2703 3599
    /// Constructor for creating an out-degree map.
2704 3600
    explicit OutDegMap(const Digraph& graph)
2705 3601
      : _digraph(graph), _deg(graph) {
2706 3602
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2707 3603

	
2708 3604
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2709 3605
        _deg[it] = countOutArcs(_digraph, it);
2710 3606
      }
2711 3607
    }
2712 3608

	
2713 3609
    /// \brief Gives back the out-degree of a Node.
2714 3610
    ///
2715 3611
    /// Gives back the out-degree of a Node.
2716 3612
    int operator[](const Key& key) const {
2717 3613
      return _deg[key];
2718 3614
    }
2719 3615

	
2720 3616
  protected:
2721 3617

	
2722 3618
    typedef typename Digraph::Arc Arc;
2723 3619

	
2724 3620
    virtual void add(const Arc& arc) {
2725 3621
      ++_deg[_digraph.source(arc)];
2726 3622
    }
2727 3623

	
2728 3624
    virtual void add(const std::vector<Arc>& arcs) {
2729 3625
      for (int i = 0; i < int(arcs.size()); ++i) {
2730 3626
        ++_deg[_digraph.source(arcs[i])];
2731 3627
      }
2732 3628
    }
2733 3629

	
2734 3630
    virtual void erase(const Arc& arc) {
2735 3631
      --_deg[_digraph.source(arc)];
2736 3632
    }
2737 3633

	
2738 3634
    virtual void erase(const std::vector<Arc>& arcs) {
2739 3635
      for (int i = 0; i < int(arcs.size()); ++i) {
2740 3636
        --_deg[_digraph.source(arcs[i])];
2741 3637
      }
2742 3638
    }
2743 3639

	
2744 3640
    virtual void build() {
2745 3641
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2746 3642
        _deg[it] = countOutArcs(_digraph, it);
2747 3643
      }
2748 3644
    }
2749 3645

	
2750 3646
    virtual void clear() {
2751 3647
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2752 3648
        _deg[it] = 0;
2753 3649
      }
2754 3650
    }
2755 3651
  private:
2756 3652

	
2757 3653
    const Digraph& _digraph;
2758 3654
    AutoNodeMap _deg;
2759 3655
  };
2760 3656

	
2761 3657
  /// \brief Potential difference map
2762 3658
  ///
2763 3659
  /// PotentialDifferenceMap returns the difference between the potentials of
2764 3660
  /// the source and target nodes of each arc in a digraph, i.e. it returns
2765 3661
  /// \code
2766 3662
  ///   potential[gr.target(arc)] - potential[gr.source(arc)].
2767 3663
  /// \endcode
2768 3664
  /// \tparam GR The digraph type.
2769 3665
  /// \tparam POT A node map storing the potentials.
2770 3666
  template <typename GR, typename POT>
2771 3667
  class PotentialDifferenceMap {
2772 3668
  public:
2773 3669
    /// Key type
2774 3670
    typedef typename GR::Arc Key;
2775 3671
    /// Value type
2776 3672
    typedef typename POT::Value Value;
2777 3673

	
2778 3674
    /// \brief Constructor
2779 3675
    ///
2780 3676
    /// Contructor of the map.
2781 3677
    explicit PotentialDifferenceMap(const GR& gr,
2782 3678
                                    const POT& potential)
2783 3679
      : _digraph(gr), _potential(potential) {}
2784 3680

	
2785 3681
    /// \brief Returns the potential difference for the given arc.
2786 3682
    ///
2787 3683
    /// Returns the potential difference for the given arc, i.e.
2788 3684
    /// \code
2789 3685
    ///   potential[gr.target(arc)] - potential[gr.source(arc)].
2790 3686
    /// \endcode
2791 3687
    Value operator[](const Key& arc) const {
2792 3688
      return _potential[_digraph.target(arc)] -
2793 3689
        _potential[_digraph.source(arc)];
2794 3690
    }
2795 3691

	
2796 3692
  private:
2797 3693
    const GR& _digraph;
2798 3694
    const POT& _potential;
2799 3695
  };
2800 3696

	
2801 3697
  /// \brief Returns a PotentialDifferenceMap.
2802 3698
  ///
2803 3699
  /// This function just returns a PotentialDifferenceMap.
2804 3700
  /// \relates PotentialDifferenceMap
2805 3701
  template <typename GR, typename POT>
2806 3702
  PotentialDifferenceMap<GR, POT>
2807 3703
  potentialDifferenceMap(const GR& gr, const POT& potential) {
2808 3704
    return PotentialDifferenceMap<GR, POT>(gr, potential);
2809 3705
  }
2810 3706

	
2811 3707
  /// @}
2812 3708
}
2813 3709

	
2814 3710
#endif // LEMON_MAPS_H
Ignore white space 6 line context
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
}
Ignore white space 6 line context
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 <deque>
20 20
#include <set>
21 21

	
22 22
#include <lemon/concept_check.h>
23 23
#include <lemon/concepts/maps.h>
24 24
#include <lemon/maps.h>
25 25
#include <lemon/list_graph.h>
26
#include <lemon/smart_graph.h>
26 27

	
27 28
#include "test_tools.h"
28 29

	
29 30
using namespace lemon;
30 31
using namespace lemon::concepts;
31 32

	
32 33
struct A {};
33 34
inline bool operator<(A, A) { return true; }
34 35
struct B {};
35 36

	
36 37
class C {
37 38
  int x;
38 39
public:
39 40
  C(int _x) : x(_x) {}
40 41
};
41 42

	
42 43
class F {
43 44
public:
44 45
  typedef A argument_type;
45 46
  typedef B result_type;
46 47

	
47 48
  B operator()(const A&) const { return B(); }
48 49
private:
49 50
  F& operator=(const F&);
50 51
};
51 52

	
52 53
int func(A) { return 3; }
53 54

	
54 55
int binc(int a, B) { return a+1; }
55 56

	
56 57
typedef ReadMap<A, double> DoubleMap;
57 58
typedef ReadWriteMap<A, double> DoubleWriteMap;
58 59
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
59 60

	
60 61
typedef ReadMap<A, bool> BoolMap;
61 62
typedef ReadWriteMap<A, bool> BoolWriteMap;
62 63
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
63 64

	
64 65
int main()
65 66
{
66 67
  // Map concepts
67 68
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
68 69
  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
69 70
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
70 71
  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
71 72
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
72 73
  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
73 74
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
74 75
  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
75 76

	
76 77
  // NullMap
77 78
  {
78 79
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
79 80
    NullMap<A,B> map1;
80 81
    NullMap<A,B> map2 = map1;
81 82
    map1 = nullMap<A,B>();
82 83
  }
83 84

	
84 85
  // ConstMap
85 86
  {
86 87
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
87 88
    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
88 89
    ConstMap<A,B> map1;
89 90
    ConstMap<A,B> map2 = B();
90 91
    ConstMap<A,B> map3 = map1;
91 92
    map1 = constMap<A>(B());
92 93
    map1 = constMap<A,B>();
93 94
    map1.setAll(B());
94 95
    ConstMap<A,C> map4(C(1));
95 96
    ConstMap<A,C> map5 = map4;
96 97
    map4 = constMap<A>(C(2));
97 98
    map4.setAll(C(3));
98 99

	
99 100
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
100 101
    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
101 102

	
102 103
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
103 104
    ConstMap<A,Const<int,10> > map6;
104 105
    ConstMap<A,Const<int,10> > map7 = map6;
105 106
    map6 = constMap<A,int,10>();
106 107
    map7 = constMap<A,Const<int,10> >();
107 108
    check(map6[A()] == 10 && map7[A()] == 10,
108 109
          "Something is wrong with ConstMap");
109 110
  }
110 111

	
111 112
  // IdentityMap
112 113
  {
113 114
    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
114 115
    IdentityMap<A> map1;
115 116
    IdentityMap<A> map2 = map1;
116 117
    map1 = identityMap<A>();
117 118

	
118 119
    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
119 120
    check(identityMap<double>()[1.0] == 1.0 &&
120 121
          identityMap<double>()[3.14] == 3.14,
121 122
          "Something is wrong with IdentityMap");
122 123
  }
123 124

	
124 125
  // RangeMap
125 126
  {
126 127
    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
127 128
    RangeMap<B> map1;
128 129
    RangeMap<B> map2(10);
129 130
    RangeMap<B> map3(10,B());
130 131
    RangeMap<B> map4 = map1;
131 132
    RangeMap<B> map5 = rangeMap<B>();
132 133
    RangeMap<B> map6 = rangeMap<B>(10);
133 134
    RangeMap<B> map7 = rangeMap(10,B());
134 135

	
135 136
    checkConcept< ReferenceMap<int, double, double&, const double&>,
136 137
                  RangeMap<double> >();
137 138
    std::vector<double> v(10, 0);
138 139
    v[5] = 100;
139 140
    RangeMap<double> map8(v);
140 141
    RangeMap<double> map9 = rangeMap(v);
141 142
    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
142 143
          "Something is wrong with RangeMap");
143 144
  }
144 145

	
145 146
  // SparseMap
146 147
  {
147 148
    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
148 149
    SparseMap<A,B> map1;
149 150
    SparseMap<A,B> map2 = B();
150 151
    SparseMap<A,B> map3 = sparseMap<A,B>();
151 152
    SparseMap<A,B> map4 = sparseMap<A>(B());
152 153

	
153 154
    checkConcept< ReferenceMap<double, int, int&, const int&>,
154 155
                  SparseMap<double, int> >();
155 156
    std::map<double, int> m;
156 157
    SparseMap<double, int> map5(m);
157 158
    SparseMap<double, int> map6(m,10);
158 159
    SparseMap<double, int> map7 = sparseMap(m);
159 160
    SparseMap<double, int> map8 = sparseMap(m,10);
160 161

	
161 162
    check(map5[1.0] == 0 && map5[3.14] == 0 &&
162 163
          map6[1.0] == 10 && map6[3.14] == 10,
163 164
          "Something is wrong with SparseMap");
164 165
    map5[1.0] = map6[3.14] = 100;
165 166
    check(map5[1.0] == 100 && map5[3.14] == 0 &&
166 167
          map6[1.0] == 10 && map6[3.14] == 100,
167 168
          "Something is wrong with SparseMap");
168 169
  }
169 170

	
170 171
  // ComposeMap
171 172
  {
172 173
    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
173 174
    checkConcept<ReadMap<B,double>, CompMap>();
174 175
    CompMap map1 = CompMap(DoubleMap(),ReadMap<B,A>());
175 176
    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
176 177

	
177 178
    SparseMap<double, bool> m1(false); m1[3.14] = true;
178 179
    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
179 180
    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1],
180 181
          "Something is wrong with ComposeMap")
181 182
  }
182 183

	
183 184
  // CombineMap
184 185
  {
185 186
    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
186 187
    checkConcept<ReadMap<A,double>, CombMap>();
187 188
    CombMap map1 = CombMap(DoubleMap(), DoubleMap());
188 189
    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
189 190

	
190 191
    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
191 192
          "Something is wrong with CombineMap");
192 193
  }
193 194

	
194 195
  // FunctorToMap, MapToFunctor
195 196
  {
196 197
    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
197 198
    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
198 199
    FunctorToMap<F> map1;
199 200
    FunctorToMap<F> map2 = FunctorToMap<F>(F());
200 201
    B b = functorToMap(F())[A()];
201 202

	
202 203
    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
203 204
    MapToFunctor<ReadMap<A,B> > map = MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>());
204 205

	
205 206
    check(functorToMap(&func)[A()] == 3,
206 207
          "Something is wrong with FunctorToMap");
207 208
    check(mapToFunctor(constMap<A,int>(2))(A()) == 2,
208 209
          "Something is wrong with MapToFunctor");
209 210
    check(mapToFunctor(functorToMap(&func))(A()) == 3 &&
210 211
          mapToFunctor(functorToMap(&func))[A()] == 3,
211 212
          "Something is wrong with FunctorToMap or MapToFunctor");
212 213
    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
213 214
          "Something is wrong with FunctorToMap or MapToFunctor");
214 215
  }
215 216

	
216 217
  // ConvertMap
217 218
  {
218 219
    checkConcept<ReadMap<double,double>,
219 220
      ConvertMap<ReadMap<double, int>, double> >();
220 221
    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
221 222
    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
222 223
  }
223 224

	
224 225
  // ForkMap
225 226
  {
226 227
    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
227 228

	
228 229
    typedef RangeMap<double> RM;
229 230
    typedef SparseMap<int, double> SM;
230 231
    RM m1(10, -1);
231 232
    SM m2(-1);
232 233
    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
233 234
    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
234 235
    ForkMap<RM, SM> map1(m1,m2);
235 236
    ForkMap<SM, RM> map2 = forkMap(m2,m1);
236 237
    map2.set(5, 10);
237 238
    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 &&
238 239
          m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
239 240
          "Something is wrong with ForkMap");
240 241
  }
241 242

	
242 243
  // Arithmetic maps:
243 244
  // - AddMap, SubMap, MulMap, DivMap
244 245
  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
245 246
  // - NegMap, NegWriteMap, AbsMap
246 247
  {
247 248
    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
248 249
    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
249 250
    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
250 251
    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
251 252

	
252 253
    ConstMap<int, double> c1(1.0), c2(3.14);
253 254
    IdentityMap<int> im;
254 255
    ConvertMap<IdentityMap<int>, double> id(im);
255 256
    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0,
256 257
          "Something is wrong with AddMap");
257 258
    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,
258 259
          "Something is wrong with SubMap");
259 260
    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28,
260 261
          "Something is wrong with MulMap");
261 262
    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57,
262 263
          "Something is wrong with DivMap");
263 264

	
264 265
    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
265 266
    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
266 267
    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
267 268
    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
268 269
    checkConcept<DoubleMap, NegMap<DoubleMap> >();
269 270
    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
270 271
    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
271 272

	
272 273
    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
273 274
          "Something is wrong with ShiftMap");
274 275
    check(shiftWriteMap(id, 2.0)[1] == 3.0 &&
275 276
          shiftWriteMap(id, 2.0)[10] == 12.0,
276 277
          "Something is wrong with ShiftWriteMap");
277 278
    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
278 279
          "Something is wrong with ScaleMap");
279 280
    check(scaleWriteMap(id, 2.0)[1] == 2.0 &&
280 281
          scaleWriteMap(id, 2.0)[10] == 20.0,
281 282
          "Something is wrong with ScaleWriteMap");
282 283
    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
283 284
          "Something is wrong with NegMap");
284 285
    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
285 286
          "Something is wrong with NegWriteMap");
286 287
    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
287 288
          "Something is wrong with AbsMap");
288 289
  }
289 290

	
290 291
  // Logical maps:
291 292
  // - TrueMap, FalseMap
292 293
  // - AndMap, OrMap
293 294
  // - NotMap, NotWriteMap
294 295
  // - EqualMap, LessMap
295 296
  {
296 297
    checkConcept<BoolMap, TrueMap<A> >();
297 298
    checkConcept<BoolMap, FalseMap<A> >();
298 299
    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
299 300
    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
300 301
    checkConcept<BoolMap, NotMap<BoolMap> >();
301 302
    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
302 303
    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
303 304
    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
304 305

	
305 306
    TrueMap<int> tm;
306 307
    FalseMap<int> fm;
307 308
    RangeMap<bool> rm(2);
308 309
    rm[0] = true; rm[1] = false;
309 310
    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] &&
310 311
          !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
311 312
          "Something is wrong with AndMap");
312 313
    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] &&
313 314
          orMap(fm,rm)[0] && !orMap(fm,rm)[1],
314 315
          "Something is wrong with OrMap");
315 316
    check(!notMap(rm)[0] && notMap(rm)[1],
316 317
          "Something is wrong with NotMap");
317 318
    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1],
318 319
          "Something is wrong with NotWriteMap");
319 320

	
320 321
    ConstMap<int, double> cm(2.0);
321 322
    IdentityMap<int> im;
322 323
    ConvertMap<IdentityMap<int>, double> id(im);
323 324
    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
324 325
          "Something is wrong with LessMap");
325 326
    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
326 327
          "Something is wrong with EqualMap");
327 328
  }
328 329

	
329 330
  // LoggerBoolMap
330 331
  {
331 332
    typedef std::vector<int> vec;
332 333
    checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >();
333 334
    checkConcept<WriteMap<int, bool>,
334 335
                 LoggerBoolMap<std::back_insert_iterator<vec> > >();
335 336

	
336 337
    vec v1;
337 338
    vec v2(10);
338 339
    LoggerBoolMap<std::back_insert_iterator<vec> >
339 340
      map1(std::back_inserter(v1));
340 341
    LoggerBoolMap<vec::iterator> map2(v2.begin());
341 342
    map1.set(10, false);
342 343
    map1.set(20, true);   map2.set(20, true);
343 344
    map1.set(30, false);  map2.set(40, false);
344 345
    map1.set(50, true);   map2.set(50, true);
345 346
    map1.set(60, true);   map2.set(60, true);
346 347
    check(v1.size() == 3 && v2.size() == 10 &&
347 348
          v1[0]==20 && v1[1]==50 && v1[2]==60 &&
348 349
          v2[0]==20 && v2[1]==50 && v2[2]==60,
349 350
          "Something is wrong with LoggerBoolMap");
350 351

	
351 352
    int i = 0;
352 353
    for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin();
353 354
          it != map2.end(); ++it )
354 355
      check(v1[i++] == *it, "Something is wrong with LoggerBoolMap");
355 356
  }
356 357
  
357 358
  // IdMap, RangeIdMap
358 359
  {
359 360
    typedef ListDigraph Graph;
360 361
    DIGRAPH_TYPEDEFS(Graph);
361 362

	
362 363
    checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >();
363 364
    checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >();
364 365
    checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >();
365 366
    checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >();
366 367
    
367 368
    Graph gr;
368 369
    IdMap<Graph, Node> nmap(gr);
369 370
    IdMap<Graph, Arc> amap(gr);
370 371
    RangeIdMap<Graph, Node> nrmap(gr);
371 372
    RangeIdMap<Graph, Arc> armap(gr);
372 373
    
373 374
    Node n0 = gr.addNode();
374 375
    Node n1 = gr.addNode();
375 376
    Node n2 = gr.addNode();
376 377
    
377 378
    Arc a0 = gr.addArc(n0, n1);
378 379
    Arc a1 = gr.addArc(n0, n2);
379 380
    Arc a2 = gr.addArc(n2, n1);
380 381
    Arc a3 = gr.addArc(n2, n0);
381 382
    
382 383
    check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap");
383 384
    check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap");
384 385
    check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap");
385 386

	
386 387
    check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap");
387 388
    check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap");
388 389
    check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap");
389 390
    check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap");
390 391

	
391 392
    check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap");
392 393
    check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap");
393 394
    
394 395
    check(nrmap.size() == 3 && armap.size() == 4,
395 396
          "Wrong RangeIdMap::size()");
396 397

	
397 398
    check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap");
398 399
    check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap");
399 400
    check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap");
400 401
    
401 402
    check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap");
402 403
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
403 404
    check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap");
404 405
    check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap");
405 406

	
406 407
    check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap");
407 408
    check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap");
408 409
    
409 410
    gr.erase(n1);
410 411
    
411 412
    if (nrmap[n0] == 1) nrmap.swap(n0, n2);
412 413
    nrmap.swap(n2, n0);
413 414
    if (armap[a1] == 1) armap.swap(a1, a3);
414 415
    armap.swap(a3, a1);
415 416
    
416 417
    check(nrmap.size() == 2 && armap.size() == 2,
417 418
          "Wrong RangeIdMap::size()");
418 419

	
419 420
    check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap");
420 421
    check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap");
421 422
    
422 423
    check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap");
423 424
    check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap");
424 425

	
425 426
    check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap");
426 427
    check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap");
427 428
  }
428 429
  
429 430
  // CrossRefMap
430 431
  {
431 432
    typedef ListDigraph Graph;
432 433
    DIGRAPH_TYPEDEFS(Graph);
433 434

	
434 435
    checkConcept<ReadWriteMap<Node, int>,
435 436
                 CrossRefMap<Graph, Node, int> >();
436 437
    checkConcept<ReadWriteMap<Node, bool>,
437 438
                 CrossRefMap<Graph, Node, bool> >();
438 439
    checkConcept<ReadWriteMap<Node, double>,
439 440
                 CrossRefMap<Graph, Node, double> >();
440 441
    
441 442
    Graph gr;
442 443
    typedef CrossRefMap<Graph, Node, char> CRMap;
443 444
    typedef CRMap::ValueIterator ValueIt;
444 445
    CRMap map(gr);
445 446
    
446 447
    Node n0 = gr.addNode();
447 448
    Node n1 = gr.addNode();
448 449
    Node n2 = gr.addNode();
449 450
    
450 451
    map.set(n0, 'A');
451 452
    map.set(n1, 'B');
452 453
    map.set(n2, 'C');
453 454
    
454 455
    check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
455 456
          "Wrong CrossRefMap");
456 457
    check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
457 458
          "Wrong CrossRefMap");
458 459
    check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
459 460
          "Wrong CrossRefMap");
460 461
    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
461 462
          "Wrong CrossRefMap::count()");
462 463
    
463 464
    ValueIt it = map.beginValue();
464 465
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
465 466
          it == map.endValue(), "Wrong value iterator");
466 467
    
467 468
    map.set(n2, 'A');
468 469

	
469 470
    check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A',
470 471
          "Wrong CrossRefMap");
471 472
    check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
472 473
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
473 474
    check(map('C') == INVALID && map.inverse()['C'] == INVALID,
474 475
          "Wrong CrossRefMap");
475 476
    check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
476 477
          "Wrong CrossRefMap::count()");
477 478

	
478 479
    it = map.beginValue();
479 480
    check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' &&
480 481
          it == map.endValue(), "Wrong value iterator");
481 482

	
482 483
    map.set(n0, 'C');
483 484

	
484 485
    check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A',
485 486
          "Wrong CrossRefMap");
486 487
    check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
487 488
    check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
488 489
    check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
489 490
    check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
490 491
          "Wrong CrossRefMap::count()");
491 492

	
492 493
    it = map.beginValue();
493 494
    check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' &&
494 495
          it == map.endValue(), "Wrong value iterator");
495 496
  }
496 497

	
498
  // Iterable bool map
499
  {
500
    typedef SmartGraph Graph;
501
    typedef SmartGraph::Node Item;
502

	
503
    typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm;
504
    checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>();
505

	
506
    const int num = 10;
507
    Graph g;
508
    std::vector<Item> items;
509
    for (int i = 0; i < num; ++i) {
510
      items.push_back(g.addNode());
511
    }
512

	
513
    Ibm map1(g, true);
514
    int n = 0;
515
    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
516
      check(map1[static_cast<Item>(it)], "Wrong TrueIt");
517
      ++n;
518
    }
519
    check(n == num, "Wrong number");
520

	
521
    n = 0;
522
    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
523
        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
524
        ++n;
525
    }
526
    check(n == num, "Wrong number");
527
    check(Ibm::FalseIt(map1) == INVALID, "Wrong FalseIt");
528
    check(Ibm::ItemIt(map1, false) == INVALID, "Wrong ItemIt for false");
529

	
530
    map1[items[5]] = true;
531

	
532
    n = 0;
533
    for (Ibm::ItemIt it(map1, true); it != INVALID; ++it) {
534
        check(map1[static_cast<Item>(it)], "Wrong ItemIt for true");
535
        ++n;
536
    }
537
    check(n == num, "Wrong number");
538

	
539
    map1[items[num / 2]] = false;
540
    check(map1[items[num / 2]] == false, "Wrong map value");
541

	
542
    n = 0;
543
    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
544
        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
545
        ++n;
546
    }
547
    check(n == num - 1, "Wrong number");
548

	
549
    n = 0;
550
    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
551
        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
552
        ++n;
553
    }
554
    check(n == 1, "Wrong number");
555

	
556
    map1[items[0]] = false;
557
    check(map1[items[0]] == false, "Wrong map value");
558

	
559
    map1[items[num - 1]] = false;
560
    check(map1[items[num - 1]] == false, "Wrong map value");
561

	
562
    n = 0;
563
    for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
564
        check(map1[static_cast<Item>(it)], "Wrong TrueIt for true");
565
        ++n;
566
    }
567
    check(n == num - 3, "Wrong number");
568
    check(map1.trueNum() == num - 3, "Wrong number");
569

	
570
    n = 0;
571
    for (Ibm::FalseIt it(map1); it != INVALID; ++it) {
572
        check(!map1[static_cast<Item>(it)], "Wrong FalseIt for true");
573
        ++n;
574
    }
575
    check(n == 3, "Wrong number");
576
    check(map1.falseNum() == 3, "Wrong number");
577
  }
578

	
579
  // Iterable int map
580
  {
581
    typedef SmartGraph Graph;
582
    typedef SmartGraph::Node Item;
583
    typedef IterableIntMap<SmartGraph, SmartGraph::Node> Iim;
584

	
585
    checkConcept<ReferenceMap<Item, int, int&, const int&>, Iim>();
586

	
587
    const int num = 10;
588
    Graph g;
589
    std::vector<Item> items;
590
    for (int i = 0; i < num; ++i) {
591
      items.push_back(g.addNode());
592
    }
593

	
594
    Iim map1(g);
595
    check(map1.size() == 0, "Wrong size");
596

	
597
    for (int i = 0; i < num; ++i) {
598
      map1[items[i]] = i;
599
    }
600
    check(map1.size() == num, "Wrong size");
601

	
602
    for (int i = 0; i < num; ++i) {
603
      Iim::ItemIt it(map1, i);
604
      check(static_cast<Item>(it) == items[i], "Wrong value");
605
      ++it;
606
      check(static_cast<Item>(it) == INVALID, "Wrong value");
607
    }
608

	
609
    for (int i = 0; i < num; ++i) {
610
      map1[items[i]] = i % 2;
611
    }
612
    check(map1.size() == 2, "Wrong size");
613

	
614
    int n = 0;
615
    for (Iim::ItemIt it(map1, 0); it != INVALID; ++it) {
616
      check(map1[static_cast<Item>(it)] == 0, "Wrong value");
617
      ++n;
618
    }
619
    check(n == (num + 1) / 2, "Wrong number");
620

	
621
    for (Iim::ItemIt it(map1, 1); it != INVALID; ++it) {
622
      check(map1[static_cast<Item>(it)] == 1, "Wrong value");
623
      ++n;
624
    }
625
    check(n == num, "Wrong number");
626

	
627
  }
628

	
629
  // Iterable value map
630
  {
631
    typedef SmartGraph Graph;
632
    typedef SmartGraph::Node Item;
633
    typedef IterableValueMap<SmartGraph, SmartGraph::Node, double> Ivm;
634

	
635
    checkConcept<ReadWriteMap<Item, double>, Ivm>();
636

	
637
    const int num = 10;
638
    Graph g;
639
    std::vector<Item> items;
640
    for (int i = 0; i < num; ++i) {
641
      items.push_back(g.addNode());
642
    }
643

	
644
    Ivm map1(g, 0.0);
645
    check(distance(map1.beginValue(), map1.endValue()) == 1, "Wrong size");
646
    check(*map1.beginValue() == 0.0, "Wrong value");
647

	
648
    for (int i = 0; i < num; ++i) {
649
      map1.set(items[i], static_cast<double>(i));
650
    }
651
    check(distance(map1.beginValue(), map1.endValue()) == num, "Wrong size");
652

	
653
    for (int i = 0; i < num; ++i) {
654
      Ivm::ItemIt it(map1, static_cast<double>(i));
655
      check(static_cast<Item>(it) == items[i], "Wrong value");
656
      ++it;
657
      check(static_cast<Item>(it) == INVALID, "Wrong value");
658
    }
659

	
660
    for (Ivm::ValueIterator vit = map1.beginValue();
661
         vit != map1.endValue(); ++vit) {
662
      check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit,
663
            "Wrong ValueIterator");
664
    }
665

	
666
    for (int i = 0; i < num; ++i) {
667
      map1.set(items[i], static_cast<double>(i % 2));
668
    }
669
    check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size");
670

	
671
    int n = 0;
672
    for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
673
      check(map1[static_cast<Item>(it)] == 0.0, "Wrong value");
674
      ++n;
675
    }
676
    check(n == (num + 1) / 2, "Wrong number");
677

	
678
    for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
679
      check(map1[static_cast<Item>(it)] == 1.0, "Wrong value");
680
      ++n;
681
    }
682
    check(n == num, "Wrong number");
683

	
684
  }
497 685
  return 0;
498 686
}
0 comments (0 inline)