lemon/maps.h
author deba
Tue, 09 Oct 2007 15:46:12 +0000
changeset 2489 48dddc283cfc
parent 2423 02fedd6652c6
child 2511 a99186a9b6b0
permissions -rw-r--r--
Bug fix and redesign StdMap
Improving map adaptors documentations
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@921
    19
#ifndef LEMON_MAPS_H
alpar@921
    20
#define LEMON_MAPS_H
klao@286
    21
deba@1778
    22
#include <iterator>
deba@2091
    23
#include <functional>
deba@1778
    24
deba@1993
    25
#include <lemon/bits/utility.h>
deba@1993
    26
#include <lemon/bits/traits.h>
alpar@1041
    27
klao@286
    28
///\file
alpar@1041
    29
///\ingroup maps
klao@286
    30
///\brief Miscellaneous property maps
klao@286
    31
///
klao@286
    32
#include <map>
klao@286
    33
alpar@921
    34
namespace lemon {
klao@286
    35
alpar@1041
    36
  /// \addtogroup maps
alpar@1041
    37
  /// @{
alpar@1041
    38
alpar@720
    39
  /// Base class of maps.
alpar@720
    40
alpar@805
    41
  /// Base class of maps.
alpar@805
    42
  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
deba@1705
    43
  template<typename K, typename T>
deba@1675
    44
  class MapBase {
alpar@720
    45
  public:
alpar@911
    46
    ///\e
alpar@987
    47
    typedef K Key;
alpar@911
    48
    ///\e
alpar@987
    49
    typedef T Value;
alpar@720
    50
  };
alpar@720
    51
alpar@805
    52
  /// Null map. (a.k.a. DoNothingMap)
klao@286
    53
klao@286
    54
  /// If you have to provide a map only for its type definitions,
alpar@805
    55
  /// or if you have to provide a writable map, but
alpar@805
    56
  /// data written to it will sent to <tt>/dev/null</tt>...
deba@1705
    57
  template<typename K, typename T>
deba@1705
    58
  class NullMap : public MapBase<K, T> {
klao@286
    59
  public:
deba@1705
    60
    typedef MapBase<K, T> Parent;
deba@1675
    61
    typedef typename Parent::Key Key;
deba@1675
    62
    typedef typename Parent::Value Value;
deba@1420
    63
    
alpar@805
    64
    /// Gives back a default constructed element.
klao@286
    65
    T operator[](const K&) const { return T(); }
alpar@805
    66
    /// Absorbs the value.
klao@286
    67
    void set(const K&, const T&) {}
klao@286
    68
  };
klao@286
    69
deba@1420
    70
  template <typename K, typename V> 
deba@1705
    71
  NullMap<K, V> nullMap() {
deba@1705
    72
    return NullMap<K, V>();
deba@1420
    73
  }
deba@1420
    74
klao@286
    75
klao@286
    76
  /// Constant map.
klao@286
    77
alpar@805
    78
  /// This is a readable map which assigns a specified value to each key.
deba@2489
    79
  /// In other aspects it is equivalent to the \c NullMap.
deba@1705
    80
  template<typename K, typename T>
deba@1705
    81
  class ConstMap : public MapBase<K, T> {
deba@1675
    82
  private:
klao@286
    83
    T v;
klao@286
    84
  public:
klao@286
    85
deba@1705
    86
    typedef MapBase<K, T> Parent;
deba@1675
    87
    typedef typename Parent::Key Key;
deba@1675
    88
    typedef typename Parent::Value Value;
deba@1420
    89
alpar@805
    90
    /// Default constructor
alpar@805
    91
alpar@805
    92
    /// The value of the map will be uninitialized. 
alpar@805
    93
    /// (More exactly it will be default constructed.)
klao@286
    94
    ConstMap() {}
alpar@911
    95
    ///\e
alpar@805
    96
alpar@805
    97
    /// \param _v The initial value of the map.
alpar@911
    98
    ///
klao@286
    99
    ConstMap(const T &_v) : v(_v) {}
deba@2489
   100
    
deba@2489
   101
    ///\e
deba@2489
   102
    T operator[](const K&) const { return v; }
klao@286
   103
deba@2489
   104
    ///\e
deba@2489
   105
    void setAll(const T &t) {
deba@2489
   106
      v = t;
deba@2489
   107
    }    
klao@286
   108
klao@286
   109
    template<typename T1>
klao@286
   110
    struct rebind {
deba@1675
   111
      typedef ConstMap<K, T1> other;
klao@286
   112
    };
klao@286
   113
klao@286
   114
    template<typename T1>
deba@1675
   115
    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
klao@286
   116
  };
klao@286
   117
deba@2489
   118
  ///Returns a \c ConstMap class
alpar@1076
   119
deba@2489
   120
  ///This function just returns a \c ConstMap class.
alpar@1076
   121
  ///\relates ConstMap
deba@1675
   122
  template<typename K, typename V> 
deba@1705
   123
  inline ConstMap<K, V> constMap(const V &v) {
deba@1705
   124
    return ConstMap<K, V>(v);
alpar@1076
   125
  }
alpar@1076
   126
alpar@1076
   127
marci@890
   128
  template<typename T, T v>
marci@890
   129
  struct Const { };
deba@1675
   130
deba@2489
   131
  /// Constant map with inlined constant value.
deba@2489
   132
deba@2489
   133
  /// This is a readable map which assigns a specified value to each key.
deba@2489
   134
  /// In other aspects it is equivalent to the \c NullMap.
deba@1705
   135
  template<typename K, typename V, V v>
deba@1705
   136
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
marci@890
   137
  public:
deba@1705
   138
    typedef MapBase<K, V> Parent;
deba@1675
   139
    typedef typename Parent::Key Key;
deba@1675
   140
    typedef typename Parent::Value Value;
deba@1675
   141
marci@890
   142
    ConstMap() { }
deba@2489
   143
    ///\e
marci@890
   144
    V operator[](const K&) const { return v; }
deba@2489
   145
    ///\e
marci@890
   146
    void set(const K&, const V&) { }
marci@890
   147
  };
klao@286
   148
deba@2489
   149
  ///Returns a \c ConstMap class
deba@1675
   150
deba@2489
   151
  ///This function just returns a \c ConstMap class with inlined value.
deba@1675
   152
  ///\relates ConstMap
deba@1675
   153
  template<typename K, typename V, V v> 
deba@1705
   154
  inline ConstMap<K, Const<V, v> > constMap() {
deba@1705
   155
    return ConstMap<K, Const<V, v> >();
deba@1675
   156
  }
deba@1675
   157
deba@2489
   158
  ///Map based on std::map
klao@286
   159
deba@2489
   160
  ///This is essentially a wrapper for \c std::map. With addition that
deba@2489
   161
  ///you can specify a default value different from \c Value() .
alpar@987
   162
  template <typename K, typename T, typename Compare = std::less<K> >
deba@2489
   163
  class StdMap {
deba@2489
   164
    template <typename K1, typename T1, typename C1>
deba@2489
   165
    friend class StdMap;
deba@2489
   166
  public:
klao@286
   167
deba@2489
   168
    typedef True ReferenceMapTag;
alpar@1456
   169
    ///\e
alpar@987
   170
    typedef K Key;
alpar@1456
   171
    ///\e
alpar@987
   172
    typedef T Value;
alpar@1456
   173
    ///\e
alpar@987
   174
    typedef T& Reference;
alpar@1456
   175
    ///\e
alpar@987
   176
    typedef const T& ConstReference;
klao@286
   177
deba@2489
   178
  private:
deba@2489
   179
    
deba@2489
   180
    typedef std::map<K, T, Compare> Map;
deba@2489
   181
    Value _value;
deba@2489
   182
    Map _map;
klao@286
   183
deba@2489
   184
  public:
deba@2489
   185
klao@286
   186
    /// Constructor with specified default value
deba@2489
   187
    StdMap(const T& value = T()) : _value(value) {}
klao@286
   188
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   189
    /// specifies a default value.
deba@2489
   190
    template <typename T1, typename Comp1>
deba@2489
   191
    StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) 
deba@2489
   192
      : _map(map.begin(), map.end()), _value(value) {}
klao@286
   193
    
deba@2489
   194
    /// \brief Constructs a map from an other StdMap.
klao@286
   195
    template<typename T1, typename Comp1>
deba@2489
   196
    StdMap(const StdMap<Key, T1, Comp1> &c) 
deba@2489
   197
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
deba@2489
   198
