lemon/maps.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 02 Aug 2009 17:22:43 +0200
changeset 722 b52189c479fb
parent 721 99124ea4f048
child 724 d8073df341f6
permissions -rw-r--r--
Doc improvements for several graph maps (#302)
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@722
    59
  /// It conforms to 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@722
    92
  /// So it conforms to 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@722
   161
  /// So it conforms to 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@722
   235
  /// integers. This map conforms to 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@722
   343
  /// This type conforms to 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@722
   709
  /// This type conforms to 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@167
  1792
  ///   dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run();
kpeter@159
  1793
  /// \endcode
kpeter@159
  1794
  /// \code
kpeter@159
  1795
  ///   std::vector<Node> v(countNodes(g));
kpeter@167
  1796
  ///   dfs(g,s).processedMap(loggerBoolMap(v.begin())).run();
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
kpeter@720
  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@722
  1868
    /// \brief The inverse map type of IdMap.
deba@220
  1869
    ///
kpeter@722
  1870
    /// The inverse map type of IdMap. The subscript operator gives back
kpeter@722
  1871
    /// an item by its id.
kpeter@722
  1872
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
deba@220
  1873
    /// \see inverse()
deba@220
  1874
    class InverseMap {
deba@220
  1875
    public:
deba@220
  1876
deba@220
  1877
      /// \brief Constructor.
deba@220
  1878
      ///
deba@220
  1879
      /// Constructor for creating an id-to-item map.
deba@220
  1880
      explicit InverseMap(const Graph& graph) : _graph(&graph) {}
deba@220
  1881
deba@220
  1882
      /// \brief Constructor.
deba@220
  1883
      ///
deba@220
  1884
      /// Constructor for creating an id-to-item map.
deba@220
  1885
      explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
deba@220
  1886
kpeter@722
  1887
      /// \brief Gives back an item by its id.
deba@220
  1888
      ///
kpeter@722
  1889
      /// Gives back an item by its id.
deba@220
  1890
      Item operator[](int id) const { return _graph->fromId(id, Item());}
deba@220
  1891
deba@220
  1892
    private:
deba@220
  1893
      const Graph* _graph;
deba@220
  1894
    };
deba@220
  1895
deba@220
  1896
    /// \brief Gives back the inverse of the map.
deba@220
  1897
    ///
deba@220
  1898
    /// Gives back the inverse of the IdMap.
deba@220
  1899
    InverseMap inverse() const { return InverseMap(*_graph);}
deba@220
  1900
  };
deba@220
  1901
deba@220
  1902
alpar@572
  1903
  /// \brief General cross reference graph map type.
kpeter@559
  1904
kpeter@559
  1905
  /// This class provides simple invertable graph maps.
kpeter@684
  1906
  /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
kpeter@684
  1907
  /// and if a key is set to a new value, then stores it in the inverse map.
kpeter@722
  1908
  /// The graph items can be accessed by their values either using
kpeter@722
  1909
  /// \c InverseMap or \c operator()(), and the values of the map can be
kpeter@722
  1910
  /// accessed with an STL compatible forward iterator (\c ValueIterator).
kpeter@722
  1911
  /// 
kpeter@722
  1912
  /// This map is intended to be used when all associated values are
kpeter@722
  1913
  /// different (the map is actually invertable) or there are only a few
kpeter@722
  1914
  /// items with the same value.
kpeter@722
  1915
  /// Otherwise consider to use \c IterableValueMap, which is more 
kpeter@722
  1916
  /// suitable and more efficient for such cases. It provides iterators
kpeter@722
  1917
  /// to traverse the items with the same associated value, however
kpeter@722
  1918
  /// it does not have \c InverseMap.
deba@220
  1919
  ///
kpeter@694
  1920
  /// This type is not reference map, so it cannot be modified with
kpeter@684
  1921
  /// the subscript operator.
kpeter@694
  1922
  ///
kpeter@559
  1923
  /// \tparam GR The graph type.
kpeter@559
  1924
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@559
  1925
  /// \c GR::Edge).
kpeter@559
  1926
  /// \tparam V The value type of the map.
deba@220
  1927
  ///
deba@220
  1928
  /// \see IterableValueMap
kpeter@559
  1929
  template <typename GR, typename K, typename V>
alpar@572
  1930
  class CrossRefMap
kpeter@559
  1931
    : protected ItemSetTraits<GR, K>::template Map<V>::Type {
deba@220
  1932
  private:
deba@220
  1933
kpeter@559
  1934
    typedef typename ItemSetTraits<GR, K>::
kpeter@559
  1935
      template Map<V>::Type Map;
kpeter@559
  1936
kpeter@684
  1937
    typedef std::multimap<V, K> Container;
deba@220
  1938
    Container _inv_map;
deba@220
  1939
deba@220
  1940
  public:
deba@220
  1941
alpar@572
  1942
    /// The graph type of CrossRefMap.
kpeter@559
  1943
    typedef GR Graph;
kpeter@617
  1944
    typedef GR Digraph;
alpar@572
  1945
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
kpeter@559
  1946
    typedef K Item;
alpar@572
  1947
    /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
kpeter@559
  1948
    typedef K Key;
alpar@572
  1949
    /// The value type of CrossRefMap.
kpeter@559
  1950
    typedef V Value;
deba@220
  1951
deba@220
  1952
    /// \brief Constructor.
deba@220
  1953
    ///
alpar@572
  1954
    /// Construct a new CrossRefMap for the given graph.
alpar@572
  1955
    explicit CrossRefMap(const Graph& graph) : Map(graph) {}
deba@220
  1956
deba@220
  1957
    /// \brief Forward iterator for values.
deba@220
  1958
    ///
kpeter@722
  1959
    /// This iterator is an STL compatible forward
deba@220
  1960
    /// iterator on the values of the map. The values can
kpeter@559
  1961
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
kpeter@684
  1962
    /// They are considered with multiplicity, so each value is
kpeter@684
  1963
    /// traversed for each item it is assigned to.
deba@220
  1964
    class ValueIterator
deba@220
  1965
      : public std::iterator<std::forward_iterator_tag, Value> {
alpar@572
  1966
      friend class CrossRefMap;
deba@220
  1967
    private:
deba@220
  1968
      ValueIterator(typename Container::const_iterator _it)
deba@220
  1969
        : it(_it) {}
deba@220
  1970
    public:
deba@220
  1971
kpeter@722
  1972
      /// Constructor
deba@220
  1973
      ValueIterator() {}
deba@220
  1974
kpeter@722
  1975
      /// \e
deba@220
  1976
      ValueIterator& operator++() { ++it; return *this; }
kpeter@722
  1977
      /// \e
deba@220
  1978
      ValueIterator operator++(int) {
deba@220
  1979
        ValueIterator tmp(*this);
deba@220
  1980
        operator++();
deba@220
  1981
        return tmp;
deba@220
  1982
      }
deba@220
  1983
kpeter@722
  1984
      /// \e
deba@220
  1985
      const Value& operator*() const { return it->first; }
kpeter@722
  1986
      /// \e
deba@220
  1987
      const Value* operator->() const { return &(it->first); }
deba@220
  1988
kpeter@722
  1989
      /// \e
deba@220
  1990
      bool operator==(ValueIterator jt) const { return it == jt.it; }
kpeter@722
  1991
      /// \e
deba@220
  1992
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@220
  1993
deba@220
  1994
    private:
deba@220
  1995
      typename Container::const_iterator it;
deba@220
  1996
    };
deba@220
  1997
deba@220
  1998
    /// \brief Returns an iterator to the first value.
deba@220
  1999
    ///
kpeter@722
  2000
    /// Returns an STL compatible iterator to the
deba@220
  2001
    /// first value of the map. The values of the
kpeter@559
  2002
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@220
  2003
    /// range.
deba@220
  2004
    ValueIterator beginValue() const {
deba@220
  2005
      return ValueIterator(_inv_map.begin());
deba@220
  2006
    }
deba@220
  2007
deba@220
  2008
    /// \brief Returns an iterator after the last value.
deba@220
  2009
    ///
kpeter@722
  2010
    /// Returns an STL compatible iterator after the
deba@220
  2011
    /// last value of the map. The values of the
kpeter@559
  2012
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@220
  2013
    /// range.
deba@220
  2014
    ValueIterator endValue() const {
deba@220
  2015
      return ValueIterator(_inv_map.end());
deba@220
  2016
    }
deba@220
  2017
kpeter@559
  2018
    /// \brief Sets the value associated with the given key.
deba@220
  2019
    ///
kpeter@559
  2020
    /// Sets the value associated with the given key.
deba@220
  2021
    void set(const Key& key, const Value& val) {
deba@220
  2022
      Value oldval = Map::operator[](key);
kpeter@684
  2023
      typename Container::iterator it;
kpeter@684
  2024
      for (it = _inv_map.equal_range(oldval).first;
kpeter@684
  2025
           it != _inv_map.equal_range(oldval).second; ++it) {
kpeter@684
  2026
        if (it->second == key) {
kpeter@684
  2027
          _inv_map.erase(it);
kpeter@684
  2028
          break;
kpeter@684
  2029
        }
deba@220
  2030
      }
deba@693
  2031
      _inv_map.insert(std::make_pair(val, key));
deba@220
  2032
      Map::set(key, val);
deba@220
  2033
    }
deba@220
  2034
kpeter@559
  2035
    /// \brief Returns the value associated with the given key.
deba@220
  2036
    ///
kpeter@559
  2037
    /// Returns the value associated with the given key.
deba@220
  2038
    typename MapTraits<Map>::ConstReturnValue
deba@220
  2039
    operator[](const Key& key) const {
deba@220
  2040
      return Map::operator[](key);
deba@220
  2041
    }
deba@220
  2042
kpeter@684
  2043
    /// \brief Gives back an item by its value.
deba@220
  2044
    ///
kpeter@684
  2045
    /// This function gives back an item that is assigned to
kpeter@684
  2046
    /// the given value or \c INVALID if no such item exists.
kpeter@684
  2047
    /// If there are more items with the same associated value,
kpeter@684
  2048
    /// only one of them is returned.
kpeter@684
  2049
    Key operator()(const Value& val) const {
kpeter@684
  2050
      typename Container::const_iterator it = _inv_map.find(val);
deba@220
  2051
      return it != _inv_map.end() ? it->second : INVALID;
deba@220
  2052
    }
kpeter@720
  2053
    
kpeter@720
  2054
    /// \brief Returns the number of items with the given value.
kpeter@720
  2055
    ///
kpeter@720
  2056
    /// This function returns the number of items with the given value
kpeter@720
  2057
    /// associated with it.
kpeter@720
  2058
    int count(const Value &val) const {
kpeter@720
  2059
      return _inv_map.count(val);
kpeter@720
  2060
    }
deba@220
  2061
deba@220
  2062
  protected:
deba@220
  2063
kpeter@559
  2064
    /// \brief Erase the key from the map and the inverse map.
deba@220
  2065
    ///
kpeter@559
  2066
    /// Erase the key from the map and the inverse map. It is called by the
deba@220
  2067
    /// \c AlterationNotifier.
deba@220
  2068
    virtual void erase(const Key& key) {
deba@220
  2069
      Value val = Map::operator[](key);
kpeter@684
  2070
      typename Container::iterator it;
kpeter@684
  2071
      for (it = _inv_map.equal_range(val).first;
kpeter@684
  2072
           it != _inv_map.equal_range(val).second; ++it) {
kpeter@684
  2073
        if (it->second == key) {
kpeter@684
  2074
          _inv_map.erase(it);
kpeter@684
  2075
          break;
kpeter@684
  2076
        }
deba@220
  2077
      }
deba@220
  2078
      Map::erase(key);
deba@220
  2079
    }
deba@220
  2080
kpeter@559
  2081
    /// \brief Erase more keys from the map and the inverse map.
deba@220
  2082
    ///
kpeter@559
  2083
    /// Erase more keys from the map and the inverse map. It is called by the
deba@220
  2084
    /// \c AlterationNotifier.
deba@220
  2085
    virtual void erase(const std::vector<Key>& keys) {
deba@220
  2086
      for (int i = 0; i < int(keys.size()); ++i) {
deba@220
  2087
        Value val = Map::operator[](keys[i]);
kpeter@684
  2088
        typename Container::iterator it;
kpeter@684
  2089
        for (it = _inv_map.equal_range(val).first;
kpeter@684
  2090
             it != _inv_map.equal_range(val).second; ++it) {
kpeter@684
  2091
          if (it->second == keys[i]) {
kpeter@684
  2092
            _inv_map.erase(it);
kpeter@684
  2093
            break;
kpeter@684
  2094
          }
deba@220
  2095
        }
deba@220
  2096
      }
deba@220
  2097
      Map::erase(keys);
deba@220
  2098
    }
deba@220
  2099
kpeter@559
  2100
    /// \brief Clear the keys from the map and the inverse map.
deba@220
  2101
    ///
kpeter@559
  2102
    /// Clear the keys from the map and the inverse map. It is called by the
deba@220
  2103
    /// \c AlterationNotifier.
deba@220
  2104
    virtual void clear() {
deba@220
  2105
      _inv_map.clear();
deba@220
  2106
      Map::clear();
deba@220
  2107
    }
deba@220
  2108
deba@220
  2109
  public:
deba@220
  2110
kpeter@722
  2111
    /// \brief The inverse map type of CrossRefMap.
deba@220
  2112
    ///
kpeter@722
  2113
    /// The inverse map type of CrossRefMap. The subscript operator gives
kpeter@722
  2114
    /// back an item by its value.
kpeter@722
  2115
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
kpeter@722
  2116
    /// \see inverse()
deba@220
  2117
    class InverseMap {
deba@220
  2118
    public:
kpeter@559
  2119
      /// \brief Constructor
deba@220
  2120
      ///
deba@220
  2121
      /// Constructor of the InverseMap.
alpar@572
  2122
      explicit InverseMap(const CrossRefMap& inverted)
deba@220
  2123
        : _inverted(inverted) {}
deba@220
  2124
deba@220
  2125
      /// The value type of the InverseMap.
alpar@572
  2126
      typedef typename CrossRefMap::Key Value;
deba@220
  2127
      /// The key type of the InverseMap.
alpar@572
  2128
      typedef typename CrossRefMap::Value Key;
deba@220
  2129
deba@220
  2130
      /// \brief Subscript operator.
deba@220
  2131
      ///
kpeter@684
  2132
      /// Subscript operator. It gives back an item
kpeter@684
  2133
      /// that is assigned to the given value or \c INVALID
kpeter@684
  2134
      /// if no such item exists.
deba@220
  2135
      Value operator[](const Key& key) const {
deba@220
  2136
        return _inverted(key);
deba@220
  2137
      }
deba@220
  2138
deba@220
  2139
    private:
alpar@572
  2140
      const CrossRefMap& _inverted;
deba@220
  2141
    };
deba@220
  2142
kpeter@722
  2143
    /// \brief Gives back the inverse of the map.
deba@220
  2144
    ///
kpeter@722
  2145
    /// Gives back the inverse of the CrossRefMap.
deba@220
  2146
    InverseMap inverse() const {
deba@220
  2147
      return InverseMap(*this);
deba@220
  2148
    }
deba@220
  2149
deba@220
  2150
  };
