lemon/maps.h
author deba
Fri, 14 Oct 2005 10:49:51 +0000
changeset 1720 578d8b2b76c6
parent 1695 e6f99fe1723f
child 1725 22752dd6c693
permissions -rw-r--r--
Matrixmaps moved to own file
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@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) {};
alpar@1044
   524
    Value operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   525
  };
alpar@1041
   526
  ///Returns a \ref ComposeMap class
alpar@1041
   527
alpar@1041
   528
  ///This function just returns a \ref ComposeMap class.
alpar@1219
   529
  ///
alpar@1041
   530
  ///\relates ComposeMap
deba@1675
   531
  template <typename M1, typename M2> 
deba@1705
   532
  inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
deba@1705
   533
    return ComposeMap<M1, M2>(m1,m2);
alpar@1041
   534
  }
alpar@1219
   535
  
alpar@1547
   536
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   537
alpar@1547
   538
  ///Combines of two maps using an STL (binary) functor.
alpar@1219
   539
  ///
alpar@1219
   540
  ///
alpar@1547
   541
  ///This \ref concept::ReadMap "read only map" takes two maps and a
alpar@1219
   542
  ///binary functor and returns the composition of
alpar@1547
   543
  ///the two
alpar@1219
   544
  ///given maps unsing the functor. 
alpar@1219
   545
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
alpar@1219
   546
  ///and \c f is of \c F,
alpar@1219
   547
  ///then for
alpar@1219
   548
  ///\code
deba@1675
   549
  ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
alpar@1219
   550
  ///\endcode
alpar@1219
   551
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   552
  ///
alpar@1219
   553
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
alpar@1219
   554
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   555
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   556
  ///to \c V.
alpar@1219
   557
  ///\todo Check the requirements.
alpar@1219
   558
deba@1675
   559
  template<typename M1, typename M2, typename F,
deba@1675
   560
	   typename V = typename F::result_type,
deba@1675
   561
	   typename NC = False> 
deba@1705
   562
  class CombineMap : public MapBase<typename M1::Key, V> {
deba@1705
   563
    const M1& m1;
deba@1705
   564
    const M2& m2;
deba@1420
   565
    F f;
alpar@1219
   566
  public:
deba@1705
   567
    typedef MapBase<typename M1::Key, V> Parent;
deba@1675
   568
    typedef typename Parent::Key Key;
deba@1675
   569
    typedef typename Parent::Value Value;
alpar@1219
   570
alpar@1219
   571
    ///Constructor
alpar@1219
   572
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
alpar@1219
   573
      : m1(_m1), m2(_m2), f(_f) {};
alpar@1219
   574
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   575
  };
alpar@1219
   576
  
alpar@1219
   577
  ///Returns a \ref CombineMap class
alpar@1219
   578
alpar@1219
   579
  ///This function just returns a \ref CombineMap class.
alpar@1219
   580
  ///
alpar@1219
   581
  ///Only the first template parameter (the value type) must be given.
alpar@1219
   582
  ///
alpar@1219
   583
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   584
  ///\code
alpar@1219
   585
  ///combineMap<double>(m1,m2,std::plus<double>)
alpar@1219
   586
  ///\endcode
alpar@1219
   587
  ///is equivalent with
alpar@1219
   588
  ///\code
alpar@1219
   589
  ///addMap(m1,m2)
alpar@1219
   590
  ///\endcode
alpar@1219
   591
  ///
alpar@1219
   592
  ///\relates CombineMap
deba@1675
   593
  template<typename M1, typename M2, typename F, typename V> 
deba@1705
   594
  inline CombineMap<M1, M2, F, V> 
deba@1675
   595
  combineMap(const M1& m1,const M2& m2, const F& f) {
deba@1705
   596
    return CombineMap<M1, M2, F, V>(m1,m2,f);
deba@1675
   597
  }
deba@1675
   598
deba@1675
   599
  template<typename M1, typename M2, typename F> 
deba@1705
   600
  inline CombineMap<M1, M2, F, typename F::result_type> 
deba@1675
   601
  combineMap(const M1& m1, const M2& m2, const F& f) {
deba@1675
   602
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
deba@1675
   603
  }
