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