deba@220
  2151
kpeter@720
  2152
  /// \brief Provides continuous and unique id for the
alpar@572
  2153
  /// items of a graph.
deba@220
  2154
  ///
alpar@572
  2155
  /// RangeIdMap provides a unique and continuous
kpeter@720
  2156
  /// id for each item of a given type (\c Node, \c Arc or
kpeter@559
  2157
  /// \c Edge) in a graph. This id is
kpeter@559
  2158
  ///  - \b unique: different items get different ids,
kpeter@559
  2159
  ///  - \b continuous: the range of the ids is the set of integers
kpeter@559
  2160
  ///    between 0 and \c n-1, where \c n is the number of the items of
alpar@572
  2161
  ///    this type (\c Node, \c Arc or \c Edge).
alpar@572
  2162
  ///  - So, the ids can change when deleting an item of the same type.
deba@220
  2163
  ///
kpeter@559
  2164
  /// Thus this id is not (necessarily) the same as what can get using
kpeter@559
  2165
  /// the \c id() function of the graph or \ref IdMap.
kpeter@559
  2166
  /// This map can be inverted with its member class \c InverseMap,
kpeter@720
  2167
  /// or with the \c operator()() member.
kpeter@559
  2168
  ///
kpeter@559
  2169
  /// \tparam GR The graph type.
kpeter@559
  2170
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@559
  2171
  /// \c GR::Edge).
kpeter@559
  2172
  ///
kpeter@559
  2173
  /// \see IdMap
kpeter@559
  2174
  template <typename GR, typename K>
alpar@572
  2175
  class RangeIdMap
kpeter@559
  2176
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
kpeter@559
  2177
kpeter@559
  2178
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
deba@220
  2179
deba@220
  2180
  public:
alpar@572
  2181
    /// The graph type of RangeIdMap.
kpeter@559
  2182
    typedef GR Graph;
kpeter@617
  2183
    typedef GR Digraph;
alpar@572
  2184
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
kpeter@559
  2185
    typedef K Item;
alpar@572
  2186
    /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
kpeter@559
  2187
    typedef K Key;
alpar@572
  2188
    /// The value type of RangeIdMap.
kpeter@559
  2189
    typedef int Value;
deba@220
  2190
deba@220
  2191
    /// \brief Constructor.
deba@220
  2192
    ///
alpar@572
  2193
    /// Constructor.
alpar@572
  2194
    explicit RangeIdMap(const Graph& gr) : Map(gr) {
deba@220
  2195
      Item it;
deba@220
  2196
      const typename Map::Notifier* nf = Map::notifier();
deba@220
  2197
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  2198
        Map::set(it, _inv_map.size());
deba@220
  2199
        _inv_map.push_back(it);
deba@220
  2200
      }
deba@220
  2201
    }
deba@220
  2202
deba@220
  2203
  protected:
deba@220
  2204
kpeter@559
  2205
    /// \brief Adds a new key to the map.
deba@220
  2206
    ///
deba@220
  2207
    /// Add a new key to the map. It is called by the
deba@220
  2208
    /// \c AlterationNotifier.
deba@220
  2209
    virtual void add(const Item& item) {
deba@220
  2210
      Map::add(item);
deba@220
  2211
      Map::set(item, _inv_map.size());
deba@220
  2212
      _inv_map.push_back(item);
deba@220
  2213
    }
deba@220
  2214
deba@220
  2215
    /// \brief Add more new keys to the map.
deba@220
  2216
    ///
deba@220
  2217
    /// Add more new keys to the map. It is called by the
deba@220
  2218
    /// \c AlterationNotifier.
deba@220
  2219
    virtual void add(const std::vector<Item>& items) {
deba@220
  2220
      Map::add(items);
deba@220
  2221
      for (int i = 0; i < int(items.size()); ++i) {
deba@220
  2222
        Map::set(items[i], _inv_map.size());
deba@220
  2223
        _inv_map.push_back(items[i]);
deba@220
  2224
      }
deba@220
  2225
    }
deba@220
  2226
deba@220
  2227
    /// \brief Erase the key from the map.
deba@220
  2228
    ///
deba@220
  2229
    /// Erase the key from the map. It is called by the
deba@220
  2230
    /// \c AlterationNotifier.
deba@220
  2231
    virtual void erase(const Item& item) {
deba@220
  2232
      Map::set(_inv_map.back(), Map::operator[](item));
deba@220
  2233
      _inv_map[Map::operator[](item)] = _inv_map.back();
deba@220
  2234
      _inv_map.pop_back();
deba@220
  2235
      Map::erase(item);
deba@220
  2236
    }
deba@220
  2237
deba@220
  2238
    /// \brief Erase more keys from the map.
deba@220
  2239
    ///
deba@220
  2240
    /// Erase more keys from the map. It is called by the
deba@220
  2241
    /// \c AlterationNotifier.
deba@220
  2242
    virtual void erase(const std::vector<Item>& items) {
deba@220
  2243
      for (int i = 0; i < int(items.size()); ++i) {
deba@220
  2244
        Map::set(_inv_map.back(), Map::operator[](items[i]));
deba@220
  2245
        _inv_map[Map::operator[](items[i])] = _inv_map.back();
deba@220
  2246
        _inv_map.pop_back();
deba@220
  2247
      }
deba@220
  2248
      Map::erase(items);
deba@220
  2249
    }
deba@220
  2250
deba@220
  2251
    /// \brief Build the unique map.
deba@220
  2252
    ///
deba@220
  2253
    /// Build the unique map. It is called by the
deba@220
  2254
    /// \c AlterationNotifier.
deba@220
  2255
    virtual void build() {
deba@220
  2256
      Map::build();
deba@220
  2257
      Item it;
deba@220
  2258
      const typename Map::Notifier* nf = Map::notifier();
deba@220
  2259
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  2260
        Map::set(it, _inv_map.size());
deba@220
  2261
        _inv_map.push_back(it);
deba@220
  2262
      }
deba@220
  2263
    }
deba@220
  2264
deba@220
  2265
    /// \brief Clear the keys from the map.
deba@220
  2266
    ///
deba@220
  2267
    /// Clear the keys from the map. It is called by the
deba@220
  2268
    /// \c AlterationNotifier.
deba@220
  2269
    virtual void clear() {
deba@220
  2270
      _inv_map.clear();
deba@220
  2271
      Map::clear();
deba@220
  2272
    }
deba@220
  2273
deba@220
  2274
  public:
deba@220
  2275
deba@220
  2276
    /// \brief Returns the maximal value plus one.
deba@220
  2277
    ///
deba@220
  2278
    /// Returns the maximal value plus one in the map.
deba@220
  2279
    unsigned int size() const {
deba@220
  2280
      return _inv_map.size();
deba@220
  2281
    }
deba@220
  2282
deba@220
  2283
    /// \brief Swaps the position of the two items in the map.
deba@220
  2284
    ///
deba@220
  2285
    /// Swaps the position of the two items in the map.
deba@220
  2286
    void swap(const Item& p, const Item& q) {
deba@220
  2287
      int pi = Map::operator[](p);
deba@220
  2288
      int qi = Map::operator[](q);
deba@220
  2289
      Map::set(p, qi);
deba@220
  2290
      _inv_map[qi] = p;
deba@220
  2291
      Map::set(q, pi);
deba@220
  2292
      _inv_map[pi] = q;
deba@220
  2293
    }
deba@220
  2294
kpeter@722
  2295
    /// \brief Gives back the \e range \e id of the item
deba@220
  2296
    ///
kpeter@722
  2297
    /// Gives back the \e range \e id of the item.
deba@220
  2298
    int operator[](const Item& item) const {
deba@220
  2299
      return Map::operator[](item);
deba@220
  2300
    }
deba@220
  2301
kpeter@722
  2302
    /// \brief Gives back the item belonging to a \e range \e id
deba@693
  2303
    ///
kpeter@722
  2304
    /// Gives back the item belonging to the given \e range \e id.
