lemon/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 25 Sep 2009 09:33:09 +0200
changeset 719 ba79e8d64448
parent 716 f47b6c94577e
parent 695 8dae88c5943e
child 726 3fc2a801c39e
permissions -rw-r--r--
Merge
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@25
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@25
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
alpar@25
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@25
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@25
     8
 *
alpar@25
     9
 * Permission to use, modify and distribute this software is granted
alpar@25
    10
 * provided that this copyright notice appears in all copies. For
alpar@25
    11
 * precise terms see the accompanying LICENSE file.
alpar@25
    12
 *
alpar@25
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@25
    14
 * express or implied, and with no claim as to its suitability for any
alpar@25
    15
 * purpose.
alpar@25
    16
 *
alpar@25
    17
 */
alpar@25
    18
alpar@25
    19
#ifndef LEMON_MAPS_H
alpar@25
    20
#define LEMON_MAPS_H
alpar@25
    21
alpar@25
    22
#include <iterator>
alpar@25
    23
#include <functional>
alpar@25
    24
#include <vector>
kpeter@694
    25
#include <map>
alpar@25
    26
deba@220
    27
#include <lemon/core.h>
alpar@25
    28
alpar@25
    29
///\file
alpar@25
    30
///\ingroup maps
alpar@25
    31
///\brief Miscellaneous property maps
kpeter@80
    32
alpar@25
    33
namespace lemon {
alpar@25
    34
alpar@25
    35
  /// \addtogroup maps
alpar@25
    36
  /// @{
alpar@25
    37
alpar@25
    38
  /// Base class of maps.
alpar@25
    39
kpeter@80
    40
  /// Base class of maps. It provides the necessary type definitions
kpeter@80
    41
  /// required by the map %concepts.
kpeter@80
    42
  template<typename K, typename V>
alpar@25
    43
  class MapBase {
alpar@25
    44
  public:
kpeter@313
    45
    /// \brief The key type of the map.
alpar@25
    46
    typedef K Key;
kpeter@80
    47
    /// \brief The value type of the map.
kpeter@80
    48
    /// (The type of objects associated with the keys).
kpeter@80
    49
    typedef V Value;
alpar@25
    50
  };
alpar@25
    51
kpeter@80
    52
alpar@25
    53
  /// Null map. (a.k.a. DoNothingMap)
alpar@25
    54
kpeter@29
    55
  /// This map can be used if you have to provide a map only for
kpeter@80
    56
  /// its type definitions, or if you have to provide a writable map,
kpeter@80
    57
  /// but data written to it is not required (i.e. it will be sent to
kpeter@29
    58
  /// <tt>/dev/null</tt>).
kpeter@80
    59
  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
kpeter@80
    60
  ///
kpeter@80
    61
  /// \sa ConstMap
kpeter@80
    62
  template<typename K, typename V>
kpeter@80
    63
  class NullMap : public MapBase<K, V> {
alpar@25
    64
  public:
kpeter@559
    65
    ///\e
kpeter@559
    66
    typedef K Key;
kpeter@559
    67
    ///\e
kpeter@559
    68
    typedef V Value;
kpeter@80
    69
alpar@25
    70
    /// Gives back a default constructed element.
kpeter@80
    71
    Value operator[](const Key&) const { return Value(); }
alpar@25
    72
    /// Absorbs the value.
kpeter@80
    73
    void set(const Key&, const Value&) {}
alpar@25
    74
  };
alpar@25
    75
kpeter@301
    76
  /// Returns a \c NullMap class
kpeter@301
    77
kpeter@301
    78
  /// This function just returns a \c NullMap class.
kpeter@80
    79
  /// \relates NullMap
kpeter@80
    80
  template <typename K, typename V>
alpar@25
    81
  NullMap<K, V> nullMap() {
alpar@25
    82
    return NullMap<K, V>();
alpar@25
    83
  }
alpar@25
    84
alpar@25
    85
alpar@25
    86
  /// Constant map.
alpar@25
    87
kpeter@82
    88
  /// This \ref concepts::ReadMap "readable map" assigns a specified
kpeter@82
    89
  /// value to each key.
kpeter@80
    90
  ///
kpeter@301
    91
  /// In other aspects it is equivalent to \c NullMap.
kpeter@80
    92
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@80
    93
  /// concept, but it absorbs the data written to it.
kpeter@80
    94
  ///
kpeter@80
    95
  /// The simplest way of using this map is through the constMap()
kpeter@80
    96
  /// function.
kpeter@80
    97
  ///
kpeter@80
    98
  /// \sa NullMap
kpeter@80
    99
  /// \sa IdentityMap
kpeter@80
   100
  template<typename K, typename V>
kpeter@80
   101
  class ConstMap : public MapBase<K, V> {
alpar@25
   102
  private:
kpeter@80
   103
    V _value;
alpar@25
   104
  public:
kpeter@559
   105
    ///\e
kpeter@559
   106
    typedef K Key;
kpeter@559
   107
    ///\e
kpeter@559
   108
    typedef V Value;
alpar@25
   109
alpar@25
   110
    /// Default constructor
alpar@25
   111
kpeter@29
   112
    /// Default constructor.
kpeter@80
   113
    /// The value of the map will be default constructed.
alpar@25
   114
    ConstMap() {}
kpeter@80
   115
kpeter@29
   116
    /// Constructor with specified initial value
alpar@25
   117
kpeter@29
   118
    /// Constructor with specified initial value.
kpeter@123
   119
    /// \param v The initial value of the map.
kpeter@80
   120
    ConstMap(const Value &v) : _value(v) {}
alpar@25
   121
kpeter@80
   122
    /// Gives back the specified value.
kpeter@80
   123
    Value operator[](const Key&) const { return _value; }
alpar@25
   124
kpeter@80
   125
    /// Absorbs the value.
kpeter@80
   126
    void set(const Key&, const Value&) {}
kpeter@80
   127
kpeter@80
   128
    /// Sets the value that is assigned to each key.
kpeter@80
   129
    void setAll(const Value &v) {
kpeter@80
   130
      _value = v;
kpeter@80
   131
    }
kpeter@80
   132
kpeter@80
   133
    template<typename V1>
kpeter@80
   134
    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
alpar@25
   135
  };
alpar@25
   136
kpeter@301
   137
  /// Returns a \c ConstMap class
kpeter@301
   138
kpeter@301
   139
  /// This function just returns a \c ConstMap class.
kpeter@80
   140
  /// \relates ConstMap
kpeter@80
   141
  template<typename K, typename V>
alpar@25
   142
  inline ConstMap<K, V> constMap(const V &v) {
alpar@25
   143
    return ConstMap<K, V>(v);
alpar@25
   144
  }
alpar@25
   145
kpeter@123
   146
  template<typename K, typename V>
kpeter@123
   147
  inline ConstMap<K, V> constMap() {
kpeter@123
   148
    return ConstMap<K, V>();
kpeter@123
   149
  }
kpeter@123
   150
alpar@25
   151
alpar@25
   152
  template<typename T, T v>
kpeter@80
   153
  struct Const {};
alpar@25
   154
alpar@25
   155
  /// Constant map with inlined constant value.
alpar@25
   156
kpeter@82
   157
  /// This \ref concepts::ReadMap "readable map" assigns a specified
kpeter@82
   158
  /// value to each key.
kpeter@80
   159
  ///
kpeter@301
   160
  /// In other aspects it is equivalent to \c NullMap.
kpeter@80
   161
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@80
   162
  /// concept, but it absorbs the data written to it.
kpeter@80
   163
  ///
kpeter@80
   164
  /// The simplest way of using this map is through the constMap()
kpeter@80
   165
  /// function.
kpeter@80
   166
  ///
kpeter@80
   167
  /// \sa NullMap
kpeter@80
   168
  /// \sa IdentityMap
alpar@25
   169
  template<typename K, typename V, V v>
alpar@25
   170
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
alpar@25
   171
  public:
kpeter@559
   172
    ///\e
kpeter@559
   173
    typedef K Key;
kpeter@559
   174
    ///\e
kpeter@559
   175
    typedef V Value;
alpar@25
   176
kpeter@80
   177
    /// Constructor.
kpeter@80
   178
    ConstMap() {}
kpeter@80
   179
kpeter@80
   180
    /// Gives back the specified value.
kpeter@80
   181
    Value operator[](const Key&) const { return v; }
kpeter@80
   182
kpeter@80
   183
    /// Absorbs the value.
kpeter@80
   184
    void set(const Key&, const Value&) {}
alpar@25
   185
  };
alpar@25
   186
kpeter@301
   187
  /// Returns a \c ConstMap class with inlined constant value
kpeter@301
   188
kpeter@301
   189
  /// This function just returns a \c ConstMap class with inlined
kpeter@80
   190
  /// constant value.
kpeter@80
   191
  /// \relates ConstMap
kpeter@80
   192
  template<typename K, typename V, V v>
alpar@25
   193
  inline ConstMap<K, Const<V, v> > constMap() {
alpar@25
   194
    return ConstMap<K, Const<V, v> >();
alpar@25
   195
  }
alpar@25
   196
alpar@25
   197
kpeter@82
   198
  /// Identity map.
kpeter@82
   199
kpeter@82
   200
  /// This \ref concepts::ReadMap "read-only map" gives back the given
kpeter@82
   201
  /// key as value without any modification.
kpeter@80
   202
  ///
kpeter@80
   203
  /// \sa ConstMap
kpeter@80
   204
  template <typename T>
kpeter@80
   205
  class IdentityMap : public MapBase<T, T> {
kpeter@80
   206
  public:
kpeter@559
   207
    ///\e
kpeter@559
   208
    typedef T Key;
kpeter@559
   209
    ///\e
kpeter@559
   210
    typedef T Value;
kpeter@80
   211
kpeter@80
   212
    /// Gives back the given value without any modification.
kpeter@82
   213
    Value operator[](const Key &k) const {
kpeter@82
   214
      return k;
kpeter@80
   215
    }
kpeter@80
   216
  };
kpeter@80
   217
kpeter@301
   218
  /// Returns an \c IdentityMap class
kpeter@301
   219
kpeter@301
   220
  /// This function just returns an \c IdentityMap class.
kpeter@80
   221
  /// \relates IdentityMap
kpeter@80
   222
  template<typename T>
kpeter@80
   223
  inline IdentityMap<T> identityMap() {
kpeter@80
   224
    return IdentityMap<T>();
kpeter@80
   225
  }
kpeter@80
   226
kpeter@80
   227
kpeter@80
   228
  /// \brief Map for storing values for integer keys from the range
kpeter@80
   229
  /// <tt>[0..size-1]</tt>.
kpeter@80
   230
  ///
kpeter@80
   231
  /// This map is essentially a wrapper for \c std::vector. It assigns
kpeter@80
   232
  /// values to integer keys from the range <tt>[0..size-1]</tt>.
kpeter@80
   233
  /// It can be used with some data structures, for example
kpeter@301
   234
  /// \c UnionFind, \c BinHeap, when the used items are small
kpeter@80
   235
  /// integers. This map conforms the \ref concepts::ReferenceMap
kpeter@80
   236
  /// "ReferenceMap" concept.
kpeter@80
   237
  ///
kpeter@80
   238
  /// The simplest way of using this map is through the rangeMap()
kpeter@80
   239
  /// function.
kpeter@80
   240
  template <typename V>
kpeter@80
   241
  class RangeMap : public MapBase<int, V> {
kpeter@80
   242
    template <typename V1>
kpeter@80
   243
    friend class RangeMap;
kpeter@80
   244
  private:
kpeter@80
   245
kpeter@80
   246
    typedef std::vector<V> Vector;
kpeter@80
   247
    Vector _vector;
kpeter@80
   248
alpar@25
   249
  public:
alpar@25
   250
kpeter@80
   251
    /// Key type
kpeter@559
   252
    typedef int Key;
kpeter@80
   253
    /// Value type
kpeter@559
   254
    typedef V Value;
kpeter@80
   255
    /// Reference type
kpeter@80
   256
    typedef typename Vector::reference Reference;
kpeter@80
   257
    /// Const reference type
kpeter@80
   258
    typedef typename Vector::const_reference ConstReference;
kpeter@80
   259
kpeter@80
   260
    typedef True ReferenceMapTag;
kpeter@80
   261
kpeter@80
   262
  public:
kpeter@80
   263
kpeter@80
   264
    /// Constructor with specified default value.
kpeter@80
   265
    RangeMap(int size = 0, const Value &value = Value())
kpeter@80
   266
      : _vector(size, value) {}
kpeter@80
   267
kpeter@80
   268
    /// Constructs the map from an appropriate \c std::vector.
kpeter@80
   269
    template <typename V1>
kpeter@80
   270
    RangeMap(const std::vector<V1>& vector)
kpeter@80
   271
      : _vector(vector.begin(), vector.end()) {}
kpeter@80
   272
kpeter@301
   273
    /// Constructs the map from another \c RangeMap.
kpeter@80
   274
    template <typename V1>
kpeter@80
   275
    RangeMap(const RangeMap<V1> &c)
kpeter@80
   276
      : _vector(c._vector.begin(), c._vector.end()) {}
kpeter@80
   277
kpeter@80
   278
    /// Returns the size of the map.
kpeter@80
   279
    int size() {
kpeter@80
   280
      return _vector.size();
kpeter@80
   281
    }
kpeter@80
   282
kpeter@80
   283
    /// Resizes the map.
kpeter@80
   284
kpeter@80
   285
    /// Resizes the underlying \c std::vector container, so changes the
kpeter@80
   286
    /// keyset of the map.
kpeter@80
   287
    /// \param size The new size of the map. The new keyset will be the
kpeter@80
   288
    /// range <tt>[0..size-1]</tt>.
kpeter@80
   289
    /// \param value The default value to assign to the new keys.
kpeter@80
   290
    void resize(int size, const Value &value = Value()) {
kpeter@80
   291
      _vector.resize(size, value);
kpeter@80
   292
    }
kpeter@80
   293
kpeter@80
   294
  private:
kpeter@80
   295
kpeter@80
   296
    RangeMap& operator=(const RangeMap&);
kpeter@80
   297
kpeter@80
   298
  public:
kpeter@80
   299
kpeter@80
   300
    ///\e
kpeter@80
   301
    Reference operator[](const Key &k) {
kpeter@80
   302
      return _vector[k];
kpeter@80
   303
    }
kpeter@80
   304
kpeter@80
   305
    ///\e
kpeter@80
   306
    ConstReference operator[](const Key &k) const {
kpeter@80
   307
      return _vector[k];
kpeter@80
   308
    }
kpeter@80
   309
kpeter@80
   310
    ///\e
kpeter@80
   311
    void set(const Key &k, const Value &v) {
kpeter@80
   312
      _vector[k] = v;
kpeter@80
   313
    }
kpeter@80
   314
  };
kpeter@80
   315
kpeter@301
   316
  /// Returns a \c RangeMap class
kpeter@301
   317
kpeter@301
   318
  /// This function just returns a \c RangeMap class.
kpeter@80
   319
  /// \relates RangeMap
kpeter@80
   320
  template<typename V>
kpeter@80
   321
  inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
kpeter@80
   322
    return RangeMap<V>(size, value);
kpeter@80
   323
  }
kpeter@80
   324
kpeter@301
   325
  /// \brief Returns a \c RangeMap class created from an appropriate
kpeter@80
   326
  /// \c std::vector
kpeter@80
   327
kpeter@301
   328
  /// This function just returns a \c RangeMap class created from an
kpeter@80
   329
  /// appropriate \c std::vector.
kpeter@80
   330
  /// \relates RangeMap
kpeter@80
   331
  template<typename V>
kpeter@80
   332
  inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
kpeter@80
   333
    return RangeMap<V>(vector);
kpeter@80
   334
  }
kpeter@80
   335
kpeter@80
   336
kpeter@80
   337
  /// Map type based on \c std::map
kpeter@80
   338
kpeter@80
   339
  /// This map is essentially a wrapper for \c std::map with addition
kpeter@80
   340
  /// that you can specify a default value for the keys that are not
kpeter@80
   341
  /// stored actually. This value can be different from the default
kpeter@80
   342
  /// contructed value (i.e. \c %Value()).
kpeter@80
   343
  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
kpeter@80
   344
  /// concept.
kpeter@80
   345
  ///
kpeter@80
   346
  /// This map is useful if a default value should be assigned to most of
kpeter@80
   347
  /// the keys and different values should be assigned only to a few
kpeter@80
   348
  /// keys (i.e. the map is "sparse").
kpeter@80
   349
  /// The name of this type also refers to this important usage.
kpeter@80
   350
  ///
kpeter@80
   351
  /// Apart form that this map can be used in many other cases since it
kpeter@80
   352
  /// is based on \c std::map, which is a general associative container.
kpeter@80
   353
  /// However keep in mind that it is usually not as efficient as other
kpeter@80
   354
  /// maps.
kpeter@80
   355
  ///
kpeter@80
   356
  /// The simplest way of using this map is through the sparseMap()
kpeter@80
   357
  /// function.
kpeter@559
   358
  template <typename K, typename V, typename Comp = std::less<K> >
kpeter@80
   359
  class SparseMap : public MapBase<K, V> {
kpeter@80
   360
    template <typename K1, typename V1, typename C1>
kpeter@80
   361
    friend class SparseMap;
kpeter@80
   362
  public:
kpeter@80
   363
kpeter@80
   364
    /// Key type
kpeter@559
   365
    typedef K Key;
kpeter@80
   366
    /// Value type
kpeter@559
   367
    typedef V Value;
kpeter@80
   368
    /// Reference type
kpeter@80
   369
    typedef Value& Reference;
kpeter@80
   370
    /// Const reference type
kpeter@80
   371
    typedef const Value& ConstReference;
alpar@25
   372
kpeter@45
   373
    typedef True ReferenceMapTag;
kpeter@45
   374
alpar@25
   375
  private:
kpeter@80
   376
kpeter@559
   377
    typedef std::map<K, V, Comp> Map;
kpeter@80
   378
    Map _map;
alpar@25
   379
    Value _value;
alpar@25
   380
alpar@25
   381
  public:
alpar@25
   382
kpeter@80
   383
    /// \brief Constructor with specified default value.
kpeter@80
   384
    SparseMap(const Value &value = Value()) : _value(value) {}
kpeter@80
   385
    /// \brief Constructs the map from an appropriate \c std::map, and
kpeter@47
   386
    /// explicitly specifies a default value.
kpeter@80
   387
    template <typename V1, typename Comp1>
kpeter@80
   388
    SparseMap(const std::map<Key, V1, Comp1> &map,
kpeter@80
   389
              const Value &value = Value())
alpar@25
   390
      : _map(map.begin(), map.end()), _value(value) {}
kpeter@80
   391
kpeter@301
   392
    /// \brief Constructs the map from another \c SparseMap.
kpeter@80
   393
    template<typename V1, typename Comp1>
kpeter@80
   394
    SparseMap(const SparseMap<Key, V1, Comp1> &c)
alpar@25
   395
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
alpar@25
   396
alpar@25
   397
  private:
alpar@25
   398
kpeter@80
   399
    SparseMap& operator=(const SparseMap&);
alpar@25
   400
alpar@25
   401
  public:
alpar@25
   402
alpar@25
   403
    ///\e
alpar@25
   404
    Reference operator[](const Key &k) {
alpar@25
   405
      typename Map::iterator it = _map.lower_bound(k);
alpar@25
   406
      if (it != _map.end() && !_map.key_comp()(k, it->first))
alpar@209
   407
        return it->second;
alpar@25
   408
      else
alpar@209
   409
        return _map.insert(it, std::make_pair(k, _value))->second;
alpar@25
   410
    }
alpar@25
   411
kpeter@80
   412
    ///\e
alpar@25
   413
    ConstReference operator[](const Key &k) const {
alpar@25
   414
      typename Map::const_iterator it = _map.find(k);
alpar@25
   415
      if (it != _map.end())
alpar@209
   416
        return it->second;
alpar@25
   417
      else
alpar@209
   418
        return _value;
alpar@25
   419
    }
alpar@25
   420
kpeter@80
   421
    ///\e
kpeter@80
   422
    void set(const Key &k, const Value &v) {
alpar@25
   423
      typename Map::iterator it = _map.lower_bound(k);
alpar@25
   424
      if (it != _map.end() && !_map.key_comp()(k, it->first))
alpar@209
   425
        it->second = v;
alpar@25
   426
      else
alpar@209
   427
        _map.insert(it, std::make_pair(k, v));
alpar@25
   428
    }
alpar@25
   429
kpeter@80
   430
    ///\e
kpeter@80
   431
    void setAll(const Value &v) {
kpeter@80
   432
      _value = v;
alpar@25
   433
      _map.clear();
kpeter@80
   434
    }
kpeter@80
   435
  };
alpar@25
   436
kpeter@301
   437
  /// Returns a \c SparseMap class
kpeter@301
   438
kpeter@301
   439
  /// This function just returns a \c SparseMap class with specified
kpeter@80
   440
  /// default value.
kpeter@80
   441
  /// \relates SparseMap
kpeter@80
   442
  template<typename K, typename V, typename Compare>
kpeter@80
   443
  inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
kpeter@80
   444
    return SparseMap<K, V, Compare>(value);
kpeter@54
   445
  }
kpeter@45
   446
kpeter@80
   447
  template<typename K, typename V>
kpeter@80
   448
  inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
kpeter@80
   449
    return SparseMap<K, V, std::less<K> >(value);
kpeter@45
   450
  }
alpar@25
   451
kpeter@301
   452
  /// \brief Returns a \c SparseMap class created from an appropriate
kpeter@80
   453
  /// \c std::map
alpar@25
   454
kpeter@301
   455
  /// This function just returns a \c SparseMap class created from an
kpeter@80
   456
  /// appropriate \c std::map.
kpeter@80
   457
  /// \relates SparseMap
kpeter@80
   458
  template<typename K, typename V, typename Compare>
kpeter@80
   459
  inline SparseMap<K, V, Compare>
kpeter@80
   460
    sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
