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