lemon/maps.h
author deba
Wed, 05 Oct 2005 13:15:47 +0000
changeset 1703 eb90e3d6bddc
parent 1691 6be54bcc14ec
child 1705 3f63d9db307b
permissions -rw-r--r--
Proper sized map 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@1420
    21
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@1675
    41
  template<typename K, typename T, typename _NeedCopy = False>
deba@1675
    42
  class MapBase {
alpar@720
    43
  public:
deba@1675
    44
    /// \e
deba@1675
    45
    typedef _NeedCopy NeedCopy;
alpar@911
    46
    ///\e
alpar@987
    47
    typedef K Key;
alpar@911
    48
    ///\e
alpar@987
    49
    typedef T Value;
alpar@720
    50
  };
alpar@720
    51
alpar@805
    52
  /// Null map. (a.k.a. DoNothingMap)
klao@286
    53
klao@286
    54
  /// If you have to provide a map only for its type definitions,
alpar@805
    55
  /// or if you have to provide a writable map, but
alpar@805
    56
  /// data written to it will sent to <tt>/dev/null</tt>...
deba@1675
    57
  template<typename K, typename T, typename NC = False>
deba@1675
    58
  class NullMap : public MapBase<K, T, NC> {
klao@286
    59
  public:
deba@1675
    60
    typedef MapBase<K, T, NC> Parent;
deba@1675
    61
    typedef typename Parent::Key Key;
deba@1675
    62
    typedef typename Parent::Value Value;
deba@1420
    63
    
alpar@805
    64
    /// Gives back a default constructed element.
klao@286
    65
    T operator[](const K&) const { return T(); }
alpar@805
    66
    /// Absorbs the value.
klao@286
    67
    void set(const K&, const T&) {}
klao@286
    68
  };
klao@286
    69
deba@1420
    70
  template <typename K, typename V> 
deba@1675
    71
  NullMap<K, V, True> nullMap() {
deba@1675
    72
    return NullMap<K, V, True>();
deba@1420
    73
  }
deba@1420
    74
klao@286
    75
klao@286
    76
  /// Constant map.
klao@286
    77
alpar@805
    78
  /// This is a readable map which assigns a specified value to each key.
alpar@805
    79
  /// In other aspects it is equivalent to the \ref NullMap.
alpar@805
    80
  /// \todo set could be used to set the value.
deba@1675
    81
  template<typename K, typename T, typename NC = False>
deba@1675
    82
  class ConstMap : public MapBase<K, T, NC> {
deba@1675
    83
  private:
klao@286
    84
    T v;
klao@286
    85
  public:
klao@286
    86
deba@1675
    87
    typedef MapBase<K, T, NC> Parent;
deba@1675
    88
    typedef typename Parent::Key Key;
deba@1675
    89
    typedef typename Parent::Value Value;
deba@1420
    90
alpar@805
    91
    /// Default constructor
alpar@805
    92
alpar@805
    93
    /// The value of the map will be uninitialized. 
alpar@805
    94
    /// (More exactly it will be default constructed.)
klao@286
    95
    ConstMap() {}
alpar@911
    96
    ///\e
alpar@805
    97
alpar@805
    98
    /// \param _v The initial value of the map.
alpar@911
    99
    ///
klao@286
   100
    ConstMap(const T &_v) : v(_v) {}
klao@286
   101
klao@286
   102
    T operator[](const K&) const { return v; }
klao@286
   103
    void set(const K&, const T&) {}
klao@286
   104
klao@286
   105
    template<typename T1>
klao@286
   106
    struct rebind {
deba@1675
   107
      typedef ConstMap<K, T1> other;
klao@286
   108
    };
klao@286
   109
klao@286
   110
    template<typename T1>
deba@1675
   111
    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
klao@286
   112
  };
klao@286
   113
alpar@1076
   114
  ///Returns a \ref ConstMap class
alpar@1076
   115
alpar@1076
   116
  ///This function just returns a \ref ConstMap class.
alpar@1076
   117
  ///\relates ConstMap
deba@1675
   118
  template<typename K, typename V> 
deba@1675
   119
  inline ConstMap<K, V, True> constMap(const V &v) {
deba@1675
   120
    return ConstMap<K, V, True>(v);
alpar@1076
   121
  }
