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