lemon/maps.h
author deba
Mon, 04 Jul 2005 16:11:00 +0000
changeset 1533 43c7b3085212
parent 1456 5289afbdb720
child 1536 308150155bb5
permissions -rw-r--r--
Specialization for string literals.
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
deba@1531
   209
  /// \brief Identity mapping.
deba@1531
   210
  ///
deba@1531
   211
  /// This mapping gives back the given key as value without any
deba@1531
   212
  /// modification. 
deba@1531
   213
  template <typename T>
deba@1531
   214
  class IdentityMap {
deba@1531
   215
  public:
deba@1531
   216
    typedef T Key;
deba@1531
   217
    typedef T Value;
deba@1531
   218
deba@1531
   219
    const Value& operator[](const Key& t) const {
deba@1531
   220
      return t;
deba@1531
   221
    }
deba@1531
   222
  };
alpar@1402
   223
alpar@1178
   224
  ///Convert the \c Value of a maps to another type.
alpar@1178
   225
alpar@1178
   226
  ///This \ref concept::ReadMap "read only map"
alpar@1178
   227
  ///converts the \c Value of a maps to type \c T.
alpar@1178
   228
  ///Its \c Value is inherited from \c M.
alpar@1178
   229
  ///
alpar@1178
   230
  ///Actually,
alpar@1178
   231
  ///\code
alpar@1178
   232
  ///  ConvertMap<X> sh(x,v);
alpar@1178
   233
  ///\endcode
alpar@1178
   234
  ///it is equivalent with
alpar@1178
   235
  ///\code
alpar@1178
   236
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1178
   237
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1178
   238
  ///\endcode
alpar@1178
   239
  ///\bug wrong documentation
alpar@1178
   240
  template<class M, class T> 
deba@1420
   241
  class ConvertMap {
deba@1420
   242
    typename SmartConstReference<M>::Type m;
alpar@1178
   243
  public:
deba@1420
   244
deba@1420
   245
    typedef True NeedCopy;
deba@1420
   246
alpar@1456
   247
    ///\e
alpar@1178
   248
    typedef typename M::Key Key;
alpar@1456
   249
    ///\e
alpar@1178
   250
    typedef T Value;
alpar@1178
   251
alpar@1178
   252
    ///Constructor
alpar@1178
   253
alpar@1178
   254
    ///Constructor
alpar@1178
   255
    ///\param _m is the undelying map
alpar@1178
   256
    ///\param _v is the convert value
alpar@1178
   257
    ConvertMap(const M &_m) : m(_m) {};
deba@1346
   258
deba@1346
   259
    /// \brief The subscript operator.
deba@1346
   260
    ///
deba@1346
   261
    /// The subscript operator.
deba@1346
   262
    /// \param edge The edge 
deba@1346
   263
    /// \return The target of the edge 
alpar@1178
   264
    Value operator[](Key k) const {return m[k];}
alpar@1178
   265
  };
alpar@1178
   266
  
alpar@1178
   267
  ///Returns an \ref ConvertMap class
alpar@1178
   268
alpar@1178
   269
  ///This function just returns an \ref ConvertMap class.
alpar@1178
   270
  ///\relates ConvertMap
alpar@1178
   271
  ///\todo The order of the template parameters are changed.
alpar@1178
   272
  template<class T, class M>
alpar@1178
   273
  inline ConvertMap<M,T> convertMap(const M &m) 
alpar@1178
   274
  {
alpar@1178
   275
    return ConvertMap<M,T>(m);
alpar@1178
   276
  }
alpar@1041
   277
alpar@1041
   278
  ///Sum of two maps
alpar@1041
   279
alpar@1041
   280
  ///This \ref concept::ReadMap "read only map" returns the sum of the two
alpar@1041
   281
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   282
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
alpar@1041
   283
alpar@1041
   284
  template<class M1,class M2> 
alpar@1041
   285
  class AddMap
