lemon/maps.h
author klao
Fri, 17 Mar 2006 15:54:02 +0000
changeset 2010 08464643a658
parent 1956 a055123339d5
child 2032 18c08f9129e4
permissions -rw-r--r--
unused parameters fix
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@1778
    23
deba@1993
    24
#include <lemon/bits/utility.h>
deba@1993
    25
#include <lemon/bits/traits.h>
alpar@1041
    26
klao@286
    27
///\file
alpar@1041
    28
///\ingroup maps
klao@286
    29
///\brief Miscellaneous property maps
klao@286
    30
///
klao@959
    31
///\todo This file has the same name as the concept file in concept/,
klao@286
    32
/// and this is not easily detectable in docs...
klao@286
    33
klao@286
    34
#include <map>
klao@286
    35
alpar@921
    36
namespace lemon {
klao@286
    37
alpar@1041
    38
  /// \addtogroup maps
alpar@1041
    39
  /// @{
alpar@1041
    40
alpar@720
    41
  /// Base class of maps.
alpar@720
    42
alpar@805
    43
  /// Base class of maps.
alpar@805
    44
  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
deba@1705
    45
  template<typename K, typename T>
deba@1675
    46
  class MapBase {
alpar@720
    47
  public:
alpar@911
    48
    ///\e
alpar@987
    49
    typedef K Key;
alpar@911
    50
    ///\e
alpar@987
    51
    typedef T Value;
alpar@720
    52
  };
alpar@720
    53
alpar@805
    54
  /// Null map. (a.k.a. DoNothingMap)
klao@286
    55
klao@286
    56
  /// If you have to provide a map only for its type definitions,
alpar@805
    57
  /// or if you have to provide a writable map, but
alpar@805
    58
  /// data written to it will sent to <tt>/dev/null</tt>...
deba@1705
    59
  template<typename K, typename T>
deba@1705
    60
  class NullMap : public MapBase<K, T> {
klao@286
    61
  public:
deba@1705
    62
    typedef MapBase<K, T> Parent;
deba@1675
    63
    typedef typename Parent::Key Key;
deba@1675
    64
    typedef typename Parent::Value Value;
deba@1420
    65
    
alpar@805
    66
    /// Gives back a default constructed element.
klao@286
    67
    T operator[](const K&) const { return T(); }
alpar@805
    68
    /// Absorbs the value.
klao@286
    69
    void set(const K&, const T&) {}
klao@286
    70
  };
klao@286
    71
deba@1420
    72
  template <typename K, typename V> 
deba@1705
    73
  NullMap<K, V> nullMap() {
deba@1705
    74
    return NullMap<K, V>();
deba@1420
    75
  }
deba@1420
    76
klao@286
    77
klao@286
    78
  /// Constant map.
klao@286
    79
alpar@805
    80
  /// This is a readable map which assigns a specified value to each key.
alpar@805
    81
  /// In other aspects it is equivalent to the \ref NullMap.
alpar@805
    82
  /// \todo set could be used to set the value.
deba@1705
    83
  template<typename K, typename T>
deba@1705
    84
  class ConstMap : public MapBase<K, T> {
deba@1675
    85
  private:
klao@286
    86
    T v;
klao@286
    87
  public:
klao@286
    88
deba@1705
    89
    typedef MapBase<K, T> Parent;
deba@1675
    90
    typedef typename Parent::Key Key;
deba@1675
    91
    typedef typename Parent::Value Value;
deba@1420
    92
alpar@805
    93
    /// Default constructor
alpar@805
    94
alpar@805
    95
    /// The value of the map will be uninitialized. 
alpar@805
    96
    /// (More exactly it will be default constructed.)
klao@286
    97
    ConstMap() {}
alpar@911
    98
    ///\e
alpar@805
    99
alpar@805
   100
    /// \param _v The initial value of the map.
alpar@911
   101
    ///
klao@286
   102
    ConstMap(const T &_v) : v(_v) {}
klao@286
   103
klao@286
   104
    T operator[](const K&) const { return v; }
klao@286
   105
    void set(const K&, const T&) {}
klao@286
   106
klao@286
   107
    template<typename T1>
klao@286
   108
    struct rebind {
deba@1675
   109
      typedef ConstMap<K, T1> other;
klao@286
   110
    };
klao@286
   111
klao@286
   112
    template<typename T1>
deba@1675
   113
    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
klao@286
   114
  };
klao@286
   115
alpar@1076
   116
  ///Returns a \ref ConstMap class
alpar@1076
   117
alpar@1076
   118
  ///This function just returns a \ref ConstMap class.
alpar@1076
   119
  ///\relates ConstMap
deba@1675
   120
  template<typename K, typename V> 
deba@1705
   121
  inline ConstMap<K, V> constMap(const V &v) {
deba@1705
   122
    return ConstMap<K, V>(v);
alpar@1076
   123
  }
alpar@1076
   124
alpar@1076
   125
alpar@1660
   126
  //\todo to document later
marci@890
   127
  template<typename T, T v>
marci@890
   128
  struct Const { };
deba@1675
   129
alpar@1660
   130
  //\todo to document later
deba@1705
   131
  template<typename K, typename V, V v>
deba@1705
   132
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
marci@890
   133
  public:
deba@1705
   134
    typedef MapBase<K, V> Parent;
deba@1675
   135
    typedef typename Parent::Key Key;
deba@1675
   136
    typedef typename Parent::Value Value;
deba@1675
   137
marci@890
   138
    ConstMap() { }
marci@890
   139
    V operator[](const K&) const { return v; }
marci@890
   140
    void set(const K&, const V&) { }
marci@890
   141
  };
klao@286
   142
deba@1675
   143
  ///Returns a \ref ConstMap class
deba@1675
   144
deba@1675
   145
  ///This function just returns a \ref ConstMap class.
deba@1675
   146
  ///\relates ConstMap
deba@1675
   147
  template<typename K, typename V, V v> 
deba@1705
   148
  inline ConstMap<K, Const<V, v> > constMap() {
deba@1705
   149
    return ConstMap<K, Const<V, v> >();
deba@1675
   150
  }
