lemon/maps.h
author kpeter
Mon, 18 Feb 2008 03:32:06 +0000
changeset 2575 e866e288cba6
parent 2553 bfced05fa852
permissions -rw-r--r--
Major improvements in NetworkSimplex.

Main changes:
- Use -potenital[] instead of potential[] to conform to the usual
terminology.
- Use function parameter instead of #define commands to select pivot rule.
- Use much faster implementation for the candidate list pivot rule.
It is about 5-20 times faster now.
- Add a new pivot rule called "Limited Search" that is a modified
version of "Block Search". It is about 25 percent faster on rather
sparse graphs.
- By default "Limited Search" is used for sparse graphs and
"Block Search" is used otherwise. This combined method is the most
efficient on every input class.
- Change the name of private members to start with "_".
- Change the name of function parameters not to start with "_".
- Remove unnecessary documentation for private members.
- Many doc improvements.
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@921
    19
#ifndef LEMON_MAPS_H
alpar@921
    20
#define LEMON_MAPS_H
klao@286
    21
deba@1778
    22
#include <iterator>
deba@2091
    23
#include <functional>
deba@2511
    24
#include <vector>
deba@1778
    25
deba@1993
    26
#include <lemon/bits/utility.h>
deba@1993
    27
#include <lemon/bits/traits.h>
alpar@1041
    28
klao@286
    29
///\file
alpar@1041
    30
///\ingroup maps
klao@286
    31
///\brief Miscellaneous property maps
klao@286
    32
///
klao@286
    33
#include <map>
klao@286
    34
alpar@921
    35
namespace lemon {
klao@286
    36
alpar@1041
    37
  /// \addtogroup maps
alpar@1041
    38
  /// @{
alpar@1041
    39
alpar@720
    40
  /// Base class of maps.
alpar@720
    41
alpar@805
    42
  /// Base class of maps.
alpar@805
    43
  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
deba@1705
    44
  template<typename K, typename T>
deba@1675
    45
  class MapBase {
alpar@720
    46
  public:
kpeter@2564
    47
    /// The key type of the map.
alpar@987
    48
    typedef K Key;
kpeter@2564
    49
    /// The value type of the map. (The type of objects associated with the keys).
alpar@987
    50
    typedef T Value;
alpar@720
    51
  };
alpar@720
    52
alpar@805
    53
  /// Null map. (a.k.a. DoNothingMap)
klao@286
    54
kpeter@2564
    55
  /// This map can be used if you have to provide a map only for
kpeter@2564
    56
  /// its type definitions, or if you have to provide a writable map, 
kpeter@2564
    57
  /// but data written to it is not required (i.e. it will be sent to 
kpeter@2564
    58
  /// <tt>/dev/null</tt>).
deba@1705
    59
  template<typename K, typename T>
deba@1705
    60
  class NullMap : public MapBase<K, T> {
klao@286
    61
  public:
deba@1705
    62
    typedef MapBase<K, T> Parent;
deba@1675
    63
    typedef typename Parent::Key Key;
deba@1675
    64
    typedef typename Parent::Value Value;
deba@1420
    65
    
alpar@805
    66
    /// Gives back a default constructed element.
klao@286
    67
    T operator[](const K&) const { return T(); }
alpar@805
    68
    /// Absorbs the value.
klao@286
    69
    void set(const K&, const T&) {}
klao@286
    70
  };
klao@286
    71
kpeter@2564
    72
  ///Returns a \c NullMap class
kpeter@2564
    73
kpeter@2564
    74
  ///This function just returns a \c NullMap class.
kpeter@2564
    75
  ///\relates NullMap
deba@1420
    76
  template <typename K, typename V> 
deba@1705
    77
  NullMap<K, V> nullMap() {
deba@1705
    78
    return NullMap<K, V>();
deba@1420
    79
  }
deba@1420
    80
klao@286
    81
klao@286
    82
  /// Constant map.
klao@286
    83
kpeter@2564
    84
  /// This is a \ref concepts::ReadMap "readable" map which assigns a 
kpeter@2564
    85
  /// specified value to each key.
kpeter@2564
    86
  /// In other aspects it is equivalent to \c NullMap.
deba@1705
    87
  template<typename K, typename T>
deba@1705
    88
  class ConstMap : public MapBase<K, T> {
deba@1675
    89
  private:
klao@286
    90
    T v;
klao@286
    91
  public:
klao@286
    92
deba@1705
    93
    typedef MapBase<K, T> Parent;
deba@1675
    94
    typedef typename Parent::Key Key;
deba@1675
    95
    typedef typename Parent::Value Value;
deba@1420
    96
alpar@805
    97
    /// Default constructor
alpar@805
    98
kpeter@2564
    99
    /// Default constructor.
alpar@805
   100
    /// The value of the map will be uninitialized. 
alpar@805
   101
    /// (More exactly it will be default constructed.)
klao@286
   102
    ConstMap() {}
kpeter@2564
   103
    
kpeter@2564
   104
    /// Constructor with specified initial value
alpar@805
   105
kpeter@2564
   106
    /// Constructor with specified initial value.
kpeter@2564
   107
    /// \param _v is the initial value of the map.
klao@286
   108
    ConstMap(const T &_v) : v(_v) {}
deba@2489
   109
    
deba@2489
   110
    ///\e
deba@2489
   111
    T operator[](const K&) const { return v; }
klao@286
   112
deba@2489
   113
    ///\e
deba@2489
   114
    void setAll(const T &t) {
deba@2489
   115
      v = t;
deba@2489
   116
    }    
klao@286
   117
klao@286
   118
    template<typename T1>
klao@286
   119
    struct rebind {
deba@1675
   120
      typedef ConstMap<K, T1> other;
klao@286
   121
    };
klao@286
   122
klao@286
   123
    template<typename T1>
deba@1675
   124
    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
klao@286
   125
  };
klao@286
   126
deba@2489
   127
  ///Returns a \c ConstMap class
alpar@1076
   128
deba@2489
   129
  ///This function just returns a \c ConstMap class.
alpar@1076
   130
  ///\relates ConstMap
deba@1675
   131
  template<typename K, typename V> 
deba@1705
   132
  inline ConstMap<K, V> constMap(const V &v) {
deba@1705
   133
    return ConstMap<K, V>(v);
alpar@1076
   134
  }
alpar@1076
   135
alpar@1076
   136
marci@890
   137
  template<typename T, T v>
marci@890
   138
  struct Const { };
deba@1675
   139
deba@2489
   140
  /// Constant map with inlined constant value.
deba@2489
   141
kpeter@2564
   142
  /// This is a \ref concepts::ReadMap "readable" map which assigns a 
kpeter@2564
   143
  /// specified value to each key.
kpeter@2564
   144
  /// In other aspects it is equivalent to \c NullMap.
deba@1705
   145
  template<typename K, typename V, V v>
deba@1705
   146
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
marci@890
   147
  public:
deba@1705
   148
    typedef MapBase<K, V> Parent;
deba@1675
   149
    typedef typename Parent::Key Key;
deba@1675
   150
    typedef typename Parent::Value Value;
deba@1675
   151
marci@890
   152
    ConstMap() { }
deba@2489
   153
    ///\e
marci@890
   154
    V operator[](const K&) const { return v; }
deba@2489
   155
    ///\e
marci@890
   156
    void set(const K&, const V&) { }
marci@890
   157
  };
klao@286
   158
kpeter@2564
   159
  ///Returns a \c ConstMap class with inlined value
deba@1675
   160
deba@2489
   161
  ///This function just returns a \c ConstMap class with inlined value.
deba@1675
   162
  ///\relates ConstMap
deba@1675
   163
  template<typename K, typename V, V v> 
deba@1705
   164
  inline ConstMap<K, Const<V, v> > constMap() {
deba@1705
   165
    return ConstMap<K, Const<V, v> >();
deba@1675
   166
  }
deba@1675
   167
kpeter@2564
   168
  ///Map based on \c std::map
klao@286
   169
kpeter@2564
   170
  ///This is essentially a wrapper for \c std::map with addition that
deba@2489
   171
  ///you can specify a default value different from \c Value() .
kpeter@2564
   172
  ///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
alpar@987
   173
  template <typename K, typename T, typename Compare = std::less<K> >
kpeter@2564
   174
  class StdMap : public MapBase<K, T> {
deba@2489
   175
    template <typename K1, typename T1, typename C1>
deba@2489
   176
    friend class StdMap;
deba@2489
   177
  public:
klao@286
   178
kpeter@2564
   179
    typedef MapBase<K, T> Parent;
kpeter@2564
   180
    ///Key type
kpeter@2564
   181
    typedef typename Parent::Key Key;
kpeter@2564
   182
    ///Value type
kpeter@2564
   183
    typedef typename Parent::Value Value;
kpeter@2564
   184
    ///Reference Type
kpeter@2564
   185
    typedef T& Reference;
kpeter@2564
   186
    ///Const reference type
kpeter@2564
   187
    typedef const T& ConstReference;
kpeter@2564
   188
deba@2489
   189
    typedef True ReferenceMapTag;
klao@286
   190
deba@2489
   191
  private:
deba@2489
   192
    
deba@2489
   193
    typedef std::map<K, T, Compare> Map;
deba@2489
   194
    Value _value;
deba@2489
   195
    Map _map;
klao@286
   196
deba@2489
   197
  public:
deba@2489
   198
klao@286
   199
    /// Constructor with specified default value
deba@2489
   200
    StdMap(const T& value = T()) : _value(value) {}
kpeter@2564
   201
    /// \brief Constructs the map from an appropriate \c std::map, and 
kpeter@2564
   202
    /// explicitly specifies a default value.
deba@2489
   203
    template <typename T1, typename Comp1>
deba@2489
   204
    StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) 
deba@2489
   205
      : _map(map.begin(), map.end()), _value(value) {}
klao@286
   206
    
kpeter@2564
   207
    /// \brief Constructs a map from an other \ref StdMap.
klao@286
   208
    template<typename T1, typename Comp1>
deba@2489
   209
    StdMap(const StdMap<Key, T1, Comp1> &c) 
deba@2489
   210
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
deba@2489
   211
deba@2489
   212
  private:
deba@2489
   213
deba@2489
   214
    StdMap& operator=(const StdMap&);
deba@2489
   215
deba@2489
   216
  public:
deba@2489
   217
deba@2489
   218
    ///\e
