gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Doc improvements for several graph maps (#302)
0 1 0
default
1 file changed with 76 insertions and 40 deletions:
↑ Collapse diff ↑
Show white space 384 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 25
#include <map>
26 26

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

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

	
33 33
namespace lemon {
34 34

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

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

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

	
52 52

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

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

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

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

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

	
85 85

	
86 86
  /// Constant map.
87 87

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

	
110 110
    /// Default constructor
111 111

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

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

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

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

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

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

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

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

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

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

	
151 151

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

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

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

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

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

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

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

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

	
197 197

	
198 198
  /// Identity map.
199 199

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

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

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

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

	
227 227

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

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

	
249 249
  public:
250 250

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

	
260 260
    typedef True ReferenceMapTag;
261 261

	
262 262
  public:
263 263

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

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

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

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

	
283 283
    /// Resizes the map.
284 284

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

	
294 294
  private:
295 295

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

	
298 298
  public:
299 299

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

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

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

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

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

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

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

	
336 336

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

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

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

	
373 373
    typedef True ReferenceMapTag;
374 374

	
375 375
  private:
376 376

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

	
381 381
  public:
382 382

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

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

	
397 397
  private:
398 398

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

	
401 401
  public:
402 402

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

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

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

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

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

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

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

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

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

	
465 465
  /// @}
466 466

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

	
470 470
  /// Composition of two maps
471 471

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

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

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

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

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

	
520 520

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

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

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

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

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

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

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

	
598 598

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

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

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

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

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

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

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

	
658 658

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

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

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

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

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

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

	
701 701

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

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

	
722 722
    /// Constructor
723 723

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

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

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

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

	
741 741

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

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

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

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

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

	
782 782

	
783 783
  /// Sum of two maps
784 784

	
785 785
  /// This \ref concepts::ReadMap "read-only map" returns the sum
786 786
  /// of the values of the two given maps.
787 787
  /// Its \c Key and \c Value types are inherited from \c M1.
788 788
  /// The \c Key and \c Value of \c M2 must be convertible to those of
789 789
  /// \c M1.
790 790
  ///
791 791
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
792 792
  /// \code
793 793
  ///   AddMap<M1,M2> am(m1,m2);
794 794
  /// \endcode
795 795
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
796 796
  ///
797 797
  /// The simplest way of using this map is through the addMap()
798 798
  /// function.
799 799
  ///
800 800
  /// \sa SubMap, MulMap, DivMap
801 801
  /// \sa ShiftMap, ShiftWriteMap
802 802
  template<typename M1, typename M2>
803 803
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
804 804
    const M1 &_m1;
805 805
    const M2 &_m2;
806 806
  public:
807 807
    ///\e
808 808
    typedef typename M1::Key Key;
809 809
    ///\e
810 810
    typedef typename M1::Value Value;
811 811

	
812 812
    /// Constructor
813 813
    AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
814 814
    ///\e
815 815
    Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
816 816
  };
817 817

	
818 818
  /// Returns an \c AddMap class
819 819

	
820 820
  /// This function just returns an \c AddMap class.
821 821
  ///
822 822
  /// For example, if \c m1 and \c m2 are both maps with \c double
823 823
  /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
824 824
  /// <tt>m1[x]+m2[x]</tt>.
825 825
  ///
826 826
  /// \relates AddMap
827 827
  template<typename M1, typename M2>
828 828
  inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
829 829
    return AddMap<M1, M2>(m1,m2);
830 830
  }
831 831

	
832 832

	
833 833
  /// Difference of two maps
834 834

	
835 835
  /// This \ref concepts::ReadMap "read-only map" returns the difference
836 836
  /// of the values of the two given maps.
837 837
  /// Its \c Key and \c Value types are inherited from \c M1.
838 838
  /// The \c Key and \c Value of \c M2 must be convertible to those of
839 839
  /// \c M1.
840 840
  ///
841 841
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
842 842
  /// \code
843 843
  ///   SubMap<M1,M2> sm(m1,m2);
844 844
  /// \endcode
845 845
  /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
846 846
  ///
847 847
  /// The simplest way of using this map is through the subMap()
848 848
  /// function.
849 849
  ///
850 850
  /// \sa AddMap, MulMap, DivMap
851 851
  template<typename M1, typename M2>
852 852
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
853 853
    const M1 &_m1;
854 854
    const M2 &_m2;
855 855
  public:
856 856
    ///\e
857 857
    typedef typename M1::Key Key;
858 858
    ///\e
859 859
    typedef typename M1::Value Value;
860 860

	
861 861
    /// Constructor
862 862
    SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
863 863
    ///\e
864 864
    Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
865 865
  };
866 866

	
867 867
  /// Returns a \c SubMap class
868 868

	
869 869
  /// This function just returns a \c SubMap class.
870 870
  ///
871 871
  /// For example, if \c m1 and \c m2 are both maps with \c double
872 872
  /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