kpeter@80
   461
  {
kpeter@80
   462
    return SparseMap<K, V, Compare>(map, value);
kpeter@45
   463
  }
alpar@25
   464
alpar@25
   465
  /// @}
alpar@25
   466
alpar@25
   467
  /// \addtogroup map_adaptors
alpar@25
   468
  /// @{
alpar@25
   469
kpeter@80
   470
  /// Composition of two maps
kpeter@80
   471
kpeter@82
   472
  /// This \ref concepts::ReadMap "read-only map" returns the
kpeter@80
   473
  /// composition of two given maps. That is to say, if \c m1 is of
kpeter@80
   474
  /// type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   475
  /// \code
kpeter@80
   476
  ///   ComposeMap<M1, M2> cm(m1,m2);
kpeter@80
   477
  /// \endcode
kpeter@80
   478
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
alpar@25
   479
  ///
kpeter@80
   480
  /// The \c Key type of the map is inherited from \c M2 and the
kpeter@80
   481
  /// \c Value type is from \c M1.
kpeter@80
   482
  /// \c M2::Value must be convertible to \c M1::Key.
kpeter@80
   483
  ///
kpeter@80
   484
  /// The simplest way of using this map is through the composeMap()
kpeter@80
   485
  /// function.
kpeter@80
   486
  ///
kpeter@80
   487
  /// \sa CombineMap
kpeter@80
   488
  template <typename M1, typename M2>
kpeter@80
   489
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
kpeter@80
   490
    const M1 &_m1;
kpeter@80
   491
    const M2 &_m2;
alpar@25
   492
  public:
kpeter@559
   493
    ///\e
kpeter@559
   494
    typedef typename M2::Key Key;
kpeter@559
   495
    ///\e
kpeter@559
   496
    typedef typename M1::Value Value;
alpar@25
   497
kpeter@80
   498
    /// Constructor
kpeter@80
   499
    ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   500
kpeter@559
   501
    ///\e
kpeter@80
   502
    typename MapTraits<M1>::ConstReturnValue
kpeter@80
   503
    operator[](const Key &k) const { return _m1[_m2[k]]; }
alpar@25
   504
  };
alpar@25
   505
kpeter@301
   506
  /// Returns a \c ComposeMap class
kpeter@301
   507
kpeter@301
   508
  /// This function just returns a \c ComposeMap class.
kpeter@80
   509
  ///
kpeter@80
   510
  /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
kpeter@80
   511
  /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
kpeter@80
   512
  /// will be equal to <tt>m1[m2[x]]</tt>.
kpeter@80
   513
  ///
kpeter@80
   514
  /// \relates ComposeMap
kpeter@80
   515
  template <typename M1, typename M2>
kpeter@80
   516
  inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
kpeter@80
   517
    return ComposeMap<M1, M2>(m1, m2);
alpar@25
   518
  }
alpar@25
   519
kpeter@80
   520
kpeter@80
   521
  /// Combination of two maps using an STL (binary) functor.
kpeter@80
   522
kpeter@82
   523
  /// This \ref concepts::ReadMap "read-only map" takes two maps and a
kpeter@80
   524
  /// binary functor and returns the combination of the two given maps
kpeter@80
   525
  /// using the functor.
kpeter@80
   526
  /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
kpeter@80
   527
  /// and \c f is of \c F, then for
kpeter@80
   528
  /// \code
kpeter@80
   529
  ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
kpeter@80
   530
  /// \endcode
kpeter@80
   531
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
alpar@26
   532
  ///
kpeter@80
   533
  /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
kpeter@80
   534
  /// must be convertible to \c M2::Key) and the \c Value type is \c V.
kpeter@80
   535
  /// \c M2::Value and \c M1::Value must be convertible to the
kpeter@80
   536
  /// corresponding input parameter of \c F and the return type of \c F
kpeter@80
   537
  /// must be convertible to \c V.
kpeter@80
   538
  ///
kpeter@80
   539
  /// The simplest way of using this map is through the combineMap()
kpeter@80
   540
  /// function.
kpeter@80
   541
  ///
kpeter@80
   542
  /// \sa ComposeMap
kpeter@80
   543
  template<typename M1, typename M2, typename F,
alpar@209
   544
           typename V = typename F::result_type>
kpeter@80
   545
  class CombineMap : public MapBase<typename M1::Key, V> {
kpeter@80
   546
    const M1 &_m1;
kpeter@80
   547
    const M2 &_m2;
kpeter@80
   548
    F _f;
alpar@25
   549
  public:
kpeter@559
   550
    ///\e
kpeter@559
   551
    typedef typename M1::Key Key;
kpeter@559
   552
    ///\e
kpeter@559
   553
    typedef V Value;
alpar@25
   554
kpeter@80
   555
    /// Constructor
kpeter@80
   556
    CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
kpeter@80
   557
      : _m1(m1), _m2(m2), _f(f) {}
kpeter@559
   558
    ///\e
kpeter@80
   559
    Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
kpeter@80
   560
  };
alpar@25
   561
kpeter@301
   562
  /// Returns a \c CombineMap class
kpeter@301
   563
kpeter@301
   564
  /// This function just returns a \c CombineMap class.
kpeter@80
   565
  ///
kpeter@80
   566
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   567
  /// values, then
kpeter@80
   568
  /// \code
kpeter@80
   569
  ///   combineMap(m1,m2,std::plus<double>())
kpeter@80
   570
  /// \endcode
kpeter@80
   571
  /// is equivalent to
kpeter@80
   572
  /// \code
kpeter@80
   573
  ///   addMap(m1,m2)
kpeter@80
   574
  /// \endcode
kpeter@80
   575
  ///
kpeter@80
   576
  /// This function is specialized for adaptable binary function
kpeter@80
   577
  /// classes and C++ functions.
kpeter@80
   578
  ///
kpeter@80
   579
  /// \relates CombineMap
kpeter@80
   580
  template<typename M1, typename M2, typename F, typename V>
kpeter@80
   581
  inline CombineMap<M1, M2, F, V>
kpeter@80
   582
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
kpeter@80
   583
    return CombineMap<M1, M2, F, V>(m1,m2,f);
alpar@25
   584
  }
alpar@25
   585
kpeter@80
   586
  template<typename M1, typename M2, typename F>
kpeter@80
   587
  inline CombineMap<M1, M2, F, typename F::result_type>
kpeter@80
   588
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
kpeter@80
   589
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
kpeter@80
   590
  }
alpar@25
   591
kpeter@80
   592
  template<typename M1, typename M2, typename K1, typename K2, typename V>
kpeter@80
   593
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
kpeter@80
   594
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
kpeter@80
   595
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
kpeter@80
   596
  }
kpeter@80
   597
kpeter@80
   598
kpeter@80
   599
  /// Converts an STL style (unary) functor to a map
kpeter@80
   600
kpeter@82
   601
  /// This \ref concepts::ReadMap "read-only map" returns the value
kpeter@80
   602
  /// of a given functor. Actually, it just wraps the functor and
kpeter@80
   603
  /// provides the \c Key and \c Value typedefs.
alpar@26
   604
  ///
kpeter@80
   605
  /// Template parameters \c K and \c V will become its \c Key and
kpeter@80
   606
  /// \c Value. In most cases they have to be given explicitly because
kpeter@80
   607
  /// a functor typically does not provide \c argument_type and
kpeter@80
   608
  /// \c result_type typedefs.
kpeter@80
   609
  /// Parameter \c F is the type of the used functor.
kpeter@29
   610
  ///
kpeter@80
   611
  /// The simplest way of using this map is through the functorToMap()
kpeter@80
   612
  /// function.
kpeter@80
   613
  ///
kpeter@80
   614
  /// \sa MapToFunctor
kpeter@80
   615
  template<typename F,
alpar@209
   616
           typename K = typename F::argument_type,
alpar@209
   617
           typename V = typename F::result_type>
kpeter@80
   618
  class FunctorToMap : public MapBase<K, V> {
kpeter@123
   619
    F _f;
kpeter@80
   620
  public:
kpeter@559
   621
    ///\e
kpeter@559
   622
    typedef K Key;
kpeter@559
   623
    ///\e
kpeter@559
   624
    typedef V Value;
alpar@25
   625
kpeter@80
   626
    /// Constructor
kpeter@80
   627
    FunctorToMap(const F &f = F()) : _f(f) {}
kpeter@559
   628
    ///\e
kpeter@80
   629
    Value operator[](const Key &k) const { return _f(k); }
kpeter@80
   630
  };
kpeter@80
   631
kpeter@301
   632
  /// Returns a \c FunctorToMap class
kpeter@301
   633
kpeter@301
   634
  /// This function just returns a \c FunctorToMap class.
kpeter@80
   635
  ///
kpeter@80
   636
  /// This function is specialized for adaptable binary function
kpeter@80
   637
  /// classes and C++ functions.
kpeter@80
   638
  ///
kpeter@80
   639
  /// \relates FunctorToMap
kpeter@80
   640
  template<typename K, typename V, typename F>
kpeter@80
   641
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
kpeter@80
   642
    return FunctorToMap<F, K, V>(f);
kpeter@80
   643
  }
kpeter@80
   644
kpeter@80
   645
  template <typename F>
kpeter@80
   646
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
kpeter@80
   647
    functorToMap(const F &f)
kpeter@80
   648
  {
kpeter@80
   649
    return FunctorToMap<F, typename F::argument_type,
kpeter@80
   650
      typename F::result_type>(f);
kpeter@80
   651
  }
kpeter@80
   652
kpeter@80
   653
  template <typename K, typename V>
kpeter@80
   654
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
kpeter@80
   655
    return FunctorToMap<V (*)(K), K, V>(f);
kpeter@80
   656
  }
kpeter@80
   657
kpeter@80
   658
kpeter@80
   659
  /// Converts a map to an STL style (unary) functor
kpeter@80
   660
kpeter@80
   661
  /// This class converts a map to an STL style (unary) functor.
kpeter@80
   662
  /// That is it provides an <tt>operator()</tt> to read its values.
kpeter@80
   663
  ///
kpeter@80
   664
  /// For the sake of convenience it also works as a usual
kpeter@80
   665
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
kpeter@80
   666
  /// and the \c Key and \c Value typedefs also exist.
kpeter@80
   667
  ///
kpeter@80
   668
  /// The simplest way of using this map is through the mapToFunctor()
kpeter@80
   669
  /// function.
kpeter@80
   670
  ///
kpeter@80
   671
  ///\sa FunctorToMap
kpeter@80
   672
  template <typename M>
kpeter@80
   673
  class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
   674
    const M &_m;
alpar@25
   675
  public:
kpeter@559
   676
    ///\e
kpeter@559
   677
    typedef typename M::Key Key;
kpeter@559
   678
    ///\e
kpeter@559
   679
    typedef typename M::Value Value;
kpeter@559
   680
kpeter@559
   681
    typedef typename M::Key argument_type;
kpeter@559
   682
    typedef typename M::Value result_type;
kpeter@80
   683
kpeter@80
   684
    /// Constructor
kpeter@80
   685
    MapToFunctor(const M &m) : _m(m) {}
kpeter@559
   686
    ///\e
kpeter@80
   687
    Value operator()(const Key &k) const { return _m[k]; }
kpeter@559
   688
    ///\e
kpeter@80
   689
    Value operator[](const Key &k) const { return _m[k]; }
alpar@25
   690
  };
kpeter@45
   691
kpeter@301
   692
  /// Returns a \c MapToFunctor class
kpeter@301
   693
kpeter@301
   694
  /// This function just returns a \c MapToFunctor class.
kpeter@80
   695
  /// \relates MapToFunctor
kpeter@45
   696
  template<typename M>
kpeter@80
   697
  inline MapToFunctor<M> mapToFunctor(const M &m) {
kpeter@80
   698
    return MapToFunctor<M>(m);
kpeter@45
   699
  }
alpar@25
   700
alpar@25
   701
kpeter@80
   702
  /// \brief Map adaptor to convert the \c Value type of a map to
kpeter@80
   703
  /// another type using the default conversion.
kpeter@80
   704
kpeter@80
   705
  /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
kpeter@80
   706
  /// "readable map" to another type using the default conversion.
kpeter@80
   707
  /// The \c Key type of it is inherited from \c M and the \c Value
kpeter@80
   708
  /// type is \c V.
kpeter@80
   709
  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
alpar@26
   710
  ///
kpeter@80
   711
  /// The simplest way of using this map is through the convertMap()
kpeter@80
   712
  /// function.
kpeter@80
   713
  template <typename M, typename V>
kpeter@80
   714
  class ConvertMap : public MapBase<typename M::Key, V> {
kpeter@80
   715
    const M &_m;
kpeter@80
   716
  public:
kpeter@559
   717
    ///\e
kpeter@559
   718
    typedef typename M::Key Key;
kpeter@559
   719
    ///\e
kpeter@559
   720
    typedef V Value;
kpeter@80
   721
kpeter@80
   722
    /// Constructor
kpeter@80
   723
kpeter@80
   724
    /// Constructor.
kpeter@80
   725
    /// \param m The underlying map.
kpeter@80
   726
    ConvertMap(const M &m) : _m(m) {}
kpeter@80
   727
kpeter@559
   728
    ///\e
kpeter@80
   729
    Value operator[](const Key &k) const { return _m[k]; }
kpeter@80
   730
  };
kpeter@80
   731
kpeter@301
   732
  /// Returns a \c ConvertMap class
kpeter@301
   733
kpeter@301
   734
  /// This function just returns a \c ConvertMap class.
kpeter@80
   735
  /// \relates ConvertMap
kpeter@80
   736
  template<typename V, typename M>
kpeter@80
   737
  inline ConvertMap<M, V> convertMap(const M &map) {
kpeter@80
   738
    return ConvertMap<M, V>(map);
kpeter@80
   739
  }
kpeter@80
   740
kpeter@80
   741
kpeter@80
   742
  /// Applies all map setting operations to two maps
kpeter@80
   743
kpeter@80
   744
  /// This map has two \ref concepts::WriteMap "writable map" parameters
kpeter@80
   745
  /// and each write request will be passed to both of them.
kpeter@80
   746
  /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
kpeter@80
   747
  /// operations will return the corresponding values of \c M1.
kpeter@29
   748
  ///
kpeter@80
   749
  /// The \c Key and \c Value types are inherited from \c M1.
kpeter@80
   750
  /// The \c Key and \c Value of \c M2 must be convertible from those
kpeter@80
   751
  /// of \c M1.
kpeter@80
   752
  ///
kpeter@80
   753
  /// The simplest way of using this map is through the forkMap()
kpeter@80
   754
  /// function.
kpeter@80
   755
  template<typename  M1, typename M2>
kpeter@80
   756
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   757
    M1 &_m1;
kpeter@80
   758
    M2 &_m2;
kpeter@80
   759
  public:
kpeter@559
   760
    ///\e
kpeter@559
   761
    typedef typename M1::Key Key;
kpeter@559
   762
    ///\e
kpeter@559
   763
    typedef typename M1::Value Value;
alpar@25
   764
kpeter@80
   765
    /// Constructor
kpeter@80
   766
    ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   767
    /// Returns the value associated with the given key in the first map.
kpeter@80
   768
    Value operator[](const Key &k) const { return _m1[k]; }
kpeter@80
   769
    /// Sets the value associated with the given key in both maps.
kpeter@80
   770
    void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
kpeter@80
   771
  };
kpeter@80
   772
kpeter@301
   773
  /// Returns a \c ForkMap class
kpeter@301
   774
kpeter@301
   775
  /// This function just returns a \c ForkMap class.
kpeter@80
   776
  /// \relates ForkMap
kpeter@80
   777
  template <typename M1, typename M2>
kpeter@80
   778
  inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
kpeter@80
   779
    return ForkMap<M1,M2>(m1,m2);
kpeter@80
   780
  }
kpeter@80
   781
kpeter@80
   782
kpeter@80
   783
  /// Sum of two maps
kpeter@80
   784
kpeter@82
   785
  /// This \ref concepts::ReadMap "read-only map" returns the sum
kpeter@80
   786
  /// of the values of the two given maps.
kpeter@80
   787
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   788
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   789
  /// \c M1.
kpeter@80
   790
  ///
kpeter@80
   791
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   792
  /// \code
kpeter@80
   793
  ///   AddMap<M1,M2> am(m1,m2);
kpeter@80
   794
  /// \endcode
kpeter@80
   795
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
kpeter@80
   796
  ///
kpeter@80
   797
  /// The simplest way of using this map is through the addMap()
kpeter@80
   798
  /// function.
kpeter@80
   799
  ///
kpeter@80
   800
  /// \sa SubMap, MulMap, DivMap
kpeter@80
   801
  /// \sa ShiftMap, ShiftWriteMap
kpeter@80
   802
  template<typename M1, typename M2>
alpar@25
   803
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   804
    const M1 &_m1;
kpeter@80
   805
    const M2 &_m2;
alpar@25
   806
  public:
kpeter@559
   807
    ///\e
kpeter@559
   808
    typedef typename M1::Key Key;
kpeter@559
   809
    ///\e
kpeter@559
   810
    typedef typename M1::Value Value;
alpar@25
   811
kpeter@80
   812
    /// Constructor
kpeter@80
   813
    AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
   814
    ///\e
kpeter@80
   815
    Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
alpar@25
   816
  };
alpar@25
   817
kpeter@301
   818
  /// Returns an \c AddMap class
kpeter@301
   819
kpeter@301
   820
  /// This function just returns an \c AddMap class.
alpar@25
   821
  ///
kpeter@80
   822
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   823
  /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   824
  /// <tt>m1[x]+m2[x]</tt>.
kpeter@80
   825
  ///
kpeter@80
   826
  /// \relates AddMap
kpeter@80
   827
  template<typename M1, typename M2>
kpeter@80
   828
  inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
alpar@25
   829
    return AddMap<M1, M2>(m1,m2);
alpar@25
   830
  }
alpar@25
   831
alpar@25
   832
kpeter@80
   833
  /// Difference of two maps
kpeter@80
   834
kpeter@82
   835
  /// This \ref concepts::ReadMap "read-only map" returns the difference
kpeter@80
   836
  /// of the values of the two given maps.
kpeter@80
   837
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   838
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   839
  /// \c M1.
alpar@25
   840
  ///
kpeter@80
   841
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   842
  /// \code
kpeter@80
   843
  ///   SubMap<M1,M2> sm(m1,m2);
kpeter@80
   844
  /// \endcode
kpeter@80
   845
  /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
kpeter@29
   846
  ///
kpeter@80
   847
  /// The simplest way of using this map is through the subMap()
kpeter@80
   848
  /// function.
kpeter@80
   849
  ///
kpeter@80
   850
  /// \sa AddMap, MulMap, DivMap
kpeter@80
   851
  template<typename M1, typename M2>
kpeter@80
   852
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   853
    const M1 &_m1;
kpeter@80
   854
    const M2 &_m2;
kpeter@80
   855
  public:
kpeter@559
   856
    ///\e
kpeter@559
   857
    typedef typename M1::Key Key;
kpeter@559
   858
    ///\e
kpeter@559
   859
    typedef typename M1::Value Value;
kpeter@80
   860
kpeter@80
   861
    /// Constructor
kpeter@80
   862
    SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
   863
    ///\e
kpeter@80
   864
    Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
kpeter@80
   865
  };
kpeter@80
   866
kpeter@301
   867
  /// Returns a \c SubMap class
kpeter@301
   868
kpeter@301
   869
  /// This function just returns a \c SubMap class.
kpeter@80
   870
  ///
kpeter@80
   871
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   872
  /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   873
  /// <tt>m1[x]-m2[x]</tt>.
kpeter@80
   874
  ///
kpeter@80
   875
  /// \relates SubMap
kpeter@80
   876
  template<typename M1, typename M2>
kpeter@80
   877
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
kpeter@80
   878
    return SubMap<M1, M2>(m1,m2);
kpeter@80
   879
  }
kpeter@80
   880
kpeter@80
   881
kpeter@80
   882
  /// Product of two maps
kpeter@80
   883
kpeter@82
   884
  /// This \ref concepts::ReadMap "read-only map" returns the product
kpeter@80
   885
  /// of the values of the two given maps.
kpeter@80
   886
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   887
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   888
  /// \c M1.
kpeter@80
   889
  ///
kpeter@80
   890
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   891
  /// \code
kpeter@80
   892
  ///   MulMap<M1,M2> mm(m1,m2);
kpeter@80
   893
  /// \endcode
kpeter@80
   894
  /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
kpeter@80
   895
  ///
kpeter@80
   896
  /// The simplest way of using this map is through the mulMap()
kpeter@80
   897
  /// function.
kpeter@80
   898
  ///
kpeter@80
   899
  /// \sa AddMap, SubMap, DivMap
kpeter@80
   900
  /// \sa ScaleMap, ScaleWriteMap
kpeter@80
   901
  template<typename M1, typename M2>
kpeter@80
   902
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   903
    const M1 &_m1;
kpeter@80
   904
    const M2 &_m2;
kpeter@80
   905
  public:
kpeter@559
   906
    ///\e
kpeter@559
   907
    typedef typename M1::Key Key;
kpeter@559
   908
    ///\e
kpeter@559
   909
    typedef typename M1::Value Value;
kpeter@80
   910
kpeter@80
   911
    /// Constructor
kpeter@80
   912
    MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
   913
    ///\e
kpeter@80
   914
    Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
kpeter@80
   915
  };
kpeter@80
   916
kpeter@301
   917
  /// Returns a \c MulMap class
kpeter@301
   918
kpeter@301
   919
  /// This function just returns a \c MulMap class.
kpeter@80
   920
  ///
kpeter@80
   921
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   922
  /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   923
  /// <tt>m1[x]*m2[x]</tt>.
kpeter@80
   924
  ///
kpeter@80
   925
  /// \relates MulMap
kpeter@80
   926
  template<typename M1, typename M2>
kpeter@80
   927
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
kpeter@80
   928
    return MulMap<M1, M2>(m1,m2);
kpeter@80
   929
  }