deba@220
  2305
    Item operator()(int id) const {
deba@220
  2306
      return _inv_map[id];
deba@220
  2307
    }
deba@220
  2308
deba@220
  2309
  private:
deba@220
  2310
deba@220
  2311
    typedef std::vector<Item> Container;
deba@220
  2312
    Container _inv_map;
deba@220
  2313
deba@220
  2314
  public:
kpeter@559
  2315
alpar@572
  2316
    /// \brief The inverse map type of RangeIdMap.
deba@220
  2317
    ///
kpeter@722
  2318
    /// The inverse map type of RangeIdMap. The subscript operator gives
kpeter@722
  2319
    /// back an item by its \e range \e id.
kpeter@722
  2320
    /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
deba@220
  2321
    class InverseMap {
deba@220
  2322
    public:
kpeter@559
  2323
      /// \brief Constructor
deba@220
  2324
      ///
deba@220
  2325
      /// Constructor of the InverseMap.
alpar@572
  2326
      explicit InverseMap(const RangeIdMap& inverted)
deba@220
  2327
        : _inverted(inverted) {}
deba@220
  2328
deba@220
  2329
deba@220
  2330
      /// The value type of the InverseMap.
alpar@572
  2331
      typedef typename RangeIdMap::Key Value;
deba@220
  2332
      /// The key type of the InverseMap.
alpar@572
  2333
      typedef typename RangeIdMap::Value Key;
deba@220
  2334
deba@220
  2335
      /// \brief Subscript operator.
deba@220
  2336
      ///
deba@220
  2337
      /// Subscript operator. It gives back the item
kpeter@722
  2338
      /// that the given \e range \e id currently belongs to.
deba@220
  2339
      Value operator[](const Key& key) const {
deba@220
  2340
        return _inverted(key);
deba@220
  2341
      }
deba@220
  2342
deba@220
  2343
      /// \brief Size of the map.
deba@220
  2344
      ///
deba@220
  2345
      /// Returns the size of the map.
deba@220
  2346
      unsigned int size() const {
deba@220
  2347
        return _inverted.size();
deba@220
  2348
      }
deba@220
  2349
deba@220
  2350
    private:
alpar@572
  2351
      const RangeIdMap& _inverted;
deba@220
  2352
    };
deba@220
  2353
deba@220
  2354
    /// \brief Gives back the inverse of the map.
deba@220
  2355
    ///
kpeter@722
  2356
    /// Gives back the inverse of the RangeIdMap.
deba@220
  2357
    const InverseMap inverse() const {
deba@220
  2358
      return InverseMap(*this);
deba@220
  2359
    }
deba@220
  2360
  };
deba@220
  2361
kpeter@694
  2362
  /// \brief Dynamic iterable \c bool map.
deba@693
  2363
  ///
kpeter@694
  2364
  /// This class provides a special graph map type which can store a
kpeter@694
  2365
  /// \c bool value for graph items (\c Node, \c Arc or \c Edge).
kpeter@694
  2366
  /// For both \c true and \c false values it is possible to iterate on
kpeter@722
  2367
  /// the keys mapped to the value.
deba@693
  2368
  ///
kpeter@694
  2369
  /// This type is a reference map, so it can be modified with the
kpeter@722
  2370
  /// subscript operator.
kpeter@694
  2371
  ///
kpeter@694
  2372
  /// \tparam GR The graph type.
kpeter@694
  2373
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@694
  2374
  /// \c GR::Edge).
kpeter@694
  2375
  ///
kpeter@694
  2376
  /// \see IterableIntMap, IterableValueMap
kpeter@694
  2377
  /// \see CrossRefMap
kpeter@694
  2378
  template <typename GR, typename K>
deba@693
  2379
  class IterableBoolMap
kpeter@694
  2380
    : protected ItemSetTraits<GR, K>::template Map<int>::Type {
deba@693
  2381
  private:
deba@693
  2382
    typedef GR Graph;
deba@693
  2383
kpeter@694
  2384
    typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
kpeter@694
  2385
    typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
kpeter@694
  2386
kpeter@694
  2387
    std::vector<K> _array;
deba@693
  2388
    int _sep;
deba@693
  2389
deba@693
  2390
  public:
deba@693
  2391
kpeter@694
  2392
    /// Indicates that the map is reference map.
deba@693
  2393
    typedef True ReferenceMapTag;
deba@693
  2394
deba@693
  2395
    /// The key type
kpeter@694
  2396
    typedef K Key;
deba@693
  2397
    /// The value type
deba@693
  2398
    typedef bool Value;
deba@693
  2399
    /// The const reference type.
deba@693
  2400
    typedef const Value& ConstReference;
deba@693
  2401
deba@693
  2402
  private:
deba@693
  2403
deba@693
  2404
    int position(const Key& key) const {
deba@693
  2405
      return Parent::operator[](key);
deba@693
  2406
    }
deba@693
  2407
deba@693
  2408
  public:
deba@693
  2409
kpeter@694
  2410
    /// \brief Reference to the value of the map.
deba@693
  2411
    ///
kpeter@694
  2412
    /// This class is similar to the \c bool type. It can be converted to
kpeter@694
  2413
    /// \c bool and it provides the same operators.
deba@693
  2414
    class Reference {
deba@693
  2415
      friend class IterableBoolMap;
deba@693
  2416
    private:
deba@693
  2417
      Reference(IterableBoolMap& map, const Key& key)
deba@693
  2418
        : _key(key), _map(map) {}
deba@693
  2419
    public:
deba@693
  2420
deba@693
  2421
      Reference& operator=(const Reference& value) {
deba@693
  2422
        _map.set(_key, static_cast<bool>(value));
deba@693
  2423
         return *this;
deba@693
  2424
      }
deba@693
  2425
deba@693
  2426
      operator bool() const {
deba@693
  2427
        return static_cast<const IterableBoolMap&>(_map)[_key];
deba@693
  2428
      }
deba@693
  2429
deba@693
  2430
      Reference& operator=(bool value) {
deba@693
  2431
        _map.set(_key, value);
deba@693
  2432
        return *this;
deba@693
  2433
      }
deba@693
  2434
      Reference& operator&=(bool value) {
deba@693
  2435
        _map.set(_key, _map[_key] & value);
deba@693
  2436
        return *this;
deba@693
  2437
      }
deba@693
  2438
      Reference& operator|=(bool value) {
deba@693
  2439
        _map.set(_key, _map[_key] | value);
deba@693
  2440
        return *this;
deba@693
  2441
      }
deba@693
  2442
      Reference& operator^=(bool value) {
deba@693
  2443
        _map.set(_key, _map[_key] ^ value);
deba@693
  2444
        return *this;
deba@693
  2445
      }
deba@693
  2446
    private:
deba@693
  2447
      Key _key;
deba@693
  2448
      IterableBoolMap& _map;
deba@693
  2449
    };
deba@693
  2450
deba@693
  2451
    /// \brief Constructor of the map with a default value.
deba@693
  2452
    ///
deba@693
  2453
    /// Constructor of the map with a default value.
deba@693
  2454
    explicit IterableBoolMap(const Graph& graph, bool def = false)
deba@693
  2455
      : Parent(graph) {
deba@693
  2456
      typename Parent::Notifier* nf = Parent::notifier();
deba@693
  2457
      Key it;
deba@693
  2458
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@693
  2459
        Parent::set(it, _array.size());
deba@693
  2460
        _array.push_back(it);
deba@693
  2461
      }
deba@693
  2462
      _sep = (def ? _array.size() : 0);
deba@693
  2463
    }
deba@693
  2464
deba@693
  2465
    /// \brief Const subscript operator of the map.
deba@693
  2466
    ///
deba@693
  2467
    /// Const subscript operator of the map.
deba@693
  2468
    bool operator[](const Key& key) const {
deba@693
  2469
      return position(key) < _sep;
deba@693
  2470
    }
deba@693
  2471
deba@693
  2472
    /// \brief Subscript operator of the map.
deba@693
  2473
    ///
deba@693
  2474
    /// Subscript operator of the map.
deba@693
  2475
    Reference operator[](const Key& key) {
deba@693
  2476
      return Reference(*this, key);
deba@693
  2477
    }
deba@693
  2478
deba@693
  2479
    /// \brief Set operation of the map.
deba@693
  2480
    ///
deba@693
  2481
    /// Set operation of the map.
deba@693
  2482
    void set(const Key& key, bool value) {
deba@693
  2483
      int pos = position(key);
deba@693
  2484
      if (value) {
deba@693
  2485
        if (pos < _sep) return;
deba@693
  2486
        Key tmp = _array[_sep];
deba@693
  2487
        _array[_sep] = key;
deba@693
  2488
        Parent::set(key, _sep);
deba@693
  2489
        _array[pos] = tmp;
deba@693
  2490
        Parent::set(tmp, pos);
deba@693
  2491
        ++_sep;
deba@693
  2492
      } else {
deba@693
  2493
        if (pos >= _sep) return;
deba@693
  2494
        --_sep;
deba@693
  2495
        Key tmp = _array[_sep];
deba@693
  2496
        _array[_sep] = key;
deba@693
  2497
        Parent::set(key, _sep);
deba@693
  2498
        _array[pos] = tmp;
deba@693
  2499
        Parent::set(tmp, pos);
deba@693
  2500
      }
deba@693
  2501
    }
deba@693
  2502
deba@693
  2503
    /// \brief Set all items.
deba@693
  2504
    ///
deba@693
  2505
    /// Set all items in the map.
deba@693
  2506
    /// \note Constant time operation.
deba@693
  2507
    void setAll(bool value) {
deba@693
  2508
      _sep = (value ? _array.size() : 0);
deba@693
  2509
    }
deba@693
  2510
kpeter@694
  2511
    /// \brief Returns the number of the keys mapped to \c true.
deba@693
  2512
    ///
kpeter@694
  2513
    /// Returns the number of the keys mapped to \c true.
deba@693
  2514
    int trueNum() const {
deba@693
  2515
      return _sep;
deba@693
  2516
    }
deba@693
  2517
kpeter@694
  2518
    /// \brief Returns the number of the keys mapped to \c false.
deba@693
  2519
    ///
kpeter@694
  2520
    /// Returns the number of the keys mapped to \c false.
deba@693
  2521
    int falseNum() const {
deba@693
  2522
      return _array.size() - _sep;
deba@693
  2523
    }
deba@693
  2524
kpeter@694
  2525
    /// \brief Iterator for the keys mapped to \c true.
deba@693
  2526
    ///
kpeter@694
  2527
    /// Iterator for the keys mapped to \c true. It works
kpeter@694
  2528
    /// like a graph item iterator, it can be converted to
deba@693
  2529
    /// the key type of the map, incremented with \c ++ operator, and
kpeter@694
  2530
    /// if the iterator leaves the last valid key, it will be equal to
deba@693
  2531
    /// \c INVALID.
deba@693
  2532
    class TrueIt : public Key {
deba@693
  2533
    public:
deba@693
  2534
      typedef Key Parent;
deba@693
  2535
deba@693
  2536
      /// \brief Creates an iterator.
deba@693
  2537
      ///
deba@693
  2538
      /// Creates an iterator. It iterates on the
kpeter@694
  2539
      /// keys mapped to \c true.
kpeter@694
  2540
      /// \param map The IterableBoolMap.
deba@693
  2541
      explicit TrueIt(const IterableBoolMap& map)
deba@693
  2542
        : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
deba@693
  2543
          _map(&map) {}
deba@693
  2544
deba@693
  2545
      /// \brief Invalid constructor \& conversion.
deba@693
  2546
      ///
kpeter@694
  2547
      /// This constructor initializes the iterator to be invalid.
deba@693
  2548
      /// \sa Invalid for more details.
deba@693
  2549
      TrueIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2550
deba@693
  2551
      /// \brief Increment operator.
deba@693
  2552
      ///
kpeter@694
  2553
      /// Increment operator.
deba@693
  2554
      TrueIt& operator++() {
deba@693
  2555
        int pos = _map->position(*this);
deba@693
  2556
        Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
deba@693
  2557
        return *this;
deba@693
  2558
      }
deba@693
  2559
deba@693
  2560
    private:
deba@693
  2561
      const IterableBoolMap* _map;
deba@693
  2562
    };
