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