kpeter@80
   930
kpeter@80
   931
kpeter@80
   932
  /// Quotient of two maps
kpeter@80
   933
kpeter@82
   934
  /// This \ref concepts::ReadMap "read-only map" returns the quotient
kpeter@80
   935
  /// of the values of the two given maps.
kpeter@80
   936
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   937
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   938
  /// \c M1.
kpeter@80
   939
  ///
kpeter@80
   940
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   941
  /// \code
kpeter@80
   942
  ///   DivMap<M1,M2> dm(m1,m2);
kpeter@80
   943
  /// \endcode
kpeter@80
   944
  /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
kpeter@80
   945
  ///
kpeter@80
   946
  /// The simplest way of using this map is through the divMap()
kpeter@80
   947
  /// function.
kpeter@80
   948
  ///
kpeter@80
   949
  /// \sa AddMap, SubMap, MulMap
kpeter@80
   950
  template<typename M1, typename M2>
kpeter@80
   951
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   952
    const M1 &_m1;
kpeter@80
   953
    const M2 &_m2;
kpeter@80
   954
  public:
kpeter@559
   955
    ///\e
kpeter@559
   956
    typedef typename M1::Key Key;
kpeter@559
   957
    ///\e
kpeter@559
   958
    typedef typename M1::Value Value;
kpeter@80
   959
kpeter@80
   960
    /// Constructor
kpeter@80
   961
    DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
   962
    ///\e
kpeter@80
   963
    Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
kpeter@80
   964
  };
kpeter@80
   965
kpeter@301
   966
  /// Returns a \c DivMap class
kpeter@301
   967
kpeter@301
   968
  /// This function just returns a \c DivMap class.
kpeter@80
   969
  ///
kpeter@80
   970
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   971
  /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   972
  /// <tt>m1[x]/m2[x]</tt>.
kpeter@80
   973
  ///
kpeter@80
   974
  /// \relates DivMap
kpeter@80
   975
  template<typename M1, typename M2>
kpeter@80
   976
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
kpeter@80
   977
    return DivMap<M1, M2>(m1,m2);
kpeter@80
   978
  }
kpeter@80
   979
kpeter@80
   980
kpeter@80
   981
  /// Shifts a map with a constant.
kpeter@80
   982
kpeter@82
   983
  /// This \ref concepts::ReadMap "read-only map" returns the sum of
kpeter@80
   984
  /// the given map and a constant value (i.e. it shifts the map with
kpeter@80
   985
  /// the constant). Its \c Key and \c Value are inherited from \c M.
kpeter@80
   986
  ///
kpeter@80
   987
  /// Actually,
kpeter@80
   988
  /// \code
kpeter@80
   989
  ///   ShiftMap<M> sh(m,v);
kpeter@80
   990
  /// \endcode
kpeter@80
   991
  /// is equivalent to
kpeter@80
   992
  /// \code
kpeter@80
   993
  ///   ConstMap<M::Key, M::Value> cm(v);
kpeter@80
   994
  ///   AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
kpeter@80
   995
  /// \endcode
kpeter@80
   996
  ///
kpeter@80
   997
  /// The simplest way of using this map is through the shiftMap()
kpeter@80
   998
  /// function.
kpeter@80
   999
  ///
kpeter@80
  1000
  /// \sa ShiftWriteMap
kpeter@80
  1001
  template<typename M, typename C = typename M::Value>
alpar@25
  1002
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1003
    const M &_m;
kpeter@80
  1004
    C _v;
alpar@25
  1005
  public:
kpeter@559
  1006
    ///\e
kpeter@559
  1007
    typedef typename M::Key Key;
kpeter@559
  1008
    ///\e
kpeter@559
  1009
    typedef typename M::Value Value;
alpar@25
  1010
kpeter@80
  1011
    /// Constructor
alpar@25
  1012
kpeter@80
  1013
    /// Constructor.
kpeter@80
  1014
    /// \param m The undelying map.
kpeter@80
  1015
    /// \param v The constant value.
kpeter@80
  1016
    ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
kpeter@559
  1017
    ///\e
kpeter@80
  1018
    Value operator[](const Key &k) const { return _m[k]+_v; }
alpar@25
  1019
  };
alpar@25
  1020
kpeter@80
  1021
  /// Shifts a map with a constant (read-write version).
alpar@25
  1022
kpeter@80
  1023
  /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
kpeter@80
  1024
  /// of the given map and a constant value (i.e. it shifts the map with
kpeter@80
  1025
  /// the constant). Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1026
  /// It makes also possible to write the map.
alpar@25
  1027
  ///
kpeter@80
  1028
  /// The simplest way of using this map is through the shiftWriteMap()
kpeter@80
  1029
  /// function.
kpeter@80
  1030
  ///
kpeter@80
  1031
  /// \sa ShiftMap
kpeter@80
  1032
  template<typename M, typename C = typename M::Value>
alpar@25
  1033
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1034
    M &_m;
kpeter@80
  1035
    C _v;
alpar@25
  1036
  public:
kpeter@559
  1037
    ///\e
kpeter@559
  1038
    typedef typename M::Key Key;
kpeter@559
  1039
    ///\e
kpeter@559
  1040
    typedef typename M::Value Value;
alpar@25
  1041
kpeter@80
  1042
    /// Constructor
alpar@25
  1043
kpeter@80
  1044
    /// Constructor.
kpeter@80
  1045
    /// \param m The undelying map.
kpeter@80
  1046
    /// \param v The constant value.
kpeter@80
  1047
    ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
kpeter@559
  1048
    ///\e
kpeter@80
  1049
    Value operator[](const Key &k) const { return _m[k]+_v; }
kpeter@559
  1050
    ///\e
kpeter@80
  1051
    void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
alpar@25
  1052
  };
alpar@25
  1053
kpeter@301
  1054
  /// Returns a \c ShiftMap class
kpeter@301
  1055
kpeter@301
  1056
  /// This function just returns a \c ShiftMap class.
kpeter@80
  1057
  ///
kpeter@80
  1058
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1059
  /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
kpeter@80
  1060
  /// <tt>m[x]+v</tt>.
kpeter@80
  1061
  ///
kpeter@80
  1062
  /// \relates ShiftMap
kpeter@80
  1063
  template<typename M, typename C>
kpeter@80
  1064
  inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
alpar@25
  1065
    return ShiftMap<M, C>(m,v);
alpar@25
  1066
  }
alpar@25
  1067
kpeter@301
  1068
  /// Returns a \c ShiftWriteMap class
kpeter@301
  1069
kpeter@301
  1070
  /// This function just returns a \c ShiftWriteMap class.
kpeter@80
  1071
  ///
kpeter@80
  1072
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1073
  /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
kpeter@80
  1074
  /// <tt>m[x]+v</tt>.
kpeter@80
  1075
  /// Moreover it makes also possible to write the map.
kpeter@80
  1076
  ///
kpeter@80
  1077
  /// \relates ShiftWriteMap
kpeter@80
  1078
  template<typename M, typename C>
kpeter@80
  1079
  inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
alpar@25
  1080
    return ShiftWriteMap<M, C>(m,v);
alpar@25
  1081
  }
alpar@25
  1082
alpar@25
  1083
kpeter@80
  1084
  /// Scales a map with a constant.
kpeter@80
  1085
kpeter@82
  1086
  /// This \ref concepts::ReadMap "read-only map" returns the value of
kpeter@80
  1087
  /// the given map multiplied from the left side with a constant value.
kpeter@80
  1088
  /// Its \c Key and \c Value are inherited from \c M.
alpar@26
  1089
  ///
kpeter@80
  1090
  /// Actually,
kpeter@80
  1091
  /// \code
kpeter@80
  1092
  ///   ScaleMap<M> sc(m,v);
kpeter@80
  1093
  /// \endcode
kpeter@80
  1094
  /// is equivalent to
kpeter@80
  1095
  /// \code
kpeter@80
  1096
  ///   ConstMap<M::Key, M::Value> cm(v);
kpeter@80
  1097
  ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
kpeter@80
  1098
  /// \endcode
alpar@25
  1099
  ///
kpeter@80
  1100
  /// The simplest way of using this map is through the scaleMap()
kpeter@80
  1101
  /// function.
alpar@25
  1102
  ///
kpeter@80
  1103
  /// \sa ScaleWriteMap
kpeter@80
  1104
  template<typename M, typename C = typename M::Value>
alpar@25
  1105
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1106
    const M &_m;
kpeter@80
  1107
    C _v;
alpar@25
  1108
  public:
kpeter@559
  1109
    ///\e
kpeter@559
  1110
    typedef typename M::Key Key;
kpeter@559
  1111
    ///\e
kpeter@559
  1112
    typedef typename M::Value Value;
alpar@25
  1113
kpeter@80
  1114
    /// Constructor
alpar@25
  1115
kpeter@80
  1116
    /// Constructor.
kpeter@80
  1117
    /// \param m The undelying map.
kpeter@80
  1118
    /// \param v The constant value.
kpeter@80
  1119
    ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
kpeter@559
  1120
    ///\e
kpeter@80
  1121
    Value operator[](const Key &k) const { return _v*_m[k]; }
alpar@25
  1122
  };
alpar@25
  1123
kpeter@80
  1124
  /// Scales a map with a constant (read-write version).
alpar@25
  1125
kpeter@80
  1126
  /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
kpeter@80
  1127
  /// the given map multiplied from the left side with a constant value.
kpeter@80
  1128
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1129
  /// It can also be used as write map if the \c / operator is defined
kpeter@80
  1130
  /// between \c Value and \c C and the given multiplier is not zero.
kpeter@29
  1131
  ///
kpeter@80
  1132
  /// The simplest way of using this map is through the scaleWriteMap()
kpeter@80
  1133
  /// function.
kpeter@80
  1134
  ///
kpeter@80
  1135
  /// \sa ScaleMap
kpeter@80
  1136
  template<typename M, typename C = typename M::Value>
alpar@25
  1137
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1138
    M &_m;
kpeter@80
  1139
    C _v;
alpar@25
  1140
  public:
kpeter@559
  1141
    ///\e
kpeter@559
  1142
    typedef typename M::Key Key;
kpeter@559
  1143
    ///\e
kpeter@559
  1144
    typedef typename M::Value Value;
alpar@25
  1145
kpeter@80
  1146
    /// Constructor
alpar@25
  1147
kpeter@80
  1148
    /// Constructor.
kpeter@80
  1149
    /// \param m The undelying map.
kpeter@80
  1150
    /// \param v The constant value.
kpeter@80
  1151
    ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
kpeter@559
  1152
    ///\e
kpeter@80
  1153
    Value operator[](const Key &k) const { return _v*_m[k]; }
kpeter@559
  1154
    ///\e
kpeter@80
  1155
    void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
alpar@25
  1156
  };
alpar@25
  1157
kpeter@301
  1158
  /// Returns a \c ScaleMap class
kpeter@301
  1159
kpeter@301
  1160
  /// This function just returns a \c ScaleMap class.
kpeter@80
  1161
  ///
kpeter@80
  1162
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1163
  /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
kpeter@80
  1164
  /// <tt>v*m[x]</tt>.
kpeter@80
  1165
  ///
kpeter@80
  1166
  /// \relates ScaleMap
kpeter@80
  1167
  template<typename M, typename C>
kpeter@80
  1168
  inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
alpar@25
  1169
    return ScaleMap<M, C>(m,v);
alpar@25
  1170
  }
alpar@25
  1171
kpeter@301
  1172
  /// Returns a \c ScaleWriteMap class
kpeter@301
  1173
kpeter@301
  1174
  /// This function just returns a \c ScaleWriteMap class.
kpeter@80
  1175
  ///
kpeter@80
  1176
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1177
  /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
kpeter@80
  1178
  /// <tt>v*m[x]</tt>.
kpeter@80
  1179
  /// Moreover it makes also possible to write the map.
kpeter@80
  1180
  ///
kpeter@80
  1181
  /// \relates ScaleWriteMap
kpeter@80
  1182
  template<typename M, typename C>
kpeter@80
  1183
  inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
alpar@25
  1184
    return ScaleWriteMap<M, C>(m,v);
alpar@25
  1185
  }
alpar@25
  1186
alpar@25
  1187
kpeter@80
  1188
  /// Negative of a map
alpar@25
  1189
kpeter@82
  1190
  /// This \ref concepts::ReadMap "read-only map" returns the negative
kpeter@80
  1191
  /// of the values of the given map (using the unary \c - operator).
kpeter@80
  1192
  /// Its \c Key and \c Value are inherited from \c M.
alpar@25
  1193
  ///
kpeter@80
  1194
  /// If M::Value is \c int, \c double etc., then
kpeter@80
  1195
  /// \code
kpeter@80
  1196
  ///   NegMap<M> neg(m);
kpeter@80
  1197
  /// \endcode
kpeter@80
  1198
  /// is equivalent to
kpeter@80
  1199
  /// \code
kpeter@80
  1200
  ///   ScaleMap<M> neg(m,-1);
kpeter@80
  1201
  /// \endcode
kpeter@29
  1202
  ///
kpeter@80
  1203
  /// The simplest way of using this map is through the negMap()
kpeter@80
  1204
  /// function.
kpeter@29
  1205
  ///
kpeter@80
  1206
  /// \sa NegWriteMap
kpeter@80
  1207
  template<typename M>
alpar@25
  1208
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1209
    const M& _m;
alpar@25
  1210
  public:
kpeter@559
  1211
    ///\e
kpeter@559
  1212
    typedef typename M::Key Key;
kpeter@559
  1213
    ///\e
kpeter@559
  1214
    typedef typename M::Value Value;
alpar@25
  1215
kpeter@80
  1216
    /// Constructor
kpeter@80
  1217
    NegMap(const M &m) : _m(m) {}
kpeter@559
  1218
    ///\e
kpeter@80
  1219
    Value operator[](const Key &k) const { return -_m[k]; }
alpar@25
  1220
  };
alpar@25
  1221
kpeter@80
  1222
  /// Negative of a map (read-write version)
kpeter@80
  1223
kpeter@80
  1224
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
kpeter@80
  1225
  /// negative of the values of the given map (using the unary \c -
kpeter@80
  1226
  /// operator).
kpeter@80
  1227
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1228
  /// It makes also possible to write the map.
kpeter@80
  1229
  ///
kpeter@80
  1230
  /// If M::Value is \c int, \c double etc., then
kpeter@80
  1231
  /// \code
kpeter@80
  1232
  ///   NegWriteMap<M> neg(m);
kpeter@80
  1233
  /// \endcode
kpeter@80
  1234
  /// is equivalent to
kpeter@80
  1235
  /// \code
kpeter@80
  1236
  ///   ScaleWriteMap<M> neg(m,-1);
kpeter@80
  1237
  /// \endcode
kpeter@80
  1238
  ///
kpeter@80
  1239
  /// The simplest way of using this map is through the negWriteMap()
kpeter@80
  1240
  /// function.
kpeter@29
  1241
  ///
kpeter@29
  1242
  /// \sa NegMap
kpeter@80
  1243
  template<typename M>
alpar@25
  1244
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1245
    M &_m;
alpar@25
  1246
  public:
kpeter@559
  1247
    ///\e
kpeter@559
  1248
    typedef typename M::Key Key;
kpeter@559
  1249
    ///\e
kpeter@559
  1250
    typedef typename M::Value Value;
alpar@25
  1251
kpeter@80
  1252
    /// Constructor
kpeter@80
  1253
    NegWriteMap(M &m) : _m(m) {}
kpeter@559
  1254
    ///\e
kpeter@80
  1255
    Value operator[](const Key &k) const { return -_m[k]; }
kpeter@559
  1256
    ///\e
kpeter@80
  1257
    void set(const Key &k, const Value &v) { _m.set(k, -v); }
alpar@25
  1258
  };
alpar@25
  1259
kpeter@301
  1260
  /// Returns a \c NegMap class
kpeter@301
  1261
kpeter@301
  1262
  /// This function just returns a \c NegMap class.
kpeter@80
  1263
  ///
kpeter@80
  1264
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1265
  /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
kpeter@80
  1266
  ///
kpeter@80
  1267
  /// \relates NegMap
kpeter@80
  1268
  template <typename M>
alpar@25
  1269
  inline NegMap<M> negMap(const M &m) {
alpar@25
  1270
    return NegMap<M>(m);
alpar@25
  1271
  }
alpar@25
  1272
kpeter@301
  1273
  /// Returns a \c NegWriteMap class
kpeter@301
  1274
kpeter@301
  1275
  /// This function just returns a \c NegWriteMap class.
kpeter@80
  1276
  ///
kpeter@80
  1277
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1278
  /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
kpeter@80
  1279
  /// Moreover it makes also possible to write the map.
kpeter@80
  1280
  ///
kpeter@80
  1281
  /// \relates NegWriteMap
kpeter@80
  1282
  template <typename M>
kpeter@80
  1283
  inline NegWriteMap<M> negWriteMap(M &m) {
alpar@25
  1284
    return NegWriteMap<M>(m);
alpar@25
  1285
  }
alpar@25
  1286
alpar@25
  1287
kpeter@80
  1288
  /// Absolute value of a map
kpeter@80
  1289
kpeter@82
  1290
  /// This \ref concepts::ReadMap "read-only map" returns the absolute
kpeter@80
  1291
  /// value of the values of the given map.
kpeter@80
  1292
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1293
  /// \c Value must be comparable to \c 0 and the unary \c -
kpeter@80
  1294
  /// operator must be defined for it, of course.
kpeter@80
  1295
  ///
kpeter@80
  1296
  /// The simplest way of using this map is through the absMap()
kpeter@80
  1297
  /// function.
kpeter@80
  1298
  template<typename M>
alpar@25
  1299
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1300
    const M &_m;
alpar@25
  1301
  public:
kpeter@559
  1302
    ///\e
kpeter@559
  1303
    typedef typename M::Key Key;
kpeter@559
  1304
    ///\e
kpeter@559
  1305
    typedef typename M::Value Value;
alpar@25
  1306
kpeter@80
  1307
    /// Constructor
kpeter@80
  1308
    AbsMap(const M &m) : _m(m) {}
kpeter@559
  1309
    ///\e
kpeter@80
  1310
    Value operator[](const Key &k) const {
kpeter@80
  1311
      Value tmp = _m[k];
alpar@25
  1312
      return tmp >= 0 ? tmp : -tmp;
alpar@25
  1313
    }
alpar@25
  1314
alpar@25
  1315
  };
alpar@25
  1316
kpeter@301
  1317
  /// Returns an \c AbsMap class
kpeter@301
  1318
kpeter@301
  1319
  /// This function just returns an \c AbsMap class.
kpeter@80
  1320
  ///
kpeter@80
  1321
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1322
  /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
kpeter@80
  1323
  /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
kpeter@80
  1324
  /// negative.
kpeter@80
  1325
  ///
kpeter@80
  1326
  /// \relates AbsMap
kpeter@80
  1327
  template<typename M>
alpar@25
  1328
  inline AbsMap<M> absMap(const M &m) {
alpar@25
  1329
    return AbsMap<M>(m);
alpar@25
  1330
  }
alpar@25
  1331
kpeter@82
  1332
  /// @}
alpar@209
  1333
kpeter@82
  1334
  // Logical maps and map adaptors:
kpeter@82
  1335
kpeter@82
  1336
  /// \addtogroup maps
kpeter@82
  1337
  /// @{
kpeter@82
  1338
kpeter@82
  1339
  /// Constant \c true map.
kpeter@82
  1340
kpeter@82
  1341
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
kpeter@82
  1342
  /// each key.
kpeter@82
  1343
  ///
kpeter@82
  1344
  /// Note that
kpeter@82
  1345
  /// \code
kpeter@82
  1346
  ///   TrueMap<K> tm;
kpeter@82
  1347
  /// \endcode
kpeter@82
  1348
  /// is equivalent to
kpeter@82
  1349
  /// \code
kpeter@82
  1350
  ///   ConstMap<K,bool> tm(true);
kpeter@82
  1351
  /// \endcode
kpeter@82
  1352
  ///
kpeter@82
  1353
  /// \sa FalseMap
kpeter@82
  1354
  /// \sa ConstMap
kpeter@82
  1355
  template <typename K>
kpeter@82
  1356
  class TrueMap : public MapBase<K, bool> {
kpeter@82
  1357
  public:
kpeter@559
  1358
    ///\e
kpeter@559
  1359
    typedef K Key;
kpeter@559
  1360
    ///\e
kpeter@559
  1361
    typedef bool Value;
kpeter@82
  1362
kpeter@82
  1363
    /// Gives back \c true.
kpeter@82
  1364
    Value operator[](const Key&) const { return true; }
kpeter@82
  1365
  };
kpeter@82
  1366
kpeter@301
  1367
  /// Returns a \c TrueMap class
kpeter@301
  1368
kpeter@301
  1369
  /// This function just returns a \c TrueMap class.
kpeter@82
  1370
  /// \relates TrueMap
kpeter@82
  1371
  template<typename K>
kpeter@82
  1372
  inline TrueMap<K> trueMap() {
kpeter@82
  1373
    return TrueMap<K>();
kpeter@82
  1374
  }
kpeter@82
  1375
kpeter@82
  1376
kpeter@82
  1377
  /// Constant \c false map.
kpeter@82
  1378
kpeter@82
  1379
  /// This \ref concepts::ReadMap "read-only map" assigns \c false to
kpeter@82
  1380
  /// each key.
kpeter@82
  1381
  ///
kpeter@82
  1382
  /// Note that
kpeter@82
  1383
  /// \code
kpeter@82
  1384
  ///   FalseMap<K> fm;
kpeter@82
  1385
  /// \endcode
kpeter@82
  1386
  /// is equivalent to
kpeter@82
  1387
  /// \code
kpeter@82
  1388
  ///   ConstMap<K,bool> fm(false);
kpeter@82
  1389
  /// \endcode
kpeter@82
  1390
  ///
kpeter@82
  1391
  /// \sa TrueMap
kpeter@82
  1392
  /// \sa ConstMap
kpeter@82
  1393
  template <typename K>