deba@693
  2563
kpeter@694
  2564
    /// \brief Iterator for the keys mapped to \c false.
deba@693
  2565
    ///
kpeter@694
  2566
    /// Iterator for the keys mapped to \c false. It works
kpeter@694
  2567
    /// like a graph item iterator, it can be converted to
deba@693
  2568
    /// the key type of the map, incremented with \c ++ operator, and
kpeter@694
  2569
    /// if the iterator leaves the last valid key, it will be equal to
deba@693
  2570
    /// \c INVALID.
deba@693
  2571
    class FalseIt : public Key {
deba@693
  2572
    public:
deba@693
  2573
      typedef Key Parent;
deba@693
  2574
deba@693
  2575
      /// \brief Creates an iterator.
deba@693
  2576
      ///
deba@693
  2577
      /// Creates an iterator. It iterates on the
kpeter@694
  2578
      /// keys mapped to \c false.
kpeter@694
  2579
      /// \param map The IterableBoolMap.
deba@693
  2580
      explicit FalseIt(const IterableBoolMap& map)
deba@693
  2581
        : Parent(map._sep < int(map._array.size()) ?
deba@693
  2582
                 map._array.back() : INVALID), _map(&map) {}
deba@693
  2583
deba@693
  2584
      /// \brief Invalid constructor \& conversion.
deba@693
  2585
      ///
kpeter@694
  2586
      /// This constructor initializes the iterator to be invalid.
deba@693
  2587
      /// \sa Invalid for more details.
deba@693
  2588
      FalseIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2589
deba@693
  2590
      /// \brief Increment operator.
deba@693
  2591
      ///
kpeter@694
  2592
      /// Increment operator.
deba@693
  2593
      FalseIt& operator++() {
deba@693
  2594
        int pos = _map->position(*this);
deba@693
  2595
        Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
deba@693
  2596
        return *this;
deba@693
  2597
      }
deba@693
  2598
deba@693
  2599
    private:
deba@693
  2600
      const IterableBoolMap* _map;
deba@693
  2601
    };
deba@693
  2602
deba@693
  2603
    /// \brief Iterator for the keys mapped to a given value.
deba@693
  2604
    ///
deba@693
  2605
    /// Iterator for the keys mapped to a given value. It works
kpeter@694
  2606
    /// like a graph item iterator, it can be converted to
deba@693
  2607
    /// the key type of the map, incremented with \c ++ operator, and
kpeter@694
  2608
    /// if the iterator leaves the last valid key, it will be equal to
deba@693
  2609
    /// \c INVALID.
deba@693
  2610
    class ItemIt : public Key {
deba@693
  2611
    public:
deba@693
  2612
      typedef Key Parent;
deba@693
  2613
kpeter@694
  2614
      /// \brief Creates an iterator with a value.
deba@693
  2615
      ///
kpeter@694
  2616
      /// Creates an iterator with a value. It iterates on the
kpeter@694
  2617
      /// keys mapped to the given value.
kpeter@694
  2618
      /// \param map The IterableBoolMap.
kpeter@694
  2619
      /// \param value The value.
deba@693
  2620
      ItemIt(const IterableBoolMap& map, bool value)
deba@693
  2621
        : Parent(value ? 
deba@693
  2622
                 (map._sep > 0 ?
deba@693
  2623
                  map._array[map._sep - 1] : INVALID) :
deba@693
  2624
                 (map._sep < int(map._array.size()) ?
deba@693
  2625
                  map._array.back() : INVALID)), _map(&map) {}
deba@693
  2626
deba@693
  2627
      /// \brief Invalid constructor \& conversion.
deba@693
  2628
      ///
kpeter@694
  2629
      /// This constructor initializes the iterator to be invalid.
deba@693
  2630
      /// \sa Invalid for more details.
deba@693
  2631
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2632
deba@693
  2633
      /// \brief Increment operator.
deba@693
  2634
      ///
kpeter@694
  2635
      /// Increment operator.
deba@693
  2636
      ItemIt& operator++() {
deba@693
  2637
        int pos = _map->position(*this);
deba@693
  2638
        int _sep = pos >= _map->_sep ? _map->_sep : 0;
deba@693
  2639
        Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
deba@693
  2640
        return *this;
deba@693
  2641
      }
deba@693
  2642
deba@693
  2643
    private:
deba@693
  2644
      const IterableBoolMap* _map;
deba@693
  2645
    };
deba@693
  2646
deba@693
  2647
  protected:
deba@693
  2648
deba@693
  2649
    virtual void add(const Key& key) {
deba@693
  2650
      Parent::add(key);
deba@693
  2651
      Parent::set(key, _array.size());
deba@693
  2652
      _array.push_back(key);
deba@693
  2653
    }
deba@693
  2654
deba@693
  2655
    virtual void add(const std::vector<Key>& keys) {
deba@693
  2656
      Parent::add(keys);
deba@693
  2657
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  2658
        Parent::set(keys[i], _array.size());
deba@693
  2659
        _array.push_back(keys[i]);
deba@693
  2660
      }
deba@693
  2661
    }
deba@693
  2662
deba@693
  2663
    virtual void erase(const Key& key) {
deba@693
  2664
      int pos = position(key);
deba@693
  2665
      if (pos < _sep) {
deba@693
  2666
        --_sep;
deba@693
  2667
        Parent::set(_array[_sep], pos);
deba@693
  2668
        _array[pos] = _array[_sep];
deba@693
  2669
        Parent::set(_array.back(), _sep);
deba@693
  2670
        _array[_sep] = _array.back();
deba@693
  2671
        _array.pop_back();
deba@693
  2672
      } else {
deba@693
  2673
        Parent::set(_array.back(), pos);
deba@693
  2674
        _array[pos] = _array.back();
deba@693
  2675
        _array.pop_back();
deba@693
  2676
      }
deba@693
  2677
      Parent::erase(key);
deba@693
  2678
    }
deba@693
  2679
deba@693
  2680
    virtual void erase(const std::vector<Key>& keys) {
deba@693
  2681
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  2682
        int pos = position(keys[i]);
deba@693
  2683
        if (pos < _sep) {
deba@693
  2684
          --_sep;
deba@693
  2685
          Parent::set(_array[_sep], pos);
deba@693
  2686
          _array[pos] = _array[_sep];
deba@693
  2687
          Parent::set(_array.back(), _sep);
deba@693
  2688
          _array[_sep] = _array.back();
deba@693
  2689
          _array.pop_back();
deba@693
  2690
        } else {
deba@693
  2691
          Parent::set(_array.back(), pos);
deba@693
  2692
          _array[pos] = _array.back();
deba@693
  2693
          _array.pop_back();
deba@693
  2694
        }
deba@693
  2695
      }
deba@693
  2696
      Parent::erase(keys);
deba@693
  2697
    }
deba@693
  2698
deba@693
  2699
    virtual void build() {
deba@693
  2700
      Parent::build();
deba@693
  2701
      typename Parent::Notifier* nf = Parent::notifier();
deba@693
  2702
      Key it;
deba@693
  2703
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@693
  2704
        Parent::set(it, _array.size());
deba@693
  2705
        _array.push_back(it);
deba@693
  2706
      }
deba@693
  2707
      _sep = 0;
deba@693
  2708
    }
deba@693
  2709
deba@693
  2710
    virtual void clear() {
deba@693
  2711
      _array.clear();
deba@693
  2712
      _sep = 0;
deba@693
  2713
      Parent::clear();
deba@693
  2714
    }
deba@693
  2715
deba@693
  2716
  };
deba@693
  2717
deba@693
  2718
deba@693
  2719
  namespace _maps_bits {
deba@693
  2720
    template <typename Item>
deba@693
  2721
    struct IterableIntMapNode {
deba@693
  2722
      IterableIntMapNode() : value(-1) {}
deba@693
  2723
      IterableIntMapNode(int _value) : value(_value) {}
deba@693
  2724
      Item prev, next;
deba@693
  2725
      int value;
deba@693
  2726
    };
deba@693
  2727
  }
deba@693
  2728
deba@693
  2729
  /// \brief Dynamic iterable integer map.
deba@693
  2730
  ///
kpeter@694
  2731
  /// This class provides a special graph map type which can store an
kpeter@694
  2732
  /// integer value for graph items (\c Node, \c Arc or \c Edge).
kpeter@694
  2733
  /// For each non-negative value it is possible to iterate on the keys
kpeter@694
  2734
  /// mapped to the value.
deba@693
  2735
  ///
kpeter@722
  2736
  /// This map is intended to be used with small integer values, for which
kpeter@722
  2737
  /// it is efficient, and supports iteration only for non-negative values.
kpeter@722
  2738
  /// If you need large values and/or iteration for negative integers,
kpeter@722
  2739
  /// consider to use \ref IterableValueMap instead.
kpeter@722
  2740
  ///
kpeter@694
  2741
  /// This type is a reference map, so it can be modified with the
kpeter@722
  2742
  /// subscript operator.
kpeter@694
  2743
  ///
kpeter@694
  2744
  /// \note The size of the data structure depends on the largest
deba@693
  2745
  /// value in the map.
deba@693
  2746
  ///
kpeter@694
  2747
  /// \tparam GR The graph type.
kpeter@694
  2748
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@694
  2749
  /// \c GR::Edge).
kpeter@694
  2750
  ///
kpeter@694
  2751
  /// \see IterableBoolMap, IterableValueMap
kpeter@694
  2752
  /// \see CrossRefMap
kpeter@694
  2753
  template <typename GR, typename K>
deba@693
  2754
  class IterableIntMap
kpeter@694
  2755
    : protected ItemSetTraits<GR, K>::
kpeter@694
  2756
        template Map<_maps_bits::IterableIntMapNode<K> >::Type {
deba@693
  2757
  public:
kpeter@694
  2758
    typedef typename ItemSetTraits<GR, K>::
kpeter@694
  2759
      template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
deba@693
  2760
deba@693
  2761
    /// The key type
kpeter@694
  2762
    typedef K Key;
deba@693
  2763
    /// The value type
deba@693
  2764
    typedef int Value;
deba@693
  2765
    /// The graph type
deba@693
  2766
    typedef GR Graph;
deba@693
  2767
deba@693
  2768
    /// \brief Constructor of the map.
deba@693
  2769
    ///
kpeter@694
  2770
    /// Constructor of the map. It sets all values to -1.
deba@693
  2771
    explicit IterableIntMap(const Graph& graph)
deba@693
  2772
      : Parent(graph) {}
deba@693
  2773
deba@693
  2774
    /// \brief Constructor of the map with a given value.
deba@693
  2775
    ///
deba@693
  2776
    /// Constructor of the map with a given value.
deba@693
  2777
    explicit IterableIntMap(const Graph& graph, int value)
kpeter@694
  2778
      : Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
deba@693
  2779
      if (value >= 0) {
deba@693
  2780
        for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@693
  2781
          lace(it);
deba@693
  2782
        }
deba@693
  2783
      }
deba@693
  2784
    }
deba@693
  2785
deba@693
  2786
  private:
deba@693
  2787