alpar@1076
   122
alpar@1076
   123
alpar@1660
   124
  //\todo to document later
marci@890
   125
  template<typename T, T v>
marci@890
   126
  struct Const { };
deba@1675
   127
alpar@1660
   128
  //\todo to document later
deba@1675
   129
  template<typename K, typename V, V v, typename NC>
deba@1675
   130
  class ConstMap<K, Const<V, v>, NC > : public MapBase<K, V, NC> {
marci@890
   131
  public:
deba@1675
   132
    typedef MapBase<K, V, False> Parent;
deba@1675
   133
    typedef typename Parent::Key Key;
deba@1675
   134
    typedef typename Parent::Value Value;
deba@1675
   135
marci@890
   136
    ConstMap() { }
marci@890
   137
    V operator[](const K&) const { return v; }
marci@890
   138
    void set(const K&, const V&) { }
marci@890
   139
  };
klao@286
   140
deba@1675
   141
  ///Returns a \ref ConstMap class
deba@1675
   142
deba@1675
   143
  ///This function just returns a \ref ConstMap class.
deba@1675
   144
  ///\relates ConstMap
deba@1675
   145
  template<typename K, typename V, V v> 
deba@1675
   146
  inline ConstMap<K, Const<V, v>, True> constMap() {
deba@1675
   147
    return ConstMap<K, Const<V, v>, True>();
deba@1675
   148
  }
deba@1675
   149
klao@286
   150
  /// \c std::map wrapper
klao@286
   151
klao@286
   152
  /// This is essentially a wrapper for \c std::map. With addition that
alpar@987
   153
  /// you can specify a default value different from \c Value() .
klao@286
   154
  ///
klao@286
   155
  /// \todo Provide allocator parameter...
alpar@987
   156
  template <typename K, typename T, typename Compare = std::less<K> >
deba@1675
   157
  class StdMap : public std::map<K, T, Compare> {
deba@1675
   158
    typedef std::map<K, T, Compare> parent;
klao@286
   159
    T v;
klao@286
   160
    typedef typename parent::value_type PairType;
klao@286
   161
klao@286
   162
  public:
alpar@1456
   163
    ///\e
alpar@987
   164
    typedef K Key;
alpar@1456
   165
    ///\e
alpar@987
   166
    typedef T Value;
alpar@1456
   167
    ///\e
alpar@987
   168
    typedef T& Reference;
alpar@1456
   169
    ///\e
alpar@987
   170
    typedef const T& ConstReference;
klao@286
   171
klao@286
   172
klao@345
   173
    StdMap() : v() {}
klao@286
   174
    /// Constructor with specified default value
klao@286
   175
    StdMap(const T& _v) : v(_v) {}
klao@286
   176
klao@286
   177
    /// \brief Constructs the map from an appropriate std::map.
klao@286
   178
    ///
klao@286
   179
    /// \warning Inefficient: copies the content of \c m !
klao@286
   180
    StdMap(const parent &m) : parent(m) {}
klao@286
   181
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   182
    /// specifies a default value.
klao@286
   183
    ///
klao@286
   184
    /// \warning Inefficient: copies the content of \c m !
klao@286
   185
    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
klao@286
   186
    
klao@286
   187
    template<typename T1, typename Comp1>
deba@1675
   188
    StdMap(const StdMap<Key, T1,Comp1> &m, const T &_v) { 
marci@389
   189
      //FIXME; 
marci@389
   190
    }
klao@286
   191
alpar@987
   192
    Reference operator[](const Key &k) {
klao@346
   193
      return insert(PairType(k,v)).first -> second;
klao@286
   194
    }
deba@1675
   195
alpar@987
   196
    ConstReference operator[](const Key &k) const {
marci@389
   197
      typename parent::iterator i = lower_bound(k);
beckerjc@391
   198
      if (i == parent::end() || parent::key_comp()(k, (*i).first))
klao@286
   199
	return v;
klao@286
   200
      return (*i).second;
klao@286
   201
    }
klao@345
   202
    void set(const Key &k, const T &t) {
klao@346
   203
      parent::operator[](k) = t;
klao@345
   204
    }
klao@286
   205
klao@286
   206
    /// Changes the default value of the map.
klao@286
   207
    /// \return Returns the previous default value.
klao@286
   208
    ///
alpar@805
   209
    /// \warning The value of some keys (which has already been queried, but
klao@286
   210
    /// the value has been unchanged from the default) may change!
klao@286
   211
    T setDefault(const T &_v) { T old=v; v=_v; return old; }
klao@286
   212
klao@286
   213
    template<typename T1>
klao@286
   214
    struct rebind {
deba@1675
   215
      typedef StdMap<Key, T1,Compare> other;
klao@286
   216
    };
klao@286
   217
  };