deba@2489
   219
    Reference operator[](const Key &k) {
deba@2489
   220
      typename Map::iterator it = _map.lower_bound(k);
deba@2489
   221
      if (it != _map.end() && !_map.key_comp()(k, it->first))
deba@2489
   222
	return it->second;
deba@2489
   223
      else
deba@2489
   224
	return _map.insert(it, std::make_pair(k, _value))->second;
marci@389
   225
    }
klao@286
   226
deba@2489
   227
    /// \e 
deba@2489
   228
    ConstReference operator[](const Key &k) const {
deba@2489
   229
      typename Map::const_iterator it = _map.find(k);
deba@2489
   230
      if (it != _map.end())
deba@2489
   231
	return it->second;
deba@2489
   232
      else
deba@2489
   233
	return _value;
klao@286
   234
    }
deba@1675
   235
deba@2489
   236
    /// \e 
klao@345
   237
    void set(const Key &k, const T &t) {
deba@2489
   238
      typename Map::iterator it = _map.lower_bound(k);
deba@2489
   239
      if (it != _map.end() && !_map.key_comp()(k, it->first))
deba@2489
   240
	it->second = t;
deba@2489
   241
      else
deba@2489
   242
	_map.insert(it, std::make_pair(k, t));
klao@345
   243
    }
klao@286
   244
deba@2489
   245
    /// \e
deba@2489
   246
    void setAll(const T &t) {
deba@2489
   247
      _value = t;
deba@2489
   248
      _map.clear();
deba@2489
   249
    }    
klao@286
   250
deba@2489
   251
    template <typename T1, typename C1 = std::less<T1> >
klao@286
   252
    struct rebind {
deba@2489
   253
      typedef StdMap<Key, T1, C1> other;
klao@286
   254
    };
klao@286
   255
  };
alpar@1041
   256
kpeter@2564
   257
  ///Returns a \c StdMap class
kpeter@2564
   258
kpeter@2564
   259
  ///This function just returns a \c StdMap class with specified 
kpeter@2564
   260
  ///default value.
kpeter@2564
   261
  ///\relates StdMap
kpeter@2564
   262
  template<typename K, typename V, typename Compare> 
kpeter@2564
   263
  inline StdMap<K, V, Compare> stdMap(const V& value = V()) {
kpeter@2564
   264
    return StdMap<K, V, Compare>(value);
kpeter@2564
   265
  }
kpeter@2564
   266
  
kpeter@2564
   267
  template<typename K, typename V> 
kpeter@2564
   268
  inline StdMap<K, V, std::less<K> > stdMap(const V& value = V()) {
kpeter@2564
   269
    return StdMap<K, V, std::less<K> >(value);
kpeter@2564
   270
  }
kpeter@2564
   271
  
kpeter@2564
   272
  ///Returns a \c StdMap class created from an appropriate \c std::map
kpeter@2564
   273
kpeter@2564
   274
  ///This function just returns a \c StdMap class created from an 
kpeter@2564
   275
  ///appropriate \c std::map.
kpeter@2564
   276
  ///\relates StdMap
kpeter@2564
   277
  template<typename K, typename V, typename Compare> 
kpeter@2564
   278
  inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map, 
kpeter@2564
   279
                                       const V& value = V() ) {
kpeter@2564
   280
    return StdMap<K, V, Compare>(map, value);
kpeter@2564
   281
  }
kpeter@2564
   282
kpeter@2564
   283
  /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
deba@2511
   284
  ///
kpeter@2564
   285
  /// This map has the <tt>[0..size-1]</tt> keyset and the values
deba@2511
   286
  /// are stored in a \c std::vector<T>  container. It can be used with
deba@2511
   287
  /// some data structures, for example \c UnionFind, \c BinHeap, when 
deba@2511
   288
  /// the used items are small integer numbers.
deba@2511
   289
  template <typename T>
kpeter@2564
   290
  class IntegerMap : public MapBase<int, T> {
deba@2511
   291
deba@2511
   292
    template <typename T1>
deba@2511
   293
    friend class IntegerMap;
deba@2511
   294
deba@2511
   295
  public:
deba@2511
   296
kpeter@2564
   297
    typedef MapBase<int, T> Parent;
deba@2511
   298
    ///\e
kpeter@2564
   299
    typedef typename Parent::Key Key;
deba@2511
   300
    ///\e
kpeter@2564
   301
    typedef typename Parent::Value Value;
deba@2511
   302
    ///\e
deba@2511
   303
    typedef T& Reference;
deba@2511
   304
    ///\e
deba@2511
   305
    typedef const T& ConstReference;
deba@2511
   306
kpeter@2564
   307
    typedef True ReferenceMapTag;
kpeter@2564
   308
deba@2511
   309
  private:
deba@2511
   310
    
deba@2511
   311
    typedef std::vector<T> Vector;
deba@2511
   312
    Vector _vector;
deba@2511
   313
deba@2511
   314
  public:
deba@2511
   315
deba@2511
   316
    /// Constructor with specified default value
deba@2511
   317
    IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
deba@2511
   318
kpeter@2564
   319
    /// \brief Constructs the map from an appropriate \c std::vector.
deba@2511
   320
    template <typename T1>
deba@2511
   321
    IntegerMap(const std::vector<T1>& vector) 
deba@2511
   322
      : _vector(vector.begin(), vector.end()) {}
deba@2511
   323
    
kpeter@2564
   324
    /// \brief Constructs a map from an other \ref IntegerMap.
deba@2511
   325
    template <typename T1>
deba@2511
   326
    IntegerMap(const IntegerMap<T1> &c) 
deba@2511
   327
      : _vector(c._vector.begin(), c._vector.end()) {}
deba@2511
   328
deba@2511
   329
    /// \brief Resize the container
deba@2511
   330
    void resize(int size, const T& value = T()) {
deba@2511
   331
      _vector.resize(size, value);
deba@2511
   332
    }
deba@2511
   333
deba@2511
   334
  private:
deba@2511
   335
deba@2511
   336
    IntegerMap& operator=(const IntegerMap&);
deba@2511
   337
deba@2511
   338
  public:
deba@2511
   339
deba@2511
   340
    ///\e
deba@2511
   341
    Reference operator[](Key k) {
deba@2511
   342
      return _vector[k];
deba@2511
   343
    }
deba@2511
   344
deba@2511
   345
    /// \e 
deba@2511
   346
    ConstReference operator[](Key k) const {
deba@2511
   347
      return _vector[k];
deba@2511
   348
    }
deba@2511
   349
deba@2511
   350
    /// \e 
deba@2511
   351
    void set(const Key &k, const T& t) {
deba@2511
   352
      _vector[k] = t;
deba@2511
   353
    }
deba@2511
   354
deba@2511
   355
  };
deba@2511
   356
kpeter@2564
   357
  ///Returns an \c IntegerMap class
kpeter@2564
   358
kpeter@2564
   359
  ///This function just returns an \c IntegerMap class.
kpeter@2564
   360
  ///\relates IntegerMap
kpeter@2564
   361
  template<typename T>
kpeter@2564
   362
  inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) {
kpeter@2564
   363
    return IntegerMap<T>(size, value);
kpeter@2564
   364
  }
kpeter@2564
   365
alpar@1402
   366
  /// @}
alpar@1402
   367
alpar@1402
   368
  /// \addtogroup map_adaptors
alpar@1402
   369
  /// @{
alpar@1402
   370
kpeter@2564
   371
  /// \brief Identity map.
deba@1531
   372
  ///
kpeter@2564
   373
  /// This map gives back the given key as value without any
deba@1531
   374
  /// modification. 
deba@1705
   375
  template <typename T>
deba@1705
   376
  class IdentityMap : public MapBase<T, T> {
deba@1531
   377
  public:
deba@1705
   378
    typedef MapBase<T, T> Parent;
deba@1675
   379
    typedef typename Parent::Key Key;
deba@1675
   380
    typedef typename Parent::Value Value;
deba@1531
   381
deba@2489
   382
    /// \e
deba@1675
   383
    const T& operator[](const T& t) const {
deba@1531
   384
      return t;
deba@1531
   385
    }
deba@1531
   386
  };
alpar@1402
   387
deba@2489
   388
  ///Returns an \c IdentityMap class
deba@1675
   389
deba@2489
   390
  ///This function just returns an \c IdentityMap class.
deba@1675
   391
  ///\relates IdentityMap
deba@1675
   392
  template<typename T>
deba@1705
   393
  inline IdentityMap<T> identityMap() {
deba@1705
   394
    return IdentityMap<T>();
deba@1675
   395
  }
deba@1675
   396
  
deba@1675
   397
kpeter@2564
   398
  ///\brief Convert the \c Value of a map to another type using
kpeter@2564
   399
  ///the default conversion.
kpeter@2564
   400
  ///
kpeter@2564
   401
  ///This \ref concepts::ReadMap "read only map"
kpeter@2564
   402
  ///converts the \c Value of a map to type \c T.
alpar@1547
   403
  ///Its \c Key is inherited from \c M.
deba@1705
   404
  template <typename M, typename T> 
deba@1705
   405
  class ConvertMap : public MapBase<typename M::Key, T> {
deba@1705
   406
    const M& m;
alpar@1178
   407
  public:
deba@1705
   408
    typedef MapBase<typename M::Key, T> Parent;
deba@1675
   409
    typedef typename Parent::Key Key;
deba@1675
   410
    typedef typename Parent::Value Value;
alpar@1178
   411
alpar@1178
   412
    ///Constructor
alpar@1178
   413
alpar@1178
   414
    ///Constructor
alpar@1536
   415
    ///\param _m is the underlying map
alpar@1178
   416
    ConvertMap(const M &_m) : m(_m) {};
deba@1346
   417
kpeter@2564
   418
    ///\e
deba@1675
   419
    Value operator[](const Key& k) const {return m[k];}
alpar@1178
   420
  };
alpar@1178
   421
  
kpeter@2564
   422
  ///Returns a \c ConvertMap class
alpar@1178
   423
kpeter@2564
   424
  ///This function just returns a \c ConvertMap class.
alpar@1178
   425
  ///\relates ConvertMap
deba@1675
   426
  template<typename T, typename M>
deba@1705
   427
  inline ConvertMap<M, T> convertMap(const M &m) {
deba@1705
   428
    return ConvertMap<M, T>(m);
alpar@1178
   429
  }
alpar@1041
   430
kpeter@2564
   431
  ///Simple wrapping of a map
deba@2248
   432