873 873
  /// <tt>m1[x]-m2[x]</tt>.
874 874
  ///
875 875
  /// \relates SubMap
876 876
  template<typename M1, typename M2>
877 877
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
878 878
    return SubMap<M1, M2>(m1,m2);
879 879
  }
880 880

	
881 881

	
882 882
  /// Product of two maps
883 883

	
884 884
  /// This \ref concepts::ReadMap "read-only map" returns the product
885 885
  /// of the values of the two given maps.
886 886
  /// Its \c Key and \c Value types are inherited from \c M1.
887 887
  /// The \c Key and \c Value of \c M2 must be convertible to those of
888 888
  /// \c M1.
889 889
  ///
890 890
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
891 891
  /// \code
892 892
  ///   MulMap<M1,M2> mm(m1,m2);
893 893
  /// \endcode
894 894
  /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
895 895
  ///
896 896
  /// The simplest way of using this map is through the mulMap()
897 897
  /// function.
898 898
  ///
899 899
  /// \sa AddMap, SubMap, DivMap
900 900
  /// \sa ScaleMap, ScaleWriteMap
901 901
  template<typename M1, typename M2>
... ...
@@ -1676,1641 +1676,1677 @@
1676 1676
    /// Constructor
1677 1677
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1678 1678
    ///\e
1679 1679
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1680 1680
  };
1681 1681

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

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

	
1696 1696
  namespace _maps_bits {
1697 1697

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

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

	
1710 1710
  }
1711 1711

	
1712 1712
  /// @}
1713 1713

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

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

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

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

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

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

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

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

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

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

	
1812 1812
  /// @}
1813 1813

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

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

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

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

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

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

	
1866 1866
  public:
1867 1867

	
1868
    /// \brief This class represents the inverse of its owner (IdMap).
1868
    /// \brief The inverse map type of IdMap.
1869 1869
    ///
1870
    /// This class represents the inverse of its owner (IdMap).
1870
    /// The inverse map type of IdMap. The subscript operator gives back
1871
    /// an item by its id.
1872
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
1871 1873
    /// \see inverse()
1872 1874
    class InverseMap {
1873 1875
    public:
1874 1876

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

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

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

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

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

	
1900 1902

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

	
1903 1905
  /// This class provides simple invertable graph maps.
1904 1906
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
1905 1907
  /// and if a key is set to a new value, then stores it in the inverse map.
1906
  /// The values of the map can be accessed
1907
  /// with stl compatible forward iterator.
1908
  /// The graph items can be accessed by their values either using
1909
  /// \c InverseMap or \c operator()(), and the values of the map can be
1910
  /// accessed with an STL compatible forward iterator (\c ValueIterator).
1911
  /// 
1912
  /// This map is intended to be used when all associated values are
1913
  /// different (the map is actually invertable) or there are only a few
1914
  /// items with the same value.
1915
  /// Otherwise consider to use \c IterableValueMap, which is more 
1916
  /// suitable and more efficient for such cases. It provides iterators
1917
  /// to traverse the items with the same associated value, however
1918
  /// it does not have \c InverseMap.
1908 1919
  ///
1909 1920
  /// This type is not reference map, so it cannot be modified with
1910 1921
  /// the subscript operator.
1911 1922
  ///
1912 1923
  /// \tparam GR The graph type.
1913 1924
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
1914 1925
  /// \c GR::Edge).
1915 1926
  /// \tparam V The value type of the map.
1916 1927
  ///
1917 1928
  /// \see IterableValueMap
1918 1929
  template <typename GR, typename K, typename V>
1919 1930
  class CrossRefMap
1920 1931
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
1921 1932
  private:
1922 1933

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

	
1926 1937
    typedef std::multimap<V, K> Container;
1927 1938
    Container _inv_map;
1928 1939

	
1929 1940
  public:
1930 1941

	
1931 1942
    /// The graph type of CrossRefMap.
1932 1943
    typedef GR Graph;
1933 1944
    typedef GR Digraph;
1934 1945
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1935 1946
    typedef K Item;
1936 1947
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
1937 1948
    typedef K Key;
1938 1949
    /// The value type of CrossRefMap.
1939 1950
    typedef V Value;
1940 1951

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

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

	
1972
      /// Constructor
1961 1973
      ValueIterator() {}
1962 1974

	
1975
      /// \e
1963 1976
      ValueIterator& operator++() { ++it; return *this; }
1977
      /// \e
1964 1978
      ValueIterator operator++(int) {
1965 1979
        ValueIterator tmp(*this);
1966 1980
        operator++();
1967 1981
        return tmp;
1968 1982
      }
1969 1983

	
1984
      /// \e
1970 1985
      const Value& operator*() const { return it->first; }
1986
      /// \e
1971 1987
      const Value* operator->() const { return &(it->first); }
1972 1988

	
1989
      /// \e
1973 1990
      bool operator==(ValueIterator jt) const { return it == jt.it; }
1991
      /// \e
1974 1992
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
1975 1993

	
1976 1994
    private:
1977 1995
      typename Container::const_iterator it;
1978 1996
    };