alpar@1041
   218
alpar@1402
   219
  /// @}
alpar@1402
   220
alpar@1402
   221
  /// \addtogroup map_adaptors
alpar@1402
   222
  /// @{
alpar@1402
   223
deba@1531
   224
  /// \brief Identity mapping.
deba@1531
   225
  ///
deba@1531
   226
  /// This mapping gives back the given key as value without any
deba@1531
   227
  /// modification. 
deba@1675
   228
  template <typename T, typename NC = False>
deba@1675
   229
  class IdentityMap : public MapBase<T, T, NC> {
deba@1531
   230
  public:
deba@1675
   231
    typedef MapBase<T, T, NC> Parent;
deba@1675
   232
    typedef typename Parent::Key Key;
deba@1675
   233
    typedef typename Parent::Value Value;
deba@1531
   234
deba@1675
   235
    const T& operator[](const T& t) const {
deba@1531
   236
      return t;
deba@1531
   237
    }
deba@1531
   238
  };
alpar@1402
   239
deba@1675
   240
  ///Returns an \ref IdentityMap class
deba@1675
   241
deba@1675
   242
  ///This function just returns an \ref IdentityMap class.
deba@1675
   243
  ///\relates IdentityMap
deba@1675
   244
  template<typename T>
deba@1675
   245
  inline IdentityMap<T, True> identityMap() {
deba@1675
   246
    return IdentityMap<T, True>();
deba@1675
   247
  }
deba@1675
   248
  
deba@1675
   249
alpar@1547
   250
  ///Convert the \c Value of a map to another type.
alpar@1178
   251
alpar@1178
   252
  ///This \ref concept::ReadMap "read only map"
alpar@1178
   253
  ///converts the \c Value of a maps to type \c T.
alpar@1547
   254
  ///Its \c Key is inherited from \c M.
deba@1675
   255
  template <typename M, typename T, typename NC = False> 
deba@1675
   256
  class ConvertMap : public MapBase<typename M::Key, T, NC> {
deba@1420
   257
    typename SmartConstReference<M>::Type m;
alpar@1178
   258
  public:
deba@1675
   259
    typedef MapBase<typename M::Key, T, NC> Parent;
deba@1675
   260
    typedef typename Parent::Key Key;
deba@1675
   261
    typedef typename Parent::Value Value;
alpar@1178
   262
alpar@1178
   263
    ///Constructor
alpar@1178
   264
alpar@1178
   265
    ///Constructor
alpar@1536
   266
    ///\param _m is the underlying map
alpar@1178
   267
    ConvertMap(const M &_m) : m(_m) {};
deba@1346
   268
deba@1346
   269
    /// \brief The subscript operator.
deba@1346
   270
    ///
deba@1346
   271
    /// The subscript operator.
alpar@1536
   272
    /// \param k The key
deba@1346
   273
    /// \return The target of the edge 
deba@1675
   274
    Value operator[](const Key& k) const {return m[k];}
alpar@1178
   275
  };
alpar@1178
   276
  
alpar@1178
   277
  ///Returns an \ref ConvertMap class
alpar@1178
   278
alpar@1178
   279
  ///This function just returns an \ref ConvertMap class.
alpar@1178
   280
  ///\relates ConvertMap
alpar@1178
   281
  ///\todo The order of the template parameters are changed.
deba@1675
   282
  template<typename T, typename M>
deba@1675
   283
  inline ConvertMap<M, T, True> convertMap(const M &m) {
deba@1675
   284
    return ConvertMap<M, T, True>(m);
alpar@1178
   285
  }
alpar@1041
   286
alpar@1041
   287
  ///Sum of two maps
