lemon/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 29 Mar 2008 15:49:50 +0000
changeset 111 e47fb28cda5e
parent 82 bce6c8f93d10
child 123 8899d1891a3c
permissions -rw-r--r--
Fix ignoring Emacs backup files
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@39
     5
 * Copyright (C) 2003-2008
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>
kpeter@80
    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
kpeter@80
    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
kpeter@80
    42
  /// Base class of maps. It provides the necessary type definitions
kpeter@80
    43
  /// required by the map %concepts.
kpeter@80
    44
  template<typename K, typename V>
alpar@25
    45
  class MapBase {
alpar@25
    46
  public:
kpeter@80
    47
    /// \biref The key type of the map.
alpar@25
    48
    typedef K Key;
kpeter@80
    49
    /// \brief The value type of the map.
kpeter@80
    50
    /// (The type of objects associated with the keys).
kpeter@80
    51
    typedef V Value;
alpar@25
    52
  };
alpar@25
    53
kpeter@80
    54
alpar@25
    55
  /// Null map. (a.k.a. DoNothingMap)
alpar@25
    56
kpeter@29
    57
  /// This map can be used if you have to provide a map only for
kpeter@80
    58
  /// its type definitions, or if you have to provide a writable map,
kpeter@80
    59
  /// but data written to it is not required (i.e. it will be sent to
kpeter@29
    60
  /// <tt>/dev/null</tt>).
kpeter@80
    61
  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
kpeter@80
    62
  ///
kpeter@80
    63
  /// \sa ConstMap
kpeter@80
    64
  template<typename K, typename V>
kpeter@80
    65
  class NullMap : public MapBase<K, V> {
alpar@25
    66
  public:
kpeter@80
    67
    typedef MapBase<K, V> Parent;
alpar@25
    68
    typedef typename Parent::Key Key;
alpar@25
    69
    typedef typename Parent::Value Value;
kpeter@80
    70
alpar@25
    71
    /// Gives back a default constructed element.
kpeter@80
    72
    Value operator[](const Key&) const { return Value(); }
alpar@25
    73
    /// Absorbs the value.
kpeter@80
    74
    void set(const Key&, const Value&) {}
alpar@25
    75
  };
alpar@25
    76
kpeter@80
    77
  /// Returns a \ref NullMap class
kpeter@29
    78
kpeter@80
    79
  /// This function just returns a \ref NullMap class.
kpeter@80
    80
  /// \relates NullMap
kpeter@80
    81
  template <typename K, typename V>
alpar@25
    82
  NullMap<K, V> nullMap() {
alpar@25
    83
    return NullMap<K, V>();
alpar@25
    84
  }
alpar@25
    85
alpar@25
    86
alpar@25
    87
  /// Constant map.
alpar@25
    88
kpeter@82
    89
  /// This \ref concepts::ReadMap "readable map" assigns a specified
kpeter@82
    90
  /// value to each key.
kpeter@80
    91
  ///
kpeter@80
    92
  /// In other aspects it is equivalent to \ref NullMap.
kpeter@80
    93
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@80
    94
  /// concept, but it absorbs the data written to it.
kpeter@80
    95
  ///
kpeter@80
    96
  /// The simplest way of using this map is through the constMap()
kpeter@80
    97
  /// function.
kpeter@80
    98
  ///
kpeter@80
    99
  /// \sa NullMap
kpeter@80
   100
  /// \sa IdentityMap
kpeter@80
   101
  template<typename K, typename V>
kpeter@80
   102
  class ConstMap : public MapBase<K, V> {
alpar@25
   103
  private:
kpeter@80
   104
    V _value;
alpar@25
   105
  public:
kpeter@80
   106
    typedef MapBase<K, V> Parent;
alpar@25
   107
    typedef typename Parent::Key Key;
alpar@25
   108
    typedef typename Parent::Value Value;
alpar@25
   109
alpar@25
   110
    /// Default constructor
alpar@25
   111
kpeter@29
   112
    /// Default constructor.
kpeter@80
   113
    /// The value of the map will be default constructed.
alpar@25
   114
    ConstMap() {}
kpeter@80
   115
kpeter@29
   116
    /// Constructor with specified initial value
alpar@25
   117
kpeter@29
   118
    /// Constructor with specified initial value.
kpeter@80
   119
    /// \param v is the initial value of the map.
kpeter@80
   120
    ConstMap(const Value &v) : _value(v) {}
alpar@25
   121
kpeter@80
   122
    /// Gives back the specified value.
kpeter@80
   123
    Value operator[](const Key&) const { return _value; }
alpar@25
   124
kpeter@80
   125
    /// Absorbs the value.
kpeter@80
   126
    void set(const Key&, const Value&) {}
kpeter@80
   127
kpeter@80
   128
    /// Sets the value that is assigned to each key.
kpeter@80
   129
    void setAll(const Value &v) {
kpeter@80
   130
      _value = v;
kpeter@80
   131
    }
kpeter@80
   132
kpeter@80
   133
    template<typename V1>
kpeter@80
   134
    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
alpar@25
   135
  };
alpar@25
   136
kpeter@80
   137
  /// Returns a \ref ConstMap class
alpar@25
   138
kpeter@80
   139
  /// This function just returns a \ref ConstMap class.
kpeter@80
   140
  /// \relates ConstMap
kpeter@80
   141
  template<typename K, typename V>
alpar@25
   142
  inline ConstMap<K, V> constMap(const V &v) {
alpar@25
   143
    return ConstMap<K, V>(v);
alpar@25
   144
  }
alpar@25
   145
alpar@25
   146
alpar@25
   147
  template<typename T, T v>
kpeter@80
   148
  struct Const {};
alpar@25
   149
alpar@25
   150
  /// Constant map with inlined constant value.
alpar@25
   151
kpeter@82
   152
  /// This \ref concepts::ReadMap "readable map" assigns a specified
kpeter@82
   153
  /// value to each key.
kpeter@80
   154
  ///
kpeter@80
   155
  /// In other aspects it is equivalent to \ref NullMap.
kpeter@80
   156
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
kpeter@80
   157
  /// concept, but it absorbs the data written to it.
kpeter@80
   158
  ///
kpeter@80
   159
  /// The simplest way of using this map is through the constMap()
kpeter@80
   160
  /// function.
kpeter@80
   161
  ///
kpeter@80
   162
  /// \sa NullMap
kpeter@80
   163
  /// \sa IdentityMap
alpar@25
   164
  template<typename K, typename V, V v>
alpar@25
   165
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
alpar@25
   166
  public:
alpar@25
   167
    typedef MapBase<K, V> Parent;
alpar@25
   168
    typedef typename Parent::Key Key;
alpar@25
   169
    typedef typename Parent::Value Value;
alpar@25
   170
kpeter@80
   171
    /// Constructor.
kpeter@80
   172
    ConstMap() {}
kpeter@80
   173
kpeter@80
   174
    /// Gives back the specified value.
kpeter@80
   175
    Value operator[](const Key&) const { return v; }
kpeter@80
   176
kpeter@80
   177
    /// Absorbs the value.
kpeter@80
   178
    void set(const Key&, const Value&) {}
alpar@25
   179
  };
alpar@25
   180
kpeter@80
   181
  /// Returns a \ref ConstMap class with inlined constant value
alpar@25
   182
kpeter@80
   183
  /// This function just returns a \ref ConstMap class with inlined
kpeter@80
   184
  /// constant value.
kpeter@80
   185
  /// \relates ConstMap
kpeter@80
   186
  template<typename K, typename V, V v>
alpar@25
   187
  inline ConstMap<K, Const<V, v> > constMap() {
alpar@25
   188
    return ConstMap<K, Const<V, v> >();
alpar@25
   189
  }
alpar@25
   190
alpar@25
   191
kpeter@82
   192
  /// Identity map.
kpeter@82
   193
kpeter@82
   194
  /// This \ref concepts::ReadMap "read-only map" gives back the given
kpeter@82
   195
  /// key as value without any modification.
kpeter@80
   196
  ///
kpeter@80
   197
  /// \sa ConstMap
kpeter@80
   198
  template <typename T>
kpeter@80
   199
  class IdentityMap : public MapBase<T, T> {
kpeter@80
   200
  public:
kpeter@80
   201
    typedef MapBase<T, T> Parent;
kpeter@80
   202
    typedef typename Parent::Key Key;
kpeter@80
   203
    typedef typename Parent::Value Value;
kpeter@80
   204
kpeter@80
   205
    /// Gives back the given value without any modification.
kpeter@82
   206
    Value operator[](const Key &k) const {
kpeter@82
   207
      return k;
kpeter@80
   208
    }
kpeter@80
   209
  };
kpeter@80
   210
kpeter@80
   211
  /// Returns an \ref IdentityMap class
kpeter@80
   212
kpeter@80
   213
  /// This function just returns an \ref IdentityMap class.
kpeter@80
   214
  /// \relates IdentityMap
kpeter@80
   215
  template<typename T>
kpeter@80
   216
  inline IdentityMap<T> identityMap() {
kpeter@80
   217
    return IdentityMap<T>();
kpeter@80
   218
  }
kpeter@80
   219
kpeter@80
   220
kpeter@80
   221
  /// \brief Map for storing values for integer keys from the range
kpeter@80
   222
  /// <tt>[0..size-1]</tt>.
kpeter@80
   223
  ///
kpeter@80
   224
  /// This map is essentially a wrapper for \c std::vector. It assigns
kpeter@80
   225
  /// values to integer keys from the range <tt>[0..size-1]</tt>.
kpeter@80
   226
  /// It can be used with some data structures, for example
kpeter@80
   227
  /// \ref UnionFind, \ref BinHeap, when the used items are small
kpeter@80
   228
  /// integers. This map conforms the \ref concepts::ReferenceMap
kpeter@80
   229
  /// "ReferenceMap" concept.
kpeter@80
   230
  ///
kpeter@80
   231
  /// The simplest way of using this map is through the rangeMap()
kpeter@80
   232
  /// function.
kpeter@80
   233
  template <typename V>
