lemon/maps.h
author deba
Fri, 12 May 2006 09:56:14 +0000
changeset 2079 7fe378247fea
parent 1993 2115143eceea
child 2080 630a5e16dc12
permissions -rw-r--r--
Remade SplitGraphAdaptor
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
  };
deba@2032
   354
deba@2032
   355
  ///Shift a map with a constant.
deba@2032
   356
deba@2032
   357
  ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the
deba@2032
   358
  ///given map and a constant value. It makes also possible to write the map.
deba@2032
   359
  ///Its \c Key and \c Value is inherited from \c M.
deba@2032
   360
  ///
deba@2032
   361
  ///Actually,
deba@2032
   362
  ///\code
deba@2032
   363
  ///  ShiftMap<X> sh(x,v);
deba@2032
   364
  ///\endcode
deba@2032
   365
  ///is equivalent with
deba@2032
   366
  ///\code
deba@2032
   367
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
deba@2032
   368
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
deba@2032
   369
  ///\endcode
deba@2032
   370
  template<typename M, typename C = typename M::Value> 
deba@2032
   371
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   372
    M& m;
deba@2032
   373
    C v;
deba@2032
   374
  public:
deba@2032
   375
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   376
    typedef typename Parent::Key Key;
deba@2032
   377
    typedef typename Parent::Value Value;
deba@2032
   378
deba@2032
   379
    ///Constructor
deba@2032
   380
deba@2032
   381
    ///Constructor
deba@2032
   382
    ///\param _m is the undelying map
deba@2032
   383
    ///\param _v is the shift value
deba@2032
   384
    ShiftWriteMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2032
   385
    Value operator[](Key k) const {return m[k] + v;}
deba@2032
   386
    void set(Key k, const Value& c) { m.set(k, c - v); }
deba@2032
   387
  };
alpar@1070
   388
  
alpar@1070
   389
  ///Returns an \ref ShiftMap class
alpar@1070
   390
alpar@1070
   391
  ///This function just returns an \ref ShiftMap class.
alpar@1070
   392
  ///\relates ShiftMap
alpar@1070
   393
  ///\todo A better name is required.
deba@1691
   394
  template<typename M, typename C> 
deba@1705
   395
  inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
deba@1705
   396
    return ShiftMap<M, C>(m,v);
alpar@1070
   397
  }
alpar@1070
   398
deba@2032
   399
  template<typename M, typename C> 
deba@2032
   400
  inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
deba@2032
   401
    return ShiftWriteMap<M, C>(m,v);
deba@2032
   402
  }
deba@2032
   403
alpar@1041
   404
  ///Difference of two maps
alpar@1041
   405
alpar@1041
   406
  ///This \ref concept::ReadMap "read only map" returns the difference
alpar@1547
   407
  ///of the values of the two
alpar@1041
   408
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   409
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   410
deba@1705
   411
  template<typename M1, typename M2> 
deba@1705
   412
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   413
    const M1& m1;
deba@1705
   414
    const M2& m2;
alpar@1041
   415
  public:
deba@1705
   416
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   417
    typedef typename Parent::Key Key;
deba@1675
   418
    typedef typename Parent::Value Value;
alpar@1041
   419
alpar@1041
   420
    ///Constructor
alpar@1041
   421
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   422
    Value operator[](Key k) const {return m1[k]-m2[k];}
alpar@1041
   423
  };
alpar@1041
   424
  
alpar@1041
   425
  ///Returns a \ref SubMap class
alpar@1041
   426
alpar@1041
   427
  ///This function just returns a \ref SubMap class.
alpar@1041
   428
  ///
alpar@1041
   429
  ///\relates SubMap
deba@1675
   430
  template<typename M1, typename M2> 
deba@1705
   431
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
deba@1705
   432
    return SubMap<M1, M2>(m1, m2);
alpar@1041
   433
  }
alpar@1041
   434
alpar@1041
   435
  ///Product of two maps
alpar@1041
   436
alpar@1041
   437
  ///This \ref concept::ReadMap "read only map" returns the product of the
alpar@1547
   438
  ///values of the two
alpar@1041
   439
  ///given
alpar@1041
   440
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   441
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   442
deba@1705
   443
  template<typename M1, typename M2> 
deba@1705
   444
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   445
    const M1& m1;
deba@1705
   446
    const M2& m2;
alpar@1041
   447
  public:
deba@1705
   448
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   449
    typedef typename Parent::Key Key;
deba@1675
   450
    typedef typename Parent::Value Value;
alpar@1041
   451
alpar@1041
   452
    ///Constructor
alpar@1041
   453
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   454
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   455
  };
alpar@1041
   456
  
alpar@1041
   457
  ///Returns a \ref MulMap class
alpar@1041
   458
alpar@1041
   459
  ///This function just returns a \ref MulMap class.
alpar@1041
   460
  ///\relates MulMap
deba@1675
   461
  template<typename M1, typename M2> 
deba@1705
   462
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
deba@1705
   463
    return MulMap<M1, M2>(m1,m2);
alpar@1041
   464
  }