alpar@1041
   288
alpar@1041
   289
  ///This \ref concept::ReadMap "read only map" returns the sum of the two
alpar@1041
   290
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   291
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
alpar@1041
   292
deba@1675
   293
  template<typename M1, typename M2, typename NC = False> 
deba@1675
   294
  class AddMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
deba@1420
   295
    typename SmartConstReference<M1>::Type m1;
deba@1420
   296
    typename SmartConstReference<M2>::Type m2;
deba@1420
   297
alpar@1041
   298
  public:
deba@1675
   299
    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
deba@1675
   300
    typedef typename Parent::Key Key;
deba@1675
   301
    typedef typename Parent::Value Value;
alpar@1041
   302
alpar@1041
   303
    ///Constructor
alpar@1041
   304
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   305
    Value operator[](Key k) const {return m1[k]+m2[k];}
alpar@1041
   306
  };
alpar@1041
   307
  
alpar@1041
   308
  ///Returns an \ref AddMap class
alpar@1041
   309
alpar@1041
   310
  ///This function just returns an \ref AddMap class.
alpar@1041
   311
  ///\todo How to call these type of functions?
alpar@1041
   312
  ///
alpar@1041
   313
  ///\relates AddMap
alpar@1041
   314
  ///\todo Wrong scope in Doxygen when \c \\relates is used
deba@1675
   315
  template<typename M1, typename M2> 
deba@1675
   316
  inline AddMap<M1, M2, True> addMap(const M1 &m1,const M2 &m2) {
deba@1675
   317
    return AddMap<M1, M2, True>(m1,m2);
alpar@1041
   318
  }
alpar@1041
   319
alpar@1547
   320
  ///Shift a map with a constant.
alpar@1070
   321
alpar@1070
   322
  ///This \ref concept::ReadMap "read only map" returns the sum of the
alpar@1070
   323
  ///given map and a constant value.
alpar@1070
   324
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   325
  ///
alpar@1070
   326
  ///Actually,
alpar@1070
   327
  ///\code
alpar@1070
   328
  ///  ShiftMap<X> sh(x,v);
alpar@1070
   329
  ///\endcode
alpar@1547
   330
  ///is equivalent with
alpar@1070
   331
  ///\code
alpar@1070
   332
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   333
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1070
   334
  ///\endcode
deba@1691
   335
  template<typename M, typename C = typename M::Value, typename NC = False> 
deba@1675
   336
  class ShiftMap : public MapBase<typename M::Key, typename M::Value, NC> {
deba@1420
   337
    typename SmartConstReference<M>::Type m;
deba@1691
   338
    C v;
alpar@1070
   339
  public:
deba@1675
   340
    typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
deba@1675
   341
    typedef typename Parent::Key Key;
deba@1675
   342
    typedef typename Parent::Value Value;
alpar@1070
   343
alpar@1070
   344
    ///Constructor
alpar@1070
   345
alpar@1070
   346
    ///Constructor
alpar@1070
   347
    ///\param _m is the undelying map
alpar@1070
   348
    ///\param _v is the shift value
deba@1691
   349
    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@1691
   350
    Value operator[](Key k) const {return m[k] + v;}
alpar@1070
   351
  };
alpar@1070
   352
  
alpar@1070
   353
  ///Returns an \ref ShiftMap class
alpar@1070
   354
alpar@1070
   355
  ///This function just returns an \ref ShiftMap class.
alpar@1070
   356
  ///\relates ShiftMap
alpar@1070
   357
  ///\todo A better name is required.
deba@1691
   358
  template<typename M, typename C> 
deba@1691
   359
  inline ShiftMap<M, C, True> shiftMap(const M &m,const C &v) {
deba@1691
   360
    return ShiftMap<M, C, True>(m,v);
alpar@1070
   361
  }
alpar@1070
   362
alpar@1041
   363
  ///Difference of two maps
alpar@1041
   364
alpar@1041
   365
  ///This \ref concept::ReadMap "read only map" returns the difference
alpar@1547
   366
  ///of the values of the two
alpar@1041
   367
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   368
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   369
deba@1675
   370
  template<typename M1, typename M2, typename NC = False> 
deba@1675
   371
  class SubMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
