lemon/maps.h
author athos
Tue, 21 Jun 2005 15:58:57 +0000
changeset 1508 389a94a1d9eb
parent 1439 2c43106bef85
child 1531 a3b20dd847b5
permissions -rw-r--r--
cplex test included
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/maps.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_MAPS_H
alpar@921
    18
#define LEMON_MAPS_H
klao@286
    19
deba@1420
    20
#include <lemon/graph_utils.h>
deba@1420
    21
#include <lemon/utility.h>
deba@1420
    22
alpar@1041
    23
klao@286
    24
///\file
alpar@1041
    25
///\ingroup maps
klao@286
    26
///\brief Miscellaneous property maps
klao@286
    27
///
klao@959
    28
///\todo This file has the same name as the concept file in concept/,
klao@286
    29
/// and this is not easily detectable in docs...
klao@286
    30
klao@286
    31
#include <map>
klao@286
    32
alpar@921
    33
namespace lemon {
klao@286
    34
alpar@1041
    35
  /// \addtogroup maps
alpar@1041
    36
  /// @{
alpar@1041
    37
alpar@720
    38
  /// Base class of maps.
alpar@720
    39
alpar@805
    40
  /// Base class of maps.
alpar@805
    41
  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
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@1456
   149
    ///\e
alpar@987
   150
    typedef K Key;
alpar@1456
   151
    ///\e
alpar@987
   152
    typedef T Value;
alpar@1456
   153
    ///\e
alpar@987
   154
    typedef T& Reference;
alpar@1456
   155
    ///\e
alpar@987
   156
    typedef const T& ConstReference;
klao@286
   157
klao@286
   158
klao@345
   159
    StdMap() : v() {}
klao@286
   160
    /// Constructor with specified default value
klao@286
   161
    StdMap(const T& _v) : v(_v) {}
klao@286
   162
klao@286
   163
    /// \brief Constructs the map from an appropriate std::map.
klao@286
   164
    ///
klao@286
   165
    /// \warning Inefficient: copies the content of \c m !
klao@286
   166
    StdMap(const parent &m) : parent(m) {}
klao@286
   167
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   168
    /// specifies a default value.
klao@286
   169
    ///
klao@286
   170
    /// \warning Inefficient: copies the content of \c m !
klao@286
   171
    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
klao@286
   172
    
klao@286
   173
    template<typename T1, typename Comp1>
marci@389
   174
    StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 
marci@389
   175
      //FIXME; 
marci@389
   176
    }
klao@286
   177
alpar@987
   178
    Reference operator[](const Key &k) {
klao@346
   179
      return insert(PairType(k,v)).first -> second;
klao@286
   180
    }
alpar@987
   181
    ConstReference operator[](const Key &k) const {
marci@389
   182
      typename parent::iterator i = lower_bound(k);
beckerjc@391
   183
      if (i == parent::end() || parent::key_comp()(k, (*i).first))
klao@286
   184
	return v;
klao@286
   185
      return (*i).second;
klao@286
   186
    }
klao@345
   187
    void set(const Key &k, const T &t) {
klao@346
   188
      parent::operator[](k) = t;
klao@345
   189
    }
klao@286
   190
klao@286
   191
    /// Changes the default value of the map.
klao@286
   192
    /// \return Returns the previous default value.
klao@286
   193
    ///
alpar@805
   194
    /// \warning The value of some keys (which has already been queried, but
klao@286
   195
    /// the value has been unchanged from the default) may change!
klao@286
   196
    T setDefault(const T &_v) { T old=v; v=_v; return old; }
klao@286
   197
klao@286
   198
    template<typename T1>
klao@286
   199
    struct rebind {
klao@286
   200
      typedef StdMap<Key,T1,Compare> other;
klao@286
   201
    };
klao@286
   202
  };
alpar@1041
   203
alpar@1402
   204
  /// @}
alpar@1402
   205
alpar@1402
   206
  /// \addtogroup map_adaptors
alpar@1402
   207
  /// @{
alpar@1402
   208
alpar@1402
   209
alpar@1178
   210
  ///Convert the \c Value of a maps to another type.
alpar@1178
   211
alpar@1178
   212
  ///This \ref concept::ReadMap "read only map"
alpar@1178
   213
  ///converts the \c Value of a maps to type \c T.
alpar@1178
   214
  ///Its \c Value is inherited from \c M.
alpar@1178
   215
  ///
alpar@1178
   216
  ///Actually,
alpar@1178
   217
  ///\code