alpar@1041
   286
  {
deba@1420
   287
    typename SmartConstReference<M1>::Type m1;
deba@1420
   288
    typename SmartConstReference<M2>::Type m2;
deba@1420
   289
alpar@1041
   290
  public:
deba@1420
   291
deba@1420
   292
    typedef True NeedCopy;
deba@1420
   293
alpar@1456
   294
    ///\e
alpar@1041
   295
    typedef typename M1::Key Key;
alpar@1456
   296
    ///\e
alpar@1041
   297
    typedef typename M1::Value Value;
alpar@1041
   298
alpar@1041
   299
    ///Constructor
alpar@1041
   300
alpar@1041
   301
    ///\e
alpar@1041
   302
    ///
alpar@1041
   303
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   304
    Value operator[](Key k) const {return m1[k]+m2[k];}
alpar@1041
   305
  };
alpar@1041
   306
  
alpar@1041
   307
  ///Returns an \ref AddMap class
alpar@1041
   308
alpar@1041
   309
  ///This function just returns an \ref AddMap class.
alpar@1041
   310
  ///\todo How to call these type of functions?
alpar@1041
   311
  ///
alpar@1041
   312
  ///\relates AddMap
alpar@1041
   313
  ///\todo Wrong scope in Doxygen when \c \\relates is used
alpar@1041
   314
  template<class M1,class M2> 
alpar@1041
   315
  inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2) 
alpar@1041
   316
  {
alpar@1041
   317
    return AddMap<M1,M2>(m1,m2);
alpar@1041
   318
  }
alpar@1041
   319
alpar@1070
   320
  ///Shift a maps with a constant.
alpar@1070
   321
alpar@1070
   322
  ///This \ref concept::ReadMap "read only map" returns the sum of the
alpar@1070
   323
  ///given map and a constant value.
alpar@1070
   324
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   325
  ///
alpar@1070
   326
  ///Actually,
alpar@1070
   327
  ///\code
alpar@1070
   328
  ///  ShiftMap<X> sh(x,v);
alpar@1070
   329
  ///\endcode
alpar@1070
   330
  ///it is equivalent with
alpar@1070
   331
  ///\code
alpar@1070
   332
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   333
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1070
   334
  ///\endcode
alpar@1070
   335
  template<class M> 
alpar@1070
   336
  class ShiftMap
alpar@1070
   337
  {
deba@1420
   338
    typename SmartConstReference<M>::Type m;
alpar@1070
   339
    typename M::Value v;
alpar@1070
   340
  public:
deba@1420
   341
deba@1420
   342
    typedef True NeedCopy;
alpar@1456
   343
    ///\e
alpar@1070
   344
    typedef typename M::Key Key;
alpar@1456
   345
    ///\e
alpar@1070
   346
    typedef typename M::Value Value;
alpar@1070
   347
alpar@1070
   348
    ///Constructor
alpar@1070
   349
alpar@1070
   350
    ///Constructor
alpar@1070
   351
    ///\param _m is the undelying map
alpar@1070
   352
    ///\param _v is the shift value
alpar@1070
   353
    ShiftMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
alpar@1070
   354
    Value operator[](Key k) const {return m[k]+v;}
alpar@1070
   355
  };
alpar@1070
   356
  
alpar@1070
   357
  ///Returns an \ref ShiftMap class
alpar@1070
   358
alpar@1070
   359
  ///This function just returns an \ref ShiftMap class.
alpar@1070
   360
  ///\relates ShiftMap
alpar@1070
   361
  ///\todo A better name is required.
alpar@1070
   362
  template<class M> 
alpar@1070
   363
  inline ShiftMap<M> shiftMap(const M &m,const typename M::Value &v) 
alpar@1070
   364
  {
alpar@1070
   365
    return ShiftMap<M>(m,v);
alpar@1070
   366
  }
alpar@1070
   367
alpar@1041
   368
  ///Difference of two maps
alpar@1041
   369
alpar@1041
   370
  ///This \ref concept::ReadMap "read only map" returns the difference
alpar@1041
   371
  ///of the values returned by the two
alpar@1041
   372
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   373
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   374
alpar@1041
   375
  template<class M1,class M2> 
alpar@1041
   376
  class SubMap
