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