[286] | 1 | // -*- C++ -*- // |
---|
| 2 | #ifndef HUGO_MAPS_H |
---|
| 3 | #define HUGO_MAPS_H |
---|
| 4 | |
---|
| 5 | ///\file |
---|
| 6 | ///\brief Miscellaneous property maps |
---|
| 7 | /// |
---|
| 8 | ///\todo This file has the same name as the concept file in skeletons, |
---|
| 9 | /// and this is not easily detectable in docs... |
---|
| 10 | |
---|
| 11 | #include <map> |
---|
| 12 | |
---|
| 13 | namespace hugo { |
---|
| 14 | |
---|
[720] | 15 | /// Base class of maps. |
---|
| 16 | |
---|
[805] | 17 | /// Base class of maps. |
---|
| 18 | /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
---|
[720] | 19 | template<typename K, typename T> |
---|
| 20 | class MapBase |
---|
| 21 | { |
---|
| 22 | public: |
---|
[805] | 23 | /// . |
---|
[720] | 24 | typedef K KeyType; |
---|
[805] | 25 | /// . |
---|
[720] | 26 | typedef T ValueType; |
---|
| 27 | }; |
---|
| 28 | |
---|
[805] | 29 | /// Null map. (a.k.a. DoNothingMap) |
---|
[286] | 30 | |
---|
| 31 | /// If you have to provide a map only for its type definitions, |
---|
[805] | 32 | /// or if you have to provide a writable map, but |
---|
| 33 | /// data written to it will sent to <tt>/dev/null</tt>... |
---|
[286] | 34 | template<typename K, typename T> |
---|
[720] | 35 | class NullMap : public MapBase<K,T> |
---|
[286] | 36 | { |
---|
| 37 | public: |
---|
| 38 | |
---|
[805] | 39 | /// Gives back a default constructed element. |
---|
[286] | 40 | T operator[](const K&) const { return T(); } |
---|
[805] | 41 | /// Absorbs the value. |
---|
[286] | 42 | void set(const K&, const T&) {} |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | /// Constant map. |
---|
| 47 | |
---|
[805] | 48 | /// This is a readable map which assigns a specified value to each key. |
---|
| 49 | /// In other aspects it is equivalent to the \ref NullMap. |
---|
| 50 | /// \todo set could be used to set the value. |
---|
[286] | 51 | template<typename K, typename T> |
---|
[720] | 52 | class ConstMap : public MapBase<K,T> |
---|
[286] | 53 | { |
---|
| 54 | T v; |
---|
| 55 | public: |
---|
| 56 | |
---|
[805] | 57 | /// Default constructor |
---|
| 58 | |
---|
| 59 | /// The value of the map will be uninitialized. |
---|
| 60 | /// (More exactly it will be default constructed.) |
---|
[286] | 61 | ConstMap() {} |
---|
[805] | 62 | /// . |
---|
| 63 | |
---|
| 64 | /// \param _v The initial value of the map. |
---|
[286] | 65 | ConstMap(const T &_v) : v(_v) {} |
---|
| 66 | |
---|
| 67 | T operator[](const K&) const { return v; } |
---|
| 68 | void set(const K&, const T&) {} |
---|
| 69 | |
---|
| 70 | template<typename T1> |
---|
| 71 | struct rebind { |
---|
| 72 | typedef ConstMap<K,T1> other; |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | template<typename T1> |
---|
| 76 | ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {} |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | /// \c std::map wrapper |
---|
| 82 | |
---|
| 83 | /// This is essentially a wrapper for \c std::map. With addition that |
---|
| 84 | /// you can specify a default value different from \c ValueType() . |
---|
| 85 | /// |
---|
| 86 | /// \todo Provide allocator parameter... |
---|
| 87 | template <typename Key, typename T, typename Compare = std::less<Key> > |
---|
| 88 | class StdMap : public std::map<Key,T,Compare> { |
---|
| 89 | typedef std::map<Key,T,Compare> parent; |
---|
| 90 | T v; |
---|
| 91 | typedef typename parent::value_type PairType; |
---|
| 92 | |
---|
| 93 | public: |
---|
| 94 | typedef Key KeyType; |
---|
| 95 | typedef T ValueType; |
---|
| 96 | typedef T& ReferenceType; |
---|
| 97 | typedef const T& ConstReferenceType; |
---|
| 98 | |
---|
| 99 | |
---|
[345] | 100 | StdMap() : v() {} |
---|
[286] | 101 | /// Constructor with specified default value |
---|
| 102 | StdMap(const T& _v) : v(_v) {} |
---|
| 103 | |
---|
| 104 | /// \brief Constructs the map from an appropriate std::map. |
---|
| 105 | /// |
---|
| 106 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 107 | StdMap(const parent &m) : parent(m) {} |
---|
| 108 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
| 109 | /// specifies a default value. |
---|
| 110 | /// |
---|
| 111 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 112 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
| 113 | |
---|
| 114 | template<typename T1, typename Comp1> |
---|
[389] | 115 | StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { |
---|
| 116 | //FIXME; |
---|
| 117 | } |
---|
[286] | 118 | |
---|
| 119 | ReferenceType operator[](const Key &k) { |
---|
[346] | 120 | return insert(PairType(k,v)).first -> second; |
---|
[286] | 121 | } |
---|
| 122 | ConstReferenceType operator[](const Key &k) const { |
---|
[389] | 123 | typename parent::iterator i = lower_bound(k); |
---|
[391] | 124 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
[286] | 125 | return v; |
---|
| 126 | return (*i).second; |
---|
| 127 | } |
---|
[345] | 128 | void set(const Key &k, const T &t) { |
---|
[346] | 129 | parent::operator[](k) = t; |
---|
[345] | 130 | } |
---|
[286] | 131 | |
---|
| 132 | /// Changes the default value of the map. |
---|
| 133 | /// \return Returns the previous default value. |
---|
| 134 | /// |
---|
[805] | 135 | /// \warning The value of some keys (which has already been queried, but |
---|
[286] | 136 | /// the value has been unchanged from the default) may change! |
---|
| 137 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
| 138 | |
---|
| 139 | template<typename T1> |
---|
| 140 | struct rebind { |
---|
| 141 | typedef StdMap<Key,T1,Compare> other; |
---|
| 142 | }; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | #endif // HUGO_MAPS_H |
---|