src/lemon/maps.h
author deba
Sat, 14 May 2005 17:37:33 +0000
changeset 1420 e37cca875667
parent 1402 655d8e78454d
permissions -rw-r--r--
Smart reference handling in map adaptors
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/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.
alpar@720
    42
  template<typename K, typename T>
alpar@720
    43
  class MapBase
alpar@720
    44
  {
alpar@720
    45
  public:
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>...
klao@286
    57
  template<typename K, typename T>
alpar@720
    58
  class NullMap : public MapBase<K,T>
klao@286
    59
  {
klao@286
    60
  public:
deba@1420
    61
    
deba@1420
    62
    typedef True NeedCopy;
klao@286
    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@1420
    71
  NullMap<K, V> nullMap() {
deba@1420
    72
    return NullMap<K, V>();
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.
klao@286
    81
  template<typename K, typename T>
alpar@720
    82
  class ConstMap : public MapBase<K,T>
klao@286
    83
  {
klao@286
    84
    T v;
klao@286
    85
  public:
klao@286
    86
deba@1420
    87
    typedef True NeedCopy;
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 {
klao@286
   105
      typedef ConstMap<K,T1> other;
klao@286
   106
    };
klao@286
   107
klao@286
   108
    template<typename T1>
klao@286
   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
alpar@1076
   116
  template<class V,class K> 
alpar@1076
   117
  inline ConstMap<V,K> constMap(const K &k) 
alpar@1076
   118
  {
alpar@1076
   119
    return ConstMap<V,K>(k);
alpar@1076
   120
  }
alpar@1076
   121
alpar@1076
   122
marci@890
   123
  //to document later
marci@890
   124
  template<typename T, T v>
marci@890
   125
  struct Const { };
marci@890
   126
  //to document later
marci@890
   127
  template<typename K, typename V, V v>
marci@890
   128
  class ConstMap<K, Const<V, v> > : public MapBase<K, V>
marci@890
   129
  {
marci@890
   130
  public:
marci@890
   131
    ConstMap() { }
marci@890
   132
    V operator[](const K&) const { return v; }
marci@890
   133
    void set(const K&, const V&) { }
marci@890
   134
  };
klao@286
   135
klao@286
   136
  /// \c std::map wrapper
klao@286
   137
klao@286
   138
  /// This is essentially a wrapper for \c std::map. With addition that
alpar@987
   139
  /// you can specify a default value different from \c Value() .
klao@286
   140
  ///
klao@286
   141
  /// \todo Provide allocator parameter...
alpar@987
   142
  template <typename K, typename T, typename Compare = std::less<K> >
alpar@987
   143
  class StdMap : public std::map<K,T,Compare> {
alpar@987
   144
    typedef std::map<K,T,Compare> parent;
klao@286
   145
    T v;
klao@286
   146
    typedef typename parent::value_type PairType;
klao@286
   147
klao@286
   148
  public:
alpar@987
   149
    typedef K Key;
alpar@987
   150
    typedef T Value;
alpar@987
   151
    typedef T& Reference;
alpar@987
   152
    typedef const T& ConstReference;
klao@286
   153
klao@286
   154
klao@345
   155
    StdMap() : v() {}
klao@286
   156
    /// Constructor with specified default value
klao@286
   157
    StdMap(const T& _v) : v(_v) {}
klao@286
   158
klao@286
   159
    /// \brief Constructs the map from an appropriate std::map.
klao@286
   160
    ///
klao@286
   161
    /// \warning Inefficient: copies the content of \c m !
klao@286
   162
    StdMap(const parent &m) : parent(m) {}
klao@286
   163
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   164
    /// specifies a default value.
klao@286
   165
    ///
klao@286
   166
    /// \warning Inefficient: copies the content of \c m !
klao@286
   167
    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
klao@286
   168
    
klao@286
   169
    template<typename T1, typename Comp1>
marci@389
   170
    StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 
marci@389
   171
      //FIXME; 
marci@389
   172
    }
klao@286
   173
alpar@987
   174
    Reference operator[](const Key &k) {
klao@346
   175
      return insert(PairType(k,v)).first -> second;
klao@286
   176
    }
alpar@987
   177
    ConstReference operator[](const Key &k) const {
marci@389
   178
      typename parent::iterator i = lower_bound(k);
beckerjc@391
   179
      if (i == parent::end() || parent::key_comp()(k, (*i).first))
klao@286
   180
	return v;
klao@286
   181
      return (*i).second;
klao@286
   182
    }
klao@345
   183
    void set(const Key &k, const T &t) {
klao@346
   184
      parent::operator[](k) = t;
klao@345
   185
    }
klao@286
   186
klao@286
   187
    /// Changes the default value of the map.
klao@286
   188
    /// \return Returns the previous default value.
klao@286
   189
    ///
alpar@805
   190
    /// \warning The value of some keys (which has already been queried, but
klao@286
   191
    /// the value has been unchanged from the default) may change!
klao@286
   192
    T setDefault(const T &_v) { T old=v; v=_v; return old; }
klao@286
   193
klao@286
   194
    template<typename T1>
klao@286
   195
    struct rebind {
klao@286
   196
      typedef StdMap<Key,T1,Compare> other;
klao@286
   197
    };
klao@286
   198
  };
alpar@1041
   199
alpar@1402
   200
  /// @}
alpar@1402
   201
alpar@1402
   202
  /// \addtogroup map_adaptors
alpar@1402
   203
  /// @{
alpar@1402
   204
alpar@1402
   205
alpar@1178
   206
  ///Convert the \c Value of a maps to another type.
alpar@1178
   207
alpar@1178
   208
  ///This \ref concept::ReadMap "read only map"
alpar@1178
   209
  ///converts the \c Value of a maps to type \c T.
alpar@1178
   210
  ///Its \c Value is inherited from \c M.
alpar@1178
   211
  ///
alpar@1178
   212
  ///Actually,
alpar@1178
   213
  ///\code
alpar@1178
   214
  ///  ConvertMap<X> sh(x,v);
alpar@1178
   215
  ///\endcode
alpar@1178
   216
  ///it is equivalent with
alpar@1178
   217
  ///\code
alpar@1178
   218
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1178
   219
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1178
   220
  ///\endcode
alpar@1178
   221
  ///\bug wrong documentation
alpar@1178
   222
  template<class M, class T> 
deba@1420
   223
  class ConvertMap {
deba@1420
   224
    typename SmartConstReference<M>::Type m;
alpar@1178
   225
  public:
deba@1420
   226
deba@1420
   227
    typedef True NeedCopy;
deba@1420
   228
alpar@1178
   229
    typedef typename M::Key Key;
alpar@1178
   230
    typedef T Value;
alpar@1178
   231
alpar@1178
   232
    ///Constructor
alpar@1178
   233
alpar@1178
   234
    ///Constructor
alpar@1178
   235
    ///\param _m is the undelying map
alpar@1178
   236
    ///\param _v is the convert value
alpar@1178
   237
    ConvertMap(const M &_m) : m(_m) {};
deba@1346
   238
deba@1346
   239
    /// \brief The subscript operator.
deba@1346
   240
    ///
deba@1346
   241
    /// The subscript operator.
deba@1346
   242
    /// \param edge The edge 
deba@1346
   243
    /// \return The target of the edge 
alpar@1178
   244
    Value operator[](Key k) const {return m[k];}
alpar@1178
   245
  };
alpar@1178
   246
  
alpar@1178
   247
  ///Returns an \ref ConvertMap class
alpar@1178
   248
alpar@1178
   249
  ///This function just returns an \ref ConvertMap class.
alpar@1178
   250
  ///\relates ConvertMap
alpar@1178
   251
  ///\todo The order of the template parameters are changed.
alpar@1178
   252
  template<class T, class M>
alpar@1178
   253
  inline ConvertMap<M,T> convertMap(const M &m) 
alpar@1178
   254
  {
alpar@1178
   255
    return ConvertMap<M,T>(m);
alpar@1178
   256
  }
alpar@1041
   257
alpar@1041
   258
  ///Sum of two maps
alpar@1041
   259
alpar@1041
   260
  ///This \ref concept::ReadMap "read only map" returns the sum of the two
alpar@1041
   261
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   262
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
alpar@1041
   263
alpar@1041
   264
  template<class M1,class M2> 
alpar@1041
   265
  class AddMap
alpar@1041
   266
  {
deba@1420
   267
    typename SmartConstReference<M1>::Type m1;
deba@1420
   268
    typename SmartConstReference<M2>::Type m2;
deba@1420
   269
alpar@1041
   270
  public:
deba@1420
   271
deba@1420
   272
    typedef True NeedCopy;
deba@1420
   273
alpar@1041
   274
    typedef typename M1::Key Key;
alpar@1041
   275
    typedef typename M1::Value Value;
alpar@1041
   276
alpar@1041
   277
    ///Constructor
alpar@1041
   278
alpar@1041
   279
    ///\e
alpar@1041
   280
    ///
alpar@1041
   281
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   282
    Value operator[](Key k) const {return m1[k]+m2[k];}
alpar@1041
   283
  };
alpar@1041
   284
  
alpar@1041
   285
  ///Returns an \ref AddMap class
alpar@1041
   286
alpar@1041
   287
  ///This function just returns an \ref AddMap class.
alpar@1041
   288
  ///\todo How to call these type of functions?
alpar@1041
   289
  ///
alpar@1041
   290
  ///\relates AddMap
alpar@1041
   291
  ///\todo Wrong scope in Doxygen when \c \\relates is used
alpar@1041
   292
  template<class M1,class M2> 
alpar@1041
   293
  inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2) 
alpar@1041
   294
  {
alpar@1041
   295
    return AddMap<M1,M2>(m1,m2);
alpar@1041
   296
  }
alpar@1041
   297
alpar@1070
   298
  ///Shift a maps with a constant.
alpar@1070
   299
alpar@1070
   300
  ///This \ref concept::ReadMap "read only map" returns the sum of the
alpar@1070
   301
  ///given map and a constant value.
alpar@1070
   302
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   303
  ///
alpar@1070
   304
  ///Actually,
alpar@1070
   305
  ///\code
alpar@1070
   306
  ///  ShiftMap<X> sh(x,v);
alpar@1070
   307
  ///\endcode
alpar@1070
   308
  ///it is equivalent with
alpar@1070
   309
  ///\code
alpar@1070
   310
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   311
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1070
   312
  ///\endcode
alpar@1070
   313
  template<class M> 
alpar@1070
   314
  class ShiftMap
alpar@1070
   315
  {
deba@1420
   316
    typename SmartConstReference<M>::Type m;
alpar@1070
   317
    typename M::Value v;
alpar@1070
   318
  public:
deba@1420
   319
deba@1420
   320
    typedef True NeedCopy;
alpar@1070
   321
    typedef typename M::Key Key;
alpar@1070
   322
    typedef typename M::Value Value;
alpar@1070
   323
alpar@1070
   324
    ///Constructor
alpar@1070
   325
alpar@1070
   326
    ///Constructor
alpar@1070
   327
    ///\param _m is the undelying map
alpar@1070
   328
    ///\param _v is the shift value
alpar@1070
   329
    ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
alpar@1070
   330
    Value operator[](Key k) const {return m[k]+v;}
alpar@1070
   331
  };
alpar@1070
   332
  
alpar@1070
   333
  ///Returns an \ref ShiftMap class
alpar@1070
   334
alpar@1070
   335
  ///This function just returns an \ref ShiftMap class.
alpar@1070
   336
  ///\relates ShiftMap
alpar@1070
   337
  ///\todo A better name is required.
alpar@1070
   338
  template<class M> 
alpar@1070
   339
  inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) 
alpar@1070
   340
  {
alpar@1070
   341
    return ShiftMap<M>(m,v);
alpar@1070
   342
  }
alpar@1070
   343
alpar@1041
   344
  ///Difference of two maps
alpar@1041
   345
alpar@1041
   346
  ///This \ref concept::ReadMap "read only map" returns the difference
alpar@1041
   347
  ///of the values returned by the two
alpar@1041
   348
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   349
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   350
alpar@1041
   351
  template<class M1,class M2> 
alpar@1041
   352
  class SubMap
alpar@1041
   353
  {
deba@1420
   354
    typename SmartConstReference<M1>::Type m1;
deba@1420
   355
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   356
  public:
deba@1420
   357
deba@1420
   358
    typedef True NeedCopy;
alpar@1041
   359
    typedef typename M1::Key Key;
alpar@1041
   360
    typedef typename M1::Value Value;
alpar@1041
   361
alpar@1041
   362
    ///Constructor
alpar@1041
   363
alpar@1041
   364
    ///\e
alpar@1041
   365
    ///
alpar@1041
   366
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   367
    Value operator[](Key k) const {return m1[k]-m2[k];}
alpar@1041
   368
  };
alpar@1041
   369
  
alpar@1041
   370
  ///Returns a \ref SubMap class
alpar@1041
   371
alpar@1041
   372
  ///This function just returns a \ref SubMap class.
alpar@1041
   373
  ///
alpar@1041
   374
  ///\relates SubMap
alpar@1041
   375
  template<class M1,class M2> 
alpar@1041
   376
  inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) 