deba@1675
   151
klao@286
   152
  /// \c std::map wrapper
klao@286
   153
klao@286
   154
  /// This is essentially a wrapper for \c std::map. With addition that
alpar@987
   155
  /// you can specify a default value different from \c Value() .
klao@286
   156
  ///
klao@286
   157
  /// \todo Provide allocator parameter...
alpar@987
   158
  template <typename K, typename T, typename Compare = std::less<K> >
deba@1675
   159
  class StdMap : public std::map<K, T, Compare> {
deba@1675
   160
    typedef std::map<K, T, Compare> parent;
klao@286
   161
    T v;
klao@286
   162
    typedef typename parent::value_type PairType;
klao@286
   163
klao@286
   164
  public:
alpar@1456
   165
    ///\e
alpar@987
   166
    typedef K Key;
alpar@1456
   167
    ///\e
alpar@987
   168
    typedef T Value;
alpar@1456
   169
    ///\e
alpar@987
   170
    typedef T& Reference;
alpar@1456
   171
    ///\e
alpar@987
   172
    typedef const T& ConstReference;
klao@286
   173
klao@286
   174
klao@345
   175
    StdMap() : v() {}
klao@286
   176
    /// Constructor with specified default value
klao@286
   177
    StdMap(const T& _v) : v(_v) {}
klao@286
   178
klao@286
   179
    /// \brief Constructs the map from an appropriate std::map.
klao@286
   180
    ///
klao@286
   181
    /// \warning Inefficient: copies the content of \c m !
klao@286
   182
    StdMap(const parent &m) : parent(m) {}
klao@286
   183
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   184
    /// specifies a default value.
klao@286
   185
    ///
klao@286
   186
    /// \warning Inefficient: copies the content of \c m !
klao@286
   187
    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
klao@286
   188
    
klao@286
   189
    template<typename T1, typename Comp1>
deba@1675
   190
    StdMap(const StdMap<Key, T1,Comp1> &m, const T &_v) { 
marci@389
   191
      //FIXME; 
marci@389
   192
    }
klao@286
   193
alpar@987
   194
    Reference operator[](const Key &k) {
klao@346
   195
      return insert(PairType(k,v)).first -> second;
klao@286
   196
    }
deba@1675
   197
alpar@987
   198
    ConstReference operator[](const Key &k) const {
marci@389
   199
      typename parent::iterator i = lower_bound(k);
beckerjc@391
   200
      if (i == parent::end() || parent::key_comp()(k, (*i).first))
klao@286
   201
	return v;
klao@286
   202
      return (*i).second;
klao@286
   203
    }
klao@345
   204
    void set(const Key &k, const T &t) {
klao@346
   205
      parent::operator[](k) = t;
klao@345
   206
    }
klao@286
   207
klao@286
   208
    /// Changes the default value of the map.
klao@286
   209
    /// \return Returns the previous default value.
klao@286
   210
    ///
alpar@805
   211
    /// \warning The value of some keys (which has already been queried, but
klao@286
   212
    /// the value has been unchanged from the default) may change!
klao@286
   213
    T setDefault(const T &_v) { T old=v; v=_v; return old; }
klao@286
   214
klao@286
   215
    template<typename T1>
klao@286
   216
    struct rebind {
deba@1675
   217
      typedef StdMap<Key, T1,Compare> other;
klao@286
   218
    };
klao@286
   219
  };
alpar@1041
   220
alpar@1402
   221
  /// @}
alpar@1402
   222
alpar@1402
   223
  /// \addtogroup map_adaptors
alpar@1402
   224
  /// @{
alpar@1402
   225
deba@1531
   226
  /// \brief Identity mapping.
deba@1531
   227
  ///
deba@1531
   228
  /// This mapping gives back the given key as value without any
deba@1531
   229
  /// modification. 
deba@1705
   230
  template <typename T>
deba@1705
   231
  class IdentityMap : public MapBase<T, T> {
deba@1531
   232
  public:
deba@1705
   233
    typedef MapBase<T, T> Parent;
deba@1675
   234
    typedef typename Parent::Key Key;
deba@1675
   235
    typedef typename Parent::Value Value;
deba@1531
   236
deba@1675
   237
    const T& operator[](const T& t) const {
deba@1531
   238
      return t;
deba@1531
   239
    }
deba@1531
   240
  };
alpar@1402
   241
deba@1675
   242
  ///Returns an \ref IdentityMap class
deba@1675
   243
deba@1675
   244
  ///This function just returns an \ref IdentityMap class.
deba@1675
   245
  ///\relates IdentityMap
deba@1675
   246
  template<typename T>
deba@1705
   247
  inline IdentityMap<T> identityMap() {
deba@1705
   248
    return IdentityMap<T>();
deba@1675
   249
  }
deba@1675
   250
  
deba@1675
   251
alpar@1547
   252
  ///Convert the \c Value of a map to another type.
alpar@1178
   253
alpar@1178
   254
  ///This \ref concept::ReadMap "read only map"
alpar@1178
   255
  ///converts the \c Value of a maps to type \c T.
alpar@1547
   256
  ///Its \c Key is inherited from \c M.
deba@1705
   257
  template <typename M, typename T> 
deba@1705
   258
  class ConvertMap : public MapBase<typename M::Key, T> {
deba@1705
   259
    const M& m;
alpar@1178
   260
  public:
deba@1705
   261
    typedef MapBase<typename M::Key, T> Parent;
deba@1675
   262
    typedef typename Parent::Key Key;
deba@1675
   263
    typedef typename Parent::Value Value;
alpar@1178
   264
alpar@1178
   265
    ///Constructor
alpar@1178
   266
alpar@1178
   267
    ///Constructor
alpar@1536
   268
    ///\param _m is the underlying map
alpar@1178
   269
    ConvertMap(const M &_m) : m(_m) {};
deba@1346
   270
deba@1346
   271
    /// \brief The subscript operator.
deba@1346
   272
    ///
deba@1346
   273
    /// The subscript operator.
alpar@1536
   274
    /// \param k The key
deba@1346
   275
    /// \return The target of the edge 
deba@1675
   276
    Value operator[](const Key& k) const {return m[k];}
alpar@1178
   277
  };