alpar@1041
   377
  {
deba@1420
   378
    typename SmartConstReference<M1>::Type m1;
deba@1420
   379
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   380
  public:
deba@1420
   381
deba@1420
   382
    typedef True NeedCopy;
alpar@1456
   383
    ///\e
alpar@1041
   384
    typedef typename M1::Key Key;
alpar@1456
   385
    ///\e
alpar@1041
   386
    typedef typename M1::Value Value;
alpar@1041
   387
alpar@1041
   388
    ///Constructor
alpar@1041
   389
alpar@1041
   390
    ///\e
alpar@1041
   391
    ///
alpar@1041
   392
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   393
    Value operator[](Key k) const {return m1[k]-m2[k];}
alpar@1041
   394
  };
alpar@1041
   395
  
alpar@1041
   396
  ///Returns a \ref SubMap class
alpar@1041
   397
alpar@1041
   398
  ///This function just returns a \ref SubMap class.
alpar@1041
   399
  ///
alpar@1041
   400
  ///\relates SubMap
alpar@1041
   401
  template<class M1,class M2> 
alpar@1041
   402
  inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2) 
alpar@1041
   403
  {
alpar@1041
   404
    return SubMap<M1,M2>(m1,m2);
alpar@1041
   405
  }
alpar@1041
   406
alpar@1041
   407
  ///Product of two maps
alpar@1041
   408
alpar@1041
   409
  ///This \ref concept::ReadMap "read only map" returns the product of the
alpar@1041
   410
  ///values returned by the two
alpar@1041
   411
  ///given
alpar@1041
   412
  ///maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   413
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   414
alpar@1041
   415
  template<class M1,class M2> 
alpar@1041
   416
  class MulMap
alpar@1041
   417
  {
deba@1420
   418
    typename SmartConstReference<M1>::Type m1;
deba@1420
   419
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   420
  public:
deba@1420
   421
deba@1420
   422
    typedef True NeedCopy;
alpar@1456
   423
    ///\e
alpar@1041
   424
    typedef typename M1::Key Key;
alpar@1456
   425
    ///\e
alpar@1041
   426
    typedef typename M1::Value Value;
alpar@1041
   427
alpar@1041
   428
    ///Constructor
alpar@1041
   429
alpar@1041
   430
    ///\e
alpar@1041
   431
    ///
alpar@1041
   432
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   433
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   434
  };
alpar@1041
   435
  
alpar@1041
   436
  ///Returns a \ref MulMap class
alpar@1041
   437
alpar@1041
   438
  ///This function just returns a \ref MulMap class.
alpar@1041
   439
  ///\relates MulMap
alpar@1041
   440
  template<class M1,class M2> 
alpar@1041
   441
  inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2) 
alpar@1041
   442
  {
alpar@1041
   443
    return MulMap<M1,M2>(m1,m2);
alpar@1041
   444
  }
alpar@1041
   445
 
alpar@1070
   446
  ///Scale a maps with a constant.
alpar@1070
   447
alpar@1070
   448
  ///This \ref concept::ReadMap "read only map" returns the value of the
alpar@1070
   449
  ///given map multipied with a constant value.
alpar@1070
   450
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   451
  ///
alpar@1070
   452
  ///Actually,
alpar@1070
   453
  ///\code
alpar@1070
   454
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   455
  ///\endcode
alpar@1070
   456
  ///it is equivalent with
alpar@1070
   457
  ///\code
alpar@1070
   458
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   459
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   460
  ///\endcode
alpar@1070
   461
  template<class M> 
alpar@1070
   462
  class ScaleMap
alpar@1070
   463
  {
deba@1420
   464
    typename SmartConstReference<M>::Type m;
alpar@1070
   465
    typename M::Value v;
alpar@1070
   466
  public:
deba@1420
   467
deba@1420
   468
    typedef True NeedCopy;
alpar@1456
   469
    ///\e
alpar@1070
   470
    typedef typename M::Key Key;
alpar@1456
   471
    ///\e
alpar@1070
   472
    typedef typename M::Value Value;
alpar@1070
   473
alpar@1070
   474
    ///Constructor
alpar@1070
   475
alpar@1070
   476
    ///Constructor
alpar@1070
   477
    ///\param _m is the undelying map
alpar@1070
   478
    ///\param _v is the scaling value
alpar@1070
   479
    ScaleMap(const M &_m,const Value &_v ) : m(_m), v(_v) {};
alpar@1070
   480
    Value operator[](Key k) const {return m[k]*v;}
alpar@1070
   481
  };
alpar@1070
   482
  
