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