alpar@1041
   377
  {
alpar@1041
   378
    return SubMap<M1,M2>(m1,m2);
alpar@1041
   379
  }
alpar@1041
   380
alpar@1041
   381
  ///Product of two maps
alpar@1041
   382
alpar@1041
   383
  ///This \ref concept::ReadMap "read only map" returns the product of the
alpar@1041
   384
  ///values returned by the two
alpar@1041
   385
  ///given
alpar@1041
   386
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   387
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   388
alpar@1041
   389
  template<class M1,class M2> 
alpar@1041
   390
  class MulMap
alpar@1041
   391
  {
deba@1420
   392
    typename SmartConstReference<M1>::Type m1;
deba@1420
   393
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   394
  public:
deba@1420
   395
deba@1420
   396
    typedef True NeedCopy;
alpar@1041
   397
    typedef typename M1::Key Key;
alpar@1041
   398
    typedef typename M1::Value Value;
alpar@1041
   399
alpar@1041
   400
    ///Constructor
alpar@1041
   401
alpar@1041
   402
    ///\e
alpar@1041
   403
    ///
alpar@1041
   404
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   405
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   406
  };
alpar@1041
   407
  
alpar@1041
   408
  ///Returns a \ref MulMap class
alpar@1041
   409
alpar@1041
   410
  ///This function just returns a \ref MulMap class.