alpar@1041
   465
 
alpar@1547
   466
  ///Scales a maps with a constant.
alpar@1070
   467
alpar@1070
   468
  ///This \ref concept::ReadMap "read only map" returns the value of the
deba@1691
   469
  ///given map multiplied from the left side with a constant value.
alpar@1070
   470
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   471
  ///
alpar@1070
   472
  ///Actually,
alpar@1070
   473
  ///\code
alpar@1070
   474
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   475
  ///\endcode
alpar@1547
   476
  ///is equivalent with
alpar@1070
   477
  ///\code
alpar@1070
   478
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   479
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   480
  ///\endcode
deba@1705
   481
  template<typename M, typename C = typename M::Value> 
deba@1705
   482
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   483
    const M& m;
deba@1691
   484
    C v;
alpar@1070
   485
  public:
deba@1705
   486
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   487
    typedef typename Parent::Key Key;
deba@1675
   488
    typedef typename Parent::Value Value;
alpar@1070
   489
alpar@1070
   490
    ///Constructor
alpar@1070
   491
alpar@1070
   492
    ///Constructor
alpar@1070
   493
    ///\param _m is the undelying map
alpar@1070
   494
    ///\param _v is the scaling value
deba@1691
   495
    ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@1691
   496
    Value operator[](Key k) const {return v * m[k];}
alpar@1070
   497
  };
deba@2032
   498
deba@2032
   499
  ///Scales a maps with a constant.
deba@2032
   500
deba@2032
   501
  ///This \ref concept::ReadWriteMap "read-write map" returns the value of the
deba@2032
   502
  ///given map multiplied from the left side with a constant value. It can
deba@2032
   503
  ///be used as write map also if the given multiplier is not zero.
deba@2032
   504
  ///Its \c Key and \c Value is inherited from \c M.
deba@2032
   505
  template<typename M, typename C = typename M::Value> 
deba@2032
   506
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   507
    M& m;
deba@2032
   508
    C v;
deba@2032
   509
  public:
deba@2032
   510
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   511
    typedef typename Parent::Key Key;
deba@2032
   512
    typedef typename Parent::Value Value;
deba@2032
   513
deba@2032
   514
    ///Constructor
deba@2032
   515
deba@2032
   516
    ///Constructor
deba@2032
   517
    ///\param _m is the undelying map
deba@2032
   518
    ///\param _v is the scaling value
deba@2032
   519
    ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2032
   520
    Value operator[](Key k) const {return v * m[k];}
deba@2032
   521
    void set(Key k, const Value& c) { m.set(k, c / v);}
deba@2032
   522
  };
alpar@1070
   523
  
alpar@1070
   524
  ///Returns an \ref ScaleMap class
alpar@1070
   525
alpar@1070
   526
  ///This function just returns an \ref ScaleMap class.
alpar@1070
   527
  ///\relates ScaleMap
alpar@1070
   528
  ///\todo A better name is required.
deba@1691
   529
  template<typename M, typename C> 
deba@1705
   530
  inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
deba@1705
   531
    return ScaleMap<M, C>(m,v);
alpar@1070
   532
  }
alpar@1070
   533
deba@2032
   534
  template<typename M, typename C> 
deba@2032
   535
  inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
deba@2032
   536
    return ScaleWriteMap<M, C>(m,v);
deba@2032
   537
  }
deba@2032
   538
alpar@1041
   539
  ///Quotient of two maps
alpar@1041
   540
alpar@1041
   541
  ///This \ref concept::ReadMap "read only map" returns the quotient of the
alpar@1547
   542
  ///values of the two
alpar@1041
   543
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   544
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   545
deba@1705
   546
  template<typename M1, typename M2> 
deba@1705
   547
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   548
    const M1& m1;
deba@1705
   549
    const M2& m2;
alpar@1041
   550
  public:
deba@1705
   551
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   552
    typedef typename Parent::Key Key;
deba@1675
   553
    typedef typename Parent::Value Value;
alpar@1041
   554
alpar@1041
   555
    ///Constructor
alpar@1041
   556
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   557
    Value operator[](Key k) const {return m1[k]/m2[k];}
alpar@1041
   558
  };
alpar@1041
   559
  
alpar@1041
   560
  ///Returns a \ref DivMap class
alpar@1041
   561
alpar@1041
   562
  ///This function just returns a \ref DivMap class.
alpar@1041
   563
  ///\relates DivMap
deba@1675
   564
  template<typename M1, typename M2> 
deba@1705
   565
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
deba@1705
   566
    return DivMap<M1, M2>(m1,m2);
alpar@1041
   567
  }
alpar@1041
   568
  
alpar@1041
   569
  ///Composition of two maps
alpar@1041
   570
alpar@1041
   571
  ///This \ref concept::ReadMap "read only map" returns the composition of
alpar@1041
   572
  ///two
alpar@1041
   573
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
alpar@1041
   574
  ///of \c M2,
alpar@1041
   575
  ///then for
alpar@1041
   576
  ///\code
deba@1675
   577
  ///  ComposeMap<M1, M2> cm(m1,m2);