kpeter@2564
   433
  ///This \ref concepts::ReadMap "read only map" returns the simple
deba@2248
   434
  ///wrapping of the given map. Sometimes the reference maps cannot be
deba@2248
   435
  ///combined with simple read maps. This map adaptor wraps the given
deba@2248
   436
  ///map to simple read map.
kpeter@2564
   437
  ///
kpeter@2564
   438
  ///\sa SimpleWriteMap
kpeter@2564
   439
  ///
kpeter@2564
   440
  /// \todo Revise the misleading name 
deba@2248
   441
  template<typename M> 
deba@2248
   442
  class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
deba@2248
   443
    const M& m;
deba@2248
   444
deba@2248
   445
  public:
deba@2248
   446
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2248
   447
    typedef typename Parent::Key Key;
deba@2248
   448
    typedef typename Parent::Value Value;
deba@2248
   449
deba@2248
   450
    ///Constructor
deba@2248
   451
    SimpleMap(const M &_m) : m(_m) {};
deba@2489
   452
    ///\e
deba@2248
   453
    Value operator[](Key k) const {return m[k];}
deba@2248
   454
  };
deba@2248
   455
kpeter@2564
   456
  ///Returns a \c SimpleMap class
deba@2248
   457
kpeter@2564
   458
  ///This function just returns a \c SimpleMap class.
kpeter@2564
   459
  ///\relates SimpleMap
kpeter@2564
   460
  template<typename M>
kpeter@2564
   461
  inline SimpleMap<M> simpleMap(const M &m) {
kpeter@2564
   462
    return SimpleMap<M>(m);
kpeter@2564
   463
  }
kpeter@2564
   464
kpeter@2564
   465
  ///Simple writable wrapping of a map
kpeter@2564
   466
kpeter@2564
   467
  ///This \ref concepts::ReadWriteMap "read-write map" returns the simple
deba@2248
   468
  ///wrapping of the given map. Sometimes the reference maps cannot be
deba@2248
   469
  ///combined with simple read-write maps. This map adaptor wraps the
deba@2248
   470
  ///given map to simple read-write map.
kpeter@2564
   471
  ///
kpeter@2564
   472
  ///\sa SimpleMap
kpeter@2564
   473
  ///
kpeter@2564
   474
  /// \todo Revise the misleading name
deba@2248
   475
  template<typename M> 
deba@2248
   476
  class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2248
   477
    M& m;
deba@2248
   478
deba@2248
   479
  public:
deba@2248
   480
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2248
   481
    typedef typename Parent::Key Key;
deba@2248
   482
    typedef typename Parent::Value Value;
deba@2248
   483
deba@2248
   484
    ///Constructor
deba@2248
   485
    SimpleWriteMap(M &_m) : m(_m) {};
deba@2489
   486
    ///\e
deba@2248
   487
    Value operator[](Key k) const {return m[k];}
deba@2489
   488
    ///\e
deba@2248
   489
    void set(Key k, const Value& c) { m.set(k, c); }
deba@2248
   490
  };
deba@2248
   491
kpeter@2564
   492
  ///Returns a \c SimpleWriteMap class
kpeter@2564
   493
kpeter@2564
   494
  ///This function just returns a \c SimpleWriteMap class.
kpeter@2564
   495
  ///\relates SimpleWriteMap
kpeter@2564
   496
  template<typename M>
kpeter@2564
   497
  inline SimpleWriteMap<M> simpleWriteMap(M &m) {
kpeter@2564
   498
    return SimpleWriteMap<M>(m);
kpeter@2564
   499
  }
kpeter@2564
   500
alpar@1041
   501
  ///Sum of two maps
alpar@1041
   502
kpeter@2564
   503
  ///This \ref concepts::ReadMap "read only map" returns the sum of the two
kpeter@2564
   504
  ///given maps.
kpeter@2564
   505
  ///Its \c Key and \c Value are inherited from \c M1.
kpeter@2564
   506
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
deba@1705
   507
  template<typename M1, typename M2> 
deba@1705
   508
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   509
    const M1& m1;
deba@1705
   510
    const M2& m2;
deba@1420
   511
alpar@1041
   512
  public:
deba@1705
   513
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   514
    typedef typename Parent::Key Key;
deba@1675
   515
    typedef typename Parent::Value Value;
alpar@1041
   516
alpar@1041
   517
    ///Constructor
alpar@1041
   518
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   519
    ///\e
alpar@1044
   520
    Value operator[](Key k) const {return m1[k]+m2[k];}
alpar@1041
   521
  };
alpar@1041
   522
  
deba@2489
   523
  ///Returns an \c AddMap class
alpar@1041
   524
deba@2489
   525
  ///This function just returns an \c AddMap class.
alpar@1041
   526
  ///\todo How to call these type of functions?
alpar@1041
   527
  ///
alpar@1041
   528
  ///\relates AddMap
deba@1675
   529
  template<typename M1, typename M2> 
deba@1705
   530
  inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
deba@1705
   531
    return AddMap<M1, M2>(m1,m2);
alpar@1041
   532
  }
alpar@1041
   533
alpar@1547
   534
  ///Shift a map with a constant.
alpar@1070
   535
kpeter@2564
   536
  ///This \ref concepts::ReadMap "read only map" returns the sum of the
alpar@1070
   537
  ///given map and a constant value.
alpar@1070
   538
  ///Its \c Key and \c Value is inherited from \c M.
alpar@1070
   539
  ///
alpar@1070
   540
  ///Actually,
alpar@1070
   541
  ///\code
alpar@1070
   542
  ///  ShiftMap<X> sh(x,v);
alpar@1070
   543
  ///\endcode
kpeter@2564
   544
  ///is equivalent to
alpar@1070
   545
  ///\code
alpar@1070
   546
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   547
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
alpar@1070
   548
  ///\endcode
kpeter@2564
   549
  ///
kpeter@2564
   550
  ///\sa ShiftWriteMap
deba@1705
   551
  template<typename M, typename C = typename M::Value> 
deba@1705
   552
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   553
    const M& m;
deba@1691
   554
    C v;
alpar@1070
   555
  public:
deba@1705
   556
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   557
    typedef typename Parent::Key Key;
deba@1675
   558
    typedef typename Parent::Value Value;
alpar@1070
   559
alpar@1070
   560
    ///Constructor
alpar@1070
   561
alpar@1070
   562
    ///Constructor
alpar@1070
   563
    ///\param _m is the undelying map
alpar@1070
   564
    ///\param _v is the shift value
deba@1691
   565
    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   566
    ///\e
deba@1691
   567
    Value operator[](Key k) const {return m[k] + v;}
alpar@1070
   568
  };
deba@2032
   569
kpeter@2564
   570
  ///Shift a map with a constant (ReadWrite version).
deba@2032
   571
kpeter@2564
   572
  ///This \ref concepts::ReadWriteMap "read-write map" returns the sum of the
deba@2032
   573
  ///given map and a constant value. It makes also possible to write the map.
kpeter@2564
   574
  ///Its \c Key and \c Value are inherited from \c M.
deba@2032
   575
  ///
kpeter@2564
   576
  ///\sa ShiftMap
deba@2032
   577
  template<typename M, typename C = typename M::Value> 
deba@2032
   578
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   579
    M& m;
deba@2032
   580
    C v;
deba@2032
   581
  public:
deba@2032
   582
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   583
    typedef typename Parent::Key Key;
deba@2032
   584
    typedef typename Parent::Value Value;
deba@2032
   585
deba@2032
   586
    ///Constructor
deba@2032
   587
deba@2032
   588
    ///Constructor
deba@2032
   589
    ///\param _m is the undelying map
deba@2032
   590
    ///\param _v is the shift value
deba@2080
   591
    ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   592
    /// \e
deba@2032
   593
    Value operator[](Key k) const {return m[k] + v;}
deba@2489
   594
    /// \e
deba@2032
   595
    void set(Key k, const Value& c) { m.set(k, c - v); }
deba@2032
   596
  };
alpar@1070
   597
  
kpeter@2564
   598
  ///Returns a \c ShiftMap class
alpar@1070
   599
deba@2489
   600
  ///This function just returns an \c ShiftMap class.
alpar@1070
   601
  ///\relates ShiftMap
deba@1691
   602
  template<typename M, typename C> 
deba@1705
   603
  inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
deba@1705
   604
    return ShiftMap<M, C>(m,v);
alpar@1070
   605
  }
alpar@1070
   606
kpeter@2564
   607
  ///Returns a \c ShiftWriteMap class
kpeter@2564
   608
kpeter@2564
   609
  ///This function just returns a \c ShiftWriteMap class.
kpeter@2564
   610
  ///\relates ShiftWriteMap
deba@2032
   611
  template<typename M, typename C> 
deba@2032
   612
  inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
deba@2032
   613
    return ShiftWriteMap<M, C>(m,v);
deba@2032
   614
  }
deba@2032
   615
alpar@1041
   616
  ///Difference of two maps
alpar@1041
   617
kpeter@2564
   618
  ///This \ref concepts::ReadMap "read only map" returns the difference
kpeter@2564
   619
  ///of the values of the two given maps.
kpeter@2564
   620
  ///Its \c Key and \c Value are inherited from \c M1.
alpar@1041
   621
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
alpar@1041
   622
deba@1705
   623
  template<typename M1, typename M2> 
deba@1705
   624
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   625
    const M1& m1;
deba@1705
   626
    const M2& m2;
alpar@1041
   627
  public:
deba@1705
   628
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   629
    typedef typename Parent::Key Key;
deba@1675
   630
    typedef typename Parent::Value Value;
alpar@1041
   631
alpar@1041
   632
    ///Constructor
alpar@1041
   633
    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   634
    /// \e
alpar@1044
   635
    Value operator[](Key k) const {return m1[k]-m2[k];}
alpar@1041
   636
  };
alpar@1041
   637
  
deba@2489
   638
  ///Returns a \c SubMap class
alpar@1041
   639
deba@2489
   640
  ///This function just returns a \c SubMap class.
alpar@1041
   641
  ///
alpar@1041
   642
  ///\relates SubMap
deba@1675
   643
  template<typename M1, typename M2> 
deba@1705
   644
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
deba@1705
   645
    return SubMap<M1, M2>(m1, m2);
alpar@1041
   646
  }
alpar@1041
   647
alpar@1041
   648
  ///Product of two maps
alpar@1041
   649
kpeter@2564
   650
  ///This \ref concepts::ReadMap "read only map" returns the product of the