alpar@1070
   483
  ///Returns an \ref ScaleMap class
alpar@1070
   484
alpar@1070
   485
  ///This function just returns an \ref ScaleMap class.
alpar@1070
   486
  ///\relates ScaleMap
alpar@1070
   487
  ///\todo A better name is required.
alpar@1070
   488
  template<class M> 
alpar@1070
   489
  inline ScaleMap<M> scaleMap(const M &m,const typename M::Value &v) 
alpar@1070
   490
  {
alpar@1070
   491
    return ScaleMap<M>(m,v);
alpar@1070
   492
  }
alpar@1070
   493
alpar@1041
   494
  ///Quotient of two maps
alpar@1041
   495
alpar@1041
   496
  ///This \ref concept::ReadMap "read only map" returns the quotient of the
alpar@1041
   497
  ///values returned by the two
alpar@1041
   498
  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
alpar@1041
   499
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   500
alpar@1041
   501
  template<class M1,class M2> 
alpar@1041
   502
  class DivMap
alpar@1041
   503
  {
deba@1420
   504
    typename SmartConstReference<M1>::Type m1;
deba@1420
   505
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   506
  public:
deba@1420
   507
deba@1420
   508
    typedef True NeedCopy;
alpar@1456
   509
    ///\e
alpar@1041
   510
    typedef typename M1::Key Key;
alpar@1456
   511
    ///\e
alpar@1041
   512
    typedef typename M1::Value Value;
alpar@1041
   513
alpar@1041
   514
    ///Constructor
alpar@1041
   515
alpar@1041
   516
    ///\e
alpar@1041
   517
    ///
alpar@1041
   518
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   519
    Value operator[](Key k) const {return m1[k]/m2[k];}
alpar@1041
   520
  };
alpar@1041
   521
  
alpar@1041
   522
  ///Returns a \ref DivMap class
alpar@1041
   523
alpar@1041
   524
  ///This function just returns a \ref DivMap class.
alpar@1041
   525
  ///\relates DivMap
alpar@1041
   526
  template<class M1,class M2> 
alpar@1041
   527
  inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2) 
alpar@1041
   528
  {
alpar@1041
   529
    return DivMap<M1,M2>(m1,m2);
alpar@1041
   530
  }
alpar@1041
   531
  
alpar@1041
   532
  ///Composition of two maps
alpar@1041
   533
alpar@1041
   534
  ///This \ref concept::ReadMap "read only map" returns the composition of
alpar@1041
   535
  ///two
alpar@1041
   536
  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
alpar@1041
   537
  ///of \c M2,
alpar@1041
   538
  ///then for
alpar@1041
   539
  ///\code
alpar@1041
   540
  ///  ComposeMap<M1,M2> cm(m1,m2);
alpar@1041
   541
  ///\endcode
alpar@1044
   542
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
alpar@1041
   543
  ///
alpar@1041
   544
  ///Its \c Key is inherited from \c M2 and its \c Value is from
alpar@1041
   545
  ///\c M1.
alpar@1041
   546
  ///The \c M2::Value must be convertible to \c M1::Key.
alpar@1041
   547
  ///\todo Check the requirements.
alpar@1041
   548
alpar@1041
   549
  template<class M1,class M2> 
alpar@1041
   550
  class ComposeMap
alpar@1041
   551
  {
deba@1420
   552
    typename SmartConstReference<M1>::Type m1;
deba@1420
   553
    typename SmartConstReference<M2>::Type m2;
alpar@1041
   554
  public:
deba@1420
   555
deba@1420
   556
    typedef True NeedCopy;
alpar@1456
   557
    ///\e
alpar@1041
   558
    typedef typename M2::Key Key;
alpar@1456
   559
    ///\e
alpar@1041
   560
    typedef typename M1::Value Value;
alpar@1041
   561
alpar@1041
   562
    ///Constructor
alpar@1041
   563
alpar@1041
   564
    ///\e
alpar@1041
   565
    ///
alpar@1041
   566
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1044
   567
    Value operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   568
  };
alpar@1041
   569
  ///Returns a \ref ComposeMap class
alpar@1041
   570
alpar@1041
   571
  ///This function just returns a \ref ComposeMap class.
alpar@1219
   572
  ///
