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