1979 1997

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

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

	
2000 2018
    /// \brief Sets the value associated with the given key.
2001 2019
    ///
2002 2020
    /// Sets the value associated with the given key.
2003 2021
    void set(const Key& key, const Value& val) {
2004 2022
      Value oldval = Map::operator[](key);
2005 2023
      typename Container::iterator it;
2006 2024
      for (it = _inv_map.equal_range(oldval).first;
2007 2025
           it != _inv_map.equal_range(oldval).second; ++it) {
2008 2026
        if (it->second == key) {
2009 2027
          _inv_map.erase(it);
2010 2028
          break;
2011 2029
        }
2012 2030
      }
2013 2031
      _inv_map.insert(std::make_pair(val, key));
2014 2032
      Map::set(key, val);
2015 2033
    }
2016 2034

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

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

	
2044 2062
  protected:
2045 2063

	
2046 2064
    /// \brief Erase the key from the map and the inverse map.
2047 2065
    ///
2048 2066
    /// Erase the key from the map and the inverse map. It is called by the
2049 2067
    /// \c AlterationNotifier.
2050 2068
    virtual void erase(const Key& key) {
2051 2069
      Value val = Map::operator[](key);
2052 2070
      typename Container::iterator it;
2053 2071
      for (it = _inv_map.equal_range(val).first;
2054 2072
           it != _inv_map.equal_range(val).second; ++it) {
2055 2073
        if (it->second == key) {
2056 2074
          _inv_map.erase(it);
2057 2075
          break;
2058 2076
        }
2059 2077
      }
2060 2078
      Map::erase(key);
2061 2079
    }
2062 2080

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

	
2082 2100
    /// \brief Clear the keys from the map and the inverse map.
2083 2101
    ///
2084 2102
    /// Clear the keys from the map and the inverse map. It is called by the
2085 2103
    /// \c AlterationNotifier.
2086 2104
    virtual void clear() {
2087 2105
      _inv_map.clear();
2088 2106
      Map::clear();
2089 2107
    }
2090 2108

	
2091 2109
  public:
2092 2110

	
2093
    /// \brief The inverse map type.
2111
    /// \brief The inverse map type of CrossRefMap.
2094 2112
    ///
2095
    /// The inverse of this map. The subscript operator of the map
2096
    /// gives back the item that was last assigned to the value.
2113
    /// The inverse map type of CrossRefMap. The subscript operator gives
2114
    /// back an item by its value.
2115
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2116
    /// \see inverse()
2097 2117
    class InverseMap {
2098 2118
    public:
2099 2119
      /// \brief Constructor
2100 2120
      ///
2101 2121
      /// Constructor of the InverseMap.
2102 2122
      explicit InverseMap(const CrossRefMap& inverted)
2103 2123
        : _inverted(inverted) {}
2104 2124

	
2105 2125
      /// The value type of the InverseMap.
2106 2126
      typedef typename CrossRefMap::Key Value;
2107 2127
      /// The key type of the InverseMap.
2108 2128
      typedef typename CrossRefMap::Value Key;
2109 2129

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

	
2119 2139
    private:
2120 2140
      const CrossRefMap& _inverted;
2121 2141
    };
2122 2142

	
2123
    /// \brief It gives back the read-only inverse map.
2143
    /// \brief Gives back the inverse of the map.
2124 2144
    ///
2125
    /// It gives back the read-only inverse map.
2145
    /// Gives back the inverse of the CrossRefMap.
2126 2146
    InverseMap inverse() const {
2127 2147
      return InverseMap(*this);
2128 2148
    }
2129 2149

	
2130 2150
  };
2131 2151

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

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

	
2160 2180
  public:
2161 2181
    /// The graph type of RangeIdMap.
2162 2182
    typedef GR Graph;
2163 2183
    typedef GR Digraph;
2164 2184
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2165 2185
    typedef K Item;
2166 2186
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
2167 2187
    typedef K Key;
2168 2188
    /// The value type of RangeIdMap.
2169 2189
    typedef int Value;
2170 2190

	
2171 2191
    /// \brief Constructor.
2172 2192
    ///
2173 2193
    /// Constructor.
2174 2194
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
2175 2195
      Item it;
2176 2196
      const typename Map::Notifier* nf = Map::notifier();
2177 2197
      for (nf->first(it); it != INVALID; nf->next(it)) {
2178 2198
        Map::set(it, _inv_map.size());
2179 2199
        _inv_map.push_back(it);
2180 2200
      }
2181 2201
    }
2182 2202

	
2183 2203
  protected:
2184 2204

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

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

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

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

	
2231 2251
    /// \brief Build the unique map.
2232 2252
    ///
2233 2253
    /// Build the unique map. It is called by the
2234 2254
    /// \c AlterationNotifier.
