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