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