alpar@1041
   578
  ///\endcode
alpar@1044
   579
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
alpar@1041
   580
  ///
alpar@1041
   581
  ///Its \c Key is inherited from \c M2 and its \c Value is from
alpar@1041
   582
  ///\c M1.
alpar@1041
   583
  ///The \c M2::Value must be convertible to \c M1::Key.
alpar@1041
   584
  ///\todo Check the requirements.
alpar@1041
   585
deba@1705
   586
  template <typename M1, typename M2> 
deba@1705
   587
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
deba@1705
   588
    const M1& m1;
deba@1705
   589
    const M2& m2;
alpar@1041
   590
  public:
deba@1705
   591
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
deba@1675
   592
    typedef typename Parent::Key Key;
deba@1675
   593
    typedef typename Parent::Value Value;
alpar@1041
   594
alpar@1041
   595
    ///Constructor
alpar@1041
   596
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@1725
   597
    
deba@1725
   598
    typename MapTraits<M1>::ConstReturnValue
deba@1725
   599
    operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   600
  };
alpar@1041
   601
  ///Returns a \ref ComposeMap class
alpar@1041
   602
alpar@1041
   603
  ///This function just returns a \ref ComposeMap class.
alpar@1219
   604
  ///
alpar@1041
   605
  ///\relates ComposeMap
deba@1675
   606
  template <typename M1, typename M2> 
deba@1705
   607
  inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
deba@1705
   608
    return ComposeMap<M1, M2>(m1,m2);
alpar@1041
   609
  }
alpar@1219
   610
  
alpar@1547
   611
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   612
alpar@1547
   613
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   614
  ///
alpar@1219
   615
  ///
alpar@1547
   616
  ///This \ref concept::ReadMap "read only map" takes two maps and a
alpar@1219
   617
  ///binary functor and returns the composition of
alpar@1547
   618
  ///the two
alpar@1219
   619
  ///given maps unsing the functor. 
alpar@1219
   620
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
alpar@1219
   621
  ///and \c f is of \c F,
alpar@1219
   622
  ///then for
alpar@1219
   623
  ///\code
deba@1675
   624
  ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
alpar@1219
   625
  ///\endcode
alpar@1219
   626
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   627
  ///
alpar@1219
   628
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
alpar@1219
   629
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   630
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   631
  ///to \c V.
alpar@1219
   632
  ///\todo Check the requirements.
alpar@1219
   633
deba@1675
   634
  template<typename M1, typename M2, typename F,
deba@1675
   635
	   typename V = typename F::result_type,
deba@1675
   636
	   typename NC = False> 
deba@1705
   637
  class CombineMap : public MapBase<typename M1::Key, V> {
deba@1705
   638
    const M1& m1;
deba@1705
   639
    const M2& m2;
deba@1420
   640
    F f;
alpar@1219
   641
  public:
deba@1705
   642
    typedef MapBase<typename M1::Key, V> Parent;
deba@1675
   643
    typedef typename Parent::Key Key;
deba@1675
   644
    typedef typename Parent::Value Value;
alpar@1219
   645
alpar@1219
   646
    ///Constructor
alpar@1219
   647
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
alpar@1219
   648
      : m1(_m1), m2(_m2), f(_f) {};
alpar@1219
   649
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   650
  };
alpar@1219
   651
  
alpar@1219
   652
  ///Returns a \ref CombineMap class
alpar@1219
   653
alpar@1219
   654
  ///This function just returns a \ref CombineMap class.
alpar@1219
   655
  ///
alpar@1219
   656
  ///Only the first template parameter (the value type) must be given.
alpar@1219
   657
  ///
alpar@1219
   658
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   659
  ///\code
alpar@1219
   660
  ///combineMap<double>(m1,m2,std::plus<double>)
alpar@1219
   661
  ///\endcode
alpar@1219
   662
  ///is equivalent with
alpar@1219
   663
  ///\code
alpar@1219
   664
  ///addMap(m1,m2)
alpar@1219
   665
  ///\endcode
alpar@1219
   666
  ///
alpar@1219
   667
  ///\relates CombineMap
deba@1675
   668
  template<typename M1, typename M2, typename F, typename V> 
deba@1705
   669
  inline CombineMap<M1, M2, F, V> 
deba@1675
   670
  combineMap(const M1& m1,const M2& m2, const F& f) {
deba@1705
   671
    return CombineMap<M1, M2, F, V>(m1,m2,f);
deba@1675
   672
  }
deba@1675
   673
deba@1675
   674
  template<typename M1, typename M2, typename F> 
deba@1705
   675
  inline CombineMap<M1, M2, F, typename F::result_type> 
deba@1675
   676
  combineMap(const M1& m1, const M2& m2, const F& f) {
deba@1675
   677
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
deba@1675
   678
  }
deba@1675
   679
deba@1675
   680
  template<typename M1, typename M2, typename K1, typename K2, typename V> 
deba@1705
   681
  inline CombineMap<M1, M2, V (*)(K1, K2), V> 