kpeter@80
   234
  class RangeMap : public MapBase<int, V> {
kpeter@80
   235
    template <typename V1>
kpeter@80
   236
    friend class RangeMap;
kpeter@80
   237
  private:
kpeter@80
   238
kpeter@80
   239
    typedef std::vector<V> Vector;
kpeter@80
   240
    Vector _vector;
kpeter@80
   241
alpar@25
   242
  public:
alpar@25
   243
kpeter@80
   244
    typedef MapBase<int, V> Parent;
kpeter@80
   245
    /// Key type
kpeter@45
   246
    typedef typename Parent::Key Key;
kpeter@80
   247
    /// Value type
kpeter@45
   248
    typedef typename Parent::Value Value;
kpeter@80
   249
    /// Reference type
kpeter@80
   250
    typedef typename Vector::reference Reference;
kpeter@80
   251
    /// Const reference type
kpeter@80
   252
    typedef typename Vector::const_reference ConstReference;
kpeter@80
   253
kpeter@80
   254
    typedef True ReferenceMapTag;
kpeter@80
   255
kpeter@80
   256
  public:
kpeter@80
   257
kpeter@80
   258
    /// Constructor with specified default value.
kpeter@80
   259
    RangeMap(int size = 0, const Value &value = Value())
kpeter@80
   260
      : _vector(size, value) {}
kpeter@80
   261
kpeter@80
   262
    /// Constructs the map from an appropriate \c std::vector.
kpeter@80
   263
    template <typename V1>
kpeter@80
   264
    RangeMap(const std::vector<V1>& vector)
kpeter@80
   265
      : _vector(vector.begin(), vector.end()) {}
kpeter@80
   266
kpeter@80
   267
    /// Constructs the map from another \ref RangeMap.
kpeter@80
   268
    template <typename V1>
kpeter@80
   269
    RangeMap(const RangeMap<V1> &c)
kpeter@80
   270
      : _vector(c._vector.begin(), c._vector.end()) {}
kpeter@80
   271
kpeter@80
   272
    /// Returns the size of the map.
kpeter@80
   273
    int size() {
kpeter@80
   274
      return _vector.size();
kpeter@80
   275
    }
kpeter@80
   276
kpeter@80
   277
    /// Resizes the map.
kpeter@80
   278
kpeter@80
   279
    /// Resizes the underlying \c std::vector container, so changes the
kpeter@80
   280
    /// keyset of the map.
kpeter@80
   281
    /// \param size The new size of the map. The new keyset will be the
kpeter@80
   282
    /// range <tt>[0..size-1]</tt>.
kpeter@80
   283
    /// \param value The default value to assign to the new keys.
kpeter@80
   284
    void resize(int size, const Value &value = Value()) {
kpeter@80
   285
      _vector.resize(size, value);
kpeter@80
   286
    }
kpeter@80
   287
kpeter@80
   288
  private:
kpeter@80
   289
kpeter@80
   290
    RangeMap& operator=(const RangeMap&);
kpeter@80
   291
kpeter@80
   292
  public:
kpeter@80
   293
kpeter@80
   294
    ///\e
kpeter@80
   295
    Reference operator[](const Key &k) {
kpeter@80
   296
      return _vector[k];
kpeter@80
   297
    }
kpeter@80
   298
kpeter@80
   299
    ///\e
kpeter@80
   300
    ConstReference operator[](const Key &k) const {
kpeter@80
   301
      return _vector[k];
kpeter@80
   302
    }
kpeter@80
   303
kpeter@80
   304
    ///\e
kpeter@80
   305
    void set(const Key &k, const Value &v) {
kpeter@80
   306
      _vector[k] = v;
kpeter@80
   307
    }
kpeter@80
   308
  };
kpeter@80
   309
kpeter@80
   310
  /// Returns a \ref RangeMap class
kpeter@80
   311
kpeter@80
   312
  /// This function just returns a \ref RangeMap class.
kpeter@80
   313
  /// \relates RangeMap
kpeter@80
   314
  template<typename V>
kpeter@80
   315
  inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
kpeter@80
   316
    return RangeMap<V>(size, value);
kpeter@80
   317
  }
kpeter@80
   318
kpeter@80
   319
  /// \brief Returns a \ref RangeMap class created from an appropriate
kpeter@80
   320
  /// \c std::vector
kpeter@80
   321
kpeter@80
   322
  /// This function just returns a \ref RangeMap class created from an
kpeter@80
   323
  /// appropriate \c std::vector.
kpeter@80
   324
  /// \relates RangeMap
kpeter@80
   325
  template<typename V>
kpeter@80
   326
  inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
kpeter@80
   327
    return RangeMap<V>(vector);
kpeter@80
   328
  }
kpeter@80
   329
kpeter@80
   330
kpeter@80
   331
  /// Map type based on \c std::map
kpeter@80
   332
kpeter@80
   333
  /// This map is essentially a wrapper for \c std::map with addition
kpeter@80
   334
  /// that you can specify a default value for the keys that are not
kpeter@80
   335
  /// stored actually. This value can be different from the default
kpeter@80
   336
  /// contructed value (i.e. \c %Value()).
kpeter@80
   337
  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
kpeter@80
   338
  /// concept.
kpeter@80
   339
  ///
kpeter@80
   340
  /// This map is useful if a default value should be assigned to most of
kpeter@80
   341
  /// the keys and different values should be assigned only to a few
kpeter@80
   342
  /// keys (i.e. the map is "sparse").
kpeter@80
   343
  /// The name of this type also refers to this important usage.
kpeter@80
   344
  ///
kpeter@80
   345
  /// Apart form that this map can be used in many other cases since it
kpeter@80
   346
  /// is based on \c std::map, which is a general associative container.
kpeter@80
   347
  /// However keep in mind that it is usually not as efficient as other
kpeter@80
   348
  /// maps.
kpeter@80
   349
  ///
kpeter@80
   350
  /// The simplest way of using this map is through the sparseMap()
kpeter@80
   351
  /// function.
kpeter@80
   352
  template <typename K, typename V, typename Compare = std::less<K> >
kpeter@80
   353
  class SparseMap : public MapBase<K, V> {
kpeter@80
   354
    template <typename K1, typename V1, typename C1>
kpeter@80
   355
    friend class SparseMap;
kpeter@80
   356
  public:
kpeter@80
   357
kpeter@80
   358
    typedef MapBase<K, V> Parent;
kpeter@80
   359
    /// Key type
kpeter@80
   360
    typedef typename Parent::Key Key;
kpeter@80
   361
    /// Value type
kpeter@80
   362
    typedef typename Parent::Value Value;
kpeter@80
   363
    /// Reference type
kpeter@80
   364
    typedef Value& Reference;
kpeter@80
   365
    /// Const reference type
kpeter@80
   366
    typedef const Value& ConstReference;
alpar@25
   367
kpeter@45
   368
    typedef True ReferenceMapTag;
kpeter@45
   369
alpar@25
   370
  private:
kpeter@80
   371
kpeter@80
   372
    typedef std::map<K, V, Compare> Map;
kpeter@80
   373
    Map _map;
alpar@25
   374
    Value _value;
alpar@25
   375
alpar@25
   376
  public:
alpar@25
   377
kpeter@80
   378
    /// \brief Constructor with specified default value.
kpeter@80
   379
    SparseMap(const Value &value = Value()) : _value(value) {}
kpeter@80
   380
    /// \brief Constructs the map from an appropriate \c std::map, and
kpeter@47
   381
    /// explicitly specifies a default value.
kpeter@80
   382
    template <typename V1, typename Comp1>
kpeter@80
   383
    SparseMap(const std::map<Key, V1, Comp1> &map,
kpeter@80
   384
              const Value &value = Value())
alpar@25
   385
      : _map(map.begin(), map.end()), _value(value) {}
kpeter@80
   386
kpeter@80
   387
    /// \brief Constructs the map from another \ref SparseMap.
kpeter@80
   388
    template<typename V1, typename Comp1>
kpeter@80
   389
    SparseMap(const SparseMap<Key, V1, Comp1> &c)
alpar@25
   390
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
alpar@25
   391
alpar@25
   392
  private:
alpar@25
   393
kpeter@80
   394
    SparseMap& operator=(const SparseMap&);
alpar@25
   395
alpar@25
   396
  public:
alpar@25
   397
alpar@25
   398
    ///\e
alpar@25
   399
    Reference operator[](const Key &k) {
alpar@25
   400
      typename Map::iterator it = _map.lower_bound(k);
alpar@25
   401
      if (it != _map.end() && !_map.key_comp()(k, it->first))
alpar@25
   402
	return it->second;
alpar@25
   403
      else
alpar@25
   404
	return _map.insert(it, std::make_pair(k, _value))->second;
alpar@25
   405
    }
alpar@25
   406
kpeter@80
   407
    ///\e
alpar@25
   408
    ConstReference operator[](const Key &k) const {
alpar@25
   409
      typename Map::const_iterator it = _map.find(k);
alpar@25
   410
      if (it != _map.end())
alpar@25
   411
	return it->second;
alpar@25
   412
      else
alpar@25
   413
	return _value;
alpar@25
   414
    }
alpar@25
   415
kpeter@80
   416
    ///\e
kpeter@80
   417
    void set(const Key &k, const Value &v) {
alpar@25
   418
      typename Map::iterator it = _map.lower_bound(k);
alpar@25
   419
      if (it != _map.end() && !_map.key_comp()(k, it->first))
kpeter@80
   420
	it->second = v;
alpar@25
   421
      else
kpeter@80
   422
	_map.insert(it, std::make_pair(k, v));
alpar@25
   423
    }
alpar@25
   424
kpeter@80
   425
    ///\e
kpeter@80
   426
    void setAll(const Value &v) {
kpeter@80
   427
      _value = v;
alpar@25
   428
      _map.clear();
kpeter@80
   429
    }
kpeter@80
   430
  };
alpar@25
   431
kpeter@80
   432
  /// Returns a \ref SparseMap class
kpeter@45
   433
kpeter@80
   434
  /// This function just returns a \ref SparseMap class with specified
kpeter@80
   435
  /// default value.
kpeter@80
   436
  /// \relates SparseMap
kpeter@80
   437
  template<typename K, typename V, typename Compare>
kpeter@80
   438
  inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
kpeter@80
   439
    return SparseMap<K, V, Compare>(value);
kpeter@54
   440
  }
kpeter@45
   441
kpeter@80
   442
  template<typename K, typename V>
kpeter@80
   443
  inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
kpeter@80
   444
    return SparseMap<K, V, std::less<K> >(value);
kpeter@45
   445
  }
alpar@25
   446
kpeter@80
   447
  /// \brief Returns a \ref SparseMap class created from an appropriate
kpeter@80
   448
  /// \c std::map
alpar@25
   449
kpeter@80
   450
  /// This function just returns a \ref SparseMap class created from an
kpeter@80
   451
  /// appropriate \c std::map.
kpeter@80
   452
  /// \relates SparseMap
