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