deba@1675
   604
deba@1675
   605
  template<typename M1, typename M2, typename K1, typename K2, typename V> 
deba@1705
   606
  inline CombineMap<M1, M2, V (*)(K1, K2), V> 
deba@1675
   607
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
deba@1675
   608
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
alpar@1219
   609
  }
alpar@1041
   610
alpar@1041
   611
  ///Negative value of a map
alpar@1041
   612
alpar@1041
   613
  ///This \ref concept::ReadMap "read only map" returns the negative
alpar@1041
   614
  ///value of the
alpar@1041
   615
  ///value returned by the
alpar@1041
   616
  ///given map. Its \c Key and \c Value will be inherited from \c M.
alpar@1041
   617
  ///The unary \c - operator must be defined for \c Value, of course.
alpar@1041
   618
deba@1705
   619
  template<typename M> 
deba@1705
   620
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   621
    const M& m;
alpar@1041
   622
  public:
deba@1705
   623
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   624
    typedef typename Parent::Key Key;
deba@1675
   625
    typedef typename Parent::Value Value;
alpar@1041
   626
alpar@1041
   627
    ///Constructor
alpar@1041
   628
    NegMap(const M &_m) : m(_m) {};
alpar@1044
   629
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   630
  };
alpar@1041
   631
  
alpar@1041
   632
  ///Returns a \ref NegMap class
alpar@1041
   633
alpar@1041
   634
  ///This function just returns a \ref NegMap class.
alpar@1041
   635
  ///\relates NegMap
deba@1675
   636
  template <typename M> 
deba@1705
   637
  inline NegMap<M> negMap(const M &m) {
deba@1705
   638
    return NegMap<M>(m);
alpar@1041
   639
  }
alpar@1041
   640
alpar@1041
   641
alpar@1041
   642
  ///Absolute value of a map
alpar@1041
   643
alpar@1041
   644
  ///This \ref concept::ReadMap "read only map" returns the absolute value
alpar@1041
   645
  ///of the
alpar@1041
   646
  ///value returned by the
alpar@1044
   647
  ///given map. Its \c Key and \c Value will be inherited
alpar@1044
   648
  ///from <tt>M</tt>. <tt>Value</tt>
alpar@1044
   649
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
alpar@1044
   650
  ///operator must be defined for it, of course.
alpar@1044
   651
  ///
alpar@1044
   652
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   653
  ///\code
alpar@1044
   654
  ///  struct _UnConvertible {};
alpar@1044
   655
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   656
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   657
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   658
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   659
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   660
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   661
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   662
  ///\endcode
alpar@1044
   663
  
alpar@1041
   664
deba@1705
   665
  template<typename M> 
deba@1705
   666
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   667
    const M& m;
alpar@1041
   668
  public:
deba@1705
   669
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   670
    typedef typename Parent::Key Key;
deba@1675
   671
    typedef typename Parent::Value Value;
alpar@1041
   672
alpar@1041
   673
    ///Constructor
alpar@1041
   674
    AbsMap(const M &_m) : m(_m) {};
deba@1675
   675
    Value operator[](Key k) const {
deba@1675
   676
      Value tmp = m[k]; 
deba@1675
   677
      return tmp >= 0 ? tmp : -tmp;
deba@1675
   678
    }
deba@1675
   679
alpar@1041
   680
  };
alpar@1041
   681
  
alpar@1041
   682
  ///Returns a \ref AbsMap class
alpar@1041
   683
alpar@1041
   684
  ///This function just returns a \ref AbsMap class.
alpar@1041
   685
  ///\relates AbsMap
deba@1675
   686
  template<typename M> 
deba@1705
   687
  inline AbsMap<M> absMap(const M &m) {
deba@1705
   688
    return AbsMap<M>(m);
alpar@1041
   689
  }
alpar@1041
   690
alpar@1402
   691
  ///Converts an STL style functor to a map
alpar@1076
   692
alpar@1076
   693
  ///This \ref concept::ReadMap "read only map" returns the value
alpar@1076
   694
  ///of a
alpar@1076
   695
  ///given map.
alpar@1076
   696
  ///
alpar@1076
   697
  ///Template parameters \c K and \c V will become its