kpeter@80
   453
  template<typename K, typename V, typename Compare>
kpeter@80
   454
  inline SparseMap<K, V, Compare>
kpeter@80
   455
    sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
kpeter@80
   456
  {
kpeter@80
   457
    return SparseMap<K, V, Compare>(map, value);
kpeter@45
   458
  }
alpar@25
   459
alpar@25
   460
  /// @}
alpar@25
   461
alpar@25
   462
  /// \addtogroup map_adaptors
alpar@25
   463
  /// @{
alpar@25
   464
kpeter@80
   465
  /// Composition of two maps
kpeter@80
   466
kpeter@82
   467
  /// This \ref concepts::ReadMap "read-only map" returns the
kpeter@80
   468
  /// composition of two given maps. That is to say, if \c m1 is of
kpeter@80
   469
  /// type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   470
  /// \code
kpeter@80
   471
  ///   ComposeMap<M1, M2> cm(m1,m2);
kpeter@80
   472
  /// \endcode
kpeter@80
   473
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
alpar@25
   474
  ///
kpeter@80
   475
  /// The \c Key type of the map is inherited from \c M2 and the
kpeter@80
   476
  /// \c Value type is from \c M1.
kpeter@80
   477
  /// \c M2::Value must be convertible to \c M1::Key.
kpeter@80
   478
  ///
kpeter@80
   479
  /// The simplest way of using this map is through the composeMap()
kpeter@80
   480
  /// function.
kpeter@80
   481
  ///
kpeter@80
   482
  /// \sa CombineMap
kpeter@80
   483
  ///
kpeter@80
   484
  /// \todo Check the requirements.
kpeter@80
   485
  template <typename M1, typename M2>
kpeter@80
   486
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
kpeter@80
   487
    const M1 &_m1;
kpeter@80
   488
    const M2 &_m2;
alpar@25
   489
  public:
kpeter@80
   490
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
alpar@25
   491
    typedef typename Parent::Key Key;
alpar@25
   492
    typedef typename Parent::Value Value;
alpar@25
   493
kpeter@80
   494
    /// Constructor
kpeter@80
   495
    ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   496
alpar@25
   497
    /// \e
kpeter@80
   498
    typename MapTraits<M1>::ConstReturnValue
kpeter@80
   499
    operator[](const Key &k) const { return _m1[_m2[k]]; }
alpar@25
   500
  };
alpar@25
   501
kpeter@80
   502
  /// Returns a \ref ComposeMap class
alpar@25
   503
kpeter@80
   504
  /// This function just returns a \ref ComposeMap class.
kpeter@80
   505
  ///
kpeter@80
   506
  /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
kpeter@80
   507
  /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
kpeter@80
   508
  /// will be equal to <tt>m1[m2[x]]</tt>.
kpeter@80
   509
  ///
kpeter@80
   510
  /// \relates ComposeMap
kpeter@80
   511
  template <typename M1, typename M2>
kpeter@80
   512
  inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
kpeter@80
   513
    return ComposeMap<M1, M2>(m1, m2);
alpar@25
   514
  }
alpar@25
   515
kpeter@80
   516
kpeter@80
   517
  /// Combination of two maps using an STL (binary) functor.
kpeter@80
   518
kpeter@82
   519
  /// This \ref concepts::ReadMap "read-only map" takes two maps and a
kpeter@80
   520
  /// binary functor and returns the combination of the two given maps
kpeter@80
   521
  /// using the functor.
kpeter@80
   522
  /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
kpeter@80
   523
  /// and \c f is of \c F, then for
kpeter@80
   524
  /// \code
kpeter@80
   525
  ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
kpeter@80
   526
  /// \endcode
kpeter@80
   527
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
alpar@26
   528
  ///
kpeter@80
   529
  /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
kpeter@80
   530
  /// must be convertible to \c M2::Key) and the \c Value type is \c V.
kpeter@80
   531
  /// \c M2::Value and \c M1::Value must be convertible to the
kpeter@80
   532
  /// corresponding input parameter of \c F and the return type of \c F
kpeter@80
   533
  /// must be convertible to \c V.
kpeter@80
   534
  ///
kpeter@80
   535
  /// The simplest way of using this map is through the combineMap()
kpeter@80
   536
  /// function.
kpeter@80
   537
  ///
kpeter@80
   538
  /// \sa ComposeMap
kpeter@80
   539
  ///
kpeter@80
   540
  /// \todo Check the requirements.
kpeter@80
   541
  template<typename M1, typename M2, typename F,
kpeter@80
   542
	   typename V = typename F::result_type>
kpeter@80
   543
  class CombineMap : public MapBase<typename M1::Key, V> {
kpeter@80
   544
    const M1 &_m1;
kpeter@80
   545
    const M2 &_m2;
kpeter@80
   546
    F _f;
alpar@25
   547
  public:
kpeter@80
   548
    typedef MapBase<typename M1::Key, V> Parent;
alpar@25
   549
    typedef typename Parent::Key Key;
alpar@25
   550
    typedef typename Parent::Value Value;
alpar@25
   551
kpeter@80
   552
    /// Constructor
kpeter@80
   553
    CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
kpeter@80
   554
      : _m1(m1), _m2(m2), _f(f) {}
kpeter@80
   555
    /// \e
kpeter@80
   556
    Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
kpeter@80
   557
  };
alpar@25
   558
kpeter@80
   559
  /// Returns a \ref CombineMap class
alpar@25
   560
kpeter@80
   561
  /// This function just returns a \ref CombineMap class.
kpeter@80
   562
  ///
kpeter@80
   563
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   564
  /// values, then
kpeter@80
   565
  /// \code
kpeter@80
   566
  ///   combineMap(m1,m2,std::plus<double>())
kpeter@80
   567
  /// \endcode
kpeter@80
   568
  /// is equivalent to
kpeter@80
   569
  /// \code
kpeter@80
   570
  ///   addMap(m1,m2)
kpeter@80
   571
  /// \endcode
kpeter@80
   572
  ///
kpeter@80
   573
  /// This function is specialized for adaptable binary function
kpeter@80
   574
  /// classes and C++ functions.
kpeter@80
   575
  ///
kpeter@80
   576
  /// \relates CombineMap
kpeter@80
   577
  template<typename M1, typename M2, typename F, typename V>
kpeter@80
   578
  inline CombineMap<M1, M2, F, V>
kpeter@80
   579
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
kpeter@80
   580
    return CombineMap<M1, M2, F, V>(m1,m2,f);
alpar@25
   581
  }
alpar@25
   582
kpeter@80
   583
  template<typename M1, typename M2, typename F>
kpeter@80
   584
  inline CombineMap<M1, M2, F, typename F::result_type>
kpeter@80
   585
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
kpeter@80
   586
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
kpeter@80
   587
  }
alpar@25
   588
kpeter@80
   589
  template<typename M1, typename M2, typename K1, typename K2, typename V>
kpeter@80
   590
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
kpeter@80
   591
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
kpeter@80
   592
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
kpeter@80
   593
  }
kpeter@80
   594
kpeter@80
   595
kpeter@80
   596
  /// Converts an STL style (unary) functor to a map
kpeter@80
   597
kpeter@82
   598
  /// This \ref concepts::ReadMap "read-only map" returns the value
kpeter@80
   599
  /// of a given functor. Actually, it just wraps the functor and
kpeter@80
   600
  /// provides the \c Key and \c Value typedefs.
alpar@26
   601
  ///
kpeter@80
   602
  /// Template parameters \c K and \c V will become its \c Key and
kpeter@80
   603
  /// \c Value. In most cases they have to be given explicitly because
kpeter@80
   604
  /// a functor typically does not provide \c argument_type and
kpeter@80
   605
  /// \c result_type typedefs.
kpeter@80
   606
  /// Parameter \c F is the type of the used functor.
kpeter@29
   607
  ///
kpeter@80
   608
  /// The simplest way of using this map is through the functorToMap()
kpeter@80
   609
  /// function.
kpeter@80
   610
  ///
kpeter@80
   611
  /// \sa MapToFunctor
kpeter@80
   612
  template<typename F,
kpeter@80
   613
	   typename K = typename F::argument_type,
kpeter@80
   614
	   typename V = typename F::result_type>
kpeter@80
   615
  class FunctorToMap : public MapBase<K, V> {
kpeter@80
   616
    const F &_f;
kpeter@80
   617
  public:
kpeter@80
   618
    typedef MapBase<K, V> Parent;
kpeter@80
   619
    typedef typename Parent::Key Key;
kpeter@80
   620
    typedef typename Parent::Value Value;
alpar@25
   621
kpeter@80
   622
    /// Constructor
kpeter@80
   623
    FunctorToMap(const F &f = F()) : _f(f) {}
kpeter@80
   624
    /// \e
kpeter@80
   625
    Value operator[](const Key &k) const { return _f(k); }
kpeter@80
   626
  };
kpeter@80
   627
kpeter@80
   628
  /// Returns a \ref FunctorToMap class
kpeter@80
   629
kpeter@80
   630
  /// This function just returns a \ref FunctorToMap class.
kpeter@80
   631
  ///
kpeter@80
   632
  /// This function is specialized for adaptable binary function
kpeter@80
   633
  /// classes and C++ functions.
kpeter@80
   634
  ///
kpeter@80
   635
  /// \relates FunctorToMap
kpeter@80
   636
  template<typename K, typename V, typename F>
kpeter@80
   637
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
kpeter@80
   638
    return FunctorToMap<F, K, V>(f);
kpeter@80
   639
  }
kpeter@80
   640
kpeter@80
   641
  template <typename F>
kpeter@80
   642
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
kpeter@80
   643
    functorToMap(const F &f)
kpeter@80
   644
  {
kpeter@80
   645
    return FunctorToMap<F, typename F::argument_type,
kpeter@80
   646
      typename F::result_type>(f);
kpeter@80
   647
  }
kpeter@80
   648
kpeter@80
   649
  template <typename K, typename V>
kpeter@80
   650
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
kpeter@80
   651
    return FunctorToMap<V (*)(K), K, V>(f);
kpeter@80
   652
  }
kpeter@80
   653
kpeter@80
   654
kpeter@80
   655
  /// Converts a map to an STL style (unary) functor
kpeter@80
   656
kpeter@80
   657
  /// This class converts a map to an STL style (unary) functor.
kpeter@80
   658
  /// That is it provides an <tt>operator()</tt> to read its values.
kpeter@80
   659
  ///
kpeter@80
   660
  /// For the sake of convenience it also works as a usual