deba@1675
   682
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
deba@1675
   683
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
alpar@1219
   684
  }
alpar@1041
   685
alpar@1041
   686
  ///Negative value of a map
alpar@1041
   687
alpar@1041
   688
  ///This \ref concept::ReadMap "read only map" returns the negative
alpar@1041
   689
  ///value of the
alpar@1041
   690
  ///value returned by the
alpar@1041
   691
  ///given map. Its \c Key and \c Value will be inherited from \c M.
alpar@1041
   692
  ///The unary \c - operator must be defined for \c Value, of course.
alpar@1041
   693
deba@1705
   694
  template<typename M> 
deba@1705
   695
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   696
    const M& m;
alpar@1041
   697
  public:
deba@1705
   698
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   699
    typedef typename Parent::Key Key;
deba@1675
   700
    typedef typename Parent::Value Value;
alpar@1041
   701
alpar@1041
   702
    ///Constructor
alpar@1041
   703
    NegMap(const M &_m) : m(_m) {};
alpar@1044
   704
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   705
  };
alpar@1041
   706
  
deba@2032
   707
  ///Negative value of a map
deba@2032
   708
deba@2032
   709
  ///This \ref concept::ReadWriteMap "read-write map" returns the negative
deba@2032
   710
  ///value of the value returned by the
deba@2032
   711
  ///given map. Its \c Key and \c Value will be inherited from \c M.
deba@2032
   712
  ///The unary \c - operator must be defined for \c Value, of course.
deba@2032
   713
deba@2032
   714
  template<typename M> 
deba@2032
   715
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   716
    M& m;
deba@2032
   717
  public:
deba@2032
   718
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   719
    typedef typename Parent::Key Key;
deba@2032
   720
    typedef typename Parent::Value Value;
deba@2032
   721
deba@2032
   722
    ///Constructor
deba@2032
   723
    NegWriteMap(M &_m) : m(_m) {};
deba@2032
   724
    Value operator[](Key k) const {return -m[k];}
deba@2032
   725
    void set(Key k, const Value& v) { m.set(k, -v); }
deba@2032
   726
  };
deba@2032
   727
alpar@1041
   728
  ///Returns a \ref NegMap class
alpar@1041
   729
alpar@1041
   730
  ///This function just returns a \ref NegMap class.
alpar@1041
   731
  ///\relates NegMap
deba@1675
   732
  template <typename M> 
deba@1705
   733
  inline NegMap<M> negMap(const M &m) {
deba@1705
   734
    return NegMap<M>(m);
alpar@1041
   735
  }
alpar@1041
   736
deba@2032
   737
  template <typename M> 
deba@2032
   738
  inline NegWriteMap<M> negMap(M &m) {
deba@2032
   739
    return NegWriteMap<M>(m);
deba@2032
   740
  }
alpar@1041
   741
alpar@1041
   742
  ///Absolute value of a map
alpar@1041
   743
alpar@1041
   744
  ///This \ref concept::ReadMap "read only map" returns the absolute value
alpar@1041
   745
  ///of the
alpar@1041
   746
  ///value returned by the
alpar@1044
   747
  ///given map. Its \c Key and \c Value will be inherited
alpar@1044
   748
  ///from <tt>M</tt>. <tt>Value</tt>
alpar@1044
   749
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
alpar@1044
   750
  ///operator must be defined for it, of course.
alpar@1044
   751
  ///
alpar@1044
   752
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   753
  ///\code
alpar@1044
   754
  ///  struct _UnConvertible {};
alpar@1044
   755
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   756
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   757
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   758
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   759
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   760
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   761
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   762
  ///\endcode
alpar@1044
   763
  
alpar@1041
   764
deba@1705
   765
  template<typename M> 
deba@1705
   766
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   767
    const M& m;
alpar@1041
   768
  public:
deba@1705
   769
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   770
    typedef typename Parent::Key Key;
deba@1675
   771
    typedef typename Parent::Value Value;
alpar@1041
   772
alpar@1041
   773
    ///Constructor
alpar@1041
   774
    AbsMap(const M &_m) : m(_m) {};
deba@1675
   775
    Value operator[](Key k) const {
deba@1675
   776
      Value tmp = m[k]; 
deba@1675
   777
      return tmp >= 0 ? tmp : -tmp;
deba@1675
   778
    }
deba@1675
   779
alpar@1041
   780
  };
alpar@1041
   781
  
alpar@1041
   782
  ///Returns a \ref AbsMap class
alpar@1041
   783
alpar@1041
   784
  ///This function just returns a \ref AbsMap class.
alpar@1041
   785
  ///\relates AbsMap
deba@1675
   786
  template<typename M> 
deba@1705
   787
  inline AbsMap<M> absMap(const M &m) {
deba@1705
   788
    return AbsMap<M>(m);
alpar@1041
   789
  }
alpar@1041
   790
alpar@1402
   791
  ///Converts an STL style functor to a map
alpar@1076
   792
alpar@1076
   793
  ///This \ref concept::ReadMap "read only map" returns the value
alpar@1076
   794
  ///of a
alpar@1076
   795
  ///given map.