alpar@1178
   218
  ///  ConvertMap<X> sh(x,v);
alpar@1178
   219
  ///\endcode
alpar@1178
   220
  ///it is equivalent with
alpar@1178
   221
  ///\code
alpar@1178
   222
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1178
   223
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1178
   224
  ///\endcode
alpar@1178
   225
  ///\bug wrong documentation
alpar@1178
   226
  template<class M, class T> 
deba@1420
   227
  class ConvertMap {
deba@1420
   228
    typename SmartConstReference<M>::Type m;
alpar@1178
   229
  public:
deba@1420
   230
deba@1420
   231
    typedef True NeedCopy;
deba@1420
   232
alpar@1456
   233
    ///\e
alpar@1178
   234
    typedef typename M::Key Key;
alpar@1456
   235
    ///\e
alpar@1178
   236
    typedef T Value;
alpar@1178
   237
alpar@1178
   238
    ///Constructor
alpar@1178
   239
alpar@1178
   240
    ///Constructor
alpar@1178
   241
    ///\param _m is the undelying map
alpar@1178
   242
    ///\param _v is the convert value
alpar@1178
   243
    ConvertMap(const M &_m) : m(_m) {};
deba@1346
   244
deba@1346
   245
    /// \brief The subscript operator.
deba@1346
   246
    ///
deba@1346
   247
    /// The subscript operator.
deba@1346
   248
    /// \param edge The edge 
deba@1346
   249
    /// \return The target of the edge 
alpar@1178
   250
    Value operator[](Key k) const {return m[k];}
alpar@1178
   251
  };
alpar@1178
   252
  
alpar@1178
   253
  ///Returns an \ref ConvertMap class
alpar@1178
   254
alpar@1178
   255
  ///This function just returns an \ref ConvertMap class.
alpar@1178
   256
  ///\relates ConvertMap
alpar@1178
   257
  ///\todo The order of the template parameters are changed.
alpar@1178
   258
  template<class T, class M>
alpar@1178
   259
  inline ConvertMap<M,T> convertMap(const M &m) 
alpar@1178
   260
  {
alpar@1178
   261
    return ConvertMap<M,T>(m);
alpar@1178
   262
  }
alpar@1041
   263
alpar@1041
   264
  ///Sum of two maps
alpar@1041
   265
alpar@1041
   266
  ///This \ref concept::ReadMap "read only map" returns the sum of the two
alpar@1041
   267
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   268
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
alpar@1041
   269
alpar@1041
   270
  template<class M1,class M2> 
alpar@1041
   271
  class AddMap
alpar@1041
   272
  {
deba@1420
   273
    typename SmartConstReference<M1>::Type m1;
deba@1420
   274
    typename SmartConstReference<M2>::Type m2;
deba@1420
   275
alpar@1041
   276
  public:
deba@1420
   277
deba@1420
   278
    typedef True NeedCopy;
deba@1420
   279
alpar@1456
   280
    ///\e
alpar@1041
   281
    typedef typename M1::Key Key;
alpar@1456
   282
    ///\e
alpar@1041
   283
    typedef typename M1::Value Value;
alpar@1041
   284
alpar@1041
   285
    ///Constructor
alpar@1041
   286
alpar@1041
   287
    ///\e
alpar@1041
   288
    ///
alpar@1041
   289
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   290
    Value operator[](Key k) const {return m1[k]+m2[k];}
alpar@1041
   291
  };
alpar@1041
   292
  
alpar@1041
   293
  ///Returns an \ref AddMap class
alpar@1041
   294
alpar@1041
   295
  ///This function just returns an \ref AddMap class.
alpar@1041
   296
  ///\todo How to call these type of functions?
alpar@1041
   297
  ///
alpar@1041
   298
  ///\relates AddMap
alpar@1041
   299
  ///\todo Wrong scope in Doxygen when \c \\relates is used
alpar@1041
   300
  template<class M1,class M2> 
alpar@1041
   301
  inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2) 
alpar@1041
   302
  {
alpar@1041
   303
    return AddMap<M1,M2>(m1,m2);
alpar@1041
   304
  }
alpar@1041
   305
alpar@1070
   306
  ///Shift a maps with a constant.
alpar@1070
   307
alpar@1070
   308
  ///This \ref concept::ReadMap "read only map" returns the sum of the
alpar@1070
   309
  ///given map and a constant value.
alpar@1070
   310
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   311
  ///
alpar@1070
   312
  ///Actually,
alpar@1070
   313
  ///\code
alpar@1070
   314
  ///  ShiftMap<X> sh(x,v);