alpar@1041
   411
  ///\relates MulMap
alpar@1041
   412
  template<class M1,class M2> 
alpar@1041
   413
  inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) 
alpar@1041
   414
  {
alpar@1041
   415
    return MulMap<M1,M2>(m1,m2);
alpar@1041
   416
  }
alpar@1041
   417
 
alpar@1070
   418
  ///Scale a maps with a constant.
alpar@1070
   419
alpar@1070
   420
  ///This \ref concept::ReadMap "read only map" returns the value of the
alpar@1070
   421
  ///given map multipied with a constant value.
alpar@1070
   422
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   423
  ///
alpar@1070
   424
  ///Actually,
alpar@1070
   425
  ///\code
alpar@1070
   426
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   427
  ///\endcode
alpar@1070
   428
  ///it is equivalent with
alpar@1070
   429
  ///\code
alpar@1070
   430
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   431
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   432
  ///\endcode
alpar@1070
   433
  template<class M> 
alpar@1070
   434
  class ScaleMap
alpar@1070
   435
  {
deba@1420
   436
    typename SmartConstReference<M>::Type m;
alpar@1070
   437
    typename M::Value v;
alpar@1070
   438
  public:
deba@1420
   439
deba@1420
   440
    typedef True NeedCopy;
alpar@1070
   441
    typedef typename M::Key Key;
alpar@1070
   442
    typedef typename M::Value Value;
alpar@1070
   443
alpar@1070
   444
    ///Constructor
alpar@1070
   445
alpar@1070
   446
    ///Constructor
alpar@1070
   447
    ///\param _m is the undelying map
alpar@1070
   448
    ///\param _v is the scaling value
alpar@1070
   449
    ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
alpar@1070
   450
    Value operator[](Key k) const {return m[k]*v;}
alpar@1070
   451
  };