alpar@1178
   278
  
alpar@1178
   279
  ///Returns an \ref ConvertMap class
alpar@1178
   280
alpar@1178
   281
  ///This function just returns an \ref ConvertMap class.
alpar@1178
   282
  ///\relates ConvertMap
alpar@1178
   283
  ///\todo The order of the template parameters are changed.
deba@1675
   284
  template<typename T, typename M>
deba@1705
   285
  inline ConvertMap<M, T> convertMap(const M &m) {
deba@1705
   286
    return ConvertMap<M, T>(m);
alpar@1178
   287
  }
alpar@1041
   288
alpar@1041
   289
  ///Sum of two maps
alpar@1041
   290
alpar@1041
   291
  ///This \ref concept::ReadMap "read only map" returns the sum of the two
alpar@1041
   292
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   293
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
alpar@1041
   294
deba@1705
   295
  template<typename M1, typename M2> 
deba@1705
   296
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   297
    const M1& m1;
deba@1705
   298
    const M2& m2;
deba@1420
   299
alpar@1041
   300
  public:
deba@1705
   301
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   302
    typedef typename Parent::Key Key;
deba@1675
   303
    typedef typename Parent::Value Value;
alpar@1041
   304
alpar@1041
   305
    ///Constructor
alpar@1041
   306
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   307
    Value operator[](Key k) const {return m1[k]+m2[k];}
alpar@1041
   308
  };
alpar@1041
   309
  
alpar@1041
   310
  ///Returns an \ref AddMap class
alpar@1041
   311
alpar@1041
   312
  ///This function just returns an \ref AddMap class.
alpar@1041
   313
  ///\todo How to call these type of functions?
alpar@1041
   314
  ///
alpar@1041
   315
  ///\relates AddMap
alpar@1041
   316
  ///\todo Wrong scope in Doxygen when \c \\relates is used
deba@1675
   317
  template<typename M1, typename M2> 
deba@1705
   318
  inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
deba@1705
   319
    return AddMap<M1, M2>(m1,m2);
alpar@1041
   320
  }
alpar@1041
   321
alpar@1547
   322
  ///Shift a map with a constant.
alpar@1070
   323
alpar@1070
   324
  ///This \ref concept::ReadMap "read only map" returns the sum of the
alpar@1070
   325
  ///given map and a constant value.
alpar@1070
   326
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   327
  ///
alpar@1070
   328
  ///Actually,
alpar@1070
   329
  ///\code
alpar@1070
   330
  ///  ShiftMap<X> sh(x,v);
alpar@1070
   331
  ///\endcode
alpar@1547
   332
  ///is equivalent with
alpar@1070
   333
  ///\code
alpar@1070
   334
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   335
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1070
   336
  ///\endcode
deba@1705
   337
  template<typename M, typename C = typename M::Value> 
deba@1705
   338
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   339
    const M& m;
deba@1691
   340
    C v;
alpar@1070
   341
  public:
deba@1705
   342
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   343
    typedef typename Parent::Key Key;
deba@1675
   344
    typedef typename Parent::Value Value;
alpar@1070
   345
alpar@1070
   346
    ///Constructor
alpar@1070
   347
alpar@1070
   348
    ///Constructor
alpar@1070
   349
    ///\param _m is the undelying map
alpar@1070
   350
    ///\param _v is the shift value
deba@1691
   351
    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@1691
   352
    Value operator[](Key k) const {return m[k] + v;}
alpar@1070
   353
  };
alpar@1070
   354
  
alpar@1070
   355
  ///Returns an \ref ShiftMap class
alpar@1070
   356
alpar@1070
   357
  ///This function just returns an \ref ShiftMap class.
alpar@1070
   358
  ///\relates ShiftMap
alpar@1070
   359
  ///\todo A better name is required.
deba@1691
   360
  template<typename M, typename C> 
deba@1705
   361
  inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
deba@1705
   362
    return ShiftMap<M, C>(m,v);
alpar@1070
   363
  }
alpar@1070
   364
alpar@1041
   365
  ///Difference of two maps
alpar@1041
   366
alpar@1041
   367
  ///This \ref concept::ReadMap "read only map" returns the difference
alpar@1547
   368
  ///of the values of the two
alpar@1041
   369
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   370
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   371
deba@1705
   372
  template<typename M1, typename M2> 
deba@1705
   373
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   374
    const M1& m1;
deba@1705
   375
    const M2& m2;
alpar@1041
   376
  public:
deba@1705
   377
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   378
    typedef typename Parent::Key Key;
deba@1675
   379
    typedef typename Parent::Value Value;
alpar@1041
   380
alpar@1041
   381
    ///Constructor
alpar@1041
   382
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   383
    Value operator[](Key k) const {return m1[k]-m2[k];}
alpar@1041
   384
  };
alpar@1041
   385
  
alpar@1041
   386
  ///Returns a \ref SubMap class
alpar@1041
   387
alpar@1041
   388
  ///This function just returns a \ref SubMap class.
alpar@1041
   389
  ///
alpar@1041
   390
  ///\relates SubMap
deba@1675
   391
  template<typename M1, typename M2> 
deba@1705
   392
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
deba@1705
   393
    return SubMap<M1, M2>(m1, m2);
alpar@1041
   394
  }
alpar@1041
   395
alpar@1041
   396
  ///Product of two maps
alpar@1041
   397
alpar@1041
   398
  ///This \ref concept::ReadMap "read only map" returns the product of the
alpar@1547
   399
  ///values of the two
alpar@1041
   400
  ///given
alpar@1041
   401
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   402
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   403
deba@1705
   404
  template<typename M1, typename M2> 
deba@1705
   405
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   406
    const M1& m1;
deba@1705
   407
    const M2& m2;
alpar@1041
   408
  public:
deba@1705
   409
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   410
    typedef typename Parent::Key Key;
deba@1675
   411
    typedef typename Parent::Value Value;
alpar@1041
   412
alpar@1041
   413
    ///Constructor
alpar@1041
   414
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   415
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   416
  };
alpar@1041
   417
  
alpar@1041
   418
  ///Returns a \ref MulMap class
alpar@1041
   419
alpar@1041
   420
  ///This function just returns a \ref MulMap class.
alpar@1041
   421
  ///\relates MulMap
deba@1675
   422
  template<typename M1, typename M2> 
deba@1705
   423
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
deba@1705
   424
    return MulMap<M1, M2>(m1,m2);
alpar@1041
   425
  }
alpar@1041
   426
 
alpar@1547
   427
  ///Scales a maps with a constant.
alpar@1070
   428
alpar@1070
   429
  ///This \ref concept::ReadMap "read only map" returns the value of the
deba@1691
   430
  ///given map multiplied from the left side with a constant value.
alpar@1070
   431
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   432
  ///
alpar@1070
   433
  ///Actually,
alpar@1070
   434
  ///\code
alpar@1070
   435
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   436
  ///\endcode
alpar@1547
   437
  ///is equivalent with
alpar@1070
   438
  ///\code
alpar@1070
   439
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   440
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   441
  ///\endcode
deba@1705
   442
  template<typename M, typename C = typename M::Value> 
deba@1705
   443
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   444
    const M& m;
deba@1691
   445
    C v;
alpar@1070
   446
  public:
deba@1705
   447
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   448
    typedef typename Parent::Key Key;
deba@1675
   449
    typedef typename Parent::Value Value;
alpar@1070
   450
alpar@1070
   451
    ///Constructor
alpar@1070
   452
alpar@1070
   453
    ///Constructor
alpar@1070
   454
    ///\param _m is the undelying map
alpar@1070
   455
    ///\param _v is the scaling value
deba@1691
   456
    ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@1691
   457
    Value operator[](Key k) const {return v * m[k];}
alpar@1070
   458
  };
alpar@1070
   459
  
alpar@1070
   460
  ///Returns an \ref ScaleMap class
alpar@1070
   461
alpar@1070
   462
  ///This function just returns an \ref ScaleMap class.
alpar@1070
   463
  ///\relates ScaleMap
alpar@1070
   464
  ///\todo A better name is required.
deba@1691
   465
  template<typename M, typename C> 
deba@1705
   466
  inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
deba@1705
   467
    return ScaleMap<M, C>(m,v);
alpar@1070
   468
  }
alpar@1070
   469
alpar@1041
   470
  ///Quotient of two maps
alpar@1041
   471
alpar@1041
   472
  ///This \ref concept::ReadMap "read only map" returns the quotient of the
alpar@1547
   473
  ///values of the two
alpar@1041
   474
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   475
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   476
deba@1705
   477
  template<typename M1, typename M2> 
deba@1705
   478
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   479
    const M1& m1;
deba@1705
   480
    const M2& m2;
alpar@1041
   481
  public:
deba@1705
   482
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   483
    typedef typename Parent::Key Key;
deba@1675
   484
    typedef typename Parent::Value Value;
alpar@1041
   485
alpar@1041
   486
    ///Constructor
alpar@1041
   487
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   488
    Value operator[](Key k) const {return m1[k]/m2[k];}
alpar@1041
   489
  };
alpar@1041
   490
  
alpar@1041
   491
  ///Returns a \ref DivMap class
alpar@1041
   492
alpar@1041
   493
  ///This function just returns a \ref DivMap class.
alpar@1041
   494
  ///\relates DivMap
deba@1675
   495
  template<typename M1, typename M2> 
deba@1705
   496
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
deba@1705
   497
    return DivMap<M1, M2>(m1,m2);
alpar@1041
   498
  }
alpar@1041
   499
  
alpar@1041
   500
  ///Composition of two maps
alpar@1041
   501
alpar@1041
   502
  ///This \ref concept::ReadMap "read only map" returns the composition of
alpar@1041
   503
  ///two
alpar@1041
   504
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
alpar@1041
   505
  ///of \c M2,
alpar@1041
   506
  ///then for
alpar@1041
   507
  ///\code
deba@1675
   508
  ///  ComposeMap<M1, M2> cm(m1,m2);
alpar@1041
   509
  ///\endcode
alpar@1044
   510
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
alpar@1041
   511
  ///
alpar@1041
   512
  ///Its \c Key is inherited from \c M2 and its \c Value is from
alpar@1041
   513
  ///\c M1.
alpar@1041
   514
  ///The \c M2::Value must be convertible to \c M1::Key.
alpar@1041
   515
  ///\todo Check the requirements.
alpar@1041
   516
deba@1705
   517
  template <typename M1, typename M2> 
deba@1705
   518
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
deba@1705
   519
    const M1& m1;
deba@1705
   520
    const M2& m2;
alpar@1041
   521
  public:
deba@1705
   522
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
deba@1675
   523
    typedef typename Parent::Key Key;
deba@1675
   524
    typedef typename Parent::Value Value;
alpar@1041
   525
alpar@1041
   526
    ///Constructor
alpar@1041
   527
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@1725
   528
    
deba@1725
   529
    typename MapTraits<M1>::ConstReturnValue
deba@1725
   530
    operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   531
  };
alpar@1041
   532
  ///Returns a \ref ComposeMap class
alpar@1041
   533
alpar@1041
   534
  ///This function just returns a \ref ComposeMap class.
alpar@1219
   535
  ///
alpar@1041
   536
  ///\relates ComposeMap
deba@1675
   537
  template <typename M1, typename M2> 
deba@1705
   538
  inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
deba@1705
   539
    return ComposeMap<M1, M2>(m1,m2);
alpar@1041
   540
  }
alpar@1219
   541
  
alpar@1547
   542
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   543
alpar@1547
   544
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   545
  ///
alpar@1219
   546
  ///
alpar@1547
   547
  ///This \ref concept::ReadMap "read only map" takes two maps and a