2235 2255
    virtual void build() {
2236 2256
      Map::build();
2237 2257
      Item it;
2238 2258
      const typename Map::Notifier* nf = Map::notifier();
2239 2259
      for (nf->first(it); it != INVALID; nf->next(it)) {
2240 2260
        Map::set(it, _inv_map.size());
2241 2261
        _inv_map.push_back(it);
2242 2262
      }
2243 2263
    }
2244 2264

	
2245 2265
    /// \brief Clear the keys from the map.
2246 2266
    ///
2247 2267
    /// Clear the keys from the map. It is called by the
2248 2268
    /// \c AlterationNotifier.
2249 2269
    virtual void clear() {
2250 2270
      _inv_map.clear();
2251 2271
      Map::clear();
2252 2272
    }
2253 2273

	
2254 2274
  public:
2255 2275

	
2256 2276
    /// \brief Returns the maximal value plus one.
2257 2277
    ///
2258 2278
    /// Returns the maximal value plus one in the map.
2259 2279
    unsigned int size() const {
2260 2280
      return _inv_map.size();
2261 2281
    }
2262 2282

	
2263 2283
    /// \brief Swaps the position of the two items in the map.
2264 2284
    ///
2265 2285
    /// Swaps the position of the two items in the map.
2266 2286
    void swap(const Item& p, const Item& q) {
2267 2287
      int pi = Map::operator[](p);
2268 2288
      int qi = Map::operator[](q);
2269 2289
      Map::set(p, qi);
2270 2290
      _inv_map[qi] = p;
2271 2291
      Map::set(q, pi);
2272 2292
      _inv_map[pi] = q;
2273 2293
    }
2274 2294

	
2275
    /// \brief Gives back the \e RangeId of the item
2295
    /// \brief Gives back the \e range \e id of the item
2276 2296
    ///
2277
    /// Gives back the \e RangeId of the item.
2297
    /// Gives back the \e range \e id of the item.
2278 2298
    int operator[](const Item& item) const {
2279 2299
      return Map::operator[](item);
2280 2300
    }
2281 2301

	
2282
    /// \brief Gives back the item belonging to a \e RangeId
2302
    /// \brief Gives back the item belonging to a \e range \e id
2283 2303
    ///
2284
    /// Gives back the item belonging to a \e RangeId.
2304
    /// Gives back the item belonging to the given \e range \e id.
2285 2305
    Item operator()(int id) const {
2286 2306
      return _inv_map[id];
2287 2307
    }
2288 2308

	
2289 2309
  private:
2290 2310

	
2291 2311
    typedef std::vector<Item> Container;
2292 2312
    Container _inv_map;
2293 2313

	
2294 2314
  public:
2295 2315

	
2296 2316
    /// \brief The inverse map type of RangeIdMap.
2297 2317
    ///
2298
    /// The inverse map type of RangeIdMap.
2318
    /// The inverse map type of RangeIdMap. The subscript operator gives
2319
    /// back an item by its \e range \e id.
2320
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
2299 2321
    class InverseMap {
2300 2322
    public:
2301 2323
      /// \brief Constructor
2302 2324
      ///
2303 2325
      /// Constructor of the InverseMap.
2304 2326
      explicit InverseMap(const RangeIdMap& inverted)
2305 2327
        : _inverted(inverted) {}
2306 2328

	
2307 2329

	
2308 2330
      /// The value type of the InverseMap.
2309 2331
      typedef typename RangeIdMap::Key Value;
2310 2332
      /// The key type of the InverseMap.
2311 2333
      typedef typename RangeIdMap::Value Key;
2312 2334

	
2313 2335
      /// \brief Subscript operator.
2314 2336
      ///
2315 2337
      /// Subscript operator. It gives back the item
2316
      /// that the descriptor currently belongs to.
2338
      /// that the given \e range \e id currently belongs to.
2317 2339
      Value operator[](const Key& key) const {
2318 2340
        return _inverted(key);
2319 2341
      }
2320 2342

	
2321 2343
      /// \brief Size of the map.
2322 2344
      ///
2323 2345
      /// Returns the size of the map.
2324 2346
      unsigned int size() const {
2325 2347
        return _inverted.size();
2326 2348
      }
2327 2349

	
2328 2350
    private:
2329 2351
      const RangeIdMap& _inverted;
2330 2352
    };
2331 2353

	
2332 2354
    /// \brief Gives back the inverse of the map.
2333 2355
    ///
2334
    /// Gives back the inverse of the map.
2356
    /// Gives back the inverse of the RangeIdMap.
2335 2357
    const InverseMap inverse() const {
2336 2358
      return InverseMap(*this);
2337 2359
    }
2338 2360
  };
2339 2361

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

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

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

	
2368 2390
  public:
2369 2391

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

	
2373 2395
    /// The key type
2374 2396
    typedef K Key;
2375 2397
    /// The value type
2376 2398
    typedef bool Value;
2377 2399
    /// The const reference type.