deba@2489
   199
  private:
deba@2489
   200
deba@2489
   201
    StdMap& operator=(const StdMap&);
deba@2489
   202
deba@2489
   203
  public:
deba@2489
   204
deba@2489
   205
    ///\e
deba@2489
   206
    Reference operator[](const Key &k) {
deba@2489
   207
      typename Map::iterator it = _map.lower_bound(k);
deba@2489
   208
      if (it != _map.end() && !_map.key_comp()(k, it->first))
deba@2489
   209
	return it->second;
deba@2489
   210
      else
deba@2489
   211
	return _map.insert(it, std::make_pair(k, _value))->second;
marci@389
   212
    }
klao@286
   213
deba@2489
   214
    /// \e 
deba@2489
   215
    ConstReference operator[](const Key &k) const {
deba@2489
   216
      typename Map::const_iterator it = _map.find(k);
deba@2489
   217
      if (it != _map.end())
deba@2489
   218
	return it->second;
deba@2489
   219
      else
deba@2489
   220
	return _value;
klao@286
   221
    }
deba@1675
   222
deba@2489
   223
    /// \e 
klao@345
   224
    void set(const Key &k, const T &t) {
deba@2489
   225
      typename Map::iterator it = _map.lower_bound(k);
deba@2489
   226
      if (it != _map.end() && !_map.key_comp()(k, it->first))
deba@2489
   227
	it->second = t;
deba@2489
   228
      else
deba@2489
   229
	_map.insert(it, std::make_pair(k, t));
klao@345
   230
    }
klao@286
   231
deba@2489
   232
    /// \e
deba@2489
   233
    void setAll(const T &t) {
deba@2489
   234
      _value = t;
deba@2489
   235
      _map.clear();
deba@2489
   236
    }    
klao@286
   237
deba@2489
   238
    template <typename T1, typename C1 = std::less<T1> >
klao@286
   239
    struct rebind {
deba@2489
   240
      typedef StdMap<Key, T1, C1> other;
klao@286
   241
    };
klao@286
   242
  };
alpar@1041
   243
alpar@1402
   244
  /// @}
alpar@1402
   245
alpar@1402
   246
  /// \addtogroup map_adaptors
alpar@1402
   247
  /// @{
alpar@1402
   248
deba@1531
   249
  /// \brief Identity mapping.
deba@1531
   250
  ///
deba@1531
   251
  /// This mapping gives back the given key as value without any
deba@1531
   252
  /// modification. 
deba@1705
   253
  template <typename T>
deba@1705
   254
  class IdentityMap : public MapBase<T, T> {
deba@1531
   255
  public:
deba@1705
   256
    typedef MapBase<T, T> Parent;
deba@1675
   257
    typedef typename Parent::Key Key;
deba@1675
   258
    typedef typename Parent::Value Value;
deba@1531
   259
deba@2489
   260
    /// \e
deba@1675
   261
    const T& operator[](const T& t) const {
deba@1531
   262
      return t;
deba@1531
   263
    }
deba@1531
   264
  };
alpar@1402
   265
deba@2489
   266
  ///Returns an \c IdentityMap class
deba@1675
   267
deba@2489
   268
  ///This function just returns an \c IdentityMap class.
deba@1675
   269
  ///\relates IdentityMap
deba@1675
   270
  template<typename T>
deba@1705
   271
  inline IdentityMap<T> identityMap() {
deba@1705
   272
    return IdentityMap<T>();
deba@1675
   273
  }
deba@1675
   274
  
deba@1675
   275
alpar@1547
   276
  ///Convert the \c Value of a map to another type.
alpar@1178
   277
deba@2489
   278
  ///This \c concepts::ReadMap "read only map"
alpar@1178
   279
  ///converts the \c Value of a maps to type \c T.
alpar@1547
   280
  ///Its \c Key is inherited from \c M.
deba@1705
   281
  template <typename M, typename T> 
deba@1705
   282
  class ConvertMap : public MapBase<typename M::Key, T> {
deba@1705
   283
    const M& m;
alpar@1178
   284
  public:
deba@1705
   285
    typedef MapBase<typename M::Key, T> Parent;
deba@1675
   286
    typedef typename Parent::Key Key;
deba@1675
   287
    typedef typename Parent::Value Value;
alpar@1178
   288
alpar@1178
   289
    ///Constructor
alpar@1178
   290
alpar@1178
   291
    ///Constructor
alpar@1536
   292
    ///\param _m is the underlying map
alpar@1178
   293
    ConvertMap(const M &_m) : m(_m) {};
deba@1346
   294
deba@1346
   295
    /// \brief The subscript operator.
deba@1346
   296
    ///
deba@1346
   297
    /// The subscript operator.
alpar@1536
   298
    /// \param k The key
deba@1346
   299
    /// \return The target of the edge 
deba@1675
   300
    Value operator[](const Key& k) const {return m[k];}
alpar@1178
   301
  };
alpar@1178
   302
  
deba@2489
   303
  ///Returns an \c ConvertMap class
alpar@1178
   304
deba@2489
   305
  ///This function just returns an \c ConvertMap class.
alpar@1178
   306
  ///\relates ConvertMap
deba@1675
   307
  template<typename T, typename M>
deba@1705
   308
  inline ConvertMap<M, T> convertMap(const M &m) {
deba@1705
   309
    return ConvertMap<M, T>(m);
alpar@1178
   310
  }
alpar@1041
   311
deba@2248
   312
  ///Simple wrapping of the map
deba@2248
   313
deba@2489
   314
  ///This \c concepts::ReadMap "read only map" returns the simple
deba@2248
   315
  ///wrapping of the given map. Sometimes the reference maps cannot be
deba@2248
   316
  ///combined with simple read maps. This map adaptor wraps the given
deba@2248
   317
  ///map to simple read map.
deba@2248
   318
  template<typename M> 
deba@2248
   319
  class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
deba@2248
   320
    const M& m;
deba@2248
   321
deba@2248
   322
  public:
deba@2248
   323
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2248
   324
    typedef typename Parent::Key Key;
deba@2248
   325
    typedef typename Parent::Value Value;
deba@2248
   326
deba@2248
   327
    ///Constructor
deba@2248
   328
    SimpleMap(const M &_m) : m(_m) {};
deba@2489
   329
    ///\e
deba@2248
   330
    Value operator[](Key k) const {return m[k];}
deba@2248
   331
  };
deba@2248
   332
deba@2248
   333
  ///Simple writeable wrapping of the map
deba@2248
   334
deba@2489
   335
  ///This \c concepts::ReadMap "read only map" returns the simple
deba@2248
   336
  ///wrapping of the given map. Sometimes the reference maps cannot be
deba@2248
   337
  ///combined with simple read-write maps. This map adaptor wraps the
deba@2248
   338
  ///given map to simple read-write map.
deba@2248
   339
  template<typename M> 
deba@2248
   340
  class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2248
   341
    M& m;
deba@2248
   342
deba@2248
   343
  public:
deba@2248
   344
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2248
   345
    typedef typename Parent::Key Key;
deba@2248
   346
    typedef typename Parent::Value Value;
deba@2248
   347
deba@2248
   348
    ///Constructor
deba@2248
   349
    SimpleWriteMap(M &_m) : m(_m) {};
deba@2489
   350
    ///\e
deba@2248
   351
    Value operator[](Key k) const {return m[k];}
deba@2489
   352
    ///\e
deba@2248
   353
    void set(Key k, const Value& c) { m.set(k, c); }
deba@2248
   354
  };
deba@2248
   355
alpar@1041
   356
  ///Sum of two maps
alpar@1041
   357
deba@2489
   358
  ///This \c concepts::ReadMap "read only map" returns the sum of the two
alpar@1041
   359
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   360
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
alpar@1041
   361
deba@1705
   362
  template<typename M1, typename M2> 
deba@1705
   363
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   364
    const M1& m1;
deba@1705
   365
    const M2& m2;
deba@1420
   366
alpar@1041
   367
  public:
deba@1705
   368
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   369
    typedef typename Parent::Key Key;
deba@1675
   370
    typedef typename Parent::Value Value;
alpar@1041
   371
alpar@1041
   372
    ///Constructor
alpar@1041
   373
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   374
    ///\e
alpar@1044
   375
    Value operator[](Key k) const {return m1[k]+m2[k];}
alpar@1041
   376
  };
alpar@1041
   377
  
deba@2489
   378
  ///Returns an \c AddMap class
alpar@1041
   379