alpar@1219
   548
  ///binary functor and returns the composition of
alpar@1547
   549
  ///the two
alpar@1219
   550
  ///given maps unsing the functor. 
alpar@1219
   551
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
alpar@1219
   552
  ///and \c f is of \c F,
alpar@1219
   553
  ///then for
alpar@1219
   554
  ///\code
deba@1675
   555
  ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
alpar@1219
   556
  ///\endcode
alpar@1219
   557
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   558
  ///
alpar@1219
   559
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
alpar@1219
   560
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   561
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   562
  ///to \c V.
alpar@1219
   563
  ///\todo Check the requirements.
alpar@1219
   564
deba@1675
   565
  template<typename M1, typename M2, typename F,
deba@1675
   566
	   typename V = typename F::result_type,
deba@1675
   567
	   typename NC = False> 
deba@1705
   568
  class CombineMap : public MapBase<typename M1::Key, V> {
deba@1705
   569
    const M1& m1;
deba@1705
   570
    const M2& m2;
deba@1420
   571
    F f;
alpar@1219
   572
  public:
deba@1705
   573
    typedef MapBase<typename M1::Key, V> Parent;
deba@1675
   574
    typedef typename Parent::Key Key;
deba@1675
   575
    typedef typename Parent::Value Value;
alpar@1219
   576
alpar@1219
   577
    ///Constructor
alpar@1219
   578
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
alpar@1219
   579
      : m1(_m1), m2(_m2), f(_f) {};
alpar@1219
   580
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   581
  };
alpar@1219
   582
  
alpar@1219
   583
  ///Returns a \ref CombineMap class
alpar@1219
   584
alpar@1219
   585
  ///This function just returns a \ref CombineMap class.
alpar@1219
   586
  ///
alpar@1219
   587
  ///Only the first template parameter (the value type) must be given.
alpar@1219
   588
  ///
alpar@1219
   589
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   590
  ///\code
alpar@1219
   591
  ///combineMap<double>(m1,m2,std::plus<double>)
alpar@1219
   592
  ///\endcode
alpar@1219
   593
  ///is equivalent with
alpar@1219
   594
  ///\code
alpar@1219
   595
  ///addMap(m1,m2)
alpar@1219
   596
  ///\endcode
alpar@1219
   597
  ///
alpar@1219
   598
  ///\relates CombineMap
deba@1675
   599
  template<typename M1, typename M2, typename F, typename V> 
deba@1705
   600
  inline CombineMap<M1, M2, F, V> 
deba@1675
   601
  combineMap(const M1& m1,const M2& m2, const F& f) {
deba@1705
   602
    return CombineMap<M1, M2, F, V>(m1,m2,f);
deba@1675
   603
  }
deba@1675
   604
deba@1675
   605
  template<typename M1, typename M2, typename F> 
deba@1705
   606
  inline CombineMap<M1, M2, F, typename F::result_type> 
deba@1675
   607
  combineMap(const M1& m1, const M2& m2, const F& f) {
deba@1675
   608
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
deba@1675
   609
  }
deba@1675
   610
deba@1675
   611
  template<typename M1, typename M2, typename K1, typename K2, typename V> 
deba@1705
   612
  inline CombineMap<M1, M2, V (*)(K1, K2), V> 
deba@1675
   613
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
deba@1675
   614
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
alpar@1219
   615
  }
alpar@1041
   616
alpar@1041
   617
  ///Negative value of a map
alpar@1041
   618
alpar@1041
   619
  ///This \ref concept::ReadMap "read only map" returns the negative
alpar@1041
   620
  ///value of the
alpar@1041
   621
  ///value returned by the
alpar@1041
   622
  ///given map. Its \c Key and \c Value will be inherited from \c M.
alpar@1041
   623
  ///The unary \c - operator must be defined for \c Value, of course.
alpar@1041
   624
deba@1705
   625
  template<typename M> 
deba@1705
   626
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   627
    const M& m;
alpar@1041
   628
  public:
deba@1705
   629
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   630
    typedef typename Parent::Key Key;
deba@1675
   631
    typedef typename Parent::Value Value;
alpar@1041
   632
alpar@1041
   633
    ///Constructor
alpar@1041
   634
    NegMap(const M &_m) : m(_m) {};
alpar@1044
   635
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   636
  };
alpar@1041
   637
  
alpar@1041
   638
  ///Returns a \ref NegMap class
alpar@1041
   639
alpar@1041
   640
  ///This function just returns a \ref NegMap class.
alpar@1041
   641
  ///\relates NegMap
deba@1675
   642
  template <typename M> 
deba@1705
   643
  inline NegMap<M> negMap(const M &m) {
deba@1705
   644
    return NegMap<M>(m);
alpar@1041
   645
  }
alpar@1041
   646
alpar@1041
   647
alpar@1041
   648
  ///Absolute value of a map
alpar@1041
   649
alpar@1041
   650
  ///This \ref concept::ReadMap "read only map" returns the absolute value
alpar@1041
   651
  ///of the
alpar@1041
   652
  ///value returned by the
alpar@1044
   653
  ///given map. Its \c Key and \c Value will be inherited
alpar@1044
   654
  ///from <tt>M</tt>. <tt>Value</tt>
alpar@1044
   655
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
alpar@1044
   656
  ///operator must be defined for it, of course.
alpar@1044
   657
  ///
alpar@1044
   658
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   659
  ///\code
alpar@1044
   660
  ///  struct _UnConvertible {};
alpar@1044
   661
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   662
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   663
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   664
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   665
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   666
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   667
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   668
  ///\endcode
alpar@1044
   669
  
alpar@1041
   670
deba@1705
   671
  template<typename M> 
deba@1705
   672
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   673
    const M& m;
alpar@1041
   674
  public:
deba@1705
   675
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   676
    typedef typename Parent::Key Key;
deba@1675
   677
    typedef typename Parent::Value Value;
alpar@1041
   678
alpar@1041
   679
    ///Constructor
