27 * will belong to and the ValueType. |
27 * will belong to and the ValueType. |
28 */ |
28 */ |
29 |
29 |
30 template <typename MapRegistry, typename Value> |
30 template <typename MapRegistry, typename Value> |
31 class ArrayMap : public MapRegistry::MapBase { |
31 class ArrayMap : public MapRegistry::MapBase { |
|
32 |
|
33 template <typename MR, typename V> friend class ArrayMap; |
32 |
34 |
33 public: |
35 public: |
34 |
36 |
35 /// The graph type of the maps. |
37 /// The graph type of the maps. |
36 typedef typename MapRegistry::Graph Graph; |
38 typedef typename MapRegistry::Graph Graph; |
61 |
63 |
62 |
64 |
63 typedef std::allocator<Value> Allocator; |
65 typedef std::allocator<Value> Allocator; |
64 |
66 |
65 |
67 |
66 /** Default constructor for the map. |
|
67 */ |
|
68 ArrayMap() : capacity(0), values(0) {} |
|
69 |
|
70 /** Graph and Registry initialized map constructor. |
68 /** Graph and Registry initialized map constructor. |
71 */ |
69 */ |
72 ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) { |
70 ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) { |
73 allocate_memory(); |
71 allocate_memory(); |
74 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
72 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
116 |
114 |
117 /** Assign operator to copy a map of the same map type. |
115 /** Assign operator to copy a map of the same map type. |
118 */ |
116 */ |
119 ArrayMap& operator=(const ArrayMap& copy) { |
117 ArrayMap& operator=(const ArrayMap& copy) { |
120 if (© == this) return *this; |
118 if (© == this) return *this; |
121 |
119 |
122 if (capacity != 0) { |
120 if (MapBase::getGraph() != copy.getGraph()) { |
123 MapBase::destroy(); |
121 if (capacity != 0) { |
124 allocator.deallocate(values, capacity); |
122 MapBase::destroy(); |
125 } |
123 allocator.deallocate(values, capacity); |
126 |
124 } |
127 MapBase::operator=(copy); |
125 |
128 |
126 MapBase::operator=(copy); |
129 capacity = copy.capacity; |
127 capacity = copy.capacity; |
130 if (capacity == 0) return *this; |
128 if (capacity == 0) return *this; |
131 values = allocator.allocate(capacity); |
129 values = allocator.allocate(capacity); |
|
130 } |
132 |
131 |
133 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
132 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
134 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
133 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
135 allocator.construct(&(values[id]), copy.values[id]); |
134 allocator.construct(&(values[id]), copy.values[id]); |
136 } |
135 } |
140 |
139 |
141 /** Assign operator to copy a map of an other map type. |
140 /** Assign operator to copy a map of an other map type. |
142 */ |
141 */ |
143 template <typename TT> |
142 template <typename TT> |
144 ArrayMap& operator=(const ArrayMap<MapRegistry, TT>& copy) { |
143 ArrayMap& operator=(const ArrayMap<MapRegistry, TT>& copy) { |
145 if (capacity != 0) { |
144 |
146 MapBase::destroy(); |
145 if (MapBase::getGraph() != copy.getGraph()) { |
147 allocator.deallocate(values, capacity); |
146 if (capacity != 0) { |
148 } |
147 MapBase::destroy(); |
149 |
148 allocator.deallocate(values, capacity); |
150 MapBase::operator=(copy); |
149 } |
151 |
150 |
152 capacity = copy.capacity; |
151 MapBase::operator=(copy); |
153 if (capacity == 0) return *this; |
152 |
154 values = allocator.allocate(capacity); |
153 capacity = copy.capacity; |
|
154 if (capacity == 0) return *this; |
|
155 values = allocator.allocate(capacity); |
|
156 } |
155 |
157 |
156 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
158 for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
157 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
159 int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
158 allocator.construct(&(values[id]), copy.values[id]); |
160 allocator.construct(&(values[id]), copy.values[id]); |
159 } |
161 } |