kpeter@80
   661
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
kpeter@80
   662
  /// and the \c Key and \c Value typedefs also exist.
kpeter@80
   663
  ///
kpeter@80
   664
  /// The simplest way of using this map is through the mapToFunctor()
kpeter@80
   665
  /// function.
kpeter@80
   666
  ///
kpeter@80
   667
  ///\sa FunctorToMap
kpeter@80
   668
  template <typename M>
kpeter@80
   669
  class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
   670
    const M &_m;
alpar@25
   671
  public:
alpar@25
   672
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
   673
    typedef typename Parent::Key Key;
alpar@25
   674
    typedef typename Parent::Value Value;
alpar@25
   675
kpeter@80
   676
    typedef typename Parent::Key argument_type;
kpeter@80
   677
    typedef typename Parent::Value result_type;
kpeter@80
   678
kpeter@80
   679
    /// Constructor
kpeter@80
   680
    MapToFunctor(const M &m) : _m(m) {}
kpeter@80
   681
    /// \e
kpeter@80
   682
    Value operator()(const Key &k) const { return _m[k]; }
kpeter@80
   683
    /// \e
kpeter@80
   684
    Value operator[](const Key &k) const { return _m[k]; }
alpar@25
   685
  };
kpeter@45
   686
kpeter@80
   687
  /// Returns a \ref MapToFunctor class
kpeter@80
   688
kpeter@80
   689
  /// This function just returns a \ref MapToFunctor class.
kpeter@80
   690
  /// \relates MapToFunctor
kpeter@45
   691
  template<typename M>
kpeter@80
   692
  inline MapToFunctor<M> mapToFunctor(const M &m) {
kpeter@80
   693
    return MapToFunctor<M>(m);
kpeter@45
   694
  }
alpar@25
   695
alpar@25
   696
kpeter@80
   697
  /// \brief Map adaptor to convert the \c Value type of a map to
kpeter@80
   698
  /// another type using the default conversion.
kpeter@80
   699
kpeter@80
   700
  /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
kpeter@80
   701
  /// "readable map" to another type using the default conversion.
kpeter@80
   702
  /// The \c Key type of it is inherited from \c M and the \c Value
kpeter@80
   703
  /// type is \c V.
kpeter@80
   704
  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
alpar@26
   705
  ///
kpeter@80
   706
  /// The simplest way of using this map is through the convertMap()
kpeter@80
   707
  /// function.
kpeter@80
   708
  template <typename M, typename V>
kpeter@80
   709
  class ConvertMap : public MapBase<typename M::Key, V> {
kpeter@80
   710
    const M &_m;
kpeter@80
   711
  public:
kpeter@80
   712
    typedef MapBase<typename M::Key, V> Parent;
kpeter@80
   713
    typedef typename Parent::Key Key;
kpeter@80
   714
    typedef typename Parent::Value Value;
kpeter@80
   715
kpeter@80
   716
    /// Constructor
kpeter@80
   717
kpeter@80
   718
    /// Constructor.
kpeter@80
   719
    /// \param m The underlying map.
kpeter@80
   720
    ConvertMap(const M &m) : _m(m) {}
kpeter@80
   721
kpeter@80
   722
    /// \e
kpeter@80
   723
    Value operator[](const Key &k) const { return _m[k]; }
kpeter@80
   724
  };
kpeter@80
   725
kpeter@80
   726
  /// Returns a \ref ConvertMap class
kpeter@80
   727
kpeter@80
   728
  /// This function just returns a \ref ConvertMap class.
kpeter@80
   729
  /// \relates ConvertMap
kpeter@80
   730
  template<typename V, typename M>
kpeter@80
   731
  inline ConvertMap<M, V> convertMap(const M &map) {
kpeter@80
   732
    return ConvertMap<M, V>(map);
kpeter@80
   733
  }
kpeter@80
   734
kpeter@80
   735
kpeter@80
   736
  /// Applies all map setting operations to two maps
kpeter@80
   737
kpeter@80
   738
  /// This map has two \ref concepts::WriteMap "writable map" parameters
kpeter@80
   739
  /// and each write request will be passed to both of them.
kpeter@80
   740
  /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
kpeter@80
   741
  /// operations will return the corresponding values of \c M1.
kpeter@29
   742
  ///
kpeter@80
   743
  /// The \c Key and \c Value types are inherited from \c M1.
kpeter@80
   744
  /// The \c Key and \c Value of \c M2 must be convertible from those
kpeter@80
   745
  /// of \c M1.
kpeter@80
   746
  ///
kpeter@80
   747
  /// The simplest way of using this map is through the forkMap()
kpeter@80
   748
  /// function.
kpeter@80
   749
  template<typename  M1, typename M2>
kpeter@80
   750
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   751
    M1 &_m1;
kpeter@80
   752
    M2 &_m2;
kpeter@80
   753
  public:
kpeter@80
   754
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
   755
    typedef typename Parent::Key Key;
kpeter@80
   756
    typedef typename Parent::Value Value;
alpar@25
   757
kpeter@80
   758
    /// Constructor
kpeter@80
   759
    ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   760
    /// Returns the value associated with the given key in the first map.
kpeter@80
   761
    Value operator[](const Key &k) const { return _m1[k]; }
kpeter@80
   762
    /// Sets the value associated with the given key in both maps.
kpeter@80
   763
    void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
kpeter@80
   764
  };
kpeter@80
   765
kpeter@80
   766
  /// Returns a \ref ForkMap class
kpeter@80
   767
kpeter@80
   768
  /// This function just returns a \ref ForkMap class.
kpeter@80
   769
  /// \relates ForkMap
kpeter@80
   770
  template <typename M1, typename M2>
kpeter@80
   771
  inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
kpeter@80
   772
    return ForkMap<M1,M2>(m1,m2);
kpeter@80
   773
  }
kpeter@80
   774
kpeter@80
   775
kpeter@80
   776
  /// Sum of two maps
kpeter@80
   777
kpeter@82
   778
  /// This \ref concepts::ReadMap "read-only map" returns the sum
kpeter@80
   779
  /// of the values of the two given maps.
kpeter@80
   780
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   781
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   782
  /// \c M1.
kpeter@80
   783
  ///
kpeter@80
   784
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   785
  /// \code
kpeter@80
   786
  ///   AddMap<M1,M2> am(m1,m2);
kpeter@80
   787
  /// \endcode
kpeter@80
   788
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
kpeter@80
   789
  ///
kpeter@80
   790
  /// The simplest way of using this map is through the addMap()
kpeter@80
   791
  /// function.
kpeter@80
   792
  ///
kpeter@80
   793
  /// \sa SubMap, MulMap, DivMap
kpeter@80
   794
  /// \sa ShiftMap, ShiftWriteMap
kpeter@80
   795
  template<typename M1, typename M2>
alpar@25
   796
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   797
    const M1 &_m1;
kpeter@80
   798
    const M2 &_m2;
alpar@25
   799
  public:
alpar@25
   800
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
alpar@25
   801
    typedef typename Parent::Key Key;
alpar@25
   802
    typedef typename Parent::Value Value;
alpar@25
   803
kpeter@80
   804
    /// Constructor
kpeter@80
   805
    AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   806
    /// \e
kpeter@80
   807
    Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
alpar@25
   808
  };
alpar@25
   809
kpeter@80
   810
  /// Returns an \ref AddMap class
kpeter@80
   811
kpeter@80
   812
  /// This function just returns an \ref AddMap class.
alpar@25
   813
  ///
kpeter@80
   814
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   815
  /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   816
  /// <tt>m1[x]+m2[x]</tt>.
kpeter@80
   817
  ///
kpeter@80
   818
  /// \relates AddMap
kpeter@80
   819
  template<typename M1, typename M2>
kpeter@80
   820
  inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
alpar@25
   821
    return AddMap<M1, M2>(m1,m2);
alpar@25
   822
  }
alpar@25
   823
alpar@25
   824
kpeter@80
   825
  /// Difference of two maps
kpeter@80
   826
kpeter@82
   827
  /// This \ref concepts::ReadMap "read-only map" returns the difference
kpeter@80
   828
  /// of the values of the two given maps.
kpeter@80
   829
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   830
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   831
  /// \c M1.
alpar@25
   832
  ///
kpeter@80
   833
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   834
  /// \code
kpeter@80
   835
  ///   SubMap<M1,M2> sm(m1,m2);
kpeter@80
   836
  /// \endcode
kpeter@80
   837
  /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
kpeter@29
   838
  ///
kpeter@80
   839
  /// The simplest way of using this map is through the subMap()
kpeter@80
   840
  /// function.
kpeter@80
   841
  ///
kpeter@80
   842
  /// \sa AddMap, MulMap, DivMap
kpeter@80
   843
  template<typename M1, typename M2>
kpeter@80
   844
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   845
    const M1 &_m1;
kpeter@80
   846
    const M2 &_m2;
kpeter@80
   847
  public:
kpeter@80
   848
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
   849
    typedef typename Parent::Key Key;
kpeter@80
   850
    typedef typename Parent::Value Value;
kpeter@80
   851
kpeter@80
   852
    /// Constructor
kpeter@80
   853
    SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   854
    /// \e
kpeter@80
   855
    Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
kpeter@80
   856
  };
kpeter@80
   857
kpeter@80
   858
  /// Returns a \ref SubMap class
kpeter@80
   859
kpeter@80
   860
  /// This function just returns a \ref SubMap class.
kpeter@80
   861
  ///
kpeter@80
   862
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   863
  /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   864
  /// <tt>m1[x]-m2[x]</tt>.
kpeter@80
   865
  ///
kpeter@80
   866
  /// \relates SubMap
kpeter@80
   867
  template<typename M1, typename M2>
kpeter@80
   868
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
kpeter@80
   869
    return SubMap<M1, M2>(m1,m2);
kpeter@80
   870
  }
kpeter@80
   871
kpeter@80
   872
kpeter@80
   873
  /// Product of two maps
kpeter@80
   874
kpeter@82
   875
  /// This \ref concepts::ReadMap "read-only map" returns the product
kpeter@80
   876
  /// of the values of the two given maps.
kpeter@80
   877
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   878
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   879
  /// \c M1.
kpeter@80
   880
  ///
kpeter@80
   881
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   882
  /// \code
kpeter@80
   883
  ///   MulMap<M1,M2> mm(m1,m2);
kpeter@80
   884
  /// \endcode
kpeter@80
   885
  /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
kpeter@80
   886
  ///
kpeter@80
   887
  /// The simplest way of using this map is through the mulMap()
kpeter@80
   888
  /// function.