alpar@1070
   452
  
alpar@1070
   453
  ///Returns an \ref ScaleMap class
alpar@1070
   454
alpar@1070
   455
  ///This function just returns an \ref ScaleMap class.
alpar@1070
   456
  ///\relates ScaleMap
alpar@1070
   457
  ///\todo A better name is required.
alpar@1070
   458
  template<class M> 
alpar@1070
   459
  inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) 
alpar@1070
   460
  {
alpar@1070
   461
    return ScaleMap<M>(m,v);
alpar@1070
   462
  }
alpar@1070
   463
alpar@1041
   464
  ///Quotient of two maps
alpar@1041
   465
alpar@1041
   466
  ///This \ref concept::ReadMap "read only map" returns the quotient of the
alpar@1041
   467
  ///values returned by the two
alpar@1041
   468
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   469
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   470
alpar@1041
   471
  template<class M1,class M2> 
alpar@1041
   472
  class DivMap
alpar@1041
   473
  {
deba@1420
   474
    typename SmartConstReference<M1>::Type m1;
deba@1420
   475
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   476
  public:
deba@1420
   477
deba@1420
   478
    typedef True NeedCopy;
alpar@1041
   479
    typedef typename M1::Key Key;
alpar@1041
   480
    typedef typename M1::Value Value;
alpar@1041
   481
alpar@1041
   482
    ///Constructor
alpar@1041
   483
alpar@1041
   484
    ///\e
alpar@1041
   485
    ///
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
alpar@1041
   494
  template<class M1,class M2> 
alpar@1041
   495
  inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) 
