1 | /* -*- C++ -*- |
---|
2 | * lemon/default_map.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
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. |
---|
10 | * |
---|
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 |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_DEFAULT_MAP_H |
---|
18 | #define LEMON_DEFAULT_MAP_H |
---|
19 | |
---|
20 | |
---|
21 | #include <lemon/bits/array_map.h> |
---|
22 | #include <lemon/bits/vector_map.h> |
---|
23 | |
---|
24 | ///\ingroup graphmapfactory |
---|
25 | ///\file |
---|
26 | ///\brief Graph maps that construct and destruct |
---|
27 | ///their elements dynamically. |
---|
28 | |
---|
29 | namespace lemon { |
---|
30 | |
---|
31 | /// \addtogroup graphmapfactory |
---|
32 | /// @{ |
---|
33 | |
---|
34 | template <typename _Graph, typename _Item, typename _Value> |
---|
35 | struct DefaultMapSelector { |
---|
36 | typedef ArrayMap<_Graph, _Item, _Value> Map; |
---|
37 | }; |
---|
38 | |
---|
39 | // bool |
---|
40 | template <typename _Graph, typename _Item> |
---|
41 | struct DefaultMapSelector<_Graph, _Item, bool> { |
---|
42 | typedef VectorMap<_Graph, _Item, bool> Map; |
---|
43 | }; |
---|
44 | |
---|
45 | // char |
---|
46 | template <typename _Graph, typename _Item> |
---|
47 | struct DefaultMapSelector<_Graph, _Item, char> { |
---|
48 | typedef VectorMap<_Graph, _Item, char> Map; |
---|
49 | }; |
---|
50 | |
---|
51 | template <typename _Graph, typename _Item> |
---|
52 | struct DefaultMapSelector<_Graph, _Item, signed char> { |
---|
53 | typedef VectorMap<_Graph, _Item, signed char> Map; |
---|
54 | }; |
---|
55 | |
---|
56 | template <typename _Graph, typename _Item> |
---|
57 | struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
---|
58 | typedef VectorMap<_Graph, _Item, unsigned char> Map; |
---|
59 | }; |
---|
60 | |
---|
61 | |
---|
62 | // int |
---|
63 | template <typename _Graph, typename _Item> |
---|
64 | struct DefaultMapSelector<_Graph, _Item, signed int> { |
---|
65 | typedef VectorMap<_Graph, _Item, signed int> Map; |
---|
66 | }; |
---|
67 | |
---|
68 | template <typename _Graph, typename _Item> |
---|
69 | struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
---|
70 | typedef VectorMap<_Graph, _Item, unsigned int> Map; |
---|
71 | }; |
---|
72 | |
---|
73 | |
---|
74 | // short |
---|
75 | template <typename _Graph, typename _Item> |
---|
76 | struct DefaultMapSelector<_Graph, _Item, signed short> { |
---|
77 | typedef VectorMap<_Graph, _Item, signed short> Map; |
---|
78 | }; |
---|
79 | |
---|
80 | template <typename _Graph, typename _Item> |
---|
81 | struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
---|
82 | typedef VectorMap<_Graph, _Item, unsigned short> Map; |
---|
83 | }; |
---|
84 | |
---|
85 | |
---|
86 | // long |
---|
87 | template <typename _Graph, typename _Item> |
---|
88 | struct DefaultMapSelector<_Graph, _Item, signed long> { |
---|
89 | typedef VectorMap<_Graph, _Item, signed long> Map; |
---|
90 | }; |
---|
91 | |
---|
92 | template <typename _Graph, typename _Item> |
---|
93 | struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
---|
94 | typedef VectorMap<_Graph, _Item, unsigned long> Map; |
---|
95 | }; |
---|
96 | |
---|
97 | // \todo handling long long type |
---|
98 | |
---|
99 | |
---|
100 | // float |
---|
101 | template <typename _Graph, typename _Item> |
---|
102 | struct DefaultMapSelector<_Graph, _Item, float> { |
---|
103 | typedef VectorMap<_Graph, _Item, float> Map; |
---|
104 | }; |
---|
105 | |
---|
106 | |
---|
107 | // double |
---|
108 | template <typename _Graph, typename _Item> |
---|
109 | struct DefaultMapSelector<_Graph, _Item, double> { |
---|
110 | typedef VectorMap<_Graph, _Item, double> Map; |
---|
111 | }; |
---|
112 | |
---|
113 | |
---|
114 | // long double |
---|
115 | template <typename _Graph, typename _Item> |
---|
116 | struct DefaultMapSelector<_Graph, _Item, long double> { |
---|
117 | typedef VectorMap<_Graph, _Item, long double> Map; |
---|
118 | }; |
---|
119 | |
---|
120 | |
---|
121 | // pointer |
---|
122 | template <typename _Graph, typename _Item, typename _Ptr> |
---|
123 | struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
---|
124 | typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
---|
125 | }; |
---|
126 | |
---|
127 | /// \e |
---|
128 | template < |
---|
129 | typename _Graph, |
---|
130 | typename _Item, |
---|
131 | typename _Value> |
---|
132 | class DefaultMap |
---|
133 | : public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
---|
134 | public: |
---|
135 | typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
---|
136 | typedef DefaultMap<_Graph, _Item, _Value> Map; |
---|
137 | |
---|
138 | typedef typename Parent::Graph Graph; |
---|
139 | typedef typename Parent::Value Value; |
---|
140 | |
---|
141 | DefaultMap(const Graph& _g) : Parent(_g) {} |
---|
142 | DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {} |
---|
143 | |
---|
144 | }; |
---|
145 | |
---|
146 | |
---|
147 | /// \e |
---|
148 | template <typename _Base> |
---|
149 | class MappableGraphExtender : public _Base { |
---|
150 | public: |
---|
151 | |
---|
152 | typedef MappableGraphExtender<_Base> Graph; |
---|
153 | typedef _Base Parent; |
---|
154 | |
---|
155 | typedef typename Parent::Node Node; |
---|
156 | typedef typename Parent::NodeIt NodeIt; |
---|
157 | |
---|
158 | typedef typename Parent::Edge Edge; |
---|
159 | typedef typename Parent::EdgeIt EdgeIt; |
---|
160 | |
---|
161 | |
---|
162 | template <typename _Value> |
---|
163 | class NodeMap |
---|
164 | : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > { |
---|
165 | public: |
---|
166 | typedef MappableGraphExtender Graph; |
---|
167 | typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent; |
---|
168 | |
---|
169 | NodeMap(const Graph& _g) |
---|
170 | : Parent(_g) {} |
---|
171 | NodeMap(const Graph& _g, const _Value& _v) |
---|
172 | : Parent(_g, _v) {} |
---|
173 | |
---|
174 | /// \brief Template assign operator. |
---|
175 | /// |
---|
176 | /// The given parameter should be conform to the ReadMap |
---|
177 | /// concecpt and could be indiced by the current item set of |
---|
178 | /// the NodeMap. In this case the value for each item |
---|
179 | /// is assigned by the value of the given ReadMap. |
---|
180 | template <typename CMap> |
---|
181 | NodeMap& operator=(const CMap& cmap) { |
---|
182 | checkConcept<concept::ReadMap<Node, _Value>, CMap>(); |
---|
183 | const typename Parent::Graph* graph = Parent::getGraph(); |
---|
184 | Node it; |
---|
185 | for (graph->first(it); it != INVALID; graph->next(it)) { |
---|
186 | Parent::set(it, cmap[it]); |
---|
187 | } |
---|
188 | return *this; |
---|
189 | } |
---|
190 | |
---|
191 | }; |
---|
192 | |
---|
193 | template <typename _Value> |
---|
194 | class EdgeMap |
---|
195 | : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > { |
---|
196 | public: |
---|
197 | typedef MappableGraphExtender Graph; |
---|
198 | typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
---|
199 | |
---|
200 | EdgeMap(const Graph& _g) |
---|
201 | : Parent(_g) {} |
---|
202 | EdgeMap(const Graph& _g, const _Value& _v) |
---|
203 | : Parent(_g, _v) {} |
---|
204 | |
---|
205 | template <typename CMap> |
---|
206 | EdgeMap& operator=(const CMap& cmap) { |
---|
207 | checkConcept<concept::ReadMap<Edge, _Value>, CMap>(); |
---|
208 | const typename Parent::Graph* graph = Parent::getGraph(); |
---|
209 | Edge it; |
---|
210 | for (graph->first(it); it != INVALID; graph->next(it)) { |
---|
211 | Parent::set(it, cmap[it]); |
---|
212 | } |
---|
213 | return *this; |
---|
214 | } |
---|
215 | }; |
---|
216 | |
---|
217 | }; |
---|
218 | |
---|
219 | /// \e |
---|
220 | template <typename _Base> |
---|
221 | class MappableUndirGraphExtender : |
---|
222 | public MappableGraphExtender<_Base> { |
---|
223 | public: |
---|
224 | |
---|
225 | typedef MappableUndirGraphExtender Graph; |
---|
226 | typedef MappableGraphExtender<_Base> Parent; |
---|
227 | |
---|
228 | typedef typename Parent::UndirEdge UndirEdge; |
---|
229 | |
---|
230 | template <typename _Value> |
---|
231 | class UndirEdgeMap |
---|
232 | : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > { |
---|
233 | public: |
---|
234 | typedef MappableUndirGraphExtender Graph; |
---|
235 | typedef IterableMapExtender< |
---|
236 | DefaultMap<Graph, UndirEdge, _Value> > Parent; |
---|
237 | |
---|
238 | UndirEdgeMap(const Graph& _g) |
---|
239 | : Parent(_g) {} |
---|
240 | UndirEdgeMap(const Graph& _g, const _Value& _v) |
---|
241 | : Parent(_g, _v) {} |
---|
242 | |
---|
243 | template <typename CMap> |
---|
244 | UndirEdgeMap& operator=(const CMap& cmap) { |
---|
245 | checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>(); |
---|
246 | const typename Parent::Graph* graph = Parent::getGraph(); |
---|
247 | UndirEdge it; |
---|
248 | for (graph->first(it); it != INVALID; graph->next(it)) { |
---|
249 | Parent::set(it, cmap[it]); |
---|
250 | } |
---|
251 | return *this; |
---|
252 | } |
---|
253 | }; |
---|
254 | |
---|
255 | |
---|
256 | }; |
---|
257 | |
---|
258 | /// @} |
---|
259 | } |
---|
260 | |
---|
261 | #endif |
---|