2 * src/hugo/array_map.h - Part of HUGOlib, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef HUGO_ARRAY_MAP_H
18 #define HUGO_ARRAY_MAP_H
22 #include <hugo/map_iterator.h>
23 #include <hugo/map_bits.h>
27 ///\brief Graph maps that construates and destruates
28 ///their elements dynamically.
33 /// \addtogroup graphmaps
36 /** The ArrayMap template class is graph map structure what
37 * automatically updates the map when a key is added to or erased from
38 * the map. This map factory uses the allocators to implement
39 * the container functionality.
41 * The template parameter is the MapRegistry that the maps
42 * will belong to and the ValueType.
45 template <typename MapRegistry, typename Value>
46 class ArrayMap : public MapRegistry::MapBase {
48 template <typename MR, typename V> friend class ArrayMap;
52 /// The graph type of the maps.
53 typedef typename MapRegistry::Graph Graph;
54 /// The key type of the maps.
55 typedef typename MapRegistry::KeyType KeyType;
56 /// The iterator to iterate on the keys.
57 typedef typename MapRegistry::KeyIt KeyIt;
59 /// The MapBase of the Map which imlements the core regisitry function.
60 typedef typename MapRegistry::MapBase MapBase;
65 /// The value type of the map.
66 typedef Value ValueType;
67 /// The reference type of the map;
68 typedef Value& ReferenceType;
69 /// The pointer type of the map;
70 typedef Value* PointerType;
72 /// The const value type of the map.
73 typedef const Value ConstValueType;
74 /// The const reference type of the map;
75 typedef const Value& ConstReferenceType;
76 /// The pointer type of the map;
77 typedef const Value* ConstPointerType;
80 typedef std::allocator<Value> Allocator;
83 /** Graph and Registry initialized map constructor.
85 ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) {
87 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
88 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
89 allocator.construct(&(values[id]), Value());
93 /** Constructor to use default value to initialize the map.
95 ArrayMap(const Graph& g, MapRegistry& r, const Value& v)
98 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
99 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
100 allocator.construct(&(values[id]), v);
104 /** Constructor to copy a map of the same map type.
106 ArrayMap(const ArrayMap& copy) : MapBase(copy) {
107 capacity = copy.capacity;
108 if (capacity == 0) return;
109 values = allocator.allocate(capacity);
110 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
111 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
112 allocator.construct(&(values[id]), copy.values[id]);
116 /** Constructor to copy a map of an other map type.
118 template <typename TT>
119 ArrayMap(const ArrayMap<MapRegistry, TT>& copy)
121 capacity = copy.capacity;
122 if (capacity == 0) return;
123 values = allocator.allocate(capacity);
124 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
125 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
126 allocator.construct(&(values[id]), copy.values[id]);
130 /** Assign operator to copy a map of the same map type.
132 ArrayMap& operator=(const ArrayMap& copy) {
133 if (© == this) return *this;
135 if (MapBase::getGraph() != copy.getGraph()) {
138 allocator.deallocate(values, capacity);
141 MapBase::operator=(copy);
142 capacity = copy.capacity;
143 if (capacity == 0) return *this;
144 values = allocator.allocate(capacity);
147 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
148 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
149 allocator.construct(&(values[id]), copy.values[id]);
155 /** Assign operator to copy a map of an other map type.
157 template <typename TT>
158 ArrayMap& operator=(const ArrayMap<MapRegistry, TT>& copy) {
160 if (MapBase::getGraph() != copy.getGraph()) {
163 allocator.deallocate(values, capacity);
166 MapBase::operator=(copy);
168 capacity = copy.capacity;
169 if (capacity == 0) return *this;
170 values = allocator.allocate(capacity);
173 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
174 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
175 allocator.construct(&(values[id]), copy.values[id]);
181 /** The destructor of the map.
183 virtual ~ArrayMap() {
186 allocator.deallocate(values, capacity);
192 * The subscript operator. The map can be subscripted by the
193 * actual keys of the graph.
195 ReferenceType operator[](const KeyType& key) {
196 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
201 * The const subscript operator. The map can be subscripted by the
202 * actual keys of the graph.
204 ConstReferenceType operator[](const KeyType& key) const {
205 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
209 /** Setter function of the map. Equivalent with map[key] = val.
210 * This is a compatibility feature with the not dereferable maps.
212 void set(const KeyType& key, const ValueType& val) {
213 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
217 /** Add a new key to the map. It called by the map registry.
219 void add(const KeyType& key) {
220 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
221 if (id >= capacity) {
222 int new_capacity = (capacity == 0 ? 1 : capacity);
223 while (new_capacity <= id) {
226 Value* new_values = allocator.allocate(new_capacity);;
227 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
228 int jd = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
230 allocator.construct(&(new_values[jd]), values[jd]);
231 allocator.destroy(&(values[jd]));
234 if (capacity != 0) allocator.deallocate(values, capacity);
236 capacity = new_capacity;
238 allocator.construct(&(values[id]), Value());
241 /** Erase a key from the map. It called by the map registry.
243 void erase(const KeyType& key) {
244 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
245 allocator.destroy(&(values[id]));
248 /** Clear the data structure.
253 allocator.deallocate(values, capacity);
258 /// The stl compatible pair iterator of the map.
259 typedef MapIterator<ArrayMap> Iterator;
260 /// The stl compatible const pair iterator of the map.
261 typedef MapConstIterator<ArrayMap> ConstIterator;
263 /** Returns the begin iterator of the map.
266 return Iterator(*this, KeyIt(*MapBase::getGraph()));
269 /** Returns the end iterator of the map.
272 return Iterator(*this, INVALID);
275 /** Returns the begin ConstIterator of the map.
277 ConstIterator begin() const {
278 return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
281 /** Returns the end const_iterator of the map.
283 ConstIterator end() const {
284 return ConstIterator(*this, INVALID);
287 /// The KeySet of the Map.
288 typedef MapConstKeySet<ArrayMap> ConstKeySet;
290 /// KeySet getter function.
291 ConstKeySet keySet() const {
292 return ConstKeySet(*this);
295 /// The ConstValueSet of the Map.
296 typedef MapConstValueSet<ArrayMap> ConstValueSet;
298 /// ConstValueSet getter function.
299 ConstValueSet valueSet() const {
300 return ConstValueSet(*this);
303 /// The ValueSet of the Map.
304 typedef MapValueSet<ArrayMap> ValueSet;
306 /// ValueSet getter function.
307 ValueSet valueSet() {
308 return ValueSet(*this);
313 void allocate_memory() {
314 int max_id = KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph());
321 while (capacity <= max_id) {
324 values = allocator.allocate(capacity);
332 // STL compatibility typedefs.
333 typedef Iterator iterator;
334 typedef ConstIterator const_iterator;
335 typedef typename Iterator::PairValueType value_type;
336 typedef typename Iterator::KeyType key_type;
337 typedef typename Iterator::ValueType data_type;
338 typedef typename Iterator::PairReferenceType reference;
339 typedef typename Iterator::PairPointerType pointer;
340 typedef typename ConstIterator::PairReferenceType const_reference;
341 typedef typename ConstIterator::PairPointerType const_pointer;
342 typedef int difference_type;
349 #endif //HUGO_ARRAY_MAP_H