kpeter@82
  1394
  class FalseMap : public MapBase<K, bool> {
kpeter@82
  1395
  public:
kpeter@559
  1396
    ///\e
kpeter@559
  1397
    typedef K Key;
kpeter@559
  1398
    ///\e
kpeter@559
  1399
    typedef bool Value;
kpeter@82
  1400
kpeter@82
  1401
    /// Gives back \c false.
kpeter@82
  1402
    Value operator[](const Key&) const { return false; }
kpeter@82
  1403
  };
kpeter@82
  1404
kpeter@301
  1405
  /// Returns a \c FalseMap class
kpeter@301
  1406
kpeter@301
  1407
  /// This function just returns a \c FalseMap class.
kpeter@82
  1408
  /// \relates FalseMap
kpeter@82
  1409
  template<typename K>
kpeter@82
  1410
  inline FalseMap<K> falseMap() {
kpeter@82
  1411
    return FalseMap<K>();
kpeter@82
  1412
  }
kpeter@82
  1413
kpeter@82
  1414
  /// @}
kpeter@82
  1415
kpeter@82
  1416
  /// \addtogroup map_adaptors
kpeter@82
  1417
  /// @{
kpeter@82
  1418
kpeter@82
  1419
  /// Logical 'and' of two maps
kpeter@82
  1420
kpeter@82
  1421
  /// This \ref concepts::ReadMap "read-only map" returns the logical
kpeter@82
  1422
  /// 'and' of the values of the two given maps.
kpeter@82
  1423
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1424
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1425
  ///
kpeter@82
  1426
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1427
  /// \code
kpeter@82
  1428
  ///   AndMap<M1,M2> am(m1,m2);
kpeter@82
  1429
  /// \endcode
kpeter@82
  1430
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
kpeter@82
  1431
  ///
kpeter@82
  1432
  /// The simplest way of using this map is through the andMap()
kpeter@82
  1433
  /// function.
kpeter@82
  1434
  ///
kpeter@82
  1435
  /// \sa OrMap
kpeter@82
  1436
  /// \sa NotMap, NotWriteMap
kpeter@82
  1437
  template<typename M1, typename M2>
kpeter@82
  1438
  class AndMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1439
    const M1 &_m1;
kpeter@82
  1440
    const M2 &_m2;
kpeter@82
  1441
  public:
kpeter@559
  1442
    ///\e
kpeter@559
  1443
    typedef typename M1::Key Key;
kpeter@559
  1444
    ///\e
kpeter@559
  1445
    typedef bool Value;
kpeter@82
  1446
kpeter@82
  1447
    /// Constructor
kpeter@82
  1448
    AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
  1449
    ///\e
kpeter@82
  1450
    Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
kpeter@82
  1451
  };
kpeter@82
  1452
kpeter@301
  1453
  /// Returns an \c AndMap class
kpeter@301
  1454
kpeter@301
  1455
  /// This function just returns an \c AndMap class.
kpeter@82
  1456
  ///
kpeter@82
  1457
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
kpeter@82
  1458
  /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1459
  /// <tt>m1[x]&&m2[x]</tt>.
kpeter@82
  1460
  ///
kpeter@82
  1461
  /// \relates AndMap
kpeter@82
  1462
  template<typename M1, typename M2>
kpeter@82
  1463
  inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1464
    return AndMap<M1, M2>(m1,m2);
kpeter@82
  1465
  }
kpeter@82
  1466
kpeter@82
  1467
kpeter@82
  1468
  /// Logical 'or' of two maps
kpeter@82
  1469
kpeter@82
  1470
  /// This \ref concepts::ReadMap "read-only map" returns the logical
kpeter@82
  1471
  /// 'or' of the values of the two given maps.
kpeter@82
  1472
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1473
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1474
  ///
kpeter@82
  1475
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1476
  /// \code
kpeter@82
  1477
  ///   OrMap<M1,M2> om(m1,m2);
kpeter@82
  1478
  /// \endcode
kpeter@82
  1479
  /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
kpeter@82
  1480
  ///
kpeter@82
  1481
  /// The simplest way of using this map is through the orMap()
kpeter@82
  1482
  /// function.
kpeter@82
  1483
  ///
kpeter@82
  1484
  /// \sa AndMap
kpeter@82
  1485
  /// \sa NotMap, NotWriteMap
kpeter@82
  1486
  template<typename M1, typename M2>
kpeter@82
  1487
  class OrMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1488
    const M1 &_m1;
kpeter@82
  1489
    const M2 &_m2;
kpeter@82
  1490
  public:
kpeter@559
  1491
    ///\e
kpeter@559
  1492
    typedef typename M1::Key Key;
kpeter@559
  1493
    ///\e
kpeter@559
  1494
    typedef bool Value;
kpeter@82
  1495
kpeter@82
  1496
    /// Constructor
kpeter@82
  1497
    OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
  1498
    ///\e
kpeter@82
  1499
    Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
kpeter@82
  1500
  };
kpeter@82
  1501
kpeter@301
  1502
  /// Returns an \c OrMap class
kpeter@301
  1503
kpeter@301
  1504
  /// This function just returns an \c OrMap class.
kpeter@82
  1505
  ///
kpeter@82
  1506
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
kpeter@82
  1507
  /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1508
  /// <tt>m1[x]||m2[x]</tt>.
kpeter@82
  1509
  ///
kpeter@82
  1510
  /// \relates OrMap
kpeter@82
  1511
  template<typename M1, typename M2>
kpeter@82
  1512
  inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1513
    return OrMap<M1, M2>(m1,m2);
kpeter@82
  1514
  }
kpeter@82
  1515
alpar@25
  1516
kpeter@80
  1517
  /// Logical 'not' of a map
kpeter@80
  1518
kpeter@82
  1519
  /// This \ref concepts::ReadMap "read-only map" returns the logical
kpeter@80
  1520
  /// negation of the values of the given map.
kpeter@80
  1521
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
alpar@25
  1522
  ///
kpeter@80
  1523
  /// The simplest way of using this map is through the notMap()
kpeter@80
  1524
  /// function.
alpar@25
  1525
  ///
kpeter@80
  1526
  /// \sa NotWriteMap
kpeter@80
  1527
  template <typename M>
alpar@25
  1528
  class NotMap : public MapBase<typename M::Key, bool> {
kpeter@80
  1529
    const M &_m;
alpar@25
  1530
  public:
kpeter@559
  1531
    ///\e
kpeter@559
  1532
    typedef typename M::Key Key;
kpeter@559
  1533
    ///\e
kpeter@559
  1534
    typedef bool Value;
alpar@25
  1535
alpar@25
  1536
    /// Constructor
kpeter@80
  1537
    NotMap(const M &m) : _m(m) {}
kpeter@559
  1538
    ///\e
kpeter@80
  1539
    Value operator[](const Key &k) const { return !_m[k]; }
alpar@25
  1540
  };
alpar@25
  1541
kpeter@80
  1542
  /// Logical 'not' of a map (read-write version)
kpeter@80
  1543
kpeter@80
  1544
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
kpeter@80
  1545
  /// logical negation of the values of the given map.
kpeter@80
  1546
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
kpeter@80
  1547
  /// It makes also possible to write the map. When a value is set,
kpeter@80
  1548
  /// the opposite value is set to the original map.
kpeter@29
  1549
  ///
kpeter@80
  1550
  /// The simplest way of using this map is through the notWriteMap()
kpeter@80
  1551
  /// function.
kpeter@80
  1552
  ///
kpeter@80
  1553
  /// \sa NotMap
kpeter@80
  1554
  template <typename M>
alpar@25
  1555
  class NotWriteMap : public MapBase<typename M::Key, bool> {
kpeter@80
  1556
    M &_m;
alpar@25
  1557
  public:
kpeter@559
  1558
    ///\e
kpeter@559
  1559
    typedef typename M::Key Key;
kpeter@559
  1560
    ///\e
kpeter@559
  1561
    typedef bool Value;
alpar@25
  1562
alpar@25
  1563
    /// Constructor
kpeter@80
  1564
    NotWriteMap(M &m) : _m(m) {}
kpeter@559
  1565
    ///\e
kpeter@80
  1566
    Value operator[](const Key &k) const { return !_m[k]; }
kpeter@559
  1567
    ///\e
kpeter@80
  1568
    void set(const Key &k, bool v) { _m.set(k, !v); }
alpar@25
  1569
  };
kpeter@80
  1570
kpeter@301
  1571
  /// Returns a \c NotMap class
kpeter@301
  1572
kpeter@301
  1573
  /// This function just returns a \c NotMap class.
kpeter@80
  1574
  ///
kpeter@80
  1575
  /// For example, if \c m is a map with \c bool values, then
kpeter@80
  1576
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
kpeter@80
  1577
  ///
kpeter@80
  1578
  /// \relates NotMap
kpeter@80
  1579
  template <typename M>
alpar@25
  1580
  inline NotMap<M> notMap(const M &m) {
alpar@25
  1581
    return NotMap<M>(m);
alpar@25
  1582
  }
kpeter@80
  1583
kpeter@301
  1584
  /// Returns a \c NotWriteMap class
kpeter@301
  1585
kpeter@301
  1586
  /// This function just returns a \c NotWriteMap class.
kpeter@80
  1587
  ///
kpeter@80
  1588
  /// For example, if \c m is a map with \c bool values, then
kpeter@80
  1589
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
kpeter@80
  1590
  /// Moreover it makes also possible to write the map.
kpeter@80
  1591
  ///
kpeter@80
  1592
  /// \relates NotWriteMap
kpeter@80
  1593
  template <typename M>
kpeter@80
  1594
  inline NotWriteMap<M> notWriteMap(M &m) {
alpar@25
  1595
    return NotWriteMap<M>(m);
alpar@25
  1596
  }
alpar@25
  1597
kpeter@82
  1598
kpeter@82
  1599
  /// Combination of two maps using the \c == operator
kpeter@82
  1600
kpeter@82
  1601
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
kpeter@82
  1602
  /// the keys for which the corresponding values of the two maps are
kpeter@82
  1603
  /// equal.
kpeter@82
  1604
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1605
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1606
  ///
kpeter@82
  1607
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1608
  /// \code
kpeter@82
  1609
  ///   EqualMap<M1,M2> em(m1,m2);
kpeter@82
  1610
  /// \endcode
kpeter@82
  1611
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
kpeter@82
  1612
  ///
kpeter@82
  1613
  /// The simplest way of using this map is through the equalMap()
kpeter@82
  1614
  /// function.
kpeter@82
  1615
  ///
kpeter@82
  1616
  /// \sa LessMap
kpeter@82
  1617
  template<typename M1, typename M2>
kpeter@82
  1618
  class EqualMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1619
    const M1 &_m1;
kpeter@82
  1620
    const M2 &_m2;
kpeter@82
  1621
  public:
kpeter@559
  1622
    ///\e
kpeter@559
  1623
    typedef typename M1::Key Key;
kpeter@559
  1624
    ///\e
kpeter@559
  1625
    typedef bool Value;
kpeter@82
  1626
kpeter@82
  1627
    /// Constructor
kpeter@82
  1628
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
  1629
    ///\e
kpeter@82
  1630
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
kpeter@82
  1631
  };
kpeter@82
  1632
kpeter@301
  1633
  /// Returns an \c EqualMap class
kpeter@301
  1634
kpeter@301
  1635
  /// This function just returns an \c EqualMap class.
kpeter@82
  1636
  ///
kpeter@82
  1637
  /// For example, if \c m1 and \c m2 are maps with keys and values of
kpeter@82
  1638
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1639
  /// <tt>m1[x]==m2[x]</tt>.
kpeter@82
  1640
  ///
kpeter@82
  1641
  /// \relates EqualMap
kpeter@82
  1642
  template<typename M1, typename M2>
kpeter@82
  1643
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1644
    return EqualMap<M1, M2>(m1,m2);
kpeter@82
  1645
  }
kpeter@82
  1646
kpeter@82
  1647
kpeter@82
  1648
  /// Combination of two maps using the \c < operator
kpeter@82
  1649
kpeter@82
  1650
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
kpeter@82
  1651
  /// the keys for which the corresponding value of the first map is
kpeter@82
  1652
  /// less then the value of the second map.
kpeter@82
  1653
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1654
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1655
  ///
kpeter@82
  1656
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1657
  /// \code
kpeter@82
  1658
  ///   LessMap<M1,M2> lm(m1,m2);
kpeter@82
  1659
  /// \endcode
kpeter@82
  1660
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
kpeter@82
  1661
  ///
kpeter@82
  1662
  /// The simplest way of using this map is through the lessMap()
kpeter@82
  1663
  /// function.
kpeter@82
  1664
  ///
kpeter@82
  1665
  /// \sa EqualMap
kpeter@82
  1666
  template<typename M1, typename M2>
kpeter@82
  1667
  class LessMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1668
    const M1 &_m1;
kpeter@82
  1669
    const M2 &_m2;
kpeter@82
  1670
  public:
kpeter@559
  1671
    ///\e
kpeter@559
  1672
    typedef typename M1::Key Key;
kpeter@559
  1673
    ///\e
kpeter@559
  1674
    typedef bool Value;
kpeter@82
  1675
kpeter@82
  1676
    /// Constructor
kpeter@82
  1677
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@559
  1678
    ///\e
kpeter@82
  1679
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
kpeter@82
  1680
  };
kpeter@82
  1681
kpeter@301
  1682
  /// Returns an \c LessMap class
kpeter@301
  1683
kpeter@301
  1684
  /// This function just returns an \c LessMap class.
kpeter@82
  1685
  ///
kpeter@82
  1686
  /// For example, if \c m1 and \c m2 are maps with keys and values of
kpeter@82
  1687
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1688
  /// <tt>m1[x]<m2[x]</tt>.
kpeter@82
  1689
  ///
kpeter@82
  1690
  /// \relates LessMap
kpeter@82
  1691
  template<typename M1, typename M2>
kpeter@82
  1692
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1693
    return LessMap<M1, M2>(m1,m2);
kpeter@82
  1694
  }
kpeter@82
  1695
alpar@104
  1696
  namespace _maps_bits {
alpar@104
  1697
alpar@104
  1698
    template <typename _Iterator, typename Enable = void>
alpar@104
  1699
    struct IteratorTraits {
alpar@104
  1700
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
alpar@104
  1701
    };
alpar@104
  1702
alpar@104
  1703
    template <typename _Iterator>
alpar@104
  1704
    struct IteratorTraits<_Iterator,
alpar@104
  1705
      typename exists<typename _Iterator::container_type>::type>
alpar@104
  1706
    {
alpar@104
  1707
      typedef typename _Iterator::container_type::value_type Value;
alpar@104
  1708
    };
alpar@104
  1709
alpar@104
  1710
  }
alpar@104
  1711
kpeter@314
  1712
  /// @}
kpeter@314
  1713
kpeter@314
  1714
  /// \addtogroup maps
kpeter@314
  1715
  /// @{
kpeter@314
  1716
alpar@104
  1717
  /// \brief Writable bool map for logging each \c true assigned element
alpar@104
  1718
  ///
kpeter@159
  1719
  /// A \ref concepts::WriteMap "writable" bool map for logging
alpar@104
  1720
  /// each \c true assigned element, i.e it copies subsequently each
alpar@104
  1721
  /// keys set to \c true to the given iterator.
kpeter@159
  1722
  /// The most important usage of it is storing certain nodes or arcs
kpeter@159
  1723
  /// that were marked \c true by an algorithm.
alpar@104
  1724
  ///
kpeter@159
  1725
  /// There are several algorithms that provide solutions through bool
kpeter@159
  1726
  /// maps and most of them assign \c true at most once for each key.
kpeter@159
  1727
  /// In these cases it is a natural request to store each \c true
kpeter@159
  1728
  /// assigned elements (in order of the assignment), which can be
kpeter@167
  1729
  /// easily done with LoggerBoolMap.
kpeter@159
  1730
  ///
kpeter@167
  1731
  /// The simplest way of using this map is through the loggerBoolMap()
kpeter@159
  1732
  /// function.
kpeter@159
  1733
  ///
kpeter@559
  1734
  /// \tparam IT The type of the iterator.
kpeter@559
  1735
  /// \tparam KEY The key type of the map. The default value set
kpeter@159
  1736
  /// according to the iterator type should work in most cases.
alpar@104
  1737
  ///
alpar@104
  1738
  /// \note The container of the iterator must contain enough space
kpeter@159
  1739
  /// for the elements or the iterator should be an inserter iterator.
kpeter@159
  1740
#ifdef DOXYGEN
kpeter@559
  1741
  template <typename IT, typename KEY>
kpeter@159
  1742
#else
kpeter@559
  1743
  template <typename IT,
kpeter@559
  1744
            typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
kpeter@159
  1745
#endif
kpeter@559
  1746
  class LoggerBoolMap : public MapBase<KEY, bool> {
alpar@104
  1747
  public:
kpeter@559
  1748
kpeter@559
  1749
    ///\e
kpeter@559
  1750
    typedef KEY Key;
kpeter@559
  1751
    ///\e
alpar@104
  1752
    typedef bool Value;
kpeter@559
  1753
    ///\e
kpeter@559
  1754
    typedef IT Iterator;
alpar@104
  1755
alpar@104
  1756
    /// Constructor
kpeter@167
  1757
    LoggerBoolMap(Iterator it)
alpar@104
  1758
      : _begin(it), _end(it) {}
alpar@104
  1759
alpar@104
  1760
    /// Gives back the given iterator set for the first key
alpar@104
  1761
    Iterator begin() const {
alpar@104
  1762
      return _begin;
alpar@104
  1763
    }
alpar@104
  1764
alpar@104
  1765
    /// Gives back the the 'after the last' iterator
alpar@104
  1766
    Iterator end() const {
alpar@104
  1767
      return _end;
alpar@104
  1768
    }
alpar@104
  1769
alpar@104
  1770
    /// The set function of the map
kpeter@159
  1771
    void set(const Key& key, Value value) {
alpar@104
  1772
      if (value) {
alpar@209
  1773
        *_end++ = key;
alpar@104
  1774
      }
alpar@104
  1775
    }
alpar@104
  1776
alpar@104
  1777
  private:
alpar@104
  1778
    Iterator _begin;
kpeter@159
  1779
    Iterator _end;
alpar@104
  1780
  };
alpar@209
  1781
kpeter@301
  1782
  /// Returns a \c LoggerBoolMap class
kpeter@301
  1783
kpeter@301
  1784
  /// This function just returns a \c LoggerBoolMap class.
kpeter@159
  1785
  ///
kpeter@159
  1786
  /// The most important usage of it is storing certain nodes or arcs
kpeter@159
  1787
  /// that were marked \c true by an algorithm.
kpeter@159
  1788
  /// For example it makes easier to store the nodes in the processing
kpeter@159
  1789
  /// order of Dfs algorithm, as the following examples show.
kpeter@159
  1790
  /// \code
kpeter@159
  1791
  ///   std::vector<Node> v;
kpeter@716
  1792
  ///   dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
kpeter@159
  1793
  /// \endcode
kpeter@159
  1794
  /// \code
kpeter@159
  1795
  ///   std::vector<Node> v(countNodes(g));
kpeter@716
  1796
  ///   dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
kpeter@159
  1797
  /// \endcode
kpeter@159
  1798
  ///
kpeter@159
  1799
  /// \note The container of the iterator must contain enough space
kpeter@159
  1800
  /// for the elements or the iterator should be an inserter iterator.
kpeter@159
  1801
  ///
kpeter@167
  1802
  /// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
kpeter@159
  1803
  /// it cannot be used when a readable map is needed, for example as
kpeter@301
  1804
  /// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
kpeter@159
  1805
  ///
kpeter@167
  1806
  /// \relates LoggerBoolMap
kpeter@159
  1807
  template<typename Iterator>
kpeter@167
  1808
  inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
kpeter@167
  1809
    return LoggerBoolMap<Iterator>(it);
kpeter@159
  1810
  }
alpar@104
  1811
kpeter@314
  1812
  /// @}
kpeter@314
  1813
kpeter@314
  1814
  /// \addtogroup graph_maps
kpeter@314
  1815
  /// @{
kpeter@314
  1816
kpeter@559
  1817
  /// \brief Provides an immutable and unique id for each item in a graph.
kpeter@559
  1818
  ///
kpeter@559
  1819
  /// IdMap provides a unique and immutable id for each item of the
deba@693
  1820
  /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
kpeter@559
  1821
  ///  - \b unique: different items get different ids,
kpeter@559
  1822
  ///  - \b immutable: the id of an item does not change (even if you
kpeter@559
  1823
  ///    delete other nodes).
kpeter@559
  1824
  ///
kpeter@559
  1825
  /// Using this map you get access (i.e. can read) the inner id values of
kpeter@559
  1826
  /// the items stored in the graph, which is returned by the \c id()
kpeter@559
  1827
  /// function of the graph. This map can be inverted with its member
deba@220
  1828
  /// class \c InverseMap or with the \c operator() member.
deba@220
  1829
  ///
kpeter@559
  1830
  /// \tparam GR The graph type.
kpeter@559
  1831
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@559
  1832
  /// \c GR::Edge).
kpeter@559
  1833
  ///
alpar@572
  1834
  /// \see RangeIdMap
kpeter@559
  1835
  template <typename GR, typename K>