deba@2489
   380
  ///This function just returns an \c AddMap class.
alpar@1041
   381
  ///\todo How to call these type of functions?
alpar@1041
   382
  ///
alpar@1041
   383
  ///\relates AddMap
deba@1675
   384
  template<typename M1, typename M2> 
deba@1705
   385
  inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
deba@1705
   386
    return AddMap<M1, M2>(m1,m2);
alpar@1041
   387
  }
alpar@1041
   388
alpar@1547
   389
  ///Shift a map with a constant.
alpar@1070
   390
deba@2489
   391
  ///This \c concepts::ReadMap "read only map" returns the sum of the
alpar@1070
   392
  ///given map and a constant value.
alpar@1070
   393
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   394
  ///
alpar@1070
   395
  ///Actually,
alpar@1070
   396
  ///\code
alpar@1070
   397
  ///  ShiftMap<X> sh(x,v);
alpar@1070
   398
  ///\endcode
alpar@1547
   399
  ///is equivalent with
alpar@1070
   400
  ///\code
alpar@1070
   401
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   402
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1070
   403
  ///\endcode
deba@1705
   404
  template<typename M, typename C = typename M::Value> 
deba@1705
   405
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   406
    const M& m;
deba@1691
   407
    C v;
alpar@1070
   408
  public:
deba@1705
   409
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   410
    typedef typename Parent::Key Key;
deba@1675
   411
    typedef typename Parent::Value Value;
alpar@1070
   412
alpar@1070
   413
    ///Constructor
alpar@1070
   414
alpar@1070
   415
    ///Constructor
alpar@1070
   416
    ///\param _m is the undelying map
alpar@1070
   417
    ///\param _v is the shift value
deba@1691
   418
    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   419
    ///\e
deba@1691
   420
    Value operator[](Key k) const {return m[k] + v;}
alpar@1070
   421
  };
deba@2032
   422
deba@2032
   423
  ///Shift a map with a constant.
deba@2032
   424
deba@2489
   425
  ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the
deba@2032
   426
  ///given map and a constant value. It makes also possible to write the map.
deba@2032
   427
  ///Its \c Key and \c Value is inherited from \c M.
deba@2032
   428
  ///
deba@2032
   429
  ///Actually,
deba@2032
   430
  ///\code
deba@2032
   431
  ///  ShiftMap<X> sh(x,v);
deba@2032
   432
  ///\endcode
deba@2032
   433
  ///is equivalent with
deba@2032
   434
  ///\code
deba@2032
   435
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
deba@2032
   436
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
deba@2032
   437
  ///\endcode
deba@2032
   438
  template<typename M, typename C = typename M::Value> 
deba@2032
   439
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   440
    M& m;
deba@2032
   441
    C v;
deba@2032
   442
  public:
deba@2032
   443
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   444
    typedef typename Parent::Key Key;
deba@2032
   445
    typedef typename Parent::Value Value;
deba@2032
   446
deba@2032
   447
    ///Constructor
deba@2032
   448
deba@2032
   449
    ///Constructor
deba@2032
   450
    ///\param _m is the undelying map
deba@2032
   451
    ///\param _v is the shift value
deba@2080
   452
    ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   453
    /// \e
deba@2032
   454
    Value operator[](Key k) const {return m[k] + v;}
deba@2489
   455
    /// \e
deba@2032
   456
    void set(Key k, const Value& c) { m.set(k, c - v); }
deba@2032
   457
  };
alpar@1070
   458
  
deba@2489
   459
  ///Returns an \c ShiftMap class
alpar@1070
   460
deba@2489
   461
  ///This function just returns an \c ShiftMap class.
alpar@1070
   462
  ///\relates ShiftMap
deba@1691
   463
  template<typename M, typename C> 
deba@1705
   464
  inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
deba@1705
   465
    return ShiftMap<M, C>(m,v);
alpar@1070
   466
  }
alpar@1070
   467
deba@2032
   468
  template<typename M, typename C> 
deba@2032
   469
  inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
deba@2032
   470
    return ShiftWriteMap<M, C>(m,v);
deba@2032
   471
  }
deba@2032
   472
alpar@1041
   473
  ///Difference of two maps
alpar@1041
   474
deba@2489
   475
  ///This \c concepts::ReadMap "read only map" returns the difference
alpar@1547
   476
  ///of the values of the two
alpar@1041
   477
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   478
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   479
deba@1705
   480
  template<typename M1, typename M2> 
deba@1705
   481
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   482
    const M1& m1;
deba@1705
   483
    const M2& m2;
alpar@1041
   484
  public:
deba@1705
   485
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   486
    typedef typename Parent::Key Key;
deba@1675
   487
    typedef typename Parent::Value Value;
alpar@1041
   488
alpar@1041
   489
    ///Constructor
alpar@1041
   490
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   491
    /// \e
alpar@1044
   492
    Value operator[](Key k) const {return m1[k]-m2[k];}
alpar@1041
   493
  };
alpar@1041
   494
  
deba@2489
   495
  ///Returns a \c SubMap class
alpar@1041
   496
deba@2489
   497
  ///This function just returns a \c SubMap class.
alpar@1041
   498
  ///
alpar@1041
   499
  ///\relates SubMap
deba@1675
   500
  template<typename M1, typename M2> 
deba@1705
   501
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
deba@1705
   502
    return SubMap<M1, M2>(m1, m2);
alpar@1041
   503
  }
alpar@1041
   504
alpar@1041
   505
  ///Product of two maps
alpar@1041
   506
deba@2489
   507
  ///This \c concepts::ReadMap "read only map" returns the product of the
alpar@1547
   508
  ///values of the two
alpar@1041
   509
  ///given
alpar@1041
   510
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   511
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   512
deba@1705
   513
  template<typename M1, typename M2> 
deba@1705
   514
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   515
    const M1& m1;
deba@1705
   516
    const M2& m2;
alpar@1041
   517
  public:
deba@1705
   518
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   519
    typedef typename Parent::Key Key;
deba@1675
   520
    typedef typename Parent::Value Value;
alpar@1041
   521
alpar@1041
   522
    ///Constructor
alpar@1041
   523
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   524
    /// \e
alpar@1044
   525
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   526
  };
alpar@1041
   527
  
deba@2489
   528
  ///Returns a \c MulMap class
alpar@1041
   529
deba@2489
   530
  ///This function just returns a \c MulMap class.
alpar@1041
   531
  ///\relates MulMap
deba@1675
   532
  template<typename M1, typename M2> 
deba@1705
   533
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
deba@1705
   534
    return MulMap<M1, M2>(m1,m2);
alpar@1041
   535
  }
alpar@1041
   536
 
alpar@1547
   537
  ///Scales a maps with a constant.
alpar@1070
   538
deba@2489
   539
  ///This \c concepts::ReadMap "read only map" returns the value of the
deba@1691
   540
  ///given map multiplied from the left side with a constant value.
alpar@1070
   541
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   542
  ///
alpar@1070
   543
  ///Actually,
alpar@1070
   544
  ///\code
alpar@1070
   545
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   546
  ///\endcode
alpar@1547
   547
  ///is equivalent with
alpar@1070
   548
  ///\code
alpar@1070
   549
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   550
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   551
  ///\endcode
deba@1705
   552
  template<typename M, typename C = typename M::Value> 
deba@1705
   553
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   554
    const M& m;
deba@1691
   555
    C v;
alpar@1070
   556
  public:
deba@1705
   557
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   558
    typedef typename Parent::Key Key;
deba@1675
   559
    typedef typename Parent::Value Value;
alpar@1070
   560
alpar@1070
   561
    ///Constructor
alpar@1070
   562
alpar@1070
   563
    ///Constructor
alpar@1070
   564
    ///\param _m is the undelying map
alpar@1070
   565
    ///\param _v is the scaling value
deba@1691
   566
    ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   567
    /// \e
deba@1691
   568
    Value operator[](Key k) const {return v * m[k];}
alpar@1070
   569
  };
deba@2032
   570
deba@2032
   571
  ///Scales a maps with a constant.
deba@2032
   572
deba@2489
   573
  ///This \c concepts::ReadWriteMap "read-write map" returns the value of the
deba@2032
   574
  ///given map multiplied from the left side with a constant value. It can
deba@2032
   575
  ///be used as write map also if the given multiplier is not zero.
deba@2032
   576
  ///Its \c Key and \c Value is inherited from \c M.
deba@2032
   577
  template<typename M, typename C = typename M::Value> 
