COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/maps.h @ 2595:1cbd377bffb3

Last change on this file since 2595:1cbd377bffb3 was 2564:3250756f5add, checked in by Peter Kovacs, 16 years ago

Several doc improvements and fixes in maps.h and concepts/maps.h.

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