kpeter@559
  1836
  class IdMap : public MapBase<K, int> {
deba@220
  1837
  public:
kpeter@559
  1838
    /// The graph type of IdMap.
kpeter@559
  1839
    typedef GR Graph;
kpeter@617
  1840
    typedef GR Digraph;
kpeter@559
  1841
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
kpeter@559
  1842
    typedef K Item;
kpeter@559
  1843
    /// The key type of IdMap (\c Node, \c Arc or \c Edge).
kpeter@559
  1844
    typedef K Key;
kpeter@559
  1845
    /// The value type of IdMap.
deba@220
  1846
    typedef int Value;
deba@220
  1847
deba@220
  1848
    /// \brief Constructor.
deba@220
  1849
    ///
deba@220
  1850
    /// Constructor of the map.
deba@220
  1851
    explicit IdMap(const Graph& graph) : _graph(&graph) {}
deba@220
  1852
deba@220
  1853
    /// \brief Gives back the \e id of the item.
deba@220
  1854
    ///
deba@220
  1855
    /// Gives back the immutable and unique \e id of the item.
deba@220
  1856
    int operator[](const Item& item) const { return _graph->id(item);}
deba@220
  1857
kpeter@559
  1858
    /// \brief Gives back the \e item by its id.
deba@220
  1859
    ///
kpeter@559
  1860
    /// Gives back the \e item by its id.
deba@220
  1861
    Item operator()(int id) { return _graph->fromId(id, Item()); }
deba@220
  1862
deba@220
  1863
  private:
deba@220
  1864
    const Graph* _graph;
deba@220
  1865
deba@220
  1866
  public:
deba@220
  1867
kpeter@559
  1868
    /// \brief This class represents the inverse of its owner (IdMap).
deba@220
  1869
    ///
kpeter@559
  1870
    /// This class represents the inverse of its owner (IdMap).
deba@220
  1871
    /// \see inverse()
deba@220
  1872
    class InverseMap {
deba@220
  1873
    public:
deba@220
  1874
deba@220
  1875
      /// \brief Constructor.
deba@220
  1876
      ///
deba@220
  1877
      /// Constructor for creating an id-to-item map.
deba@220
  1878
      explicit InverseMap(const Graph& graph) : _graph(&graph) {}
deba@220
  1879
deba@220
  1880
      /// \brief Constructor.
deba@220
  1881
      ///
deba@220
  1882
      /// Constructor for creating an id-to-item map.
deba@220
  1883
      explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
deba@220
  1884
deba@220
  1885
      /// \brief Gives back the given item from its id.
deba@220
  1886
      ///
deba@220
  1887
      /// Gives back the given item from its id.
deba@220
  1888
      Item operator[](int id) const { return _graph->fromId(id, Item());}
deba@220
  1889
deba@220
  1890
    private:
deba@220
  1891
      const Graph* _graph;
deba@220
  1892
    };
deba@220
  1893
deba@220
  1894
    /// \brief Gives back the inverse of the map.
deba@220
  1895
    ///
deba@220
  1896
    /// Gives back the inverse of the IdMap.
deba@220
  1897
    InverseMap inverse() const { return InverseMap(*_graph);}
deba@220
  1898
  };
deba@220
  1899
deba@220
  1900
alpar@572
  1901
  /// \brief General cross reference graph map type.
kpeter@559
  1902
kpeter@559
  1903
  /// This class provides simple invertable graph maps.
kpeter@684
  1904
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
kpeter@684
  1905
  /// and if a key is set to a new value, then stores it in the inverse map.
deba@220
  1906
  /// The values of the map can be accessed
deba@220
  1907
  /// with stl compatible forward iterator.
deba@220
  1908
  ///
kpeter@694
  1909
  /// This type is not reference map, so it cannot be modified with
kpeter@684
  1910
  /// the subscript operator.
kpeter@694
  1911
  ///
kpeter@559
  1912
  /// \tparam GR The graph type.
kpeter@559
  1913
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@559
  1914
  /// \c GR::Edge).
kpeter@559
  1915
  /// \tparam V The value type of the map.
deba@220
  1916
  ///
deba@220
  1917
  /// \see IterableValueMap
kpeter@559
  1918
  template <typename GR, typename K, typename V>
alpar@572
  1919
  class CrossRefMap
kpeter@559
  1920
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
deba@220
  1921
  private:
deba@220
  1922
kpeter@559
  1923
    typedef typename ItemSetTraits<GR, K>::
kpeter@559
  1924
      template Map<V>::Type Map;
kpeter@559
  1925
kpeter@684
  1926
    typedef std::multimap<V, K> Container;
deba@220
  1927
    Container _inv_map;
deba@220
  1928
deba@220
  1929
  public:
deba@220
  1930
alpar@572
  1931
    /// The graph type of CrossRefMap.
kpeter@559
  1932
    typedef GR Graph;
kpeter@617
  1933
    typedef GR Digraph;
alpar@572
  1934
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
kpeter@559
  1935
    typedef K Item;
alpar@572
  1936
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
kpeter@559
  1937
    typedef K Key;
alpar@572
  1938
    /// The value type of CrossRefMap.
kpeter@559
  1939
    typedef V Value;
deba@220
  1940
deba@220
  1941
    /// \brief Constructor.
deba@220
  1942
    ///
alpar@572
  1943
    /// Construct a new CrossRefMap for the given graph.
alpar@572
  1944
    explicit CrossRefMap(const Graph& graph) : Map(graph) {}
deba@220
  1945
deba@220
  1946
    /// \brief Forward iterator for values.
deba@220
  1947
    ///
deba@220
  1948
    /// This iterator is an stl compatible forward
deba@220
  1949
    /// iterator on the values of the map. The values can
kpeter@559
  1950
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
kpeter@684
  1951
    /// They are considered with multiplicity, so each value is
kpeter@684
  1952
    /// traversed for each item it is assigned to.
deba@220
  1953
    class ValueIterator
deba@220
  1954
      : public std::iterator<std::forward_iterator_tag, Value> {
alpar@572
  1955
      friend class CrossRefMap;
deba@220
  1956
    private:
deba@220
  1957
      ValueIterator(typename Container::const_iterator _it)
deba@220
  1958
        : it(_it) {}
deba@220
  1959
    public:
deba@220
  1960
deba@220
  1961
      ValueIterator() {}
deba@220
  1962
deba@220
  1963
      ValueIterator& operator++() { ++it; return *this; }
deba@220
  1964
      ValueIterator operator++(int) {
deba@220
  1965
        ValueIterator tmp(*this);
deba@220
  1966
        operator++();
deba@220
  1967
        return tmp;
deba@220
  1968
      }
deba@220
  1969
deba@220
  1970
      const Value& operator*() const { return it->first; }
deba@220
  1971
      const Value* operator->() const { return &(it->first); }
deba@220
  1972
deba@220
  1973
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@220
  1974
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@220
  1975
deba@220
  1976
    private:
deba@220
  1977
      typename Container::const_iterator it;
deba@220
  1978
    };
deba@220
  1979
deba@220
  1980
    /// \brief Returns an iterator to the first value.
deba@220
  1981
    ///
deba@220
  1982
    /// Returns an stl compatible iterator to the
deba@220
  1983
    /// first value of the map. The values of the
kpeter@559
  1984
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@220
  1985
    /// range.
deba@220
  1986
    ValueIterator beginValue() const {
deba@220
  1987
      return ValueIterator(_inv_map.begin());
deba@220
  1988
    }
deba@220
  1989
deba@220
  1990
    /// \brief Returns an iterator after the last value.
deba@220
  1991
    ///
deba@220
  1992
    /// Returns an stl compatible iterator after the
deba@220
  1993
    /// last value of the map. The values of the
kpeter@559
  1994
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@220
  1995
    /// range.
deba@220
  1996
    ValueIterator endValue() const {
deba@220
  1997
      return ValueIterator(_inv_map.end());
deba@220
  1998
    }
deba@220
  1999
kpeter@559
  2000
    /// \brief Sets the value associated with the given key.
deba@220
  2001
    ///
kpeter@559
  2002
    /// Sets the value associated with the given key.
deba@220
  2003
    void set(const Key& key, const Value& val) {
deba@220
  2004
      Value oldval = Map::operator[](key);
kpeter@684
  2005
      typename Container::iterator it;
kpeter@684
  2006
      for (it = _inv_map.equal_range(oldval).first;
kpeter@684
  2007
           it != _inv_map.equal_range(oldval).second; ++it) {
kpeter@684
  2008
        if (it->second == key) {
kpeter@684
  2009
          _inv_map.erase(it);
kpeter@684
  2010
          break;
kpeter@684
  2011
        }
deba@220
  2012
      }
deba@693
  2013
      _inv_map.insert(std::make_pair(val, key));
deba@220
  2014
      Map::set(key, val);
deba@220
  2015
    }
deba@220
  2016
kpeter@559
  2017
    /// \brief Returns the value associated with the given key.
deba@220
  2018
    ///
kpeter@559
  2019
    /// Returns the value associated with the given key.
deba@220
  2020
    typename MapTraits<Map>::ConstReturnValue
deba@220
  2021
    operator[](const Key& key) const {
deba@220
  2022
      return Map::operator[](key);
deba@220
  2023
    }
deba@220
  2024
kpeter@684
  2025
    /// \brief Gives back an item by its value.
deba@220
  2026
    ///
kpeter@684
  2027
    /// This function gives back an item that is assigned to
kpeter@684
  2028
    /// the given value or \c INVALID if no such item exists.
kpeter@684
  2029
    /// If there are more items with the same associated value,
kpeter@684
  2030
    /// only one of them is returned.
kpeter@684
  2031
    Key operator()(const Value& val) const {
kpeter@684
  2032
      typename Container::const_iterator it = _inv_map.find(val);
deba@220
  2033
      return it != _inv_map.end() ? it->second : INVALID;
deba@220
  2034
    }
deba@220
  2035
deba@220
  2036
  protected:
deba@220
  2037
kpeter@559
  2038
    /// \brief Erase the key from the map and the inverse map.
deba@220
  2039
    ///
kpeter@559
  2040
    /// Erase the key from the map and the inverse map. It is called by the
deba@220
  2041
    /// \c AlterationNotifier.
deba@220
  2042
    virtual void erase(const Key& key) {
deba@220
  2043
      Value val = Map::operator[](key);
kpeter@684
  2044
      typename Container::iterator it;
kpeter@684
  2045
      for (it = _inv_map.equal_range(val).first;
kpeter@684
  2046
           it != _inv_map.equal_range(val).second; ++it) {
kpeter@684
  2047
        if (it->second == key) {
kpeter@684
  2048
          _inv_map.erase(it);
kpeter@684
  2049
          break;
kpeter@684
  2050
        }
deba@220
  2051
      }
deba@220
  2052
      Map::erase(key);
deba@220
  2053
    }
deba@220
  2054
kpeter@559
  2055
    /// \brief Erase more keys from the map and the inverse map.
deba@220
  2056
    ///
kpeter@559
  2057
    /// Erase more keys from the map and the inverse map. It is called by the
deba@220
  2058
    /// \c AlterationNotifier.
deba@220
  2059
    virtual void erase(const std::vector<Key>& keys) {
deba@220
  2060
      for (int i = 0; i < int(keys.size()); ++i) {
deba@220
  2061
        Value val = Map::operator[](keys[i]);
kpeter@684
  2062
        typename Container::iterator it;
kpeter@684
  2063
        for (it = _inv_map.equal_range(val).first;
kpeter@684
  2064
             it != _inv_map.equal_range(val).second; ++it) {
kpeter@684
  2065
          if (it->second == keys[i]) {
kpeter@684
  2066
            _inv_map.erase(it);
kpeter@684
  2067
            break;
kpeter@684
  2068
          }
deba@220
  2069
        }
deba@220
  2070
      }
deba@220
  2071
      Map::erase(keys);
deba@220
  2072
    }
deba@220
  2073
kpeter@559
  2074
    /// \brief Clear the keys from the map and the inverse map.
deba@220
  2075
    ///
kpeter@559
  2076
    /// Clear the keys from the map and the inverse map. It is called by the
deba@220
  2077
    /// \c AlterationNotifier.
deba@220
  2078
    virtual void clear() {
deba@220
  2079
      _inv_map.clear();
deba@220
  2080
      Map::clear();
deba@220
  2081
    }
deba@220
  2082
deba@220
  2083
  public:
deba@220
  2084
deba@220
  2085
    /// \brief The inverse map type.
deba@220
  2086
    ///
deba@220
  2087
    /// The inverse of this map. The subscript operator of the map
kpeter@559
  2088
    /// gives back the item that was last assigned to the value.
deba@220
  2089
    class InverseMap {
deba@220
  2090
    public:
kpeter@559
  2091
      /// \brief Constructor
deba@220
  2092
      ///
deba@220
  2093
      /// Constructor of the InverseMap.
alpar@572
  2094
      explicit InverseMap(const CrossRefMap& inverted)
deba@220
  2095
        : _inverted(inverted) {}
deba@220
  2096
deba@220
  2097
      /// The value type of the InverseMap.
alpar@572
  2098
      typedef typename CrossRefMap::Key Value;
deba@220
  2099
      /// The key type of the InverseMap.
alpar@572
  2100
      typedef typename CrossRefMap::Value Key;
deba@220
  2101
deba@220
  2102
      /// \brief Subscript operator.
deba@220
  2103
      ///
kpeter@684
  2104
      /// Subscript operator. It gives back an item
kpeter@684
  2105
      /// that is assigned to the given value or \c INVALID
kpeter@684
  2106
      /// if no such item exists.
deba@220
  2107
      Value operator[](const Key& key) const {
deba@220
  2108
        return _inverted(key);
deba@220
  2109
      }
deba@220
  2110
deba@220
  2111
    private:
alpar@572
  2112
      const CrossRefMap& _inverted;
deba@220
  2113
    };
deba@220
  2114
kpeter@559
  2115
    /// \brief It gives back the read-only inverse map.
deba@220
  2116
    ///
kpeter@559
  2117
    /// It gives back the read-only inverse map.
deba@220
  2118
    InverseMap inverse() const {
deba@220
  2119
      return InverseMap(*this);
deba@220
  2120
    }
deba@220
  2121
deba@220
  2122
  };
deba@220
  2123
alpar@572
  2124
  /// \brief Provides continuous and unique ID for the
alpar@572
  2125
  /// items of a graph.
deba@220
  2126
  ///
alpar@572
  2127
  /// RangeIdMap provides a unique and continuous
alpar@572
  2128
  /// ID for each item of a given type (\c Node, \c Arc or
kpeter@559
  2129
  /// \c Edge) in a graph. This id is
kpeter@559
  2130
  ///  - \b unique: different items get different ids,
kpeter@559
  2131
  ///  - \b continuous: the range of the ids is the set of integers
kpeter@559
  2132
  ///    between 0 and \c n-1, where \c n is the number of the items of
alpar@572
  2133
  ///    this type (\c Node, \c Arc or \c Edge).
alpar@572
  2134
  ///  - So, the ids can change when deleting an item of the same type.
deba@220
  2135
  ///
kpeter@559
  2136
  /// Thus this id is not (necessarily) the same as what can get using
kpeter@559
  2137
  /// the \c id() function of the graph or \ref IdMap.
kpeter@559
  2138
  /// This map can be inverted with its member class \c InverseMap,
kpeter@559
  2139
  /// or with the \c operator() member.
kpeter@559
  2140
  ///
kpeter@559
  2141
  /// \tparam GR The graph type.
kpeter@559
  2142
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@559
  2143
  /// \c GR::Edge).
kpeter@559
  2144
  ///
kpeter@559
  2145
  /// \see IdMap
kpeter@559
  2146
  template <typename GR, typename K>
alpar@572
  2147
  class RangeIdMap
kpeter@559
  2148
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
kpeter@559
  2149
kpeter@559
  2150
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
deba@220
  2151
deba@220
  2152
  public:
alpar@572
  2153
    /// The graph type of RangeIdMap.
kpeter@559
  2154
    typedef GR Graph;
kpeter@617
  2155
    typedef GR Digraph;
alpar@572
  2156
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
kpeter@559
  2157
    typedef K Item;
alpar@572
  2158
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
kpeter@559
  2159
    typedef K Key;
alpar@572
  2160
    /// The value type of RangeIdMap.
kpeter@559
  2161
    typedef int Value;
deba@220
  2162
deba@220
  2163
    /// \brief Constructor.
deba@220
  2164
    ///
alpar@572
  2165
    /// Constructor.
alpar@572
  2166
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
deba@220
  2167
      Item it;
deba@220
  2168
      const typename Map::Notifier* nf = Map::notifier();
deba@220
  2169
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  2170
        Map::set(it, _inv_map.size());
deba@220
  2171
        _inv_map.push_back(it);
deba@220
  2172
      }
deba@220
  2173
    }
deba@220
  2174
deba@220
  2175
  protected:
deba@220
  2176
kpeter@559
  2177
    /// \brief Adds a new key to the map.
deba@220
  2178
    ///
deba@220
  2179
    /// Add a new key to the map. It is called by the
deba@220
  2180
    /// \c AlterationNotifier.
deba@220
  2181
    virtual void add(const Item& item) {
deba@220
  2182
      Map::add(item);
deba@220
  2183
      Map::set(item, _inv_map.size());
deba@220
  2184
      _inv_map.push_back(item);
deba@220
  2185
    }
deba@220
  2186
deba@220
  2187
    /// \brief Add more new keys to the map.
deba@220
  2188
    ///
deba@220
  2189
    /// Add more new keys to the map. It is called by the
deba@220
  2190
    /// \c AlterationNotifier.
deba@220
  2191
    virtual void add(const std::vector<Item>& items) {
deba@220
  2192
      Map::add(items);
deba@220
  2193
      for (int i = 0; i < int(items.size()); ++i) {
deba@220
  2194
        Map::set(items[i], _inv_map.size());
deba@220
  2195
        _inv_map.push_back(items[i]);
deba@220
  2196
      }
deba@220
  2197
    }
deba@220
  2198
deba@220
  2199
    /// \brief Erase the key from the map.
deba@220
  2200
    ///
deba@220
  2201
    /// Erase the key from the map. It is called by the
deba@220
  2202
    /// \c AlterationNotifier.
deba@220
  2203
    virtual void erase(const Item& item) {
deba@220
  2204
      Map::set(_inv_map.back(), Map::operator[](item));
deba@220
  2205
      _inv_map[Map::operator[](item)] = _inv_map.back();
deba@220
  2206
      _inv_map.pop_back();
deba@220
  2207
      Map::erase(item);
deba@220
  2208
    }
deba@220
  2209
deba@220
  2210
    /// \brief Erase more keys from the map.
deba@220
  2211
    ///
deba@220
  2212
    /// Erase more keys from the map. It is called by the
deba@220
  2213
    /// \c AlterationNotifier.
deba@220
  2214
    virtual void erase(const std::vector<Item>& items) {
deba@220
  2215
      for (int i = 0; i < int(items.size()); ++i) {
deba@220
  2216
        Map::set(_inv_map.back(), Map::operator[](items[i]));
deba@220
  2217
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
deba@220
  2218
        _inv_map.pop_back();
deba@220
  2219
      }
deba@220
  2220
      Map::erase(items);
deba@220
  2221
    }
deba@220
  2222
deba@220
  2223
    /// \brief Build the unique map.
deba@220
  2224
    ///
deba@220
  2225
    /// Build the unique map. It is called by the
deba@220
  2226
    /// \c AlterationNotifier.
deba@220
  2227
    virtual void build() {
deba@220
  2228
      Map::build();
deba@220
  2229
      Item it;
deba@220
  2230
      const typename Map::Notifier* nf = Map::notifier();
deba@220
  2231
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  2232
        Map::set(it, _inv_map.size());
deba@220
  2233
        _inv_map.push_back(it);
deba@220
  2234
      }
deba@220
  2235
    }
deba@220
  2236
deba@220
  2237
    /// \brief Clear the keys from the map.
deba@220
  2238
    ///
deba@220
  2239
    /// Clear the keys from the map. It is called by the
deba@220
  2240
    /// \c AlterationNotifier.
deba@220
  2241
    virtual void clear() {
deba@220
  2242
      _inv_map.clear();
deba@220
  2243
      Map::clear();
deba@220
  2244
    }
deba@220
  2245
deba@220
  2246
  public:
deba@220
  2247
deba@220
  2248
    /// \brief Returns the maximal value plus one.
deba@220
  2249
    ///
deba@220
  2250
    /// Returns the maximal value plus one in the map.
deba@220
  2251
    unsigned int size() const {
deba@220
  2252
      return _inv_map.size();
deba@220
  2253
    }
deba@220
  2254
deba@220
  2255
    /// \brief Swaps the position of the two items in the map.
deba@220
  2256
    ///
deba@220
  2257
    /// Swaps the position of the two items in the map.
deba@220
  2258
    void swap(const Item& p, const Item& q) {
deba@220
  2259
      int pi = Map::operator[](p);
deba@220
  2260
      int qi = Map::operator[](q);
deba@220
  2261
      Map::set(p, qi);
deba@220
  2262
      _inv_map[qi] = p;
deba@220
  2263
      Map::set(q, pi);
deba@220
  2264
      _inv_map[pi] = q;
deba@220
  2265
    }
deba@220
  2266
alpar@572
  2267
    /// \brief Gives back the \e RangeId of the item
deba@220
  2268
    ///
alpar@572
  2269
    /// Gives back the \e RangeId of the item.
deba@220
  2270
    int operator[](const Item& item) const {
deba@220
  2271
      return Map::operator[](item);
deba@220
  2272
    }
deba@220
  2273
alpar@572
  2274
    /// \brief Gives back the item belonging to a \e RangeId
deba@693
  2275
    ///
alpar@572
  2276
    /// Gives back the item belonging to a \e RangeId.
deba@220
  2277
    Item operator()(int id) const {
deba@220
  2278
      return _inv_map[id];
deba@220
  2279
    }
deba@220
  2280
deba@220
  2281
  private:
deba@220
  2282
deba@220
  2283
    typedef std::vector<Item> Container;
deba@220
  2284
    Container _inv_map;
deba@220
  2285
deba@220
  2286
  public:
kpeter@559
  2287
alpar@572
  2288
    /// \brief The inverse map type of RangeIdMap.
deba@220
  2289
    ///
alpar@572
  2290
    /// The inverse map type of RangeIdMap.