kpeter@2564
   651
  ///values of the two given maps.
kpeter@2564
   652
  ///Its \c Key and \c Value are inherited from \c M1.
alpar@1041
   653
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
deba@1705
   654
  template<typename M1, typename M2> 
deba@1705
   655
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   656
    const M1& m1;
deba@1705
   657
    const M2& m2;
alpar@1041
   658
  public:
deba@1705
   659
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   660
    typedef typename Parent::Key Key;
deba@1675
   661
    typedef typename Parent::Value Value;
alpar@1041
   662
alpar@1041
   663
    ///Constructor
alpar@1041
   664
    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   665
    /// \e
alpar@1044
   666
    Value operator[](Key k) const {return m1[k]*m2[k];}
alpar@1041
   667
  };
alpar@1041
   668
  
deba@2489
   669
  ///Returns a \c MulMap class
alpar@1041
   670
deba@2489
   671
  ///This function just returns a \c MulMap class.
alpar@1041
   672
  ///\relates MulMap
deba@1675
   673
  template<typename M1, typename M2> 
deba@1705
   674
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
deba@1705
   675
    return MulMap<M1, M2>(m1,m2);
alpar@1041
   676
  }
alpar@1041
   677
 
kpeter@2564
   678
  ///Scales a map with a constant.
alpar@1070
   679
kpeter@2564
   680
  ///This \ref concepts::ReadMap "read only map" returns the value of the
deba@1691
   681
  ///given map multiplied from the left side with a constant value.
kpeter@2564
   682
  ///Its \c Key and \c Value are inherited from \c M.
alpar@1070
   683
  ///
alpar@1070
   684
  ///Actually,
alpar@1070
   685
  ///\code
alpar@1070
   686
  ///  ScaleMap<X> sc(x,v);
alpar@1070
   687
  ///\endcode
kpeter@2564
   688
  ///is equivalent to
alpar@1070
   689
  ///\code
alpar@1070
   690
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
alpar@1070
   691
  ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
alpar@1070
   692
  ///\endcode
kpeter@2564
   693
  ///
kpeter@2564
   694
  ///\sa ScaleWriteMap
deba@1705
   695
  template<typename M, typename C = typename M::Value> 
deba@1705
   696
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   697
    const M& m;
deba@1691
   698
    C v;
alpar@1070
   699
  public:
deba@1705
   700
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   701
    typedef typename Parent::Key Key;
deba@1675
   702
    typedef typename Parent::Value Value;
alpar@1070
   703
alpar@1070
   704
    ///Constructor
alpar@1070
   705
alpar@1070
   706
    ///Constructor
alpar@1070
   707
    ///\param _m is the undelying map
alpar@1070
   708
    ///\param _v is the scaling value
deba@1691
   709
    ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   710
    /// \e
deba@1691
   711
    Value operator[](Key k) const {return v * m[k];}
alpar@1070
   712
  };
deba@2032
   713
kpeter@2564
   714
  ///Scales a map with a constant (ReadWrite version).
deba@2032
   715
kpeter@2564
   716
  ///This \ref concepts::ReadWriteMap "read-write map" returns the value of the
deba@2032
   717
  ///given map multiplied from the left side with a constant value. It can
kpeter@2564
   718
  ///also be used as write map if the \c / operator is defined between
kpeter@2564
   719
  ///\c Value and \c C and the given multiplier is not zero.
kpeter@2564
   720
  ///Its \c Key and \c Value are inherited from \c M.
kpeter@2564
   721
  ///
kpeter@2564
   722
  ///\sa ScaleMap
deba@2032
   723
  template<typename M, typename C = typename M::Value> 
deba@2032
   724
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   725
    M& m;
deba@2032
   726
    C v;
deba@2032
   727
  public:
deba@2032
   728
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   729
    typedef typename Parent::Key Key;
deba@2032
   730
    typedef typename Parent::Value Value;
deba@2032
   731
deba@2032
   732
    ///Constructor
deba@2032
   733
deba@2032
   734
    ///Constructor
deba@2032
   735
    ///\param _m is the undelying map
deba@2032
   736
    ///\param _v is the scaling value
deba@2032
   737
    ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
deba@2489
   738
    /// \e
deba@2032
   739
    Value operator[](Key k) const {return v * m[k];}
deba@2489
   740
    /// \e
deba@2032
   741
    void set(Key k, const Value& c) { m.set(k, c / v);}
deba@2032
   742
  };
alpar@1070
   743
  
kpeter@2564
   744
  ///Returns a \c ScaleMap class
alpar@1070
   745
kpeter@2564
   746
  ///This function just returns a \c ScaleMap class.
alpar@1070
   747
  ///\relates ScaleMap
deba@1691
   748
  template<typename M, typename C> 
deba@1705
   749
  inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
deba@1705
   750
    return ScaleMap<M, C>(m,v);
alpar@1070
   751
  }
alpar@1070
   752
kpeter@2564
   753
  ///Returns a \c ScaleWriteMap class
kpeter@2564
   754
kpeter@2564
   755
  ///This function just returns a \c ScaleWriteMap class.
kpeter@2564
   756
  ///\relates ScaleWriteMap
deba@2032
   757
  template<typename M, typename C> 
deba@2032
   758
  inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
deba@2032
   759
    return ScaleWriteMap<M, C>(m,v);
deba@2032
   760
  }
deba@2032
   761
alpar@1041
   762
  ///Quotient of two maps
alpar@1041
   763
kpeter@2564
   764
  ///This \ref concepts::ReadMap "read only map" returns the quotient of the
kpeter@2564
   765
  ///values of the two given maps.
kpeter@2564
   766
  ///Its \c Key and \c Value are inherited from \c M1.
alpar@1041
   767
  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
deba@1705
   768
  template<typename M1, typename M2> 
deba@1705
   769
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
   770
    const M1& m1;
deba@1705
   771
    const M2& m2;
alpar@1041
   772
  public:
deba@1705
   773
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
   774
    typedef typename Parent::Key Key;
deba@1675
   775
    typedef typename Parent::Value Value;
alpar@1041
   776
alpar@1041
   777
    ///Constructor
alpar@1041
   778
    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
   779
    /// \e
alpar@1044
   780
    Value operator[](Key k) const {return m1[k]/m2[k];}
alpar@1041
   781
  };
alpar@1041
   782
  
deba@2489
   783
  ///Returns a \c DivMap class
alpar@1041
   784
deba@2489
   785
  ///This function just returns a \c DivMap class.
alpar@1041
   786
  ///\relates DivMap
deba@1675
   787
  template<typename M1, typename M2> 
deba@1705
   788
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
deba@1705
   789
    return DivMap<M1, M2>(m1,m2);
alpar@1041
   790
  }
alpar@1041
   791
  
alpar@1041
   792
  ///Composition of two maps
alpar@1041
   793
kpeter@2564
   794
  ///This \ref concepts::ReadMap "read only map" returns the composition of
kpeter@2564
   795
  ///two given maps.
kpeter@2564
   796
  ///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2,
alpar@1041
   797
  ///then for
alpar@1041
   798
  ///\code
deba@1675
   799
  ///  ComposeMap<M1, M2> cm(m1,m2);
alpar@1041
   800
  ///\endcode
kpeter@2564
   801
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
alpar@1041
   802
  ///
kpeter@2564
   803
  ///Its \c Key is inherited from \c M2 and its \c Value is from \c M1.
kpeter@2564
   804
  ///\c M2::Value must be convertible to \c M1::Key.
kpeter@2564
   805
  ///
kpeter@2564
   806
  ///\sa CombineMap
kpeter@2564
   807
  ///
alpar@1041
   808
  ///\todo Check the requirements.
deba@1705
   809
  template <typename M1, typename M2> 
deba@1705
   810
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
deba@1705
   811
    const M1& m1;
deba@1705
   812
    const M2& m2;
alpar@1041
   813
  public:
deba@1705
   814
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
deba@1675
   815
    typedef typename Parent::Key Key;
deba@1675
   816
    typedef typename Parent::Value Value;
alpar@1041
   817
alpar@1041
   818
    ///Constructor
alpar@1041
   819
    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@1725
   820
    
deba@1725
   821
    typename MapTraits<M1>::ConstReturnValue
deba@2489
   822
    /// \e
deba@1725
   823
    operator[](Key k) const {return m1[m2[k]];}
alpar@1041
   824
  };
deba@2489
   825
  ///Returns a \c ComposeMap class
alpar@1041
   826
deba@2489
   827
  ///This function just returns a \c ComposeMap class.
alpar@1219
   828
  ///
alpar@1041
   829
  ///\relates ComposeMap
deba@1675
   830
  template <typename M1, typename M2> 
deba@1705
   831
  inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
deba@1705
   832
    return ComposeMap<M1, M2>(m1,m2);
alpar@1041
   833
  }
alpar@1219
   834
  
kpeter@2564
   835
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   836
kpeter@2564
   837
  ///Combine of two maps using an STL (binary) functor.
alpar@1219
   838
  ///
kpeter@2564
   839
  ///This \ref concepts::ReadMap "read only map" takes two maps and a
kpeter@2564
   840
  ///binary functor and returns the composition of the two
alpar@1219
   841
  ///given maps unsing the functor. 
alpar@1219
   842
  ///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
kpeter@2564
   843
  ///and \c f is of \c F, then for
alpar@1219
   844
  ///\code
deba@1675
   845
  ///  CombineMap<M1, M2,F,V> cm(m1,m2,f);
alpar@1219
   846
  ///\endcode
alpar@1219
   847
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
alpar@1219
   848
  ///
alpar@1219
   849
  ///Its \c Key is inherited from \c M1 and its \c Value is \c V.
kpeter@2564
   850
  ///\c M2::Value and \c M1::Value must be convertible to the corresponding
alpar@1219
   851
  ///input parameter of \c F and the return type of \c F must be convertible
alpar@1219
   852
  ///to \c V.
kpeter@2564
   853
  ///
kpeter@2564
   854
  ///\sa ComposeMap
kpeter@2564
   855
  ///
alpar@1219
   856
  ///\todo Check the requirements.
deba@1675
   857
  template<typename M1, typename M2, typename F,
deba@2489
   858
	   typename V = typename F::result_type> 