alpar@1070
   315
  ///\endcode
alpar@1070
   316
  ///it is equivalent with
alpar@1070
   317
  ///\code
alpar@1070
   318
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   319
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1070
   320
  ///\endcode
alpar@1070
   321
  template<class M> 
alpar@1070
   322
  class ShiftMap
alpar@1070
   323
  {
deba@1420
   324
    typename SmartConstReference<M>::Type m;
alpar@1070
   325
    typename M::Value v;
alpar@1070
   326
  public:
deba@1420
   327
deba@1420
   328
    typedef True NeedCopy;
alpar@1456
   329
    ///\e
alpar@1070
   330
    typedef typename M::Key Key;
alpar@1456
   331
    ///\e
alpar@1070
   332
    typedef typename M::Value Value;
alpar@1070
   333
alpar@1070
   334
    ///Constructor
alpar@1070
   335
alpar@1070
   336
    ///Constructor
alpar@1070
   337
    ///\param _m is the undelying map
alpar@1070
   338
    ///\param _v is the shift value
alpar@1070
   339
    ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
alpar@1070
   340
    Value operator[](Key k) const {return m[k]+v;}
alpar@1070
   341
  };
alpar@1070
   342
  
alpar@1070
   343
  ///Returns an \ref ShiftMap class
alpar@1070
   344
alpar@1070
   345
  ///This function just returns an \ref ShiftMap class.
alpar@1070
   346
  ///\relates ShiftMap
alpar@1070
   347
  ///\todo A better name is required.
alpar@1070
   348
  template<class M> 
alpar@1070
   349
  inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) 
alpar@1070
   350
  {
alpar@1070
   351
    return ShiftMap<M>(m,v);
alpar@1070
   352
  }
alpar@1070
   353
alpar@1041
   354
  ///Difference of two maps
alpar@1041
   355
alpar@1041
   356
  ///This \ref concept::ReadMap "read only map" returns the difference
alpar@1041
   357
  ///of the values returned by the two
alpar@1041
   358
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   359
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   360
alpar@1041
   361
  template<class M1,class M2> 
alpar@1041
   362
  class SubMap
alpar@1041
   363
  {
deba@1420
   364
    typename SmartConstReference<M1>::Type m1;
deba@1420
   365
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   366
  public:
deba@1420
   367
deba@1420
   368
    typedef True NeedCopy;
alpar@1456
   369
    ///\e
alpar@1041
   370
    typedef typename M1::Key Key;
alpar@1456
   371
    ///\e
alpar@1041
   372
    typedef typename M1::Value Value;
alpar@1041
   373
alpar@1041
   374
    ///Constructor
alpar@1041
   375
alpar@1041
   376
    ///\e
alpar@1041
   377
    ///
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
alpar@1041
   387
  template<class M1,class M2> 
alpar@1041
   388
  inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) 
alpar@1041
   389
  {
alpar@1041
   390
    return SubMap<M1,M2>(m1,m2);
alpar@1041
   391
  }
alpar@1041
   392
alpar@1041
   393
  ///Product of two maps
alpar@1041
   394
alpar@1041
   395
  ///This \ref concept::ReadMap "read only map" returns the product of the
alpar@1041
   396
  ///values returned by the two
alpar@1041
   397
  ///given
alpar@1041
   398
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   399
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   400
alpar@1041
   401
  template<class M1,class M2> 
alpar@1041
   402
  class MulMap
alpar@1041
   403
  {
deba@1420
   404
    typename SmartConstReference<M1>::Type m1;
deba@1420
   405
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   406
  public:
deba@1420
   407
deba@1420
   408
    typedef True NeedCopy;
alpar@1456
   409
    ///\e
alpar@1041
   410
    typedef typename M1::Key Key;
alpar@1456
   411
    ///\e
alpar@1041
   412
    typedef typename M1::Value Value;
alpar@1041
   413
alpar@1041
   414
    ///Constructor
alpar@1041
   415
alpar@1041
   416
    ///\e
alpar@1041
   417
    ///
alpar@1041
   418
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   419
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   420
  };
alpar@1041
   421
  
alpar@1041
   422
  ///Returns a \ref MulMap class
alpar@1041
   423
alpar@1041
   424
  ///This function just returns a \ref MulMap class.
alpar@1041
   425
  ///\relates MulMap
alpar@1041
   426
  template<class M1,class M2> 
alpar@1041
   427
  inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) 
alpar@1041
   428
  {
alpar@1041
   429
    return MulMap<M1,M2>(m1,m2);
alpar@1041
   430
  }