kpeter@80
   889
  ///
kpeter@80
   890
  /// \sa AddMap, SubMap, DivMap
kpeter@80
   891
  /// \sa ScaleMap, ScaleWriteMap
kpeter@80
   892
  template<typename M1, typename M2>
kpeter@80
   893
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   894
    const M1 &_m1;
kpeter@80
   895
    const M2 &_m2;
kpeter@80
   896
  public:
kpeter@80
   897
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
   898
    typedef typename Parent::Key Key;
kpeter@80
   899
    typedef typename Parent::Value Value;
kpeter@80
   900
kpeter@80
   901
    /// Constructor
kpeter@80
   902
    MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   903
    /// \e
kpeter@80
   904
    Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
kpeter@80
   905
  };
kpeter@80
   906
kpeter@80
   907
  /// Returns a \ref MulMap class
kpeter@80
   908
kpeter@80
   909
  /// This function just returns a \ref MulMap class.
kpeter@80
   910
  ///
kpeter@80
   911
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   912
  /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   913
  /// <tt>m1[x]*m2[x]</tt>.
kpeter@80
   914
  ///
kpeter@80
   915
  /// \relates MulMap
kpeter@80
   916
  template<typename M1, typename M2>
kpeter@80
   917
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
kpeter@80
   918
    return MulMap<M1, M2>(m1,m2);
kpeter@80
   919
  }
kpeter@80
   920
kpeter@80
   921
kpeter@80
   922
  /// Quotient of two maps
kpeter@80
   923
kpeter@82
   924
  /// This \ref concepts::ReadMap "read-only map" returns the quotient
kpeter@80
   925
  /// of the values of the two given maps.
kpeter@80
   926
  /// Its \c Key and \c Value types are inherited from \c M1.
kpeter@80
   927
  /// The \c Key and \c Value of \c M2 must be convertible to those of
kpeter@80
   928
  /// \c M1.
kpeter@80
   929
  ///
kpeter@80
   930
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@80
   931
  /// \code
kpeter@80
   932
  ///   DivMap<M1,M2> dm(m1,m2);
kpeter@80
   933
  /// \endcode
kpeter@80
   934
  /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
kpeter@80
   935
  ///
kpeter@80
   936
  /// The simplest way of using this map is through the divMap()
kpeter@80
   937
  /// function.
kpeter@80
   938
  ///
kpeter@80
   939
  /// \sa AddMap, SubMap, MulMap
kpeter@80
   940
  template<typename M1, typename M2>
kpeter@80
   941
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
kpeter@80
   942
    const M1 &_m1;
kpeter@80
   943
    const M2 &_m2;
kpeter@80
   944
  public:
kpeter@80
   945
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
kpeter@80
   946
    typedef typename Parent::Key Key;
kpeter@80
   947
    typedef typename Parent::Value Value;
kpeter@80
   948
kpeter@80
   949
    /// Constructor
kpeter@80
   950
    DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@80
   951
    /// \e
kpeter@80
   952
    Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
kpeter@80
   953
  };
kpeter@80
   954
kpeter@80
   955
  /// Returns a \ref DivMap class
kpeter@80
   956
kpeter@80
   957
  /// This function just returns a \ref DivMap class.
kpeter@80
   958
  ///
kpeter@80
   959
  /// For example, if \c m1 and \c m2 are both maps with \c double
kpeter@80
   960
  /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
kpeter@80
   961
  /// <tt>m1[x]/m2[x]</tt>.
kpeter@80
   962
  ///
kpeter@80
   963
  /// \relates DivMap
kpeter@80
   964
  template<typename M1, typename M2>
kpeter@80
   965
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
kpeter@80
   966
    return DivMap<M1, M2>(m1,m2);
kpeter@80
   967
  }
kpeter@80
   968
kpeter@80
   969
kpeter@80
   970
  /// Shifts a map with a constant.
kpeter@80
   971
kpeter@82
   972
  /// This \ref concepts::ReadMap "read-only map" returns the sum of
kpeter@80
   973
  /// the given map and a constant value (i.e. it shifts the map with
kpeter@80
   974
  /// the constant). Its \c Key and \c Value are inherited from \c M.
kpeter@80
   975
  ///
kpeter@80
   976
  /// Actually,
kpeter@80
   977
  /// \code
kpeter@80
   978
  ///   ShiftMap<M> sh(m,v);
kpeter@80
   979
  /// \endcode
kpeter@80
   980
  /// is equivalent to
kpeter@80
   981
  /// \code
kpeter@80
   982
  ///   ConstMap<M::Key, M::Value> cm(v);
kpeter@80
   983
  ///   AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
kpeter@80
   984
  /// \endcode
kpeter@80
   985
  ///
kpeter@80
   986
  /// The simplest way of using this map is through the shiftMap()
kpeter@80
   987
  /// function.
kpeter@80
   988
  ///
kpeter@80
   989
  /// \sa ShiftWriteMap
kpeter@80
   990
  template<typename M, typename C = typename M::Value>
alpar@25
   991
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
   992
    const M &_m;
kpeter@80
   993
    C _v;
alpar@25
   994
  public:
alpar@25
   995
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
   996
    typedef typename Parent::Key Key;
alpar@25
   997
    typedef typename Parent::Value Value;
alpar@25
   998
kpeter@80
   999
    /// Constructor
alpar@25
  1000
kpeter@80
  1001
    /// Constructor.
kpeter@80
  1002
    /// \param m The undelying map.
kpeter@80
  1003
    /// \param v The constant value.
kpeter@80
  1004
    ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
kpeter@80
  1005
    /// \e
kpeter@80
  1006
    Value operator[](const Key &k) const { return _m[k]+_v; }
alpar@25
  1007
  };
alpar@25
  1008
kpeter@80
  1009
  /// Shifts a map with a constant (read-write version).
alpar@25
  1010
kpeter@80
  1011
  /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
kpeter@80
  1012
  /// of the given map and a constant value (i.e. it shifts the map with
kpeter@80
  1013
  /// the constant). Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1014
  /// It makes also possible to write the map.
alpar@25
  1015
  ///
kpeter@80
  1016
  /// The simplest way of using this map is through the shiftWriteMap()
kpeter@80
  1017
  /// function.
kpeter@80
  1018
  ///
kpeter@80
  1019
  /// \sa ShiftMap
kpeter@80
  1020
  template<typename M, typename C = typename M::Value>
alpar@25
  1021
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1022
    M &_m;
kpeter@80
  1023
    C _v;
alpar@25
  1024
  public:
alpar@25
  1025
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1026
    typedef typename Parent::Key Key;
alpar@25
  1027
    typedef typename Parent::Value Value;
alpar@25
  1028
kpeter@80
  1029
    /// Constructor
alpar@25
  1030
kpeter@80
  1031
    /// Constructor.
kpeter@80
  1032
    /// \param m The undelying map.
kpeter@80
  1033
    /// \param v The constant value.
kpeter@80
  1034
    ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
alpar@25
  1035
    /// \e
kpeter@80
  1036
    Value operator[](const Key &k) const { return _m[k]+_v; }
alpar@25
  1037
    /// \e
kpeter@80
  1038
    void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
alpar@25
  1039
  };
alpar@25
  1040
kpeter@80
  1041
  /// Returns a \ref ShiftMap class
kpeter@80
  1042
kpeter@80
  1043
  /// This function just returns a \ref ShiftMap class.
kpeter@80
  1044
  ///
kpeter@80
  1045
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1046
  /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
kpeter@80
  1047
  /// <tt>m[x]+v</tt>.
kpeter@80
  1048
  ///
kpeter@80
  1049
  /// \relates ShiftMap
kpeter@80
  1050
  template<typename M, typename C>
kpeter@80
  1051
  inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
alpar@25
  1052
    return ShiftMap<M, C>(m,v);
alpar@25
  1053
  }
alpar@25
  1054
kpeter@80
  1055
  /// Returns a \ref ShiftWriteMap class
kpeter@29
  1056
kpeter@80
  1057
  /// This function just returns a \ref ShiftWriteMap class.
kpeter@80
  1058
  ///
kpeter@80
  1059
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1060
  /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
kpeter@80
  1061
  /// <tt>m[x]+v</tt>.
kpeter@80
  1062
  /// Moreover it makes also possible to write the map.
kpeter@80
  1063
  ///
kpeter@80
  1064
  /// \relates ShiftWriteMap
kpeter@80
  1065
  template<typename M, typename C>
kpeter@80
  1066
  inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
alpar@25
  1067
    return ShiftWriteMap<M, C>(m,v);
alpar@25
  1068
  }
alpar@25
  1069
alpar@25
  1070
kpeter@80
  1071
  /// Scales a map with a constant.
kpeter@80
  1072
kpeter@82
  1073
  /// This \ref concepts::ReadMap "read-only map" returns the value of
kpeter@80
  1074
  /// the given map multiplied from the left side with a constant value.
kpeter@80
  1075
  /// Its \c Key and \c Value are inherited from \c M.
alpar@26
  1076
  ///
kpeter@80
  1077
  /// Actually,
kpeter@80
  1078
  /// \code
kpeter@80
  1079
  ///   ScaleMap<M> sc(m,v);
kpeter@80
  1080
  /// \endcode
kpeter@80
  1081
  /// is equivalent to
kpeter@80
  1082
  /// \code
kpeter@80
  1083
  ///   ConstMap<M::Key, M::Value> cm(v);
kpeter@80
  1084
  ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
kpeter@80
  1085
  /// \endcode
alpar@25
  1086
  ///
kpeter@80
  1087
  /// The simplest way of using this map is through the scaleMap()
kpeter@80
  1088
  /// function.
alpar@25
  1089
  ///
kpeter@80
  1090
  /// \sa ScaleWriteMap
kpeter@80
  1091
  template<typename M, typename C = typename M::Value>
alpar@25
  1092
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1093
    const M &_m;
kpeter@80
  1094
    C _v;
alpar@25
  1095
  public:
alpar@25
  1096
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1097
    typedef typename Parent::Key Key;
alpar@25
  1098
    typedef typename Parent::Value Value;
alpar@25
  1099
kpeter@80
  1100
    /// Constructor
alpar@25
  1101
kpeter@80
  1102
    /// Constructor.
kpeter@80
  1103
    /// \param m The undelying map.
kpeter@80
  1104
    /// \param v The constant value.
kpeter@80
  1105
    ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
alpar@25
  1106
    /// \e
kpeter@80
  1107
    Value operator[](const Key &k) const { return _v*_m[k]; }
alpar@25
  1108
  };