deba@1705
   859
  class CombineMap : public MapBase<typename M1::Key, V> {
deba@1705
   860
    const M1& m1;
deba@1705
   861
    const M2& m2;
deba@1420
   862
    F f;
alpar@1219
   863
  public:
deba@1705
   864
    typedef MapBase<typename M1::Key, V> Parent;
deba@1675
   865
    typedef typename Parent::Key Key;
deba@1675
   866
    typedef typename Parent::Value Value;
alpar@1219
   867
alpar@1219
   868
    ///Constructor
deba@2489
   869
    CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
alpar@1219
   870
      : m1(_m1), m2(_m2), f(_f) {};
deba@2489
   871
    /// \e
alpar@1219
   872
    Value operator[](Key k) const {return f(m1[k],m2[k]);}
alpar@1219
   873
  };
alpar@1219
   874
  
deba@2489
   875
  ///Returns a \c CombineMap class
alpar@1219
   876
deba@2489
   877
  ///This function just returns a \c CombineMap class.
alpar@1219
   878
  ///
alpar@1219
   879
  ///For example if \c m1 and \c m2 are both \c double valued maps, then 
alpar@1219
   880
  ///\code
kpeter@2564
   881
  ///combineMap(m1,m2,std::plus<double>())
alpar@1219
   882
  ///\endcode
kpeter@2564
   883
  ///is equivalent to
alpar@1219
   884
  ///\code
alpar@1219
   885
  ///addMap(m1,m2)
alpar@1219
   886
  ///\endcode
alpar@1219
   887
  ///
deba@2489
   888
  ///This function is specialized for adaptable binary function
kpeter@2564
   889
  ///classes and C++ functions.
deba@2489
   890
  ///
alpar@1219
   891
  ///\relates CombineMap
deba@1675
   892
  template<typename M1, typename M2, typename F, typename V> 
deba@1705
   893
  inline CombineMap<M1, M2, F, V> 
deba@1675
   894
  combineMap(const M1& m1,const M2& m2, const F& f) {
deba@1705
   895
    return CombineMap<M1, M2, F, V>(m1,m2,f);
deba@1675
   896
  }
deba@1675
   897
deba@1675
   898
  template<typename M1, typename M2, typename F> 
deba@1705
   899
  inline CombineMap<M1, M2, F, typename F::result_type> 
deba@1675
   900
  combineMap(const M1& m1, const M2& m2, const F& f) {
deba@1675
   901
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
deba@1675
   902
  }
deba@1675
   903
deba@1675
   904
  template<typename M1, typename M2, typename K1, typename K2, typename V> 
deba@1705
   905
  inline CombineMap<M1, M2, V (*)(K1, K2), V> 
deba@1675
   906
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
deba@1675
   907
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
alpar@1219
   908
  }
alpar@1041
   909
alpar@1041
   910
  ///Negative value of a map
alpar@1041
   911
kpeter@2564
   912
  ///This \ref concepts::ReadMap "read only map" returns the negative
kpeter@2564
   913
  ///value of the value returned by the given map.
kpeter@2564
   914
  ///Its \c Key and \c Value are inherited from \c M.
alpar@1041
   915
  ///The unary \c - operator must be defined for \c Value, of course.
kpeter@2564
   916
  ///
kpeter@2564
   917
  ///\sa NegWriteMap
deba@1705
   918
  template<typename M> 
deba@1705
   919
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   920
    const M& m;
alpar@1041
   921
  public:
deba@1705
   922
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
   923
    typedef typename Parent::Key Key;
deba@1675
   924
    typedef typename Parent::Value Value;
alpar@1041
   925
alpar@1041
   926
    ///Constructor
alpar@1041
   927
    NegMap(const M &_m) : m(_m) {};
deba@2489
   928
    /// \e
alpar@1044
   929
    Value operator[](Key k) const {return -m[k];}
alpar@1041
   930
  };
alpar@1041
   931
  
kpeter@2564
   932
  ///Negative value of a map (ReadWrite version)
deba@2032
   933
kpeter@2564
   934
  ///This \ref concepts::ReadWriteMap "read-write map" returns the negative
kpeter@2564
   935
  ///value of the value returned by the given map.
kpeter@2564
   936
  ///Its \c Key and \c Value are inherited from \c M.
deba@2032
   937
  ///The unary \c - operator must be defined for \c Value, of course.
kpeter@2564
   938
  ///
kpeter@2564
   939
  /// \sa NegMap
deba@2032
   940
  template<typename M> 
deba@2032
   941
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
deba@2032
   942
    M& m;
deba@2032
   943
  public:
deba@2032
   944
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@2032
   945
    typedef typename Parent::Key Key;
deba@2032
   946
    typedef typename Parent::Value Value;
deba@2032
   947
deba@2032
   948
    ///Constructor
deba@2032
   949
    NegWriteMap(M &_m) : m(_m) {};
deba@2489
   950
    /// \e
deba@2032
   951
    Value operator[](Key k) const {return -m[k];}
deba@2489
   952
    /// \e
deba@2032
   953
    void set(Key k, const Value& v) { m.set(k, -v); }
deba@2032
   954
  };
deba@2032
   955
deba@2489
   956
  ///Returns a \c NegMap class
alpar@1041
   957
deba@2489
   958
  ///This function just returns a \c NegMap class.
alpar@1041
   959
  ///\relates NegMap
deba@1675
   960
  template <typename M> 
deba@1705
   961
  inline NegMap<M> negMap(const M &m) {
deba@1705
   962
    return NegMap<M>(m);
alpar@1041
   963
  }
alpar@1041
   964
kpeter@2564
   965
  ///Returns a \c NegWriteMap class
kpeter@2564
   966
kpeter@2564
   967
  ///This function just returns a \c NegWriteMap class.
kpeter@2564
   968
  ///\relates NegWriteMap
deba@2032
   969
  template <typename M> 
deba@2032
   970
  inline NegWriteMap<M> negMap(M &m) {
deba@2032
   971
    return NegWriteMap<M>(m);
deba@2032
   972
  }
alpar@1041
   973
alpar@1041
   974
  ///Absolute value of a map
alpar@1041
   975
kpeter@2564
   976
  ///This \ref concepts::ReadMap "read only map" returns the absolute value
kpeter@2564
   977
  ///of the value returned by the given map.
kpeter@2564
   978
  ///Its \c Key and \c Value are inherited from \c M. 
kpeter@2564
   979
  ///\c Value must be comparable to \c 0 and the unary \c -
alpar@1044
   980
  ///operator must be defined for it, of course.
alpar@1044
   981
  ///
alpar@1044
   982
  ///\bug We need a unified way to handle the situation below:
alpar@1044
   983
  ///\code
alpar@1044
   984
  ///  struct _UnConvertible {};
alpar@1044
   985
  ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
alpar@1044
   986
  ///  template<> inline int t_abs<>(int n) {return abs(n);}
alpar@1044
   987
  ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
alpar@1044
   988
  ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
alpar@1044
   989
  ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
alpar@1044
   990
  ///  template<> inline double t_abs<>(double n) {return fabs(n);}
alpar@1044
   991
  ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
alpar@1044
   992
  ///\endcode
alpar@1044
   993
  
alpar@1041
   994
deba@1705
   995
  template<typename M> 
deba@1705
   996
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
deba@1705
   997
    const M& m;
alpar@1041
   998
  public:
deba@1705
   999
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
  1000
    typedef typename Parent::Key Key;
deba@1675
  1001
    typedef typename Parent::Value Value;
alpar@1041
  1002
alpar@1041
  1003
    ///Constructor
alpar@1041
  1004
    AbsMap(const M &_m) : m(_m) {};
deba@2489
  1005
    /// \e
deba@1675
  1006
    Value operator[](Key k) const {
deba@1675
  1007
      Value tmp = m[k]; 
deba@1675
  1008
      return tmp >= 0 ? tmp : -tmp;
deba@1675
  1009
    }
deba@1675
  1010
alpar@1041
  1011
  };
alpar@1041
  1012
  
kpeter@2564
  1013
  ///Returns an \c AbsMap class
alpar@1041
  1014
kpeter@2564
  1015
  ///This function just returns an \c AbsMap class.
alpar@1041
  1016
  ///\relates AbsMap
deba@1675
  1017
  template<typename M> 
deba@1705
  1018
  inline AbsMap<M> absMap(const M &m) {
deba@1705
  1019
    return AbsMap<M>(m);
alpar@1041
  1020
  }
alpar@1041
  1021
alpar@1402
  1022
  ///Converts an STL style functor to a map
alpar@1076
  1023
kpeter@2564
  1024
  ///This \ref concepts::ReadMap "read only map" returns the value
kpeter@2564
  1025
  ///of a given functor.
alpar@1076
  1026
  ///
alpar@1076
  1027
  ///Template parameters \c K and \c V will become its
kpeter@2564
  1028
  ///\c Key and \c Value. 
kpeter@2564
  1029
  ///In most cases they have to be given explicitly because a 
kpeter@2564
  1030
  ///functor typically does not provide \c argument_type and 
kpeter@2564
  1031
  ///\c result_type typedefs.
alpar@1076
  1032
  ///
alpar@1076
  1033
  ///Parameter \c F is the type of the used functor.
kpeter@2564
  1034
  ///
kpeter@2564
  1035
  ///\sa MapFunctor
deba@1675
  1036
  template<typename F, 
deba@1675
  1037
	   typename K = typename F::argument_type, 
deba@2489
  1038
	   typename V = typename F::result_type> 
deba@1705
  1039
  class FunctorMap : public MapBase<K, V> {
deba@1679
  1040
    F f;
alpar@1076
  1041
  public:
deba@1705
  1042
    typedef MapBase<K, V> Parent;
deba@1675
  1043
    typedef typename Parent::Key Key;
deba@1675
  1044
    typedef typename Parent::Value Value;
alpar@1076
  1045
alpar@1076
  1046
    ///Constructor
deba@2489
  1047
    FunctorMap(const F &_f = F()) : f(_f) {}
deba@2489
  1048
    /// \e
deba@1679
  1049
    Value operator[](Key k) const { return f(k);}
alpar@1076
  1050
  };
alpar@1076
  1051
  
deba@2489
  1052
  ///Returns a \c FunctorMap class
alpar@1076
  1053
deba@2489
  1054
  ///This function just returns a \c FunctorMap class.
alpar@1076
  1055
  ///
kpeter@2564
  1056
  ///This function is specialized for adaptable binary function
kpeter@2564
  1057
  ///classes and C++ functions.
kpeter@2564
  1058
  ///
alpar@1076
  1059
  ///\relates FunctorMap
