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