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