alpar@1041
   496
  {
alpar@1041
   497
    return DivMap<M1,M2>(m1,m2);
alpar@1041
   498
  }
alpar@1041
   499
  
alpar@1041
   500
  ///Composition of two maps
alpar@1041
   501
alpar@1041
   502
  ///This \ref concept::ReadMap "read only map" returns the composition of
alpar@1041
   503
  ///two
alpar@1041
   504
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
alpar@1041
   505
  ///of \c M2,
alpar@1041
   506
  ///then for
alpar@1041
   507
  ///\code
alpar@1041
   508
  ///  ComposeMap<M1,M2> cm(m1,m2);
alpar@1041
   509
  ///\endcode
alpar@1044
   510
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
alpar@1041
   511
  ///
alpar@1041
   512
  ///Its \c Key is inherited from \c M2 and its \c Value is from
alpar@1041
   513
  ///\c M1.
alpar@1041
   514
  ///The \c M2::Value must be convertible to \c M1::Key.
alpar@1041
   515
  ///\todo Check the requirements.
alpar@1041
   516
alpar@1041
   517
  template<class M1,class M2> 
alpar@1041
   518
  class ComposeMap
alpar@1041
   519
  {
deba@1420
   520
    typename SmartConstReference<M1>::Type m1;
deba@1420
   521
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   522
  public:
deba@1420
   523
deba@1420
   524
    typedef True NeedCopy;
alpar@1041
   525
    typedef typename M2::Key Key;
alpar@1041
   526
    typedef typename M1::Value Value;
alpar@1041
   527
deba@1420
   528
    typedef True NeedCopy;
deba@1420
   529
alpar@1041
   530
    ///Constructor
alpar@1041
   531
alpar@1041
   532
    ///\e
alpar@1041
   533
    ///
alpar@1041
   534
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   535
    Value operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   536
  };
alpar@1041
   537
  ///Returns a \ref ComposeMap class
alpar@1041
   538
alpar@1041
   539
  ///This function just returns a \ref ComposeMap class.
alpar@1219
   540
  ///
alpar@1041
   541
  ///\relates ComposeMap
alpar@1041
   542
  template<class M1,class M2> 
alpar@1041
   543
  inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) 
alpar@1041
   544
  {
alpar@1041
   545
    return ComposeMap<M1,M2>(m1,m2);
alpar@1041
   546
  }
alpar@1219
   547
  
alpar@1219
   548
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   549
alpar@1219
   550
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   551
  ///
alpar@1219
   552
  ///
alpar@1219
   553
  ///This \ref concept::ReadMap "read only map" takes to maps and a
alpar@1219
   554
  ///binary functor and returns the composition of
