| 1 | // -*- c++ -*- |
|---|
| 2 | #ifndef VECTOR_MAP_H |
|---|
| 3 | #define VECTOR_MAP_H |
|---|
| 4 | |
|---|
| 5 | #include <vector> |
|---|
| 6 | |
|---|
| 7 | #include <hugo/map_iterator.h> |
|---|
| 8 | #include <hugo/map_bits.h> |
|---|
| 9 | |
|---|
| 10 | ///\ingroup graphmaps |
|---|
| 11 | ///\file |
|---|
| 12 | ///\brief Vector based graph maps. |
|---|
| 13 | |
|---|
| 14 | namespace hugo { |
|---|
| 15 | |
|---|
| 16 | /// \addtogroup graphmaps |
|---|
| 17 | /// @{ |
|---|
| 18 | |
|---|
| 19 | /** The ArrayMap template class is graph map structure what |
|---|
| 20 | * automatically updates the map when a key is added to or erased from |
|---|
| 21 | * the map. This map factory uses the allocators to implement |
|---|
| 22 | * the container functionality. This map factory |
|---|
| 23 | * uses the std::vector to implement the container function. |
|---|
| 24 | * |
|---|
| 25 | * The template parameter is the MapRegistry that the maps |
|---|
| 26 | * will belong to and the ValueType. |
|---|
| 27 | * |
|---|
| 28 | * \todo It should use a faster initialization using the maxNodeId() or |
|---|
| 29 | * maxEdgeId() function of the graph instead of iterating through each |
|---|
| 30 | * edge/node. |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | template <typename MapRegistry, typename Value> |
|---|
| 34 | class VectorMap : public MapRegistry::MapBase { |
|---|
| 35 | template <typename MR, typename T> friend class VectorMap; |
|---|
| 36 | public: |
|---|
| 37 | |
|---|
| 38 | /// The graph type of the maps. |
|---|
| 39 | typedef typename MapRegistry::Graph Graph; |
|---|
| 40 | /// The key type of the maps. |
|---|
| 41 | typedef typename MapRegistry::KeyType KeyType; |
|---|
| 42 | /// The iterator to iterate on the keys. |
|---|
| 43 | typedef typename MapRegistry::KeyIt KeyIt; |
|---|
| 44 | |
|---|
| 45 | /// The map type. |
|---|
| 46 | typedef VectorMap Map; |
|---|
| 47 | /// The MapBase of the Map which implements the core regisitry function. |
|---|
| 48 | typedef typename MapRegistry::MapBase MapBase; |
|---|
| 49 | |
|---|
| 50 | private: |
|---|
| 51 | |
|---|
| 52 | /// The container type of the map. |
|---|
| 53 | typedef std::vector<Value> Container; |
|---|
| 54 | |
|---|
| 55 | public: |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /// The value type of the map. |
|---|
| 59 | typedef Value ValueType; |
|---|
| 60 | /// The reference type of the map; |
|---|
| 61 | typedef typename Container::reference ReferenceType; |
|---|
| 62 | /// The pointer type of the map; |
|---|
| 63 | typedef typename Container::pointer PointerType; |
|---|
| 64 | |
|---|
| 65 | /// The const value type of the map. |
|---|
| 66 | typedef const Value ConstValueType; |
|---|
| 67 | /// The const reference type of the map; |
|---|
| 68 | typedef typename Container::const_reference ConstReferenceType; |
|---|
| 69 | /// The pointer type of the map; |
|---|
| 70 | typedef typename Container::const_pointer ConstPointerType; |
|---|
| 71 | |
|---|
| 72 | /** Default constructor for the map. |
|---|
| 73 | */ |
|---|
| 74 | VectorMap() {} |
|---|
| 75 | |
|---|
| 76 | /** Graph and Registry initialized map constructor. |
|---|
| 77 | */ |
|---|
| 78 | VectorMap(const Graph& g, MapRegistry& r) |
|---|
| 79 | : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {} |
|---|
| 80 | |
|---|
| 81 | /** Constructor to use default value to initialize the map. |
|---|
| 82 | */ |
|---|
| 83 | VectorMap(const Graph& g, MapRegistry& r, const Value& v) |
|---|
| 84 | : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {} |
|---|
| 85 | |
|---|
| 86 | /** Assign operator to copy a map of an other map type. |
|---|
| 87 | */ |
|---|
| 88 | template <typename TT> |
|---|
| 89 | VectorMap(const VectorMap<MapRegistry, TT>& c) |
|---|
| 90 | : MapBase(c), container(c.container.size()) { |
|---|
| 91 | for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
|---|
| 92 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
|---|
| 93 | container[id] = c.container[id]; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** Assign operator to copy a map of an other map type. |
|---|
| 98 | */ |
|---|
| 99 | template <typename TT> |
|---|
| 100 | VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) { |
|---|
| 101 | container.resize(c.container.size()); |
|---|
| 102 | MapBase::operator=(c); |
|---|
| 103 | for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
|---|
| 104 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
|---|
| 105 | container[id] = c.container[id]; |
|---|
| 106 | } |
|---|
| 107 | return *this; |
|---|
| 108 | } |
|---|
| 109 | /** |
|---|
| 110 | * The subscript operator. The map can be subscripted by the |
|---|
| 111 | * actual keys of the graph. |
|---|
| 112 | */ |
|---|
| 113 | ReferenceType operator[](const KeyType& key) { |
|---|
| 114 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| 115 | return container[id]; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * The const subscript operator. The map can be subscripted by the |
|---|
| 120 | * actual keys of the graph. |
|---|
| 121 | */ |
|---|
| 122 | ConstReferenceType operator[](const KeyType& key) const { |
|---|
| 123 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| 124 | return container[id]; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** Setter function of the map. Equivalent with map[key] = val. |
|---|
| 128 | * This is a compatibility feature with the not dereferable maps. |
|---|
| 129 | */ |
|---|
| 130 | void set(const KeyType& key, const ValueType& val) { |
|---|
| 131 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| 132 | container[id] = val; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | /** Add a new key to the map. It called by the map registry. |
|---|
| 136 | */ |
|---|
| 137 | void add(const KeyType& key) { |
|---|
| 138 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| 139 | if (id >= (int)container.size()) { |
|---|
| 140 | container.resize(id + 1); |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** Erase a key from the map. It called by the map registry. |
|---|
| 145 | */ |
|---|
| 146 | void erase(const KeyType& key) {} |
|---|
| 147 | |
|---|
| 148 | /** Clear the data structure. |
|---|
| 149 | */ |
|---|
| 150 | void clear() { |
|---|
| 151 | container.clear(); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /// The stl compatible pair iterator of the map. |
|---|
| 155 | typedef MapIterator<VectorMap> Iterator; |
|---|
| 156 | /// The stl compatible const pair iterator of the map. |
|---|
| 157 | typedef MapConstIterator<VectorMap> ConstIterator; |
|---|
| 158 | |
|---|
| 159 | /** Returns the begin iterator of the map. |
|---|
| 160 | */ |
|---|
| 161 | Iterator begin() { |
|---|
| 162 | return Iterator(*this, KeyIt(*MapBase::getGraph())); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /** Returns the end iterator of the map. |
|---|
| 166 | */ |
|---|
| 167 | Iterator end() { |
|---|
| 168 | return Iterator(*this, INVALID); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /** Returns the begin ConstIterator of the map. |
|---|
| 172 | */ |
|---|
| 173 | ConstIterator begin() const { |
|---|
| 174 | return ConstIterator(*this, KeyIt(*MapBase::getGraph())); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /** Returns the end const_iterator of the map. |
|---|
| 178 | */ |
|---|
| 179 | ConstIterator end() const { |
|---|
| 180 | return ConstIterator(*this, INVALID); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /// The KeySet of the Map. |
|---|
| 184 | typedef MapConstKeySet<VectorMap> ConstKeySet; |
|---|
| 185 | |
|---|
| 186 | /// KeySet getter function. |
|---|
| 187 | ConstKeySet keySet() const { |
|---|
| 188 | return ConstKeySet(*this); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | /// The ConstValueSet of the Map. |
|---|
| 192 | typedef MapConstValueSet<VectorMap> ConstValueSet; |
|---|
| 193 | |
|---|
| 194 | /// ConstValueSet getter function. |
|---|
| 195 | ConstValueSet valueSet() const { |
|---|
| 196 | return ConstValueSet(*this); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /// The ValueSet of the Map. |
|---|
| 200 | typedef MapValueSet<VectorMap> ValueSet; |
|---|
| 201 | |
|---|
| 202 | /// ValueSet getter function. |
|---|
| 203 | ValueSet valueSet() { |
|---|
| 204 | return ValueSet(*this); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | private: |
|---|
| 209 | |
|---|
| 210 | Container container; |
|---|
| 211 | |
|---|
| 212 | public: |
|---|
| 213 | // STL compatibility typedefs. |
|---|
| 214 | typedef Iterator iterator; |
|---|
| 215 | typedef ConstIterator const_iterator; |
|---|
| 216 | typedef typename Iterator::PairValueType value_type; |
|---|
| 217 | typedef typename Iterator::KeyType key_type; |
|---|
| 218 | typedef typename Iterator::ValueType data_type; |
|---|
| 219 | typedef typename Iterator::PairReferenceType reference; |
|---|
| 220 | typedef typename Iterator::PairPointerType pointer; |
|---|
| 221 | typedef typename ConstIterator::PairReferenceType const_reference; |
|---|
| 222 | typedef typename ConstIterator::PairPointerType const_pointer; |
|---|
| 223 | typedef int difference_type; |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | /// @} |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | #endif |
|---|