deba@2032
   578
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   579
    M& m;
deba@2032
   580
    C v;
deba@2032
   581
  public:
deba@2032
   582
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   583
    typedef typename Parent::Key Key;
deba@2032
   584
    typedef typename Parent::Value Value;
deba@2032
   585
deba@2032
   586
    ///Constructor
deba@2032
   587
deba@2032
   588
    ///Constructor
deba@2032
   589
    ///\param _m is the undelying map
deba@2032
   590
    ///\param _v is the scaling value
deba@2032
   591
    ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   592
    /// \e
deba@2032
   593
    Value operator[](Key k) const {return v * m[k];}
deba@2489
   594
    /// \e
deba@2032
   595
    void set(Key k, const Value& c) { m.set(k, c / v);}
deba@2032
   596
  };
alpar@1070
   597
  
deba@2489
   598
  ///Returns an \c ScaleMap class
alpar@1070
   599
deba@2489
   600
  ///This function just returns an \c ScaleMap class.
alpar@1070
   601
  ///\relates ScaleMap
deba@1691
   602
  template<typename M, typename C> 
deba@1705
   603
  inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
deba@1705
   604
    return ScaleMap<M, C>(m,v);
alpar@1070
   605
  }
alpar@1070
   606
deba@2032
   607
  template<typename M, typename C> 
deba@2032
   608
  inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
deba@2032
   609
    return ScaleWriteMap<M, C>(m,v);
deba@2032
   610
  }
deba@2032
   611
alpar@1041
   612
  ///Quotient of two maps
alpar@1041
   613
deba@2489
   614
  ///This \c concepts::ReadMap "read only map" returns the quotient of the
alpar@1547
   615
  ///values of the two
alpar@1041
   616
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   617
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   618
deba@1705
   619
  template<typename M1, typename M2> 
deba@1705
   620
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   621
    const M1& m1;
deba@1705
   622
    const M2& m2;
alpar@1041
   623
  public:
deba@1705
   624
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   625
    typedef typename Parent::Key Key;
deba@1675
   626
    typedef typename Parent::Value Value;
alpar@1041
   627
alpar@1041
   628
    ///Constructor
alpar@1041
   629
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   630
    /// \e
alpar@1044
   631
    Value operator[](Key k) const {return m1[k]/m2[k];}
alpar@1041
   632
  };
alpar@1041
   633
  
deba@2489
   634
  ///Returns a \c DivMap class
alpar@1041
   635
deba@2489
   636
  ///This function just returns a \c DivMap class.
alpar@1041
   637
  ///\relates DivMap
deba@1675
   638
  template<typename M1, typename M2> 
deba@1705
   639
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
deba@1705
   640
    return DivMap<M1, M2>(m1,m2);
alpar@1041
   641
  }
alpar@1041
   642
  
alpar@1041
   643
  ///Composition of two maps
alpar@1041
   644
deba@2489
   645
  ///This \c concepts::ReadMap "read only map" returns the composition of
alpar@1041
   646
  ///two
alpar@1041
   647
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
alpar@1041
   648
  ///of \c M2,
alpar@1041
   649
  ///then for
alpar@1041
   650
  ///\code
deba@1675
   651
  ///  ComposeMap<M1, M2> cm(m1,m2);
alpar@1041
   652
  ///\endcode
alpar@1044
   653
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
alpar@1041
   654
  ///
alpar@1041
   655
  ///Its \c Key is inherited from \c M2 and its \c Value is from
alpar@1041
   656
  ///\c M1.
alpar@1041
   657
  ///The \c M2::Value must be convertible to \c M1::Key.
alpar@1041
   658
  ///\todo Check the requirements.
deba@1705
   659
  template <typename M1, typename M2> 
deba@1705
   660
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
deba@1705
   661
    const M1& m1;
deba@1705
   662
    const M2& m2;
alpar@1041
   663
  public:
deba@1705
   664
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
deba@1675
   665
    typedef typename Parent::Key Key;
deba@1675
   666
    typedef typename Parent::Value Value;
alpar@1041
   667
alpar@1041
   668
    ///Constructor
alpar@1041
   669
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@1725
   670
    
deba@1725
   671
    typename MapTraits<M1>::ConstReturnValue
deba@2489
   672
    /// \e
deba@1725
   673
    operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   674
  };
deba@2489
   675
  ///Returns a \c ComposeMap class
alpar@1041
   676
deba@2489
   677
  ///This function just returns a \c ComposeMap class.
alpar@1219
   678
  ///
alpar@1041
   679
  ///\relates ComposeMap
deba@1675
   680
  template <typename M1, typename M2> 
deba@1705
   681
  inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
deba@1705
   682
    return ComposeMap<M1, M2>(m1,m2);
alpar@1041
   683
  }
alpar@1219
   684
  
alpar@1547
   685
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   686
alpar@1547
   687
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   688
  ///
alpar@1219
   689
  ///
deba@2489
   690
  ///This \c concepts::ReadMap "read only map" takes two maps and a
alpar@1219
   691
  ///binary functor and returns the composition of
alpar@1547
   692
  ///the two
alpar@1219
   693
  ///given maps unsing the functor. 
alpar@1219
   694
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
alpar@1219
   695
  ///and \c f is of \c F,
alpar@1219
   696
  ///then for
alpar@1219
   697
  ///\code
deba@1675
   698
  ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
alpar@1219
   699
  ///\endcode
alpar@1219
   700
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   701
  ///
alpar@1219
   702
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
alpar@1219
   703
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   704
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   705
  ///to \c V.
alpar@1219
   706
  ///\todo Check the requirements.
deba@1675
   707
  template<typename M1, typename M2, typename F,
deba@2489
   708
	   typename V = typename F::result_type> 
deba@1705
   709
  class CombineMap : public MapBase<typename M1::Key, V> {
deba@1705
   710
    const M1& m1;
deba@1705
   711
    const M2& m2;
deba@1420
   712
    F f;
alpar@1219
   713
  public:
deba@1705
   714
    typedef MapBase<typename M1::Key, V> Parent;
deba@1675
   715
    typedef typename Parent::Key Key;
deba@1675
   716
    typedef typename Parent::Value Value;
alpar@1219
   717
alpar@1219
   718
    ///Constructor
deba@2489
   719
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
alpar@1219
   720
      : m1(_m1), m2(_m2), f(_f) {};
deba@2489
   721
    /// \e
alpar@1219
   722
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   723
  };
alpar@1219
   724
  
deba@2489
   725
  ///Returns a \c CombineMap class
alpar@1219
   726
deba@2489
   727
  ///This function just returns a \c CombineMap class.
alpar@1219
   728
  ///
alpar@1219
   729
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   730
  ///\code
deba@2489
   731
  ///combineMap<double>(m1,m2,std::plus<double>())
alpar@1219
   732
  ///\endcode
alpar@1219
   733
  ///is equivalent with
alpar@1219
   734
  ///\code
alpar@1219
   735
  ///addMap(m1,m2)
alpar@1219
   736
  ///\endcode
alpar@1219
   737
  ///
deba@2489
   738
  ///This function is specialized for adaptable binary function
deba@2489
   739
  ///classes and c++ functions.
deba@2489
   740
  ///
alpar@1219
   741
  ///\relates CombineMap
deba@1675
   742
  template<typename M1, typename M2, typename F, typename V> 
deba@1705
   743
  inline CombineMap<M1, M2, F, V> 
deba@1675
   744
  combineMap(const M1& m1,const M2& m2, const F& f) {
deba@1705
   745
    return CombineMap<M1, M2, F, V>(m1,m2,f);
deba@1675
   746
  }
deba@1675
   747
deba@1675
   748
  template<typename M1, typename M2, typename F> 
deba@1705
   749
  inline CombineMap<M1, M2, F, typename F::result_type> 
deba@1675
   750
  combineMap(const M1& m1, const M2& m2, const F& f) {
deba@1675
   751
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
deba@1675
   752
  }
deba@1675
   753
deba@1675
   754
  template<typename M1, typename M2, typename K1, typename K2, typename V> 
deba@1705
   755
  inline CombineMap<M1, M2, V (*)(K1, K2), V> 
deba@1675
   756
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
deba@1675
   757
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
alpar@1219
   758
  }
alpar@1041
   759
alpar@1041
   760
  ///Negative value of a map
alpar@1041
   761
deba@2489
   762
  ///This \c concepts::ReadMap "read only map" returns the negative
alpar@1041
   763
  ///value of the
alpar@1041
   764
  ///value returned by the