alpar@1041
   431
 
alpar@1070
   432
  ///Scale a maps with a constant.
alpar@1070
   433
alpar@1070
   434
  ///This \ref concept::ReadMap "read only map" returns the value of the
alpar@1070
   435
  ///given map multipied with a constant value.
alpar@1070
   436
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   437
  ///
alpar@1070
   438
  ///Actually,
alpar@1070
   439
  ///\code
alpar@1070
   440
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   441
  ///\endcode
alpar@1070
   442
  ///it is equivalent with
alpar@1070
   443
  ///\code
alpar@1070
   444
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   445
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   446
  ///\endcode
alpar@1070
   447
  template<class M> 
alpar@1070
   448
  class ScaleMap
alpar@1070
   449
  {
deba@1420
   450
    typename SmartConstReference<M>::Type m;
alpar@1070
   451
    typename M::Value v;
alpar@1070
   452
  public:
deba@1420
   453
deba@1420
   454
    typedef True NeedCopy;
alpar@1456
   455
    ///\e
alpar@1070
   456
    typedef typename M::Key Key;
alpar@1456
   457
    ///\e
alpar@1070
   458
    typedef typename M::Value Value;
alpar@1070
   459
alpar@1070
   460
    ///Constructor
alpar@1070
   461
alpar@1070
   462
    ///Constructor
alpar@1070
   463
    ///\param _m is the undelying map
alpar@1070
   464
    ///\param _v is the scaling value
alpar@1070
   465
    ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
alpar@1070
   466
    Value operator[](Key k) const {return m[k]*v;}
alpar@1070
   467
  };
alpar@1070
   468
  
alpar@1070
   469
  ///Returns an \ref ScaleMap class
alpar@1070
   470
alpar@1070
   471
  ///This function just returns an \ref ScaleMap class.
alpar@1070
   472
  ///\relates ScaleMap
alpar@1070
   473
  ///\todo A better name is required.
alpar@1070
   474
  template<class M> 
alpar@1070
   475
  inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) 
alpar@1070
   476
  {
alpar@1070
   477
    return ScaleMap<M>(m,v);
alpar@1070
   478
  }
alpar@1070
   479
alpar@1041
   480
  ///Quotient of two maps
alpar@1041
   481
alpar@1041
   482
  ///This \ref concept::ReadMap "read only map" returns the quotient of the
alpar@1041
   483
  ///values returned by the two
alpar@1041
   484
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   485
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   486
alpar@1041
   487
  template<class M1,class M2> 
alpar@1041
   488
  class DivMap
alpar@1041
   489
  {
deba@1420
   490
    typename SmartConstReference<M1>::Type m1;
deba@1420
   491
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   492
  public:
deba@1420
   493
deba@1420
   494
    typedef True NeedCopy;
alpar@1456
   495
    ///\e
alpar@1041
   496
    typedef typename M1::Key Key;
alpar@1456
   497
    ///\e
alpar@1041
   498
    typedef typename M1::Value Value;
alpar@1041
   499
alpar@1041
   500
    ///Constructor
alpar@1041
   501
alpar@1041
   502
    ///\e
alpar@1041
   503
    ///
alpar@1041
   504
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   505
    Value operator[](Key k) const {return m1[k]/m2[k];}
alpar@1041
   506
  };
alpar@1041
   507
  
alpar@1041
   508
  ///Returns a \ref DivMap class
alpar@1041
   509
alpar@1041
   510
  ///This function just returns a \ref DivMap class.
alpar@1041
   511
  ///\relates DivMap
alpar@1041
   512
  template<class M1,class M2> 
alpar@1041
   513
  inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) 
alpar@1041
   514
  {
alpar@1041
   515
    return DivMap<M1,M2>(m1,m2);
alpar@1041
   516
  }
alpar@1041
   517
  
alpar@1041
   518
  ///Composition of two maps
alpar@1041
   519
alpar@1041
   520
  ///This \ref concept::ReadMap "read only map" returns the composition of
alpar@1041
   521
  ///two
alpar@1041
   522
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
alpar@1041
   523
  ///of \c M2,
alpar@1041
   524
  ///then for
alpar@1041
   525
  ///\code
alpar@1041
   526
  ///  ComposeMap<M1,M2> cm(m1,m2);
alpar@1041
   527
  ///\endcode
alpar@1044
   528
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
alpar@1041
   529
  ///
alpar@1041
   530
  ///Its \c Key is inherited from \c M2 and its \c Value is from