alpar@1076
   796
  ///
alpar@1076
   797
  ///Template parameters \c K and \c V will become its
alpar@1076
   798
  ///\c Key and \c Value. They must be given explicitely
alpar@1076
   799
  ///because a functor does not provide such typedefs.
alpar@1076
   800
  ///
alpar@1076
   801
  ///Parameter \c F is the type of the used functor.
alpar@1076
   802
  
alpar@1076
   803
deba@1675
   804
  template<typename F, 
deba@1675
   805
	   typename K = typename F::argument_type, 
deba@1675
   806
	   typename V = typename F::result_type,
deba@1675
   807
	   typename NC = False> 
deba@1705
   808
  class FunctorMap : public MapBase<K, V> {
deba@1679
   809
    F f;
alpar@1076
   810
  public:
deba@1705
   811
    typedef MapBase<K, V> Parent;
deba@1675
   812
    typedef typename Parent::Key Key;
deba@1675
   813
    typedef typename Parent::Value Value;
alpar@1076
   814
alpar@1076
   815
    ///Constructor
deba@1679
   816
    FunctorMap(const F &_f) : f(_f) {}
deba@1679
   817
deba@1679
   818
    Value operator[](Key k) const { return f(k);}
alpar@1076
   819
  };
alpar@1076
   820
  
alpar@1076
   821
  ///Returns a \ref FunctorMap class
alpar@1076
   822
alpar@1076
   823
  ///This function just returns a \ref FunctorMap class.
alpar@1076
   824
  ///
alpar@1076
   825
  ///The third template parameter isn't necessary to be given.
alpar@1076
   826
  ///\relates FunctorMap
deba@1675
   827
  template<typename K, typename V, typename F> inline 
deba@1705
   828
  FunctorMap<F, K, V> functorMap(const F &f) {
deba@1705
   829
    return FunctorMap<F, K, V>(f);
alpar@1076
   830
  }
alpar@1076
   831
deba@1675
   832
  template <typename F> inline 
deba@1705
   833
  FunctorMap<F, typename F::argument_type, typename F::result_type> 
deba@1675
   834
  functorMap(const F &f) {
deba@1679
   835
    return FunctorMap<F, typename F::argument_type, 
deba@1705
   836
      typename F::result_type>(f);
deba@1675
   837
  }
deba@1675
   838
deba@1675
   839
  template <typename K, typename V> inline 
deba@1705
   840
  FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
deba@1705
   841
    return FunctorMap<V (*)(K), K, V>(f);
deba@1675
   842
  }
deba@1675
   843
deba@1675
   844
alpar@1219
   845
  ///Converts a map to an STL style (unary) functor
alpar@1076
   846
alpar@1219
   847
  ///This class Converts a map to an STL style (unary) functor.
alpar@1076
   848
  ///that is it provides an <tt>operator()</tt> to read its values.
alpar@1076
   849
  ///
alpar@1223
   850
  ///For the sake of convenience it also works as
alpar@1537
   851
  ///a ususal \ref concept::ReadMap "readable map",
alpar@1537
   852
  ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
alpar@1076
   853
deba@1705
   854
  template <typename M> 
deba@1705
   855
  class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   856
    const M& m;
alpar@1076
   857
  public:
deba@1705
   858
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   859
    typedef typename Parent::Key Key;
deba@1675
   860
    typedef typename Parent::Value Value;
deba@1420
   861
alpar@1456
   862
    ///\e
alpar@1223
   863
    typedef typename M::Key argument_type;
alpar@1456
   864
    ///\e
alpar@1223
   865
    typedef typename M::Value result_type;
alpar@1076
   866
alpar@1076
   867
    ///Constructor
alpar@1076
   868
    MapFunctor(const M &_m) : m(_m) {};
alpar@1076
   869
    ///Returns a value of the map
alpar@1076
   870
    Value operator()(Key k) const {return m[k];}
alpar@1076
   871
    ///\e
alpar@1076
   872
    Value operator[](Key k) const {return m[k];}
alpar@1076
   873
  };
alpar@1076
   874
  
alpar@1076
   875
  ///Returns a \ref MapFunctor class
alpar@1076
   876
alpar@1076
   877
  ///This function just returns a \ref MapFunctor class.
alpar@1076
   878
  ///\relates MapFunctor
deba@1675
   879
  template<typename M> 
deba@1705
   880
  inline MapFunctor<M> mapFunctor(const M &m) {
deba@1705
   881
    return MapFunctor<M>(m);
alpar@1076
   882
  }
alpar@1076
   883
alpar@1547
   884
  ///Applies all map setting operations to two maps
alpar@1219
   885
deba@2032
   886
  ///This map has two \ref concept::ReadMap "readable map"
deba@2032
   887
  ///parameters and each read request will be passed just to the
deba@2032
   888
  ///first map. This class is the just readable map type of the ForkWriteMap.
alpar@1219
   889
  ///
alpar@1219
   890
  ///The \c Key and \c Value will be inherited from \c M1.
alpar@1219
   891
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
alpar@1219
   892