alpar@1041
   765
  ///given map. Its \c Key and \c Value will be inherited from \c M.
alpar@1041
   766
  ///The unary \c - operator must be defined for \c Value, of course.
alpar@1041
   767
deba@1705
   768
  template<typename M> 
deba@1705
   769
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   770
    const M& m;
alpar@1041
   771
  public:
deba@1705
   772
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   773
    typedef typename Parent::Key Key;
deba@1675
   774
    typedef typename Parent::Value Value;
alpar@1041
   775
alpar@1041
   776
    ///Constructor
alpar@1041
   777
    NegMap(const M &_m) : m(_m) {};
deba@2489
   778
    /// \e
alpar@1044
   779
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   780
  };
alpar@1041
   781
  
deba@2032
   782
  ///Negative value of a map
deba@2032
   783
deba@2489
   784
  ///This \c concepts::ReadWriteMap "read-write map" returns the negative
deba@2032
   785
  ///value of the value returned by the
deba@2032
   786
  ///given map. Its \c Key and \c Value will be inherited from \c M.
deba@2032
   787
  ///The unary \c - operator must be defined for \c Value, of course.
deba@2032
   788
deba@2032
   789
  template<typename M> 
deba@2032
   790
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   791
    M& m;
deba@2032
   792
  public:
deba@2032
   793
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   794
    typedef typename Parent::Key Key;
deba@2032
   795
    typedef typename Parent::Value Value;
deba@2032
   796
deba@2032
   797
    ///Constructor
deba@2032
   798
    NegWriteMap(M &_m) : m(_m) {};
deba@2489
   799
    /// \e
deba@2032
   800
    Value operator[](Key k) const {return -m[k];}
deba@2489
   801
    /// \e
deba@2032
   802
    void set(Key k, const Value& v) { m.set(k, -v); }
deba@2032
   803
  };
deba@2032
   804
deba@2489
   805
  ///Returns a \c NegMap class
alpar@1041
   806
deba@2489
   807
  ///This function just returns a \c NegMap class.
alpar@1041
   808
  ///\relates NegMap
deba@1675
   809
  template <typename M> 
deba@1705
   810
  inline NegMap<M> negMap(const M &m) {
deba@1705
   811
    return NegMap<M>(m);
alpar@1041
   812
  }
alpar@1041
   813
deba@2032
   814
  template <typename M> 
deba@2032
   815
  inline NegWriteMap<M> negMap(M &m) {
deba@2032
   816
    return NegWriteMap<M>(m);
deba@2032
   817
  }
alpar@1041
   818
alpar@1041
   819
  ///Absolute value of a map
alpar@1041
   820
deba@2489
   821
  ///This \c concepts::ReadMap "read only map" returns the absolute value
alpar@1041
   822
  ///of the
alpar@1041
   823
  ///value returned by the
alpar@1044
   824
  ///given map. Its \c Key and \c Value will be inherited
alpar@1044
   825
  ///from <tt>M</tt>. <tt>Value</tt>
alpar@1044
   826
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
alpar@1044
   827
  ///operator must be defined for it, of course.
alpar@1044
   828
  ///
alpar@1044
   829
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   830
  ///\code
alpar@1044
   831
  ///  struct _UnConvertible {};
alpar@1044
   832
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   833
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   834
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   835
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   836
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   837
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   838
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   839
  ///\endcode
alpar@1044
   840
  
alpar@1041
   841
deba@1705
   842
  template<typename M> 
deba@1705
   843
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   844
    const M& m;
alpar@1041
   845
  public:
deba@1705
   846
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   847
    typedef typename Parent::Key Key;
deba@1675
   848
    typedef typename Parent::Value Value;
alpar@1041
   849
alpar@1041
   850
    ///Constructor
alpar@1041
   851
    AbsMap(const M &_m) : m(_m) {};
deba@2489
   852
    /// \e
deba@1675
   853
    Value operator[](Key k) const {
deba@1675
   854
      Value tmp = m[k]; 
deba@1675
   855
      return tmp >= 0 ? tmp : -tmp;
deba@1675
   856
    }
deba@1675
   857
alpar@1041
   858
  };
alpar@1041
   859
  
deba@2489
   860
  ///Returns a \c AbsMap class
alpar@1041
   861
deba@2489
   862
  ///This function just returns a \c AbsMap class.
alpar@1041
   863
  ///\relates AbsMap
deba@1675
   864
  template<typename M> 
deba@1705
   865
  inline AbsMap<M> absMap(const M &m) {
deba@1705
   866
    return AbsMap<M>(m);
alpar@1041
   867
  }
alpar@1041
   868
alpar@1402
   869
  ///Converts an STL style functor to a map
alpar@1076
   870
deba@2489
   871
  ///This \c concepts::ReadMap "read only map" returns the value
alpar@1076
   872
  ///of a
alpar@1076
   873
  ///given map.
alpar@1076
   874
  ///
alpar@1076
   875
  ///Template parameters \c K and \c V will become its
alpar@1076
   876
  ///\c Key and \c Value. They must be given explicitely
alpar@1076
   877
  ///because a functor does not provide such typedefs.
alpar@1076
   878
  ///
alpar@1076
   879
  ///Parameter \c F is the type of the used functor.
deba@1675
   880
  template<typename F, 
deba@1675
   881
	   typename K = typename F::argument_type, 
deba@2489
   882
	   typename V = typename F::result_type> 
deba@1705
   883
  class FunctorMap : public MapBase<K, V> {
deba@1679
   884
    F f;
alpar@1076
   885
  public:
deba@1705
   886
    typedef MapBase<K, V> Parent;
deba@1675
   887
    typedef typename Parent::Key Key;
deba@1675
   888
    typedef typename Parent::Value Value;
alpar@1076
   889
alpar@1076
   890
    ///Constructor
deba@2489
   891
    FunctorMap(const F &_f = F()) : f(_f) {}
deba@2489
   892
    /// \e
deba@1679
   893
    Value operator[](Key k) const { return f(k);}
alpar@1076
   894
  };
alpar@1076
   895
  
deba@2489
   896
  ///Returns a \c FunctorMap class
alpar@1076
   897
deba@2489
   898
  ///This function just returns a \c FunctorMap class.
alpar@1076
   899
  ///
deba@2489
   900
  ///It is specialized for adaptable function classes and
deba@2489
   901
  ///c++ functions.
alpar@1076
   902
  ///\relates FunctorMap
deba@1675
   903
  template<typename K, typename V, typename F> inline 
deba@1705
   904
  FunctorMap<F, K, V> functorMap(const F &f) {
deba@1705
   905
    return FunctorMap<F, K, V>(f);
alpar@1076
   906
  }
alpar@1076
   907
deba@1675
   908
  template <typename F> inline 
deba@1705
   909
  FunctorMap<F, typename F::argument_type, typename F::result_type> 
deba@1675
   910
  functorMap(const F &f) {
deba@1679
   911
    return FunctorMap<F, typename F::argument_type, 
deba@1705
   912
      typename F::result_type>(f);
deba@1675
   913
  }
deba@1675
   914
deba@1675
   915
  template <typename K, typename V> inline 
deba@1705
   916
  FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
deba@1705
   917
    return FunctorMap<V (*)(K), K, V>(f);
deba@1675
   918
  }
deba@1675
   919
deba@1675
   920
alpar@1219
   921
  ///Converts a map to an STL style (unary) functor
alpar@1076
   922
alpar@1219
   923
  ///This class Converts a map to an STL style (unary) functor.
alpar@1076
   924
  ///that is it provides an <tt>operator()</tt> to read its values.
alpar@1076
   925
  ///
alpar@1223
   926
  ///For the sake of convenience it also works as
deba@2489
   927
  ///a ususal \c concepts::ReadMap "readable map",
alpar@1537
   928
  ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
deba@1705
   929
  template <typename M> 
deba@1705
   930
  class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   931
    const M& m;
alpar@1076
   932
  public:
deba@1705
   933
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   934
    typedef typename Parent::Key Key;
deba@1675
   935
    typedef typename Parent::Value Value;
deba@1420
   936
alpar@1223
   937
    typedef typename M::Key argument_type;
alpar@1223
   938
    typedef typename M::Value result_type;
alpar@1076
   939
alpar@1076
   940
    ///Constructor
alpar@1076
   941
    MapFunctor(const M &_m) : m(_m) {};
deba@2489
   942
    ///\e
alpar@1076
   943
    Value operator()(Key k) const {return m[k];}