alpar@1041
   573
  ///\relates ComposeMap
alpar@1041
   574
  template<class M1,class M2> 
alpar@1041
   575
  inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2) 
alpar@1041
   576
  {
alpar@1041
   577
    return ComposeMap<M1,M2>(m1,m2);
alpar@1041
   578
  }
alpar@1219
   579
  
alpar@1219
   580
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   581
alpar@1219
   582
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   583
  ///
alpar@1219
   584
  ///
alpar@1219
   585
  ///This \ref concept::ReadMap "read only map" takes to maps and a
alpar@1219
   586
  ///binary functor and returns the composition of
alpar@1219
   587
  ///two
alpar@1219
   588
  ///given maps unsing the functor. 
alpar@1219
   589
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
alpar@1219
   590
  ///and \c f is of \c F,
alpar@1219
   591
  ///then for
alpar@1219
   592
  ///\code
alpar@1219
   593
  ///  CombineMap<M1,M2,F,V> cm(m1,m2,f);
alpar@1219
   594
  ///\endcode
alpar@1219
   595
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   596
  ///
alpar@1219
   597
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
alpar@1219
   598
  ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   599
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   600
  ///to \c V.
alpar@1219
   601
  ///\todo Check the requirements.
alpar@1219
   602
deba@1420
   603
  template<class M1,class M2,class F,class V = typename F::result_type> 
alpar@1219
   604
  class CombineMap
alpar@1219
   605
  {
deba@1420
   606
    typename SmartConstReference<M1>::Type m1;
deba@1420
   607
    typename SmartConstReference<M2>::Type m2;
deba@1420
   608
    F f;
alpar@1219
   609
  public:
deba@1420
   610
deba@1420
   611
    typedef True NeedCopy;
alpar@1456
   612
    ///\e
alpar@1219
   613
    typedef typename M1::Key Key;
alpar@1456
   614
    ///\e
alpar@1219
   615
    typedef V Value;
alpar@1219
   616
alpar@1219
   617
    ///Constructor
alpar@1219
   618
alpar@1219
   619
    ///\e
alpar@1219
   620
    ///
alpar@1219
   621
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f)
alpar@1219
   622
      : m1(_m1), m2(_m2), f(_f) {};
alpar@1219
   623
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   624
  };
alpar@1219
   625
  
alpar@1219
   626
  ///Returns a \ref CombineMap class
alpar@1219
   627
alpar@1219
   628
  ///This function just returns a \ref CombineMap class.
alpar@1219
   629
  ///
alpar@1219
   630
  ///Only the first template parameter (the value type) must be given.
alpar@1219
   631
  ///
alpar@1219
   632
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   633
  ///\code
alpar@1219
   634
  ///combineMap<double>(m1,m2,std::plus<double>)
alpar@1219
   635
  ///\endcode
alpar@1219
   636
  ///is equivalent with
alpar@1219
   637
  ///\code
alpar@1219
   638
  ///addMap(m1,m2)
alpar@1219
   639
  ///\endcode
alpar@1219
   640
  ///
alpar@1219
   641
  ///\relates CombineMap
deba@1420
   642
  template<class M1,class M2,class F> 
deba@1420
   643
  inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) 
alpar@1219
   644
  {
deba@1420
   645
    return CombineMap<M1,M2,F>(m1,m2,f);
alpar@1219
   646
  }
alpar@1041
   647
alpar@1041
   648
  ///Negative value of a map
alpar@1041
   649
alpar@1041
   650
  ///This \ref concept::ReadMap "read only map" returns the negative
alpar@1041
   651
  ///value of the
alpar@1041
   652
  ///value returned by the
alpar@1041
   653
  ///given map. Its \c Key and \c Value will be inherited from \c M.
alpar@1041
   654
  ///The unary \c - operator must be defined for \c Value, of course.
alpar@1041
   655
alpar@1041
   656
  template<class M> 
alpar@1041
   657
  class NegMap