2378 2400
    typedef const Value& ConstReference;
2379 2401

	
2380 2402
  private:
2381 2403

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

	
2386 2408
  public:
2387 2409

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

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

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

	
2408 2430
      Reference& operator=(bool value) {
2409 2431
        _map.set(_key, value);
2410 2432
        return *this;
2411 2433
      }
2412 2434
      Reference& operator&=(bool value) {
2413 2435
        _map.set(_key, _map[_key] & value);
2414 2436
        return *this;
2415 2437
      }
2416 2438
      Reference& operator|=(bool value) {
2417 2439
        _map.set(_key, _map[_key] | value);
2418 2440
        return *this;
2419 2441
      }
2420 2442
      Reference& operator^=(bool value) {
2421 2443
        _map.set(_key, _map[_key] ^ value);
2422 2444
        return *this;
2423 2445
      }
2424 2446
    private:
2425 2447
      Key _key;
2426 2448
      IterableBoolMap& _map;
2427 2449
    };
2428 2450

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

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

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

	
2457 2479
    /// \brief Set operation of the map.
2458 2480
    ///
2459 2481
    /// Set operation of the map.
2460 2482
    void set(const Key& key, bool value) {
2461 2483
      int pos = position(key);
2462 2484
      if (value) {
2463 2485
        if (pos < _sep) return;
2464 2486
        Key tmp = _array[_sep];
2465 2487
        _array[_sep] = key;
2466 2488
        Parent::set(key, _sep);
2467 2489
        _array[pos] = tmp;
2468 2490
        Parent::set(tmp, pos);
2469 2491
        ++_sep;
2470 2492
      } else {
2471 2493
        if (pos >= _sep) return;
2472 2494
        --_sep;
2473 2495
        Key tmp = _array[_sep];
2474 2496
        _array[_sep] = key;
2475 2497
        Parent::set(key, _sep);
2476 2498
        _array[pos] = tmp;
2477 2499
        Parent::set(tmp, pos);
2478 2500
      }
2479 2501
    }
2480 2502

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

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

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

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

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

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

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

	
2538 2560
    private:
2539 2561
      const IterableBoolMap* _map;
2540 2562
    };
2541 2563

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

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

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

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

	
2577 2599
    private:
2578 2600
      const IterableBoolMap* _map;
2579 2601
    };
2580 2602

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

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

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

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

	
2621 2643
    private:
2622 2644
      const IterableBoolMap* _map;
2623 2645
    };
2624 2646

	
2625 2647
  protected:
2626 2648

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

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

	
2641 2663
    virtual void erase(const Key& key) {
2642 2664
      int pos = position(key);
2643 2665
      if (pos < _sep) {
2644 2666
        --_sep;
2645 2667
        Parent::set(_array[_sep], pos);
2646 2668
        _array[pos] = _array[_sep];
2647 2669
        Parent::set(_array.back(), _sep);
2648 2670
        _array[_sep] = _array.back();
2649 2671
        _array.pop_back();
2650 2672
      } else {
2651 2673
        Parent::set(_array.back(), pos);
2652 2674
        _array[pos] = _array.back();
2653 2675
        _array.pop_back();
2654 2676
      }
2655 2677
      Parent::erase(key);
2656 2678
    }
2657 2679

	
2658 2680
    virtual void erase(const std::vector<Key>& keys) {
2659 2681
      for (int i = 0; i < int(keys.size()); ++i) {
2660 2682
        int pos = position(keys[i]);
2661 2683
        if (pos < _sep) {
2662 2684
          --_sep;
2663 2685
          Parent::set(_array[_sep], pos);
2664 2686
          _array[pos] = _array[_sep];
2665 2687
          Parent::set(_array.back(), _sep);
2666 2688
          _array[_sep] = _array.back();
2667 2689
          _array.pop_back();
2668 2690
        } else {
2669 2691
          Parent::set(_array.back(), pos);
2670 2692
          _array[pos] = _array.back();
2671 2693
          _array.pop_back();
2672 2694
        }
2673 2695
      }
2674 2696
      Parent::erase(keys);
2675 2697
    }
2676 2698

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

	
2688 2710
    virtual void clear() {
2689 2711
      _array.clear();
2690 2712
      _sep = 0;
2691 2713
      Parent::clear();
2692 2714
    }
2693 2715

	
2694 2716
  };
2695 2717

	
2696 2718

	
2697 2719
  namespace _maps_bits {
2698 2720
    template <typename Item>
2699 2721
    struct IterableIntMapNode {
2700 2722
      IterableIntMapNode() : value(-1) {}
2701 2723
      IterableIntMapNode(int _value) : value(_value) {}
2702 2724
      Item prev, next;
2703 2725
      int value;
2704 2726
    };
2705 2727
  }
2706 2728

	
2707 2729
  /// \brief Dynamic iterable integer map.
2708 2730
  ///
2709 2731
  /// This class provides a special graph map type which can store an