alpar@25
  1109
kpeter@80
  1110
  /// Scales a map with a constant (read-write version).
alpar@25
  1111
kpeter@80
  1112
  /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
kpeter@80
  1113
  /// the given map multiplied from the left side with a constant value.
kpeter@80
  1114
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1115
  /// It can also be used as write map if the \c / operator is defined
kpeter@80
  1116
  /// between \c Value and \c C and the given multiplier is not zero.
kpeter@29
  1117
  ///
kpeter@80
  1118
  /// The simplest way of using this map is through the scaleWriteMap()
kpeter@80
  1119
  /// function.
kpeter@80
  1120
  ///
kpeter@80
  1121
  /// \sa ScaleMap
kpeter@80
  1122
  template<typename M, typename C = typename M::Value>
alpar@25
  1123
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1124
    M &_m;
kpeter@80
  1125
    C _v;
alpar@25
  1126
  public:
alpar@25
  1127
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1128
    typedef typename Parent::Key Key;
alpar@25
  1129
    typedef typename Parent::Value Value;
alpar@25
  1130
kpeter@80
  1131
    /// Constructor
alpar@25
  1132
kpeter@80
  1133
    /// Constructor.
kpeter@80
  1134
    /// \param m The undelying map.
kpeter@80
  1135
    /// \param v The constant value.
kpeter@80
  1136
    ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
alpar@25
  1137
    /// \e
kpeter@80
  1138
    Value operator[](const Key &k) const { return _v*_m[k]; }
alpar@25
  1139
    /// \e
kpeter@80
  1140
    void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
alpar@25
  1141
  };
alpar@25
  1142
kpeter@80
  1143
  /// Returns a \ref ScaleMap class
kpeter@80
  1144
kpeter@80
  1145
  /// This function just returns a \ref ScaleMap class.
kpeter@80
  1146
  ///
kpeter@80
  1147
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1148
  /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
kpeter@80
  1149
  /// <tt>v*m[x]</tt>.
kpeter@80
  1150
  ///
kpeter@80
  1151
  /// \relates ScaleMap
kpeter@80
  1152
  template<typename M, typename C>
kpeter@80
  1153
  inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
alpar@25
  1154
    return ScaleMap<M, C>(m,v);
alpar@25
  1155
  }
alpar@25
  1156
kpeter@80
  1157
  /// Returns a \ref ScaleWriteMap class
kpeter@29
  1158
kpeter@80
  1159
  /// This function just returns a \ref ScaleWriteMap class.
kpeter@80
  1160
  ///
kpeter@80
  1161
  /// For example, if \c m is a map with \c double values and \c v is
kpeter@80
  1162
  /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
kpeter@80
  1163
  /// <tt>v*m[x]</tt>.
kpeter@80
  1164
  /// Moreover it makes also possible to write the map.
kpeter@80
  1165
  ///
kpeter@80
  1166
  /// \relates ScaleWriteMap
kpeter@80
  1167
  template<typename M, typename C>
kpeter@80
  1168
  inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
alpar@25
  1169
    return ScaleWriteMap<M, C>(m,v);
alpar@25
  1170
  }
alpar@25
  1171
alpar@25
  1172
kpeter@80
  1173
  /// Negative of a map
alpar@25
  1174
kpeter@82
  1175
  /// This \ref concepts::ReadMap "read-only map" returns the negative
kpeter@80
  1176
  /// of the values of the given map (using the unary \c - operator).
kpeter@80
  1177
  /// Its \c Key and \c Value are inherited from \c M.
alpar@25
  1178
  ///
kpeter@80
  1179
  /// If M::Value is \c int, \c double etc., then
kpeter@80
  1180
  /// \code
kpeter@80
  1181
  ///   NegMap<M> neg(m);
kpeter@80
  1182
  /// \endcode
kpeter@80
  1183
  /// is equivalent to
kpeter@80
  1184
  /// \code
kpeter@80
  1185
  ///   ScaleMap<M> neg(m,-1);
kpeter@80
  1186
  /// \endcode
kpeter@29
  1187
  ///
kpeter@80
  1188
  /// The simplest way of using this map is through the negMap()
kpeter@80
  1189
  /// function.
kpeter@29
  1190
  ///
kpeter@80
  1191
  /// \sa NegWriteMap
kpeter@80
  1192
  template<typename M>
alpar@25
  1193
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1194
    const M& _m;
alpar@25
  1195
  public:
alpar@25
  1196
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1197
    typedef typename Parent::Key Key;
alpar@25
  1198
    typedef typename Parent::Value Value;
alpar@25
  1199
kpeter@80
  1200
    /// Constructor
kpeter@80
  1201
    NegMap(const M &m) : _m(m) {}
alpar@25
  1202
    /// \e
kpeter@80
  1203
    Value operator[](const Key &k) const { return -_m[k]; }
alpar@25
  1204
  };
alpar@25
  1205
kpeter@80
  1206
  /// Negative of a map (read-write version)
kpeter@80
  1207
kpeter@80
  1208
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
kpeter@80
  1209
  /// negative of the values of the given map (using the unary \c -
kpeter@80
  1210
  /// operator).
kpeter@80
  1211
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1212
  /// It makes also possible to write the map.
kpeter@80
  1213
  ///
kpeter@80
  1214
  /// If M::Value is \c int, \c double etc., then
kpeter@80
  1215
  /// \code
kpeter@80
  1216
  ///   NegWriteMap<M> neg(m);
kpeter@80
  1217
  /// \endcode
kpeter@80
  1218
  /// is equivalent to
kpeter@80
  1219
  /// \code
kpeter@80
  1220
  ///   ScaleWriteMap<M> neg(m,-1);
kpeter@80
  1221
  /// \endcode
kpeter@80
  1222
  ///
kpeter@80
  1223
  /// The simplest way of using this map is through the negWriteMap()
kpeter@80
  1224
  /// function.
kpeter@29
  1225
  ///
kpeter@29
  1226
  /// \sa NegMap
kpeter@80
  1227
  template<typename M>
alpar@25
  1228
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1229
    M &_m;
alpar@25
  1230
  public:
alpar@25
  1231
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1232
    typedef typename Parent::Key Key;
alpar@25
  1233
    typedef typename Parent::Value Value;
alpar@25
  1234
kpeter@80
  1235
    /// Constructor
kpeter@80
  1236
    NegWriteMap(M &m) : _m(m) {}
alpar@25
  1237
    /// \e
kpeter@80
  1238
    Value operator[](const Key &k) const { return -_m[k]; }
alpar@25
  1239
    /// \e
kpeter@80
  1240
    void set(const Key &k, const Value &v) { _m.set(k, -v); }
alpar@25
  1241
  };
alpar@25
  1242
kpeter@80
  1243
  /// Returns a \ref NegMap class
alpar@25
  1244
kpeter@80
  1245
  /// This function just returns a \ref NegMap class.
kpeter@80
  1246
  ///
kpeter@80
  1247
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1248
  /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
kpeter@80
  1249
  ///
kpeter@80
  1250
  /// \relates NegMap
kpeter@80
  1251
  template <typename M>
alpar@25
  1252
  inline NegMap<M> negMap(const M &m) {
alpar@25
  1253
    return NegMap<M>(m);
alpar@25
  1254
  }
alpar@25
  1255
kpeter@80
  1256
  /// Returns a \ref NegWriteMap class
kpeter@29
  1257
kpeter@80
  1258
  /// This function just returns a \ref NegWriteMap class.
kpeter@80
  1259
  ///
kpeter@80
  1260
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1261
  /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
kpeter@80
  1262
  /// Moreover it makes also possible to write the map.
kpeter@80
  1263
  ///
kpeter@80
  1264
  /// \relates NegWriteMap
kpeter@80
  1265
  template <typename M>
kpeter@80
  1266
  inline NegWriteMap<M> negWriteMap(M &m) {
alpar@25
  1267
    return NegWriteMap<M>(m);
alpar@25
  1268
  }
alpar@25
  1269
alpar@25
  1270
kpeter@80
  1271
  /// Absolute value of a map
kpeter@80
  1272
kpeter@82
  1273
  /// This \ref concepts::ReadMap "read-only map" returns the absolute
kpeter@80
  1274
  /// value of the values of the given map.
kpeter@80
  1275
  /// Its \c Key and \c Value are inherited from \c M.
kpeter@80
  1276
  /// \c Value must be comparable to \c 0 and the unary \c -
kpeter@80
  1277
  /// operator must be defined for it, of course.
kpeter@80
  1278
  ///
kpeter@80
  1279
  /// The simplest way of using this map is through the absMap()
kpeter@80
  1280
  /// function.
kpeter@80
  1281
  template<typename M>
alpar@25
  1282
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
kpeter@80
  1283
    const M &_m;
alpar@25
  1284
  public:
alpar@25
  1285
    typedef MapBase<typename M::Key, typename M::Value> Parent;
alpar@25
  1286
    typedef typename Parent::Key Key;
alpar@25
  1287
    typedef typename Parent::Value Value;
alpar@25
  1288
kpeter@80
  1289
    /// Constructor
kpeter@80
  1290
    AbsMap(const M &m) : _m(m) {}
alpar@25
  1291
    /// \e
kpeter@80
  1292
    Value operator[](const Key &k) const {
kpeter@80
  1293
      Value tmp = _m[k];
alpar@25
  1294
      return tmp >= 0 ? tmp : -tmp;
alpar@25
  1295
    }
alpar@25
  1296
alpar@25
  1297
  };
alpar@25
  1298
kpeter@80
  1299
  /// Returns an \ref AbsMap class
kpeter@80
  1300
kpeter@80
  1301
  /// This function just returns an \ref AbsMap class.
kpeter@80
  1302
  ///
kpeter@80
  1303
  /// For example, if \c m is a map with \c double values, then
kpeter@80
  1304
  /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
kpeter@80
  1305
  /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
kpeter@80
  1306
  /// negative.
kpeter@80
  1307
  ///
kpeter@80
  1308
  /// \relates AbsMap
kpeter@80
  1309
  template<typename M>
alpar@25
  1310
  inline AbsMap<M> absMap(const M &m) {
alpar@25
  1311
    return AbsMap<M>(m);
alpar@25
  1312
  }
alpar@25
  1313
kpeter@82
  1314
  /// @}
kpeter@82
  1315
  
kpeter@82
  1316
  // Logical maps and map adaptors:
kpeter@82
  1317
kpeter@82
  1318
  /// \addtogroup maps
kpeter@82
  1319
  /// @{
kpeter@82
  1320