alpar@1041
   531
  ///\c M1.
alpar@1041
   532
  ///The \c M2::Value must be convertible to \c M1::Key.
alpar@1041
   533
  ///\todo Check the requirements.
alpar@1041
   534
alpar@1041
   535
  template<class M1,class M2> 
alpar@1041
   536
  class ComposeMap
alpar@1041
   537
  {
deba@1420
   538
    typename SmartConstReference<M1>::Type m1;
deba@1420
   539
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   540
  public:
deba@1420
   541
deba@1420
   542
    typedef True NeedCopy;
alpar@1456
   543
    ///\e
alpar@1041
   544
    typedef typename M2::Key Key;
alpar@1456
   545
    ///\e
alpar@1041
   546
    typedef typename M1::Value Value;
alpar@1041
   547
alpar@1041
   548
    ///Constructor
alpar@1041
   549
alpar@1041
   550
    ///\e
alpar@1041
   551
    ///
alpar@1041
   552
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   553
    Value operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   554
  };
alpar@1041
   555
  ///Returns a \ref ComposeMap class
alpar@1041
   556
alpar@1041
   557
  ///This function just returns a \ref ComposeMap class.
alpar@1219
   558
  ///
alpar@1041
   559
  ///\relates ComposeMap
alpar@1041
   560
  template<class M1,class M2> 
alpar@1041
   561
  inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) 
alpar@1041
   562
  {
alpar@1041
   563
    return ComposeMap<M1,M2>(m1,m2);
alpar@1041
   564
  }
alpar@1219
   565
  
alpar@1219
   566
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   567
alpar@1219
   568
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   569
  ///
alpar@1219
   570
  ///
alpar@1219
   571
  ///This \ref concept::ReadMap "read only map" takes to maps and a
alpar@1219
   572
  ///binary functor and returns the composition of
alpar@1219
   573
  ///two
alpar@1219
   574
  ///given maps unsing the functor. 
alpar@1219
   575
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
alpar@1219
   576
  ///and \c f is of \c F,
alpar@1219
   577
  ///then for
alpar@1219
   578
  ///\code
alpar@1219
   579
  ///  CombineMap<M1,M2,F,V> cm(m1,m2,f);
alpar@1219
   580
  ///\endcode
alpar@1219
   581
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   582
  ///
alpar@1219
   583
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
alpar@1219
   584
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   585
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   586
  ///to \c V.
alpar@1219
   587
  ///\todo Check the requirements.
alpar@1219
   588
deba@1420
   589
  template<class M1,class M2,class F,class V = typename F::result_type> 
alpar@1219
   590
  class CombineMap
alpar@1219
   591
  {
deba@1420
   592
    typename SmartConstReference<M1>::Type m1;
deba@1420
   593
    typename SmartConstReference<M2>::Type m2;
deba@1420
   594
    F f;
alpar@1219
   595
  public:
deba@1420
   596
deba@1420
   597
    typedef True NeedCopy;
alpar@1456
   598
    ///\e
alpar@1219
   599
    typedef typename M1::Key Key;
alpar@1456
   600
    ///\e
alpar@1219
   601
    typedef V Value;
alpar@1219
   602
alpar@1219
   603
    ///Constructor
alpar@1219
   604
alpar@1219
   605
    ///\e
alpar@1219
   606
    ///
alpar@1219
   607
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
alpar@1219
   608
      : m1(_m1), m2(_m2), f(_f) {};
alpar@1219
   609
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   610
  };
alpar@1219
   611
  
alpar@1219
   612
  ///Returns a \ref CombineMap class
alpar@1219
   613
alpar@1219
   614
  ///This function just returns a \ref CombineMap class.
alpar@1219
   615
  ///
alpar@1219
   616
  ///Only the first template parameter (the value type) must be given.
alpar@1219
   617
  ///
alpar@1219
   618
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   619
  ///\code
alpar@1219
   620
  ///combineMap<double>(m1,m2,std::plus<double>)
alpar@1219
   621
  ///\endcode
alpar@1219
   622
  ///is equivalent with
alpar@1219
   623
  ///\code
alpar@1219
   624
  ///addMap(m1,m2)
alpar@1219
   625
  ///\endcode
alpar@1219
   626
  ///
alpar@1219
   627
  ///\relates CombineMap
deba@1420
   628
  template<class M1,class M2,class F> 
deba@1420
   629
  inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) 
alpar@1219
   630
  {
deba@1420
   631
    return CombineMap<M1,M2,F>(m1,m2,f);
alpar@1219
   632
  }