alpar@1219
   555
  ///two
alpar@1219
   556
  ///given maps unsing the functor. 
alpar@1219
   557
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
alpar@1219
   558
  ///and \c f is of \c F,
alpar@1219
   559
  ///then for
alpar@1219
   560
  ///\code
alpar@1219
   561
  ///  CombineMap<M1,M2,F,V> cm(m1,m2,f);
alpar@1219
   562
  ///\endcode
alpar@1219
   563
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   564
  ///
alpar@1219
   565
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
alpar@1219
   566
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   567
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   568
  ///to \c V.
alpar@1219
   569
  ///\todo Check the requirements.
alpar@1219
   570
deba@1420
   571
  template<class M1,class M2,class F,class V = typename F::result_type> 
alpar@1219
   572
  class CombineMap
alpar@1219
   573
  {
deba@1420
   574
    typename SmartConstReference<M1>::Type m1;
deba@1420
   575
    typename SmartConstReference<M2>::Type m2;
deba@1420
   576
    F f;
alpar@1219
   577
  public:
deba@1420
   578
deba@1420
   579
    typedef True NeedCopy;
alpar@1219
   580
    typedef typename M1::Key Key;
alpar@1219
   581
    typedef V Value;
alpar@1219
   582
alpar@1219
   583
    ///Constructor
alpar@1219
   584
alpar@1219
   585
    ///\e
alpar@1219
   586
    ///
alpar@1219
   587
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
alpar@1219
   588
      : m1(_m1), m2(_m2), f(_f) {};
alpar@1219
   589
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   590
  };
alpar@1219
   591
  
alpar@1219
   592
  ///Returns a \ref CombineMap class
alpar@1219
   593
alpar@1219
   594
  ///This function just returns a \ref CombineMap class.
alpar@1219
   595
  ///
alpar@1219
   596
  ///Only the first template parameter (the value type) must be given.
alpar@1219
   597
  ///
alpar@1219
   598
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   599
  ///\code
alpar@1219
   600
  ///combineMap<double>(m1,m2,std::plus<double>)
alpar@1219
   601
  ///\endcode
alpar@1219
   602
  ///is equivalent with
alpar@1219
   603
  ///\code
alpar@1219
   604
  ///addMap(m1,m2)
alpar@1219
   605
  ///\endcode
alpar@1219
   606
  ///
alpar@1219
   607
  ///\relates CombineMap
deba@1420
   608
  template<class M1,class M2,class F> 
deba@1420
   609
  inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) 