kpeter@82
  1321
  /// Constant \c true map.
kpeter@82
  1322
kpeter@82
  1323
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
kpeter@82
  1324
  /// each key.
kpeter@82
  1325
  ///
kpeter@82
  1326
  /// Note that
kpeter@82
  1327
  /// \code
kpeter@82
  1328
  ///   TrueMap<K> tm;
kpeter@82
  1329
  /// \endcode
kpeter@82
  1330
  /// is equivalent to
kpeter@82
  1331
  /// \code
kpeter@82
  1332
  ///   ConstMap<K,bool> tm(true);
kpeter@82
  1333
  /// \endcode
kpeter@82
  1334
  ///
kpeter@82
  1335
  /// \sa FalseMap
kpeter@82
  1336
  /// \sa ConstMap
kpeter@82
  1337
  template <typename K>
kpeter@82
  1338
  class TrueMap : public MapBase<K, bool> {
kpeter@82
  1339
  public:
kpeter@82
  1340
    typedef MapBase<K, bool> Parent;
kpeter@82
  1341
    typedef typename Parent::Key Key;
kpeter@82
  1342
    typedef typename Parent::Value Value;
kpeter@82
  1343
kpeter@82
  1344
    /// Gives back \c true.
kpeter@82
  1345
    Value operator[](const Key&) const { return true; }
kpeter@82
  1346
  };
kpeter@82
  1347
kpeter@82
  1348
  /// Returns a \ref TrueMap class
kpeter@82
  1349
kpeter@82
  1350
  /// This function just returns a \ref TrueMap class.
kpeter@82
  1351
  /// \relates TrueMap
kpeter@82
  1352
  template<typename K>
kpeter@82
  1353
  inline TrueMap<K> trueMap() {
kpeter@82
  1354
    return TrueMap<K>();
kpeter@82
  1355
  }
kpeter@82
  1356
kpeter@82
  1357
kpeter@82
  1358
  /// Constant \c false map.
kpeter@82
  1359
kpeter@82
  1360
  /// This \ref concepts::ReadMap "read-only map" assigns \c false to
kpeter@82
  1361
  /// each key.
kpeter@82
  1362
  ///
kpeter@82
  1363
  /// Note that
kpeter@82
  1364
  /// \code
kpeter@82
  1365
  ///   FalseMap<K> fm;
kpeter@82
  1366
  /// \endcode
kpeter@82
  1367
  /// is equivalent to
kpeter@82
  1368
  /// \code
kpeter@82
  1369
  ///   ConstMap<K,bool> fm(false);
kpeter@82
  1370
  /// \endcode
kpeter@82
  1371
  ///
kpeter@82
  1372
  /// \sa TrueMap
kpeter@82
  1373
  /// \sa ConstMap
kpeter@82
  1374
  template <typename K>
kpeter@82
  1375
  class FalseMap : public MapBase<K, bool> {
kpeter@82
  1376
  public:
kpeter@82
  1377
    typedef MapBase<K, bool> Parent;
kpeter@82
  1378
    typedef typename Parent::Key Key;
kpeter@82
  1379
    typedef typename Parent::Value Value;
kpeter@82
  1380
kpeter@82
  1381
    /// Gives back \c false.
kpeter@82
  1382
    Value operator[](const Key&) const { return false; }
kpeter@82
  1383
  };
kpeter@82
  1384
kpeter@82
  1385
  /// Returns a \ref FalseMap class
kpeter@82
  1386
kpeter@82
  1387
  /// This function just returns a \ref FalseMap class.
kpeter@82
  1388
  /// \relates FalseMap
kpeter@82
  1389
  template<typename K>
kpeter@82
  1390
  inline FalseMap<K> falseMap() {
kpeter@82
  1391
    return FalseMap<K>();
kpeter@82
  1392
  }
kpeter@82
  1393
kpeter@82
  1394
  /// @}
kpeter@82
  1395
kpeter@82
  1396
  /// \addtogroup map_adaptors
kpeter@82
  1397
  /// @{
kpeter@82
  1398
kpeter@82
  1399
  /// Logical 'and' of two maps
kpeter@82
  1400
kpeter@82
  1401
  /// This \ref concepts::ReadMap "read-only map" returns the logical
kpeter@82
  1402
  /// 'and' of the values of the two given maps.
kpeter@82
  1403
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1404
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1405
  ///
kpeter@82
  1406
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1407
  /// \code
kpeter@82
  1408
  ///   AndMap<M1,M2> am(m1,m2);
kpeter@82
  1409
  /// \endcode
kpeter@82
  1410
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
kpeter@82
  1411
  ///
kpeter@82
  1412
  /// The simplest way of using this map is through the andMap()
kpeter@82
  1413
  /// function.
kpeter@82
  1414
  ///
kpeter@82
  1415
  /// \sa OrMap
kpeter@82
  1416
  /// \sa NotMap, NotWriteMap
kpeter@82
  1417
  template<typename M1, typename M2>
kpeter@82
  1418
  class AndMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1419
    const M1 &_m1;
kpeter@82
  1420
    const M2 &_m2;
kpeter@82
  1421
  public:
kpeter@82
  1422
    typedef MapBase<typename M1::Key, bool> Parent;
kpeter@82
  1423
    typedef typename Parent::Key Key;
kpeter@82
  1424
    typedef typename Parent::Value Value;
kpeter@82
  1425
kpeter@82
  1426
    /// Constructor
kpeter@82
  1427
    AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@82
  1428
    /// \e
kpeter@82
  1429
    Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
kpeter@82
  1430
  };
kpeter@82
  1431
kpeter@82
  1432
  /// Returns an \ref AndMap class
kpeter@82
  1433
kpeter@82
  1434
  /// This function just returns an \ref AndMap class.
kpeter@82
  1435
  ///
kpeter@82
  1436
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
kpeter@82
  1437
  /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1438
  /// <tt>m1[x]&&m2[x]</tt>.
kpeter@82
  1439
  ///
kpeter@82
  1440
  /// \relates AndMap
kpeter@82
  1441
  template<typename M1, typename M2>
kpeter@82
  1442
  inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1443
    return AndMap<M1, M2>(m1,m2);
kpeter@82
  1444
  }
kpeter@82
  1445
kpeter@82
  1446
kpeter@82
  1447
  /// Logical 'or' of two maps
kpeter@82
  1448
kpeter@82
  1449
  /// This \ref concepts::ReadMap "read-only map" returns the logical
kpeter@82
  1450
  /// 'or' of the values of the two given maps.
kpeter@82
  1451
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1452
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1453
  ///
kpeter@82
  1454
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1455
  /// \code
kpeter@82
  1456
  ///   OrMap<M1,M2> om(m1,m2);
kpeter@82
  1457
  /// \endcode
kpeter@82
  1458
  /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
kpeter@82
  1459
  ///
kpeter@82
  1460
  /// The simplest way of using this map is through the orMap()
kpeter@82
  1461
  /// function.
kpeter@82
  1462
  ///
kpeter@82
  1463
  /// \sa AndMap
kpeter@82
  1464
  /// \sa NotMap, NotWriteMap
kpeter@82
  1465
  template<typename M1, typename M2>
kpeter@82
  1466
  class OrMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1467
    const M1 &_m1;
kpeter@82
  1468
    const M2 &_m2;
kpeter@82
  1469
  public:
kpeter@82
  1470
    typedef MapBase<typename M1::Key, bool> Parent;
kpeter@82
  1471
    typedef typename Parent::Key Key;
kpeter@82
  1472
    typedef typename Parent::Value Value;
kpeter@82
  1473
kpeter@82
  1474
    /// Constructor
kpeter@82
  1475
    OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@82
  1476
    /// \e
kpeter@82
  1477
    Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
kpeter@82
  1478
  };
kpeter@82
  1479
kpeter@82
  1480
  /// Returns an \ref OrMap class
kpeter@82
  1481
kpeter@82
  1482
  /// This function just returns an \ref OrMap class.
kpeter@82
  1483
  ///
kpeter@82
  1484
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
kpeter@82
  1485
  /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1486
  /// <tt>m1[x]||m2[x]</tt>.
kpeter@82
  1487
  ///
kpeter@82
  1488
  /// \relates OrMap
kpeter@82
  1489
  template<typename M1, typename M2>
kpeter@82
  1490
  inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1491
    return OrMap<M1, M2>(m1,m2);
kpeter@82
  1492
  }
kpeter@82
  1493
alpar@25
  1494
kpeter@80
  1495
  /// Logical 'not' of a map
kpeter@80
  1496
kpeter@82
  1497
  /// This \ref concepts::ReadMap "read-only map" returns the logical
kpeter@80
  1498
  /// negation of the values of the given map.
kpeter@80
  1499
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
alpar@25
  1500
  ///
kpeter@80
  1501
  /// The simplest way of using this map is through the notMap()
kpeter@80
  1502
  /// function.
alpar@25
  1503
  ///
kpeter@80
  1504
  /// \sa NotWriteMap
kpeter@80
  1505
  template <typename M>
alpar@25
  1506
  class NotMap : public MapBase<typename M::Key, bool> {
kpeter@80
  1507
    const M &_m;
alpar@25
  1508
  public:
alpar@25
  1509
    typedef MapBase<typename M::Key, bool> Parent;
alpar@25
  1510
    typedef typename Parent::Key Key;
alpar@25
  1511
    typedef typename Parent::Value Value;
alpar@25
  1512
alpar@25
  1513
    /// Constructor
kpeter@80
  1514
    NotMap(const M &m) : _m(m) {}
kpeter@80
  1515
    /// \e
kpeter@80
  1516
    Value operator[](const Key &k) const { return !_m[k]; }
alpar@25
  1517
  };
alpar@25
  1518
kpeter@80
  1519
  /// Logical 'not' of a map (read-write version)
kpeter@80
  1520
kpeter@80
  1521
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
kpeter@80
  1522
  /// logical negation of the values of the given map.
kpeter@80
  1523
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
kpeter@80
  1524
  /// It makes also possible to write the map. When a value is set,
kpeter@80
  1525
  /// the opposite value is set to the original map.
kpeter@29
  1526
  ///
kpeter@80
  1527
  /// The simplest way of using this map is through the notWriteMap()
kpeter@80
  1528
  /// function.
kpeter@80
  1529
  ///
kpeter@80
  1530
  /// \sa NotMap
kpeter@80
  1531
  template <typename M>