alpar@1076
   944
    ///\e
alpar@1076
   945
    Value operator[](Key k) const {return m[k];}
alpar@1076
   946
  };
alpar@1076
   947
  
deba@2489
   948
  ///Returns a \c MapFunctor class
alpar@1076
   949
deba@2489
   950
  ///This function just returns a \c MapFunctor class.
alpar@1076
   951
  ///\relates MapFunctor
deba@1675
   952
  template<typename M> 
deba@1705
   953
  inline MapFunctor<M> mapFunctor(const M &m) {
deba@1705
   954
    return MapFunctor<M>(m);
alpar@1076
   955
  }
alpar@1076
   956
alpar@1547
   957
  ///Applies all map setting operations to two maps
alpar@1219
   958
deba@2489
   959
  ///This map has two \c concepts::ReadMap "readable map"
deba@2032
   960
  ///parameters and each read request will be passed just to the
deba@2032
   961
  ///first map. This class is the just readable map type of the ForkWriteMap.
alpar@1219
   962
  ///
alpar@1219
   963
  ///The \c Key and \c Value will be inherited from \c M1.
alpar@1219
   964
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
deba@1705
   965
  template<typename  M1, typename M2> 
deba@1705
   966
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   967
    const M1& m1;
deba@1705
   968
    const M2& m2;
alpar@1219
   969
  public:
deba@1705
   970
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   971
    typedef typename Parent::Key Key;
deba@1675
   972
    typedef typename Parent::Value Value;
alpar@1219
   973
alpar@1219
   974
    ///Constructor
deba@2032
   975
    ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   976
    /// \e
alpar@1219
   977
    Value operator[](Key k) const {return m1[k];}
deba@2032
   978
  };
deba@2032
   979
deba@2032
   980
deba@2032
   981
  ///Applies all map setting operations to two maps
deba@2032
   982
deba@2489
   983
  ///This map has two \c concepts::WriteMap "writable map"
deba@2032
   984
  ///parameters and each write request will be passed to both of them.
deba@2489
   985
  ///If \c M1 is also \c concepts::ReadMap "readable",
deba@2032
   986
  ///then the read operations will return the
deba@2032
   987
  ///corresponding values of \c M1.
deba@2032
   988
  ///
deba@2032
   989
  ///The \c Key and \c Value will be inherited from \c M1.
deba@2032
   990
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
deba@2032
   991
  template<typename  M1, typename M2> 
deba@2032
   992
  class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@2032
   993
    M1& m1;
deba@2032
   994
    M2& m2;
deba@2032
   995
  public:
deba@2032
   996
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@2032
   997
    typedef typename Parent::Key Key;
deba@2032
   998
    typedef typename Parent::Value Value;
deba@2032
   999
deba@2032
  1000
    ///Constructor
deba@2032
  1001
    ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
  1002
    ///\e
deba@2032
  1003
    Value operator[](Key k) const {return m1[k];}
deba@2489
  1004
    ///\e
deba@2032
  1005
    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
  1006
  };
alpar@1219
  1007
  
deba@2489
  1008
  ///Returns an \c ForkMap class
alpar@1219
  1009
deba@2489
  1010
  ///This function just returns an \c ForkMap class.
alpar@1219
  1011
  ///
alpar@1219
  1012
  ///\relates ForkMap
deba@1675
  1013
  template <typename M1, typename M2> 
deba@2032
  1014
  inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
deba@1705
  1015
    return ForkMap<M1, M2>(m1,m2);
alpar@1219
  1016
  }
alpar@1219
  1017
deba@2032
  1018
  template <typename M1, typename M2> 
deba@2032
  1019
  inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
deba@2032
  1020
    return ForkWriteMap<M1, M2>(m1,m2);
deba@2032
  1021
  }
deba@2032
  1022
alpar@1456
  1023
alpar@1456
  1024
  
alpar@1456
  1025
  /* ************* BOOL MAPS ******************* */
alpar@1456
  1026
  
alpar@1456
  1027
  ///Logical 'not' of a map
alpar@1456
  1028
  
deba@2489
  1029
  ///This bool \c concepts::ReadMap "read only map" returns the 
alpar@1456
  1030
  ///logical negation of
alpar@1456
  1031
  ///value returned by the
alpar@1456
  1032
  ///given map. Its \c Key and will be inherited from \c M,
alpar@1456
  1033
  ///its Value is <tt>bool</tt>.
deba@1705
  1034
  template <typename M> 
deba@1705
  1035
  class NotMap : public MapBase<typename M::Key, bool> {
deba@1705
  1036
    const M& m;
alpar@1456
  1037
  public:
deba@1705
  1038
    typedef MapBase<typename M::Key, bool> Parent;
deba@1675
  1039
    typedef typename Parent::Key Key;
deba@1675
  1040
    typedef typename Parent::Value Value;
alpar@1456
  1041
deba@1778
  1042
    /// Constructor
alpar@1456
  1043
    NotMap(const M &_m) : m(_m) {};
deba@2489
  1044
    ///\e
alpar@1456
  1045
    Value operator[](Key k) const {return !m[k];}
alpar@1456
  1046
  };
deba@2032
  1047
deba@2032
  1048
  ///Logical 'not' of a map with writing possibility
deba@2032
  1049
  
deba@2489
  1050
  ///This bool \c concepts::ReadWriteMap "read-write map" returns the 
alpar@2258
  1051
  ///logical negation of value returned by the given map. When it is set,
alpar@2258
  1052
  ///the opposite value is set to the original map.
deba@2032
  1053
  ///Its \c Key and will be inherited from \c M,
deba@2032
  1054
  ///its Value is <tt>bool</tt>.
deba@2032
  1055
  template <typename M> 
deba@2032
  1056
  class NotWriteMap : public MapBase<typename M::Key, bool> {
deba@2032
  1057
    M& m;
deba@2032
  1058
  public:
deba@2032
  1059
    typedef MapBase<typename M::Key, bool> Parent;
deba@2032
  1060
    typedef typename Parent::Key Key;
deba@2032
  1061
    typedef typename Parent::Value Value;
deba@2032
  1062
deba@2032
  1063
    /// Constructor
deba@2032
  1064
    NotWriteMap(M &_m) : m(_m) {};
deba@2489
  1065
    ///\e
deba@2032
  1066
    Value operator[](Key k) const {return !m[k];}
deba@2489
  1067
    ///\e
deba@2032
  1068
    void set(Key k, bool v) { m.set(k, !v); }
deba@2032
  1069
  };
alpar@1456
  1070
  
deba@2489
  1071
  ///Returns a \c NotMap class
alpar@1456
  1072
  
deba@2489
  1073
  ///This function just returns a \c NotMap class.
alpar@1456
  1074
  ///\relates NotMap
deba@1675
  1075
  template <typename M> 
deba@1705
  1076
  inline NotMap<M> notMap(const M &m) {
deba@1705
  1077
    return NotMap<M>(m);
alpar@1456
  1078
  }
deba@2423
  1079
  
deba@2032
  1080
  template <typename M> 
deba@2032
  1081
  inline NotWriteMap<M> notMap(M &m) {
deba@2032
  1082
    return NotWriteMap<M>(m);
deba@2032
  1083
  }
deba@2032
  1084
deba@2091
  1085
  namespace _maps_bits {
deba@2423
  1086
deba@2091
  1087
    template <typename Value>
deba@2091
  1088
    struct Identity {
deba@2091
  1089
      typedef Value argument_type;
deba@2091
  1090
      typedef Value result_type;
deba@2423
  1091
      Value operator()(const Value& val) const {
deba@2091
  1092
	return val;
deba@2091
  1093
      }
deba@2091
  1094
    };
deba@2423
  1095
deba@2423
  1096
    template <typename _Iterator, typename Enable = void>
deba@2423
  1097
    struct IteratorTraits {
deba@2423
  1098
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
deba@2423
  1099
    };
deba@2423
  1100
deba@2423
  1101
    template <typename _Iterator>
deba@2423
  1102
    struct IteratorTraits<_Iterator,
deba@2423
  1103
      typename exists<typename _Iterator::container_type>::type> 
deba@2423
  1104
    {
deba@2423
  1105
      typedef typename _Iterator::container_type::value_type Value;
deba@2423
  1106
    };
deba@2423
  1107
deba@2091
  1108
  }
deba@2091
  1109
  
deba@2091
  1110
alpar@1808
  1111
  /// \brief Writable bool map for store each true assigned elements.