deba@220
  2291
    class InverseMap {
deba@220
  2292
    public:
kpeter@559
  2293
      /// \brief Constructor
deba@220
  2294
      ///
deba@220
  2295
      /// Constructor of the InverseMap.
alpar@572
  2296
      explicit InverseMap(const RangeIdMap& inverted)
deba@220
  2297
        : _inverted(inverted) {}
deba@220
  2298
deba@220
  2299
deba@220
  2300
      /// The value type of the InverseMap.
alpar@572
  2301
      typedef typename RangeIdMap::Key Value;
deba@220
  2302
      /// The key type of the InverseMap.
alpar@572
  2303
      typedef typename RangeIdMap::Value Key;
deba@220
  2304
deba@220
  2305
      /// \brief Subscript operator.
deba@220
  2306
      ///
deba@220
  2307
      /// Subscript operator. It gives back the item
kpeter@559
  2308
      /// that the descriptor currently belongs to.
deba@220
  2309
      Value operator[](const Key& key) const {
deba@220
  2310
        return _inverted(key);
deba@220
  2311
      }
deba@220
  2312
deba@220
  2313
      /// \brief Size of the map.
deba@220
  2314
      ///
deba@220
  2315
      /// Returns the size of the map.
deba@220
  2316
      unsigned int size() const {
deba@220
  2317
        return _inverted.size();
deba@220
  2318
      }
deba@220
  2319
deba@220
  2320
    private:
alpar@572
  2321
      const RangeIdMap& _inverted;
deba@220
  2322
    };
deba@220
  2323
deba@220
  2324
    /// \brief Gives back the inverse of the map.
deba@220
  2325
    ///
deba@220
  2326
    /// Gives back the inverse of the map.
deba@220
  2327
    const InverseMap inverse() const {
deba@220
  2328
      return InverseMap(*this);
deba@220
  2329
    }
deba@220
  2330
  };
deba@220
  2331
kpeter@694
  2332
  /// \brief Dynamic iterable \c bool map.
deba@693
  2333
  ///
kpeter@694
  2334
  /// This class provides a special graph map type which can store a
kpeter@694
  2335
  /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
kpeter@694
  2336
  /// For both \c true and \c false values it is possible to iterate on
kpeter@694
  2337
  /// the keys.
deba@693
  2338
  ///
kpeter@694
  2339
  /// This type is a reference map, so it can be modified with the
alpar@695
  2340
  /// subscript operator.
kpeter@694
  2341
  ///
kpeter@694
  2342
  /// \tparam GR The graph type.
kpeter@694
  2343
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@694
  2344
  /// \c GR::Edge).
kpeter@694
  2345
  ///
kpeter@694
  2346
  /// \see IterableIntMap, IterableValueMap
kpeter@694
  2347
  /// \see CrossRefMap
kpeter@694
  2348
  template <typename GR, typename K>
deba@693
  2349
  class IterableBoolMap
kpeter@694
  2350
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
deba@693
  2351
  private:
deba@693
  2352
    typedef GR Graph;
deba@693
  2353
kpeter@694
  2354
    typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
kpeter@694
  2355
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
kpeter@694
  2356
kpeter@694
  2357
    std::vector<K> _array;
deba@693
  2358
    int _sep;
deba@693
  2359
deba@693
  2360
  public:
deba@693
  2361
kpeter@694
  2362
    /// Indicates that the map is reference map.
deba@693
  2363
    typedef True ReferenceMapTag;
deba@693
  2364
deba@693
  2365
    /// The key type
kpeter@694
  2366
    typedef K Key;
deba@693
  2367
    /// The value type
deba@693
  2368
    typedef bool Value;
deba@693
  2369
    /// The const reference type.
deba@693
  2370
    typedef const Value& ConstReference;
deba@693
  2371
deba@693
  2372
  private:
deba@693
  2373
deba@693
  2374
    int position(const Key& key) const {
deba@693
  2375
      return Parent::operator[](key);
deba@693
  2376
    }
deba@693
  2377
deba@693
  2378
  public:
deba@693
  2379
kpeter@694
  2380
    /// \brief Reference to the value of the map.
deba@693
  2381
    ///
kpeter@694
  2382
    /// This class is similar to the \c bool type. It can be converted to
kpeter@694
  2383
    /// \c bool and it provides the same operators.
deba@693
  2384
    class Reference {
deba@693
  2385
      friend class IterableBoolMap;
deba@693
  2386
    private:
deba@693
  2387
      Reference(IterableBoolMap& map, const Key& key)
deba@693
  2388
        : _key(key), _map(map) {}
deba@693
  2389
    public:
deba@693
  2390
deba@693
  2391
      Reference& operator=(const Reference& value) {
deba@693
  2392
        _map.set(_key, static_cast<bool>(value));
deba@693
  2393
         return *this;
deba@693
  2394
      }
deba@693
  2395
deba@693
  2396
      operator bool() const {
deba@693
  2397
        return static_cast<const IterableBoolMap&>(_map)[_key];
deba@693
  2398
      }
deba@693
  2399
deba@693
  2400
      Reference& operator=(bool value) {
deba@693
  2401
        _map.set(_key, value);
deba@693
  2402
        return *this;
deba@693
  2403
      }
deba@693
  2404
      Reference& operator&=(bool value) {
deba@693
  2405
        _map.set(_key, _map[_key] & value);
deba@693
  2406
        return *this;
deba@693
  2407
      }
deba@693
  2408
      Reference& operator|=(bool value) {
deba@693
  2409
        _map.set(_key, _map[_key] | value);
deba@693
  2410
        return *this;
deba@693
  2411
      }
deba@693
  2412
      Reference& operator^=(bool value) {
deba@693
  2413
        _map.set(_key, _map[_key] ^ value);
deba@693
  2414
        return *this;
deba@693
  2415
      }
deba@693
  2416
    private:
deba@693
  2417
      Key _key;
deba@693
  2418
      IterableBoolMap& _map;
deba@693
  2419
    };
deba@693
  2420
deba@693
  2421
    /// \brief Constructor of the map with a default value.
deba@693
  2422
    ///
deba@693
  2423
    /// Constructor of the map with a default value.
deba@693
  2424
    explicit IterableBoolMap(const Graph& graph, bool def = false)
deba@693
  2425
      : Parent(graph) {
deba@693
  2426
      typename Parent::Notifier* nf = Parent::notifier();
deba@693
  2427
      Key it;
deba@693
  2428
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@693
  2429
        Parent::set(it, _array.size());
deba@693
  2430
        _array.push_back(it);
deba@693
  2431
      }
deba@693
  2432
      _sep = (def ? _array.size() : 0);
deba@693
  2433
    }
deba@693
  2434
deba@693
  2435
    /// \brief Const subscript operator of the map.
deba@693
  2436
    ///
deba@693
  2437
    /// Const subscript operator of the map.
deba@693
  2438
    bool operator[](const Key& key) const {
deba@693
  2439
      return position(key) < _sep;
deba@693
  2440
    }
deba@693
  2441
deba@693
  2442
    /// \brief Subscript operator of the map.
deba@693
  2443
    ///
deba@693
  2444
    /// Subscript operator of the map.
deba@693
  2445
    Reference operator[](const Key& key) {
deba@693
  2446
      return Reference(*this, key);
deba@693
  2447
    }
deba@693
  2448
deba@693
  2449
    /// \brief Set operation of the map.
deba@693
  2450
    ///
deba@693
  2451
    /// Set operation of the map.
deba@693
  2452
    void set(const Key& key, bool value) {
deba@693
  2453
      int pos = position(key);
deba@693
  2454
      if (value) {
deba@693
  2455
        if (pos < _sep) return;
deba@693
  2456
        Key tmp = _array[_sep];
deba@693
  2457
        _array[_sep] = key;
deba@693
  2458
        Parent::set(key, _sep);
deba@693
  2459
        _array[pos] = tmp;
deba@693
  2460
        Parent::set(tmp, pos);
deba@693
  2461
        ++_sep;
deba@693
  2462
      } else {
deba@693
  2463
        if (pos >= _sep) return;
deba@693
  2464
        --_sep;
deba@693
  2465
        Key tmp = _array[_sep];
deba@693
  2466
        _array[_sep] = key;
deba@693
  2467
        Parent::set(key, _sep);
deba@693
  2468
        _array[pos] = tmp;
deba@693
  2469
        Parent::set(tmp, pos);
deba@693
  2470
      }
deba@693
  2471
    }
deba@693
  2472
deba@693
  2473
    /// \brief Set all items.
deba@693
  2474
    ///
deba@693
  2475
    /// Set all items in the map.
deba@693
  2476
    /// \note Constant time operation.
deba@693
  2477
    void setAll(bool value) {
deba@693
  2478
      _sep = (value ? _array.size() : 0);
deba@693
  2479
    }
deba@693
  2480
kpeter@694
  2481
    /// \brief Returns the number of the keys mapped to \c true.
deba@693
  2482
    ///
kpeter@694
  2483
    /// Returns the number of the keys mapped to \c true.
deba@693
  2484
    int trueNum() const {
deba@693
  2485
      return _sep;
deba@693
  2486
    }
deba@693
  2487
kpeter@694
  2488
    /// \brief Returns the number of the keys mapped to \c false.
deba@693
  2489
    ///
kpeter@694
  2490
    /// Returns the number of the keys mapped to \c false.
deba@693
  2491
    int falseNum() const {
deba@693
  2492
      return _array.size() - _sep;
deba@693
  2493
    }
deba@693
  2494
kpeter@694
  2495
    /// \brief Iterator for the keys mapped to \c true.
deba@693
  2496
    ///
kpeter@694
  2497
    /// Iterator for the keys mapped to \c true. It works
kpeter@694
  2498
    /// like a graph item iterator, it can be converted to
deba@693
  2499
    /// the key type of the map, incremented with \c ++ operator, and
kpeter@694
  2500
    /// if the iterator leaves the last valid key, it will be equal to
deba@693
  2501
    /// \c INVALID.
deba@693
  2502
    class TrueIt : public Key {
deba@693
  2503
    public:
deba@693
  2504
      typedef Key Parent;
deba@693
  2505
deba@693
  2506
      /// \brief Creates an iterator.
deba@693
  2507
      ///
deba@693
  2508
      /// Creates an iterator. It iterates on the
kpeter@694
  2509
      /// keys mapped to \c true.
kpeter@694
  2510
      /// \param map The IterableBoolMap.
deba@693
  2511
      explicit TrueIt(const IterableBoolMap& map)
deba@693
  2512
        : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
deba@693
  2513
          _map(&map) {}
deba@693
  2514
deba@693
  2515
      /// \brief Invalid constructor \& conversion.
deba@693
  2516
      ///
kpeter@694
  2517
      /// This constructor initializes the iterator to be invalid.
deba@693
  2518
      /// \sa Invalid for more details.
deba@693
  2519
      TrueIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2520
deba@693
  2521
      /// \brief Increment operator.
deba@693
  2522
      ///
kpeter@694
  2523
      /// Increment operator.
deba@693
  2524
      TrueIt& operator++() {
deba@693
  2525
        int pos = _map->position(*this);
deba@693
  2526
        Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
deba@693
  2527
        return *this;
deba@693
  2528
      }
deba@693
  2529
deba@693
  2530
    private:
deba@693
  2531
      const IterableBoolMap* _map;
deba@693
  2532
    };
deba@693
  2533
kpeter@694
  2534
    /// \brief Iterator for the keys mapped to \c false.
deba@693
  2535
    ///
kpeter@694
  2536
    /// Iterator for the keys mapped to \c false. It works
kpeter@694
  2537
    /// like a graph item iterator, it can be converted to
deba@693
  2538
    /// the key type of the map, incremented with \c ++ operator, and
kpeter@694
  2539
    /// if the iterator leaves the last valid key, it will be equal to
deba@693
  2540
    /// \c INVALID.
deba@693
  2541
    class FalseIt : public Key {
deba@693
  2542
    public:
deba@693
  2543
      typedef Key Parent;
deba@693
  2544
deba@693
  2545
      /// \brief Creates an iterator.
deba@693
  2546
      ///
deba@693
  2547
      /// Creates an iterator. It iterates on the
kpeter@694
  2548
      /// keys mapped to \c false.
kpeter@694
  2549
      /// \param map The IterableBoolMap.
deba@693
  2550
      explicit FalseIt(const IterableBoolMap& map)
deba@693
  2551
        : Parent(map._sep < int(map._array.size()) ?
deba@693
  2552
                 map._array.back() : INVALID), _map(&map) {}
deba@693
  2553
deba@693
  2554
      /// \brief Invalid constructor \& conversion.
deba@693
  2555
      ///
kpeter@694
  2556
      /// This constructor initializes the iterator to be invalid.
deba@693
  2557
      /// \sa Invalid for more details.
deba@693
  2558
      FalseIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2559
deba@693
  2560
      /// \brief Increment operator.
deba@693
  2561
      ///
kpeter@694
  2562
      /// Increment operator.
deba@693
  2563
      FalseIt& operator++() {
deba@693
  2564
        int pos = _map->position(*this);
deba@693
  2565
        Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
deba@693
  2566
        return *this;
deba@693
  2567
      }
deba@693
  2568
deba@693
  2569
    private:
deba@693
  2570
      const IterableBoolMap* _map;
deba@693
  2571
    };
deba@693
  2572
deba@693
  2573
    /// \brief Iterator for the keys mapped to a given value.
deba@693
  2574
    ///
deba@693
  2575
    /// Iterator for the keys mapped to a given value. It works
kpeter@694
  2576
    /// like a graph item iterator, it can be converted to
deba@693
  2577
    /// the key type of the map, incremented with \c ++ operator, and
kpeter@694
  2578
    /// if the iterator leaves the last valid key, it will be equal to
deba@693
  2579
    /// \c INVALID.
deba@693
  2580
    class ItemIt : public Key {
deba@693
  2581
    public:
deba@693
  2582
      typedef Key Parent;
deba@693
  2583
kpeter@694
  2584
      /// \brief Creates an iterator with a value.
deba@693
  2585
      ///
kpeter@694
  2586
      /// Creates an iterator with a value. It iterates on the
kpeter@694
  2587
      /// keys mapped to the given value.
kpeter@694
  2588
      /// \param map The IterableBoolMap.
kpeter@694
  2589
      /// \param value The value.
deba@693
  2590
      ItemIt(const IterableBoolMap& map, bool value)
deba@693
  2591
        : Parent(value ? 
deba@693
  2592
                 (map._sep > 0 ?
deba@693
  2593
                  map._array[map._sep - 1] : INVALID) :
deba@693
  2594
                 (map._sep < int(map._array.size()) ?
deba@693
  2595
                  map._array.back() : INVALID)), _map(&map) {}
deba@693
  2596
deba@693
  2597
      /// \brief Invalid constructor \& conversion.
deba@693
  2598
      ///
kpeter@694
  2599
      /// This constructor initializes the iterator to be invalid.
deba@693
  2600
      /// \sa Invalid for more details.
deba@693
  2601
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2602
deba@693
  2603
      /// \brief Increment operator.
deba@693
  2604
      ///
kpeter@694
  2605
      /// Increment operator.
deba@693
  2606
      ItemIt& operator++() {
deba@693
  2607
        int pos = _map->position(*this);
deba@693
  2608
        int _sep = pos >= _map->_sep ? _map->_sep : 0;
deba@693
  2609
        Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
deba@693
  2610
        return *this;
deba@693
  2611
      }
deba@693
  2612
deba@693
  2613
    private:
deba@693
  2614
      const IterableBoolMap* _map;
deba@693
  2615
    };
deba@693
  2616
deba@693
  2617
  protected:
deba@693
  2618
deba@693
  2619
    virtual void add(const Key& key) {
deba@693
  2620
      Parent::add(key);
deba@693
  2621
      Parent::set(key, _array.size());
deba@693
  2622
      _array.push_back(key);
deba@693
  2623
    }
deba@693
  2624
deba@693
  2625
    virtual void add(const std::vector<Key>& keys) {
deba@693
  2626
      Parent::add(keys);
deba@693
  2627
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  2628
        Parent::set(keys[i], _array.size());
deba@693
  2629
        _array.push_back(keys[i]);
deba@693
  2630
      }
deba@693
  2631
    }
deba@693
  2632
deba@693
  2633
    virtual void erase(const Key& key) {
deba@693
  2634
      int pos = position(key);
deba@693
  2635
      if (pos < _sep) {
deba@693
  2636
        --_sep;
deba@693
  2637
        Parent::set(_array[_sep], pos);
deba@693
  2638
        _array[pos] = _array[_sep];
deba@693
  2639
        Parent::set(_array.back(), _sep);
deba@693
  2640
        _array[_sep] = _array.back();
deba@693
  2641
        _array.pop_back();
deba@693
  2642
      } else {
deba@693
  2643
        Parent::set(_array.back(), pos);
deba@693
  2644
        _array[pos] = _array.back();
deba@693
  2645
        _array.pop_back();
deba@693
  2646
      }
deba@693
  2647
      Parent::erase(key);
deba@693
  2648
    }
deba@693
  2649
deba@693
  2650
    virtual void erase(const std::vector<Key>& keys) {
deba@693
  2651
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  2652
        int pos = position(keys[i]);
deba@693
  2653
        if (pos < _sep) {
deba@693
  2654
          --_sep;
deba@693
  2655
          Parent::set(_array[_sep], pos);
deba@693
  2656
          _array[pos] = _array[_sep];
deba@693
  2657
          Parent::set(_array.back(), _sep);
deba@693
  2658
          _array[_sep] = _array.back();
deba@693
  2659
          _array.pop_back();
deba@693
  2660
        } else {
deba@693
  2661
          Parent::set(_array.back(), pos);
deba@693
  2662
          _array[pos] = _array.back();
deba@693
  2663
          _array.pop_back();
deba@693
  2664
        }
deba@693
  2665
      }
deba@693
  2666
      Parent::erase(keys);
deba@693
  2667
    }
deba@693
  2668
deba@693
  2669
    virtual void build() {
deba@693
  2670
      Parent::build();
deba@693
  2671
      typename Parent::Notifier* nf = Parent::notifier();
deba@693
  2672
      Key it;
deba@693
  2673
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@693
  2674
        Parent::set(it, _array.size());
deba@693
  2675
        _array.push_back(it);
deba@693
  2676
      }
deba@693
  2677
      _sep = 0;
deba@693
  2678
    }
deba@693
  2679
deba@693
  2680
    virtual void clear() {
deba@693
  2681
      _array.clear();
deba@693
  2682
      _sep = 0;
deba@693
  2683
      Parent::clear();
deba@693
  2684
    }
deba@693
  2685
deba@693
  2686
  };
deba@693
  2687
deba@693
  2688
deba@693
  2689
  namespace _maps_bits {
deba@693
  2690
    template <typename Item>
deba@693
  2691
    struct IterableIntMapNode {
deba@693
  2692
      IterableIntMapNode() : value(-1) {}
deba@693
  2693
      IterableIntMapNode(int _value) : value(_value) {}
deba@693
  2694
      Item prev, next;
deba@693
  2695
      int value;
deba@693
  2696
    };
deba@693
  2697
  }
deba@693
  2698
deba@693
  2699
  /// \brief Dynamic iterable integer map.
deba@693
  2700
  ///
kpeter@694
  2701
  /// This class provides a special graph map type which can store an
kpeter@694
  2702
  /// integer value for graph items (\c Node, \c Arc or \c Edge).
kpeter@694
  2703
  /// For each non-negative value it is possible to iterate on the keys
kpeter@694
  2704
  /// mapped to the value.
deba@693
  2705
  ///
kpeter@694
  2706
  /// This type is a reference map, so it can be modified with the
alpar@695
  2707
  /// subscript operator.
kpeter@694
  2708
  ///
kpeter@694
  2709
  /// \note The size of the data structure depends on the largest
deba@693
  2710
  /// value in the map.
deba@693
  2711
  ///
kpeter@694
  2712
  /// \tparam GR The graph type.
kpeter@694
  2713
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@694
  2714
  /// \c GR::Edge).
kpeter@694
  2715
  ///
kpeter@694
  2716
  /// \see IterableBoolMap, IterableValueMap
kpeter@694
  2717
  /// \see CrossRefMap
kpeter@694
  2718
  template <typename GR, typename K>
deba@693
  2719
  class IterableIntMap
kpeter@694
  2720
    : protected ItemSetTraits<GR, K>::
kpeter@694
  2721
        template Map<_maps_bits::IterableIntMapNode<K> >::Type {
deba@693
  2722
  public:
kpeter@694
  2723
    typedef typename ItemSetTraits<GR, K>::
kpeter@694
  2724
      template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
deba@693
  2725
deba@693
  2726
    /// The key type
kpeter@694
  2727
    typedef K Key;
deba@693
  2728
    /// The value type
deba@693
  2729
    typedef int Value;
deba@693
  2730
    /// The graph type
deba@693
  2731
    typedef GR Graph;
deba@693
  2732
deba@693
  2733
    /// \brief Constructor of the map.
deba@693
  2734
    ///
kpeter@694
  2735
    /// Constructor of the map. It sets all values to -1.
deba@693
  2736
    explicit IterableIntMap(const Graph& graph)
deba@693
  2737
      : Parent(graph) {}
deba@693
  2738
deba@693
  2739
    /// \brief Constructor of the map with a given value.
deba@693
  2740
    ///
deba@693
  2741
    /// Constructor of the map with a given value.
deba@693
  2742
    explicit IterableIntMap(const Graph& graph, int value)
kpeter@694
  2743
      : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
deba@693
  2744
      if (value >= 0) {
deba@693
  2745
        for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@693
  2746
          lace(it);
deba@693
  2747
        }
deba@693
  2748
      }
deba@693
  2749
    }
deba@693
  2750
deba@693
  2751
  private:
deba@693
  2752
deba@693
  2753
    void unlace(const Key& key) {
deba@693
  2754
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  2755
      if (node.value < 0) return;
deba@693
  2756
      if (node.prev != INVALID) {
deba@693
  2757
        Parent::operator[](node.prev).next = node.next;
deba@693
  2758
      } else {
deba@693
  2759
        _first[node.value] = node.next;
deba@693
  2760
      }
deba@693
  2761
      if (node.next != INVALID) {
deba@693
  2762
        Parent::operator[](node.next).prev = node.prev;
deba@693
  2763
      }
deba@693
  2764
      while (!_first.empty() && _first.back() == INVALID) {
deba@693
  2765
        _first.pop_back();
deba@693
  2766
      }
deba@693
  2767
    }