deba@693
  2788
    void unlace(const Key& key) {
deba@693
  2789
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  2790
      if (node.value < 0) return;
deba@693
  2791
      if (node.prev != INVALID) {
deba@693
  2792
        Parent::operator[](node.prev).next = node.next;
deba@693
  2793
      } else {
deba@693
  2794
        _first[node.value] = node.next;
deba@693
  2795
      }
deba@693
  2796
      if (node.next != INVALID) {
deba@693
  2797
        Parent::operator[](node.next).prev = node.prev;
deba@693
  2798
      }
deba@693
  2799
      while (!_first.empty() && _first.back() == INVALID) {
deba@693
  2800
        _first.pop_back();
deba@693
  2801
      }
deba@693
  2802
    }
deba@693
  2803
deba@693
  2804
    void lace(const Key& key) {
deba@693
  2805
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  2806
      if (node.value < 0) return;
deba@693
  2807
      if (node.value >= int(_first.size())) {
deba@693
  2808
        _first.resize(node.value + 1, INVALID);
deba@693
  2809
      }
deba@693
  2810
      node.prev = INVALID;
deba@693
  2811
      node.next = _first[node.value];
deba@693
  2812
      if (node.next != INVALID) {
deba@693
  2813
        Parent::operator[](node.next).prev = key;
deba@693
  2814
      }
deba@693
  2815
      _first[node.value] = key;
deba@693
  2816
    }
deba@693
  2817
deba@693
  2818
  public:
deba@693
  2819
kpeter@694
  2820
    /// Indicates that the map is reference map.
deba@693
  2821
    typedef True ReferenceMapTag;
deba@693
  2822
kpeter@694
  2823
    /// \brief Reference to the value of the map.
deba@693
  2824
    ///
kpeter@694
  2825
    /// This class is similar to the \c int type. It can
kpeter@694
  2826
    /// be converted to \c int and it has the same operators.
deba@693
  2827
    class Reference {
deba@693
  2828
      friend class IterableIntMap;
deba@693
  2829
    private:
deba@693
  2830
      Reference(IterableIntMap& map, const Key& key)
deba@693
  2831
        : _key(key), _map(map) {}
deba@693
  2832
    public:
deba@693
  2833
deba@693
  2834
      Reference& operator=(const Reference& value) {
deba@693
  2835
        _map.set(_key, static_cast<const int&>(value));
deba@693
  2836
         return *this;
deba@693
  2837
      }
deba@693
  2838
deba@693
  2839
      operator const int&() const {
deba@693
  2840
        return static_cast<const IterableIntMap&>(_map)[_key];
deba@693
  2841
      }
deba@693
  2842
deba@693
  2843
      Reference& operator=(int value) {
deba@693
  2844
        _map.set(_key, value);
deba@693
  2845
        return *this;
deba@693
  2846
      }
deba@693
  2847
      Reference& operator++() {
deba@693
  2848
        _map.set(_key, _map[_key] + 1);
deba@693
  2849
        return *this;
deba@693
  2850
      }
deba@693
  2851
      int operator++(int) {
deba@693
  2852
        int value = _map[_key];
deba@693
  2853
        _map.set(_key, value + 1);
deba@693
  2854
        return value;
deba@693
  2855
      }
deba@693
  2856
      Reference& operator--() {
deba@693
  2857
        _map.set(_key, _map[_key] - 1);
deba@693
  2858
        return *this;
deba@693
  2859
      }
deba@693
  2860
      int operator--(int) {
deba@693
  2861
        int value = _map[_key];
deba@693
  2862
        _map.set(_key, value - 1);
deba@693
  2863
        return value;
deba@693
  2864
      }
deba@693
  2865
      Reference& operator+=(int value) {
deba@693
  2866
        _map.set(_key, _map[_key] + value);
deba@693
  2867
        return *this;
deba@693
  2868
      }
deba@693
  2869
      Reference& operator-=(int value) {
deba@693
  2870
        _map.set(_key, _map[_key] - value);
deba@693
  2871
        return *this;
deba@693
  2872
      }
deba@693
  2873
      Reference& operator*=(int value) {
deba@693
  2874
        _map.set(_key, _map[_key] * value);
deba@693
  2875
        return *this;
deba@693
  2876
      }
deba@693
  2877
      Reference& operator/=(int value) {
deba@693
  2878
        _map.set(_key, _map[_key] / value);
deba@693
  2879
        return *this;
deba@693
  2880
      }
deba@693
  2881
      Reference& operator%=(int value) {
deba@693
  2882
        _map.set(_key, _map[_key] % value);
deba@693
  2883
        return *this;
deba@693
  2884
      }
deba@693
  2885
      Reference& operator&=(int value) {
deba@693
  2886
        _map.set(_key, _map[_key] & value);
deba@693
  2887
        return *this;
deba@693
  2888
      }
deba@693
  2889
      Reference& operator|=(int value) {
deba@693
  2890
        _map.set(_key, _map[_key] | value);
deba@693
  2891
        return *this;
deba@693
  2892
      }
deba@693
  2893
      Reference& operator^=(int value) {
deba@693
  2894
        _map.set(_key, _map[_key] ^ value);
deba@693
  2895
        return *this;
deba@693
  2896
      }
deba@693
  2897
      Reference& operator<<=(int value) {
deba@693
  2898
        _map.set(_key, _map[_key] << value);
deba@693
  2899
        return *this;
deba@693
  2900
      }
deba@693
  2901
      Reference& operator>>=(int value) {
deba@693
  2902
        _map.set(_key, _map[_key] >> value);
deba@693
  2903
        return *this;
deba@693
  2904
      }
deba@693
  2905
deba@693
  2906
    private:
deba@693
  2907
      Key _key;
deba@693
  2908
      IterableIntMap& _map;
deba@693
  2909
    };
deba@693
  2910
deba@693
  2911
    /// The const reference type.
deba@693
  2912
    typedef const Value& ConstReference;
deba@693
  2913
deba@693
  2914
    /// \brief Gives back the maximal value plus one.
deba@693
  2915
    ///
deba@693
  2916
    /// Gives back the maximal value plus one.
deba@693
  2917
    int size() const {
deba@693
  2918
      return _first.size();
deba@693
  2919
    }
deba@693
  2920
deba@693
  2921
    /// \brief Set operation of the map.
deba@693
  2922
    ///
deba@693
  2923
    /// Set operation of the map.
deba@693
  2924
    void set(const Key& key, const Value& value) {
deba@693
  2925
      unlace(key);
deba@693
  2926
      Parent::operator[](key).value = value;
deba@693
  2927
      lace(key);
deba@693
  2928
    }
deba@693
  2929
deba@693
  2930
    /// \brief Const subscript operator of the map.
deba@693
  2931
    ///
deba@693
  2932
    /// Const subscript operator of the map.
deba@693
  2933
    const Value& operator[](const Key& key) const {
deba@693
  2934
      return Parent::operator[](key).value;
deba@693
  2935
    }
deba@693
  2936
deba@693
  2937
    /// \brief Subscript operator of the map.
deba@693
  2938
    ///
deba@693
  2939
    /// Subscript operator of the map.
deba@693
  2940
    Reference operator[](const Key& key) {
deba@693
  2941
      return Reference(*this, key);
deba@693
  2942
    }
deba@693
  2943
deba@693
  2944
    /// \brief Iterator for the keys with the same value.
deba@693
  2945
    ///
deba@693
  2946
    /// Iterator for the keys with the same value. It works
kpeter@694
  2947
    /// like a graph item iterator, it can be converted to
deba@693
  2948
    /// the item type of the map, incremented with \c ++ operator, and
kpeter@694
  2949
    /// if the iterator leaves the last valid item, it will be equal to
deba@693
  2950
    /// \c INVALID.
kpeter@694
  2951
    class ItemIt : public Key {
deba@693
  2952
    public:
kpeter@694
  2953
      typedef Key Parent;
deba@693
  2954
deba@693
  2955
      /// \brief Invalid constructor \& conversion.
deba@693
  2956
      ///
kpeter@694
  2957
      /// This constructor initializes the iterator to be invalid.
deba@693
  2958
      /// \sa Invalid for more details.
deba@693
  2959
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  2960
deba@693
  2961
      /// \brief Creates an iterator with a value.
deba@693
  2962
      ///
deba@693
  2963
      /// Creates an iterator with a value. It iterates on the
kpeter@694
  2964
      /// keys mapped to the given value.
kpeter@694
  2965
      /// \param map The IterableIntMap.
kpeter@694
  2966
      /// \param value The value.
deba@693
  2967
      ItemIt(const IterableIntMap& map, int value) : _map(&map) {
deba@693
  2968
        if (value < 0 || value >= int(_map->_first.size())) {
deba@693
  2969
          Parent::operator=(INVALID);
deba@693
  2970
        } else {
deba@693
  2971
          Parent::operator=(_map->_first[value]);
deba@693
  2972
        }
deba@693
  2973
      }
deba@693
  2974
deba@693
  2975
      /// \brief Increment operator.
deba@693
  2976
      ///
kpeter@694
  2977
      /// Increment operator.
deba@693
  2978
      ItemIt& operator++() {
deba@693
  2979
        Parent::operator=(_map->IterableIntMap::Parent::
deba@693
  2980
                          operator[](static_cast<Parent&>(*this)).next);
deba@693
  2981
        return *this;
deba@693
  2982
      }
deba@693
  2983
deba@693
  2984
    private:
deba@693
  2985
      const IterableIntMap* _map;
deba@693
  2986
    };
deba@693
  2987
deba@693
  2988
  protected:
deba@693
  2989
deba@693
  2990
    virtual void erase(const Key& key) {
deba@693
  2991
      unlace(key);
deba@693
  2992
      Parent::erase(key);
deba@693
  2993
    }
deba@693
  2994
deba@693
  2995
    virtual void erase(const std::vector<Key>& keys) {
deba@693
  2996
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  2997
        unlace(keys[i]);
deba@693
  2998
      }
deba@693
  2999
      Parent::erase(keys);
deba@693
  3000
    }
deba@693
  3001
deba@693
  3002
    virtual void clear() {
deba@693
  3003
      _first.clear();
deba@693
  3004
      Parent::clear();
deba@693
  3005
    }
deba@693
  3006
deba@693
  3007
  private:
kpeter@694
  3008
    std::vector<Key> _first;
deba@693
  3009
  };
deba@693
  3010
deba@693
  3011
  namespace _maps_bits {
deba@693
  3012
    template <typename Item, typename Value>
deba@693
  3013
    struct IterableValueMapNode {
deba@693
  3014
      IterableValueMapNode(Value _value = Value()) : value(_value) {}
deba@693
  3015
      Item prev, next;
deba@693
  3016
      Value value;
deba@693
  3017
    };
deba@693
  3018
  }
deba@693
  3019
deba@693
  3020
  /// \brief Dynamic iterable map for comparable values.
deba@693
  3021
  ///
kpeter@722
  3022
  /// This class provides a special graph map type which can store a
kpeter@694
  3023
  /// comparable value for graph items (\c Node, \c Arc or \c Edge).
kpeter@694
  3024
  /// For each value it is possible to iterate on the keys mapped to
kpeter@722
  3025
  /// the value (\c ItemIt), and the values of the map can be accessed
kpeter@722
  3026
  /// with an STL compatible forward iterator (\c ValueIterator).
kpeter@722
  3027
  /// The map stores a linked list for each value, which contains
kpeter@722
  3028
  /// the items mapped to the value, and the used values are stored
kpeter@722
  3029
  /// in balanced binary tree (\c std::map).
kpeter@694
  3030
  ///
kpeter@722
  3031
  /// \ref IterableBoolMap and \ref IterableIntMap are similar classes