alpar@1076
   698
  ///\c Key and \c Value. They must be given explicitely
alpar@1076
   699
  ///because a functor does not provide such typedefs.
alpar@1076
   700
  ///
alpar@1076
   701
  ///Parameter \c F is the type of the used functor.
alpar@1076
   702
  
alpar@1076
   703
deba@1675
   704
  template<typename F, 
deba@1675
   705
	   typename K = typename F::argument_type, 
deba@1675
   706
	   typename V = typename F::result_type,
deba@1675
   707
	   typename NC = False> 
deba@1705
   708
  class FunctorMap : public MapBase<K, V> {
deba@1679
   709
    F f;
alpar@1076
   710
  public:
deba@1705
   711
    typedef MapBase<K, V> Parent;
deba@1675
   712
    typedef typename Parent::Key Key;
deba@1675
   713
    typedef typename Parent::Value Value;
alpar@1076
   714
alpar@1076
   715
    ///Constructor
deba@1679
   716
    FunctorMap(const F &_f) : f(_f) {}
deba@1679
   717
deba@1679
   718
    Value operator[](Key k) const { return f(k);}
alpar@1076
   719
  };
alpar@1076
   720
  
alpar@1076
   721
  ///Returns a \ref FunctorMap class
alpar@1076
   722
alpar@1076
   723
  ///This function just returns a \ref FunctorMap class.
alpar@1076
   724
  ///
alpar@1076
   725
  ///The third template parameter isn't necessary to be given.
alpar@1076
   726
  ///\relates FunctorMap
deba@1675
   727
  template<typename K, typename V, typename F> inline 
deba@1705
   728
  FunctorMap<F, K, V> functorMap(const F &f) {
deba@1705
   729
    return FunctorMap<F, K, V>(f);
alpar@1076
   730
  }
alpar@1076
   731
deba@1675
   732
  template <typename F> inline 
deba@1705
   733
  FunctorMap<F, typename F::argument_type, typename F::result_type> 
deba@1675
   734
  functorMap(const F &f) {
deba@1679
   735
    return FunctorMap<F, typename F::argument_type, 
deba@1705
   736
      typename F::result_type>(f);
deba@1675
   737
  }
deba@1675
   738
deba@1675
   739
  template <typename K, typename V> inline 
deba@1705
   740
  FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
deba@1705
   741
    return FunctorMap<V (*)(K), K, V>(f);
deba@1675
   742
  }
deba@1675
   743
deba@1675
   744
alpar@1219
   745
  ///Converts a map to an STL style (unary) functor
alpar@1076
   746
alpar@1219
   747
  ///This class Converts a map to an STL style (unary) functor.
alpar@1076
   748
  ///that is it provides an <tt>operator()</tt> to read its values.
alpar@1076
   749
  ///
alpar@1223
   750
  ///For the sake of convenience it also works as
alpar@1537
   751
  ///a ususal \ref concept::ReadMap "readable map",
alpar@1537
   752
  ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
alpar@1076
   753
deba@1705
   754
  template <typename M> 
deba@1705
   755
  class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   756
    const M& m;
alpar@1076
   757
  public:
deba@1705
   758
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   759
    typedef typename Parent::Key Key;
deba@1675
   760
    typedef typename Parent::Value Value;
deba@1420
   761
alpar@1456
   762
    ///\e
alpar@1223
   763
    typedef typename M::Key argument_type;
alpar@1456
   764
    ///\e
alpar@1223
   765
    typedef typename M::Value result_type;
alpar@1076
   766
alpar@1076
   767
    ///Constructor
alpar@1076
   768
    MapFunctor(const M &_m) : m(_m) {};
alpar@1076
   769
    ///Returns a value of the map
alpar@1076
   770
    Value operator()(Key k) const {return m[k];}
alpar@1076
   771
    ///\e
alpar@1076
   772
    Value operator[](Key k) const {return m[k];}
alpar@1076
   773
  };
alpar@1076
   774
  
alpar@1076
   775
  ///Returns a \ref MapFunctor class
alpar@1076
   776
alpar@1076
   777
  ///This function just returns a \ref MapFunctor class.
alpar@1076
   778
  ///\relates MapFunctor