alpar@25
  1532
  class NotWriteMap : public MapBase<typename M::Key, bool> {
kpeter@80
  1533
    M &_m;
alpar@25
  1534
  public:
alpar@25
  1535
    typedef MapBase<typename M::Key, bool> Parent;
alpar@25
  1536
    typedef typename Parent::Key Key;
alpar@25
  1537
    typedef typename Parent::Value Value;
alpar@25
  1538
alpar@25
  1539
    /// Constructor
kpeter@80
  1540
    NotWriteMap(M &m) : _m(m) {}
kpeter@80
  1541
    /// \e
kpeter@80
  1542
    Value operator[](const Key &k) const { return !_m[k]; }
kpeter@80
  1543
    /// \e
kpeter@80
  1544
    void set(const Key &k, bool v) { _m.set(k, !v); }
alpar@25
  1545
  };
kpeter@80
  1546
kpeter@80
  1547
  /// Returns a \ref NotMap class
kpeter@80
  1548
kpeter@80
  1549
  /// This function just returns a \ref NotMap class.
kpeter@80
  1550
  ///
kpeter@80
  1551
  /// For example, if \c m is a map with \c bool values, then
kpeter@80
  1552
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
kpeter@80
  1553
  ///
kpeter@80
  1554
  /// \relates NotMap
kpeter@80
  1555
  template <typename M>
alpar@25
  1556
  inline NotMap<M> notMap(const M &m) {
alpar@25
  1557
    return NotMap<M>(m);
alpar@25
  1558
  }
kpeter@80
  1559
kpeter@80
  1560
  /// Returns a \ref NotWriteMap class
kpeter@80
  1561
kpeter@80
  1562
  /// This function just returns a \ref NotWriteMap class.
kpeter@80
  1563
  ///
kpeter@80
  1564
  /// For example, if \c m is a map with \c bool values, then
kpeter@80
  1565
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
kpeter@80
  1566
  /// Moreover it makes also possible to write the map.
kpeter@80
  1567
  ///
kpeter@80
  1568
  /// \relates NotWriteMap
kpeter@80
  1569
  template <typename M>
kpeter@80
  1570
  inline NotWriteMap<M> notWriteMap(M &m) {
alpar@25
  1571
    return NotWriteMap<M>(m);
alpar@25
  1572
  }
alpar@25
  1573
kpeter@82
  1574
kpeter@82
  1575
  /// Combination of two maps using the \c == operator
kpeter@82
  1576
kpeter@82
  1577
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
kpeter@82
  1578
  /// the keys for which the corresponding values of the two maps are
kpeter@82
  1579
  /// equal.
kpeter@82
  1580
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1581
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1582
  ///
kpeter@82
  1583
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1584
  /// \code
kpeter@82
  1585
  ///   EqualMap<M1,M2> em(m1,m2);
kpeter@82
  1586
  /// \endcode
kpeter@82
  1587
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
kpeter@82
  1588
  ///
kpeter@82
  1589
  /// The simplest way of using this map is through the equalMap()
kpeter@82
  1590
  /// function.
kpeter@82
  1591
  ///
kpeter@82
  1592
  /// \sa LessMap
kpeter@82
  1593
  template<typename M1, typename M2>
kpeter@82
  1594
  class EqualMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1595
    const M1 &_m1;
kpeter@82
  1596
    const M2 &_m2;
kpeter@82
  1597
  public:
kpeter@82
  1598
    typedef MapBase<typename M1::Key, bool> Parent;
kpeter@82
  1599
    typedef typename Parent::Key Key;
kpeter@82
  1600
    typedef typename Parent::Value Value;
kpeter@82
  1601
kpeter@82
  1602
    /// Constructor
kpeter@82
  1603
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@82
  1604
    /// \e
kpeter@82
  1605
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
kpeter@82
  1606
  };
kpeter@82
  1607
kpeter@82
  1608
  /// Returns an \ref EqualMap class
kpeter@82
  1609
kpeter@82
  1610
  /// This function just returns an \ref EqualMap class.
kpeter@82
  1611
  ///
kpeter@82
  1612
  /// For example, if \c m1 and \c m2 are maps with keys and values of
kpeter@82
  1613
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1614
  /// <tt>m1[x]==m2[x]</tt>.
kpeter@82
  1615
  ///
kpeter@82
  1616
  /// \relates EqualMap
kpeter@82
  1617
  template<typename M1, typename M2>
kpeter@82
  1618
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1619
    return EqualMap<M1, M2>(m1,m2);
kpeter@82
  1620
  }
kpeter@82
  1621
kpeter@82
  1622
kpeter@82
  1623
  /// Combination of two maps using the \c < operator
kpeter@82
  1624
kpeter@82
  1625
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
kpeter@82
  1626
  /// the keys for which the corresponding value of the first map is
kpeter@82
  1627
  /// less then the value of the second map.
kpeter@82
  1628
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
kpeter@82
  1629
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
kpeter@82
  1630
  ///
kpeter@82
  1631
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
kpeter@82
  1632
  /// \code
kpeter@82
  1633
  ///   LessMap<M1,M2> lm(m1,m2);
kpeter@82
  1634
  /// \endcode
kpeter@82
  1635
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
kpeter@82
  1636
  ///
kpeter@82
  1637
  /// The simplest way of using this map is through the lessMap()
kpeter@82
  1638
  /// function.
kpeter@82
  1639
  ///
kpeter@82
  1640
  /// \sa EqualMap
kpeter@82
  1641
  template<typename M1, typename M2>
kpeter@82
  1642
  class LessMap : public MapBase<typename M1::Key, bool> {
kpeter@82
  1643
    const M1 &_m1;
kpeter@82
  1644
    const M2 &_m2;
kpeter@82
  1645
  public:
kpeter@82
  1646
    typedef MapBase<typename M1::Key, bool> Parent;
kpeter@82
  1647
    typedef typename Parent::Key Key;
kpeter@82
  1648
    typedef typename Parent::Value Value;
kpeter@82
  1649
kpeter@82
  1650
    /// Constructor
kpeter@82
  1651
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
kpeter@82
  1652
    /// \e
kpeter@82
  1653
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
kpeter@82
  1654
  };
kpeter@82
  1655
kpeter@82
  1656
  /// Returns an \ref LessMap class
kpeter@82
  1657
kpeter@82
  1658
  /// This function just returns an \ref LessMap class.
kpeter@82
  1659
  ///
kpeter@82
  1660
  /// For example, if \c m1 and \c m2 are maps with keys and values of
kpeter@82
  1661
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
kpeter@82
  1662
  /// <tt>m1[x]<m2[x]</tt>.
kpeter@82
  1663
  ///
kpeter@82
  1664
  /// \relates LessMap
kpeter@82
  1665
  template<typename M1, typename M2>
kpeter@82
  1666
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
kpeter@82
  1667
    return LessMap<M1, M2>(m1,m2);
kpeter@82
  1668
  }
kpeter@82
  1669
alpar@104
  1670
  namespace _maps_bits {
alpar@104
  1671
alpar@104
  1672
    template <typename Value>
alpar@104
  1673
    struct Identity {
alpar@104
  1674
      typedef Value argument_type;
alpar@104
  1675
      typedef Value result_type;
alpar@104
  1676
      Value operator()(const Value& val) const {
alpar@104
  1677
	return val;
alpar@104
  1678
      }
alpar@104
  1679
    };
alpar@104
  1680
alpar@104
  1681
    template <typename _Iterator, typename Enable = void>
alpar@104
  1682
    struct IteratorTraits {
alpar@104
  1683
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
alpar@104
  1684
    };
alpar@104
  1685
alpar@104
  1686
    template <typename _Iterator>
alpar@104
  1687
    struct IteratorTraits<_Iterator,
alpar@104
  1688
      typename exists<typename _Iterator::container_type>::type>
alpar@104
  1689
    {
alpar@104
  1690
      typedef typename _Iterator::container_type::value_type Value;
alpar@104
  1691
    };
alpar@104
  1692
alpar@104
  1693
  }
alpar@104
  1694
alpar@104
  1695
  /// \brief Writable bool map for logging each \c true assigned element
alpar@104
  1696
  ///
alpar@104
  1697
  /// A \ref concepts::ReadWriteMap "read-write" bool map for logging
alpar@104
  1698
  /// each \c true assigned element, i.e it copies subsequently each
alpar@104
  1699
  /// keys set to \c true to the given iterator.
alpar@104
  1700
  ///
alpar@104
  1701
  /// \tparam It the type of the Iterator.
alpar@104
  1702
  /// \tparam Ke the type of the map's Key. The default value should
alpar@104
  1703
  /// work in most cases.
alpar@104
  1704
  ///
alpar@104
  1705
  /// \note The container of the iterator must contain enough space
alpar@104
  1706
  /// for the elements. (Or it should be an inserter iterator).
alpar@104
  1707
  ///
alpar@104
  1708
  /// \todo Revise the name of this class and give an example code.
alpar@104
  1709
  template <typename It,
alpar@104
  1710
	    typename Ke=typename _maps_bits::IteratorTraits<It>::Value>
alpar@104
  1711
  class StoreBoolMap {
alpar@104
  1712
  public:
alpar@104
  1713
    typedef It Iterator;
alpar@104
  1714
alpar@104
  1715
    typedef Ke Key;
alpar@104
  1716
    typedef bool Value;
alpar@104
  1717
alpar@104
  1718
    /// Constructor
alpar@104
  1719
    StoreBoolMap(Iterator it)
alpar@104
  1720
      : _begin(it), _end(it) {}
alpar@104
  1721
alpar@104
  1722
    /// Gives back the given iterator set for the first key
alpar@104
  1723
    Iterator begin() const {
alpar@104
  1724
      return _begin;
alpar@104
  1725
    }
alpar@104
  1726
alpar@104
  1727
    /// Gives back the the 'after the last' iterator
alpar@104
  1728
    Iterator end() const {
alpar@104
  1729
      return _end;
alpar@104
  1730
    }
alpar@104
  1731
alpar@104
  1732
    /// The set function of the map
alpar@104
  1733
    void set(const Key& key, Value value) const {
alpar@104
  1734
      if (value) {
alpar@104
  1735
	*_end++ = key;
alpar@104
  1736
      }
alpar@104
  1737
    }
alpar@104
  1738
alpar@104
  1739
  private:
alpar@104
  1740
    Iterator _begin;
alpar@104
  1741
    mutable Iterator _end;
alpar@104
  1742
  };
alpar@104
  1743
alpar@25
  1744
  /// @}
alpar@25
  1745
}
alpar@25
  1746
alpar@25
  1747
#endif // LEMON_MAPS_H