alpar@1041
   680
    AbsMap(const M &_m) : m(_m) {};
deba@1675
   681
    Value operator[](Key k) const {
deba@1675
   682
      Value tmp = m[k]; 
deba@1675
   683
      return tmp >= 0 ? tmp : -tmp;
deba@1675
   684
    }
deba@1675
   685
alpar@1041
   686
  };
alpar@1041
   687
  
alpar@1041
   688
  ///Returns a \ref AbsMap class
alpar@1041
   689
alpar@1041
   690
  ///This function just returns a \ref AbsMap class.
alpar@1041
   691
  ///\relates AbsMap
deba@1675
   692
  template<typename M> 
deba@1705
   693
  inline AbsMap<M> absMap(const M &m) {
deba@1705
   694
    return AbsMap<M>(m);
alpar@1041
   695
  }
alpar@1041
   696
alpar@1402
   697
  ///Converts an STL style functor to a map
alpar@1076
   698
alpar@1076
   699
  ///This \ref concept::ReadMap "read only map" returns the value
alpar@1076
   700
  ///of a
alpar@1076
   701
  ///given map.
alpar@1076
   702
  ///
alpar@1076
   703
  ///Template parameters \c K and \c V will become its
alpar@1076
   704
  ///\c Key and \c Value. They must be given explicitely
alpar@1076
   705
  ///because a functor does not provide such typedefs.
alpar@1076
   706
  ///
alpar@1076
   707
  ///Parameter \c F is the type of the used functor.
alpar@1076
   708
  
alpar@1076
   709
deba@1675
   710
  template<typename F, 
deba@1675
   711
	   typename K = typename F::argument_type, 
deba@1675
   712
	   typename V = typename F::result_type,
deba@1675
   713
	   typename NC = False> 
deba@1705
   714
  class FunctorMap : public MapBase<K, V> {
deba@1679
   715
    F f;
alpar@1076
   716
  public:
deba@1705
   717
    typedef MapBase<K, V> Parent;
deba@1675
   718
    typedef typename Parent::Key Key;
deba@1675
   719
    typedef typename Parent::Value Value;
alpar@1076
   720
alpar@1076
   721
    ///Constructor
deba@1679
   722
    FunctorMap(const F &_f) : f(_f) {}
deba@1679
   723
deba@1679
   724
    Value operator[](Key k) const { return f(k);}
alpar@1076
   725
  };
alpar@1076
   726
  
alpar@1076
   727
  ///Returns a \ref FunctorMap class
alpar@1076
   728
alpar@1076
   729
  ///This function just returns a \ref FunctorMap class.
alpar@1076
   730
  ///
alpar@1076
   731
  ///The third template parameter isn't necessary to be given.
alpar@1076
   732
  ///\relates FunctorMap
deba@1675
   733
  template<typename K, typename V, typename F> inline 
deba@1705
   734
  FunctorMap<F, K, V> functorMap(const F &f) {
deba@1705
   735
    return FunctorMap<F, K, V>(f);
alpar@1076
   736
  }
alpar@1076
   737
deba@1675
   738
  template <typename F> inline 
deba@1705
   739
  FunctorMap<F, typename F::argument_type, typename F::result_type> 
deba@1675
   740
  functorMap(const F &f) {
deba@1679
   741
    return FunctorMap<F, typename F::argument_type, 
deba@1705
   742
      typename F::result_type>(f);
deba@1675
   743
  }
deba@1675
   744
deba@1675
   745
  template <typename K, typename V> inline 
deba@1705
   746
  FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
deba@1705
   747
    return FunctorMap<V (*)(K), K, V>(f);
deba@1675
   748
  }
deba@1675
   749
deba@1675
   750
alpar@1219
   751
  ///Converts a map to an STL style (unary) functor
alpar@1076
   752
alpar@1219
   753
  ///This class Converts a map to an STL style (unary) functor.
alpar@1076
   754
  ///that is it provides an <tt>operator()</tt> to read its values.
alpar@1076
   755
  ///
alpar@1223
   756
  ///For the sake of convenience it also works as
alpar@1537
   757
  ///a ususal \ref concept::ReadMap "readable map",
alpar@1537
   758
  ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
alpar@1076
   759
deba@1705
   760
  template <typename M> 
deba@1705
   761
  class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   762
    const M& m;
alpar@1076
   763
  public:
deba@1705
   764
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   765
    typedef typename Parent::Key Key;
deba@1675
   766
    typedef typename Parent::Value Value;
deba@1420
   767
alpar@1456
   768
    ///\e
alpar@1223
   769
    typedef typename M::Key argument_type;
alpar@1456
   770
    ///\e
alpar@1223
   771
    typedef typename M::Value result_type;
alpar@1076
   772
alpar@1076
   773
    ///Constructor
alpar@1076
   774
    MapFunctor(const M &_m) : m(_m) {};
alpar@1076
   775
    ///Returns a value of the map
alpar@1076
   776
    Value operator()(Key k) const {return m[k];}
alpar@1076
   777
    ///\e
alpar@1076
   778
    Value operator[](Key k) const {return m[k];}
alpar@1076
   779
  };
alpar@1076
   780
  
alpar@1076
   781
  ///Returns a \ref MapFunctor class
alpar@1076
   782
alpar@1076
   783
  ///This function just returns a \ref MapFunctor class.
alpar@1076
   784
  ///\relates MapFunctor
deba@1675
   785
  template<typename M> 
deba@1705
   786
  inline MapFunctor<M> mapFunctor(const M &m) {
deba@1705
   787
    return MapFunctor<M>(m);
alpar@1076
   788
  }
alpar@1076
   789
alpar@1076
   790
alpar@1547
   791
  ///Applies all map setting operations to two maps
alpar@1219
   792
alpar@1219
   793
  ///This map has two \ref concept::WriteMap "writable map"
alpar@1219
   794
  ///parameters and each write request will be passed to both of them.
alpar@1219
   795
  ///If \c M1 is also \ref concept::ReadMap "readable",
alpar@1219
   796
  ///then the read operations will return the
