lemon/maps.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 15 Mar 2008 21:07:24 +0100
changeset 80 15968e25ca08
parent 54 ff9bac2351bf
child 81 7ff1c348ae0c
permissions -rw-r--r--
Overall clean-up in maps.h

- Rename some map types:
* IntegerMap -> RangeMap
* StdMap -> SparseMap
* FunctorMap -> FunctorToMap
* MapFunctor -> MapToFunctor
* ForkWriteMap -> ForkMap
* SimpleMap -> WrapMap
* SimpleWriteMap -> WrapWriteMap
- Remove the read-only ForkMap version.
- Rename map-creator functions for the read-write arithmetic and
logical maps.
- Small fixes and improvements in the code.
- Fix the typedefs of RangeMap to work correctly with bool type, too.
- Rename template parameters, function parameters, and private members
in many classes to be uniform and to avoid parameter names starting
with underscore.
- Use Key and Value types instead of K and V template parameters in
public functions.
- Extend the documentation with examples (e.g. for basic arithmetic and
logical maps).
- Many doc improvements.
- Reorder the classes.
- StoreBoolMap, BackInserterBoolMap, FrontInserterBoolMap,
InserterBoolMap, FillBoolMap, SettingOrderBoolMap are almost unchanged,
since they will be removed.
- Also improve maps_test.cc to correctly check every map class, every
constructor, and every creator function.
alpar@25
     1