deba@1675
  1060
  template<typename K, typename V, typename F> inline 
deba@1705
  1061
  FunctorMap<F, K, V> functorMap(const F &f) {
deba@1705
  1062
    return FunctorMap<F, K, V>(f);
alpar@1076
  1063
  }
alpar@1076
  1064
deba@1675
  1065
  template <typename F> inline 
deba@1705
  1066
  FunctorMap<F, typename F::argument_type, typename F::result_type> 
deba@1675
  1067
  functorMap(const F &f) {
deba@1679
  1068
    return FunctorMap<F, typename F::argument_type, 
deba@1705
  1069
      typename F::result_type>(f);
deba@1675
  1070
  }
deba@1675
  1071
deba@1675
  1072
  template <typename K, typename V> inline 
deba@1705
  1073
  FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
deba@1705
  1074
    return FunctorMap<V (*)(K), K, V>(f);
deba@1675
  1075
  }
deba@1675
  1076
deba@1675
  1077
alpar@1219
  1078
  ///Converts a map to an STL style (unary) functor
alpar@1076
  1079
alpar@1219
  1080
  ///This class Converts a map to an STL style (unary) functor.
kpeter@2564
  1081
  ///That is it provides an <tt>operator()</tt> to read its values.
alpar@1076
  1082
  ///
alpar@1223
  1083
  ///For the sake of convenience it also works as
kpeter@2564
  1084
  ///a ususal \ref concepts::ReadMap "readable map",
alpar@1537
  1085
  ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
kpeter@2564
  1086
  ///
kpeter@2564
  1087
  ///\sa FunctorMap
deba@1705
  1088
  template <typename M> 
deba@1705
  1089
  class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
deba@1705
  1090
    const M& m;
alpar@1076
  1091
  public:
deba@1705
  1092
    typedef MapBase<typename M::Key, typename M::Value> Parent;
deba@1675
  1093
    typedef typename Parent::Key Key;
deba@1675
  1094
    typedef typename Parent::Value Value;
deba@1420
  1095
alpar@1223
  1096
    typedef typename M::Key argument_type;
alpar@1223
  1097
    typedef typename M::Value result_type;
alpar@1076
  1098
alpar@1076
  1099
    ///Constructor
alpar@1076
  1100
    MapFunctor(const M &_m) : m(_m) {};
deba@2489
  1101
    ///\e
alpar@1076
  1102
    Value operator()(Key k) const {return m[k];}
alpar@1076
  1103
    ///\e
alpar@1076
  1104
    Value operator[](Key k) const {return m[k];}
alpar@1076
  1105
  };
alpar@1076
  1106
  
deba@2489
  1107
  ///Returns a \c MapFunctor class
alpar@1076
  1108
deba@2489
  1109
  ///This function just returns a \c MapFunctor class.
alpar@1076
  1110
  ///\relates MapFunctor
deba@1675
  1111
  template<typename M> 
deba@1705
  1112
  inline MapFunctor<M> mapFunctor(const M &m) {
deba@1705
  1113
    return MapFunctor<M>(m);
alpar@1076
  1114
  }
alpar@1076
  1115
kpeter@2564
  1116
  ///Just readable version of \ref ForkWriteMap
alpar@1219
  1117
kpeter@2564
  1118
  ///This map has two \ref concepts::ReadMap "readable map"
deba@2032
  1119
  ///parameters and each read request will be passed just to the
kpeter@2564
  1120
  ///first map. This class is the just readable map type of \c ForkWriteMap.
alpar@1219
  1121
  ///
kpeter@2564
  1122
  ///The \c Key and \c Value are inherited from \c M1.
kpeter@2564
  1123
  ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
kpeter@2564
  1124
  ///
kpeter@2564
  1125
  ///\sa ForkWriteMap
kpeter@2564
  1126
deba@1705
  1127
  template<typename  M1, typename M2> 
deba@1705
  1128
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@1705
  1129
    const M1& m1;
deba@1705
  1130
    const M2& m2;
alpar@1219
  1131
  public:
deba@1705
  1132
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@1675
  1133
    typedef typename Parent::Key Key;
deba@1675
  1134
    typedef typename Parent::Value Value;
alpar@1219
  1135
alpar@1219
  1136
    ///Constructor
deba@2032
  1137
    ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
  1138
    /// \e
alpar@1219
  1139
    Value operator[](Key k) const {return m1[k];}
deba@2032
  1140
  };
deba@2032
  1141
deba@2032
  1142
deba@2032
  1143
  ///Applies all map setting operations to two maps
deba@2032
  1144
kpeter@2564
  1145
  ///This map has two \ref concepts::WriteMap "writable map"
deba@2032
  1146
  ///parameters and each write request will be passed to both of them.
kpeter@2564
  1147
  ///If \c M1 is also \ref concepts::ReadMap "readable",
deba@2032
  1148
  ///then the read operations will return the
deba@2032
  1149
  ///corresponding values of \c M1.
deba@2032
  1150
  ///
kpeter@2564
  1151
  ///The \c Key and \c Value are inherited from \c M1.
kpeter@2564
  1152
  ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
kpeter@2564
  1153
  ///
kpeter@2564
  1154
  ///\sa ForkMap
deba@2032
  1155
  template<typename  M1, typename M2> 
deba@2032
  1156
  class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
deba@2032
  1157
    M1& m1;
deba@2032
  1158
    M2& m2;
deba@2032
  1159
  public:
deba@2032
  1160
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
deba@2032
  1161
    typedef typename Parent::Key Key;
deba@2032
  1162
    typedef typename Parent::Value Value;
deba@2032
  1163
deba@2032
  1164
    ///Constructor
deba@2032
  1165
    ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
deba@2489
  1166
    ///\e
deba@2032
  1167
    Value operator[](Key k) const {return m1[k];}
deba@2489
  1168
    ///\e
deba@2032
  1169
    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
alpar@1219
  1170
  };
alpar@1219
  1171
  
kpeter@2564
  1172
  ///Returns a \c ForkMap class
alpar@1219
  1173
kpeter@2564
  1174
  ///This function just returns a \c ForkMap class.
alpar@1219
  1175
  ///\relates ForkMap
deba@1675
  1176
  template <typename M1, typename M2> 
deba@2032
  1177
  inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
deba@1705
  1178
    return ForkMap<M1, M2>(m1,m2);
alpar@1219
  1179
  }
alpar@1219
  1180
kpeter@2564
  1181
  ///Returns a \c ForkWriteMap class
kpeter@2564
  1182
kpeter@2564
  1183
  ///This function just returns a \c ForkWriteMap class.
kpeter@2564
  1184
  ///\relates ForkWriteMap
deba@2032
  1185
  template <typename M1, typename M2> 
deba@2032
  1186
  inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
deba@2032
  1187
    return ForkWriteMap<M1, M2>(m1,m2);
deba@2032
  1188
  }
deba@2032
  1189
alpar@1456
  1190
alpar@1456
  1191
  
alpar@1456
  1192
  /* ************* BOOL MAPS ******************* */
alpar@1456
  1193
  
alpar@1456
  1194
  ///Logical 'not' of a map
alpar@1456
  1195
  
kpeter@2564
  1196
  ///This bool \ref concepts::ReadMap "read only map" returns the 
kpeter@2564
  1197
  ///logical negation of the value returned by the given map.
kpeter@2564
  1198
  ///Its \c Key is inherited from \c M, its \c Value is \c bool.
kpeter@2564
  1199
  ///
kpeter@2564
  1200
  ///\sa NotWriteMap
deba@1705
  1201
  template <typename M> 
deba@1705
  1202
  class NotMap : public MapBase<typename M::Key, bool> {
deba@1705
  1203
    const M& m;
alpar@1456
  1204
  public:
deba@1705
  1205
    typedef MapBase<typename M::Key, bool> Parent;
deba@1675
  1206
    typedef typename Parent::Key Key;
deba@1675
  1207
    typedef typename Parent::Value Value;
alpar@1456
  1208
deba@1778
  1209
    /// Constructor
alpar@1456
  1210
    NotMap(const M &_m) : m(_m) {};
deba@2489
  1211
    ///\e
alpar@1456
  1212
    Value operator[](Key k) const {return !m[k];}
alpar@1456
  1213
  };
deba@2032
  1214
kpeter@2564
  1215
  ///Logical 'not' of a map (ReadWrie version)
deba@2032
  1216
  
kpeter@2564
  1217
  ///This bool \ref concepts::ReadWriteMap "read-write map" returns the 
kpeter@2564
  1218
  ///logical negation of the value returned by the given map. When it is set,
alpar@2258
  1219
  ///the opposite value is set to the original map.
kpeter@2564
  1220
  ///Its \c Key is inherited from \c M, its \c Value is \c bool.
kpeter@2564
  1221
  ///
kpeter@2564
  1222
  ///\sa NotMap
deba@2032
  1223
  template <typename M> 
deba@2032
  1224
  class NotWriteMap : public MapBase<typename M::Key, bool> {
deba@2032
  1225
    M& m;
deba@2032
  1226
  public:
deba@2032
  1227
    typedef MapBase<typename M::Key, bool> Parent;
deba@2032
  1228
    typedef typename Parent::Key Key;
deba@2032
  1229
    typedef typename Parent::Value Value;
deba@2032
  1230
deba@2032
  1231
    /// Constructor
deba@2032
  1232
    NotWriteMap(M &_m) : m(_m) {};
deba@2489
  1233
    ///\e
deba@2032
  1234
    Value operator[](Key k) const {return !m[k];}
deba@2489
  1235
    ///\e
deba@2032
  1236
    void set(Key k, bool v) { m.set(k, !v); }
deba@2032
  1237
  };
alpar@1456
  1238
  
deba@2489
  1239
  ///Returns a \c NotMap class
alpar@1456
  1240
  
deba@2489
  1241
  ///This function just returns a \c NotMap class.
alpar@1456
  1242
  ///\relates NotMap
deba@1675
  1243
  template <typename M> 
deba@1705
  1244
  inline NotMap<M> notMap(const M &m) {
deba@1705
  1245
    return NotMap<M>(m);
alpar@1456
  1246
  }
deba@2423
  1247
  
kpeter@2564
  1248
  ///Returns a \c NotWriteMap class
kpeter@2564
  1249
  
kpeter@2564
  1250
  ///This function just returns a \c NotWriteMap class.
kpeter@2564
  1251
  ///\relates NotWriteMap