2710 2732
  /// integer value for graph items (\c Node, \c Arc or \c Edge).
2711 2733
  /// For each non-negative value it is possible to iterate on the keys
2712 2734
  /// mapped to the value.
2713 2735
  ///
2736
  /// This map is intended to be used with small integer values, for which
2737
  /// it is efficient, and supports iteration only for non-negative values.
2738
  /// If you need large values and/or iteration for negative integers,
2739
  /// consider to use \ref IterableValueMap instead.
2740
  ///
2714 2741
  /// This type is a reference map, so it can be modified with the
2715
  /// subscription operator.
2742
  /// subscript operator.
2716 2743
  ///
2717 2744
  /// \note The size of the data structure depends on the largest
2718 2745
  /// value in the map.
2719 2746
  ///
2720 2747
  /// \tparam GR The graph type.
2721 2748
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
2722 2749
  /// \c GR::Edge).
2723 2750
  ///
2724 2751
  /// \see IterableBoolMap, IterableValueMap
2725 2752
  /// \see CrossRefMap
2726 2753
  template <typename GR, typename K>
2727 2754
  class IterableIntMap
2728 2755
    : protected ItemSetTraits<GR, K>::
2729 2756
        template Map<_maps_bits::IterableIntMapNode<K> >::Type {
2730 2757
  public:
2731 2758
    typedef typename ItemSetTraits<GR, K>::
2732 2759
      template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
2733 2760

	
2734 2761
    /// The key type
2735 2762
    typedef K Key;
2736 2763
    /// The value type
2737 2764
    typedef int Value;
2738 2765
    /// The graph type
2739 2766
    typedef GR Graph;
2740 2767

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

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

	
2759 2786
  private:
2760 2787

	
2761 2788
    void unlace(const Key& key) {
2762 2789
      typename Parent::Value& node = Parent::operator[](key);
2763 2790
      if (node.value < 0) return;
2764 2791
      if (node.prev != INVALID) {
2765 2792
        Parent::operator[](node.prev).next = node.next;
2766 2793
      } else {
2767 2794
        _first[node.value] = node.next;
2768 2795
      }
2769 2796
      if (node.next != INVALID) {
2770 2797
        Parent::operator[](node.next).prev = node.prev;
2771 2798
      }
2772 2799
      while (!_first.empty() && _first.back() == INVALID) {
2773 2800
        _first.pop_back();
2774 2801
      }
2775 2802
    }
2776 2803

	
2777 2804
    void lace(const Key& key) {
2778 2805
      typename Parent::Value& node = Parent::operator[](key);
2779 2806
      if (node.value < 0) return;
2780 2807
      if (node.value >= int(_first.size())) {
2781 2808
        _first.resize(node.value + 1, INVALID);
2782 2809
      }
2783 2810
      node.prev = INVALID;
2784 2811
      node.next = _first[node.value];
2785 2812
      if (node.next != INVALID) {
2786 2813
        Parent::operator[](node.next).prev = key;
2787 2814
      }
2788 2815
      _first[node.value] = key;
2789 2816
    }
2790 2817

	
2791 2818
  public:
2792 2819

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

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

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

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

	
2816 2843
      Reference& operator=(int value) {
2817 2844
        _map.set(_key, value);
2818 2845
        return *this;
2819 2846
      }
2820 2847
      Reference& operator++() {
2821 2848
        _map.set(_key, _map[_key] + 1);
2822 2849
        return *this;
2823 2850
      }
2824 2851
      int operator++(int) {
2825 2852
        int value = _map[_key];
2826 2853
        _map.set(_key, value + 1);
2827 2854
        return value;
2828 2855
      }
2829 2856
      Reference& operator--() {
2830 2857
        _map.set(_key, _map[_key] - 1);
2831 2858
        return *this;
2832 2859
      }
2833 2860
      int operator--(int) {
2834 2861
        int value = _map[_key];
2835 2862
        _map.set(_key, value - 1);
2836 2863
        return value;
2837 2864
      }
2838 2865
      Reference& operator+=(int value) {
2839 2866
        _map.set(_key, _map[_key] + value);
2840 2867
        return *this;
2841 2868
      }
2842 2869
      Reference& operator-=(int value) {
2843 2870
        _map.set(_key, _map[_key] - value);
2844 2871
        return *this;
2845 2872
      }
2846 2873
      Reference& operator*=(int value) {
2847 2874
        _map.set(_key, _map[_key] * value);
2848 2875
        return *this;
2849 2876
      }
2850 2877
      Reference& operator/=(int value) {
2851 2878
        _map.set(_key, _map[_key] / value);
2852 2879
        return *this;
2853 2880
      }
2854 2881
      Reference& operator%=(int value) {
2855 2882
        _map.set(_key, _map[_key] % value);
2856 2883
        return *this;
2857 2884
      }
2858 2885
      Reference& operator&=(int value) {
2859 2886
        _map.set(_key, _map[_key] & value);
2860 2887
        return *this;
2861 2888
      }
2862 2889
      Reference& operator|=(int value) {
2863 2890
        _map.set(_key, _map[_key] | value);
2864 2891
        return *this;
2865 2892
      }
2866 2893
      Reference& operator^=(int value) {
2867 2894
        _map.set(_key, _map[_key] ^ value);
2868 2895
        return *this;
2869 2896
      }
2870 2897
      Reference& operator<<=(int value) {
2871 2898
        _map.set(_key, _map[_key] << value);
2872 2899
        return *this;
2873 2900
      }
2874 2901
      Reference& operator>>=(int value) {
2875 2902
        _map.set(_key, _map[_key] >> value);
2876 2903
        return *this;
2877 2904
      }
2878 2905

	
2879 2906
    private:
2880 2907
      Key _key;
2881 2908
      IterableIntMap& _map;
2882 2909
    };