deba@1420
   372
    typename SmartConstReference<M1>::Type m1;
deba@1420
   373
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   374
  public:
deba@1675
   375
    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
deba@1675
   376
    typedef typename Parent::Key Key;
deba@1675
   377
    typedef typename Parent::Value Value;
alpar@1041
   378
alpar@1041
   379
    ///Constructor
alpar@1041
   380
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   381
    Value operator[](Key k) const {return m1[k]-m2[k];}
alpar@1041
   382
  };
alpar@1041
   383
  
alpar@1041
   384
  ///Returns a \ref SubMap class
alpar@1041
   385
alpar@1041
   386
  ///This function just returns a \ref SubMap class.
alpar@1041
   387
  ///
alpar@1041
   388
  ///\relates SubMap
deba@1675
   389
  template<typename M1, typename M2> 
deba@1675
   390
  inline SubMap<M1, M2, True> subMap(const M1 &m1, const M2 &m2) {
deba@1675
   391
    return SubMap<M1, M2, True>(m1, m2);
alpar@1041
   392
  }
alpar@1041
   393
alpar@1041
   394
  ///Product of two maps
alpar@1041
   395
alpar@1041
   396
  ///This \ref concept::ReadMap "read only map" returns the product of the
alpar@1547
   397
  ///values of the two
alpar@1041
   398
  ///given
alpar@1041
   399
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   400
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   401
deba@1675
   402
  template<typename M1, typename M2, typename NC = False> 
deba@1675
   403
  class MulMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
deba@1420
   404
    typename SmartConstReference<M1>::Type m1;
deba@1420
   405
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   406
  public:
deba@1675
   407
    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
deba@1675
   408
    typedef typename Parent::Key Key;
deba@1675
   409
    typedef typename Parent::Value Value;
alpar@1041
   410
alpar@1041
   411
    ///Constructor
alpar@1041
   412
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   413
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   414
  };
alpar@1041
   415
  
alpar@1041
   416
  ///Returns a \ref MulMap class
alpar@1041
   417
alpar@1041
   418
  ///This function just returns a \ref MulMap class.
alpar@1041
   419
  ///\relates MulMap
deba@1675
   420
  template<typename M1, typename M2> 
deba@1675
   421
  inline MulMap<M1, M2, True> mulMap(const M1 &m1,const M2 &m2) {
deba@1675
   422
    return MulMap<M1, M2, True>(m1,m2);
alpar@1041
   423
  }
alpar@1041
   424
 
alpar@1547
   425
  ///Scales a maps with a constant.
alpar@1070
   426
alpar@1070
   427
  ///This \ref concept::ReadMap "read only map" returns the value of the
deba@1691
   428
  ///given map multiplied from the left side with a constant value.
alpar@1070
   429
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   430
  ///
alpar@1070
   431
  ///Actually,
alpar@1070
   432
  ///\code
alpar@1070
   433
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   434
  ///\endcode
alpar@1547
   435
  ///is equivalent with
alpar@1070
   436
  ///\code
alpar@1070
   437
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   438
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   439
  ///\endcode
deba@1691
   440
  template<typename M, typename C = typename M::Value, typename NC = False> 
deba@1675
   441
  class ScaleMap : public MapBase<typename M::Key, typename M::Value, NC> {
deba@1420
   442
    typename SmartConstReference<M>::Type m;
deba@1691
   443
    C v;
alpar@1070
   444
  public:
deba@1675
   445
    typedef MapBase<typename M::Key, typename M::Value, NC> Parent;
deba@1675
   446
    typedef typename Parent::Key Key;
deba@1675
   447
    typedef typename Parent::Value Value;
alpar@1070
   448
alpar@1070
   449
    ///Constructor
alpar@1070
   450
alpar@1070
   451
    ///Constructor
alpar@1070
   452
    ///\param _m is the undelying map
alpar@1070
   453
    ///\param _v is the scaling value
deba@1691
   454
    ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@1691
   455
    Value operator[](Key k) const {return v * m[k];}
alpar@1070
   456
  };
alpar@1070
   457
  
alpar@1070
   458
  ///Returns an \ref ScaleMap class
alpar@1070
   459
alpar@1070
   460
  ///This function just returns an \ref ScaleMap class.