kpeter@722
  3032
  /// specialized for \c bool and \c int values, respectively.
deba@693
  3033
  ///
kpeter@694
  3034
  /// This type is not reference map, so it cannot be modified with
kpeter@722
  3035
  /// the subscript operator.
deba@693
  3036
  ///
kpeter@694
  3037
  /// \tparam GR The graph type.
kpeter@694
  3038
  /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
kpeter@694
  3039
  /// \c GR::Edge).
kpeter@694
  3040
  /// \tparam V The value type of the map. It can be any comparable
kpeter@694
  3041
  /// value type.
deba@693
  3042
  ///
kpeter@694
  3043
  /// \see IterableBoolMap, IterableIntMap
kpeter@694
  3044
  /// \see CrossRefMap
kpeter@694
  3045
  template <typename GR, typename K, typename V>
deba@693
  3046
  class IterableValueMap
kpeter@694
  3047
    : protected ItemSetTraits<GR, K>::
kpeter@694
  3048
        template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
deba@693
  3049
  public:
kpeter@694
  3050
    typedef typename ItemSetTraits<GR, K>::
kpeter@694
  3051
      template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
deba@693
  3052
deba@693
  3053
    /// The key type
kpeter@694
  3054
    typedef K Key;
deba@693
  3055
    /// The value type
kpeter@694
  3056
    typedef V Value;
deba@693
  3057
    /// The graph type
deba@693
  3058
    typedef GR Graph;
deba@693
  3059
deba@693
  3060
  public:
deba@693
  3061
kpeter@694
  3062
    /// \brief Constructor of the map with a given value.
deba@693
  3063
    ///
kpeter@694
  3064
    /// Constructor of the map with a given value.
deba@693
  3065
    explicit IterableValueMap(const Graph& graph,
deba@693
  3066
                              const Value& value = Value())
kpeter@694
  3067
      : Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
deba@693
  3068
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@693
  3069
        lace(it);
deba@693
  3070
      }
deba@693
  3071
    }
deba@693
  3072
deba@693
  3073
  protected:
deba@693
  3074
deba@693
  3075
    void unlace(const Key& key) {
deba@693
  3076
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  3077
      if (node.prev != INVALID) {
deba@693
  3078
        Parent::operator[](node.prev).next = node.next;
deba@693
  3079
      } else {
deba@693
  3080
        if (node.next != INVALID) {
deba@693
  3081
          _first[node.value] = node.next;
deba@693
  3082
        } else {
deba@693
  3083
          _first.erase(node.value);
deba@693
  3084
        }
deba@693
  3085
      }
deba@693
  3086
      if (node.next != INVALID) {
deba@693
  3087
        Parent::operator[](node.next).prev = node.prev;
deba@693
  3088
      }
deba@693
  3089
    }
deba@693
  3090
deba@693
  3091
    void lace(const Key& key) {
deba@693
  3092
      typename Parent::Value& node = Parent::operator[](key);
deba@693
  3093
      typename std::map<Value, Key>::iterator it = _first.find(node.value);
deba@693
  3094
      if (it == _first.end()) {
deba@693
  3095
        node.prev = node.next = INVALID;
deba@693
  3096
        _first.insert(std::make_pair(node.value, key));
deba@693
  3097
      } else {
deba@693
  3098
        node.prev = INVALID;
deba@693
  3099
        node.next = it->second;
deba@693
  3100
        if (node.next != INVALID) {
deba@693
  3101
          Parent::operator[](node.next).prev = key;
deba@693
  3102
        }
deba@693
  3103
        it->second = key;
deba@693
  3104
      }
deba@693
  3105
    }
deba@693
  3106
deba@693
  3107
  public:
deba@693
  3108
deba@693
  3109
    /// \brief Forward iterator for values.
deba@693
  3110
    ///
kpeter@722
  3111
    /// This iterator is an STL compatible forward
deba@693
  3112
    /// iterator on the values of the map. The values can
kpeter@694
  3113
    /// be accessed in the <tt>[beginValue, endValue)</tt> range.
deba@693
  3114
    class ValueIterator
deba@693
  3115
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@693
  3116
      friend class IterableValueMap;
deba@693
  3117
    private:
deba@693
  3118
      ValueIterator(typename std::map<Value, Key>::const_iterator _it)
deba@693
  3119
        : it(_it) {}
deba@693
  3120
    public:
deba@693
  3121
kpeter@722
  3122
      /// Constructor
deba@693
  3123
      ValueIterator() {}
deba@693
  3124
kpeter@722
  3125
      /// \e
deba@693
  3126
      ValueIterator& operator++() { ++it; return *this; }
kpeter@722
  3127
      /// \e
deba@693
  3128
      ValueIterator operator++(int) {
deba@693
  3129
        ValueIterator tmp(*this);
deba@693
  3130
        operator++();
deba@693
  3131
        return tmp;
deba@693
  3132
      }
deba@693
  3133
kpeter@722
  3134
      /// \e
deba@693
  3135
      const Value& operator*() const { return it->first; }
kpeter@722
  3136
      /// \e
deba@693
  3137
      const Value* operator->() const { return &(it->first); }
deba@693
  3138
kpeter@722
  3139
      /// \e
deba@693
  3140
      bool operator==(ValueIterator jt) const { return it == jt.it; }
kpeter@722
  3141
      /// \e
deba@693
  3142
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@693
  3143
deba@693
  3144
    private:
deba@693
  3145
      typename std::map<Value, Key>::const_iterator it;
deba@693
  3146
    };
deba@693
  3147
deba@693
  3148
    /// \brief Returns an iterator to the first value.
deba@693
  3149
    ///
kpeter@722
  3150
    /// Returns an STL compatible iterator to the
deba@693
  3151
    /// first value of the map. The values of the
kpeter@694
  3152
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@693
  3153
    /// range.
deba@693
  3154
    ValueIterator beginValue() const {
deba@693
  3155
      return ValueIterator(_first.begin());
deba@693
  3156
    }
deba@693
  3157
deba@693
  3158
    /// \brief Returns an iterator after the last value.
deba@693
  3159
    ///
kpeter@722
  3160
    /// Returns an STL compatible iterator after the
deba@693
  3161
    /// last value of the map. The values of the
kpeter@694
  3162
    /// map can be accessed in the <tt>[beginValue, endValue)</tt>
deba@693
  3163
    /// range.
deba@693
  3164
    ValueIterator endValue() const {
deba@693
  3165
      return ValueIterator(_first.end());
deba@693
  3166
    }
deba@693
  3167
deba@693
  3168
    /// \brief Set operation of the map.
deba@693
  3169
    ///
deba@693
  3170
    /// Set operation of the map.
deba@693
  3171
    void set(const Key& key, const Value& value) {
deba@693
  3172
      unlace(key);
deba@693
  3173
      Parent::operator[](key).value = value;
deba@693
  3174
      lace(key);
deba@693
  3175
    }
deba@693
  3176
deba@693
  3177
    /// \brief Const subscript operator of the map.
deba@693
  3178
    ///
deba@693
  3179
    /// Const subscript operator of the map.
deba@693
  3180
    const Value& operator[](const Key& key) const {
deba@693
  3181
      return Parent::operator[](key).value;
deba@693
  3182
    }
deba@693
  3183
deba@693
  3184
    /// \brief Iterator for the keys with the same value.
deba@693
  3185
    ///
deba@693
  3186
    /// Iterator for the keys with the same value. It works
kpeter@694
  3187
    /// like a graph item iterator, it can be converted to
deba@693
  3188
    /// the item type of the map, incremented with \c ++ operator, and
kpeter@694
  3189
    /// if the iterator leaves the last valid item, it will be equal to
deba@693
  3190
    /// \c INVALID.
kpeter@694
  3191
    class ItemIt : public Key {
deba@693
  3192
    public:
kpeter@694
  3193
      typedef Key Parent;
deba@693
  3194
deba@693
  3195
      /// \brief Invalid constructor \& conversion.
deba@693
  3196
      ///
kpeter@694
  3197
      /// This constructor initializes the iterator to be invalid.
deba@693
  3198
      /// \sa Invalid for more details.
deba@693
  3199
      ItemIt(Invalid) : Parent(INVALID), _map(0) {}
deba@693
  3200
deba@693
  3201
      /// \brief Creates an iterator with a value.
deba@693
  3202
      ///
deba@693
  3203
      /// Creates an iterator with a value. It iterates on the
deba@693
  3204
      /// keys which have the given value.
deba@693
  3205
      /// \param map The IterableValueMap
deba@693
  3206
      /// \param value The value
deba@693
  3207
      ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
deba@693
  3208
        typename std::map<Value, Key>::const_iterator it =
deba@693
  3209
          map._first.find(value);
deba@693
  3210
        if (it == map._first.end()) {
deba@693
  3211
          Parent::operator=(INVALID);
deba@693
  3212
        } else {
deba@693
  3213
          Parent::operator=(it->second);
deba@693
  3214
        }
deba@693
  3215
      }
deba@693
  3216
deba@693
  3217
      /// \brief Increment operator.
deba@693
  3218
      ///
deba@693
  3219
      /// Increment Operator.
deba@693
  3220
      ItemIt& operator++() {
deba@693
  3221
        Parent::operator=(_map->IterableValueMap::Parent::
deba@693
  3222
                          operator[](static_cast<Parent&>(*this)).next);
deba@693
  3223
        return *this;
deba@693
  3224
      }
deba@693
  3225
deba@693
  3226
deba@693
  3227
    private:
deba@693
  3228
      const IterableValueMap* _map;
deba@693
  3229
    };
deba@693
  3230
deba@693
  3231
  protected:
deba@693
  3232
deba@693
  3233
    virtual void add(const Key& key) {
deba@693
  3234
      Parent::add(key);
deba@693
  3235
      unlace(key);
deba@693
  3236
    }
deba@693
  3237
deba@693
  3238
    virtual void add(const std::vector<Key>& keys) {
deba@693
  3239
      Parent::add(keys);
deba@693
  3240
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  3241
        lace(keys[i]);
deba@693
  3242
      }
deba@693
  3243
    }
deba@693
  3244
deba@693
  3245
    virtual void erase(const Key& key) {
deba@693
  3246
      unlace(key);
deba@693
  3247
      Parent::erase(key);
deba@693
  3248
    }
deba@693
  3249
deba@693
  3250
    virtual void erase(const std::vector<Key>& keys) {
deba@693
  3251
      for (int i = 0; i < int(keys.size()); ++i) {
deba@693
  3252
        unlace(keys[i]);
deba@693
  3253
      }
deba@693
  3254
      Parent::erase(keys);
deba@693
  3255
    }
deba@693
  3256
deba@693
  3257
    virtual void build() {
deba@693
  3258
      Parent::build();
deba@693
  3259
      for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
deba@693
  3260
        lace(it);
deba@693
  3261
      }
deba@693
  3262
    }
deba@693
  3263
deba@693
  3264
    virtual void clear() {
deba@693
  3265
      _first.clear();
deba@693
  3266
      Parent::clear();
deba@693
  3267
    }
deba@693
  3268
deba@693
  3269
  private:
deba@693
  3270
    std::map<Value, Key> _first;
deba@693
  3271
  };
deba@693
  3272
kpeter@559
  3273
  /// \brief Map of the source nodes of arcs in a digraph.
deba@220
  3274
  ///
kpeter@559
  3275
  /// SourceMap provides access for the source node of each arc in a digraph,
kpeter@559
  3276
  /// which is returned by the \c source() function of the digraph.
kpeter@559
  3277
  /// \tparam GR The digraph type.
