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