alpar@1041
   658
  {
deba@1420
   659
    typename SmartConstReference<M>::Type m;
alpar@1041
   660
  public:
deba@1420
   661
deba@1420
   662
    typedef True NeedCopy;
alpar@1456
   663
    ///\e
alpar@1041
   664
    typedef typename M::Key Key;
alpar@1456
   665
    ///\e
alpar@1041
   666
    typedef typename M::Value Value;
alpar@1041
   667
alpar@1041
   668
    ///Constructor
alpar@1041
   669
alpar@1041
   670
    ///\e
alpar@1041
   671
    ///
alpar@1041
   672
    NegMap(const M &_m) : m(_m) {};
alpar@1044
   673
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   674
  };
alpar@1041
   675
  
alpar@1041
   676
  ///Returns a \ref NegMap class
alpar@1041
   677
alpar@1041
   678
  ///This function just returns a \ref NegMap class.
alpar@1041
   679
  ///\relates NegMap
alpar@1041
   680
  template<class M> 
alpar@1041
   681
  inline NegMap<M> negMap(const M &m) 
alpar@1041
   682
  {
alpar@1041
   683
    return NegMap<M>(m);
alpar@1041
   684
  }
alpar@1041
   685
alpar@1041
   686
alpar@1041
   687
  ///Absolute value of a map
alpar@1041
   688
alpar@1041
   689
  ///This \ref concept::ReadMap "read only map" returns the absolute value
alpar@1041
   690
  ///of the
alpar@1041
   691
  ///value returned by the
alpar@1044
   692
  ///given map. Its \c Key and \c Value will be inherited
alpar@1044
   693
  ///from <tt>M</tt>. <tt>Value</tt>
alpar@1044
   694
  ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
alpar@1044
   695
  ///operator must be defined for it, of course.
alpar@1044
   696
  ///
alpar@1044
   697
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   698
  ///\code
alpar@1044
   699
  ///  struct _UnConvertible {};
alpar@1044
   700
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   701
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   702
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   703
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   704
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   705
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   706
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   707
  ///\endcode
alpar@1044
   708
  
alpar@1041
   709
alpar@1041
   710
  template<class M> 
alpar@1041
   711
  class AbsMap
alpar@1041
   712
  {
deba@1420
   713
    typename SmartConstReference<M>::Type m;
alpar@1041
   714
  public:
deba@1420
   715
deba@1420
   716
    typedef True NeedCopy;
alpar@1456
   717
    ///\e
alpar@1041
   718
    typedef typename M::Key Key;
alpar@1456
   719
    ///\e
alpar@1041
   720
    typedef typename M::Value Value;
alpar@1041
   721
alpar@1041
   722
    ///Constructor
alpar@1041
   723
alpar@1041
   724
    ///\e
alpar@1041
   725
    ///
alpar@1041
   726
    AbsMap(const M &_m) : m(_m) {};
alpar@1044
   727
    Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
alpar@1041
   728
  };
alpar@1041
   729
  
alpar@1041
   730
  ///Returns a \ref AbsMap class
alpar@1041
   731
alpar@1041
   732
  ///This function just returns a \ref AbsMap class.
alpar@1041
   733
  ///\relates AbsMap
alpar@1041
   734
  template<class M> 
alpar@1041
   735
  inline AbsMap<M> absMap(const M &m) 
alpar@1041
   736
  {
alpar@1041
   737
    return AbsMap<M>(m);
alpar@1041
   738
  }
alpar@1041
   739
alpar@1402
   740
  ///Converts an STL style functor to a map
alpar@1076
   741
alpar@1076
   742
  ///This \ref concept::ReadMap "read only map" returns the value
alpar@1076
   743
  ///of a
alpar@1076
   744
  ///given map.
alpar@1076
   745
  ///
alpar@1076
   746
  ///Template parameters \c K and \c V will become its
alpar@1076
   747
  ///\c Key and \c Value. They must be given explicitely
alpar@1076
   748
  ///because a functor does not provide such typedefs.
alpar@1076
   749
  ///
alpar@1076
   750
  ///Parameter \c F is the type of the used functor.
alpar@1076
   751
  
alpar@1076
   752
alpar@1076
   753
  template<class K,class V,class F> 
alpar@1076
   754
  class FunctorMap