2883 2910

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

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

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

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

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

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

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

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

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

	
2957 2984
    private:
2958 2985
      const IterableIntMap* _map;
2959 2986
    };
2960 2987

	
2961 2988
  protected:
2962 2989

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

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

	
2975 3002
    virtual void clear() {
2976 3003
      _first.clear();
2977 3004
      Parent::clear();
2978 3005
    }
2979 3006

	
2980 3007
  private:
2981 3008
    std::vector<Key> _first;
2982 3009
  };
2983 3010

	
2984 3011
  namespace _maps_bits {
2985 3012
    template <typename Item, typename Value>
2986 3013
    struct IterableValueMapNode {
2987 3014
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
2988 3015
      Item prev, next;
2989 3016
      Value value;
2990 3017
    };
2991 3018
  }
2992 3019

	
2993 3020
  /// \brief Dynamic iterable map for comparable values.
2994 3021
  ///
2995
  /// This class provides a special graph map type which can store an
3022
  /// This class provides a special graph map type which can store a
2996 3023
  /// comparable value for graph items (\c Node, \c Arc or \c Edge).
2997 3024
  /// For each value it is possible to iterate on the keys mapped to
2998
  /// the value.
3025
  /// the value (\c ItemIt), and the values of the map can be accessed
3026
  /// with an STL compatible forward iterator (\c ValueIterator).
3027
  /// The map stores a linked list for each value, which contains
3028
  /// the items mapped to the value, and the used values are stored
3029
  /// in balanced binary tree (\c std::map).
2999 3030
  ///
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.
3031
  /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
3032
  /// specialized for \c bool and \c int values, respectively.
3004 3033
  ///
3005 3034
  /// This type is not reference map, so it cannot be modified with
3006
  /// the subscription operator.
3035
  /// the subscript operator.
3007 3036
  ///
3008 3037
  /// \tparam GR The graph type.
3009 3038
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
3010 3039
  /// \c GR::Edge).
3011 3040
  /// \tparam V The value type of the map. It can be any comparable
3012 3041
  /// value type.
3013 3042
  ///
3014 3043
  /// \see IterableBoolMap, IterableIntMap
3015 3044
  /// \see CrossRefMap
3016 3045
  template <typename GR, typename K, typename V>
3017 3046
  class IterableValueMap
3018 3047
    : protected ItemSetTraits<GR, K>::
3019 3048
        template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
3020 3049
  public:
3021 3050
    typedef typename ItemSetTraits<GR, K>::
3022 3051
      template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
3023 3052

	
3024 3053
    /// The key type
3025 3054
    typedef K Key;
3026 3055
    /// The value type
3027 3056
    typedef V Value;
3028 3057
    /// The graph type
3029 3058
    typedef GR Graph;
3030 3059

	
3031 3060
  public:
3032 3061

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

	
3044 3073
  protected:
3045 3074

	
3046 3075
    void unlace(const Key& key) {
3047 3076
      typename Parent::Value& node = Parent::operator[](key);
3048 3077
      if (node.prev != INVALID) {
3049 3078
        Parent::operator[](node.prev).next = node.next;
3050 3079
      } else {
3051 3080
        if (node.next != INVALID) {
3052 3081
          _first[node.value] = node.next;
3053 3082
        } else {
3054 3083
          _first.erase(node.value);
3055 3084
        }
3056 3085
      }
3057 3086
      if (node.next != INVALID) {
3058 3087
        Parent::operator[](node.next).prev = node.prev;
3059 3088
      }
3060 3089
    }
3061 3090

	
3062 3091
    void lace(const Key& key) {
3063 3092
      typename Parent::Value& node = Parent::operator[](key);
3064 3093
      typename std::map<Value, Key>::iterator it = _first.find(node.value);
3065 3094
      if (it == _first.end()) {
3066 3095
        node.prev = node.next = INVALID;
3067 3096
        _first.insert(std::make_pair(node.value, key));
3068 3097
      } else {
3069 3098
        node.prev = INVALID;
3070 3099
        node.next = it->second;
3071 3100
        if (node.next != INVALID) {
3072 3101
          Parent::operator[](node.next).prev = key;
3073 3102
        }
3074 3103
        it->second = key;
3075 3104
      }
3076 3105
    }