alpar@1041
   633
alpar@1041
   634
  ///Negative value of a map
alpar@1041
   635
alpar@1041
   636
  ///This \ref concept::ReadMap "read only map" returns the negative
alpar@1041
   637
  ///value of the
alpar@1041
   638
  ///value returned by the
alpar@1041
   639
  ///given map. Its \c Key and \c Value will be inherited from \c M.
alpar@1041
   640
  ///The unary \c - operator must be defined for \c Value, of course.
alpar@1041
   641
alpar@1041
   642
  template<class M> 
alpar@1041
   643
  class NegMap
alpar@1041
   644
  {
deba@1420
   645
    typename SmartConstReference<M>::Type m;
alpar@1041
   646
  public:
deba@1420
   647
deba@1420
   648
    typedef True NeedCopy;
alpar@1456
   649
    ///\e
alpar@1041
   650
    typedef typename M::Key Key;
alpar@1456
   651
    ///\e
alpar@1041
   652
    typedef typename M::Value Value;
alpar@1041
   653
alpar@1041
   654
    ///Constructor
alpar@1041
   655
alpar@1041
   656
    ///\e
alpar@1041
   657
    ///
alpar@1041
   658
    NegMap(const M &_m) : m(_m) {};
alpar@1044
   659
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   660
  };
alpar@1041
   661
  
alpar@1041
   662
  ///Returns a \ref NegMap class
alpar@1041
   663
alpar@1041
   664
  ///This function just returns a \ref NegMap class.
alpar@1041
   665
  ///\relates NegMap
alpar@1041
   666
  template<class M> 
alpar@1041
   667
  inline NegMap<M> negMap(const M &m) 
alpar@1041
   668
  {
alpar@1041
   669
    return NegMap<M>(m);
alpar@1041
   670
  }
alpar@1041
   671
alpar@1041
   672
alpar@1041
   673
  ///Absolute value of a map
alpar@1041
   674
alpar@1041
   675
  ///This \ref concept::ReadMap "read only map" returns the absolute value
alpar@1041
   676
  ///of the
alpar@1041
   677
  ///value returned by the
alpar@1044
   678
  ///given map. Its \c Key and \c Value will be inherited
alpar@1044
   679
  ///from <tt>M</tt>. <tt>Value</tt>
alpar@1044
   680
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
alpar@1044
   681
  ///operator must be defined for it, of course.
alpar@1044
   682
  ///
alpar@1044
   683
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   684
  ///\code
alpar@1044
   685
  ///  struct _UnConvertible {};
alpar@1044
   686
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   687
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   688
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   689
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   690
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   691
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   692
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   693
  ///\endcode
alpar@1044
   694
  
alpar@1041
   695
alpar@1041
   696
  template<class M> 
alpar@1041
   697
  class AbsMap
alpar@1041
   698
  {
deba@1420
   699
    typename SmartConstReference<M>::Type m;
alpar@1041
   700
  public:
deba@1420
   701
deba@1420
   702
    typedef True NeedCopy;
alpar@1456
   703
    ///\e
alpar@1041
   704
    typedef typename M::Key Key;
alpar@1456
   705
    ///\e
alpar@1041
   706
    typedef typename M::Value Value;
alpar@1041
   707
alpar@1041
   708
    ///Constructor
alpar@1041
   709
alpar@1041
   710
    ///\e
alpar@1041
   711
    ///
alpar@1041
   712
    AbsMap(const M &_m) : m(_m) {};
alpar@1044
   713
    Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
alpar@1041
   714
  };
alpar@1041
   715
  
alpar@1041
   716
  ///Returns a \ref AbsMap class
alpar@1041
   717
alpar@1041
   718
  ///This function just returns a \ref AbsMap class.
alpar@1041
   719
  ///\relates AbsMap
alpar@1041
   720
  template<class M> 
alpar@1041
   721
  inline AbsMap<M> absMap(const M &m) 
alpar@1041
   722
  {
alpar@1041
   723
    return AbsMap<M>(m);
alpar@1041
   724
  }
alpar@1041
   725
alpar@1402
   726
  ///Converts an STL style functor to a map
alpar@1076
   727
alpar@1076
   728
  ///This \ref concept::ReadMap "read only map" returns the value
alpar@1076
   729
  ///of a
alpar@1076
   730
  ///given map.
alpar@1076
   731
  ///
alpar@1076
   732
  ///Template parameters \c K and \c V will become its