alpar@1076
   755
  {
alpar@1076
   756
    const F &f;
alpar@1076
   757
  public:
deba@1420
   758
deba@1420
   759
    typedef True NeedCopy;
alpar@1456
   760
    ///\e
alpar@1076
   761
    typedef K Key;
alpar@1456
   762
    ///\e
alpar@1076
   763
    typedef V Value;
alpar@1076
   764
alpar@1076
   765
    ///Constructor
alpar@1076
   766
alpar@1076
   767
    ///\e
alpar@1076
   768
    ///
alpar@1076
   769
    FunctorMap(const F &_f) : f(_f) {};
alpar@1076
   770
    Value operator[](Key k) const {return f(k);}
alpar@1076
   771
  };
alpar@1076
   772
  
alpar@1076
   773
  ///Returns a \ref FunctorMap class
alpar@1076
   774
alpar@1076
   775
  ///This function just returns a \ref FunctorMap class.
alpar@1076
   776
  ///
alpar@1076
   777
  ///The third template parameter isn't necessary to be given.
alpar@1076
   778
  ///\relates FunctorMap
alpar@1076
   779
  template<class K,class V, class F>
alpar@1076
   780
  inline FunctorMap<K,V,F> functorMap(const F &f) 
alpar@1076
   781
  {
alpar@1076
   782
    return FunctorMap<K,V,F>(f);
alpar@1076
   783
  }
alpar@1076
   784
alpar@1219
   785
  ///Converts a map to an STL style (unary) functor
alpar@1076
   786
alpar@1219
   787
  ///This class Converts a map to an STL style (unary) functor.
alpar@1076
   788
  ///that is it provides an <tt>operator()</tt> to read its values.
alpar@1076
   789
  ///
alpar@1223
   790
  ///For the sake of convenience it also works as
alpar@1223
   791
  ///a ususal \ref concept::ReadMap "readable map", i.e
marci@1172
   792
  ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
alpar@1076
   793
alpar@1076
   794
  template<class M> 
alpar@1076
   795
  class MapFunctor
alpar@1076
   796
  {
deba@1420
   797
    typename SmartConstReference<M>::Type m;
alpar@1076
   798
  public:
deba@1420
   799
deba@1420
   800
    typedef True NeedCopy;
alpar@1456
   801
    ///\e
alpar@1223
   802
    typedef typename M::Key argument_type;
alpar@1456
   803
    ///\e
alpar@1223
   804
    typedef typename M::Value result_type;
alpar@1456
   805
    ///\e
alpar@1076
   806
    typedef typename M::Key Key;
alpar@1456
   807
    ///\e
alpar@1076
   808
    typedef typename M::Value Value;
alpar@1076
   809
alpar@1076
   810
    ///Constructor
alpar@1076
   811
alpar@1076
   812
    ///\e
alpar@1076
   813
    ///
alpar@1076
   814
    MapFunctor(const M &_m) : m(_m) {};
alpar@1076
   815
    ///Returns a value of the map
alpar@1076
   816
    
alpar@1076
   817
    ///\e
alpar@1076
   818
    ///
alpar@1076
   819
    Value operator()(Key k) const {return m[k];}
alpar@1076
   820
    ///\e
alpar@1076
   821
    ///
alpar@1076
   822
    Value operator[](Key k) const {return m[k];}
alpar@1076
   823
  };
alpar@1076
   824
  
alpar@1076
   825
  ///Returns a \ref MapFunctor class
alpar@1076
   826
alpar@1076
   827
  ///This function just returns a \ref MapFunctor class.
alpar@1076
   828
  ///\relates MapFunctor
alpar@1076
   829
  template<class M> 
alpar@1076
   830
  inline MapFunctor<M> mapFunctor(const M &m) 
alpar@1076
   831
  {
alpar@1076
   832
    return MapFunctor<M>(m);
alpar@1076
   833
  }
alpar@1076
   834
alpar@1076
   835
alpar@1219
   836
  ///Apply all map setting operations to two maps
alpar@1219
   837
alpar@1219
   838
  ///This map has two \ref concept::WriteMap "writable map"
alpar@1219
   839
  ///parameters and each write request will be passed to both of them.
alpar@1219
   840
  ///If \c M1 is also \ref concept::ReadMap "readable",
alpar@1219
   841
  ///then the read operations will return the
alpar@1317
   842
  ///corresponding values of \c M1.
alpar@1219
   843
  ///
alpar@1219
   844
  ///The \c Key and \c Value will be inherited from \c M1.