alpar@1070
   461
  ///\relates ScaleMap
alpar@1070
   462
  ///\todo A better name is required.
deba@1691
   463
  template<typename M, typename C> 
deba@1691
   464
  inline ScaleMap<M, C, True> scaleMap(const M &m,const C &v) {
deba@1691
   465
    return ScaleMap<M, C, True>(m,v);
alpar@1070
   466
  }
alpar@1070
   467
alpar@1041
   468
  ///Quotient of two maps
alpar@1041
   469
alpar@1041
   470
  ///This \ref concept::ReadMap "read only map" returns the quotient of the
alpar@1547
   471
  ///values of the two
alpar@1041
   472
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   473
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   474
deba@1675
   475
  template<typename M1, typename M2, typename NC = False> 
deba@1675
   476
  class DivMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
deba@1420
   477
    typename SmartConstReference<M1>::Type m1;
deba@1420
   478
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   479
  public:
deba@1675
   480
    typedef MapBase<typename M1::Key, typename M1::Value, NC> Parent;
deba@1675
   481
    typedef typename Parent::Key Key;
deba@1675
   482
    typedef typename Parent::Value Value;
alpar@1041
   483
alpar@1041
   484
    ///Constructor
alpar@1041
   485
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   486
    Value operator[](Key k) const {return m1[k]/m2[k];}
alpar@1041
   487
  };
alpar@1041
   488
  
alpar@1041
   489
  ///Returns a \ref DivMap class
alpar@1041
   490
alpar@1041
   491
  ///This function just returns a \ref DivMap class.
alpar@1041
   492
  ///\relates DivMap
deba@1675
   493
  template<typename M1, typename M2> 
deba@1675
   494
  inline DivMap<M1, M2, True> divMap(const M1 &m1,const M2 &m2) {
deba@1675
   495
    return DivMap<M1, M2, True>(m1,m2);
alpar@1041
   496
  }
alpar@1041
   497
  
alpar@1041
   498
  ///Composition of two maps
alpar@1041
   499
alpar@1041
   500
  ///This \ref concept::ReadMap "read only map" returns the composition of
alpar@1041
   501
  ///two
alpar@1041
   502
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
alpar@1041
   503
  ///of \c M2,
alpar@1041
   504
  ///then for
alpar@1041
   505
  ///\code
deba@1675
   506
  ///  ComposeMap<M1, M2> cm(m1,m2);
alpar@1041
   507
  ///\endcode
alpar@1044
   508
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
alpar@1041
   509
  ///
alpar@1041
   510
  ///Its \c Key is inherited from \c M2 and its \c Value is from
alpar@1041
   511
  ///\c M1.
alpar@1041
   512
  ///The \c M2::Value must be convertible to \c M1::Key.
alpar@1041
   513
  ///\todo Check the requirements.
alpar@1041
   514
deba@1675
   515
  template <typename M1, typename M2, typename NC = False> 