alpar@1219
   610
  {
deba@1420
   611
    return CombineMap<M1,M2,F>(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
alpar@1041
   622
  template<class M> 
alpar@1041
   623
  class NegMap
alpar@1041
   624
  {
deba@1420
   625
    typename SmartConstReference<M>::Type m;
alpar@1041
   626
  public:
deba@1420
   627
deba@1420
   628
    typedef True NeedCopy;
alpar@1041
   629
    typedef typename M::Key Key;
alpar@1041
   630
    typedef typename M::Value Value;
alpar@1041
   631
alpar@1041
   632
    ///Constructor
alpar@1041
   633
alpar@1041
   634
    ///\e
alpar@1041
   635
    ///
alpar@1041
   636
    NegMap(const M &_m) : m(_m) {};
alpar@1044
   637
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   638
  };
alpar@1041
   639
  
alpar@1041
   640
  ///Returns a \ref NegMap class
alpar@1041
   641
alpar@1041
   642
  ///This function just returns a \ref NegMap class.
alpar@1041
   643
  ///\relates NegMap
alpar@1041
   644
  template<class M> 
alpar@1041
   645
  inline NegMap<M> negMap(const M &m) 
alpar@1041
   646
  {
alpar@1041
   647
    return NegMap<M>(m);
alpar@1041
   648
  }
alpar@1041
   649
alpar@1041
   650
alpar@1041
   651
  ///Absolute value of a map
alpar@1041
   652
alpar@1041
   653
  ///This \ref concept::ReadMap "read only map" returns the absolute value
alpar@1041
   654
  ///of the
alpar@1041
   655
  ///value returned by the
alpar@1044
   656
  ///given map. Its \c Key and \c Value will be inherited
alpar@1044
   657
  ///from <tt>M</tt>. <tt>Value</tt>
alpar@1044
   658
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
alpar@1044
   659
  ///operator must be defined for it, of course.
alpar@1044
   660
  ///
alpar@1044
   661
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   662
  ///\code
alpar@1044
   663
  ///  struct _UnConvertible {};
alpar@1044
   664
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   665
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   666
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   667
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   668
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   669
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   670
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   671
  ///\endcode
alpar@1044
   672
  
alpar@1041
   673
alpar@1041
   674
  template<class M> 
alpar@1041
   675
  class AbsMap
alpar@1041
   676
  {
deba@1420
   677
    typename SmartConstReference<M>::Type m;
alpar@1041
   678
  public:
deba@1420
   679
deba@1420
   680
    typedef True NeedCopy;
alpar@1041
   681
    typedef typename M::Key Key;
alpar@1041
   682
    typedef typename M::Value Value;
alpar@1041
   683
alpar@1041
   684
    ///Constructor
alpar@1041
   685
alpar@1041
   686
    ///\e
alpar@1041
   687
    ///
alpar@1041
   688
    AbsMap(const M &_m) : m(_m) {};
alpar@1044
   689
    Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
alpar@1041
   690
  };
alpar@1041
   691
  
alpar@1041
   692
  ///Returns a \ref AbsMap class
alpar@1041
   693
alpar@1041
   694
  ///This function just returns a \ref AbsMap class.
alpar@1041
   695
  ///\relates AbsMap
alpar@1041
   696
  template<class M> 
alpar@1041
   697
  inline AbsMap<M> absMap(const M &m) 
alpar@1041
   698
  {
alpar@1041
   699
    return AbsMap<M>(m);
alpar@1041
   700
  }
alpar@1041
   701
alpar@1402
   702
  ///Converts an STL style functor to a map
alpar@1076
   703
alpar@1076
   704
  ///This \ref concept::ReadMap "read only map" returns the value
alpar@1076
   705
  ///of a
alpar@1076
   706
  ///given map.
alpar@1076
   707
  ///
alpar@1076
   708
  ///Template parameters \c K and \c V will become its
alpar@1076
   709
  ///\c Key and \c Value. They must be given explicitely
alpar@1076
   710
  ///because a functor does not provide such typedefs.
alpar@1076
   711
  ///
alpar@1076
   712
  ///Parameter \c F is the type of the used functor.
alpar@1076
   713
  
alpar@1076
   714
alpar@1076
   715
  template<class K,class V,class F> 
alpar@1076
   716
  class FunctorMap
alpar@1076
   717
  {
alpar@1076
   718
    const F &f;
alpar@1076
   719
  public:
deba@1420
   720
deba@1420
   721
    typedef True NeedCopy;
alpar@1076
   722
    typedef K Key;
alpar@1076
   723
    typedef V Value;
alpar@1076
   724
alpar@1076
   725
    ///Constructor
alpar@1076
   726
alpar@1076
   727
    ///\e
alpar@1076
   728
    ///
alpar@1076
   729
    FunctorMap(const F &_f) : f(_f) {};
alpar@1076
   730
    Value operator[](Key k) const {return f(k);}
alpar@1076
   731
  };
alpar@1076
   732
  
alpar@1076
   733
  ///Returns a \ref FunctorMap class
alpar@1076
   734
alpar@1076
   735
  ///This function just returns a \ref FunctorMap class.
alpar@1076
   736
  ///
alpar@1076
   737
  ///The third template parameter isn't necessary to be given.
alpar@1076
   738
  ///\relates FunctorMap
alpar@1076
   739
  template<class K,class V, class F>
alpar@1076
   740
  inline FunctorMap<K,V,F> functorMap(const F &f) 
alpar@1076
   741
  {
alpar@1076
   742
    return FunctorMap<K,V,F>(f);
alpar@1076
   743
  }
alpar@1076
   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@1223
   751
  ///a ususal \ref concept::ReadMap "readable map", i.e
marci@1172
   752
  ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
alpar@1076
   753
alpar@1076
   754
  template<class M> 
alpar@1076
   755
  class MapFunctor
alpar@1076
   756
  {
deba@1420
   757
    typename SmartConstReference<M>::Type m;
alpar@1076
   758
  public:
deba@1420
   759
deba@1420
   760
    typedef True NeedCopy;
alpar@1223
   761
    typedef typename M::Key argument_type;
alpar@1223
   762
    typedef typename M::Value result_type;
alpar@1076
   763
    typedef typename M::Key Key;
alpar@1076
   764
    typedef typename M::Value Value;
alpar@1076
   765
alpar@1076
   766
    ///Constructor
alpar@1076
   767
alpar@1076
   768
    ///\e
alpar@1076
   769
    ///
alpar@1076
   770
    MapFunctor(const M &_m) : m(_m) {};
alpar@1076
   771
    ///Returns a value of the map
alpar@1076
   772
    
alpar@1076
   773
    ///\e
alpar@1076
   774
    ///
alpar@1076
   775
    Value operator()(Key k) const {return m[k];}
alpar@1076
   776
    ///\e
alpar@1076
   777
    ///
alpar@1076
   778
    Value operator[](Key k) const {return m[k];}
alpar@1076
   779
  };
alpar@1076
   780
  
alpar@1076
   781
  ///Returns a \ref MapFunctor class
alpar@1076
   782
alpar@1076
   783
  ///This function just returns a \ref MapFunctor class.
alpar@1076
   784
  ///\relates MapFunctor
alpar@1076
   785
  template<class M> 
alpar@1076
   786
  inline MapFunctor<M> mapFunctor(const M &m) 
alpar@1076
   787
  {
alpar@1076
   788
    return MapFunctor<M>(m);
alpar@1076
   789
  }
alpar@1076
   790
alpar@1076
   791
alpar@1219
   792
  ///Apply all map setting operations to two maps
alpar@1219
   793
alpar@1219
   794
  ///This map has two \ref concept::WriteMap "writable map"
alpar@1219
   795
  ///parameters and each write request will be passed to both of them.
alpar@1219
   796
  ///If \c M1 is also \ref concept::ReadMap "readable",
alpar@1219
   797
  ///then the read operations will return the
alpar@1317
   798
  ///corresponding values of \c M1.
alpar@1219
   799
  ///
alpar@1219
   800
  ///The \c Key and \c Value will be inherited from \c M1.
alpar@1219
   801
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
alpar@1219
   802
alpar@1219
   803
  template<class M1,class M2> 
alpar@1219
   804
  class ForkMap
alpar@1219
   805
  {
deba@1420
   806
    typename SmartConstReference<M1>::Type m1;
deba@1420
   807
    typename SmartConstReference<M2>::Type m2;
alpar@1219
   808
  public:
deba@1420
   809
deba@1420
   810
    typedef True NeedCopy;
alpar@1219
   811
    typedef typename M1::Key Key;
alpar@1219
   812
    typedef typename M1::Value Value;
alpar@1219
   813
alpar@1219
   814
    ///Constructor
alpar@1219
   815
alpar@1219
   816
    ///\e
alpar@1219
   817
    ///
alpar@1219
   818
    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1219
   819
    Value operator[](Key k) const {return m1[k];}
alpar@1219
   820
    void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
   821
  };
alpar@1219
   822
  
alpar@1219
   823
  ///Returns an \ref ForkMap class
alpar@1219
   824
alpar@1219
   825
  ///This function just returns an \ref ForkMap class.
alpar@1219
   826
  ///\todo How to call these type of functions?
alpar@1219
   827
  ///
alpar@1219
   828
  ///\relates ForkMap
alpar@1219
   829
  ///\todo Wrong scope in Doxygen when \c \\relates is used
alpar@1219
   830
  template<class M1,class M2> 
alpar@1219
   831
  inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2) 
alpar@1219
   832
  {
alpar@1219
   833
    return ForkMap<M1,M2>(m1,m2);
alpar@1219
   834
  }
alpar@1219
   835
alpar@1041
   836
  /// @}
klao@286
   837
  
klao@286
   838
}
alpar@1041
   839
alpar@1041
   840
alpar@921
   841
#endif // LEMON_MAPS_H