lemon/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 22 Dec 2007 12:35:00 +0000
changeset 25 751cd8f9bb1c
child 26 61bf7f22a6d6
permissions -rw-r--r--
Port general map related stuff from svn -r3424 + minor changes

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