deba@1675
   516
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value, NC> {
deba@1420
   517
    typename SmartConstReference<M1>::Type m1;
deba@1420
   518
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   519
  public:
deba@1675
   520
    typedef MapBase<typename M2::Key, typename M1::Value, NC> Parent;
deba@1675
   521
    typedef typename Parent::Key Key;
deba@1675
   522
    typedef typename Parent::Value Value;
alpar@1041
   523
alpar@1041
   524
    ///Constructor
alpar@1041
   525
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   526
    Value 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@1675
   534
  inline ComposeMap<M1, M2, True> composeMap(const M1 &m1,const M2 &m2) {
deba@1675
   535
    return ComposeMap<M1, M2, True>(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@1675
   564
  class CombineMap : public MapBase<typename M1::Key, V, NC> {
deba@1420
   565
    typename SmartConstReference<M1>::Type m1;
deba@1420
   566
    typename SmartConstReference<M2>::Type m2;
deba@1420
   567
    F f;
alpar@1219
   568
  public:
deba@1675
   569
    typedef MapBase<typename M1::Key, V, NC> 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@1675
   596
  inline CombineMap<M1, M2, F, V, True> 
deba@1675
   597
  combineMap(const M1& m1,const M2& m2, const F& f) {
deba@1675
   598
    return CombineMap<M1, M2, F, V, True>(m1,m2,f);
deba@1675
   599
  }
deba@1675
   600
deba@1675
   601
  template<typename M1, typename M2, typename F> 
deba@1675
   602
  inline CombineMap<M1, M2, F, typename F::result_type, True> 
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@1675
   608
  inline CombineMap<M1, M2, V (*)(K1, K2), V, True> 
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@1675
   621
  template<typename M, typename NC = False> 
deba@1675
   622
  class NegMap : public MapBase<typename M::Key, typename M::Value, NC> {
deba@1420
   623
    typename SmartConstReference<M>::Type m;
alpar@1041
   624
  public:
deba@1675
   625
    typedef MapBase<typename M::Key, typename M::Value, NC> 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@1675
   639
  inline NegMap<M, True> negMap(const M &m) {
deba@1675
   640
    return NegMap<M, True>(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@1675
   667
  template<typename M, typename NC = False> 
deba@1675
   668
  class AbsMap : public MapBase<typename M::Key, typename M::Value, NC> {
deba@1420
   669
    typename SmartConstReference<M>::Type m;
alpar@1041
   670
  public:
deba@1675
   671
    typedef MapBase<typename M::Key, typename M::Value, NC> 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@1675
   689
  inline AbsMap<M, True> absMap(const M &m) {
deba@1675
   690
    return AbsMap<M, True>(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@1675
   710
  class FunctorMap : public MapBase<K, V, NC> {
deba@1679
   711
    F f;
alpar@1076
   712
  public:
deba@1675
   713
    typedef MapBase<K, V, NC> 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@1675
   730
  FunctorMap<F, K, V, True> functorMap(const F &f) {
deba@1675
   731
    return FunctorMap<F, K, V, True>(f);
alpar@1076
   732
  }
alpar@1076
   733
deba@1675
   734
  template <typename F> inline 
deba@1675
   735
  FunctorMap<F, typename F::argument_type, typename F::result_type, True> 
deba@1675
   736
  functorMap(const F &f) {
deba@1679
   737
    return FunctorMap<F, typename F::argument_type, 
deba@1679
   738
      typename F::result_type, True>(f);
deba@1675
   739
  }
deba@1675
   740
deba@1675
   741
  template <typename K, typename V> inline 
deba@1675
   742
  FunctorMap<V (*)(K), K, V, True> functorMap(V (*f)(K)) {
deba@1679
   743
    return FunctorMap<V (*)(K), K, V, True>(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@1675
   756
  template <typename M, typename NC = False> 
deba@1675
   757
  class MapFunctor : public MapBase<typename M::Key, typename M::Value, NC> {
deba@1420
   758
    typename SmartConstReference<M>::Type m;
alpar@1076
   759
  public:
deba@1675
   760
    typedef MapBase<typename M::Key, typename M::Value, NC> 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@1675
   782
  inline MapFunctor<M, True> mapFunctor(const M &m) {
deba@1675
   783
    return MapFunctor<M, True>(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@1675
   798
  template<typename  M1, typename M2, typename NC = False> 
deba@1675
   799
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value, NC> {
deba@1420
   800
    typename SmartConstReference<M1>::Type m1;
deba@1420
   801
    typename SmartConstReference<M2>::Type m2;
alpar@1219
   802
  public:
deba@1675
   803
    typedef MapBase<typename M1::Key, typename M1::Value, NC> 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@1675
   821
  inline ForkMap<M1, M2, True> forkMap(const M1 &m1,const M2 &m2) {
deba@1675
   822
    return ForkMap<M1, M2, True>(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@1675
   837
  template <typename M, typename NC = False> 
deba@1675
   838
  class NotMap : public MapBase<typename M::Key, bool, NC> {
alpar@1456
   839
    typename SmartConstReference<M>::Type m;
alpar@1456
   840
  public:
deba@1675
   841
    typedef MapBase<typename M::Key, bool, NC> 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@1675
   855
  inline NotMap<M, True> notMap(const M &m) {
deba@1675
   856
    return NotMap<M, True>(m);
alpar@1456
   857
  }
alpar@1456
   858
alpar@1041
   859
  /// @}
klao@286
   860
}
alpar@1041
   861
alpar@921
   862
#endif // LEMON_MAPS_H