deba@1705
   893
  template<typename  M1, typename M2> 
deba@1705
   894
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   895
    const M1& m1;
deba@1705
   896
    const M2& m2;
alpar@1219
   897
  public:
deba@1705
   898
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   899
    typedef typename Parent::Key Key;
deba@1675
   900
    typedef typename Parent::Value Value;
alpar@1219
   901
alpar@1219
   902
    ///Constructor
deba@2032
   903
    ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1219
   904
    Value operator[](Key k) const {return m1[k];}
deba@2032
   905
  };
deba@2032
   906
deba@2032
   907
deba@2032
   908
  ///Applies all map setting operations to two maps
deba@2032
   909
deba@2032
   910
  ///This map has two \ref concept::WriteMap "writable map"
deba@2032
   911
  ///parameters and each write request will be passed to both of them.
deba@2032
   912
  ///If \c M1 is also \ref concept::ReadMap "readable",
deba@2032
   913
  ///then the read operations will return the
deba@2032
   914
  ///corresponding values of \c M1.
deba@2032
   915
  ///
deba@2032
   916
  ///The \c Key and \c Value will be inherited from \c M1.
deba@2032
   917
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
deba@2032
   918
deba@2032
   919
  template<typename  M1, typename M2> 
deba@2032
   920
  class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@2032
   921
    M1& m1;
deba@2032
   922
    M2& m2;
deba@2032
   923
  public:
deba@2032
   924
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@2032
   925
    typedef typename Parent::Key Key;
deba@2032
   926
    typedef typename Parent::Value Value;
deba@2032
   927
deba@2032
   928
    ///Constructor
deba@2032
   929
    ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2032
   930
    Value operator[](Key k) const {return m1[k];}
deba@2032
   931
    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
   932
  };
alpar@1219
   933
  
alpar@1219
   934
  ///Returns an \ref ForkMap class
alpar@1219
   935
alpar@1219
   936
  ///This function just returns an \ref ForkMap class.
alpar@1219
   937
  ///\todo How to call these type of functions?
alpar@1219
   938
  ///
alpar@1219
   939
  ///\relates ForkMap
alpar@1219
   940
  ///\todo Wrong scope in Doxygen when \c \\relates is used
deba@1675
   941
  template <typename M1, typename M2> 
deba@2032
   942
  inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
deba@1705
   943
    return ForkMap<M1, M2>(m1,m2);
alpar@1219
   944
  }
alpar@1219
   945
deba@2032
   946
  template <typename M1, typename M2> 
deba@2032
   947
  inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
deba@2032
   948
    return ForkWriteMap<M1, M2>(m1,m2);
deba@2032
   949
  }
deba@2032
   950
alpar@1456
   951
alpar@1456
   952
  
alpar@1456
   953
  /* ************* BOOL MAPS ******************* */
alpar@1456
   954
  
alpar@1456
   955
  ///Logical 'not' of a map
alpar@1456
   956
  
alpar@1456
   957
  ///This bool \ref concept::ReadMap "read only map" returns the 
alpar@1456
   958
  ///logical negation of
alpar@1456
   959
  ///value returned by the
alpar@1456
   960
  ///given map. Its \c Key and will be inherited from \c M,
alpar@1456
   961
  ///its Value is <tt>bool</tt>.
alpar@1456
   962
deba@1705
   963
  template <typename M> 
deba@1705
   964
  class NotMap : public MapBase<typename M::Key, bool> {
deba@1705
   965
    const M& m;
alpar@1456
   966
  public:
deba@1705
   967
    typedef MapBase<typename M::Key, bool> Parent;
deba@1675
   968
    typedef typename Parent::Key Key;
deba@1675
   969
    typedef typename Parent::Value Value;
alpar@1456
   970
deba@1778
   971
    /// Constructor
alpar@1456
   972
    NotMap(const M &_m) : m(_m) {};
alpar@1456
   973
    Value operator[](Key k) const {return !m[k];}
alpar@1456
   974
  };
deba@2032
   975
deba@2032
   976
  ///Logical 'not' of a map with writing possibility
deba@2032
   977
  
deba@2032
   978
  ///This bool \ref concept::ReadWriteMap "read-write map" returns the 
deba@2032
   979
  ///logical negation of value returned by the given map. It is setted
deba@2032
   980
  ///then the negation of the value be setted to the original map.
deba@2032
   981
  ///Its \c Key and will be inherited from \c M,
deba@2032
   982
  ///its Value is <tt>bool</tt>.
deba@2032
   983
  template <typename M> 
deba@2032
   984
  class NotWriteMap : public MapBase<typename M::Key, bool> {
deba@2032
   985
    M& m;
deba@2032
   986
  public:
deba@2032
   987
    typedef MapBase<typename M::Key, bool> Parent;
deba@2032
   988
    typedef typename Parent::Key Key;
deba@2032
   989
    typedef typename Parent::Value Value;
deba@2032
   990
deba@2032
   991
    /// Constructor
deba@2032
   992
    NotWriteMap(M &_m) : m(_m) {};
deba@2032
   993
    Value operator[](Key k) const {return !m[k];}
deba@2032
   994
    void set(Key k, bool v) { m.set(k, !v); }
deba@2032
   995
  };