deba@1778
  1112
  ///
alpar@2258
  1113
  /// Writable bool map to store each true assigned elements. It will
alpar@2258
  1114
  /// copies all the keys set to true to the given iterator.
deba@1778
  1115
  ///
deba@2091
  1116
  /// \note The container of the iterator should contain space 
deba@2091
  1117
  /// for each element.
deba@2091
  1118
  ///
deba@2091
  1119
  /// The next example shows how can you write the nodes directly
deba@2091
  1120
  /// to the standard output.
deba@2091
  1121
  ///\code
deba@2091
  1122
  /// typedef IdMap<UGraph, UEdge> UEdgeIdMap;
deba@2091
  1123
  /// UEdgeIdMap uedgeId(ugraph);
deba@2091
  1124
  ///
deba@2091
  1125
  /// typedef MapFunctor<UEdgeIdMap> UEdgeIdFunctor;
deba@2091
  1126
  /// UEdgeIdFunctor uedgeIdFunctor(uedgeId);
deba@2091
  1127
  ///
deba@2091
  1128
  /// StoreBoolMap<ostream_iterator<int>, UEdgeIdFunctor> 
deba@2091
  1129
  ///   writerMap(ostream_iterator<int>(cout, " "), uedgeIdFunctor);
deba@2091
  1130
  ///
deba@2091
  1131
  /// prim(ugraph, cost, writerMap);
deba@2091
  1132
  ///\endcode
deba@2091
  1133
  template <typename _Iterator, 
deba@2423
  1134
            typename _Functor =
deba@2423
  1135
            _maps_bits::Identity<typename _maps_bits::
deba@2423
  1136
                                 IteratorTraits<_Iterator>::Value> >
deba@1778
  1137
  class StoreBoolMap {
deba@1778
  1138
  public:
deba@1778
  1139
    typedef _Iterator Iterator;
deba@1778
  1140
deba@2091
  1141
    typedef typename _Functor::argument_type Key;
deba@1778
  1142
    typedef bool Value;
deba@1778
  1143
deba@2091
  1144
    typedef _Functor Functor;
deba@2091
  1145
deba@1778
  1146
    /// Constructor
deba@2091
  1147
    StoreBoolMap(Iterator it, const Functor& functor = Functor()) 
deba@2091
  1148
      : _begin(it), _end(it), _functor(functor) {}
deba@1778
  1149
alpar@2258
  1150
    /// Gives back the given iterator set for the first time.
deba@1778
  1151
    Iterator begin() const {
deba@1778
  1152
      return _begin;
deba@1778
  1153
    }
deba@1778
  1154
 
alpar@2258
  1155
    /// Gives back the iterator after the last set operation.
deba@1778
  1156
    Iterator end() const {
deba@1778
  1157
      return _end;
deba@1778
  1158
    }
deba@1778
  1159
deba@1778
  1160
    /// Setter function of the map
deba@2423
  1161
    void set(const Key& key, Value value) const {
deba@1778
  1162
      if (value) {
deba@2091
  1163
	*_end++ = _functor(key);
deba@1778
  1164
      }
deba@1778
  1165
    }
deba@1778
  1166
    
deba@1778
  1167
  private:
deba@2423
  1168
    Iterator _begin;
deba@2423
  1169
    mutable Iterator _end;
deba@2091
  1170
    Functor _functor;
deba@1778
  1171
  };
deba@1778
  1172
alpar@1808
  1173
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
  1174
  /// a back insertable container.
deba@1778
  1175
  ///
alpar@1808
  1176
  /// Writable bool map for store each true assigned elements in a back 
alpar@2258
  1177
  /// insertable container. It will push back all the keys set to true into
deba@2091
  1178
  /// the container. It can be used to retrieve the items into a standard
deba@2091
  1179
  /// container. The next example shows how can you store the undirected
deba@2091
  1180
  /// edges in a vector with prim algorithm.
deba@2091
  1181
  ///
deba@2091
  1182
  ///\code
deba@2091
  1183
  /// vector<UEdge> span_tree_uedges;
deba@2091
  1184
  /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges);
deba@2091
  1185
  /// prim(ugraph, cost, inserter_map);
deba@2091
  1186
  ///\endcode
deba@2091
  1187
  template <typename Container,
deba@2091
  1188
            typename Functor =
deba@2091
  1189
            _maps_bits::Identity<typename Container::value_type> >
deba@1778
  1190
  class BackInserterBoolMap {
deba@1778
  1191
  public:
deba@1778
  1192
    typedef typename Container::value_type Key;
deba@1778
  1193
    typedef bool Value;
deba@1778
  1194
deba@1778
  1195
    /// Constructor
deba@2091
  1196
    BackInserterBoolMap(Container& _container, 
deba@2091
  1197
                        const Functor& _functor = Functor()) 
deba@2091
  1198
      : container(_container), functor(_functor) {}
deba@1778
  1199
deba@1778
  1200
    /// Setter function of the map
deba@1778
  1201
    void set(const Key& key, Value value) {
deba@1778
  1202
      if (value) {
deba@2091
  1203
	container.push_back(functor(key));
deba@1778
  1204
      }
deba@1778
  1205
    }
deba@1778
  1206
    
deba@1778
  1207
  private:
deba@2091
  1208
    Container& container;
deba@2091
  1209
    Functor functor;
deba@1778
  1210
  };
deba@1778
  1211
alpar@1808
  1212
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
  1213
  /// a front insertable container.
deba@1778
  1214
  ///
alpar@1808
  1215
  /// Writable bool map for store each true assigned elements in a front 
alpar@2258
  1216
  /// insertable container. It will push front all the keys set to \c true into
deba@2091
  1217
  /// the container. For example see the BackInserterBoolMap.
deba@2091
  1218
  template <typename Container,
deba@2091
  1219
            typename Functor =
deba@2091
  1220
            _maps_bits::Identity<typename Container::value_type> >
deba@1778
  1221
  class FrontInserterBoolMap {
deba@1778
  1222
  public:
deba@1778
  1223
    typedef typename Container::value_type Key;
deba@1778
  1224
    typedef bool Value;
deba@1778
  1225
deba@1778
  1226
    /// Constructor
deba@2091
  1227
    FrontInserterBoolMap(Container& _container,
deba@2091
  1228
                         const Functor& _functor = Functor()) 
deba@2091
  1229
      : container(_container), functor(_functor) {}
deba@1778
  1230
deba@1778
  1231
    /// Setter function of the map
deba@1778
  1232
    void set(const Key& key, Value value) {
deba@1778
  1233
      if (value) {
deba@1778
  1234
	container.push_front(key);
deba@1778
  1235
      }
deba@1778
  1236
    }
deba@1778
  1237
    
deba@1778
  1238
  private:
deba@1778
  1239
    Container& container;    
deba@2091
  1240
    Functor functor;
deba@1778
  1241
  };
deba@1778
  1242
alpar@1808
  1243
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
  1244
  /// an insertable container.
deba@1778
  1245
  ///
alpar@1808
  1246
  /// Writable bool map for store each true assigned elements in an 
alpar@2258
  1247
  /// insertable container. It will insert all the keys set to \c true into
deba@2091
  1248
  /// the container. If you want to store the cut edges of the strongly
deba@2091
  1249
  /// connected components in a set you can use the next code:
deba@2091
  1250
  ///
deba@2091
  1251
  ///\code
deba@2091
  1252
  /// set<Edge> cut_edges;
deba@2091
  1253
  /// InserterBoolMap<set<Edge> > inserter_map(cut_edges);
deba@2091
  1254
  /// stronglyConnectedCutEdges(graph, cost, inserter_map);
deba@2091
  1255
  ///\endcode
deba@2091
  1256
  template <typename Container,
deba@2091
  1257
            typename Functor =
deba@2091
  1258
            _maps_bits::Identity<typename Container::value_type> >