alpar@1076
   733
  ///\c Key and \c Value. They must be given explicitely
alpar@1076
   734
  ///because a functor does not provide such typedefs.
alpar@1076
   735
  ///
alpar@1076
   736
  ///Parameter \c F is the type of the used functor.
alpar@1076
   737
  
alpar@1076
   738
alpar@1076
   739
  template<class K,class V,class F> 
alpar@1076
   740
  class FunctorMap
alpar@1076
   741
  {
alpar@1076
   742
    const F &f;
alpar@1076
   743
  public:
deba@1420
   744
deba@1420
   745
    typedef True NeedCopy;
alpar@1456
   746
    ///\e
alpar@1076
   747
    typedef K Key;
alpar@1456
   748
    ///\e
alpar@1076
   749
    typedef V Value;
alpar@1076
   750
alpar@1076
   751
    ///Constructor
alpar@1076
   752
alpar@1076
   753
    ///\e
alpar@1076
   754
    ///
alpar@1076
   755
    FunctorMap(const F &_f) : f(_f) {};
alpar@1076
   756
    Value operator[](Key k) const {return f(k);}
alpar@1076
   757
  };
alpar@1076
   758
  
alpar@1076
   759
  ///Returns a \ref FunctorMap class
alpar@1076
   760
alpar@1076
   761
  ///This function just returns a \ref FunctorMap class.
alpar@1076
   762
  ///
alpar@1076
   763
  ///The third template parameter isn't necessary to be given.
alpar@1076
   764
  ///\relates FunctorMap
alpar@1076
   765
  template<class K,class V, class F>
alpar@1076
   766
  inline FunctorMap<K,V,F> functorMap(const F &f) 
alpar@1076
   767
  {
alpar@1076
   768
    return FunctorMap<K,V,F>(f);
alpar@1076
   769
  }
alpar@1076
   770
alpar@1219
   771
  ///Converts a map to an STL style (unary) functor
alpar@1076
   772
alpar@1219
   773
  ///This class Converts a map to an STL style (unary) functor.
alpar@1076
   774
  ///that is it provides an <tt>operator()</tt> to read its values.
alpar@1076
   775
  ///
alpar@1223
   776
  ///For the sake of convenience it also works as
alpar@1223
   777
  ///a ususal \ref concept::ReadMap "readable map", i.e
marci@1172
   778
  ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
alpar@1076
   779
alpar@1076
   780
  template<class M> 
alpar@1076
   781
  class MapFunctor
alpar@1076
   782
  {
deba@1420
   783
    typename SmartConstReference<M>::Type m;
alpar@1076
   784
  public:
deba@1420
   785
deba@1420
   786
    typedef True NeedCopy;
alpar@1456
   787
    ///\e
alpar@1223
   788
    typedef typename M::Key argument_type;
alpar@1456
   789
    ///\e
alpar@1223
   790
    typedef typename M::Value result_type;
alpar@1456
   791
    ///\e
alpar@1076
   792
    typedef typename M::Key Key;
alpar@1456
   793
    ///\e
alpar@1076
   794
    typedef typename M::Value Value;
alpar@1076
   795
alpar@1076
   796
    ///Constructor
alpar@1076
   797
alpar@1076
   798
    ///\e
alpar@1076
   799
    ///
alpar@1076
   800
    MapFunctor(const M &_m) : m(_m) {};
alpar@1076
   801
    ///Returns a value of the map
alpar@1076
   802
    
alpar@1076
   803
    ///\e
alpar@1076
   804
    ///
alpar@1076
   805
    Value operator()(Key k) const {return m[k];}
alpar@1076
   806
    ///\e
alpar@1076
   807
    ///
alpar@1076
   808
    Value operator[](Key k) const {return m[k];}
alpar@1076
   809
  };
alpar@1076
   810
  
alpar@1076
   811
  ///Returns a \ref MapFunctor class
alpar@1076
   812
alpar@1076
   813
  ///This function just returns a \ref MapFunctor class.
alpar@1076
   814
  ///\relates MapFunctor
alpar@1076
   815
  template<class M> 
alpar@1076
   816
  inline MapFunctor<M> mapFunctor(const M &m) 
alpar@1076
   817
  {
alpar@1076
   818
    return MapFunctor<M>(m);
alpar@1076
   819
  }
alpar@1076
   820
alpar@1076
   821
alpar@1219
   822
  ///Apply all map setting operations to two maps
alpar@1219
   823
alpar@1219
   824
  ///This map has two \ref concept::WriteMap "writable map"