alpar@1456
   996
  
alpar@1456
   997
  ///Returns a \ref NotMap class
alpar@1456
   998
  
alpar@1456
   999
  ///This function just returns a \ref NotMap class.
alpar@1456
  1000
  ///\relates NotMap
deba@1675
  1001
  template <typename M> 
deba@1705
  1002
  inline NotMap<M> notMap(const M &m) {
deba@1705
  1003
    return NotMap<M>(m);
alpar@1456
  1004
  }
alpar@1456
  1005
deba@2032
  1006
  template <typename M> 
deba@2032
  1007
  inline NotWriteMap<M> notMap(M &m) {
deba@2032
  1008
    return NotWriteMap<M>(m);
deba@2032
  1009
  }
deba@2032
  1010
alpar@1808
  1011
  /// \brief Writable bool map for store each true assigned elements.
deba@1778
  1012
  ///
alpar@1808
  1013
  /// Writable bool map for store each true assigned elements. It will
deba@1778
  1014
  /// copies all the true setted keys to the given iterator.
deba@1778
  1015
  ///
deba@1778
  1016
  /// \note The container of the iterator should contain for each element.
deba@1778
  1017
  template <typename _Iterator>
deba@1778
  1018
  class StoreBoolMap {
deba@1778
  1019
  public:
deba@1778
  1020
    typedef _Iterator Iterator;
deba@1778
  1021
deba@1778
  1022
    typedef typename std::iterator_traits<Iterator>::value_type Key;
deba@1778
  1023
    typedef bool Value;
deba@1778
  1024
deba@1778
  1025
    /// Constructor
deba@1778
  1026
    StoreBoolMap(Iterator it) : _begin(it), _end(it) {}
deba@1778
  1027
deba@1778
  1028
    /// Gives back the given first setted iterator.
deba@1778
  1029
    Iterator begin() const {
deba@1778
  1030
      return _begin;
deba@1778
  1031
    }
deba@1778
  1032
 
deba@1778
  1033
    /// Gives back the iterator after the last setted.
deba@1778
  1034
    Iterator end() const {
deba@1778
  1035
      return _end;
deba@1778
  1036
    }
deba@1778
  1037
deba@1778
  1038
    /// Setter function of the map
deba@1778
  1039
    void set(const Key& key, Value value) {
deba@1778
  1040
      if (value) {
deba@1778
  1041
	*_end++ = key;
deba@1778
  1042
      }
deba@1778
  1043
    }
deba@1778
  1044
    
deba@1778
  1045
  private:
deba@1778
  1046
    Iterator _begin, _end;
deba@1778
  1047
  };
deba@1778
  1048
alpar@1808
  1049
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
  1050
  /// a back insertable container.
deba@1778
  1051
  ///
alpar@1808
  1052
  /// Writable bool map for store each true assigned elements in a back 
deba@1778
  1053
  /// insertable container. It will push back all the true setted keys into
deba@1778
  1054
  /// the container.
deba@1778
  1055
  template <typename Container>
deba@1778
  1056
  class BackInserterBoolMap {
deba@1778
  1057
  public:
deba@1778
  1058
    typedef typename Container::value_type Key;
deba@1778
  1059
    typedef bool Value;
deba@1778
  1060
deba@1778
  1061
    /// Constructor
deba@1778
  1062
    BackInserterBoolMap(Container& _container) : container(_container) {}
deba@1778
  1063
deba@1778
  1064
    /// Setter function of the map
deba@1778
  1065
    void set(const Key& key, Value value) {
deba@1778
  1066
      if (value) {
deba@1778
  1067
	container.push_back(key);
deba@1778
  1068
      }
deba@1778
  1069
    }
deba@1778
  1070
    
deba@1778
  1071
  private:
deba@1778
  1072
    Container& container;    
deba@1778
  1073
  };
deba@1778
  1074
alpar@1808
  1075
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
  1076
  /// a front insertable container.
deba@1778
  1077
  ///
alpar@1808
  1078
  /// Writable bool map for store each true assigned elements in a front 
deba@1778
  1079
  /// insertable container. It will push front all the true setted keys into
deba@1778
  1080
  /// the container.
deba@1778
  1081
  template <typename Container>
deba@1778
  1082
  class FrontInserterBoolMap {
deba@1778
  1083
  public:
deba@1778
  1084
    typedef typename Container::value_type Key;
deba@1778
  1085
    typedef bool Value;
deba@1778
  1086
deba@1778
  1087
    /// Constructor
deba@1778
  1088
    FrontInserterBoolMap(Container& _container) : container(_container) {}
deba@1778
  1089
deba@1778
  1090
    /// Setter function of the map
deba@1778
  1091
    void set(const Key& key, Value value) {
deba@1778
  1092
      if (value) {
deba@1778
  1093
	container.push_front(key);
deba@1778
  1094
      }
deba@1778
  1095
    }
deba@1778
  1096
    
deba@1778
  1097
  private:
deba@1778
  1098
    Container& container;    
deba@1778
  1099
  };