/* -*- C++ -*-
alpar@25
     2
 *
alpar@25
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@25
     4
 *
alpar@39
     5
 * Copyright (C) 2003-2008
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>
alpar@25
    25
alpar@25
    26
#include <lemon/bits/utility.h>
kpeter@80
    27
#include <lemon/bits/traits.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
#include <map>
alpar@25
    34
alpar@25
    35
namespace lemon {
alpar@25
    36
alpar@25
    37
  /// \addtogroup maps
alpar@25
    38
  /// @{
alpar@25
    39
alpar@25
    40
  /// Base class of maps.
alpar@25
    41
kpeter@80
    42
  /// Base class of maps. It provides the necessary type definitions
kpeter@80
    43
  /// required by the map %concepts.
kpeter@80
    44
  template<typename K, typename V>
alpar@25
    45
  class MapBase {
alpar@25
    46
  public:
kpeter@80
    47
    /// \biref The key type of the map.
alpar@25
    48
    typedef K Key;
kpeter@80
    49
    /// \brief The value type of the map.
kpeter@80
    50
    /// (The type of objects associated with the keys).
kpeter@80
    51
    typedef V Value;
alpar@25
    52
  };
alpar@25
    53
kpeter@80
    54
alpar@25
    55
  /// Null map. (a.k.a. DoNothingMap)
alpar@25
    56
kpeter@29
    57
  /// This map can be used if you have to provide a map only for
kpeter@80
    58
  /// its type definitions, or if you have to provide a writable map,
kpeter@80
    59
  /// but data written to it is not required (i.e. it will be sent to
kpeter@29
    60
  /// <tt>/dev/null</tt>).
kpeter@80
    61
  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
kpeter@80
    62
  ///
kpeter@80
    63
  /// \sa ConstMap
kpeter@80
    64
  template<typename K, typename V>
kpeter@80
    65
  class NullMap : public MapBase<K, V> {
alpar@25
    66
  public:
kpeter@80
    67
    typedef MapBase<K, V> Parent;
alpar@25
    68
    typedef typename Parent::Key Key;
alpar@25
    69
    typedef typename Parent::Value Value;
kpeter@80
    70
alpar@25
    71
    /// Gives back a default constructed element.
kpeter@80
    72
    Value operator[](const Key&) const { return Value(); }
alpar@25
    73
    /// Absorbs the value.
kpeter@80
    74
    void set(const Key&, const Value&) {}
alpar@25
    75
  };
alpar@25
    76
kpeter@80
    77
  /// Returns a \ref NullMap class
kpeter@29
    78
kpeter@80
    79
  /// This function just returns a \ref NullMap class.
kpeter@80
    80
  /// \relates NullMap
kpeter@80
    81
  template <typename K, typename V>
alpar@25
    82
  NullMap<K, V> nullMap() {
alpar@25
    83
    return NullMap<K, V>();
alpar@25
    84
  }
alpar@25
    85
alpar@25
    86
alpar@25
    87
  /// Constant map.
alpar@25
    88
kpeter@80
    89
  /// This is a \ref concepts::ReadMap "readable" map which assigns a
kpeter@47
    90
  /// specified value to each key.
kpeter@80
    91
  ///
kpeter@80
    92
  /// In other aspects it is equivalent to \ref NullMap.
kpeter@80
    93
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@80
    94
  /// concept, but it absorbs the data written to it.
kpeter@80
    95
  ///
kpeter@80
    96
  /// The simplest way of using this map is through the constMap()
kpeter@80
    97
  /// function.
kpeter@80
    98
  ///
kpeter@80
    99
  /// \sa NullMap
kpeter@80
   100
  /// \sa IdentityMap
kpeter@80
   101
  template<typename K, typename V>
kpeter@80
   102
  class ConstMap : public MapBase<K, V> {
alpar@25
   103
  private:
kpeter@80
   104
    V _value;
alpar@25
   105
  public:
kpeter@80
   106
    typedef MapBase<K, V> Parent;
alpar@25
   107
    typedef typename Parent::Key Key;
alpar@25
   108
    typedef typename Parent::Value 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@80
   119
    /// \param v is 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@80
   137
  /// Returns a \ref ConstMap class
alpar@25
   138
kpeter@80
   139
  /// This function just returns a \ref 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
alpar@25
   146
alpar@25
   147
  template<typename T, T v>
kpeter@80
   148
  struct Const {};
alpar@25
   149
alpar@25
   150
  /// Constant map with inlined constant value.
alpar@25
   151
kpeter@80
   152
  /// This is a \ref concepts::ReadMap "readable" map which assigns a
kpeter@47
   153
  /// specified value to each key.
kpeter@80
   154
  ///
kpeter@80
   155
  /// In other aspects it is equivalent to \ref NullMap.
kpeter@80
   156
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@80
   157
  /// concept, but it absorbs the data written to it.
kpeter@80
   158
  ///
kpeter@80
   159
  /// The simplest way of using this map is through the constMap()
kpeter@80
   160
  /// function.
kpeter@80
   161
  ///
kpeter@80
   162
  /// \sa NullMap
kpeter@80
   163
  /// \sa IdentityMap
alpar@25
   164
  template<typename K, typename V, V v>
alpar@25
   165
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
alpar@25
   166
  public:
alpar@25
   167
    typedef MapBase<K, V> Parent;
alpar@25
   168
    typedef typename Parent::Key Key;
alpar@25
   169
    typedef typename Parent::Value Value;
alpar@25
   170
kpeter@80
   171
    /// Constructor.
kpeter@80
   172
    ConstMap() {}
kpeter@80
   173
kpeter@80
   174
    /// Gives back the specified value.
kpeter@80
   175
    Value operator[](const Key&) const { return v; }
kpeter@80
   176
kpeter@80
   177
    /// Absorbs the value.
kpeter@80
   178
    void set(const Key&, const Value&) {}
alpar@25
   179
  };
alpar@25
   180
kpeter@80
   181
  /// Returns a \ref ConstMap class with inlined constant value
alpar@25
   182
kpeter@80
   183
  /// This function just returns a \ref ConstMap class with inlined
kpeter@80
   184
  /// constant value.
kpeter@80
   185
  /// \relates ConstMap
kpeter@80
   186
  template<typename K, typename V, V v>
alpar@25
   187
  inline ConstMap<K, Const<V, v> > constMap() {
alpar@25
   188
    return ConstMap<K, Const<V, v> >();
alpar@25
   189
  }
alpar@25
   190
alpar@25
   191
kpeter@80
   192
  /// \brief Identity map.
kpeter@80
   193
  ///
kpeter@80
   194
  /// This map gives back the given key as value without any
kpeter@80
   195
  /// modification.
kpeter@80
   196
  ///
kpeter@80
   197
  /// \sa ConstMap
kpeter@80
   198
  template <typename T>
kpeter@80
   199
  class IdentityMap : public MapBase<T, T> {
kpeter@80
   200
  public:
kpeter@80
   201
    typedef MapBase<T, T> Parent;
kpeter@80
   202
    typedef typename Parent::Key Key;
kpeter@80
   203
    typedef typename Parent::Value Value;
kpeter@80
   204
kpeter@80
   205
    /// Gives back the given value without any modification.
kpeter@80
   206
    const T& operator[](const T& t) const {
kpeter@80
   207
      return t;
kpeter@80
   208
    }
kpeter@80
   209
  };
kpeter@80
   210
kpeter@80
   211
  /// Returns an \ref IdentityMap class
kpeter@80
   212
kpeter@80
   213
  /// This function just returns an \ref IdentityMap class.
kpeter@80
   214
  /// \relates IdentityMap
kpeter@80
   215
  template<typename T>
kpeter@80
   216
  inline IdentityMap<T> identityMap() {
kpeter@80
   217
    return IdentityMap<T>();
kpeter@80
   218
  }
kpeter@80
   219
kpeter@80
   220
kpeter@80
   221
  /// \brief Map for storing values for integer keys from the range
kpeter@80
   222
  /// <tt>[0..size-1]</tt>.
kpeter@80
   223
  ///
kpeter@80
   224
  /// This map is essentially a wrapper for \c std::vector. It assigns
kpeter@80
   225
  /// values to integer keys from the range <tt>[0..size-1]</tt>.
kpeter@80
   226
  /// It can be used with some data structures, for example
kpeter@80
   227
  /// \ref UnionFind, \ref BinHeap, when the used items are small
kpeter@80
   228
  /// integers. This map conforms the \ref concepts::ReferenceMap
kpeter@80
   229
  /// "ReferenceMap" concept.
kpeter@80
   230
  ///
kpeter@80
   231
  /// The simplest way of using this map is through the rangeMap()
kpeter@80
   232
  /// function.
kpeter@80
   233
  template <typename V>
kpeter@80
   234
  class RangeMap : public MapBase<int, V> {
kpeter@80
   235
    template <typename V1>
kpeter@80
   236
    friend class RangeMap;
kpeter@80
   237
  private:
kpeter@80
   238
kpeter@80
   239
    typedef std::vector<V> Vector;
kpeter@80
   240
    Vector _vector;
kpeter@80
   241
alpar@25
   242
  public:
alpar@25
   243
kpeter@80
   244
    typedef MapBase<int, V> Parent;
kpeter@80
   245
    /// Key type
kpeter@45
   246
    typedef typename Parent::Key Key;
kpeter@80
   247
    /// Value type
kpeter@45
   248
    typedef typename Parent::Value Value;
kpeter@80
   249
    /// Reference type
kpeter@80
   250
    typedef typename Vector::reference Reference;
kpeter@80
   251
    /// Const reference type
kpeter@80
   252
    typedef typename Vector::const_reference ConstReference;
kpeter@80
   253
kpeter@80
   254
    typedef True ReferenceMapTag;
kpeter@80
   255
kpeter@80
   256
  public:
kpeter@80
   257
kpeter@80
   258
    /// Constructor with specified default value.
kpeter@80
   259
    RangeMap(int size = 0, const Value &value = Value())
kpeter@80
   260
      : _vector(size, value) {}
kpeter@80
   261
kpeter@80
   262
    /// Constructs the map from an appropriate \c std::vector.
kpeter@80
   263
    template <typename V1>
kpeter@80
   264
    RangeMap(const std::vector<V1>& vector)
kpeter@80
   265
      : _vector(vector.begin(), vector.end()) {}
kpeter@80
   266
kpeter@80
   267
    /// Constructs the map from another \ref RangeMap.
kpeter@80
   268
    template <typename V1>
kpeter@80
   269
    RangeMap(const RangeMap<V1> &c)
kpeter@80
   270
      : _vector(c._vector.begin(), c._vector.end()) {}
kpeter@80
   271
kpeter@80
   272
    /// Returns the size of the map.
kpeter@80
   273
    int size() {
kpeter@80
   274
      return _vector.size();
kpeter@80
   275
    }
kpeter@80
   276
kpeter@80
   277
    /// Resizes the map.
kpeter@80
   278
kpeter@80
   279
    /// Resizes the underlying \c std::vector container, so changes the
kpeter@80
   280
    /// keyset of the map.
kpeter@80
   281
    /// \param size The new size of the map. The new keyset will be the
kpeter@80
   282
    /// range <tt>[0..size-1]</tt>.
kpeter@80
   283
    /// \param value The default value to assign to the new keys.
kpeter@80
   284
    void resize(int size, const Value &value = Value()) {
kpeter@80
   285
      _vector.resize(size, value);
kpeter@80
   286
    }
kpeter@80
   287
kpeter@80
   288
  private:
kpeter@80
   289
kpeter@80
   290
    RangeMap& operator=(const RangeMap&);
kpeter@80
   291
kpeter@80
   292
  public:
kpeter@80
   293
kpeter@80
   294
    ///\e
kpeter@80
   295
    Reference operator[](const Key &k) {
kpeter@80
   296
      return _vector[k];
kpeter@80
   297
    }
kpeter@80
   298
kpeter@80
   299
    ///\e
kpeter@80
   300
    ConstReference operator[](const Key &k) const {
kpeter@80
   301
      return _vector[k];
kpeter@80
   302
    }
kpeter@80
   303
kpeter@80
   304
    ///\e
kpeter@80
   305
    void set(const Key &k, const Value &v) {
kpeter@80
   306
      _vector[k] = v;
kpeter@80
   307
    }
kpeter@80
   308
  };
kpeter@80
   309
kpeter@80
   310
  /// Returns a \ref RangeMap class
kpeter@80
   311
kpeter@80
   312
  /// This function just returns a \ref RangeMap class.
kpeter@80
   313
  /// \relates RangeMap
kpeter@80
   314
  template<typename V>
kpeter@80
   315
  inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
kpeter@80
   316
    return RangeMap<V>(size, value);
kpeter@80
   317
  }
kpeter@80
   318
kpeter@80
   319
  /// \brief Returns a \ref RangeMap class created from an appropriate
kpeter@80
   320
  /// \c std::vector
kpeter@80
   321
kpeter@80
   322
  /// This function just returns a \ref RangeMap class created from an
kpeter@80
   323
  /// appropriate \c std::vector.
kpeter@80
   324
  /// \relates RangeMap
kpeter@80
   325
  template<typename V>
kpeter@80
   326
  inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
kpeter@80
   327
    return RangeMap<V>(vector);
kpeter@80
   328
  }
kpeter@80
   329
kpeter@80
   330
kpeter@80
   331
  /// Map type based on \c std::map
kpeter@80
   332
kpeter@80
   333
  /// This map is essentially a wrapper for \c std::map with addition
kpeter@80
   334
  /// that you can specify a default value for the keys that are not
kpeter@80
   335
  /// stored actually. This value can be different from the default
kpeter@80
   336
  /// contructed value (i.e. \c %Value()).
kpeter@80
   337
  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
kpeter@80
   338
  /// concept.
kpeter@80
   339
  ///
kpeter@80
   340
  /// This map is useful if a default value should be assigned to most of
kpeter@80
   341
  /// the keys and different values should be assigned only to a few
kpeter@80
   342
  /// keys (i.e. the map is "sparse").
kpeter@80
   343
  /// The name of this type also refers to this important usage.
kpeter@80
   344
  ///
kpeter@80
   345
  /// Apart form that this map can be used in many other cases since it
kpeter@80
   346
  /// is based on \c std::map, which is a general associative container.
kpeter@80
   347
  /// However keep in mind that it is usually not as efficient as other
kpeter@80
   348
  /// maps.
kpeter@80
   349
  ///
kpeter@80
   350
  /// The simplest way of using this map is through the sparseMap()
kpeter@80
   351
  /// function.
kpeter@80
   352
  template <typename K, typename V, typename Compare = std::less<K> >
kpeter@80
   353
  class SparseMap : public MapBase<K, V> {
kpeter@80
   354
    template <typename K1, typename V1, typename C1>
kpeter@80
   355
    friend class SparseMap;
kpeter@80
   356
  public:
kpeter@80
   357
kpeter@80
   358
    typedef MapBase<K, V> Parent;
kpeter@80
   359
    /// Key type
kpeter@80
   360
    typedef typename Parent::Key Key;
kpeter@80
   361
    /// Value type
kpeter@80
   362
    typedef typename Parent::Value Value;
kpeter@80
   363
    /// Reference type
kpeter@80
   364
    typedef Value& Reference;
kpeter@80
   365
    /// Const reference type
kpeter@80
   366
    typedef const Value& ConstReference;
alpar@25
   367
kpeter@45
   368
    typedef True ReferenceMapTag;
kpeter@45
   369
alpar@25
   370
  private:
kpeter@80
   371
kpeter@80
   372
    typedef std::map<K, V, Compare> Map;
kpeter@80
   373
    Map _map;
alpar@25
   374
    Value _value;
alpar@25
   375
alpar@25
   376
  public:
alpar@25
   377
kpeter@80
   378
    /// \brief Constructor with specified default value.
kpeter@80
   379
    SparseMap(const Value &value = Value()) : _value(value) {}
kpeter@80
   380
    /// \brief Constructs the map from an appropriate \c std::map, and
kpeter@47
   381
    /// explicitly specifies a default value.
kpeter@80
   382
    template <typename V1, typename Comp1>
kpeter@80
   383
    SparseMap(const std::map<Key, V1, Comp1> &map,
kpeter@80
   384
              const Value &value = Value())
alpar@25
   385
      : _map(map.begin(), map.end()), _value(value) {}
kpeter@80
   386
kpeter@80
   387
    /// \brief Constructs the map from another \ref SparseMap.
kpeter@80
   388
    template<typename V1, typename Comp1>
kpeter@80
   389
    SparseMap(const SparseMap<Key, V1, Comp1> &c)
alpar@25
   390
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
alpar@25
   391
alpar@25
   392
  private:
alpar@25
   393
kpeter@80
   394
    SparseMap& operator=(const SparseMap&);
alpar@25
   395
alpar@25
   396
  public:
alpar@25
   397
alpar@25
   398
    ///\e
alpar@25
   399
    Reference operator[](const Key &k) {
alpar@25
   400
      typename Map::iterator it = _map.lower_bound(k);
alpar@25
   401
      if (it != _map.end() && !_map.key_comp()(k, it->first))
alpar@25
   402
	return it->second;
alpar@25
   403
      else
alpar@25
   404
	return _map.insert(it, std::make_pair(k, _value))->second;
alpar@25
   405
    }
alpar@25
   406
kpeter@80
   407
    ///\e
alpar@25
   408
    ConstReference operator[](const Key &k) const {
alpar@25
   409
      typename Map::const_iterator it = _map.find(k);
alpar@25
   410
      if (it != _map.end())
alpar@25
   411
	return it->second;
alpar@25
   412
      else
alpar@25
   413
	return _value;
alpar@25
   414
    }
alpar@25
   415
kpeter@80
   416
    ///\e
kpeter@80
   417
    void set(const Key &k, const Value &v) {
alpar@25
   418
      typename Map::iterator it = _map.lower_bound(k);
alpar@25
   419
      if (it != _map.end() && !_map.key_comp()(k, it->first))
kpeter@80
   420
	it->second = v;
alpar@25
   421
      else
kpeter@80
   422
	_map.insert(it, std::make_pair(k, v));
alpar@25
   423
    }
alpar@25
   424
kpeter@80
   425
    ///\e
kpeter@80
   426
    void setAll(const Value &v) {
kpeter@80
   427
      _value = v;
alpar@25
   428
      _map.clear();
kpeter@80
   429
    }
kpeter@80
   430
  };
alpar@25
   431
kpeter@80
   432
  /// Returns a \ref SparseMap class
kpeter@45
   433
kpeter@80
   434
  /// This function just returns a \ref SparseMap class with specified
kpeter@80
   435
  /// default value.
kpeter@80
   436
  /// \relates SparseMap
kpeter@80
   437
  template<typename K, typename V, typename Compare>
kpeter@80
   438
  inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
kpeter@80
   439
    return SparseMap<K, V, Compare>(value);
kpeter@54
   440
  }
kpeter@45
   441
kpeter@80
   442
  template<typename K, typename V>
kpeter@80
   443
  inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
kpeter@80
   444
    return SparseMap<K, V, std::less<K> >(value);
kpeter@45
   445
  }
alpar@25
   446
kpeter@80
   447
  /// \brief Returns a \ref SparseMap class created from an appropriate
kpeter@80
   448
  /// \c std::map
alpar@25
   449
kpeter@80
   450
  /// This function just returns a \ref SparseMap class created from an
kpeter@80
   451
  /// appropriate \c std::map.
kpeter@80
   452
  /// \relates SparseMap
kpeter@80
   453
  template<typename K, typename V, typename Compare>
kpeter@80
   454
  inline SparseMap<K, V, Compare>
kpeter@80
   455
    sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
kpeter@80
   456
  {
kpeter@80
   457
    return SparseMap<K, V, Compare>(map, value);
kpeter@45
   458
  }
alpar@25
   459
alpar@25
   460
  /// @}
alpar@25
   461
alpar@25
   462
  /// \addtogroup map_adaptors
alpar@25
   463
  /// @{
alpar@25
   464
kpeter@80
   465
  /// Composition of two maps
kpeter@80
   466
kpeter@80
   467
  /// This \ref concepts::ReadMap "read only map" returns the
kpeter@80
   468
  /// composition of two given maps. That is to say, if \c m1 is of
kpeter@80
   469
  /// type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   470
  /// \code
kpeter@80
   471
  ///   ComposeMap<M1, M2> cm(m1,m2);
kpeter@80
   472
  /// \endcode
kpeter@80
   473
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
alpar@25
   474
  ///
kpeter@80
   475
  /// The \c Key type of the map is inherited from \c M2 and the
kpeter@80
   476
  /// \c Value type is from \c M1.
kpeter@80
   477
  /// \c M2::Value must be convertible to \c M1::Key.
kpeter@80
   478
  ///
kpeter@80
   479
  /// The simplest way of using this map is through the composeMap()
kpeter@80
   480
  /// function.
kpeter@80
   481
  ///
kpeter@80
   482
  /// \sa CombineMap
kpeter@80
   483
  ///
kpeter@80
   484
  /// \todo Check the requirements.
kpeter@80
   485
  template <typename M1, typename M2>
kpeter@80
   486
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
kpeter@80
   487
    const M1 &_m1;
kpeter@80
   488
    const M2 &_m2;
alpar@25
   489
  public:
kpeter@80
   490
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
alpar@25
   491
    typedef typename Parent::Key Key;
alpar@25
   492
    typedef typename Parent::Value Value;
alpar@25
   493
kpeter@80
   494
    /// Constructor
kpeter@80
   495
    ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   496
alpar@25
   497
    /// \e
kpeter@80
   498
    typename MapTraits<M1>::ConstReturnValue
kpeter@80
   499
    operator[](const Key &k) const { return _m1[_m2[k]]; }
alpar@25
   500
  };
alpar@25
   501
kpeter@80
   502
  /// Returns a \ref ComposeMap class
alpar@25
   503
kpeter@80
   504
  /// This function just returns a \ref ComposeMap class.
kpeter@80
   505
  ///
kpeter@80
   506
  /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
kpeter@80
   507
  /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
kpeter@80
   508
  /// will be equal to <tt>m1[m2[x]]</tt>.
kpeter@80
   509
  ///
kpeter@80
   510
  /// \relates ComposeMap
kpeter@80
   511
  template <typename M1, typename M2>
kpeter@80
   512
  inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
kpeter@80
   513
    return ComposeMap<M1, M2>(m1, m2);
alpar@25
   514
  }
alpar@25
   515
kpeter@80
   516
kpeter@80
   517
  /// Combination of two maps using an STL (binary) functor.
kpeter@80
   518
kpeter@80
   519
  /// This \ref concepts::ReadMap "read only map" takes two maps and a
kpeter@80
   520
  /// binary functor and returns the combination of the two given maps
kpeter@80
   521
  /// using the functor.
kpeter@80
   522
  /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
kpeter@80
   523
  /// and \c f is of \c F, then for
kpeter@80
   524
  /// \code
kpeter@80
   525
  ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
kpeter@80
   526
  /// \endcode
kpeter@80
   527
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
alpar@26
   528
  ///
kpeter@80
   529
  /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
kpeter@80
   530
  /// must be convertible to \c M2::Key) and the \c Value type is \c V.
kpeter@80
   531
  /// \c M2::Value and \c M1::Value must be convertible to the
kpeter@80
   532
  /// corresponding input parameter of \c F and the return type of \c F
kpeter@80
   533
  /// must be convertible to \c V.
kpeter@80
   534
  ///
kpeter@80
   535
  /// The simplest way of using this map is through the combineMap()
kpeter@80
   536
  /// function.
kpeter@80
   537
  ///
kpeter@80
   538
  /// \sa ComposeMap
kpeter@80
   539
  ///
kpeter@80
   540
  /// \todo Check the requirements.
kpeter@80
   541
  template<typename M1, typename M2, typename F,
kpeter@80
   542
	   typename V = typename F::result_type>
kpeter@80
   543
  class CombineMap : public MapBase<typename M1::Key, V> {
kpeter@80
   544
    const M1 &_m1;
kpeter@80
   545
    const M2 &_m2;
kpeter@80
   546
    F _f;
alpar@25
   547
  public:
kpeter@80
   548
    typedef MapBase<typename M1::Key, V> Parent;
alpar@25
   549
    typedef typename Parent::Key Key;
alpar@25
   550
    typedef typename Parent::Value Value;
alpar@25
   551
kpeter@80
   552
    /// Constructor
kpeter@80
   553
    CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
kpeter@80
   554
      : _m1(m1), _m2(m2), _f(f) {}
kpeter@80
   555
    /// \e
kpeter@80
   556
    Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
kpeter@80
   557
  };
alpar@25
   558
kpeter@80
   559
  /// Returns a \ref CombineMap class
alpar@25
   560
kpeter@80
   561
  /// This function just returns a \ref CombineMap class.
kpeter@80
   562
  ///
kpeter@80
   563
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   564
  /// values, then
kpeter@80
   565
  /// \code
kpeter@80
   566
  ///   combineMap(m1,m2,std::plus<double>())
kpeter@80
   567
  /// \endcode
kpeter@80
   568
  /// is equivalent to
kpeter@80
   569
  /// \code
kpeter@80
   570
  ///   addMap(m1,m2)
kpeter@80
   571
  /// \endcode
kpeter@80
   572
  ///
kpeter@80
   573
  /// This function is specialized for adaptable binary function
kpeter@80
   574
  /// classes and C++ functions.
kpeter@80
   575
  ///
kpeter@80
   576
  /// \relates CombineMap
kpeter@80
   577
  template<typename M1, typename M2, typename F, typename V>
kpeter@80
   578
  inline CombineMap<M1, M2, F, V>
kpeter@80
   579
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
kpeter@80
   580
    return CombineMap<M1, M2, F, V>(m1,m2,f);
alpar@25
   581
  }
alpar@25
   582
kpeter@80
   583
  template<typename M1, typename M2, typename F>
kpeter@80
   584
  inline CombineMap<M1, M2, F, typename F::result_type>
kpeter@80
   585
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
kpeter@80
   586
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
kpeter@80
   587
  }
alpar@25
   588
kpeter@80
   589
  template<typename M1, typename M2, typename K1, typename K2, typename V>
kpeter@80
   590
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
kpeter@80
   591
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
kpeter@80
   592
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
kpeter@80
   593
  }
kpeter@80
   594
kpeter@80
   595
kpeter@80
   596
  /// Converts an STL style (unary) functor to a map
kpeter@80
   597
kpeter@80
   598
  /// This \ref concepts::ReadMap "read only map" returns the value
kpeter@80
   599
  /// of a given functor. Actually, it just wraps the functor and
kpeter@80
   600
  /// provides the \c Key and \c Value typedefs.
alpar@26
   601
  ///
kpeter@80
   602
  /// Template parameters \c K and \c V will become its \c Key and
kpeter@80
   603
  /// \c Value. In most cases they have to be given explicitly because
kpeter@80
   604
  /// a functor typically does not provide \c argument_type and
kpeter@80
   605
  /// \c result_type typedefs.
kpeter@80
   606
  /// Parameter \c F is the type of the used functor.
kpeter@29
   607
  ///
kpeter@80
   608
  /// The simplest way of using this map is through the functorToMap()
kpeter@80
   609
  /// function.
kpeter@80
   610
  ///
kpeter@80
   611
  /// \sa MapToFunctor
kpeter@80
   612
  template<typename F,
kpeter@80
   613
	   typename K = typename F::argument_type,
kpeter@80
   614
	   typename V = typename F::result_type>
kpeter@80
   615
  class FunctorToMap : public MapBase<K, V> {
kpeter@80
   616
    const F &_f;
kpeter@80
   617
  public:
kpeter@80
   618
    typedef MapBase<K, V> Parent;
kpeter@80
   619
    typedef typename Parent::Key Key;
kpeter@80
   620
    typedef typename Parent::Value Value;
alpar@25
   621
kpeter@80
   622
    /// Constructor
kpeter@80
   623
    FunctorToMap(const F &f = F()) : _f(f) {}
kpeter@80
   624
    /// \e
kpeter@80
   625
    Value operator[](const Key &k) const { return _f(k); }
kpeter@80
   626
  };
kpeter@80
   627
kpeter@80
   628
  /// Returns a \ref FunctorToMap class
kpeter@80
   629
kpeter@80
   630
  /// This function just returns a \ref FunctorToMap class.
kpeter@80
   631
  ///
kpeter@80
   632
  /// This function is specialized for adaptable binary function
kpeter@80
   633
  /// classes and C++ functions.
kpeter@80
   634
  ///
kpeter@80
   635
  /// \relates FunctorToMap
kpeter@80
   636
  template<typename K, typename V, typename F>
kpeter@80
   637
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
kpeter@80
   638
    return FunctorToMap<F, K, V>(f);
kpeter@80
   639
  }
kpeter@80
   640
kpeter@80
   641
  template <typename F>
kpeter@80
   642
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
kpeter@80
   643
    functorToMap(const F &f)
kpeter@80
   644
  {
kpeter@80
   645
    return FunctorToMap<F, typename F::argument_type,
kpeter@80
   646
      typename F::result_type>(f);
kpeter@80
   647
  }
kpeter@80
   648
kpeter@80
   649
  template <typename K, typename V>
kpeter@80
   650
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
kpeter@80
   651
    return FunctorToMap<V (*)(K), K, V>(f);
kpeter@80
   652
  }
kpeter@80
   653
kpeter@80
   654
kpeter@80
   655
  /// Converts a map to an STL style (unary) functor
kpeter@80
   656
kpeter@80
   657
  /// This class converts a map to an STL style (unary) functor.
kpeter@80
   658
  /// That is it provides an <tt>operator()</tt> to read its values.
kpeter@80
   659
  ///
kpeter@80
   660
  /// For the sake of convenience it also works as a usual
kpeter@80
   661
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
kpeter@80
   662
  /// and the \c Key and \c Value typedefs also exist.
kpeter@80
   663
  ///
kpeter@80
   664
  /// The simplest way of using this map is through the mapToFunctor()
kpeter@80
   665
  /// function.
kpeter@80
   666
  ///
kpeter@80
   667
  ///\sa FunctorToMap
kpeter@80
   668
  template <typename M>
kpeter@80
   669
  class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
   670
    const M &_m;
alpar@25
   671
  public:
alpar@25
   672
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
   673
    typedef typename Parent::Key Key;
alpar@25
   674
    typedef typename Parent::Value Value;
alpar@25
   675
kpeter@80
   676
    typedef typename Parent::Key argument_type;
kpeter@80
   677
    typedef typename Parent::Value result_type;
kpeter@80
   678
kpeter@80
   679
    /// Constructor
kpeter@80
   680
    MapToFunctor(const M &m) : _m(m) {}
kpeter@80
   681
    /// \e
kpeter@80
   682
    Value operator()(const Key &k) const { return _m[k]; }
kpeter@80
   683
    /// \e
kpeter@80
   684
    Value operator[](const Key &k) const { return _m[k]; }
alpar@25
   685
  };
kpeter@45
   686
kpeter@80
   687
  /// Returns a \ref MapToFunctor class
kpeter@80
   688
kpeter@80
   689
  /// This function just returns a \ref MapToFunctor class.
kpeter@80
   690
  /// \relates MapToFunctor
kpeter@45
   691
  template<typename M>
kpeter@80
   692
  inline MapToFunctor<M> mapToFunctor(const M &m) {
kpeter@80
   693
    return MapToFunctor<M>(m);
kpeter@45
   694
  }
alpar@25
   695
alpar@25
   696
kpeter@80
   697
  /// \brief Map adaptor to convert the \c Value type of a map to
kpeter@80
   698
  /// another type using the default conversion.
kpeter@80
   699
kpeter@80
   700
  /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
kpeter@80
   701
  /// "readable map" to another type using the default conversion.
kpeter@80
   702
  /// The \c Key type of it is inherited from \c M and the \c Value
kpeter@80
   703
  /// type is \c V.
kpeter@80
   704
  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
alpar@26
   705
  ///
kpeter@80
   706
  /// The simplest way of using this map is through the convertMap()
kpeter@80
   707
  /// function.
kpeter@80
   708
  template <typename M, typename V>
kpeter@80
   709
  class ConvertMap : public MapBase<typename M::Key, V> {
kpeter@80
   710
    const M &_m;
kpeter@80
   711
  public:
kpeter@80
   712
    typedef MapBase<typename M::Key, V> Parent;
kpeter@80
   713
    typedef typename Parent::Key Key;
kpeter@80
   714
    typedef typename Parent::Value Value;
kpeter@80
   715
kpeter@80
   716
    /// Constructor
kpeter@80
   717
kpeter@80
   718
    /// Constructor.
kpeter@80
   719
    /// \param m The underlying map.
kpeter@80
   720
    ConvertMap(const M &m) : _m(m) {}
kpeter@80
   721
kpeter@80
   722
    /// \e
kpeter@80
   723
    Value operator[](const Key &k) const { return _m[k]; }
kpeter@80
   724
  };
kpeter@80
   725
kpeter@80
   726
  /// Returns a \ref ConvertMap class
kpeter@80
   727
kpeter@80
   728
  /// This function just returns a \ref ConvertMap class.
kpeter@80
   729
  /// \relates ConvertMap
kpeter@80
   730
  template<typename V, typename M>
kpeter@80
   731
  inline ConvertMap<M, V> convertMap(const M &map) {
kpeter@80
   732
    return ConvertMap<M, V>(map);
kpeter@80
   733
  }
kpeter@80
   734
kpeter@80
   735
kpeter@80
   736
  /// Applies all map setting operations to two maps
kpeter@80
   737
kpeter@80
   738
  /// This map has two \ref concepts::WriteMap "writable map" parameters
kpeter@80
   739
  /// and each write request will be passed to both of them.
kpeter@80
   740
  /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
kpeter@80
   741
  /// operations will return the corresponding values of \c M1.
kpeter@29
   742
  ///
kpeter@80
   743
  /// The \c Key and \c Value types are inherited from \c M1.
kpeter@80
   744
  /// The \c Key and \c Value of \c M2 must be convertible from those
kpeter@80
   745
  /// of \c M1.
kpeter@80
   746
  ///
kpeter@80
   747
  /// The simplest way of using this map is through the forkMap()
kpeter@80
   748
  /// function.
kpeter@80
   749
  template<typename  M1, typename M2>
kpeter@80
   750
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   751
    M1 &_m1;
kpeter@80
   752
    M2 &_m2;
kpeter@80
   753
  public:
kpeter@80
   754
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
   755
    typedef typename Parent::Key Key;
kpeter@80
   756
    typedef typename Parent::Value Value;
alpar@25
   757
kpeter@80
   758
    /// Constructor
kpeter@80
   759
    ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   760
    /// Returns the value associated with the given key in the first map.
kpeter@80
   761
    Value operator[](const Key &k) const { return _m1[k]; }
kpeter@80
   762
    /// Sets the value associated with the given key in both maps.
kpeter@80
   763
    void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
kpeter@80
   764
  };
kpeter@80
   765
kpeter@80
   766
  /// Returns a \ref ForkMap class
kpeter@80
   767
kpeter@80
   768
  /// This function just returns a \ref ForkMap class.
kpeter@80
   769
  /// \relates ForkMap
kpeter@80
   770
  template <typename M1, typename M2>
kpeter@80
   771
  inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
kpeter@80
   772
    return ForkMap<M1,M2>(m1,m2);
kpeter@80
   773
  }
kpeter@80
   774
kpeter@80
   775
kpeter@80
   776
  /// Simple wrapping of a map
kpeter@80
   777
kpeter@80
   778
  /// This \ref concepts::ReadMap "read only map" returns the simple
kpeter@80
   779
  /// wrapping of the given map. Sometimes the reference maps cannot be
kpeter@80
   780
  /// combined with simple read maps. This map adaptor wraps the given
kpeter@80
   781
  /// map to simple read map.
kpeter@80
   782
  ///
kpeter@80
   783
  /// The simplest way of using this map is through the wrapMap()
kpeter@80
   784
  /// function.
kpeter@80
   785
  ///
kpeter@80
   786
  /// \sa WrapWriteMap
kpeter@80
   787
  template<typename M>
kpeter@80
   788
  class WrapMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
   789
    const M &_m;
alpar@25
   790
  public:
alpar@25
   791
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
   792
    typedef typename Parent::Key Key;
alpar@25
   793
    typedef typename Parent::Value Value;
alpar@25
   794
kpeter@80
   795
    /// Constructor
kpeter@80
   796
    WrapMap(const M &m) : _m(m) {}
kpeter@80
   797
    /// \e
kpeter@80
   798
    Value operator[](const Key &k) const { return _m[k]; }
alpar@25
   799
  };
alpar@25
   800
kpeter@80
   801
  /// Returns a \ref WrapMap class
kpeter@45
   802
kpeter@80
   803
  /// This function just returns a \ref WrapMap class.
kpeter@80
   804
  /// \relates WrapMap
kpeter@45
   805
  template<typename M>
kpeter@80
   806
  inline WrapMap<M> wrapMap(const M &map) {
kpeter@80
   807
    return WrapMap<M>(map);
kpeter@45
   808
  }
kpeter@45
   809
alpar@25
   810
kpeter@80
   811
  /// Simple writable wrapping of a map
kpeter@80
   812
kpeter@80
   813
  /// This \ref concepts::ReadWriteMap "read-write map" returns the simple
kpeter@80
   814
  /// wrapping of the given map. Sometimes the reference maps cannot be
kpeter@80
   815
  /// combined with simple read-write maps. This map adaptor wraps the
kpeter@80
   816
  /// given map to simple read-write map.
kpeter@80
   817
  ///
kpeter@80
   818
  /// The simplest way of using this map is through the wrapWriteMap()
kpeter@80
   819
  /// function.
kpeter@80
   820
  ///
kpeter@80
   821
  /// \sa WrapMap
kpeter@80
   822
  template<typename M>
kpeter@80
   823
  class WrapWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
   824
    M &_m;
kpeter@80
   825
  public:
kpeter@80
   826
    typedef MapBase<typename M::Key, typename M::Value> Parent;
kpeter@80
   827
    typedef typename Parent::Key Key;
kpeter@80
   828
    typedef typename Parent::Value Value;
kpeter@80
   829
kpeter@80
   830
    /// Constructor
kpeter@80
   831
    WrapWriteMap(M &m) : _m(m) {}
kpeter@80
   832
    /// \e
kpeter@80
   833
    Value operator[](const Key &k) const { return _m[k]; }
kpeter@80
   834
    /// \e
kpeter@80
   835
    void set(const Key &k, const Value &c) { _m.set(k, c); }
kpeter@80
   836
  };
kpeter@80
   837
kpeter@80
   838
  ///Returns a \ref WrapWriteMap class
kpeter@80
   839
kpeter@80
   840
  ///This function just returns a \ref WrapWriteMap class.
kpeter@80
   841
  ///\relates WrapWriteMap
kpeter@80
   842
  template<typename M>
kpeter@80
   843
  inline WrapWriteMap<M> wrapWriteMap(M &map) {
kpeter@80
   844
    return WrapWriteMap<M>(map);
kpeter@80
   845
  }
kpeter@80
   846
kpeter@80
   847
kpeter@80
   848
  /// Sum of two maps
kpeter@80
   849
kpeter@80
   850
  /// This \ref concepts::ReadMap "read only map" returns the sum
kpeter@80
   851
  /// of the values of the two given maps.
kpeter@80
   852
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   853
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   854
  /// \c M1.
kpeter@80
   855
  ///
kpeter@80
   856
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   857
  /// \code
kpeter@80
   858
  ///   AddMap<M1,M2> am(m1,m2);
kpeter@80
   859
  /// \endcode
kpeter@80
   860
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
kpeter@80
   861
  ///
kpeter@80
   862
  /// The simplest way of using this map is through the addMap()
kpeter@80
   863
  /// function.
kpeter@80
   864
  ///
kpeter@80
   865
  /// \sa SubMap, MulMap, DivMap
kpeter@80
   866
  /// \sa ShiftMap, ShiftWriteMap
kpeter@80
   867
  template<typename M1, typename M2>
alpar@25
   868
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   869
    const M1 &_m1;
kpeter@80
   870
    const M2 &_m2;
alpar@25
   871
  public:
alpar@25
   872
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
alpar@25
   873
    typedef typename Parent::Key Key;
alpar@25
   874
    typedef typename Parent::Value Value;
alpar@25
   875
kpeter@80
   876
    /// Constructor
kpeter@80
   877
    AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   878
    /// \e
kpeter@80
   879
    Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
alpar@25
   880
  };
alpar@25
   881
kpeter@80
   882
  /// Returns an \ref AddMap class
kpeter@80
   883
kpeter@80
   884
  /// This function just returns an \ref AddMap class.
alpar@25
   885
  ///
kpeter@80
   886
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   887
  /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   888
  /// <tt>m1[x]+m2[x]</tt>.
kpeter@80
   889
  ///
kpeter@80
   890
  /// \relates AddMap
kpeter@80
   891
  template<typename M1, typename M2>
kpeter@80
   892
  inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
alpar@25
   893
    return AddMap<M1, M2>(m1,m2);
alpar@25
   894
  }
alpar@25
   895
alpar@25
   896
kpeter@80
   897
  /// Difference of two maps
kpeter@80
   898
kpeter@80
   899
  /// This \ref concepts::ReadMap "read only map" returns the difference
kpeter@80
   900
  /// of the values of the two given maps.
kpeter@80
   901
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   902
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   903
  /// \c M1.
alpar@25
   904
  ///
kpeter@80
   905
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   906
  /// \code
kpeter@80
   907
  ///   SubMap<M1,M2> sm(m1,m2);
kpeter@80
   908
  /// \endcode
kpeter@80
   909
  /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
kpeter@29
   910
  ///
kpeter@80
   911
  /// The simplest way of using this map is through the subMap()
kpeter@80
   912
  /// function.
kpeter@80
   913
  ///
kpeter@80
   914
  /// \sa AddMap, MulMap, DivMap
kpeter@80
   915
  template<typename M1, typename M2>
kpeter@80
   916
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   917
    const M1 &_m1;
kpeter@80
   918
    const M2 &_m2;
kpeter@80
   919
  public:
kpeter@80
   920
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
   921
    typedef typename Parent::Key Key;
kpeter@80
   922
    typedef typename Parent::Value Value;
kpeter@80
   923
kpeter@80
   924
    /// Constructor
kpeter@80
   925
    SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   926
    /// \e
kpeter@80
   927
    Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
kpeter@80
   928
  };
kpeter@80
   929
kpeter@80
   930
  /// Returns a \ref SubMap class
kpeter@80
   931
kpeter@80
   932
  /// This function just returns a \ref SubMap class.
kpeter@80
   933
  ///
kpeter@80
   934
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   935
  /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   936
  /// <tt>m1[x]-m2[x]</tt>.
kpeter@80
   937
  ///
kpeter@80
   938
  /// \relates SubMap
kpeter@80
   939
  template<typename M1, typename M2>
kpeter@80
   940
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
kpeter@80
   941
    return SubMap<M1, M2>(m1,m2);
kpeter@80
   942
  }
kpeter@80
   943
kpeter@80
   944
kpeter@80
   945
  /// Product of two maps
kpeter@80
   946
kpeter@80
   947
  /// This \ref concepts::ReadMap "read only map" returns the product
kpeter@80
   948
  /// of the values of the two given maps.
kpeter@80
   949
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   950
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   951
  /// \c M1.
kpeter@80
   952
  ///
kpeter@80
   953
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   954
  /// \code
kpeter@80
   955
  ///   MulMap<M1,M2> mm(m1,m2);
kpeter@80
   956
  /// \endcode
kpeter@80
   957
  /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
kpeter@80
   958
  ///
kpeter@80
   959
  /// The simplest way of using this map is through the mulMap()
kpeter@80
   960
  /// function.
kpeter@80
   961
  ///
kpeter@80
   962
  /// \sa AddMap, SubMap, DivMap
kpeter@80
   963
  /// \sa ScaleMap, ScaleWriteMap
kpeter@80
   964
  template<typename M1, typename M2>
kpeter@80
   965
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   966
    const M1 &_m1;
kpeter@80
   967
    const M2 &_m2;
kpeter@80
   968
  public:
kpeter@80
   969
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
   970
    typedef typename Parent::Key Key;
kpeter@80
   971
    typedef typename Parent::Value Value;
kpeter@80
   972
kpeter@80
   973
    /// Constructor
kpeter@80
   974
    MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   975
    /// \e
kpeter@80
   976
    Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
kpeter@80
   977
  };
kpeter@80
   978
kpeter@80
   979
  /// Returns a \ref MulMap class
kpeter@80
   980
kpeter@80
   981
  /// This function just returns a \ref MulMap class.
kpeter@80
   982
  ///
kpeter@80
   983
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   984
  /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   985
  /// <tt>m1[x]*m2[x]</tt>.
kpeter@80
   986
  ///
kpeter@80
   987
  /// \relates MulMap
kpeter@80
   988
  template<typename M1, typename M2>
kpeter@80
   989
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
kpeter@80
   990
    return MulMap<M1, M2>(m1,m2);
kpeter@80
   991
  }
kpeter@80
   992
kpeter@80
   993
kpeter@80
   994
  /// Quotient of two maps
kpeter@80
   995
kpeter@80
   996
  /// This \ref concepts::ReadMap "read only map" returns the quotient
kpeter@80
   997
  /// of the values of the two given maps.
kpeter@80
   998
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   999
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
  1000
  /// \c M1.
kpeter@80
  1001
  ///
kpeter@80
  1002
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
  1003
  /// \code
kpeter@80
  1004
  ///   DivMap<M1,M2> dm(m1,m2);
kpeter@80
  1005
  /// \endcode
kpeter@80
  1006
  /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
kpeter@80
  1007
  ///
kpeter@80
  1008
  /// The simplest way of using this map is through the divMap()
kpeter@80
  1009
  /// function.
kpeter@80
  1010
  ///
kpeter@80
  1011
  /// \sa AddMap, SubMap, MulMap
kpeter@80
  1012
  template<typename M1, typename M2>
kpeter@80
  1013
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
  1014
    const M1 &_m1;
kpeter@80
  1015
    const M2 &_m2;
kpeter@80
  1016
  public:
kpeter@80
  1017
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
  1018
    typedef typename Parent::Key Key;
kpeter@80
  1019
    typedef typename Parent::Value Value;
kpeter@80
  1020
kpeter@80
  1021
    /// Constructor
kpeter@80
  1022
    DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
  1023
    /// \e
kpeter@80
  1024
    Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
kpeter@80
  1025
  };
kpeter@80
  1026
kpeter@80
  1027
  /// Returns a \ref DivMap class
kpeter@80
  1028
kpeter@80
  1029
  /// This function just returns a \ref DivMap class.
kpeter@80
  1030
  ///
kpeter@80
  1031
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
  1032
  /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
kpeter@80
  1033
  /// <tt>m1[x]/m2[x]</tt>.
kpeter@80
  1034
  ///
kpeter@80
  1035
  /// \relates DivMap
kpeter@80
  1036
  template<typename M1, typename M2>
kpeter@80
  1037
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
kpeter@80
  1038
    return DivMap<M1, M2>(m1,m2);
kpeter@80
  1039
  }
kpeter@80
  1040
kpeter@80
  1041
kpeter@80
  1042
  /// Shifts a map with a constant.
kpeter@80
  1043
kpeter@80
  1044
  /// This \ref concepts::ReadMap "read only map" returns the sum of
kpeter@80
  1045
  /// the given map and a constant value (i.e. it shifts the map with
kpeter@80
  1046
  /// the constant). Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1047
  ///
kpeter@80
  1048
  /// Actually,
kpeter@80
  1049
  /// \code
kpeter@80
  1050
  ///   ShiftMap<M> sh(m,v);
kpeter@80
  1051
  /// \endcode
kpeter@80
  1052
  /// is equivalent to
kpeter@80
  1053
  /// \code
kpeter@80
  1054
  ///   ConstMap<M::Key, M::Value> cm(v);
kpeter@80
  1055
  ///   AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
kpeter@80
  1056
  /// \endcode
kpeter@80
  1057
  ///
kpeter@80
  1058
  /// The simplest way of using this map is through the shiftMap()
kpeter@80
  1059
  /// function.
kpeter@80
  1060
  ///
kpeter@80
  1061
  /// \sa ShiftWriteMap
kpeter@80
  1062
  template<typename M, typename C = typename M::Value>
alpar@25
  1063
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1064
    const M &_m;
kpeter@80
  1065
    C _v;
alpar@25
  1066
  public:
alpar@25
  1067
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1068
    typedef typename Parent::Key Key;
alpar@25
  1069
    typedef typename Parent::Value Value;
alpar@25
  1070
kpeter@80
  1071
    /// Constructor
alpar@25
  1072
kpeter@80
  1073
    /// Constructor.
kpeter@80
  1074
    /// \param m The undelying map.
kpeter@80
  1075
    /// \param v The constant value.
kpeter@80
  1076
    ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
kpeter@80
  1077
    /// \e
kpeter@80
  1078
    Value operator[](const Key &k) const { return _m[k]+_v; }
alpar@25
  1079
  };
alpar@25
  1080
kpeter@80
  1081
  /// Shifts a map with a constant (read-write version).
alpar@25
  1082
kpeter@80
  1083
  /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
kpeter@80
  1084
  /// of the given map and a constant value (i.e. it shifts the map with
kpeter@80
  1085
  /// the constant). Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1086
  /// It makes also possible to write the map.
alpar@25
  1087
  ///
kpeter@80
  1088
  /// The simplest way of using this map is through the shiftWriteMap()
kpeter@80
  1089
  /// function.
kpeter@80
  1090
  ///
kpeter@80
  1091
  /// \sa ShiftMap
kpeter@80
  1092
  template<typename M, typename C = typename M::Value>
alpar@25
  1093
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1094
    M &_m;
kpeter@80
  1095
    C _v;
alpar@25
  1096
  public:
alpar@25
  1097
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1098
    typedef typename Parent::Key Key;
alpar@25
  1099
    typedef typename Parent::Value Value;
alpar@25
  1100
kpeter@80
  1101
    /// Constructor
alpar@25
  1102
kpeter@80
  1103
    /// Constructor.
kpeter@80
  1104
    /// \param m The undelying map.
kpeter@80
  1105
    /// \param v The constant value.
kpeter@80
  1106
    ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
alpar@25
  1107
    /// \e
kpeter@80
  1108
    Value operator[](const Key &k) const { return _m[k]+_v; }
alpar@25
  1109
    /// \e
kpeter@80
  1110
    void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
alpar@25
  1111
  };
alpar@25
  1112
kpeter@80
  1113
  /// Returns a \ref ShiftMap class
kpeter@80
  1114
kpeter@80
  1115
  /// This function just returns a \ref ShiftMap class.
kpeter@80
  1116
  ///
kpeter@80
  1117
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1118
  /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
kpeter@80
  1119
  /// <tt>m[x]+v</tt>.
kpeter@80
  1120
  ///
kpeter@80
  1121
  /// \relates ShiftMap
kpeter@80
  1122
  template<typename M, typename C>
kpeter@80
  1123
  inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
alpar@25
  1124
    return ShiftMap<M, C>(m,v);
alpar@25
  1125
  }
alpar@25
  1126
kpeter@80
  1127
  /// Returns a \ref ShiftWriteMap class
kpeter@29
  1128
kpeter@80
  1129
  /// This function just returns a \ref ShiftWriteMap class.
kpeter@80
  1130
  ///
kpeter@80
  1131
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1132
  /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
kpeter@80
  1133
  /// <tt>m[x]+v</tt>.
kpeter@80
  1134
  /// Moreover it makes also possible to write the map.
kpeter@80
  1135
  ///
kpeter@80
  1136
  /// \relates ShiftWriteMap
kpeter@80
  1137
  template<typename M, typename C>
kpeter@80
  1138
  inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
alpar@25
  1139
    return ShiftWriteMap<M, C>(m,v);
alpar@25
  1140
  }
alpar@25
  1141
alpar@25
  1142
kpeter@80
  1143
  /// Scales a map with a constant.
kpeter@80
  1144
kpeter@80
  1145
  /// This \ref concepts::ReadMap "read only map" returns the value of
kpeter@80
  1146
  /// the given map multiplied from the left side with a constant value.
kpeter@80
  1147
  /// Its \c Key and \c Value are inherited from \c M.
alpar@26
  1148
  ///
kpeter@80
  1149
  /// Actually,
kpeter@80
  1150
  /// \code
kpeter@80
  1151
  ///   ScaleMap<M> sc(m,v);
kpeter@80
  1152
  /// \endcode
kpeter@80
  1153
  /// is equivalent to
kpeter@80
  1154
  /// \code
kpeter@80
  1155
  ///   ConstMap<M::Key, M::Value> cm(v);
kpeter@80
  1156
  ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
kpeter@80
  1157
  /// \endcode
alpar@25
  1158
  ///
kpeter@80
  1159
  /// The simplest way of using this map is through the scaleMap()
kpeter@80
  1160
  /// function.
alpar@25
  1161
  ///
kpeter@80
  1162
  /// \sa ScaleWriteMap
kpeter@80
  1163
  template<typename M, typename C = typename M::Value>
alpar@25
  1164
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1165
    const M &_m;
kpeter@80
  1166
    C _v;
alpar@25
  1167
  public:
alpar@25
  1168
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1169
    typedef typename Parent::Key Key;
alpar@25
  1170
    typedef typename Parent::Value Value;
alpar@25
  1171
kpeter@80
  1172
    /// Constructor
alpar@25
  1173
kpeter@80
  1174
    /// Constructor.
kpeter@80
  1175
    /// \param m The undelying map.
kpeter@80
  1176
    /// \param v The constant value.
kpeter@80
  1177
    ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
alpar@25
  1178
    /// \e
kpeter@80
  1179
    Value operator[](const Key &k) const { return _v*_m[k]; }
alpar@25
  1180
  };
alpar@25
  1181
kpeter@80
  1182
  /// Scales a map with a constant (read-write version).
alpar@25
  1183
kpeter@80
  1184
  /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
kpeter@80
  1185
  /// the given map multiplied from the left side with a constant value.
kpeter@80
  1186
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1187
  /// It can also be used as write map if the \c / operator is defined
kpeter@80
  1188
  /// between \c Value and \c C and the given multiplier is not zero.
kpeter@29
  1189
  ///
kpeter@80
  1190
  /// The simplest way of using this map is through the scaleWriteMap()
kpeter@80
  1191
  /// function.
kpeter@80
  1192
  ///
kpeter@80
  1193
  /// \sa ScaleMap
kpeter@80
  1194
  template<typename M, typename C = typename M::Value>
alpar@25
  1195
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1196
    M &_m;
kpeter@80
  1197
    C _v;
alpar@25
  1198
  public:
alpar@25
  1199
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1200
    typedef typename Parent::Key Key;
alpar@25
  1201
    typedef typename Parent::Value Value;
alpar@25
  1202
kpeter@80
  1203
    /// Constructor
alpar@25
  1204
kpeter@80
  1205
    /// Constructor.
kpeter@80
  1206
    /// \param m The undelying map.
kpeter@80
  1207
    /// \param v The constant value.
kpeter@80
  1208
    ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
alpar@25
  1209
    /// \e
kpeter@80
  1210
    Value operator[](const Key &k) const { return _v*_m[k]; }
alpar@25
  1211
    /// \e
kpeter@80
  1212
    void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
alpar@25
  1213
  };
alpar@25
  1214
kpeter@80
  1215
  /// Returns a \ref ScaleMap class
kpeter@80
  1216
kpeter@80
  1217
  /// This function just returns a \ref ScaleMap class.
kpeter@80
  1218
  ///
kpeter@80
  1219
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1220
  /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
kpeter@80
  1221
  /// <tt>v*m[x]</tt>.
kpeter@80
  1222
  ///
kpeter@80
  1223
  /// \relates ScaleMap
kpeter@80
  1224
  template<typename M, typename C>
kpeter@80
  1225
  inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
alpar@25
  1226
    return ScaleMap<M, C>(m,v);
alpar@25
  1227
  }
alpar@25
  1228
kpeter@80
  1229
  /// Returns a \ref ScaleWriteMap class
kpeter@29
  1230
kpeter@80
  1231
  /// This function just returns a \ref ScaleWriteMap class.
kpeter@80
  1232
  ///
kpeter@80
  1233
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1234
  /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
kpeter@80
  1235
  /// <tt>v*m[x]</tt>.
kpeter@80
  1236
  /// Moreover it makes also possible to write the map.
kpeter@80
  1237
  ///
kpeter@80
  1238
  /// \relates ScaleWriteMap
kpeter@80
  1239
  template<typename M, typename C>
kpeter@80
  1240
  inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
alpar@25
  1241
    return ScaleWriteMap<M, C>(m,v);
alpar@25
  1242
  }
alpar@25
  1243
alpar@25
  1244
kpeter@80
  1245
  /// Negative of a map
alpar@25
  1246
kpeter@80
  1247
  /// This \ref concepts::ReadMap "read only map" returns the negative
kpeter@80
  1248
  /// of the values of the given map (using the unary \c - operator).
kpeter@80
  1249
  /// Its \c Key and \c Value are inherited from \c M.
alpar@25
  1250
  ///
kpeter@80
  1251
  /// If M::Value is \c int, \c double etc., then
kpeter@80
  1252
  /// \code
kpeter@80
  1253
  ///   NegMap<M> neg(m);
kpeter@80
  1254
  /// \endcode
kpeter@80
  1255
  /// is equivalent to
kpeter@80
  1256
  /// \code
kpeter@80
  1257
  ///   ScaleMap<M> neg(m,-1);
kpeter@80
  1258
  /// \endcode
kpeter@29
  1259
  ///
kpeter@80
  1260
  /// The simplest way of using this map is through the negMap()
kpeter@80
  1261
  /// function.
kpeter@29
  1262
  ///
kpeter@80
  1263
  /// \sa NegWriteMap
kpeter@80
  1264
  template<typename M>
alpar@25
  1265
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1266
    const M& _m;
alpar@25
  1267
  public:
alpar@25
  1268
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1269
    typedef typename Parent::Key Key;
alpar@25
  1270
    typedef typename Parent::Value Value;
alpar@25
  1271
kpeter@80
  1272
    /// Constructor
kpeter@80
  1273
    NegMap(const M &m) : _m(m) {}
alpar@25
  1274
    /// \e
kpeter@80
  1275
    Value operator[](const Key &k) const { return -_m[k]; }
alpar@25
  1276
  };
alpar@25
  1277
kpeter@80
  1278
  /// Negative of a map (read-write version)
kpeter@80
  1279
kpeter@80
  1280
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
kpeter@80
  1281
  /// negative of the values of the given map (using the unary \c -
kpeter@80
  1282
  /// operator).
kpeter@80
  1283
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1284
  /// It makes also possible to write the map.
kpeter@80
  1285
  ///
kpeter@80
  1286
  /// If M::Value is \c int, \c double etc., then
kpeter@80
  1287
  /// \code
kpeter@80
  1288
  ///   NegWriteMap<M> neg(m);
kpeter@80
  1289
  /// \endcode
kpeter@80
  1290
  /// is equivalent to
kpeter@80
  1291
  /// \code
kpeter@80
  1292
  ///   ScaleWriteMap<M> neg(m,-1);
kpeter@80
  1293
  /// \endcode
kpeter@80
  1294
  ///
kpeter@80
  1295
  /// The simplest way of using this map is through the negWriteMap()
kpeter@80
  1296
  /// function.
kpeter@29
  1297
  ///
kpeter@29
  1298
  /// \sa NegMap
kpeter@80
  1299
  template<typename M>
alpar@25
  1300
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1301
    M &_m;
alpar@25
  1302
  public:
alpar@25
  1303
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1304
    typedef typename Parent::Key Key;
alpar@25
  1305
    typedef typename Parent::Value Value;
alpar@25
  1306
kpeter@80
  1307
    /// Constructor
kpeter@80
  1308
    NegWriteMap(M &m) : _m(m) {}
alpar@25
  1309
    /// \e
kpeter@80
  1310
    Value operator[](const Key &k) const { return -_m[k]; }
alpar@25
  1311
    /// \e
kpeter@80
  1312
    void set(const Key &k, const Value &v) { _m.set(k, -v); }
alpar@25
  1313
  };
alpar@25
  1314
kpeter@80
  1315
  /// Returns a \ref NegMap class
alpar@25
  1316
kpeter@80
  1317
  /// This function just returns a \ref NegMap class.
kpeter@80
  1318
  ///
kpeter@80
  1319
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1320
  /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
kpeter@80
  1321
  ///
kpeter@80
  1322
  /// \relates NegMap
kpeter@80
  1323
  template <typename M>
alpar@25
  1324
  inline NegMap<M> negMap(const M &m) {
alpar@25
  1325
    return NegMap<M>(m);
alpar@25
  1326
  }
alpar@25
  1327
kpeter@80
  1328
  /// Returns a \ref NegWriteMap class
kpeter@29
  1329
kpeter@80
  1330
  /// This function just returns a \ref NegWriteMap class.
kpeter@80
  1331
  ///
kpeter@80
  1332
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1333
  /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
kpeter@80
  1334
  /// Moreover it makes also possible to write the map.
kpeter@80
  1335
  ///
kpeter@80
  1336
  /// \relates NegWriteMap
kpeter@80
  1337
  template <typename M>
kpeter@80
  1338
  inline NegWriteMap<M> negWriteMap(M &m) {
alpar@25
  1339
    return NegWriteMap<M>(m);
alpar@25
  1340
  }
alpar@25
  1341
alpar@25
  1342
kpeter@80
  1343
  /// Absolute value of a map
kpeter@80
  1344
kpeter@80
  1345
  /// This \ref concepts::ReadMap "read only map" returns the absolute
kpeter@80
  1346
  /// value of the values of the given map.
kpeter@80
  1347
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1348
  /// \c Value must be comparable to \c 0 and the unary \c -
kpeter@80
  1349
  /// operator must be defined for it, of course.
kpeter@80
  1350
  ///
kpeter@80
  1351
  /// The simplest way of using this map is through the absMap()
kpeter@80
  1352
  /// function.
kpeter@80
  1353
  template<typename M>
alpar@25
  1354
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1355
    const M &_m;
alpar@25
  1356
  public:
alpar@25
  1357
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1358
    typedef typename Parent::Key Key;
alpar@25
  1359
    typedef typename Parent::Value Value;
alpar@25
  1360
kpeter@80
  1361
    /// Constructor
kpeter@80
  1362
    AbsMap(const M &m) : _m(m) {}
alpar@25
  1363
    /// \e
kpeter@80
  1364
    Value operator[](const Key &k) const {
kpeter@80
  1365
      Value tmp = _m[k];
alpar@25
  1366
      return tmp >= 0 ? tmp : -tmp;
alpar@25
  1367
    }
alpar@25
  1368
alpar@25
  1369
  };
alpar@25
  1370
kpeter@80
  1371
  /// Returns an \ref AbsMap class
kpeter@80
  1372
kpeter@80
  1373
  /// This function just returns an \ref AbsMap class.
kpeter@80
  1374
  ///
kpeter@80
  1375
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1376
  /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
kpeter@80
  1377
  /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
kpeter@80
  1378
  /// negative.
kpeter@80
  1379
  ///
kpeter@80
  1380
  /// \relates AbsMap
kpeter@80
  1381
  template<typename M>
alpar@25
  1382
  inline AbsMap<M> absMap(const M &m) {
alpar@25
  1383
    return AbsMap<M>(m);
alpar@25
  1384
  }
alpar@25
  1385
alpar@25
  1386
kpeter@80
  1387
  /// Logical 'not' of a map
kpeter@80
  1388
kpeter@80
  1389
  /// This \ref concepts::ReadMap "read only map" returns the logical
kpeter@80
  1390
  /// negation of the values of the given map.
kpeter@80
  1391
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
alpar@25
  1392
  ///
kpeter@80
  1393
  /// The simplest way of using this map is through the notMap()
kpeter@80
  1394
  /// function.
alpar@25
  1395
  ///
kpeter@80
  1396
  /// \sa NotWriteMap
kpeter@80
  1397
  template <typename M>
alpar@25
  1398
  class NotMap : public MapBase<typename M::Key, bool> {
kpeter@80
  1399
    const M &_m;
alpar@25
  1400
  public:
alpar@25
  1401
    typedef MapBase<typename M::Key, bool> Parent;
alpar@25
  1402
    typedef typename Parent::Key Key;
alpar@25
  1403
    typedef typename Parent::Value Value;
alpar@25
  1404
alpar@25
  1405
    /// Constructor
kpeter@80
  1406
    NotMap(const M &m) : _m(m) {}
kpeter@80
  1407
    /// \e
kpeter@80
  1408
    Value operator[](const Key &k) const { return !_m[k]; }
alpar@25
  1409
  };
alpar@25
  1410
kpeter@80
  1411
  /// Logical 'not' of a map (read-write version)
kpeter@80
  1412
kpeter@80
  1413
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
kpeter@80
  1414
  /// logical negation of the values of the given map.
kpeter@80
  1415
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
kpeter@80
  1416
  /// It makes also possible to write the map. When a value is set,
kpeter@80
  1417
  /// the opposite value is set to the original map.
kpeter@29
  1418
  ///
kpeter@80
  1419
  /// The simplest way of using this map is through the notWriteMap()
kpeter@80
  1420
  /// function.
kpeter@80
  1421
  ///
kpeter@80
  1422
  /// \sa NotMap
kpeter@80
  1423
  template <typename M>
alpar@25
  1424
  class NotWriteMap : public MapBase<typename M::Key, bool> {
kpeter@80
  1425
    M &_m;
alpar@25
  1426
  public:
alpar@25
  1427
    typedef MapBase<typename M::Key, bool> Parent;
alpar@25
  1428
    typedef typename Parent::Key Key;
alpar@25
  1429
    typedef typename Parent::Value Value;
alpar@25
  1430
alpar@25
  1431
    /// Constructor
kpeter@80
  1432
    NotWriteMap(M &m) : _m(m) {}
kpeter@80
  1433
    /// \e
kpeter@80
  1434
    Value operator[](const Key &k) const { return !_m[k]; }
kpeter@80
  1435
    /// \e
kpeter@80
  1436
    void set(const Key &k, bool v) { _m.set(k, !v); }
alpar@25
  1437
  };
kpeter@80
  1438
kpeter@80
  1439
  /// Returns a \ref NotMap class
kpeter@80
  1440
kpeter@80
  1441
  /// This function just returns a \ref NotMap class.
kpeter@80
  1442
  ///
kpeter@80
  1443
  /// For example, if \c m is a map with \c bool values, then
kpeter@80
  1444
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
kpeter@80
  1445
  ///
kpeter@80
  1446
  /// \relates NotMap
kpeter@80
  1447
  template <typename M>
alpar@25
  1448
  inline NotMap<M> notMap(const M &m) {
alpar@25
  1449
    return NotMap<M>(m);
alpar@25
  1450
  }
kpeter@80
  1451
kpeter@80
  1452
  /// Returns a \ref NotWriteMap class
kpeter@80
  1453
kpeter@80
  1454
  /// This function just returns a \ref NotWriteMap class.
kpeter@80
  1455
  ///
kpeter@80
  1456
  /// For example, if \c m is a map with \c bool values, then
kpeter@80
  1457
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
kpeter@80
  1458
  /// Moreover it makes also possible to write the map.
kpeter@80
  1459
  ///
kpeter@80
  1460
  /// \relates NotWriteMap
kpeter@80
  1461
  template <typename M>
kpeter@80
  1462
  inline NotWriteMap<M> notWriteMap(M &m) {
alpar@25
  1463
    return NotWriteMap<M>(m);
alpar@25
  1464
  }
alpar@25
  1465
kpeter@80
  1466
alpar@25
  1467
  namespace _maps_bits {
alpar@25
  1468
alpar@25
  1469
    template <typename Value>
alpar@25
  1470
    struct Identity {
alpar@25
  1471
      typedef Value argument_type;
alpar@25
  1472
      typedef Value result_type;
alpar@25
  1473
      Value operator()(const Value& val) const {
alpar@25
  1474
	return val;
alpar@25
  1475
      }
alpar@25
  1476
    };
alpar@25
  1477
alpar@25
  1478
    template <typename _Iterator, typename Enable = void>
alpar@25
  1479
    struct IteratorTraits {
alpar@25
  1480
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
alpar@25
  1481
    };
alpar@25
  1482
alpar@25
  1483
    template <typename _Iterator>
alpar@25
  1484
    struct IteratorTraits<_Iterator,
kpeter@80
  1485
      typename exists<typename _Iterator::container_type>::type>
alpar@25
  1486
    {
alpar@25
  1487
      typedef typename _Iterator::container_type::value_type Value;
alpar@25
  1488
    };
alpar@25
  1489
alpar@25
  1490
  }
kpeter@80
  1491
alpar@25
  1492
kpeter@29
  1493
  /// \brief Writable bool map for logging each \c true assigned element
alpar@25
  1494
  ///
kpeter@80
  1495
  /// A \ref concepts::ReadWriteMap "read-write" bool map for logging
kpeter@80
  1496
  /// each \c true assigned element, i.e it copies all the keys set
kpeter@46
  1497
  /// to \c true to the given iterator.
alpar@25
  1498
  ///
kpeter@80
  1499
  /// \note The container of the iterator should contain space
alpar@25
  1500
  /// for each element.
alpar@25
  1501
  ///
kpeter@80
  1502
  /// The following example shows how you can write the edges found by
kpeter@47
  1503
  /// the \ref Prim algorithm directly to the standard output.
kpeter@80
  1504
  /// \code
kpeter@80
  1505
  ///   typedef IdMap<Graph, Edge> EdgeIdMap;
kpeter@80
  1506
  ///   EdgeIdMap edgeId(graph);
alpar@25
  1507
  ///
kpeter@80
  1508
  ///   typedef MapToFunctor<EdgeIdMap> EdgeIdFunctor;
kpeter@80
  1509
  ///   EdgeIdFunctor edgeIdFunctor(edgeId);
alpar@25
  1510
  ///
kpeter@80
  1511
  ///   StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor>
kpeter@80
  1512
  ///     writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor);
alpar@25
  1513
  ///
kpeter@80
  1514
  ///   prim(graph, cost, writerMap);
kpeter@80
  1515
  /// \endcode
alpar@26
  1516
  ///
kpeter@80
  1517
  /// \sa BackInserterBoolMap
kpeter@80
  1518
  /// \sa FrontInserterBoolMap
kpeter@80
  1519
  /// \sa InserterBoolMap
kpeter@29
  1520
  ///
kpeter@80
  1521
  /// \todo Revise the name of this class and the related ones.
kpeter@80
  1522
  template <typename _Iterator,
alpar@25
  1523
            typename _Functor =
alpar@25
  1524
            _maps_bits::Identity<typename _maps_bits::
alpar@25
  1525
                                 IteratorTraits<_Iterator>::Value> >
alpar@25
  1526
  class StoreBoolMap {
alpar@25
  1527
  public:
alpar@25
  1528
    typedef _Iterator Iterator;
alpar@25
  1529
alpar@25
  1530
    typedef typename _Functor::argument_type Key;
alpar@25
  1531
    typedef bool Value;
alpar@25
  1532
alpar@25
  1533
    typedef _Functor Functor;
alpar@25
  1534
alpar@25
  1535
    /// Constructor
kpeter@80
  1536
    StoreBoolMap(Iterator it, const Functor& functor = Functor())
alpar@25
  1537
      : _begin(it), _end(it), _functor(functor) {}
alpar@25
  1538
alpar@26
  1539
    /// Gives back the given iterator set for the first key
alpar@25
  1540
    Iterator begin() const {
alpar@25
  1541
      return _begin;
alpar@25
  1542
    }
kpeter@80
  1543
alpar@26
  1544
    /// Gives back the the 'after the last' iterator
alpar@25
  1545
    Iterator end() const {
alpar@25
  1546
      return _end;
alpar@25
  1547
    }
alpar@25
  1548
kpeter@80
  1549
    /// The set function of the map
alpar@25
  1550
    void set(const Key& key, Value value) const {
alpar@25
  1551
      if (value) {
alpar@25
  1552
	*_end++ = _functor(key);
alpar@25
  1553
      }
alpar@25
  1554
    }
kpeter@80
  1555
alpar@25
  1556
  private:
alpar@25
  1557
    Iterator _begin;
alpar@25
  1558
    mutable Iterator _end;
alpar@25
  1559
    Functor _functor;
alpar@25
  1560
  };
alpar@25
  1561
kpeter@80
  1562
  /// \brief Writable bool map for logging each \c true assigned element in
kpeter@29
  1563
  /// a back insertable container.
alpar@25
  1564
  ///
kpeter@29
  1565
  /// Writable bool map for logging each \c true assigned element by pushing
kpeter@29
  1566
  /// them into a back insertable container.
alpar@26
  1567
  /// It can be used to retrieve the items into a standard
alpar@26
  1568
  /// container. The next example shows how you can store the
alpar@26
  1569
  /// edges found by the Prim algorithm in a vector.
alpar@25
  1570
  ///
kpeter@80
  1571
  /// \code
kpeter@80
  1572
  ///   vector<Edge> span_tree_edges;
kpeter@80
  1573
  ///   BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges);
kpeter@80
  1574
  ///   prim(graph, cost, inserter_map);
kpeter@80
  1575
  /// \endcode
kpeter@29
  1576
  ///
kpeter@80
  1577
  /// \sa StoreBoolMap
kpeter@80
  1578
  /// \sa FrontInserterBoolMap
kpeter@80
  1579
  /// \sa InserterBoolMap
alpar@25
  1580
  template <typename Container,
alpar@25
  1581
            typename Functor =
alpar@25
  1582
            _maps_bits::Identity<typename Container::value_type> >
alpar@25
  1583
  class BackInserterBoolMap {
alpar@25
  1584
  public:
kpeter@34
  1585
    typedef typename Functor::argument_type Key;
alpar@25
  1586
    typedef bool Value;
alpar@25
  1587
alpar@25
  1588
    /// Constructor
kpeter@80
  1589
    BackInserterBoolMap(Container& _container,
kpeter@80
  1590
                        const Functor& _functor = Functor())
alpar@25
  1591
      : container(_container), functor(_functor) {}
alpar@25
  1592
kpeter@80
  1593
    /// The set function of the map
alpar@25
  1594
    void set(const Key& key, Value value) {
alpar@25
  1595
      if (value) {
alpar@25
  1596
	container.push_back(functor(key));
alpar@25
  1597
      }
alpar@25
  1598
    }
kpeter@80
  1599
alpar@25
  1600
  private:
alpar@25
  1601
    Container& container;
alpar@25
  1602
    Functor functor;
alpar@25
  1603
  };
alpar@25
  1604
kpeter@80
  1605
  /// \brief Writable bool map for logging each \c true assigned element in
alpar@25
  1606
  /// a front insertable container.
alpar@25
  1607
  ///
kpeter@29
  1608
  /// Writable bool map for logging each \c true assigned element by pushing
kpeter@29
  1609
  /// them into a front insertable container.
kpeter@29
  1610
  /// It can be used to retrieve the items into a standard
kpeter@29
  1611
  /// container. For example see \ref BackInserterBoolMap.
kpeter@29
  1612
  ///
kpeter@80
  1613
  /// \sa BackInserterBoolMap
kpeter@80
  1614
  /// \sa InserterBoolMap
alpar@25
  1615
  template <typename Container,
alpar@25
  1616
            typename Functor =
alpar@25
  1617
            _maps_bits::Identity<typename Container::value_type> >
alpar@25
  1618
  class FrontInserterBoolMap {
alpar@25
  1619
  public:
kpeter@34
  1620
    typedef typename Functor::argument_type Key;
alpar@25
  1621
    typedef bool Value;
alpar@25
  1622
alpar@25
  1623
    /// Constructor
alpar@25
  1624
    FrontInserterBoolMap(Container& _container,
kpeter@80
  1625
                         const Functor& _functor = Functor())
alpar@25
  1626
      : container(_container), functor(_functor) {}
alpar@25
  1627
kpeter@80
  1628
    /// The set function of the map
alpar@25
  1629
    void set(const Key& key, Value value) {
alpar@25
  1630
      if (value) {
kpeter@30
  1631
	container.push_front(functor(key));
alpar@25
  1632
      }
alpar@25
  1633
    }
kpeter@80
  1634
alpar@25
  1635
  private:
kpeter@80
  1636
    Container& container;
alpar@25
  1637
    Functor functor;
alpar@25
  1638
  };
alpar@25
  1639
kpeter@80
  1640
  /// \brief Writable bool map for storing each \c true assigned element in
alpar@25
  1641
  /// an insertable container.
alpar@25
  1642
  ///
kpeter@80
  1643
  /// Writable bool map for storing each \c true assigned element in an
alpar@25
  1644
  /// insertable container. It will insert all the keys set to \c true into
alpar@26
  1645
  /// the container.
alpar@26
  1646
  ///
alpar@26
  1647
  /// For example, if you want to store the cut arcs of the strongly
alpar@25
  1648
  /// connected components in a set you can use the next code:
alpar@25
  1649
  ///
kpeter@80
  1650
  /// \code
kpeter@80
  1651
  ///   set<Arc> cut_arcs;
kpeter@80
  1652
  ///   InserterBoolMap<set<Arc> > inserter_map(cut_arcs);
kpeter@80
  1653
  ///   stronglyConnectedCutArcs(digraph, cost, inserter_map);
kpeter@80
  1654
  /// \endcode
kpeter@29
  1655
  ///
kpeter@80
  1656
  /// \sa BackInserterBoolMap
kpeter@80
  1657
  /// \sa FrontInserterBoolMap
alpar@25
  1658
  template <typename Container,
alpar@25
  1659
            typename Functor =
alpar@25
  1660
            _maps_bits::Identity<typename Container::value_type> >
alpar@25
  1661
  class InserterBoolMap {
alpar@25
  1662
  public:
alpar@25
  1663
    typedef typename Container::value_type Key;
alpar@25
  1664
    typedef bool Value;
alpar@25
  1665
kpeter@29
  1666
    /// Constructor with specified iterator
kpeter@80
  1667
kpeter@29
  1668
    /// Constructor with specified iterator.
kpeter@29
  1669
    /// \param _container The container for storing the elements.
kpeter@29
  1670
    /// \param _it The elements will be inserted before this iterator.
kpeter@29
  1671
    /// \param _functor The functor that is used when an element is stored.
alpar@25
  1672
    InserterBoolMap(Container& _container, typename Container::iterator _it,
kpeter@80
  1673
                    const Functor& _functor = Functor())
alpar@25
  1674
      : container(_container), it(_it), functor(_functor) {}
alpar@25
  1675
alpar@25
  1676
    /// Constructor
kpeter@29
  1677
kpeter@29
  1678
    /// Constructor without specified iterator.
kpeter@29
  1679
    /// The elements will be inserted before <tt>_container.end()</tt>.
kpeter@29
  1680
    /// \param _container The container for storing the elements.
kpeter@29
  1681
    /// \param _functor The functor that is used when an element is stored.
alpar@25
  1682
    InserterBoolMap(Container& _container, const Functor& _functor = Functor())
alpar@25
  1683
      : container(_container), it(_container.end()), functor(_functor) {}
alpar@25
  1684
kpeter@80
  1685
    /// The set function of the map
alpar@25
  1686
    void set(const Key& key, Value value) {
alpar@25
  1687
      if (value) {
kpeter@30
  1688
	it = container.insert(it, functor(key));
alpar@25
  1689
        ++it;
alpar@25
  1690
      }
alpar@25
  1691
    }
kpeter@80
  1692
alpar@25
  1693
  private:
alpar@25
  1694
    Container& container;
alpar@25
  1695
    typename Container::iterator it;
alpar@25
  1696
    Functor functor;
alpar@25
  1697
  };
alpar@25
  1698
kpeter@80
  1699
  /// \brief Writable bool map for filling each \c true assigned element with a
kpeter@29
  1700
  /// given value.
alpar@25
  1701
  ///
kpeter@80
  1702
  /// Writable bool map for filling each \c true assigned element with a
kpeter@29
  1703
  /// given value. The value can set the container.
alpar@25
  1704
  ///
alpar@26
  1705
  /// The following code finds the connected components of a graph
alpar@25
  1706
  /// and stores it in the \c comp map:
kpeter@80
  1707
  /// \code
kpeter@80
  1708
  ///   typedef Graph::NodeMap<int> ComponentMap;
kpeter@80
  1709
  ///   ComponentMap comp(graph);
kpeter@80
  1710
  ///   typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap;
kpeter@80
  1711
  ///   ComponentFillerMap filler(comp, 0);
alpar@25
  1712
  ///
kpeter@80
  1713
  ///   Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph);
kpeter@80
  1714
  ///   dfs.processedMap(filler);
kpeter@80
  1715
  ///   dfs.init();
kpeter@80
  1716
  ///   for (NodeIt it(graph); it != INVALID; ++it) {
kpeter@80
  1717
  ///     if (!dfs.reached(it)) {
kpeter@80
  1718
  ///       dfs.addSource(it);
kpeter@80
  1719
  ///       dfs.start();
kpeter@80
  1720
  ///       ++filler.fillValue();
kpeter@80
  1721
  ///     }
alpar@25
  1722
  ///   }
kpeter@80
  1723
  /// \endcode
alpar@25
  1724
  template <typename Map>
alpar@25
  1725
  class FillBoolMap {
alpar@25
  1726
  public:
alpar@25
  1727
    typedef typename Map::Key Key;
alpar@25
  1728
    typedef bool Value;
alpar@25
  1729
alpar@25
  1730
    /// Constructor
kpeter@80
  1731
    FillBoolMap(Map& _map, const typename Map::Value& _fill)
alpar@25
  1732
      : map(_map), fill(_fill) {}
alpar@25
  1733
alpar@25
  1734
    /// Constructor
kpeter@80
  1735
    FillBoolMap(Map& _map)
alpar@25
  1736
      : map(_map), fill() {}
alpar@25
  1737
alpar@25
  1738
    /// Gives back the current fill value
alpar@25
  1739
    const typename Map::Value& fillValue() const {
alpar@25
  1740
      return fill;
kpeter@80
  1741
    }
alpar@25
  1742
alpar@25
  1743
    /// Gives back the current fill value
alpar@25
  1744
    typename Map::Value& fillValue() {
alpar@25
  1745
      return fill;
kpeter@80
  1746
    }
alpar@25
  1747
alpar@25
  1748
    /// Sets the current fill value
alpar@25
  1749
    void fillValue(const typename Map::Value& _fill) {
alpar@25
  1750
      fill = _fill;
kpeter@80
  1751
    }
alpar@25
  1752
kpeter@80
  1753
    /// The set function of the map
alpar@25
  1754
    void set(const Key& key, Value value) {
alpar@25
  1755
      if (value) {
alpar@25
  1756
	map.set(key, fill);
alpar@25
  1757
      }
alpar@25
  1758
    }
kpeter@80
  1759
alpar@25
  1760
  private:
alpar@25
  1761
    Map& map;
alpar@25
  1762
    typename Map::Value fill;
alpar@25
  1763
  };
alpar@25
  1764
alpar@25
  1765
kpeter@80
  1766
  /// \brief Writable bool map for storing the sequence number of
kpeter@80
  1767
  /// \c true assignments.
kpeter@80
  1768
  ///
kpeter@80
  1769
  /// Writable bool map that stores for each \c true assigned elements
alpar@26
  1770
  /// the sequence number of this setting.
alpar@26
  1771
  /// It makes it easy to calculate the leaving
kpeter@80
  1772
  /// order of the nodes in the \ref Dfs algorithm.
alpar@25
  1773
  ///
kpeter@80
  1774
  /// \code
kpeter@80
  1775
  ///   typedef Digraph::NodeMap<int> OrderMap;
kpeter@80
  1776
  ///   OrderMap order(digraph);
kpeter@80
  1777
  ///   typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
kpeter@80
  1778
  ///   OrderSetterMap setter(order);
kpeter@80
  1779
  ///   Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph);
kpeter@80
  1780
  ///   dfs.processedMap(setter);
kpeter@80
  1781
  ///   dfs.init();
kpeter@80
  1782
  ///   for (NodeIt it(digraph); it != INVALID; ++it) {
kpeter@80
  1783
  ///     if (!dfs.reached(it)) {
kpeter@80
  1784
  ///       dfs.addSource(it);
kpeter@80
  1785
  ///       dfs.start();
kpeter@80
  1786
  ///     }
alpar@25
  1787
  ///   }
kpeter@80
  1788
  /// \endcode
alpar@25
  1789
  ///
alpar@26
  1790
  /// The storing of the discovering order is more difficult because the
alpar@25
  1791
  /// ReachedMap should be readable in the dfs algorithm but the setting
alpar@26
  1792
  /// order map is not readable. Thus we must use the fork map:
alpar@25
  1793
  ///
kpeter@80
  1794
  /// \code
kpeter@80
  1795
  ///   typedef Digraph::NodeMap<int> OrderMap;
kpeter@80
  1796
  ///   OrderMap order(digraph);
kpeter@80
  1797
  ///   typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
kpeter@80
  1798
  ///   OrderSetterMap setter(order);
kpeter@80
  1799
  ///   typedef Digraph::NodeMap<bool> StoreMap;
kpeter@80
  1800
  ///   StoreMap store(digraph);
alpar@25
  1801
  ///
kpeter@80
  1802
  ///   typedef ForkMap<StoreMap, OrderSetterMap> ReachedMap;
kpeter@80
  1803
  ///   ReachedMap reached(store, setter);
alpar@25
  1804
  ///
kpeter@80
  1805
  ///   Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph);
kpeter@80
  1806
  ///   dfs.reachedMap(reached);
kpeter@80
  1807
  ///   dfs.init();
kpeter@80
  1808
  ///   for (NodeIt it(digraph); it != INVALID; ++it) {
kpeter@80
  1809
  ///     if (!dfs.reached(it)) {
kpeter@80
  1810
  ///       dfs.addSource(it);
kpeter@80
  1811
  ///       dfs.start();
kpeter@80
  1812
  ///     }
alpar@25
  1813
  ///   }
kpeter@80
  1814
  /// \endcode
alpar@25
  1815
  template <typename Map>
alpar@25
  1816
  class SettingOrderBoolMap {
alpar@25
  1817
  public:
alpar@25
  1818
    typedef typename Map::Key Key;
alpar@25
  1819
    typedef bool Value;
alpar@25
  1820
alpar@25
  1821
    /// Constructor
kpeter@80
  1822
    SettingOrderBoolMap(Map& _map)
alpar@25
  1823
      : map(_map), counter(0) {}
alpar@25
  1824
alpar@25
  1825
    /// Number of set operations.
alpar@25
  1826
    int num() const {
alpar@25
  1827
      return counter;
alpar@25
  1828
    }
alpar@25
  1829
kpeter@80
  1830
    /// The set function of the map
alpar@25
  1831
    void set(const Key& key, Value value) {
alpar@25
  1832
      if (value) {
alpar@25
  1833
	map.set(key, counter++);
alpar@25
  1834
      }
alpar@25
  1835
    }
kpeter@80
  1836
alpar@25
  1837
  private:
alpar@25
  1838
    Map& map;
alpar@25
  1839
    int counter;
alpar@25
  1840
  };
alpar@25
  1841
alpar@25
  1842
  /// @}
alpar@25
  1843
}
alpar@25
  1844
alpar@25
  1845
#endif // LEMON_MAPS_H