deba@2032
  1252
  template <typename M> 
deba@2032
  1253
  inline NotWriteMap<M> notMap(M &m) {
deba@2032
  1254
    return NotWriteMap<M>(m);
deba@2032
  1255
  }
deba@2032
  1256
deba@2091
  1257
  namespace _maps_bits {
deba@2423
  1258
deba@2091
  1259
    template <typename Value>
deba@2091
  1260
    struct Identity {
deba@2091
  1261
      typedef Value argument_type;
deba@2091
  1262
      typedef Value result_type;
deba@2423
  1263
      Value operator()(const Value& val) const {
deba@2091
  1264
	return val;
deba@2091
  1265
      }
deba@2091
  1266
    };
deba@2423
  1267
deba@2423
  1268
    template <typename _Iterator, typename Enable = void>
deba@2423
  1269
    struct IteratorTraits {
deba@2423
  1270
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
deba@2423
  1271
    };
deba@2423
  1272
deba@2423
  1273
    template <typename _Iterator>
deba@2423
  1274
    struct IteratorTraits<_Iterator,
deba@2423
  1275
      typename exists<typename _Iterator::container_type>::type> 
deba@2423
  1276
    {
deba@2423
  1277
      typedef typename _Iterator::container_type::value_type Value;
deba@2423
  1278
    };
deba@2423
  1279
deba@2091
  1280
  }
deba@2091
  1281
  
deba@2091
  1282
kpeter@2564
  1283
  /// \brief Writable bool map for logging each \c true assigned element
deba@1778
  1284
  ///
kpeter@2564
  1285
  /// A \ref concepts::ReadWriteMap "read-write" bool map for logging 
kpeter@2564
  1286
  /// each \c true assigned element, i.e it copies all the keys set 
kpeter@2564
  1287
  /// to \c true to the given iterator.
deba@1778
  1288
  ///
deba@2091
  1289
  /// \note The container of the iterator should contain space 
deba@2091
  1290
  /// for each element.
deba@2091
  1291
  ///
kpeter@2564
  1292
  /// The following example shows how you can write the edges found by 
kpeter@2564
  1293
  /// the \ref Prim algorithm directly to the standard output.
deba@2091
  1294
  ///\code
deba@2091
  1295
  /// typedef IdMap<UGraph, UEdge> UEdgeIdMap;
deba@2091
  1296
  /// UEdgeIdMap uedgeId(ugraph);
deba@2091
  1297
  ///
deba@2091
  1298
  /// typedef MapFunctor<UEdgeIdMap> UEdgeIdFunctor;
deba@2091
  1299
  /// UEdgeIdFunctor uedgeIdFunctor(uedgeId);
deba@2091
  1300
  ///
deba@2091
  1301
  /// StoreBoolMap<ostream_iterator<int>, UEdgeIdFunctor> 
deba@2091
  1302
  ///   writerMap(ostream_iterator<int>(cout, " "), uedgeIdFunctor);
deba@2091
  1303
  ///
deba@2091
  1304
  /// prim(ugraph, cost, writerMap);
deba@2091
  1305
  ///\endcode
kpeter@2564
  1306
  ///
kpeter@2564
  1307
  ///\sa BackInserterBoolMap 
kpeter@2564
  1308
  ///\sa FrontInserterBoolMap 
kpeter@2564
  1309
  ///\sa InserterBoolMap 
deba@2091
  1310
  template <typename _Iterator, 
deba@2423
  1311
            typename _Functor =
deba@2423
  1312
            _maps_bits::Identity<typename _maps_bits::
deba@2423
  1313
                                 IteratorTraits<_Iterator>::Value> >
deba@1778
  1314
  class StoreBoolMap {
deba@1778
  1315
  public:
deba@1778
  1316
    typedef _Iterator Iterator;
deba@1778
  1317
deba@2091
  1318
    typedef typename _Functor::argument_type Key;
deba@1778
  1319
    typedef bool Value;
deba@1778
  1320
deba@2091
  1321
    typedef _Functor Functor;
deba@2091
  1322
deba@1778
  1323
    /// Constructor
deba@2091
  1324
    StoreBoolMap(Iterator it, const Functor& functor = Functor()) 
deba@2091
  1325
      : _begin(it), _end(it), _functor(functor) {}
deba@1778
  1326
kpeter@2564
  1327
    /// Gives back the given iterator set for the first key
deba@1778
  1328
    Iterator begin() const {
deba@1778
  1329
      return _begin;
deba@1778
  1330
    }
deba@1778
  1331
 
kpeter@2564
  1332
    /// Gives back the the 'after the last' iterator
deba@1778
  1333
    Iterator end() const {
deba@1778
  1334
      return _end;
deba@1778
  1335
    }
deba@1778
  1336
kpeter@2564
  1337
    /// The \c set function of the map
deba@2423
  1338
    void set(const Key& key, Value value) const {
deba@1778
  1339
      if (value) {
deba@2091
  1340
	*_end++ = _functor(key);
deba@1778
  1341
      }
deba@1778
  1342
    }
deba@1778
  1343
    
deba@1778
  1344
  private:
deba@2423
  1345
    Iterator _begin;
deba@2423
  1346
    mutable Iterator _end;
deba@2091
  1347
    Functor _functor;
deba@1778
  1348
  };
deba@1778
  1349
kpeter@2564
  1350
  /// \brief Writable bool map for logging each \c true assigned element in 
deba@1778
  1351
  /// a back insertable container.
deba@1778
  1352
  ///
kpeter@2564
  1353
  /// Writable bool map for logging each \c true assigned element by pushing
kpeter@2564
  1354
  /// them into a back insertable container.
kpeter@2564
  1355
  /// It can be used to retrieve the items into a standard
kpeter@2564
  1356
  /// container. The next example shows how you can store the
kpeter@2564
  1357
  /// edges found by the Prim algorithm in a vector.
deba@2091
  1358
  ///
deba@2091
  1359
  ///\code
deba@2091
  1360
  /// vector<UEdge> span_tree_uedges;
deba@2091
  1361
  /// BackInserterBoolMap<vector<UEdge> > inserter_map(span_tree_uedges);
deba@2091
  1362
  /// prim(ugraph, cost, inserter_map);
deba@2091
  1363
  ///\endcode
kpeter@2564
  1364
  ///
kpeter@2564
  1365
  ///\sa StoreBoolMap
kpeter@2564
  1366
  ///\sa FrontInserterBoolMap
kpeter@2564
  1367
  ///\sa InserterBoolMap
deba@2091
  1368
  template <typename Container,
deba@2091
  1369
            typename Functor =
deba@2091
  1370
            _maps_bits::Identity<typename Container::value_type> >
deba@1778
  1371
  class BackInserterBoolMap {
deba@1778
  1372
  public:
kpeter@2564
  1373
    typedef typename Functor::argument_type Key;
deba@1778
  1374
    typedef bool Value;
deba@1778
  1375
deba@1778
  1376
    /// Constructor
deba@2091
  1377
    BackInserterBoolMap(Container& _container, 
deba@2091
  1378
                        const Functor& _functor = Functor()) 
deba@2091
  1379
      : container(_container), functor(_functor) {}
deba@1778
  1380
kpeter@2564
  1381
    /// The \c set function of the map
deba@1778
  1382
    void set(const Key& key, Value value) {
deba@1778
  1383
      if (value) {
deba@2091
  1384
	container.push_back(functor(key));
deba@1778
  1385
      }
deba@1778
  1386
    }
deba@1778
  1387
    
deba@1778
  1388
  private:
deba@2091
  1389
    Container& container;
deba@2091
  1390
    Functor functor;
deba@1778
  1391
  };
deba@1778
  1392
kpeter@2564
  1393
  /// \brief Writable bool map for logging each \c true assigned element in 
deba@1778
  1394
  /// a front insertable container.
deba@1778
  1395
  ///
kpeter@2564
  1396
  /// Writable bool map for logging each \c true assigned element by pushing
kpeter@2564
  1397
  /// them into a front insertable container.
kpeter@2564
  1398
  /// It can be used to retrieve the items into a standard
kpeter@2564
  1399
  /// container. For example see \ref BackInserterBoolMap.
kpeter@2564
  1400
  ///
kpeter@2564
  1401
  ///\sa BackInserterBoolMap
kpeter@2564
  1402
  ///\sa InserterBoolMap
deba@2091
  1403
  template <typename Container,
deba@2091
  1404
            typename Functor =
deba@2091
  1405
            _maps_bits::Identity<typename Container::value_type> >
deba@1778
  1406
  class FrontInserterBoolMap {
deba@1778
  1407
  public:
kpeter@2564
  1408
    typedef typename Functor::argument_type Key;
deba@1778
  1409
    typedef bool Value;
deba@1778
  1410
deba@1778
  1411
    /// Constructor
deba@2091
  1412
    FrontInserterBoolMap(Container& _container,
deba@2091
  1413
                         const Functor& _functor = Functor()) 
deba@2091
  1414
      : container(_container), functor(_functor) {}
deba@1778
  1415
kpeter@2564
  1416
    /// The \c set function of the map
deba@1778
  1417
    void set(const Key& key, Value value) {
deba@1778
  1418
      if (value) {
kpeter@2564
  1419
	container.push_front(functor(key));
deba@1778
  1420
      }
deba@1778
  1421
    }
deba@1778
  1422
    
deba@1778
  1423
  private:
deba@1778
  1424
    Container& container;    
deba@2091
  1425
    Functor functor;
deba@1778
  1426
  };
deba@1778
  1427
kpeter@2564
  1428
  /// \brief Writable bool map for storing each \c true assigned element in 
deba@1778
  1429
  /// an insertable container.
deba@1778
  1430
  ///
kpeter@2564
  1431
  /// Writable bool map for storing each \c true assigned element in an 
alpar@2258
  1432
  /// insertable container. It will insert all the keys set to \c true into
kpeter@2564
  1433
  /// the container.
kpeter@2564
  1434
  ///
kpeter@2564
  1435
  /// For example, if you want to store the cut arcs of the strongly
deba@2091
  1436
  /// connected components in a set you can use the next code:
deba@2091
  1437
  ///
deba@2091
  1438
  ///\code
deba@2091
  1439
  /// set<Edge> cut_edges;
deba@2091
  1440
  /// InserterBoolMap<set<Edge> > inserter_map(cut_edges);
deba@2091
  1441
  /// stronglyConnectedCutEdges(graph, cost, inserter_map);