deba@1675
   779
  template<typename M> 
deba@1705
   780
  inline MapFunctor<M> mapFunctor(const M &m) {
deba@1705
   781
    return MapFunctor<M>(m);
alpar@1076
   782
  }
alpar@1076
   783
alpar@1076
   784
alpar@1547
   785
  ///Applies all map setting operations to two maps
alpar@1219
   786
alpar@1219
   787
  ///This map has two \ref concept::WriteMap "writable map"
alpar@1219
   788
  ///parameters and each write request will be passed to both of them.
alpar@1219
   789
  ///If \c M1 is also \ref concept::ReadMap "readable",
alpar@1219
   790
  ///then the read operations will return the
alpar@1317
   791
  ///corresponding values of \c M1.
alpar@1219
   792
  ///
alpar@1219
   793
  ///The \c Key and \c Value will be inherited from \c M1.
alpar@1219
   794
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
alpar@1219
   795
deba@1705
   796
  template<typename  M1, typename M2> 
deba@1705
   797
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   798
    const M1& m1;
deba@1705
   799
    const M2& m2;
alpar@1219
   800
  public:
deba@1705
   801
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   802
    typedef typename Parent::Key Key;
deba@1675
   803
    typedef typename Parent::Value Value;
alpar@1219
   804
alpar@1219
   805
    ///Constructor
alpar@1219
   806
    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1219
   807
    Value operator[](Key k) const {return m1[k];}
deba@1675
   808
    //    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
   809
  };
alpar@1219
   810
  
alpar@1219
   811
  ///Returns an \ref ForkMap class
alpar@1219
   812
alpar@1219
   813
  ///This function just returns an \ref ForkMap class.
alpar@1219
   814
  ///\todo How to call these type of functions?
alpar@1219
   815
  ///
alpar@1219
   816
  ///\relates ForkMap
alpar@1219
   817
  ///\todo Wrong scope in Doxygen when \c \\relates is used
deba@1675
   818
  template <typename M1, typename M2> 
deba@1705
   819
  inline ForkMap<M1, M2> forkMap(const M1 &m1,const M2 &m2) {
deba@1705
   820
    return ForkMap<M1, M2>(m1,m2);
alpar@1219
   821
  }
alpar@1219
   822
alpar@1456
   823
alpar@1456
   824
  
alpar@1456
   825
  /* ************* BOOL MAPS ******************* */
alpar@1456
   826
  
alpar@1456
   827
  ///Logical 'not' of a map
alpar@1456
   828
  
alpar@1456
   829
  ///This bool \ref concept::ReadMap "read only map" returns the 
alpar@1456
   830
  ///logical negation of
alpar@1456
   831
  ///value returned by the
alpar@1456
   832
  ///given map. Its \c Key and will be inherited from \c M,
alpar@1456
   833
  ///its Value is <tt>bool</tt>.
alpar@1456
   834
deba@1705
   835
  template <typename M> 
deba@1705
   836
  class NotMap : public MapBase<typename M::Key, bool> {
deba@1705
   837
    const M& m;
alpar@1456
   838
  public:
deba@1705
   839
    typedef MapBase<typename M::Key, bool> Parent;
deba@1675
   840
    typedef typename Parent::Key Key;
deba@1675
   841
    typedef typename Parent::Value Value;
alpar@1456
   842
alpar@1456
   843
    ///Constructor
alpar@1456
   844
    NotMap(const M &_m) : m(_m) {};
alpar@1456
   845
    Value operator[](Key k) const {return !m[k];}
alpar@1456
   846
  };
alpar@1456
   847
  
alpar@1456
   848
  ///Returns a \ref NotMap class
alpar@1456
   849
  
alpar@1456
   850
  ///This function just returns a \ref NotMap class.
alpar@1456
   851
  ///\relates NotMap
deba@1675
   852
  template <typename M> 
deba@1705
   853
  inline NotMap<M> notMap(const M &m) {
deba@1705
   854
    return NotMap<M>(m);
alpar@1456
   855
  }
alpar@1456
   856
alpar@1041
   857
  /// @}
klao@286
   858
}
alpar@1041
   859
alpar@921
   860
#endif // LEMON_MAPS_H