| 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 |  | 
|---|
| 15 |   /// Base class of maps. | 
|---|
| 16 |  | 
|---|
| 17 |   /// Base class of maps. | 
|---|
| 18 |   /// It provides the necessary <tt>typedef</tt>s required by the map concept. | 
|---|
| 19 |   template<typename K, typename T> | 
|---|
| 20 |   class MapBase | 
|---|
| 21 |   { | 
|---|
| 22 |   public: | 
|---|
| 23 |     /// . | 
|---|
| 24 |     typedef K KeyType; | 
|---|
| 25 |     /// . | 
|---|
| 26 |     typedef T ValueType; | 
|---|
| 27 |   }; | 
|---|
| 28 |  | 
|---|
| 29 |   /// Null map. (a.k.a. DoNothingMap) | 
|---|
| 30 |  | 
|---|
| 31 |   /// If you have to provide a map only for its type definitions, | 
|---|
| 32 |   /// or if you have to provide a writable map, but | 
|---|
| 33 |   /// data written to it will sent to <tt>/dev/null</tt>... | 
|---|
| 34 |   template<typename K, typename T> | 
|---|
| 35 |   class NullMap : public MapBase<K,T> | 
|---|
| 36 |   { | 
|---|
| 37 |   public: | 
|---|
| 38 |  | 
|---|
| 39 |     /// Gives back a default constructed element. | 
|---|
| 40 |     T operator[](const K&) const { return T(); } | 
|---|
| 41 |     /// Absorbs the value. | 
|---|
| 42 |     void set(const K&, const T&) {} | 
|---|
| 43 |   }; | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 |   /// Constant map. | 
|---|
| 47 |  | 
|---|
| 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. | 
|---|
| 51 |   template<typename K, typename T> | 
|---|
| 52 |   class ConstMap : public MapBase<K,T> | 
|---|
| 53 |   { | 
|---|
| 54 |     T v; | 
|---|
| 55 |   public: | 
|---|
| 56 |  | 
|---|
| 57 |     /// Default constructor | 
|---|
| 58 |  | 
|---|
| 59 |     /// The value of the map will be uninitialized.  | 
|---|
| 60 |     /// (More exactly it will be default constructed.) | 
|---|
| 61 |     ConstMap() {} | 
|---|
| 62 |     /// . | 
|---|
| 63 |  | 
|---|
| 64 |     /// \param _v The initial value of the map. | 
|---|
| 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 |  | 
|---|
| 100 |     StdMap() : v() {} | 
|---|
| 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> | 
|---|
| 115 |     StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {  | 
|---|
| 116 |       //FIXME;  | 
|---|
| 117 |     } | 
|---|
| 118 |  | 
|---|
| 119 |     ReferenceType operator[](const Key &k) { | 
|---|
| 120 |       return insert(PairType(k,v)).first -> second; | 
|---|
| 121 |     } | 
|---|
| 122 |     ConstReferenceType operator[](const Key &k) const { | 
|---|
| 123 |       typename parent::iterator i = lower_bound(k); | 
|---|
| 124 |       if (i == parent::end() || parent::key_comp()(k, (*i).first)) | 
|---|
| 125 |         return v; | 
|---|
| 126 |       return (*i).second; | 
|---|
| 127 |     } | 
|---|
| 128 |     void set(const Key &k, const T &t) { | 
|---|
| 129 |       parent::operator[](k) = t; | 
|---|
| 130 |     } | 
|---|
| 131 |  | 
|---|
| 132 |     /// Changes the default value of the map. | 
|---|
| 133 |     /// \return Returns the previous default value. | 
|---|
| 134 |     /// | 
|---|
| 135 |     /// \warning The value of some keys (which has already been queried, but | 
|---|
| 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 | 
|---|