alpar@1219
   845
  ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
alpar@1219
   846
alpar@1219
   847
  template<class M1,class M2> 
alpar@1219
   848
  class ForkMap
alpar@1219
   849
  {
deba@1420
   850
    typename SmartConstReference<M1>::Type m1;
deba@1420
   851
    typename SmartConstReference<M2>::Type m2;
alpar@1219
   852
  public:
deba@1420
   853
deba@1420
   854
    typedef True NeedCopy;
alpar@1456
   855
    ///\e
alpar@1219
   856
    typedef typename M1::Key Key;
alpar@1456
   857
    ///\e
alpar@1219
   858
    typedef typename M1::Value Value;
alpar@1219
   859
alpar@1219
   860
    ///Constructor
alpar@1219
   861
alpar@1219
   862
    ///\e
alpar@1219
   863
    ///
alpar@1219
   864
    ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
alpar@1219
   865
    Value operator[](Key k) const {return m1[k];}
alpar@1219
   866
    void set(Key k,const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
   867
  };
alpar@1219
   868
  
alpar@1219
   869
  ///Returns an \ref ForkMap class
alpar@1219
   870
alpar@1219
   871
  ///This function just returns an \ref ForkMap class.
alpar@1219
   872
  ///\todo How to call these type of functions?
alpar@1219
   873
  ///
alpar@1219
   874
  ///\relates ForkMap
alpar@1219
   875
  ///\todo Wrong scope in Doxygen when \c \\relates is used
alpar@1219
   876
  template<class M1,class M2> 
alpar@1219
   877
  inline ForkMap<M1,M2> forkMap(const M1 &m1,const M2 &m2) 
alpar@1219
   878
  {
alpar@1219
   879
    return ForkMap<M1,M2>(m1,m2);
alpar@1219
   880
  }
alpar@1219
   881
alpar@1456
   882
alpar@1456
   883
  
alpar@1456
   884
  /* ************* BOOL MAPS ******************* */
alpar@1456
   885
  
alpar@1456
   886
  ///Logical 'not' of a map
alpar@1456
   887
  
alpar@1456
   888
  ///This bool \ref concept::ReadMap "read only map" returns the 
alpar@1456
   889
  ///logical negation of
alpar@1456
   890
  ///value returned by the
alpar@1456
   891
  ///given map. Its \c Key and will be inherited from \c M,
alpar@1456
   892
  ///its Value is <tt>bool</tt>.
alpar@1456
   893
alpar@1456
   894
  template<class M> 
alpar@1456
   895
  class NotMap
alpar@1456
   896
  {
alpar@1456
   897
    typename SmartConstReference<M>::Type m;
alpar@1456
   898
  public:
alpar@1456
   899
alpar@1456
   900
    typedef True NeedCopy;
alpar@1456
   901
    ///\e
alpar@1456
   902
    typedef typename M::Key Key;
alpar@1456
   903
    ///\e
alpar@1456
   904
    typedef bool Value;
alpar@1456
   905
alpar@1456
   906
    ///Constructor
alpar@1456
   907
alpar@1456
   908
    ///\e
alpar@1456
   909
    ///
alpar@1456
   910
    NotMap(const M &_m) : m(_m) {};
alpar@1456
   911
    Value operator[](Key k) const {return !m[k];}
alpar@1456
   912
  };
alpar@1456
   913
  
alpar@1456
   914
  ///Returns a \ref NotMap class
alpar@1456
   915
  
alpar@1456
   916
  ///This function just returns a \ref NotMap class.
alpar@1456
   917
  ///\relates NotMap
alpar@1456
   918
  template<class M> 
alpar@1456
   919
  inline NotMap<M> notMap(const M &m) 
alpar@1456
   920
  {
alpar@1456
   921
    return NotMap<M>(m);
alpar@1456
   922
  }
alpar@1456
   923
alpar@1456
   924
alpar@1456
   925
alpar@1456
   926
alpar@1456
   927
alpar@1456
   928
alpar@1456
   929
alpar@1456
   930
alpar@1456
   931
alpar@1456
   932
alpar@1456
   933
alpar@1041
   934
  /// @}
klao@286
   935
}
alpar@1041
   936
alpar@921
   937
#endif // LEMON_MAPS_H