deba@1778
  1259
  class InserterBoolMap {
deba@1778
  1260
  public:
deba@1778
  1261
    typedef typename Container::value_type Key;
deba@1778
  1262
    typedef bool Value;
deba@1778
  1263
deba@1778
  1264
    /// Constructor
deba@2091
  1265
    InserterBoolMap(Container& _container, typename Container::iterator _it,
deba@2091
  1266
                    const Functor& _functor = Functor()) 
deba@2091
  1267
      : container(_container), it(_it), functor(_functor) {}
deba@2091
  1268
deba@2091
  1269
    /// Constructor
deba@2091
  1270
    InserterBoolMap(Container& _container, const Functor& _functor = Functor())
deba@2091
  1271
      : container(_container), it(_container.end()), functor(_functor) {}
deba@1778
  1272
deba@1778
  1273
    /// Setter function of the map
deba@1778
  1274
    void set(const Key& key, Value value) {
deba@1778
  1275
      if (value) {
deba@2091
  1276
	it = container.insert(it, key);
deba@2091
  1277
        ++it;
deba@1778
  1278
      }
deba@1778
  1279
    }
deba@1778
  1280
    
deba@1778
  1281
  private:
deba@2091
  1282
    Container& container;
deba@2091
  1283
    typename Container::iterator it;
deba@2091
  1284
    Functor functor;
deba@1778
  1285
  };
deba@1778
  1286
alpar@2258
  1287
  /// \brief Fill the true set elements with a given value.
deba@1778
  1288
  ///
alpar@2258
  1289
  /// Writable bool map to fill the elements set to \c true with a given value.
alpar@2258
  1290
  /// The value can set 
deba@1778
  1291
  /// the container.
deba@2091
  1292
  ///
deba@2091
  1293
  /// The next code finds the connected components of the undirected graph
deba@2091
  1294
  /// and stores it in the \c comp map:
deba@2091
  1295
  ///\code
deba@2091
  1296
  /// typedef UGraph::NodeMap<int> ComponentMap;
deba@2091
  1297
  /// ComponentMap comp(ugraph);
deba@2091
  1298
  /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap;
deba@2091
  1299
  /// ComponentFillerMap filler(comp, 0);
deba@2091
  1300
  ///
deba@2091
  1301
  /// Dfs<UGraph>::DefProcessedMap<ComponentFillerMap>::Create dfs(ugraph);
deba@2091
  1302
  /// dfs.processedMap(filler);
deba@2091
  1303
  /// dfs.init();
deba@2091
  1304
  /// for (NodeIt it(ugraph); it != INVALID; ++it) {
deba@2091
  1305
  ///   if (!dfs.reached(it)) {
deba@2091
  1306
  ///     dfs.addSource(it);
deba@2091
  1307
  ///     dfs.start();
deba@2091
  1308
  ///     ++filler.fillValue();
deba@2091
  1309
  ///   }
deba@2091
  1310
  /// }
deba@2091
  1311
  ///\endcode
deba@1778
  1312
  template <typename Map>
deba@1778
  1313
  class FillBoolMap {
deba@1778
  1314
  public:
deba@1778
  1315
    typedef typename Map::Key Key;
deba@1778
  1316
    typedef bool Value;
deba@1778
  1317
deba@1778
  1318
    /// Constructor
deba@1778
  1319
    FillBoolMap(Map& _map, const typename Map::Value& _fill) 
deba@1778
  1320
      : map(_map), fill(_fill) {}
deba@1778
  1321
deba@1778
  1322
    /// Constructor
deba@1778
  1323
    FillBoolMap(Map& _map) 
deba@1778
  1324
      : map(_map), fill() {}
deba@1778
  1325
deba@1778
  1326
    /// Gives back the current fill value
deba@2091
  1327
    const typename Map::Value& fillValue() const {
deba@2091
  1328
      return fill;
deba@2091
  1329
    } 
deba@2091
  1330
deba@2091
  1331
    /// Gives back the current fill value
deba@2091
  1332
    typename Map::Value& fillValue() {
deba@1778
  1333
      return fill;
deba@1778
  1334
    } 
deba@1778
  1335
deba@1778
  1336
    /// Sets the current fill value
deba@1778
  1337
    void fillValue(const typename Map::Value& _fill) {
deba@1778
  1338
      fill = _fill;
deba@1778
  1339
    } 
deba@1778
  1340
deba@1778
  1341
    /// Setter function of the map
deba@1778
  1342
    void set(const Key& key, Value value) {
deba@1778
  1343
      if (value) {
deba@1778
  1344
	map.set(key, fill);
deba@1778
  1345
      }
deba@1778
  1346
    }
deba@1778
  1347
    
deba@1778
  1348
  private:
deba@1778
  1349
    Map& map;
deba@1778
  1350
    typename Map::Value fill;
deba@1778
  1351
  };
deba@1778
  1352
deba@1778
  1353
alpar@1808
  1354
  /// \brief Writable bool map which stores for each true assigned elements  
deba@1778
  1355
  /// the setting order number.
deba@1778
  1356
  ///
alpar@1808
  1357
  /// Writable bool map which stores for each true assigned elements  
deba@2091
  1358
  /// the setting order number. It make easy to calculate the leaving
deba@2489
  1359
  /// order of the nodes in the \c Dfs algorithm.
deba@2091
  1360
  ///
deba@2091
  1361
  ///\code
deba@2091
  1362
  /// typedef Graph::NodeMap<int> OrderMap;
deba@2091
  1363
  /// OrderMap order(graph);
deba@2091
  1364
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
deba@2091
  1365
  /// OrderSetterMap setter(order);
deba@2091
  1366
  /// Dfs<Graph>::DefProcessedMap<OrderSetterMap>::Create dfs(graph);
deba@2091
  1367
  /// dfs.processedMap(setter);
deba@2091
  1368
  /// dfs.init();
deba@2091
  1369
  /// for (NodeIt it(graph); it != INVALID; ++it) {
deba@2091
  1370
  ///   if (!dfs.reached(it)) {
deba@2091
  1371
  ///     dfs.addSource(it);
deba@2091
  1372
  ///     dfs.start();
deba@2091
  1373
  ///   }
deba@2091
  1374
  /// }
deba@2091
  1375
  ///\endcode
deba@2091
  1376
  ///
deba@2091
  1377
  /// The discovering order can be stored a little harder because the
deba@2091
  1378
  /// ReachedMap should be readable in the dfs algorithm but the setting
deba@2091
  1379
  /// order map is not readable. Now we should use the fork map:
deba@2091
  1380
  ///
deba@2091
  1381
  ///\code
deba@2091
  1382
  /// typedef Graph::NodeMap<int> OrderMap;
deba@2091
  1383
  /// OrderMap order(graph);
deba@2091
  1384
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
deba@2091
  1385
  /// OrderSetterMap setter(order);
deba@2091
  1386
  /// typedef Graph::NodeMap<bool> StoreMap;
deba@2091
  1387
  /// StoreMap store(graph);
deba@2091
  1388
  ///
deba@2091
  1389
  /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
deba@2091
  1390
  /// ReachedMap reached(store, setter);
deba@2091
  1391
  ///
deba@2091
  1392
  /// Dfs<Graph>::DefReachedMap<ReachedMap>::Create dfs(graph);
deba@2091
  1393
  /// dfs.reachedMap(reached);
deba@2091
  1394
  /// dfs.init();
deba@2091
  1395
  /// for (NodeIt it(graph); it != INVALID; ++it) {
deba@2091
  1396
  ///   if (!dfs.reached(it)) {
deba@2091
  1397
  ///     dfs.addSource(it);
deba@2091
  1398
  ///     dfs.start();
deba@2091
  1399
  ///   }
deba@2091
  1400
  /// }
deba@2091
  1401
  ///\endcode
deba@1778
  1402
  template <typename Map>
deba@1778
  1403
  class SettingOrderBoolMap {
deba@1778
  1404
  public:
deba@1778
  1405
    typedef typename Map::Key Key;
deba@1778
  1406
    typedef bool Value;
deba@1778
  1407
deba@1778
  1408
    /// Constructor
deba@1778
  1409
    SettingOrderBoolMap(Map& _map) 
deba@1778
  1410
      : map(_map), counter(0) {}
deba@1778
  1411
alpar@2258
  1412
    /// Number of set operations.
deba@1778
  1413
    int num() const {
deba@1778
  1414
      return counter;
deba@1778
  1415
    }
deba@1778
  1416
deba@1778
  1417
    /// Setter function of the map
deba@1778
  1418
    void set(const Key& key, Value value) {
deba@1778
  1419
      if (value) {
deba@1778
  1420
	map.set(key, counter++);
deba@1778
  1421
      }
deba@1778
  1422
    }
deba@1778
  1423
    
deba@1778
  1424
  private:
deba@1778
  1425
    Map& map;
deba@1778
  1426
    int counter;
deba@1778
  1427
  };
deba@1778
  1428
alpar@1041
  1429
  /// @}
klao@286
  1430
}
alpar@1041
  1431
alpar@921
  1432
#endif // LEMON_MAPS_H