deba@1778
  1100
alpar@1808
  1101
  /// \brief Writable bool map for store each true assigned elements in 
deba@1778
  1102
  /// an insertable container.
deba@1778
  1103
  ///
alpar@1808
  1104
  /// Writable bool map for store each true assigned elements in an 
deba@1778
  1105
  /// insertable container. It will insert all the true setted keys into
deba@1778
  1106
  /// the container.
deba@1778
  1107
  template <typename Container>
deba@1778
  1108
  class InserterBoolMap {
deba@1778
  1109
  public:
deba@1778
  1110
    typedef typename Container::value_type Key;
deba@1778
  1111
    typedef bool Value;
deba@1778
  1112
deba@1778
  1113
    /// Constructor
deba@1778
  1114
    InserterBoolMap(Container& _container) : container(_container) {}
deba@1778
  1115
deba@1778
  1116
    /// Setter function of the map
deba@1778
  1117
    void set(const Key& key, Value value) {
deba@1778
  1118
      if (value) {
deba@1778
  1119
	container.insert(key);
deba@1778
  1120
      }
deba@1778
  1121
    }
deba@1778
  1122
    
deba@1778
  1123
  private:
deba@1778
  1124
    Container& container;    
deba@1778
  1125
  };
deba@1778
  1126
deba@1778
  1127
  /// \brief Fill the true setted elements with a given value.
deba@1778
  1128
  ///
alpar@1808
  1129
  /// Writable bool map for fill the true setted elements with a given value.
deba@1778
  1130
  /// The value can be setted 
deba@1778
  1131
  /// the container.
deba@1778
  1132
  template <typename Map>
deba@1778
  1133
  class FillBoolMap {
deba@1778
  1134
  public:
deba@1778
  1135
    typedef typename Map::Key Key;
deba@1778
  1136
    typedef bool Value;
deba@1778
  1137
deba@1778
  1138
    /// Constructor
deba@1778
  1139
    FillBoolMap(Map& _map, const typename Map::Value& _fill) 
deba@1778
  1140
      : map(_map), fill(_fill) {}
deba@1778
  1141
deba@1778
  1142
    /// Constructor
deba@1778
  1143
    FillBoolMap(Map& _map) 
deba@1778
  1144
      : map(_map), fill() {}
deba@1778
  1145
deba@1778
  1146
    /// Gives back the current fill value
deba@1778
  1147
    typename Map::Value fillValue() const {
deba@1778
  1148
      return fill;
deba@1778
  1149
    } 
deba@1778
  1150
deba@1778
  1151
    /// Sets the current fill value
deba@1778
  1152
    void fillValue(const typename Map::Value& _fill) {
deba@1778
  1153
      fill = _fill;
deba@1778
  1154
    } 
deba@1778
  1155
deba@1778
  1156
    /// Setter function of the map
deba@1778
  1157
    void set(const Key& key, Value value) {
deba@1778
  1158
      if (value) {
deba@1778
  1159
	map.set(key, fill);
deba@1778
  1160
      }
deba@1778
  1161
    }
deba@1778
  1162
    
deba@1778
  1163
  private:
deba@1778
  1164
    Map& map;
deba@1778
  1165
    typename Map::Value fill;
deba@1778
  1166
  };
deba@1778
  1167
deba@1778
  1168
alpar@1808
  1169
  /// \brief Writable bool map which stores for each true assigned elements  
deba@1778
  1170
  /// the setting order number.
deba@1778
  1171
  ///
alpar@1808
  1172
  /// Writable bool map which stores for each true assigned elements  
deba@1778
  1173
  /// the setting order number.
deba@1778
  1174
  template <typename Map>
deba@1778
  1175
  class SettingOrderBoolMap {
deba@1778
  1176
  public:
deba@1778
  1177
    typedef typename Map::Key Key;
deba@1778
  1178
    typedef bool Value;
deba@1778
  1179
deba@1778
  1180
    /// Constructor
deba@1778
  1181
    SettingOrderBoolMap(Map& _map) 
deba@1778
  1182
      : map(_map), counter(0) {}
deba@1778
  1183
deba@1778
  1184
    /// Number of setted keys.
deba@1778
  1185
    int num() const {
deba@1778
  1186
      return counter;
deba@1778
  1187
    }
deba@1778
  1188
deba@1778
  1189
    /// Setter function of the map
deba@1778
  1190
    void set(const Key& key, Value value) {
deba@1778
  1191
      if (value) {
deba@1778
  1192
	map.set(key, counter++);
deba@1778
  1193
      }
deba@1778
  1194
    }
deba@1778
  1195
    
deba@1778
  1196
  private:
deba@1778
  1197
    Map& map;
deba@1778
  1198
    int counter;
deba@1778
  1199
  };
deba@1778
  1200
alpar@1041
  1201
  /// @}
klao@286
  1202
}
alpar@1041
  1203
alpar@921
  1204
#endif // LEMON_MAPS_H