2 * src/hugo/vector_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_VECTOR_MAP_H
18 #define HUGO_VECTOR_MAP_H
22 #include <hugo/map_iterator.h>
23 #include <hugo/map_bits.h>
27 ///\brief Vector based graph maps.
31 /// \addtogroup graphmaps
34 /** The ArrayMap template class is graph map structure what
35 * automatically updates the map when a key is added to or erased from
36 * the map. This map factory uses the allocators to implement
37 * the container functionality. This map factory
38 * uses the std::vector to implement the container function.
40 * The template parameter is the MapRegistry that the maps
41 * will belong to and the ValueType.
43 * \todo It should use a faster initialization using the maxNodeId() or
44 * maxEdgeId() function of the graph instead of iterating through each
48 template <typename MapRegistry, typename Value>
49 class VectorMap : public MapRegistry::MapBase {
50 template <typename MR, typename T> friend class VectorMap;
53 /// The graph type of the maps.
54 typedef typename MapRegistry::Graph Graph;
55 /// The key type of the maps.
56 typedef typename MapRegistry::KeyType KeyType;
57 /// The iterator to iterate on the keys.
58 typedef typename MapRegistry::KeyIt KeyIt;
61 typedef VectorMap Map;
62 /// The MapBase of the Map which implements the core regisitry function.
63 typedef typename MapRegistry::MapBase MapBase;
67 /// The container type of the map.
68 typedef std::vector<Value> Container;
73 /// The value type of the map.
74 typedef Value ValueType;
75 /// The reference type of the map;
76 typedef typename Container::reference ReferenceType;
77 /// The pointer type of the map;
78 typedef typename Container::pointer PointerType;
80 /// The const value type of the map.
81 typedef const Value ConstValueType;
82 /// The const reference type of the map;
83 typedef typename Container::const_reference ConstReferenceType;
84 /// The pointer type of the map;
85 typedef typename Container::const_pointer ConstPointerType;
87 /** Graph and Registry initialized map constructor.
89 VectorMap(const Graph& g, MapRegistry& r)
90 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
92 /** Constructor to use default value to initialize the map.
94 VectorMap(const Graph& g, MapRegistry& r, const Value& v)
95 : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
97 /** Assign operator to copy a map of an other map type.
99 template <typename TT>
100 VectorMap(const VectorMap<MapRegistry, TT>& c)
101 : MapBase(c), container(c.container.size()) {
102 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
103 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
104 container[id] = c.container[id];
108 /** Assign operator to copy a map of an other map type.
110 template <typename TT>
111 VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) {
112 if (MapBase::getGraph() != c.getGraph()) {
113 MapBase::operator=(c);
114 container.resize(c.container.size());
116 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
117 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
118 container[id] = c.container[id];
123 * The subscript operator. The map can be subscripted by the
124 * actual keys of the graph.
126 ReferenceType operator[](const KeyType& key) {
127 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
128 return container[id];
132 * The const subscript operator. The map can be subscripted by the
133 * actual keys of the graph.
135 ConstReferenceType operator[](const KeyType& key) const {
136 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
137 return container[id];
140 /** Setter function of the map. Equivalent with map[key] = val.
141 * This is a compatibility feature with the not dereferable maps.
143 void set(const KeyType& key, const ValueType& val) {
144 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
148 /** Add a new key to the map. It called by the map registry.
150 void add(const KeyType& key) {
151 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
152 if (id >= (int)container.size()) {
153 container.resize(id + 1);
157 /** Erase a key from the map. It called by the map registry.
159 void erase(const KeyType& key) {}
161 /** Clear the data structure.
167 /// The stl compatible pair iterator of the map.
168 typedef MapIterator<VectorMap> Iterator;
169 /// The stl compatible const pair iterator of the map.
170 typedef MapConstIterator<VectorMap> ConstIterator;
172 /** Returns the begin iterator of the map.
175 return Iterator(*this, KeyIt(*MapBase::getGraph()));
178 /** Returns the end iterator of the map.
181 return Iterator(*this, INVALID);
184 /** Returns the begin ConstIterator of the map.
186 ConstIterator begin() const {
187 return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
190 /** Returns the end const_iterator of the map.
192 ConstIterator end() const {
193 return ConstIterator(*this, INVALID);
196 /// The KeySet of the Map.
197 typedef MapConstKeySet<VectorMap> ConstKeySet;
199 /// KeySet getter function.
200 ConstKeySet keySet() const {
201 return ConstKeySet(*this);
204 /// The ConstValueSet of the Map.
205 typedef MapConstValueSet<VectorMap> ConstValueSet;
207 /// ConstValueSet getter function.
208 ConstValueSet valueSet() const {
209 return ConstValueSet(*this);
212 /// The ValueSet of the Map.
213 typedef MapValueSet<VectorMap> ValueSet;
215 /// ValueSet getter function.
216 ValueSet valueSet() {
217 return ValueSet(*this);
226 // STL compatibility typedefs.
227 typedef Iterator iterator;
228 typedef ConstIterator const_iterator;
229 typedef typename Iterator::PairValueType value_type;
230 typedef typename Iterator::KeyType key_type;
231 typedef typename Iterator::ValueType data_type;
232 typedef typename Iterator::PairReferenceType reference;
233 typedef typename Iterator::PairPointerType pointer;
234 typedef typename ConstIterator::PairReferenceType const_reference;
235 typedef typename ConstIterator::PairPointerType const_pointer;
236 typedef int difference_type;