deba@2091
  1442
  ///\endcode
kpeter@2564
  1443
  ///
kpeter@2564
  1444
  ///\sa BackInserterBoolMap
kpeter@2564
  1445
  ///\sa FrontInserterBoolMap
deba@2091
  1446
  template <typename Container,
deba@2091
  1447
            typename Functor =
deba@2091
  1448
            _maps_bits::Identity<typename Container::value_type> >
deba@1778
  1449
  class InserterBoolMap {
deba@1778
  1450
  public:
deba@1778
  1451
    typedef typename Container::value_type Key;
deba@1778
  1452
    typedef bool Value;
deba@1778
  1453
kpeter@2564
  1454
    /// Constructor with specified iterator
kpeter@2564
  1455
    
kpeter@2564
  1456
    /// Constructor with specified iterator.
kpeter@2564
  1457
    /// \param _container The container for storing the elements.
kpeter@2564
  1458
    /// \param _it The elements will be inserted before this iterator.
kpeter@2564
  1459
    /// \param _functor The functor that is used when an element is stored.
deba@2091
  1460
    InserterBoolMap(Container& _container, typename Container::iterator _it,
deba@2091
  1461
                    const Functor& _functor = Functor()) 
deba@2091
  1462
      : container(_container), it(_it), functor(_functor) {}
deba@2091
  1463
deba@2091
  1464
    /// Constructor
kpeter@2564
  1465
kpeter@2564
  1466
    /// Constructor without specified iterator.
kpeter@2564
  1467
    /// The elements will be inserted before <tt>_container.end()</tt>.
kpeter@2564
  1468
    /// \param _container The container for storing the elements.
kpeter@2564
  1469
    /// \param _functor The functor that is used when an element is stored.
deba@2091
  1470
    InserterBoolMap(Container& _container, const Functor& _functor = Functor())
deba@2091
  1471
      : container(_container), it(_container.end()), functor(_functor) {}
deba@1778
  1472
kpeter@2564
  1473
    /// The \c set function of the map
deba@1778
  1474
    void set(const Key& key, Value value) {
deba@1778
  1475
      if (value) {
kpeter@2564
  1476
	it = container.insert(it, functor(key));
deba@2091
  1477
        ++it;
deba@1778
  1478
      }
deba@1778
  1479
    }
deba@1778
  1480
    
deba@1778
  1481
  private:
deba@2091
  1482
    Container& container;
deba@2091
  1483
    typename Container::iterator it;
deba@2091
  1484
    Functor functor;
deba@1778
  1485
  };
deba@1778
  1486
kpeter@2564
  1487
  /// \brief Writable bool map for filling each \c true assigned element with a 
kpeter@2564
  1488
  /// given value.
deba@1778
  1489
  ///
kpeter@2564
  1490
  /// Writable bool map for filling each \c true assigned element with a 
kpeter@2564
  1491
  /// given value. The value can set the container.
deba@2091
  1492
  ///
kpeter@2564
  1493
  /// The following code finds the connected components of a graph
deba@2091
  1494
  /// and stores it in the \c comp map:
deba@2091
  1495
  ///\code
deba@2091
  1496
  /// typedef UGraph::NodeMap<int> ComponentMap;
deba@2091
  1497
  /// ComponentMap comp(ugraph);
deba@2091
  1498
  /// typedef FillBoolMap<UGraph::NodeMap<int> > ComponentFillerMap;
deba@2091
  1499
  /// ComponentFillerMap filler(comp, 0);
deba@2091
  1500
  ///
deba@2091
  1501
  /// Dfs<UGraph>::DefProcessedMap<ComponentFillerMap>::Create dfs(ugraph);
deba@2091
  1502
  /// dfs.processedMap(filler);
deba@2091
  1503
  /// dfs.init();
deba@2091
  1504
  /// for (NodeIt it(ugraph); it != INVALID; ++it) {
deba@2091
  1505
  ///   if (!dfs.reached(it)) {
deba@2091
  1506
  ///     dfs.addSource(it);
deba@2091
  1507
  ///     dfs.start();
deba@2091
  1508
  ///     ++filler.fillValue();
deba@2091
  1509
  ///   }
deba@2091
  1510
  /// }
deba@2091
  1511
  ///\endcode
deba@1778
  1512
  template <typename Map>
deba@1778
  1513
  class FillBoolMap {
deba@1778
  1514
  public:
deba@1778
  1515
    typedef typename Map::Key Key;
deba@1778
  1516
    typedef bool Value;
deba@1778
  1517
deba@1778
  1518
    /// Constructor
deba@1778
  1519
    FillBoolMap(Map& _map, const typename Map::Value& _fill) 
deba@1778
  1520
      : map(_map), fill(_fill) {}
deba@1778
  1521
deba@1778
  1522
    /// Constructor
deba@1778
  1523
    FillBoolMap(Map& _map) 
deba@1778
  1524
      : map(_map), fill() {}
deba@1778
  1525
deba@1778
  1526
    /// Gives back the current fill value
deba@2091
  1527
    const typename Map::Value& fillValue() const {
deba@2091
  1528
      return fill;
deba@2091
  1529
    } 
deba@2091
  1530
deba@2091
  1531
    /// Gives back the current fill value
deba@2091
  1532
    typename Map::Value& fillValue() {
deba@1778
  1533
      return fill;
deba@1778
  1534
    } 
deba@1778
  1535
deba@1778
  1536
    /// Sets the current fill value
deba@1778
  1537
    void fillValue(const typename Map::Value& _fill) {
deba@1778
  1538
      fill = _fill;
deba@1778
  1539
    } 
deba@1778
  1540
kpeter@2564
  1541
    /// The \c set function of the map
deba@1778
  1542
    void set(const Key& key, Value value) {
deba@1778
  1543
      if (value) {
deba@1778
  1544
	map.set(key, fill);
deba@1778
  1545
      }
deba@1778
  1546
    }
deba@1778
  1547
    
deba@1778
  1548
  private:
deba@1778
  1549
    Map& map;
deba@1778
  1550
    typename Map::Value fill;
deba@1778
  1551
  };
deba@1778
  1552
deba@1778
  1553
kpeter@2564
  1554
  /// \brief Writable bool map for storing the sequence number of 
kpeter@2564
  1555
  /// \c true assignments.  
deba@1778
  1556
  ///
kpeter@2564
  1557
  /// Writable bool map that stores for each \c true assigned elements  
kpeter@2564
  1558
  /// the sequence number of this setting.
kpeter@2564
  1559
  /// It makes it easy to calculate the leaving
deba@2489
  1560
  /// order of the nodes in the \c Dfs algorithm.
deba@2091
  1561
  ///
deba@2091
  1562
  ///\code
deba@2091
  1563
  /// typedef Graph::NodeMap<int> OrderMap;
deba@2091
  1564
  /// OrderMap order(graph);
deba@2091
  1565
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
deba@2091
  1566
  /// OrderSetterMap setter(order);
deba@2091
  1567
  /// Dfs<Graph>::DefProcessedMap<OrderSetterMap>::Create dfs(graph);
deba@2091
  1568
  /// dfs.processedMap(setter);
deba@2091
  1569
  /// dfs.init();
deba@2091
  1570
  /// for (NodeIt it(graph); it != INVALID; ++it) {
deba@2091
  1571
  ///   if (!dfs.reached(it)) {
deba@2091
  1572
  ///     dfs.addSource(it);
deba@2091
  1573
  ///     dfs.start();
deba@2091
  1574
  ///   }
deba@2091
  1575
  /// }
deba@2091
  1576
  ///\endcode
deba@2091
  1577
  ///
kpeter@2564
  1578
  /// The storing of the discovering order is more difficult because the
deba@2091
  1579
  /// ReachedMap should be readable in the dfs algorithm but the setting
kpeter@2564
  1580
  /// order map is not readable. Thus we must use the fork map:
deba@2091
  1581
  ///
deba@2091
  1582
  ///\code
deba@2091
  1583
  /// typedef Graph::NodeMap<int> OrderMap;
deba@2091
  1584
  /// OrderMap order(graph);
deba@2091
  1585
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
deba@2091
  1586
  /// OrderSetterMap setter(order);
deba@2091
  1587
  /// typedef Graph::NodeMap<bool> StoreMap;
deba@2091
  1588
  /// StoreMap store(graph);
deba@2091
  1589
  ///
deba@2091
  1590
  /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
deba@2091
  1591
  /// ReachedMap reached(store, setter);
deba@2091
  1592
  ///
deba@2091
  1593
  /// Dfs<Graph>::DefReachedMap<ReachedMap>::Create dfs(graph);
deba@2091
  1594
  /// dfs.reachedMap(reached);
deba@2091
  1595
  /// dfs.init();
deba@2091
  1596
  /// for (NodeIt it(graph); it != INVALID; ++it) {
deba@2091
  1597
  ///   if (!dfs.reached(it)) {
deba@2091
  1598
  ///     dfs.addSource(it);
deba@2091
  1599
  ///     dfs.start();
deba@2091
  1600
  ///   }
deba@2091
  1601
  /// }
deba@2091
  1602
  ///\endcode
deba@1778
  1603
  template <typename Map>
deba@1778
  1604
  class SettingOrderBoolMap {
deba@1778
  1605
  public:
deba@1778
  1606
    typedef typename Map::Key Key;
deba@1778
  1607
    typedef bool Value;
deba@1778
  1608
deba@1778
  1609
    /// Constructor
deba@1778
  1610
    SettingOrderBoolMap(Map& _map) 
deba@1778
  1611
      : map(_map), counter(0) {}
deba@1778
  1612
alpar@2258
  1613
    /// Number of set operations.
deba@1778
  1614
    int num() const {
deba@1778
  1615
      return counter;
deba@1778
  1616
    }
deba@1778
  1617
kpeter@2564
  1618
    /// The \c set function of the map
deba@1778
  1619
    void set(const Key& key, Value value) {
deba@1778
  1620
      if (value) {
deba@1778
  1621
	map.set(key, counter++);
deba@1778
  1622
      }
deba@1778
  1623
    }
deba@1778
  1624
    
deba@1778
  1625
  private:
deba@1778
  1626
    Map& map;
deba@1778
  1627
    int counter;
deba@1778
  1628
  };
deba@1778
  1629
alpar@1041
  1630
  /// @}
klao@286
  1631
}
alpar@1041
  1632
alpar@921
  1633
#endif // LEMON_MAPS_H