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