alpar@1317
   797
  ///corresponding values of \c M1.
alpar@1219
   798
  ///
alpar@1219
   799
  ///The \c Key and \c Value will be inherited from \c M1.
alpar@1219
   800
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
alpar@1219
   801
deba@1705
   802
  template<typename  M1, typename M2> 
deba@1705
   803
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   804
    const M1& m1;
deba@1705
   805
    const M2& m2;
alpar@1219
   806
  public:
deba@1705
   807
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   808
    typedef typename Parent::Key Key;
deba@1675
   809
    typedef typename Parent::Value Value;
alpar@1219
   810
alpar@1219
   811
    ///Constructor
alpar@1219
   812
    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1219
   813
    Value operator[](Key k) const {return m1[k];}
deba@1675
   814
    //    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
   815
  };
alpar@1219
   816
  
alpar@1219
   817
  ///Returns an \ref ForkMap class
alpar@1219
   818
alpar@1219
   819
  ///This function just returns an \ref ForkMap class.
alpar@1219
   820
  ///\todo How to call these type of functions?
alpar@1219
   821
  ///
alpar@1219
   822
  ///\relates ForkMap
alpar@1219
   823
  ///\todo Wrong scope in Doxygen when \c \\relates is used
deba@1675
   824
  template <typename M1, typename M2> 
deba@1705
   825
  inline ForkMap<M1, M2> forkMap(const M1 &m1,const M2 &m2) {
deba@1705
   826
    return ForkMap<M1, M2>(m1,m2);
alpar@1219
   827
  }
alpar@1219
   828
alpar@1456
   829
alpar@1456
   830
  
alpar@1456
   831
  /* ************* BOOL MAPS ******************* */
alpar@1456
   832
  
alpar@1456
   833
  ///Logical 'not' of a map
alpar@1456
   834
  
alpar@1456
   835
  ///This bool \ref concept::ReadMap "read only map" returns the 
alpar@1456
   836
  ///logical negation of
alpar@1456
   837
  ///value returned by the
alpar@1456
   838
  ///given map. Its \c Key and will be inherited from \c M,
alpar@1456
   839
  ///its Value is <tt>bool</tt>.
alpar@1456
   840
deba@1705
   841
  template <typename M> 
deba@1705
   842
  class NotMap : public MapBase<typename M::Key, bool> {
deba@1705
   843
    const M& m;
alpar@1456
   844
  public:
deba@1705
   845
    typedef MapBase<typename M::Key, bool> Parent;
deba@1675
   846
    typedef typename Parent::Key Key;
deba@1675
   847
    typedef typename Parent::Value Value;
alpar@1456
   848
deba@1778
   849
    /// Constructor
alpar@1456
   850
    NotMap(const M &_m) : m(_m) {};
alpar@1456
   851
    Value operator[](Key k) const {return !m[k];}
alpar@1456
   852
  };
alpar@1456
   853
  
alpar@1456
   854
  ///Returns a \ref NotMap class
alpar@1456
   855
  
alpar@1456
   856
  ///This function just returns a \ref NotMap class.
alpar@1456
   857
  ///\relates NotMap
deba@1675
   858
  template <typename M> 
deba@1705
   859
  inline NotMap<M> notMap(const M &m) {
deba@1705
   860
    return NotMap<M>(m);
alpar@1456
   861
  }
alpar@1456
   862
alpar@1808
   863
  /// \brief Writable bool map for store each true assigned elements.
deba@1778
   864
  ///
alpar@1808
   865
  /// Writable bool map for store each true assigned elements. It will
deba@1778
   866
  /// copies all the true setted keys to the given iterator.
deba@1778
   867
  ///
deba@1778
   868
  /// \note The container of the iterator should contain for each element.
deba@1778
   869
  template <typename _Iterator>
deba@1778
   870
  class StoreBoolMap {
deba@1778
   871
  public:
deba@1778
   872
    typedef _Iterator Iterator;
deba@1778
   873
deba@1778
   874
    typedef typename std::iterator_traits<Iterator>::value_type Key;
deba@1778
   875
    typedef bool Value;
deba@1778
   876
deba@1778
   877
    /// Constructor
deba@1778
   878
    StoreBoolMap(Iterator it) : _begin(it), _end(it) {}
deba@1778
   879
deba@1778
   880
    /// Gives back the given first setted iterator.
deba@1778
   881
    Iterator begin() const {
deba@1778
   882
      return _begin;
deba@1778
   883
    }
deba@1778
   884
 
deba@1778
   885
    /// Gives back the iterator after the last setted.
deba@1778
   886
    Iterator end() const {
deba@1778
   887
      return _end;
deba@1778
   888
    }
deba@1778
   889
deba@1778
   890
    /// Setter function of the map
deba@1778
   891
    void set(const Key& key, Value value) {
deba@1778
   892
      if (value) {
deba@1778
   893
	*_end++ = key;
deba@1778
   894
      }
deba@1778
   895
    }
deba@1778
   896
    
deba@1778
   897
  private:
deba@1778
   898
    Iterator _begin, _end;
deba@1778
   899
  };
deba@1778
   900
alpar@1808
   901
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
   902
  /// a back insertable container.
deba@1778
   903
  ///
alpar@1808
   904
  /// Writable bool map for store each true assigned elements in a back 
deba@1778
   905
  /// insertable container. It will push back all the true setted keys into
deba@1778
   906
  /// the container.
deba@1778
   907
  template <typename Container>
deba@1778
   908
  class BackInserterBoolMap {
deba@1778
   909
  public:
deba@1778
   910
    typedef typename Container::value_type Key;
deba@1778
   911
    typedef bool Value;
deba@1778
   912
deba@1778
   913
    /// Constructor
deba@1778
   914
    BackInserterBoolMap(Container& _container) : container(_container) {}
deba@1778
   915
deba@1778
   916
    /// Setter function of the map
deba@1778
   917
    void set(const Key& key, Value value) {
deba@1778
   918
      if (value) {
deba@1778
   919
	container.push_back(key);
deba@1778
   920
      }
deba@1778
   921
    }
deba@1778
   922
    
deba@1778
   923
  private:
deba@1778
   924
    Container& container;    
deba@1778
   925
  };
