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