alpar@1219
   825
  ///parameters and each write request will be passed to both of them.
alpar@1219
   826
  ///If \c M1 is also \ref concept::ReadMap "readable",
alpar@1219
   827
  ///then the read operations will return the
alpar@1317
   828
  ///corresponding values of \c M1.
alpar@1219
   829
  ///
alpar@1219
   830
  ///The \c Key and \c Value will be inherited from \c M1.
alpar@1219
   831
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
alpar@1219
   832
alpar@1219
   833
  template<class M1,class M2> 
alpar@1219
   834
  class ForkMap
alpar@1219
   835
  {
deba@1420
   836
    typename SmartConstReference<M1>::Type m1;
deba@1420
   837
    typename SmartConstReference<M2>::Type m2;
alpar@1219
   838
  public:
deba@1420
   839
deba@1420
   840
    typedef True NeedCopy;
alpar@1456
   841
    ///\e
alpar@1219
   842
    typedef typename M1::Key Key;
alpar@1456
   843
    ///\e
alpar@1219
   844
    typedef typename M1::Value Value;
alpar@1219
   845
alpar@1219
   846
    ///Constructor
alpar@1219
   847
alpar@1219
   848
    ///\e
alpar@1219
   849
    ///
alpar@1219
   850
    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1219
   851
    Value operator[](Key k) const {return m1[k];}
alpar@1219
   852
    void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
   853
  };
alpar@1219
   854
  
alpar@1219
   855
  ///Returns an \ref ForkMap class
alpar@1219
   856
alpar@1219
   857
  ///This function just returns an \ref ForkMap class.
alpar@1219
   858
  ///\todo How to call these type of functions?
alpar@1219
   859
  ///
alpar@1219
   860
  ///\relates ForkMap
alpar@1219
   861
  ///\todo Wrong scope in Doxygen when \c \\relates is used
alpar@1219
   862
  template<class M1,class M2> 
alpar@1219
   863
  inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2) 
alpar@1219
   864
  {
alpar@1219
   865
    return ForkMap<M1,M2>(m1,m2);
alpar@1219
   866
  }
alpar@1219
   867
alpar@1456
   868
alpar@1456
   869
  
alpar@1456
   870
  /* ************* BOOL MAPS ******************* */
alpar@1456
   871
  
alpar@1456
   872
  ///Logical 'not' of a map
alpar@1456
   873
  
alpar@1456
   874
  ///This bool \ref concept::ReadMap "read only map" returns the 
alpar@1456
   875
  ///logical negation of
alpar@1456
   876
  ///value returned by the
alpar@1456
   877
  ///given map. Its \c Key and will be inherited from \c M,
alpar@1456
   878
  ///its Value is <tt>bool</tt>.
alpar@1456
   879
alpar@1456
   880
  template<class M> 
alpar@1456
   881
  class NotMap
alpar@1456
   882
  {
alpar@1456
   883
    typename SmartConstReference<M>::Type m;
alpar@1456
   884
  public:
alpar@1456
   885
alpar@1456
   886
    typedef True NeedCopy;
alpar@1456
   887
    ///\e
alpar@1456
   888
    typedef typename M::Key Key;
alpar@1456
   889
    ///\e
alpar@1456
   890
    typedef bool Value;
alpar@1456
   891
alpar@1456
   892
    ///Constructor
alpar@1456
   893
alpar@1456
   894
    ///\e
alpar@1456
   895
    ///
alpar@1456
   896
    NotMap(const M &_m) : m(_m) {};
alpar@1456
   897
    Value operator[](Key k) const {return !m[k];}
alpar@1456
   898
  };
alpar@1456
   899
  
alpar@1456
   900
  ///Returns a \ref NotMap class
alpar@1456
   901
  
alpar@1456
   902
  ///This function just returns a \ref NotMap class.
alpar@1456
   903
  ///\relates NotMap
alpar@1456
   904
  template<class M> 
alpar@1456
   905
  inline NotMap<M> notMap(const M &m) 
alpar@1456
   906
  {
alpar@1456
   907
    return NotMap<M>(m);
alpar@1456
   908
  }
alpar@1456
   909
alpar@1456
   910
alpar@1456
   911
alpar@1456
   912
alpar@1456
   913
alpar@1456
   914
alpar@1456
   915
alpar@1456
   916
alpar@1456
   917
alpar@1456
   918
alpar@1456
   919
alpar@1041
   920
  /// @}
klao@286
   921
}
alpar@1041
   922
alpar@921
   923
#endif // LEMON_MAPS_H