deba@1778
   926
alpar@1808
   927
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
   928
  /// a front insertable container.
deba@1778
   929
  ///
alpar@1808
   930
  /// Writable bool map for store each true assigned elements in a front 
deba@1778
   931
  /// insertable container. It will push front all the true setted keys into
deba@1778
   932
  /// the container.
deba@1778
   933
  template <typename Container>
deba@1778
   934
  class FrontInserterBoolMap {
deba@1778
   935
  public:
deba@1778
   936
    typedef typename Container::value_type Key;
deba@1778
   937
    typedef bool Value;
deba@1778
   938
deba@1778
   939
    /// Constructor
deba@1778
   940
    FrontInserterBoolMap(Container& _container) : container(_container) {}
deba@1778
   941
deba@1778
   942
    /// Setter function of the map
deba@1778
   943
    void set(const Key& key, Value value) {
deba@1778
   944
      if (value) {
deba@1778
   945
	container.push_front(key);
deba@1778
   946
      }
deba@1778
   947
    }
deba@1778
   948
    
deba@1778
   949
  private:
deba@1778
   950
    Container& container;    
deba@1778
   951
  };
deba@1778
   952
alpar@1808
   953
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
   954
  /// an insertable container.
deba@1778
   955
  ///
alpar@1808
   956
  /// Writable bool map for store each true assigned elements in an 
deba@1778
   957
  /// insertable container. It will insert all the true setted keys into
deba@1778
   958
  /// the container.
deba@1778
   959
  template <typename Container>
deba@1778
   960
  class InserterBoolMap {
deba@1778
   961
  public:
deba@1778
   962
    typedef typename Container::value_type Key;
deba@1778
   963
    typedef bool Value;
deba@1778
   964
deba@1778
   965
    /// Constructor
deba@1778
   966
    InserterBoolMap(Container& _container) : container(_container) {}
deba@1778
   967
deba@1778
   968
    /// Setter function of the map
deba@1778
   969
    void set(const Key& key, Value value) {
deba@1778
   970
      if (value) {
deba@1778
   971
	container.insert(key);
deba@1778
   972
      }
deba@1778
   973
    }
deba@1778
   974
    
deba@1778
   975
  private:
deba@1778
   976
    Container& container;    
deba@1778
   977
  };
deba@1778
   978
deba@1778
   979
  /// \brief Fill the true setted elements with a given value.
deba@1778
   980
  ///
alpar@1808
   981
  /// Writable bool map for fill the true setted elements with a given value.
deba@1778
   982
  /// The value can be setted 
deba@1778
   983
  /// the container.
deba@1778
   984
  template <typename Map>
deba@1778
   985
  class FillBoolMap {
deba@1778
   986
  public:
deba@1778
   987
    typedef typename Map::Key Key;
deba@1778
   988
    typedef bool Value;
deba@1778
   989
deba@1778
   990
    /// Constructor
deba@1778
   991
    FillBoolMap(Map& _map, const typename Map::Value& _fill) 
deba@1778
   992
      : map(_map), fill(_fill) {}
deba@1778
   993
deba@1778
   994
    /// Constructor
deba@1778
   995
    FillBoolMap(Map& _map) 
deba@1778
   996
      : map(_map), fill() {}
deba@1778
   997
deba@1778
   998
    /// Gives back the current fill value
deba@1778
   999
    typename Map::Value fillValue() const {
deba@1778
  1000
      return fill;
deba@1778
  1001
    } 
deba@1778
  1002
deba@1778
  1003
    /// Sets the current fill value
deba@1778
  1004
    void fillValue(const typename Map::Value& _fill) {
deba@1778
  1005
      fill = _fill;
deba@1778
  1006
    } 
deba@1778
  1007
deba@1778
  1008
    /// Setter function of the map
deba@1778
  1009
    void set(const Key& key, Value value) {
deba@1778
  1010
      if (value) {
deba@1778
  1011
	map.set(key, fill);
deba@1778
  1012
      }
deba@1778
  1013
    }
deba@1778
  1014
    
deba@1778
  1015
  private:
deba@1778
  1016
    Map& map;
deba@1778
  1017
    typename Map::Value fill;
deba@1778
  1018
  };
deba@1778
  1019
deba@1778
  1020
alpar@1808
  1021
  /// \brief Writable bool map which stores for each true assigned elements  
deba@1778
  1022
  /// the setting order number.
deba@1778
  1023
  ///
alpar@1808
  1024
  /// Writable bool map which stores for each true assigned elements  
deba@1778
  1025
  /// the setting order number.
deba@1778
  1026
  template <typename Map>
deba@1778
  1027
  class SettingOrderBoolMap {
deba@1778
  1028
  public:
deba@1778
  1029
    typedef typename Map::Key Key;
deba@1778
  1030
    typedef bool Value;
deba@1778
  1031
deba@1778
  1032
    /// Constructor
deba@1778
  1033
    SettingOrderBoolMap(Map& _map) 
deba@1778
  1034
      : map(_map), counter(0) {}
deba@1778
  1035
deba@1778
  1036
    /// Number of setted keys.
deba@1778
  1037
    int num() const {
deba@1778
  1038
      return counter;
deba@1778
  1039
    }
deba@1778
  1040
deba@1778
  1041
    /// Setter function of the map
deba@1778
  1042
    void set(const Key& key, Value value) {
deba@1778
  1043
      if (value) {
deba@1778
  1044
	map.set(key, counter++);
deba@1778
  1045
      }
deba@1778
  1046
    }
deba@1778
  1047
    
deba@1778
  1048
  private:
deba@1778
  1049
    Map& map;
deba@1778
  1050
    int counter;
deba@1778
  1051
  };
deba@1778
  1052
alpar@1041
  1053
  /// @}
klao@286
  1054
}
alpar@1041
  1055
alpar@921
  1056
#endif // LEMON_MAPS_H