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