deba@693
  2768
deba@693
  2769
    void lace(const Key& key) {
deba@693
  2770
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  2771
      if (node.value < 0) return;
deba@693
  2772
      if (node.value >= int(_first.size())) {
deba@693
  2773
        _first.resize(node.value + 1, INVALID);
deba@693
  2774
      }
deba@693
  2775
      node.prev = INVALID;
deba@693
  2776
      node.next = _first[node.value];
deba@693
  2777
      if (node.next != INVALID) {
deba@693
  2778
        Parent::operator[](node.next).prev = key;
deba@693
  2779
      }
deba@693
  2780
      _first[node.value] = key;
deba@693
  2781
    }
deba@693
  2782
deba@693
  2783
  public:
deba@693
  2784
kpeter@694
  2785
    /// Indicates that the map is reference map.
deba@693
  2786
    typedef True ReferenceMapTag;
deba@693
  2787
kpeter@694
  2788
    /// \brief Reference to the value of the map.
deba@693
  2789
    ///
kpeter@694
  2790
    /// This class is similar to the \c int type. It can
kpeter@694
  2791
    /// be converted to \c int and it has the same operators.
deba@693
  2792
    class Reference {
deba@693
  2793
      friend class IterableIntMap;
deba@693
  2794
    private:
deba@693
  2795
      Reference(IterableIntMap& map, const Key& key)
deba@693
  2796
        : _key(key), _map(map) {}
deba@693
  2797
    public:
deba@693
  2798
deba@693
  2799
      Reference& operator=(const Reference& value) {
deba@693
  2800
        _map.set(_key, static_cast<const int&>(value));
deba@693
  2801
         return *this;
deba@693
  2802
      }
deba@693
  2803
deba@693
  2804
      operator const int&() const {
deba@693
  2805
        return static_cast<const IterableIntMap&>(_map)[_key];
deba@693
  2806
      }
deba@693
  2807
deba@693
  2808
      Reference& operator=(int value) {
deba@693
  2809
        _map.set(_key, value);
deba@693
  2810
        return *this;
deba@693
  2811
      }
deba@693
  2812
      Reference& operator++() {
deba@693
  2813
        _map.set(_key, _map[_key] + 1);
deba@693
  2814
        return *this;
deba@693
  2815
      }
deba@693
  2816
      int operator++(int) {
deba@693
  2817
        int value = _map[_key];
deba@693
  2818
        _map.set(_key, value + 1);
deba@693
  2819
        return value;
deba@693
  2820
      }
deba@693
  2821
      Reference& operator--() {
deba@693
  2822
        _map.set(_key, _map[_key] - 1);
deba@693
  2823
        return *this;
deba@693
  2824
      }
deba@693
  2825
      int operator--(int) {
deba@693
  2826
        int value = _map[_key];
deba@693
  2827
        _map.set(_key, value - 1);
deba@693
  2828
        return value;
deba@693
  2829
      }
deba@693
  2830
      Reference& operator+=(int value) {
deba@693
  2831
        _map.set(_key, _map[_key] + value);
deba@693
  2832
        return *this;
deba@693
  2833
      }
deba@693
  2834
      Reference& operator-=(int value) {
deba@693
  2835
        _map.set(_key, _map[_key] - value);
deba@693
  2836
        return *this;
deba@693
  2837
      }
deba@693
  2838
      Reference& operator*=(int value) {
deba@693
  2839
        _map.set(_key, _map[_key] * value);
deba@693
  2840
        return *this;
deba@693
  2841
      }
deba@693
  2842
      Reference& operator/=(int value) {
deba@693
  2843
        _map.set(_key, _map[_key] / value);
deba@693
  2844
        return *this;
deba@693
  2845
      }
deba@693
  2846
      Reference& operator%=(int value) {
deba@693
  2847
        _map.set(_key, _map[_key] % value);
deba@693
  2848
        return *this;
deba@693
  2849
      }
deba@693
  2850
      Reference& operator&=(int value) {
deba@693
  2851
        _map.set(_key, _map[_key] & value);
deba@693
  2852
        return *this;
deba@693
  2853
      }
deba@693
  2854
      Reference& operator|=(int value) {
deba@693
  2855
        _map.set(_key, _map[_key] | value);
deba@693
  2856
        return *this;
deba@693
  2857
      }
deba@693
  2858
      Reference& operator^=(int value) {
deba@693
  2859
        _map.set(_key, _map[_key] ^ value);
deba@693
  2860
        return *this;
deba@693
  2861
      }
deba@693
  2862
      Reference& operator<<=(int value) {
deba@693
  2863
        _map.set(_key, _map[_key] << value);
deba@693
  2864
        return *this;
deba@693
  2865
      }
deba@693
  2866
      Reference& operator>>=(int value) {
deba@693
  2867
        _map.set(_key, _map[_key] >> value);
deba@693
  2868
        return *this;
deba@693
  2869
      }
deba@693
  2870
deba@693
  2871
    private:
deba@693
  2872
      Key _key;
deba@693
  2873
      IterableIntMap& _map;
deba@693
  2874
    };
deba@693
  2875
deba@693
  2876
    /// The const reference type.
deba@693
  2877
    typedef const Value& ConstReference;
deba@693
  2878
deba@693
  2879
    /// \brief Gives back the maximal value plus one.
deba@693
  2880
    ///
deba@693
  2881
    /// Gives back the maximal value plus one.
deba@693
  2882
    int size() const {
deba@693
  2883
      return _first.size();
deba@693
  2884
    }
deba@693
  2885
deba@693
  2886
    /// \brief Set operation of the map.
deba@693
  2887
    ///
deba@693
  2888
    /// Set operation of the map.
deba@693
  2889
    void set(const Key& key, const Value& value) {
deba@693
  2890
      unlace(key);
deba@693
  2891
      Parent::operator[](key).value = value;
deba@693
  2892
      lace(key);
deba@693
  2893
    }
deba@693
  2894
deba@693
  2895
    /// \brief Const subscript operator of the map.
deba@693
  2896
    ///
deba@693
  2897
    /// Const subscript operator of the map.
deba@693
  2898
    const Value& operator[](const Key& key) const {
deba@693
  2899
      return Parent::operator[](key).value;
deba@693
  2900
    }
deba@693
  2901
deba@693
  2902
    /// \brief Subscript operator of the map.
deba@693
  2903
    ///
deba@693
  2904
    /// Subscript operator of the map.
deba@693
  2905
    Reference operator[](const Key& key) {
deba@693
  2906
      return Reference(*this, key);
deba@693
  2907
    }
deba@693
  2908
deba@693
  2909
    /// \brief Iterator for the keys with the same value.
deba@693
  2910
    ///
deba@693
  2911
    /// Iterator for the keys with the same value. It works
kpeter@694
  2912
    /// like a graph item iterator, it can be converted to
deba@693
  2913
    /// the item type of the map, incremented with \c ++ operator, and
kpeter@694
  2914
    /// if the iterator leaves the last valid item, it will be equal to
deba@693
  2915
    /// \c INVALID.
kpeter@694
  2916
    class ItemIt : public Key {
deba@693
  2917
    public:
kpeter@694
  2918
      typedef Key Parent;
deba@693
  2919
deba@693
  2920
      /// \brief Invalid constructor \& conversion.
deba@693
  2921
      ///
kpeter@694
  2922
      /// This constructor initializes the iterator to be invalid.
deba@693
  2923
      /// \sa Invalid for more details.
deba@693
  2924
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2925
deba@693
  2926
      /// \brief Creates an iterator with a value.
deba@693
  2927
      ///
deba@693
  2928
      /// Creates an iterator with a value. It iterates on the
kpeter@694
  2929
      /// keys mapped to the given value.
kpeter@694
  2930
      /// \param map The IterableIntMap.
kpeter@694
  2931
      /// \param value The value.
deba@693
  2932
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
deba@693
  2933
        if (value < 0 || value >= int(_map->_first.size())) {
deba@693
  2934
          Parent::operator=(INVALID);
deba@693
  2935
        } else {
deba@693
  2936
          Parent::operator=(_map->_first[value]);
deba@693
  2937
        }
deba@693
  2938
      }
deba@693
  2939
deba@693
  2940
      /// \brief Increment operator.
deba@693
  2941
      ///
kpeter@694
  2942
      /// Increment operator.
deba@693
  2943
      ItemIt& operator++() {
deba@693
  2944
        Parent::operator=(_map->IterableIntMap::Parent::
deba@693
  2945
                          operator[](static_cast<Parent&>(*this)).next);
deba@693
  2946
        return *this;
deba@693
  2947
      }
deba@693
  2948
deba@693
  2949
    private:
deba@693
  2950
      const IterableIntMap* _map;
deba@693
  2951
    };
deba@693
  2952
deba@693
  2953
  protected:
deba@693
  2954
deba@693
  2955
    virtual void erase(const Key& key) {
deba@693
  2956
      unlace(key);
deba@693
  2957
      Parent::erase(key);
deba@693
  2958
    }
deba@693
  2959
deba@693
  2960
    virtual void erase(const std::vector<Key>& keys) {
deba@693
  2961
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  2962
        unlace(keys[i]);
deba@693
  2963
      }
deba@693
  2964
      Parent::erase(keys);
deba@693
  2965
    }
deba@693
  2966
deba@693
  2967
    virtual void clear() {
deba@693
  2968
      _first.clear();
deba@693
  2969
      Parent::clear();
deba@693
  2970
    }
deba@693
  2971
deba@693
  2972
  private:
kpeter@694
  2973
    std::vector<Key> _first;
deba@693
  2974
  };
deba@693
  2975
deba@693
  2976
  namespace _maps_bits {
deba@693
  2977
    template <typename Item, typename Value>
deba@693
  2978
    struct IterableValueMapNode {
deba@693
  2979
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
deba@693
  2980
      Item prev, next;
deba@693
  2981
      Value value;
deba@693
  2982
    };
deba@693
  2983
  }
deba@693
  2984
deba@693
  2985
  /// \brief Dynamic iterable map for comparable values.
deba@693
  2986
  ///
kpeter@694
  2987
  /// This class provides a special graph map type which can store an
kpeter@694
  2988
  /// comparable value for graph items (\c Node, \c Arc or \c Edge).
kpeter@694
  2989
  /// For each value it is possible to iterate on the keys mapped to
kpeter@694
  2990
  /// the value.
kpeter@694
  2991
  ///
kpeter@694
  2992
  /// The map stores for each value a linked list with
deba@693
  2993
  /// the items which mapped to the value, and the values are stored
deba@693
  2994
  /// in balanced binary tree. The values of the map can be accessed
deba@693
  2995
  /// with stl compatible forward iterator.
deba@693
  2996
  ///
kpeter@694
  2997
  /// This type is not reference map, so it cannot be modified with
alpar@695
  2998
  /// the subscript operator.
deba@693
  2999
  ///
kpeter@694
  3000
  /// \tparam GR The graph type.
kpeter@694
  3001
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@694
  3002
  /// \c GR::Edge).
kpeter@694
  3003
  /// \tparam V The value type of the map. It can be any comparable
kpeter@694
  3004
  /// value type.
deba@693
  3005
  ///
kpeter@694
  3006
  /// \see IterableBoolMap, IterableIntMap
kpeter@694
  3007
  /// \see CrossRefMap
kpeter@694
  3008
  template <typename GR, typename K, typename V>
deba@693
  3009
  class IterableValueMap
kpeter@694
  3010
    : protected ItemSetTraits<GR, K>::
kpeter@694
  3011
        template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
deba@693
  3012
  public:
kpeter@694
  3013
    typedef typename ItemSetTraits<GR, K>::
kpeter@694
  3014
      template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
deba@693
  3015
deba@693
  3016
    /// The key type
kpeter@694
  3017
    typedef K Key;
deba@693
  3018
    /// The value type
kpeter@694
  3019
    typedef V Value;
deba@693
  3020
    /// The graph type
deba@693
  3021
    typedef GR Graph;
deba@693
  3022
deba@693
  3023
  public:
deba@693
  3024
kpeter@694
  3025
    /// \brief Constructor of the map with a given value.
deba@693
  3026
    ///
kpeter@694
  3027
    /// Constructor of the map with a given value.
deba@693
  3028
    explicit IterableValueMap(const Graph& graph,
deba@693
  3029
                              const Value& value = Value())
kpeter@694
  3030
      : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
deba@693
  3031
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@693
  3032
        lace(it);
deba@693
  3033
      }
deba@693
  3034
    }
deba@693
  3035
deba@693
  3036
  protected:
deba@693
  3037
deba@693
  3038
    void unlace(const Key& key) {
deba@693
  3039
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  3040
      if (node.prev != INVALID) {
deba@693
  3041
        Parent::operator[](node.prev).next = node.next;
deba@693
  3042
      } else {
deba@693
  3043
        if (node.next != INVALID) {
deba@693
  3044
          _first[node.value] = node.next;
deba@693
  3045
        } else {
deba@693
  3046
          _first.erase(node.value);
deba@693
  3047
        }
deba@693
  3048
      }
deba@693
  3049
      if (node.next != INVALID) {
deba@693
  3050
        Parent::operator[](node.next).prev = node.prev;
deba@693
  3051
      }
deba@693
  3052
    }
deba@693
  3053
deba@693
  3054
    void lace(const Key& key) {
deba@693
  3055
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  3056
      typename std::map<Value, Key>::iterator it = _first.find(node.value);
deba@693
  3057
      if (it == _first.end()) {
deba@693
  3058
        node.prev = node.next = INVALID;
deba@693
  3059
        _first.insert(std::make_pair(node.value, key));
deba@693
  3060
      } else {
deba@693
  3061
        node.prev = INVALID;
deba@693
  3062
        node.next = it->second;
deba@693
  3063
        if (node.next != INVALID) {
deba@693
  3064
          Parent::operator[](node.next).prev = key;
deba@693
  3065
        }
deba@693
  3066
        it->second = key;
deba@693
  3067
      }
deba@693
  3068
    }
deba@693
  3069
deba@693
  3070
  public:
deba@693
  3071
deba@693
  3072
    /// \brief Forward iterator for values.
deba@693
  3073
    ///
deba@693
  3074
    /// This iterator is an stl compatible forward
deba@693
  3075
    /// iterator on the values of the map. The values can
kpeter@694
  3076
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
deba@693
  3077
    class ValueIterator
deba@693
  3078
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@693
  3079
      friend class IterableValueMap;
deba@693
  3080
    private:
deba@693
  3081
      ValueIterator(typename std::map<Value, Key>::const_iterator _it)
deba@693
  3082
        : it(_it) {}
deba@693
  3083
    public:
deba@693
  3084
deba@693
  3085
      ValueIterator() {}
deba@693
  3086
deba@693
  3087
      ValueIterator& operator++() { ++it; return *this; }
deba@693
  3088
      ValueIterator operator++(int) {
deba@693
  3089
        ValueIterator tmp(*this);
deba@693
  3090
        operator++();
deba@693
  3091
        return tmp;
deba@693
  3092
      }
deba@693
  3093
deba@693
  3094
      const Value& operator*() const { return it->first; }
deba@693
  3095
      const Value* operator->() const { return &(it->first); }
deba@693
  3096
deba@693
  3097
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@693
  3098
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@693
  3099
deba@693
  3100
    private:
deba@693
  3101
      typename std::map<Value, Key>::const_iterator it;
deba@693
  3102
    };
deba@693
  3103
deba@693
  3104
    /// \brief Returns an iterator to the first value.
deba@693
  3105
    ///
deba@693
  3106
    /// Returns an stl compatible iterator to the
deba@693
  3107
    /// first value of the map. The values of the
kpeter@694
  3108
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@693
  3109
    /// range.
deba@693
  3110
    ValueIterator beginValue() const {
deba@693
  3111
      return ValueIterator(_first.begin());
deba@693
  3112
    }
deba@693
  3113
deba@693
  3114
    /// \brief Returns an iterator after the last value.
deba@693
  3115
    ///
deba@693
  3116
    /// Returns an stl compatible iterator after the
deba@693
  3117
    /// last value of the map. The values of the
kpeter@694
  3118
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@693
  3119
    /// range.
deba@693
  3120
    ValueIterator endValue() const {
deba@693
  3121
      return ValueIterator(_first.end());
deba@693
  3122
    }
deba@693
  3123
deba@693
  3124
    /// \brief Set operation of the map.
deba@693
  3125
    ///
deba@693
  3126
    /// Set operation of the map.
deba@693
  3127
    void set(const Key& key, const Value& value) {
deba@693
  3128
      unlace(key);
deba@693
  3129
      Parent::operator[](key).value = value;
deba@693
  3130
      lace(key);
deba@693
  3131
    }
deba@693
  3132
deba@693
  3133
    /// \brief Const subscript operator of the map.
deba@693
  3134
    ///
deba@693
  3135
    /// Const subscript operator of the map.
deba@693
  3136
    const Value& operator[](const Key& key) const {
deba@693
  3137
      return Parent::operator[](key).value;
deba@693
  3138
    }
deba@693
  3139
deba@693
  3140
    /// \brief Iterator for the keys with the same value.
deba@693
  3141
    ///
deba@693
  3142
    /// Iterator for the keys with the same value. It works
kpeter@694
  3143
    /// like a graph item iterator, it can be converted to
deba@693
  3144
    /// the item type of the map, incremented with \c ++ operator, and
kpeter@694
  3145
    /// if the iterator leaves the last valid item, it will be equal to
deba@693
  3146
    /// \c INVALID.
kpeter@694
  3147
    class ItemIt : public Key {
deba@693
  3148
    public:
kpeter@694
  3149
      typedef Key Parent;
deba@693
  3150
deba@693
  3151
      /// \brief Invalid constructor \& conversion.
deba@693
  3152
      ///
kpeter@694
  3153
      /// This constructor initializes the iterator to be invalid.
deba@693
  3154
      /// \sa Invalid for more details.
deba@693
  3155
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  3156
deba@693
  3157
      /// \brief Creates an iterator with a value.
deba@693
  3158
      ///
deba@693
  3159
      /// Creates an iterator with a value. It iterates on the
deba@693
  3160
      /// keys which have the given value.
deba@693
  3161
      /// \param map The IterableValueMap
deba@693
  3162
      /// \param value The value
deba@693
  3163
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
deba@693
  3164
        typename std::map<Value, Key>::const_iterator it =
deba@693
  3165
          map._first.find(value);
deba@693
  3166
        if (it == map._first.end()) {
deba@693
  3167
          Parent::operator=(INVALID);
deba@693
  3168
        } else {
deba@693
  3169
          Parent::operator=(it->second);
deba@693
  3170
        }
deba@693
  3171
      }
deba@693
  3172
deba@693
  3173
      /// \brief Increment operator.
deba@693
  3174
      ///
deba@693
  3175
      /// Increment Operator.
deba@693
  3176
      ItemIt& operator++() {
deba@693
  3177
        Parent::operator=(_map->IterableValueMap::Parent::
deba@693
  3178
                          operator[](static_cast<Parent&>(*this)).next);
deba@693
  3179
        return *this;
deba@693
  3180
      }
deba@693
  3181
deba@693
  3182
deba@693
  3183
    private:
deba@693
  3184
      const IterableValueMap* _map;
deba@693
  3185
    };
deba@693
  3186
deba@693
  3187
  protected:
deba@693
  3188
deba@693
  3189
    virtual void add(const Key& key) {
deba@693
  3190
      Parent::add(key);
deba@693
  3191
      unlace(key);
deba@693
  3192
    }
deba@693
  3193
deba@693
  3194
    virtual void add(const std::vector<Key>& keys) {
deba@693
  3195
      Parent::add(keys);
deba@693
  3196
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  3197
        lace(keys[i]);
deba@693
  3198
      }
deba@693
  3199
    }
deba@693
  3200
deba@693
  3201
    virtual void erase(const Key& key) {
deba@693
  3202
      unlace(key);
deba@693
  3203
      Parent::erase(key);
deba@693
  3204
    }
deba@693
  3205
deba@693
  3206
    virtual void erase(const std::vector<Key>& keys) {
deba@693
  3207
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  3208
        unlace(keys[i]);
deba@693
  3209
      }
deba@693
  3210
      Parent::erase(keys);
deba@693
  3211
    }
deba@693
  3212
deba@693
  3213
    virtual void build() {
deba@693
  3214
      Parent::build();
deba@693
  3215
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@693
  3216
        lace(it);
deba@693
  3217
      }
deba@693
  3218
    }
deba@693
  3219
deba@693
  3220
    virtual void clear() {
deba@693
  3221
      _first.clear();
deba@693
  3222
      Parent::clear();
deba@693
  3223
    }
deba@693
  3224
deba@693
  3225
  private:
deba@693
  3226
    std::map<Value, Key> _first;
deba@693
  3227
  };
deba@693
  3228
kpeter@559
  3229
  /// \brief Map of the source nodes of arcs in a digraph.
deba@220
  3230
  ///
kpeter@559
  3231
  /// SourceMap provides access for the source node of each arc in a digraph,
kpeter@559
  3232
  /// which is returned by the \c source() function of the digraph.
kpeter@559
  3233
  /// \tparam GR The digraph type.
deba@220
  3234
  /// \see TargetMap
kpeter@559
  3235
  template <typename GR>
deba@220
  3236
  class SourceMap {
deba@220
  3237
  public:
deba@220
  3238
kpeter@559
  3239
    ///\e
kpeter@559
  3240
    typedef typename GR::Arc Key;
kpeter@559
  3241
    ///\e
kpeter@559
  3242
    typedef typename GR::Node Value;
deba@220
  3243
deba@220
  3244
    /// \brief Constructor
deba@220
  3245
    ///
kpeter@559
  3246
    /// Constructor.
kpeter@313
  3247
    /// \param digraph The digraph that the map belongs to.
kpeter@559
  3248
    explicit SourceMap(const GR& digraph) : _graph(digraph) {}
kpeter@559
  3249
kpeter@559
  3250
    /// \brief Returns the source node of the given arc.
deba@220
  3251
    ///
kpeter@559
  3252
    /// Returns the source node of the given arc.
deba@220
  3253
    Value operator[](const Key& arc) const {
kpeter@559
  3254
      return _graph.source(arc);
deba@220
  3255
    }
deba@220
  3256
deba@220
  3257
  private:
kpeter@559
  3258
    const GR& _graph;
deba@220
  3259
  };