deba@220
  3278
  /// \see TargetMap
kpeter@559
  3279
  template <typename GR>
deba@220
  3280
  class SourceMap {
deba@220
  3281
  public:
deba@220
  3282
kpeter@559
  3283
    ///\e
kpeter@559
  3284
    typedef typename GR::Arc Key;
kpeter@559
  3285
    ///\e
kpeter@559
  3286
    typedef typename GR::Node Value;
deba@220
  3287
deba@220
  3288
    /// \brief Constructor
deba@220
  3289
    ///
kpeter@559
  3290
    /// Constructor.
kpeter@313
  3291
    /// \param digraph The digraph that the map belongs to.
kpeter@559
  3292
    explicit SourceMap(const GR& digraph) : _graph(digraph) {}
kpeter@559
  3293
kpeter@559
  3294
    /// \brief Returns the source node of the given arc.
deba@220
  3295
    ///
kpeter@559
  3296
    /// Returns the source node of the given arc.
deba@220
  3297
    Value operator[](const Key& arc) const {
kpeter@559
  3298
      return _graph.source(arc);
deba@220
  3299
    }
deba@220
  3300
deba@220
  3301
  private:
kpeter@559
  3302
    const GR& _graph;
deba@220
  3303
  };
deba@220
  3304
kpeter@301
  3305
  /// \brief Returns a \c SourceMap class.
deba@220
  3306
  ///
kpeter@301
  3307
  /// This function just returns an \c SourceMap class.
deba@220
  3308
  /// \relates SourceMap
kpeter@559
  3309
  template <typename GR>
kpeter@559
  3310
  inline SourceMap<GR> sourceMap(const GR& graph) {
kpeter@559
  3311
    return SourceMap<GR>(graph);
deba@220
  3312
  }
deba@220
  3313
kpeter@559
  3314
  /// \brief Map of the target nodes of arcs in a digraph.
deba@220
  3315
  ///
kpeter@559
  3316
  /// TargetMap provides access for the target node of each arc in a digraph,
kpeter@559
  3317
  /// which is returned by the \c target() function of the digraph.
kpeter@559
  3318
  /// \tparam GR The digraph type.
deba@220
  3319
  /// \see SourceMap
kpeter@559
  3320
  template <typename GR>
deba@220
  3321
  class TargetMap {
deba@220
  3322
  public:
deba@220
  3323
kpeter@559
  3324
    ///\e
kpeter@559
  3325
    typedef typename GR::Arc Key;
kpeter@559
  3326
    ///\e
kpeter@559
  3327
    typedef typename GR::Node Value;
deba@220
  3328
deba@220
  3329
    /// \brief Constructor
deba@220
  3330
    ///
kpeter@559
  3331
    /// Constructor.
kpeter@313
  3332
    /// \param digraph The digraph that the map belongs to.
kpeter@559
  3333
    explicit TargetMap(const GR& digraph) : _graph(digraph) {}
kpeter@559
  3334
kpeter@559
  3335
    /// \brief Returns the target node of the given arc.
deba@220
  3336
    ///
kpeter@559
  3337
    /// Returns the target node of the given arc.
deba@220
  3338
    Value operator[](const Key& e) const {
kpeter@559
  3339
      return _graph.target(e);
deba@220
  3340
    }
deba@220
  3341
deba@220
  3342
  private:
kpeter@559
  3343
    const GR& _graph;
deba@220
  3344
  };
deba@220
  3345
kpeter@301
  3346
  /// \brief Returns a \c TargetMap class.
deba@220
  3347
  ///
kpeter@301
  3348
  /// This function just returns a \c TargetMap class.
deba@220
  3349
  /// \relates TargetMap
kpeter@559
  3350
  template <typename GR>
kpeter@559
  3351
  inline TargetMap<GR> targetMap(const GR& graph) {
kpeter@559
  3352
    return TargetMap<GR>(graph);
deba@220
  3353
  }
deba@220
  3354
kpeter@559
  3355
  /// \brief Map of the "forward" directed arc view of edges in a graph.
deba@220
  3356
  ///
kpeter@559
  3357
  /// ForwardMap provides access for the "forward" directed arc view of
kpeter@559
  3358
  /// each edge in a graph, which is returned by the \c direct() function
kpeter@559
  3359
  /// of the graph with \c true parameter.
kpeter@559
  3360
  /// \tparam GR The graph type.
deba@220
  3361
  /// \see BackwardMap
kpeter@559
  3362
  template <typename GR>
deba@220
  3363
  class ForwardMap {
deba@220
  3364
  public:
deba@220
  3365
kpeter@559
  3366
    typedef typename GR::Arc Value;
kpeter@559
  3367
    typedef typename GR::Edge Key;
deba@220
  3368
deba@220
  3369
    /// \brief Constructor
deba@220
  3370
    ///
kpeter@559
  3371
    /// Constructor.
kpeter@313
  3372
    /// \param graph The graph that the map belongs to.
kpeter@559
  3373
    explicit ForwardMap(const GR& graph) : _graph(graph) {}
kpeter@559
  3374
kpeter@559
  3375
    /// \brief Returns the "forward" directed arc view of the given edge.
deba@220
  3376
    ///
kpeter@559
  3377
    /// Returns the "forward" directed arc view of the given edge.
deba@220
  3378
    Value operator[](const Key& key) const {
deba@220
  3379
      return _graph.direct(key, true);
deba@220
  3380
    }
deba@220
  3381
deba@220
  3382
  private:
kpeter@559
  3383
    const GR& _graph;
deba@220
  3384
  };
deba@220
  3385
kpeter@301
  3386
  /// \brief Returns a \c ForwardMap class.
deba@220
  3387
  ///
kpeter@301
  3388
  /// This function just returns an \c ForwardMap class.
deba@220
  3389
  /// \relates ForwardMap
kpeter@559
  3390
  template <typename GR>
kpeter@559
  3391
  inline ForwardMap<GR> forwardMap(const GR& graph) {
kpeter@559
  3392
    return ForwardMap<GR>(graph);
deba@220
  3393
  }
deba@220
  3394
kpeter@559
  3395
  /// \brief Map of the "backward" directed arc view of edges in a graph.
deba@220
  3396
  ///
kpeter@559
  3397
  /// BackwardMap provides access for the "backward" directed arc view of
kpeter@559
  3398
  /// each edge in a graph, which is returned by the \c direct() function
kpeter@559
  3399
  /// of the graph with \c false parameter.
kpeter@559
  3400
  /// \tparam GR The graph type.
deba@220
  3401
  /// \see ForwardMap
kpeter@559
  3402
  template <typename GR>
deba@220
  3403
  class BackwardMap {
deba@220
  3404
  public:
deba@220
  3405
kpeter@559
  3406
    typedef typename GR::Arc Value;
kpeter@559
  3407
    typedef typename GR::Edge Key;
deba@220
  3408
deba@220
  3409
    /// \brief Constructor
deba@220
  3410
    ///
kpeter@559
  3411
    /// Constructor.
kpeter@313
  3412
    /// \param graph The graph that the map belongs to.
kpeter@559
  3413
    explicit BackwardMap(const GR& graph) : _graph(graph) {}
kpeter@559
  3414
kpeter@559
  3415
    /// \brief Returns the "backward" directed arc view of the given edge.
deba@220
  3416
    ///
kpeter@559
  3417
    /// Returns the "backward" directed arc view of the given edge.
deba@220
  3418
    Value operator[](const Key& key) const {
deba@220
  3419
      return _graph.direct(key, false);
deba@220
  3420
    }
deba@220
  3421
deba@220
  3422
  private:
kpeter@559
  3423
    const GR& _graph;
deba@220
  3424
  };
deba@220
  3425
kpeter@301
  3426
  /// \brief Returns a \c BackwardMap class
kpeter@301
  3427
kpeter@301
  3428
  /// This function just returns a \c BackwardMap class.
deba@220
  3429
  /// \relates BackwardMap
kpeter@559
  3430
  template <typename GR>
kpeter@559
  3431
  inline BackwardMap<GR> backwardMap(const GR& graph) {
kpeter@559
  3432
    return BackwardMap<GR>(graph);
deba@220
  3433
  }
deba@220
  3434
kpeter@559
  3435
  /// \brief Map of the in-degrees of nodes in a digraph.
deba@220
  3436
  ///
deba@220
  3437
  /// This map returns the in-degree of a node. Once it is constructed,
kpeter@559
  3438
  /// the degrees are stored in a standard \c NodeMap, so each query is done
deba@220
  3439
  /// in constant time. On the other hand, the values are updated automatically
deba@220
  3440
  /// whenever the digraph changes.
deba@220
  3441
  ///
deba@693
  3442
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
kpeter@559
  3443
  /// may provide alternative ways to modify the digraph.
kpeter@559
  3444
  /// The correct behavior of InDegMap is not guarantied if these additional
kpeter@559
  3445
  /// features are used. For example the functions
kpeter@559
  3446
  /// \ref ListDigraph::changeSource() "changeSource()",
deba@220
  3447
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
deba@220
  3448
  /// \ref ListDigraph::reverseArc() "reverseArc()"
deba@220
  3449
  /// of \ref ListDigraph will \e not update the degree values correctly.
deba@220
  3450
  ///
deba@220
  3451
  /// \sa OutDegMap
kpeter@559
  3452
  template <typename GR>
deba@220
  3453
  class InDegMap
kpeter@559
  3454
    : protected ItemSetTraits<GR, typename GR::Arc>
deba@220
  3455
      ::ItemNotifier::ObserverBase {
deba@220
  3456
deba@220
  3457
  public:
deba@693
  3458
kpeter@617
  3459
    /// The graph type of InDegMap
kpeter@617
  3460
    typedef GR Graph;
kpeter@559
  3461
    typedef GR Digraph;
kpeter@559
  3462
    /// The key type
kpeter@559
  3463
    typedef typename Digraph::Node Key;
kpeter@559
  3464
    /// The value type
deba@220
  3465
    typedef int Value;
deba@220
  3466
deba@220
  3467
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
deba@220
  3468
    ::ItemNotifier::ObserverBase Parent;
deba@220
  3469
deba@220
  3470
  private:
deba@220
  3471
deba@220
  3472
    class AutoNodeMap
deba@220
  3473
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
deba@220
  3474
    public:
deba@220
  3475
deba@220
  3476
      typedef typename ItemSetTraits<Digraph, Key>::
deba@220
  3477
      template Map<int>::Type Parent;
deba@220
  3478
deba@220
  3479
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
deba@220
  3480
deba@220
  3481
      virtual void add(const Key& key) {
deba@220
  3482
        Parent::add(key);
deba@220
  3483
        Parent::set(key, 0);
deba@220
  3484
      }
deba@220
  3485
deba@220
  3486
      virtual void add(const std::vector<Key>& keys) {
deba@220
  3487
        Parent::add(keys);
deba@220
  3488
        for (int i = 0; i < int(keys.size()); ++i) {
deba@220
  3489
          Parent::set(keys[i], 0);
deba@220
  3490
        }
deba@220
  3491
      }
deba@220
  3492
deba@220
  3493
      virtual void build() {
deba@220
  3494
        Parent::build();
deba@220
  3495
        Key it;
deba@220
  3496
        typename Parent::Notifier* nf = Parent::notifier();
deba@220
  3497
        for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  3498
          Parent::set(it, 0);
deba@220
  3499
        }
deba@220
  3500
      }
deba@220
  3501
    };
deba@220
  3502
deba@220
  3503
  public:
deba@220
  3504
deba@220
  3505
    /// \brief Constructor.
deba@220
  3506
    ///
kpeter@559
  3507
    /// Constructor for creating an in-degree map.