3077 3106

	
3078 3107
  public:
3079 3108

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

	
3122
      /// Constructor
3093 3123
      ValueIterator() {}
3094 3124

	
3125
      /// \e
3095 3126
      ValueIterator& operator++() { ++it; return *this; }
3127
      /// \e
3096 3128
      ValueIterator operator++(int) {
3097 3129
        ValueIterator tmp(*this);
3098 3130
        operator++();
3099 3131
        return tmp;
3100 3132
      }
3101 3133

	
3134
      /// \e
3102 3135
      const Value& operator*() const { return it->first; }
3136
      /// \e
3103 3137
      const Value* operator->() const { return &(it->first); }
3104 3138

	
3139
      /// \e
3105 3140
      bool operator==(ValueIterator jt) const { return it == jt.it; }
3141
      /// \e
3106 3142
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
3107 3143

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

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

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

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

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

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

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

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

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

	
3190 3226

	
3191 3227
    private:
3192 3228
      const IterableValueMap* _map;
3193 3229
    };
3194 3230

	
3195 3231
  protected:
3196 3232

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

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

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

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

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

	
3228 3264
    virtual void clear() {
3229 3265
      _first.clear();
3230 3266
      Parent::clear();
3231 3267
    }
3232 3268

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

	
3237 3273
  /// \brief Map of the source nodes of arcs in a digraph.
3238 3274
  ///
3239 3275
  /// SourceMap provides access for the source node of each arc in a digraph,
3240 3276
  /// which is returned by the \c source() function of the digraph.
3241 3277
  /// \tparam GR The digraph type.
3242 3278
  /// \see TargetMap
3243 3279
  template <typename GR>
3244 3280
  class SourceMap {
3245 3281
  public:
3246 3282

	
3247 3283
    ///\e
3248 3284
    typedef typename GR::Arc Key;
3249 3285
    ///\e
3250 3286
    typedef typename GR::Node Value;
3251 3287

	
3252 3288
    /// \brief Constructor
3253 3289
    ///
3254 3290
    /// Constructor.
3255 3291
    /// \param digraph The digraph that the map belongs to.
3256 3292
    explicit SourceMap(const GR& digraph) : _graph(digraph) {}
3257 3293

	
3258 3294
    /// \brief Returns the source node of the given arc.
3259 3295
    ///
3260 3296
    /// Returns the source node of the given arc.
3261 3297
    Value operator[](const Key& arc) const {
3262 3298
      return _graph.source(arc);
3263 3299
    }
3264 3300

	
3265 3301
  private:
3266 3302
    const GR& _graph;
3267 3303
  };
3268 3304

	
3269 3305
  /// \brief Returns a \c SourceMap class.
3270 3306
  ///
3271 3307
  /// This function just returns an \c SourceMap class.
3272 3308
  /// \relates SourceMap
3273 3309
  template <typename GR>
3274 3310
  inline SourceMap<GR> sourceMap(const GR& graph) {
3275 3311
    return SourceMap<GR>(graph);
3276 3312
  }
3277 3313

	
3278 3314
  /// \brief Map of the target nodes of arcs in a digraph.
3279 3315
  ///
3280 3316
  /// TargetMap provides access for the target node of each arc in a digraph,
3281 3317
  /// which is returned by the \c target() function of the digraph.
3282 3318
  /// \tparam GR The digraph type.
3283 3319
  /// \see SourceMap
3284 3320
  template <typename GR>
3285 3321
  class TargetMap {
3286 3322
  public:
3287 3323

	
3288 3324
    ///\e
3289 3325
    typedef typename GR::Arc Key;
3290 3326
    ///\e
3291 3327
    typedef typename GR::Node Value;
3292 3328

	
3293 3329
    /// \brief Constructor
3294 3330
    ///
3295 3331
    /// Constructor.
3296 3332
    /// \param digraph The digraph that the map belongs to.
3297 3333
    explicit TargetMap(const GR& digraph) : _graph(digraph) {}
3298 3334

	
3299 3335
    /// \brief Returns the target node of the given arc.
3300 3336
    ///
3301 3337
    /// Returns the target node of the given arc.
3302 3338
    Value operator[](const Key& e) const {
3303 3339
      return _graph.target(e);
3304 3340
    }
3305 3341

	
3306 3342
  private:
3307 3343
    const GR& _graph;
3308 3344
  };
3309 3345

	
3310 3346
  /// \brief Returns a \c TargetMap class.
3311 3347
  ///
3312 3348
  /// This function just returns a \c TargetMap class.
3313 3349
  /// \relates TargetMap
3314 3350
  template <typename GR>
3315 3351
  inline TargetMap<GR> targetMap(const GR& graph) {
3316 3352
    return TargetMap<GR>(graph);
0 comments (0 inline)