deba@220
  3260
kpeter@301
  3261
  /// \brief Returns a \c SourceMap class.
deba@220
  3262
  ///
kpeter@301
  3263
  /// This function just returns an \c SourceMap class.
deba@220
  3264
  /// \relates SourceMap
kpeter@559
  3265
  template <typename GR>
kpeter@559
  3266
  inline SourceMap<GR> sourceMap(const GR& graph) {
kpeter@559
  3267
    return SourceMap<GR>(graph);
deba@220
  3268
  }
deba@220
  3269
kpeter@559
  3270
  /// \brief Map of the target nodes of arcs in a digraph.
deba@220
  3271
  ///
kpeter@559
  3272
  /// TargetMap provides access for the target node of each arc in a digraph,
kpeter@559
  3273
  /// which is returned by the \c target() function of the digraph.
kpeter@559
  3274
  /// \tparam GR The digraph type.
deba@220
  3275
  /// \see SourceMap
kpeter@559
  3276
  template <typename GR>
deba@220
  3277
  class TargetMap {
deba@220
  3278
  public:
deba@220
  3279
kpeter@559
  3280
    ///\e
kpeter@559
  3281
    typedef typename GR::Arc Key;
kpeter@559
  3282
    ///\e
kpeter@559
  3283
    typedef typename GR::Node Value;
deba@220
  3284
deba@220
  3285
    /// \brief Constructor
deba@220
  3286
    ///
kpeter@559
  3287
    /// Constructor.
kpeter@313
  3288
    /// \param digraph The digraph that the map belongs to.
kpeter@559
  3289
    explicit TargetMap(const GR& digraph) : _graph(digraph) {}
kpeter@559
  3290
kpeter@559
  3291
    /// \brief Returns the target node of the given arc.
deba@220
  3292
    ///
kpeter@559
  3293
    /// Returns the target node of the given arc.
deba@220
  3294
    Value operator[](const Key& e) const {
kpeter@559
  3295
      return _graph.target(e);
deba@220
  3296
    }
deba@220
  3297
deba@220
  3298
  private:
kpeter@559
  3299
    const GR& _graph;
deba@220
  3300
  };
deba@220
  3301
kpeter@301
  3302
  /// \brief Returns a \c TargetMap class.
deba@220
  3303
  ///
kpeter@301
  3304
  /// This function just returns a \c TargetMap class.
deba@220
  3305
  /// \relates TargetMap
kpeter@559
  3306
  template <typename GR>
kpeter@559
  3307
  inline TargetMap<GR> targetMap(const GR& graph) {
kpeter@559
  3308
    return TargetMap<GR>(graph);
deba@220
  3309
  }
deba@220
  3310
kpeter@559
  3311
  /// \brief Map of the "forward" directed arc view of edges in a graph.
deba@220
  3312
  ///
kpeter@559
  3313
  /// ForwardMap provides access for the "forward" directed arc view of
kpeter@559
  3314
  /// each edge in a graph, which is returned by the \c direct() function
kpeter@559
  3315
  /// of the graph with \c true parameter.
kpeter@559
  3316
  /// \tparam GR The graph type.
deba@220
  3317
  /// \see BackwardMap
kpeter@559
  3318
  template <typename GR>
deba@220
  3319
  class ForwardMap {
deba@220
  3320
  public:
deba@220
  3321
kpeter@559
  3322
    typedef typename GR::Arc Value;
kpeter@559
  3323
    typedef typename GR::Edge Key;
deba@220
  3324
deba@220
  3325
    /// \brief Constructor
deba@220
  3326
    ///
kpeter@559
  3327
    /// Constructor.
kpeter@313
  3328
    /// \param graph The graph that the map belongs to.
kpeter@559
  3329
    explicit ForwardMap(const GR& graph) : _graph(graph) {}
kpeter@559
  3330
kpeter@559
  3331
    /// \brief Returns the "forward" directed arc view of the given edge.
deba@220
  3332
    ///
kpeter@559
  3333
    /// Returns the "forward" directed arc view of the given edge.
deba@220
  3334
    Value operator[](const Key& key) const {
deba@220
  3335
      return _graph.direct(key, true);
deba@220
  3336
    }
deba@220
  3337
deba@220
  3338
  private:
kpeter@559
  3339
    const GR& _graph;
deba@220
  3340
  };
deba@220
  3341
kpeter@301
  3342
  /// \brief Returns a \c ForwardMap class.
deba@220
  3343
  ///
kpeter@301
  3344
  /// This function just returns an \c ForwardMap class.
deba@220
  3345
  /// \relates ForwardMap
kpeter@559
  3346
  template <typename GR>
kpeter@559
  3347
  inline ForwardMap<GR> forwardMap(const GR& graph) {
kpeter@559
  3348
    return ForwardMap<GR>(graph);
deba@220
  3349
  }
deba@220
  3350
kpeter@559
  3351
  /// \brief Map of the "backward" directed arc view of edges in a graph.
deba@220
  3352
  ///
kpeter@559
  3353
  /// BackwardMap provides access for the "backward" directed arc view of
kpeter@559
  3354
  /// each edge in a graph, which is returned by the \c direct() function
kpeter@559
  3355
  /// of the graph with \c false parameter.
kpeter@559
  3356
  /// \tparam GR The graph type.
deba@220
  3357
  /// \see ForwardMap
kpeter@559
  3358
  template <typename GR>
deba@220
  3359
  class BackwardMap {
deba@220
  3360
  public:
deba@220
  3361
kpeter@559
  3362
    typedef typename GR::Arc Value;
kpeter@559
  3363
    typedef typename GR::Edge Key;
deba@220
  3364
deba@220
  3365
    /// \brief Constructor
deba@220
  3366
    ///
kpeter@559
  3367
    /// Constructor.
kpeter@313
  3368
    /// \param graph The graph that the map belongs to.
kpeter@559
  3369
    explicit BackwardMap(const GR& graph) : _graph(graph) {}
kpeter@559
  3370
kpeter@559
  3371
    /// \brief Returns the "backward" directed arc view of the given edge.
deba@220
  3372
    ///
kpeter@559
  3373
    /// Returns the "backward" directed arc view of the given edge.
deba@220
  3374
    Value operator[](const Key& key) const {
deba@220
  3375
      return _graph.direct(key, false);
deba@220
  3376
    }
deba@220
  3377
deba@220
  3378
  private:
kpeter@559
  3379
    const GR& _graph;
deba@220
  3380
  };
deba@220
  3381
kpeter@301
  3382
  /// \brief Returns a \c BackwardMap class
kpeter@301
  3383
kpeter@301
  3384
  /// This function just returns a \c BackwardMap class.
deba@220
  3385
  /// \relates BackwardMap
kpeter@559
  3386
  template <typename GR>
kpeter@559
  3387
  inline BackwardMap<GR> backwardMap(const GR& graph) {
kpeter@559
  3388
    return BackwardMap<GR>(graph);
deba@220
  3389
  }
deba@220
  3390
kpeter@559
  3391
  /// \brief Map of the in-degrees of nodes in a digraph.
deba@220
  3392
  ///
deba@220
  3393
  /// This map returns the in-degree of a node. Once it is constructed,
kpeter@559
  3394
  /// the degrees are stored in a standard \c NodeMap, so each query is done
deba@220
  3395
  /// in constant time. On the other hand, the values are updated automatically
deba@220
  3396
  /// whenever the digraph changes.
deba@220
  3397
  ///
deba@693
  3398
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
kpeter@559
  3399
  /// may provide alternative ways to modify the digraph.
kpeter@559
  3400
  /// The correct behavior of InDegMap is not guarantied if these additional
kpeter@559
  3401
  /// features are used. For example the functions
kpeter@559
  3402
  /// \ref ListDigraph::changeSource() "changeSource()",
deba@220
  3403
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
deba@220
  3404
  /// \ref ListDigraph::reverseArc() "reverseArc()"
deba@220
  3405
  /// of \ref ListDigraph will \e not update the degree values correctly.
deba@220
  3406
  ///
deba@220
  3407
  /// \sa OutDegMap
kpeter@559
  3408
  template <typename GR>
deba@220
  3409
  class InDegMap
kpeter@559
  3410
    : protected ItemSetTraits<GR, typename GR::Arc>
deba@220
  3411
      ::ItemNotifier::ObserverBase {
deba@220
  3412
deba@220
  3413
  public:
deba@693
  3414
kpeter@617
  3415
    /// The graph type of InDegMap
kpeter@617
  3416
    typedef GR Graph;
kpeter@559
  3417
    typedef GR Digraph;
kpeter@559
  3418
    /// The key type
kpeter@559
  3419
    typedef typename Digraph::Node Key;
kpeter@559
  3420
    /// The value type
deba@220
  3421
    typedef int Value;
deba@220
  3422
deba@220
  3423
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
deba@220
  3424
    ::ItemNotifier::ObserverBase Parent;
deba@220
  3425
deba@220
  3426
  private:
deba@220
  3427
deba@220
  3428
    class AutoNodeMap
deba@220
  3429
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
deba@220
  3430
    public:
deba@220
  3431
deba@220
  3432
      typedef typename ItemSetTraits<Digraph, Key>::
deba@220
  3433
      template Map<int>::Type Parent;
deba@220
  3434
deba@220
  3435
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
deba@220
  3436
deba@220
  3437
      virtual void add(const Key& key) {
deba@220
  3438
        Parent::add(key);
deba@220
  3439
        Parent::set(key, 0);
deba@220
  3440
      }
deba@220
  3441
deba@220
  3442
      virtual void add(const std::vector<Key>& keys) {
deba@220
  3443
        Parent::add(keys);
deba@220
  3444
        for (int i = 0; i < int(keys.size()); ++i) {
deba@220
  3445
          Parent::set(keys[i], 0);
deba@220
  3446
        }
deba@220
  3447
      }
deba@220
  3448
deba@220
  3449
      virtual void build() {
deba@220
  3450
        Parent::build();
deba@220
  3451
        Key it;
deba@220
  3452
        typename Parent::Notifier* nf = Parent::notifier();
deba@220
  3453
        for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  3454
          Parent::set(it, 0);
deba@220
  3455
        }
deba@220
  3456
      }
deba@220
  3457
    };
deba@220
  3458
deba@220
  3459
  public:
deba@220
  3460
deba@220
  3461
    /// \brief Constructor.
deba@220
  3462
    ///
kpeter@559
  3463
    /// Constructor for creating an in-degree map.
kpeter@559
  3464
    explicit InDegMap(const Digraph& graph)
kpeter@559
  3465
      : _digraph(graph), _deg(graph) {
deba@220
  3466
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
deba@220
  3467
deba@220
  3468
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3469
        _deg[it] = countInArcs(_digraph, it);
deba@220
  3470
      }
deba@220
  3471
    }
deba@220
  3472
kpeter@559
  3473
    /// \brief Gives back the in-degree of a Node.
kpeter@559
  3474
    ///
deba@220
  3475
    /// Gives back the in-degree of a Node.
deba@220
  3476
    int operator[](const Key& key) const {
deba@220
  3477
      return _deg[key];
deba@220
  3478
    }
deba@220
  3479
deba@220
  3480
  protected:
deba@220
  3481
deba@220
  3482
    typedef typename Digraph::Arc Arc;
deba@220
  3483
deba@220
  3484
    virtual void add(const Arc& arc) {
deba@220
  3485
      ++_deg[_digraph.target(arc)];
deba@220
  3486
    }
deba@220
  3487
deba@220
  3488
    virtual void add(const std::vector<Arc>& arcs) {
deba@220
  3489
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3490
        ++_deg[_digraph.target(arcs[i])];
deba@220
  3491
      }
deba@220
  3492
    }
deba@220
  3493
deba@220
  3494
    virtual void erase(const Arc& arc) {
deba@220
  3495
      --_deg[_digraph.target(arc)];
deba@220
  3496
    }
deba@220
  3497
deba@220
  3498
    virtual void erase(const std::vector<Arc>& arcs) {
deba@220
  3499
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3500
        --_deg[_digraph.target(arcs[i])];
deba@220
  3501
      }
deba@220
  3502
    }
deba@220
  3503
deba@220
  3504
    virtual void build() {
deba@220
  3505
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3506
        _deg[it] = countInArcs(_digraph, it);
deba@220
  3507
      }
deba@220
  3508
    }
deba@220
  3509
deba@220
  3510
    virtual void clear() {
deba@220
  3511
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3512
        _deg[it] = 0;
deba@220
  3513
      }
deba@220
  3514
    }
deba@220
  3515
  private:
deba@220
  3516
deba@220
  3517
    const Digraph& _digraph;
deba@220
  3518
    AutoNodeMap _deg;
deba@220
  3519
  };
deba@220
  3520
kpeter@559
  3521
  /// \brief Map of the out-degrees of nodes in a digraph.
deba@220
  3522
  ///
deba@220
  3523
  /// This map returns the out-degree of a node. Once it is constructed,
kpeter@559
  3524
  /// the degrees are stored in a standard \c NodeMap, so each query is done
deba@220
  3525
  /// in constant time. On the other hand, the values are updated automatically
deba@220
  3526
  /// whenever the digraph changes.
deba@220
  3527
  ///
deba@693
  3528
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
kpeter@559
  3529
  /// may provide alternative ways to modify the digraph.
kpeter@559
  3530
  /// The correct behavior of OutDegMap is not guarantied if these additional
kpeter@559
  3531
  /// features are used. For example the functions
kpeter@559
  3532
  /// \ref ListDigraph::changeSource() "changeSource()",
deba@220
  3533
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
deba@220
  3534
  /// \ref ListDigraph::reverseArc() "reverseArc()"
deba@220
  3535
  /// of \ref ListDigraph will \e not update the degree values correctly.
deba@220
  3536
  ///
deba@220
  3537
  /// \sa InDegMap
kpeter@559
  3538
  template <typename GR>
deba@220
  3539
  class OutDegMap
kpeter@559
  3540
    : protected ItemSetTraits<GR, typename GR::Arc>
deba@220
  3541
      ::ItemNotifier::ObserverBase {
deba@220
  3542
deba@220
  3543
  public:
deba@220
  3544
kpeter@617
  3545
    /// The graph type of OutDegMap
kpeter@617
  3546
    typedef GR Graph;
kpeter@559
  3547
    typedef GR Digraph;
kpeter@559
  3548
    /// The key type
kpeter@559
  3549
    typedef typename Digraph::Node Key;
kpeter@559
  3550
    /// The value type
deba@220
  3551
    typedef int Value;
deba@220
  3552
deba@220
  3553
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
deba@220
  3554
    ::ItemNotifier::ObserverBase Parent;
deba@220
  3555
deba@220
  3556
  private:
deba@220
  3557
deba@220
  3558
    class AutoNodeMap
deba@220
  3559
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
deba@220
  3560
    public:
deba@220
  3561
deba@220
  3562
      typedef typename ItemSetTraits<Digraph, Key>::
deba@220
  3563
      template Map<int>::Type Parent;
deba@220
  3564
deba@220
  3565
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
deba@220
  3566
deba@220
  3567
      virtual void add(const Key& key) {
deba@220
  3568
        Parent::add(key);
deba@220
  3569
        Parent::set(key, 0);
deba@220
  3570
      }
deba@220
  3571
      virtual void add(const std::vector<Key>& keys) {
deba@220
  3572
        Parent::add(keys);
deba@220
  3573
        for (int i = 0; i < int(keys.size()); ++i) {
deba@220
  3574
          Parent::set(keys[i], 0);
deba@220
  3575
        }
deba@220
  3576
      }
deba@220
  3577
      virtual void build() {
deba@220
  3578
        Parent::build();
deba@220
  3579
        Key it;
deba@220
  3580
        typename Parent::Notifier* nf = Parent::notifier();
deba@220
  3581
        for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  3582
          Parent::set(it, 0);
deba@220
  3583
        }
deba@220
  3584
      }
deba@220
  3585
    };
deba@220
  3586
deba@220
  3587
  public:
deba@220
  3588
deba@220
  3589
    /// \brief Constructor.
deba@220
  3590
    ///
kpeter@559
  3591
    /// Constructor for creating an out-degree map.
kpeter@559
  3592
    explicit OutDegMap(const Digraph& graph)
kpeter@559
  3593
      : _digraph(graph), _deg(graph) {
deba@220
  3594
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
deba@220
  3595
deba@220
  3596
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3597
        _deg[it] = countOutArcs(_digraph, it);
deba@220
  3598
      }
deba@220
  3599
    }
deba@220
  3600
kpeter@559
  3601
    /// \brief Gives back the out-degree of a Node.
kpeter@559
  3602
    ///
deba@220
  3603
    /// Gives back the out-degree of a Node.
deba@220
  3604
    int operator[](const Key& key) const {
deba@220
  3605
      return _deg[key];
deba@220
  3606
    }
deba@220
  3607
deba@220
  3608
  protected:
deba@220
  3609
deba@220
  3610
    typedef typename Digraph::Arc Arc;
deba@220
  3611
deba@220
  3612
    virtual void add(const Arc& arc) {
deba@220
  3613
      ++_deg[_digraph.source(arc)];
deba@220
  3614
    }
deba@220
  3615
deba@220
  3616
    virtual void add(const std::vector<Arc>& arcs) {
deba@220
  3617
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3618
        ++_deg[_digraph.source(arcs[i])];
deba@220
  3619
      }
deba@220
  3620
    }
deba@220
  3621
deba@220
  3622
    virtual void erase(const Arc& arc) {
deba@220
  3623
      --_deg[_digraph.source(arc)];
deba@220
  3624
    }
deba@220
  3625
deba@220
  3626
    virtual void erase(const std::vector<Arc>& arcs) {
deba@220
  3627
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3628
        --_deg[_digraph.source(arcs[i])];
deba@220
  3629
      }
deba@220
  3630
    }
deba@220
  3631
deba@220
  3632
    virtual void build() {
deba@220
  3633
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3634
        _deg[it] = countOutArcs(_digraph, it);
deba@220
  3635
      }
deba@220
  3636
    }
deba@220
  3637
deba@220
  3638
    virtual void clear() {
deba@220
  3639
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3640
        _deg[it] = 0;
deba@220
  3641
      }
deba@220
  3642
    }
deba@220
  3643
  private:
deba@220
  3644
deba@220
  3645
    const Digraph& _digraph;
deba@220
  3646
    AutoNodeMap _deg;
deba@220
  3647
  };
deba@220
  3648
kpeter@559
  3649
  /// \brief Potential difference map
kpeter@559
  3650
  ///
kpeter@584
  3651
  /// PotentialDifferenceMap returns the difference between the potentials of
kpeter@584
  3652
  /// the source and target nodes of each arc in a digraph, i.e. it returns
kpeter@559
  3653
  /// \code
kpeter@559
  3654
  ///   potential[gr.target(arc)] - potential[gr.source(arc)].
kpeter@559
  3655
  /// \endcode
kpeter@559
  3656
  /// \tparam GR The digraph type.
kpeter@559
  3657
  /// \tparam POT A node map storing the potentials.
kpeter@559
  3658
  template <typename GR, typename POT>
kpeter@559
  3659
  class PotentialDifferenceMap {
kpeter@559
  3660
  public:
kpeter@559
  3661
    /// Key type
kpeter@559
  3662
    typedef typename GR::Arc Key;
kpeter@559
  3663
    /// Value type
kpeter@559
  3664
    typedef typename POT::Value Value;
kpeter@559
  3665
kpeter@559
  3666
    /// \brief Constructor
kpeter@559
  3667
    ///
kpeter@559
  3668
    /// Contructor of the map.
kpeter@559
  3669
    explicit PotentialDifferenceMap(const GR& gr,
kpeter@559
  3670
                                    const POT& potential)
kpeter@559
  3671
      : _digraph(gr), _potential(potential) {}
kpeter@559
  3672
kpeter@559
  3673
    /// \brief Returns the potential difference for the given arc.
kpeter@559
  3674
    ///
kpeter@559
  3675
    /// Returns the potential difference for the given arc, i.e.
kpeter@559
  3676
    /// \code
kpeter@559
  3677
    ///   potential[gr.target(arc)] - potential[gr.source(arc)].
kpeter@559
  3678
    /// \endcode
kpeter@559
  3679
    Value operator[](const Key& arc) const {
kpeter@559
  3680
      return _potential[_digraph.target(arc)] -
kpeter@559
  3681
        _potential[_digraph.source(arc)];
kpeter@559
  3682
    }
kpeter@559
  3683
kpeter@559
  3684
  private:
kpeter@559
  3685
    const GR& _digraph;
kpeter@559
  3686
    const POT& _potential;
kpeter@559
  3687
  };
kpeter@559
  3688
kpeter@559
  3689
  /// \brief Returns a PotentialDifferenceMap.
kpeter@559
  3690
  ///
kpeter@559
  3691
  /// This function just returns a PotentialDifferenceMap.
kpeter@559
  3692
  /// \relates PotentialDifferenceMap
kpeter@559
  3693
  template <typename GR, typename POT>
kpeter@559
  3694
  PotentialDifferenceMap<GR, POT>
kpeter@559
  3695
  potentialDifferenceMap(const GR& gr, const POT& potential) {
kpeter@559
  3696
    return PotentialDifferenceMap<GR, POT>(gr, potential);
kpeter@559
  3697
  }
kpeter@559
  3698
alpar@25
  3699
  /// @}
alpar@25
  3700
}
alpar@25
  3701
alpar@25
  3702
#endif // LEMON_MAPS_H