kpeter@559
  3508
    explicit InDegMap(const Digraph& graph)
kpeter@559
  3509
      : _digraph(graph), _deg(graph) {
deba@220
  3510
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
deba@220
  3511
deba@220
  3512
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3513
        _deg[it] = countInArcs(_digraph, it);
deba@220
  3514
      }
deba@220
  3515
    }
deba@220
  3516
kpeter@559
  3517
    /// \brief Gives back the in-degree of a Node.
kpeter@559
  3518
    ///
deba@220
  3519
    /// Gives back the in-degree of a Node.
deba@220
  3520
    int operator[](const Key& key) const {
deba@220
  3521
      return _deg[key];
deba@220
  3522
    }
deba@220
  3523
deba@220
  3524
  protected:
deba@220
  3525
deba@220
  3526
    typedef typename Digraph::Arc Arc;
deba@220
  3527
deba@220
  3528
    virtual void add(const Arc& arc) {
deba@220
  3529
      ++_deg[_digraph.target(arc)];
deba@220
  3530
    }
deba@220
  3531
deba@220
  3532
    virtual void add(const std::vector<Arc>& arcs) {
deba@220
  3533
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3534
        ++_deg[_digraph.target(arcs[i])];
deba@220
  3535
      }
deba@220
  3536
    }
deba@220
  3537
deba@220
  3538
    virtual void erase(const Arc& arc) {
deba@220
  3539
      --_deg[_digraph.target(arc)];
deba@220
  3540
    }
deba@220
  3541
deba@220
  3542
    virtual void erase(const std::vector<Arc>& arcs) {
deba@220
  3543
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3544
        --_deg[_digraph.target(arcs[i])];
deba@220
  3545
      }
deba@220
  3546
    }
deba@220
  3547
deba@220
  3548
    virtual void build() {
deba@220
  3549
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3550
        _deg[it] = countInArcs(_digraph, it);
deba@220
  3551
      }
deba@220
  3552
    }
deba@220
  3553
deba@220
  3554
    virtual void clear() {
deba@220
  3555
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3556
        _deg[it] = 0;
deba@220
  3557
      }
deba@220
  3558
    }
deba@220
  3559
  private:
deba@220
  3560
deba@220
  3561
    const Digraph& _digraph;
deba@220
  3562
    AutoNodeMap _deg;
deba@220
  3563
  };
deba@220
  3564
kpeter@559
  3565
  /// \brief Map of the out-degrees of nodes in a digraph.
deba@220
  3566
  ///
deba@220
  3567
  /// This map returns the out-degree of a node. Once it is constructed,
kpeter@559
  3568
  /// the degrees are stored in a standard \c NodeMap, so each query is done
deba@220
  3569
  /// in constant time. On the other hand, the values are updated automatically
deba@220
  3570
  /// whenever the digraph changes.
deba@220
  3571
  ///
deba@693
  3572
  /// \warning Besides \c addNode() and \c addArc(), a digraph structure
kpeter@559
  3573
  /// may provide alternative ways to modify the digraph.
kpeter@559
  3574
  /// The correct behavior of OutDegMap is not guarantied if these additional
kpeter@559
  3575
  /// features are used. For example the functions
kpeter@559
  3576
  /// \ref ListDigraph::changeSource() "changeSource()",
deba@220
  3577
  /// \ref ListDigraph::changeTarget() "changeTarget()" and
deba@220
  3578
  /// \ref ListDigraph::reverseArc() "reverseArc()"
deba@220
  3579
  /// of \ref ListDigraph will \e not update the degree values correctly.
deba@220
  3580
  ///
deba@220
  3581
  /// \sa InDegMap
kpeter@559
  3582
  template <typename GR>
deba@220
  3583
  class OutDegMap
kpeter@559
  3584
    : protected ItemSetTraits<GR, typename GR::Arc>
deba@220
  3585
      ::ItemNotifier::ObserverBase {
deba@220
  3586
deba@220
  3587
  public:
deba@220
  3588
kpeter@617
  3589
    /// The graph type of OutDegMap
kpeter@617
  3590
    typedef GR Graph;
kpeter@559
  3591
    typedef GR Digraph;
kpeter@559
  3592
    /// The key type
kpeter@559
  3593
    typedef typename Digraph::Node Key;
kpeter@559
  3594
    /// The value type
deba@220
  3595
    typedef int Value;
deba@220
  3596
deba@220
  3597
    typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
deba@220
  3598
    ::ItemNotifier::ObserverBase Parent;
deba@220
  3599
deba@220
  3600
  private:
deba@220
  3601
deba@220
  3602
    class AutoNodeMap
deba@220
  3603
      : public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
deba@220
  3604
    public:
deba@220
  3605
deba@220
  3606
      typedef typename ItemSetTraits<Digraph, Key>::
deba@220
  3607
      template Map<int>::Type Parent;
deba@220
  3608
deba@220
  3609
      AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
deba@220
  3610
deba@220
  3611
      virtual void add(const Key& key) {
deba@220
  3612
        Parent::add(key);
deba@220
  3613
        Parent::set(key, 0);
deba@220
  3614
      }
deba@220
  3615
      virtual void add(const std::vector<Key>& keys) {
deba@220
  3616
        Parent::add(keys);
deba@220
  3617
        for (int i = 0; i < int(keys.size()); ++i) {
deba@220
  3618
          Parent::set(keys[i], 0);
deba@220
  3619
        }
deba@220
  3620
      }
deba@220
  3621
      virtual void build() {
deba@220
  3622
        Parent::build();
deba@220
  3623
        Key it;
deba@220
  3624
        typename Parent::Notifier* nf = Parent::notifier();
deba@220
  3625
        for (nf->first(it); it != INVALID; nf->next(it)) {
deba@220
  3626
          Parent::set(it, 0);
deba@220
  3627
        }
deba@220
  3628
      }
deba@220
  3629
    };
deba@220
  3630
deba@220
  3631
  public:
deba@220
  3632
deba@220
  3633
    /// \brief Constructor.
deba@220
  3634
    ///
kpeter@559
  3635
    /// Constructor for creating an out-degree map.
kpeter@559
  3636
    explicit OutDegMap(const Digraph& graph)
kpeter@559
  3637
      : _digraph(graph), _deg(graph) {
deba@220
  3638
      Parent::attach(_digraph.notifier(typename Digraph::Arc()));
deba@220
  3639
deba@220
  3640
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3641
        _deg[it] = countOutArcs(_digraph, it);
deba@220
  3642
      }
deba@220
  3643
    }
deba@220
  3644
kpeter@559
  3645
    /// \brief Gives back the out-degree of a Node.
kpeter@559
  3646
    ///
deba@220
  3647
    /// Gives back the out-degree of a Node.
deba@220
  3648
    int operator[](const Key& key) const {
deba@220
  3649
      return _deg[key];
deba@220
  3650
    }
deba@220
  3651
deba@220
  3652
  protected:
deba@220
  3653
deba@220
  3654
    typedef typename Digraph::Arc Arc;
deba@220
  3655
deba@220
  3656
    virtual void add(const Arc& arc) {
deba@220
  3657
      ++_deg[_digraph.source(arc)];
deba@220
  3658
    }
deba@220
  3659
deba@220
  3660
    virtual void add(const std::vector<Arc>& arcs) {
deba@220
  3661
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3662
        ++_deg[_digraph.source(arcs[i])];
deba@220
  3663
      }
deba@220
  3664
    }
deba@220
  3665
deba@220
  3666
    virtual void erase(const Arc& arc) {
deba@220
  3667
      --_deg[_digraph.source(arc)];
deba@220
  3668
    }
deba@220
  3669
deba@220
  3670
    virtual void erase(const std::vector<Arc>& arcs) {
deba@220
  3671
      for (int i = 0; i < int(arcs.size()); ++i) {
deba@220
  3672
        --_deg[_digraph.source(arcs[i])];
deba@220
  3673
      }
deba@220
  3674
    }
deba@220
  3675
deba@220
  3676
    virtual void build() {
deba@220
  3677
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3678
        _deg[it] = countOutArcs(_digraph, it);
deba@220
  3679
      }
deba@220
  3680
    }
deba@220
  3681
deba@220
  3682
    virtual void clear() {
deba@220
  3683
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
deba@220
  3684
        _deg[it] = 0;
deba@220
  3685
      }
deba@220
  3686
    }
deba@220
  3687
  private:
deba@220
  3688
deba@220
  3689
    const Digraph& _digraph;
deba@220
  3690
    AutoNodeMap _deg;
deba@220
  3691
  };
deba@220
  3692
kpeter@559
  3693
  /// \brief Potential difference map
kpeter@559
  3694
  ///
kpeter@584
  3695
  /// PotentialDifferenceMap returns the difference between the potentials of
kpeter@584
  3696
  /// the source and target nodes of each arc in a digraph, i.e. it returns
kpeter@559
  3697
  /// \code
kpeter@559
  3698
  ///   potential[gr.target(arc)] - potential[gr.source(arc)].
kpeter@559
  3699
  /// \endcode
kpeter@559
  3700
  /// \tparam GR The digraph type.
kpeter@559
  3701
  /// \tparam POT A node map storing the potentials.
kpeter@559
  3702
  template <typename GR, typename POT>
kpeter@559
  3703
  class PotentialDifferenceMap {
kpeter@559
  3704
  public:
kpeter@559
  3705
    /// Key type
kpeter@559
  3706
    typedef typename GR::Arc Key;
kpeter@559
  3707
    /// Value type
kpeter@559
  3708
    typedef typename POT::Value Value;
kpeter@559
  3709
kpeter@559
  3710
    /// \brief Constructor
kpeter@559
  3711
    ///
kpeter@559
  3712
    /// Contructor of the map.
kpeter@559
  3713
    explicit PotentialDifferenceMap(const GR& gr,
kpeter@559
  3714
                                    const POT& potential)
kpeter@559
  3715
      : _digraph(gr), _potential(potential) {}
kpeter@559
  3716
kpeter@559
  3717
    /// \brief Returns the potential difference for the given arc.
kpeter@559
  3718
    ///
kpeter@559
  3719
    /// Returns the potential difference for the given arc, i.e.
kpeter@559
  3720
    /// \code
kpeter@559
  3721
    ///   potential[gr.target(arc)] - potential[gr.source(arc)].
kpeter@559
  3722
    /// \endcode
kpeter@559
  3723
    Value operator[](const Key& arc) const {
kpeter@559
  3724
      return _potential[_digraph.target(arc)] -
kpeter@559
  3725
        _potential[_digraph.source(arc)];
kpeter@559
  3726
    }
kpeter@559
  3727
kpeter@559
  3728
  private:
kpeter@559
  3729
    const GR& _digraph;
kpeter@559
  3730
    const POT& _potential;
kpeter@559
  3731
  };
kpeter@559
  3732
kpeter@559
  3733
  /// \brief Returns a PotentialDifferenceMap.
kpeter@559
  3734
  ///
kpeter@559
  3735
  /// This function just returns a PotentialDifferenceMap.
kpeter@559
  3736
  /// \relates PotentialDifferenceMap
kpeter@559
  3737
  template <typename GR, typename POT>
kpeter@559
  3738
  PotentialDifferenceMap<GR, POT>
kpeter@559
  3739
  potentialDifferenceMap(const GR& gr, const POT& potential) {
kpeter@559
  3740
    return PotentialDifferenceMap<GR, POT>(gr, potential);
kpeter@559
  3741
  }
kpeter@559
  3742
alpar@25
  3743
  /// @}
alpar@25
  3744
}
alpar@25
  3745
alpar@25
  3746
#endif // LEMON_MAPS_H