1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_KRUSKAL_H |
20 | 20 |
#define LEMON_KRUSKAL_H |
21 | 21 |
|
22 | 22 |
#include <algorithm> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <lemon/unionfind.h> |
25 | 25 |
// #include <lemon/graph_utils.h> |
26 | 26 |
#include <lemon/maps.h> |
27 | 27 |
|
28 | 28 |
// #include <lemon/radix_sort.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/bits/utility.h> |
31 | 31 |
#include <lemon/bits/traits.h> |
32 | 32 |
|
33 | 33 |
///\ingroup spantree |
34 | 34 |
///\file |
35 | 35 |
///\brief Kruskal's algorithm to compute a minimum cost tree |
36 | 36 |
/// |
37 | 37 |
///Kruskal's algorithm to compute a minimum cost tree. |
38 | 38 |
/// |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
namespace _kruskal_bits { |
43 | 43 |
|
44 | 44 |
// Kruskal for directed graphs. |
45 | 45 |
|
46 | 46 |
template <typename Digraph, typename In, typename Out> |
47 | 47 |
typename disable_if<lemon::UndirectedTagIndicator<Digraph>, |
48 | 48 |
typename In::value_type::second_type >::type |
49 | 49 |
kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) { |
50 | 50 |
typedef typename In::value_type::second_type Value; |
51 | 51 |
typedef typename Digraph::template NodeMap<int> IndexMap; |
52 | 52 |
typedef typename Digraph::Node Node; |
53 | 53 |
|
54 | 54 |
IndexMap index(digraph); |
55 | 55 |
UnionFind<IndexMap> uf(index); |
56 | 56 |
for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
57 | 57 |
uf.insert(it); |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
Value tree_value = 0; |
61 | 61 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
62 | 62 |
if (uf.join(digraph.target(it->first),digraph.source(it->first))) { |
63 | 63 |
out.set(it->first, true); |
64 | 64 |
tree_value += it->second; |
65 | 65 |
} |
66 | 66 |
else { |
67 | 67 |
out.set(it->first, false); |
68 | 68 |
} |
69 | 69 |
} |
70 | 70 |
return tree_value; |
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
// Kruskal for undirected graphs. |
74 | 74 |
|
75 | 75 |
template <typename Graph, typename In, typename Out> |
76 | 76 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>, |
77 | 77 |
typename In::value_type::second_type >::type |
78 | 78 |
kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) { |
79 | 79 |
typedef typename In::value_type::second_type Value; |
80 | 80 |
typedef typename Graph::template NodeMap<int> IndexMap; |
81 | 81 |
typedef typename Graph::Node Node; |
82 | 82 |
|
83 | 83 |
IndexMap index(graph); |
84 | 84 |
UnionFind<IndexMap> uf(index); |
85 | 85 |
for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { |
86 | 86 |
uf.insert(it); |
87 | 87 |
} |
88 | 88 |
|
89 | 89 |
Value tree_value = 0; |
90 | 90 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
91 | 91 |
if (uf.join(graph.u(it->first),graph.v(it->first))) { |
92 | 92 |
out.set(it->first, true); |
93 | 93 |
tree_value += it->second; |
94 | 94 |
} |
95 | 95 |
else { |
96 | 96 |
out.set(it->first, false); |
97 | 97 |
} |
98 | 98 |
} |
99 | 99 |
return tree_value; |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
|
103 | 103 |
template <typename Sequence> |
104 | 104 |
struct PairComp { |
105 | 105 |
typedef typename Sequence::value_type Value; |
106 | 106 |
bool operator()(const Value& left, const Value& right) { |
107 | 107 |
return left.second < right.second; |
108 | 108 |
} |
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
template <typename In, typename Enable = void> |
112 | 112 |
struct SequenceInputIndicator { |
113 | 113 |
static const bool value = false; |
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
template <typename In> |
117 | 117 |
struct SequenceInputIndicator<In, |
118 | 118 |
typename exists<typename In::value_type::first_type>::type> { |
119 | 119 |
static const bool value = true; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
template <typename In, typename Enable = void> |
123 | 123 |
struct MapInputIndicator { |
124 | 124 |
static const bool value = false; |
125 | 125 |
}; |
126 | 126 |
|
127 | 127 |
template <typename In> |
128 | 128 |
struct MapInputIndicator<In, |
129 | 129 |
typename exists<typename In::Value>::type> { |
130 | 130 |
static const bool value = true; |
131 | 131 |
}; |
132 | 132 |
|
133 | 133 |
template <typename In, typename Enable = void> |
134 | 134 |
struct SequenceOutputIndicator { |
135 | 135 |
static const bool value = false; |
136 | 136 |
}; |
137 | 137 |
|
138 | 138 |
template <typename Out> |
139 | 139 |
struct SequenceOutputIndicator<Out, |
140 | 140 |
typename exists<typename Out::value_type>::type> { |
141 | 141 |
static const bool value = true; |
142 | 142 |
}; |
143 | 143 |
|
144 | 144 |
template <typename Out, typename Enable = void> |
145 | 145 |
struct MapOutputIndicator { |
146 | 146 |
static const bool value = false; |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
template <typename Out> |
150 | 150 |
struct MapOutputIndicator<Out, |
151 | 151 |
typename exists<typename Out::Value>::type> { |
152 | 152 |
static const bool value = true; |
153 | 153 |
}; |
154 | 154 |
|
155 | 155 |
template <typename In, typename InEnable = void> |
156 | 156 |
struct KruskalValueSelector {}; |
157 | 157 |
|
158 | 158 |
template <typename In> |
159 | 159 |
struct KruskalValueSelector<In, |
160 | 160 |
typename enable_if<SequenceInputIndicator<In>, void>::type> |
161 | 161 |
{ |
162 | 162 |
typedef typename In::value_type::second_type Value; |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
template <typename In> |
166 | 166 |
struct KruskalValueSelector<In, |
167 | 167 |
typename enable_if<MapInputIndicator<In>, void>::type> |
168 | 168 |
{ |
169 | 169 |
typedef typename In::Value Value; |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
template <typename Graph, typename In, typename Out, |
173 | 173 |
typename InEnable = void> |
174 | 174 |
struct KruskalInputSelector {}; |
175 | 175 |
|
176 | 176 |
template <typename Graph, typename In, typename Out, |
177 | 177 |
typename InEnable = void> |
178 | 178 |
struct KruskalOutputSelector {}; |
179 | 179 |
|
180 | 180 |
template <typename Graph, typename In, typename Out> |
181 | 181 |
struct KruskalInputSelector<Graph, In, Out, |
182 | 182 |
typename enable_if<SequenceInputIndicator<In>, void>::type > |
183 | 183 |
{ |
184 | 184 |
typedef typename In::value_type::second_type Value; |
185 | 185 |
|
186 | 186 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
187 | 187 |
return KruskalOutputSelector<Graph, In, Out>:: |
188 | 188 |
kruskal(graph, in, out); |
189 | 189 |
} |
190 | 190 |
|
191 | 191 |
}; |
192 | 192 |
|
193 | 193 |
template <typename Graph, typename In, typename Out> |
194 | 194 |
struct KruskalInputSelector<Graph, In, Out, |
195 | 195 |
typename enable_if<MapInputIndicator<In>, void>::type > |
196 | 196 |
{ |
197 | 197 |
typedef typename In::Value Value; |
198 | 198 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
199 | 199 |
typedef typename In::Key MapArc; |
200 | 200 |
typedef typename In::Value Value; |
201 | 201 |
typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt; |
202 | 202 |
typedef std::vector<std::pair<MapArc, Value> > Sequence; |
203 | 203 |
Sequence seq; |
204 | 204 |
|
205 | 205 |
for (MapArcIt it(graph); it != INVALID; ++it) { |
206 | 206 |
seq.push_back(std::make_pair(it, in[it])); |
207 | 207 |
} |
208 | 208 |
|
209 | 209 |
std::sort(seq.begin(), seq.end(), PairComp<Sequence>()); |
210 | 210 |
return KruskalOutputSelector<Graph, Sequence, Out>:: |
211 | 211 |
kruskal(graph, seq, out); |
212 | 212 |
} |
213 | 213 |
}; |
214 | 214 |
|
215 |
template <typename T> |
|
216 |
struct RemoveConst { |
|
217 |
typedef T type; |
|
218 |
}; |
|
219 |
|
|
220 |
template <typename T> |
|
221 |
struct RemoveConst<const T> { |
|
222 |
typedef T type; |
|
223 |
}; |
|
224 |
|
|
215 | 225 |
template <typename Graph, typename In, typename Out> |
216 | 226 |
struct KruskalOutputSelector<Graph, In, Out, |
217 | 227 |
typename enable_if<SequenceOutputIndicator<Out>, void>::type > |
218 | 228 |
{ |
219 | 229 |
typedef typename In::value_type::second_type Value; |
220 | 230 |
|
221 | 231 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
222 |
typedef StoreBoolMap<Out> Map; |
|
232 |
typedef StoreBoolMap<typename RemoveConst<Out>::type> Map; |
|
223 | 233 |
Map map(out); |
224 | 234 |
return _kruskal_bits::kruskal(graph, in, map); |
225 | 235 |
} |
226 | 236 |
|
227 | 237 |
}; |
228 | 238 |
|
229 | 239 |
template <typename Graph, typename In, typename Out> |
230 | 240 |
struct KruskalOutputSelector<Graph, In, Out, |
231 | 241 |
typename enable_if<MapOutputIndicator<Out>, void>::type > |
232 | 242 |
{ |
233 | 243 |
typedef typename In::value_type::second_type Value; |
234 | 244 |
|
235 | 245 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
236 | 246 |
return _kruskal_bits::kruskal(graph, in, out); |
237 | 247 |
} |
238 | 248 |
}; |
239 | 249 |
|
240 | 250 |
} |
241 | 251 |
|
242 | 252 |
/// \ingroup spantree |
243 | 253 |
/// |
244 | 254 |
/// \brief Kruskal's algorithm to find a minimum cost tree of a graph. |
245 | 255 |
/// |
246 | 256 |
/// This function runs Kruskal's algorithm to find a minimum cost tree. |
247 | 257 |
/// Due to some C++ hacking, it accepts various input and output types. |
248 | 258 |
/// |
249 | 259 |
/// \param g The graph the algorithm runs on. |
250 | 260 |
/// It can be either \ref concepts::Digraph "directed" or |
251 | 261 |
/// \ref concepts::Graph "undirected". |
252 | 262 |
/// If the graph is directed, the algorithm consider it to be |
253 | 263 |
/// undirected by disregarding the direction of the arcs. |
254 | 264 |
/// |
255 | 265 |
/// \param in This object is used to describe the arc costs. It can be one |
256 | 266 |
/// of the following choices. |
257 | 267 |
/// - An STL compatible 'Forward Container' with |
258 | 268 |
/// <tt>std::pair<GR::Edge,X></tt> or |
259 | 269 |
/// <tt>std::pair<GR::Arc,X></tt> as its <tt>value_type</tt>, where |
260 | 270 |
/// \c X is the type of the costs. The pairs indicates the arcs |
261 | 271 |
/// along with the assigned cost. <em>They must be in a |
262 | 272 |
/// cost-ascending order.</em> |
263 | 273 |
/// - Any readable Arc map. The values of the map indicate the arc costs. |
264 | 274 |
/// |
265 | 275 |
/// \retval out Here we also have a choise. |
266 | 276 |
/// - It can be a writable \c bool arc map. After running the |
267 | 277 |
/// algorithm this will contain the found minimum cost spanning |
268 | 278 |
/// tree: the value of an arc will be set to \c true if it belongs |
269 | 279 |
/// to the tree, otherwise it will be set to \c false. The value of |
270 | 280 |
/// each arc will be set exactly once. |
271 | 281 |
/// - It can also be an iteraror of an STL Container with |
272 | 282 |
/// <tt>GR::Edge</tt> or <tt>GR::Arc</tt> as its |
273 | 283 |
/// <tt>value_type</tt>. The algorithm copies the elements of the |
274 | 284 |
/// found tree into this sequence. For example, if we know that the |
275 | 285 |
/// spanning tree of the graph \c g has say 53 arcs, then we can |
276 | 286 |
/// put its arcs into an STL vector \c tree with a code like this. |
277 | 287 |
///\code |
278 | 288 |
/// std::vector<Arc> tree(53); |
279 | 289 |
/// kruskal(g,cost,tree.begin()); |
280 | 290 |
///\endcode |
281 | 291 |
/// Or if we don't know in advance the size of the tree, we can |
282 | 292 |
/// write this. |
283 | 293 |
///\code std::vector<Arc> tree; |
284 | 294 |
/// kruskal(g,cost,std::back_inserter(tree)); |
285 | 295 |
///\endcode |
286 | 296 |
/// |
287 | 297 |
/// \return The total cost of the found tree. |
288 | 298 |
/// |
289 | 299 |
/// \warning If kruskal runs on an be consistent of using the same |
290 | 300 |
/// Arc type for input and output. |
291 | 301 |
/// |
292 | 302 |
|
293 | 303 |
#ifdef DOXYGEN |
294 | 304 |
template <class Graph, class In, class Out> |
295 | 305 |
Value kruskal(GR const& g, const In& in, Out& out) |
296 | 306 |
#else |
297 | 307 |
template <class Graph, class In, class Out> |
298 | 308 |
inline typename _kruskal_bits::KruskalValueSelector<In>::Value |
299 | 309 |
kruskal(const Graph& graph, const In& in, Out& out) |
300 | 310 |
#endif |
301 | 311 |
{ |
302 | 312 |
return _kruskal_bits::KruskalInputSelector<Graph, In, Out>:: |
303 | 313 |
kruskal(graph, in, out); |
304 | 314 |
} |
305 | 315 |
|
306 | 316 |
|
307 | 317 |
|
308 | 318 |
|
309 | 319 |
template <class Graph, class In, class Out> |
310 | 320 |
inline typename _kruskal_bits::KruskalValueSelector<In>::Value |
311 | 321 |
kruskal(const Graph& graph, const In& in, const Out& out) |
312 | 322 |
{ |
313 | 323 |
return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>:: |
314 | 324 |
kruskal(graph, in, out); |
315 | 325 |
} |
316 | 326 |
|
317 | 327 |
} //namespace lemon |
318 | 328 |
|
319 | 329 |
#endif //LEMON_KRUSKAL_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <deque> |
20 | 20 |
#include <set> |
21 | 21 |
|
22 | 22 |
#include <lemon/concept_check.h> |
23 | 23 |
#include <lemon/concepts/maps.h> |
24 | 24 |
#include <lemon/maps.h> |
25 | 25 |
|
26 | 26 |
#include "test_tools.h" |
27 | 27 |
|
28 | 28 |
using namespace lemon; |
29 | 29 |
using namespace lemon::concepts; |
30 | 30 |
|
31 | 31 |
struct A {}; |
32 | 32 |
inline bool operator<(A, A) { return true; } |
33 | 33 |
struct B {}; |
34 | 34 |
|
35 | 35 |
class C { |
36 | 36 |
int x; |
37 | 37 |
public: |
38 | 38 |
C(int _x) : x(_x) {} |
39 | 39 |
}; |
40 | 40 |
|
41 | 41 |
class F { |
42 | 42 |
public: |
43 | 43 |
typedef A argument_type; |
44 | 44 |
typedef B result_type; |
45 | 45 |
|
46 | 46 |
B operator()(const A&) const { return B(); } |
47 | 47 |
private: |
48 | 48 |
F& operator=(const F&); |
49 | 49 |
}; |
50 | 50 |
|
51 | 51 |
int func(A) { return 3; } |
52 | 52 |
|
53 | 53 |
int binc(int a, B) { return a+1; } |
54 | 54 |
|
55 | 55 |
typedef ReadMap<A, double> DoubleMap; |
56 | 56 |
typedef ReadWriteMap<A, double> DoubleWriteMap; |
57 | 57 |
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap; |
58 | 58 |
|
59 | 59 |
typedef ReadMap<A, bool> BoolMap; |
60 | 60 |
typedef ReadWriteMap<A, bool> BoolWriteMap; |
61 | 61 |
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap; |
62 | 62 |
|
63 | 63 |
int main() |
64 | 64 |
{ |
65 | 65 |
// Map concepts |
66 | 66 |
checkConcept<ReadMap<A,B>, ReadMap<A,B> >(); |
67 | 67 |
checkConcept<ReadMap<A,C>, ReadMap<A,C> >(); |
68 | 68 |
checkConcept<WriteMap<A,B>, WriteMap<A,B> >(); |
69 | 69 |
checkConcept<WriteMap<A,C>, WriteMap<A,C> >(); |
70 | 70 |
checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >(); |
71 | 71 |
checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >(); |
72 | 72 |
checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >(); |
73 | 73 |
checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >(); |
74 | 74 |
|
75 | 75 |
// NullMap |
76 | 76 |
{ |
77 | 77 |
checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >(); |
78 | 78 |
NullMap<A,B> map1; |
79 | 79 |
NullMap<A,B> map2 = map1; |
80 | 80 |
map1 = nullMap<A,B>(); |
81 | 81 |
} |
82 | 82 |
|
83 | 83 |
// ConstMap |
84 | 84 |
{ |
85 | 85 |
checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >(); |
86 | 86 |
checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >(); |
87 | 87 |
ConstMap<A,B> map1; |
88 |
ConstMap<A,B> map2 |
|
88 |
ConstMap<A,B> map2 = B(); |
|
89 | 89 |
ConstMap<A,B> map3 = map1; |
90 | 90 |
map1 = constMap<A>(B()); |
91 | 91 |
map1 = constMap<A,B>(); |
92 | 92 |
map1.setAll(B()); |
93 | 93 |
ConstMap<A,C> map4(C(1)); |
94 | 94 |
ConstMap<A,C> map5 = map4; |
95 | 95 |
map4 = constMap<A>(C(2)); |
96 | 96 |
map4.setAll(C(3)); |
97 | 97 |
|
98 | 98 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >(); |
99 | 99 |
check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap"); |
100 | 100 |
|
101 | 101 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >(); |
102 | 102 |
ConstMap<A,Const<int,10> > map6; |
103 | 103 |
ConstMap<A,Const<int,10> > map7 = map6; |
104 | 104 |
map6 = constMap<A,int,10>(); |
105 | 105 |
map7 = constMap<A,Const<int,10> >(); |
106 | 106 |
check(map6[A()] == 10 && map7[A()] == 10, "Something is wrong with ConstMap"); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
// IdentityMap |
110 | 110 |
{ |
111 | 111 |
checkConcept<ReadMap<A,A>, IdentityMap<A> >(); |
112 | 112 |
IdentityMap<A> map1; |
113 | 113 |
IdentityMap<A> map2 = map1; |
114 | 114 |
map1 = identityMap<A>(); |
115 | 115 |
|
116 | 116 |
checkConcept<ReadMap<double,double>, IdentityMap<double> >(); |
117 | 117 |
check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14, |
118 | 118 |
"Something is wrong with IdentityMap"); |
119 | 119 |
} |
120 | 120 |
|
121 | 121 |
// RangeMap |
122 | 122 |
{ |
123 | 123 |
checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >(); |
124 | 124 |
RangeMap<B> map1; |
125 | 125 |
RangeMap<B> map2(10); |
126 | 126 |
RangeMap<B> map3(10,B()); |
127 | 127 |
RangeMap<B> map4 = map1; |
128 | 128 |
RangeMap<B> map5 = rangeMap<B>(); |
129 | 129 |
RangeMap<B> map6 = rangeMap<B>(10); |
130 | 130 |
RangeMap<B> map7 = rangeMap(10,B()); |
131 | 131 |
|
132 | 132 |
checkConcept< ReferenceMap<int, double, double&, const double&>, |
133 | 133 |
RangeMap<double> >(); |
134 | 134 |
std::vector<double> v(10, 0); |
135 | 135 |
v[5] = 100; |
136 | 136 |
RangeMap<double> map8(v); |
137 | 137 |
RangeMap<double> map9 = rangeMap(v); |
138 | 138 |
check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100, |
139 | 139 |
"Something is wrong with RangeMap"); |
140 | 140 |
} |
141 | 141 |
|
142 | 142 |
// SparseMap |
143 | 143 |
{ |
144 | 144 |
checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >(); |
145 | 145 |
SparseMap<A,B> map1; |
146 |
SparseMap<A,B> map2 |
|
146 |
SparseMap<A,B> map2 = B(); |
|
147 | 147 |
SparseMap<A,B> map3 = sparseMap<A,B>(); |
148 | 148 |
SparseMap<A,B> map4 = sparseMap<A>(B()); |
149 | 149 |
|
150 | 150 |
checkConcept< ReferenceMap<double, int, int&, const int&>, |
151 | 151 |
SparseMap<double, int> >(); |
152 | 152 |
std::map<double, int> m; |
153 | 153 |
SparseMap<double, int> map5(m); |
154 | 154 |
SparseMap<double, int> map6(m,10); |
155 | 155 |
SparseMap<double, int> map7 = sparseMap(m); |
156 | 156 |
SparseMap<double, int> map8 = sparseMap(m,10); |
157 | 157 |
|
158 | 158 |
check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10, |
159 | 159 |
"Something is wrong with SparseMap"); |
160 | 160 |
map5[1.0] = map6[3.14] = 100; |
161 | 161 |
check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100, |
162 | 162 |
"Something is wrong with SparseMap"); |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
// ComposeMap |
166 | 166 |
{ |
167 | 167 |
typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap; |
168 | 168 |
checkConcept<ReadMap<B,double>, CompMap>(); |
169 | 169 |
CompMap map1(DoubleMap(),ReadMap<B,A>()); |
170 | 170 |
CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>()); |
171 | 171 |
|
172 | 172 |
SparseMap<double, bool> m1(false); m1[3.14] = true; |
173 | 173 |
RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14; |
174 | 174 |
check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap") |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
// CombineMap |
178 | 178 |
{ |
179 | 179 |
typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap; |
180 | 180 |
checkConcept<ReadMap<A,double>, CombMap>(); |
181 | 181 |
CombMap map1(DoubleMap(), DoubleMap()); |
182 | 182 |
CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>()); |
183 | 183 |
|
184 | 184 |
check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3, |
185 | 185 |
"Something is wrong with CombineMap"); |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
// FunctorToMap, MapToFunctor |
189 | 189 |
{ |
190 | 190 |
checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >(); |
191 | 191 |
checkConcept<ReadMap<A,B>, FunctorToMap<F> >(); |
192 | 192 |
FunctorToMap<F> map1; |
193 | 193 |
FunctorToMap<F> map2(F()); |
194 | 194 |
B b = functorToMap(F())[A()]; |
195 | 195 |
|
196 | 196 |
checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >(); |
197 | 197 |
MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>()); |
198 | 198 |
|
199 | 199 |
check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap"); |
200 | 200 |
check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor"); |
201 | 201 |
check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3, |
202 | 202 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
203 | 203 |
check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2, |
204 | 204 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
// ConvertMap |
208 | 208 |
{ |
209 | 209 |
checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >(); |
210 | 210 |
ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true)); |
211 | 211 |
ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false)); |
212 | 212 |
} |
213 | 213 |
|
214 | 214 |
// ForkMap |
215 | 215 |
{ |
216 | 216 |
checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >(); |
217 | 217 |
|
218 | 218 |
typedef RangeMap<double> RM; |
219 | 219 |
typedef SparseMap<int, double> SM; |
220 | 220 |
RM m1(10, -1); |
221 | 221 |
SM m2(-1); |
222 | 222 |
checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >(); |
223 | 223 |
checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >(); |
224 | 224 |
ForkMap<RM, SM> map1(m1,m2); |
225 | 225 |
ForkMap<SM, RM> map2 = forkMap(m2,m1); |
226 | 226 |
map2.set(5, 10); |
227 | 227 |
check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10, |
228 | 228 |
"Something is wrong with ForkMap"); |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
// Arithmetic maps: |
232 | 232 |
// - AddMap, SubMap, MulMap, DivMap |
233 | 233 |
// - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap |
234 | 234 |
// - NegMap, NegWriteMap, AbsMap |
235 | 235 |
{ |
236 | 236 |
checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >(); |
237 | 237 |
checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >(); |
238 | 238 |
checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >(); |
239 | 239 |
checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >(); |
240 | 240 |
|
241 | 241 |
ConstMap<int, double> c1(1.0), c2(3.14); |
242 | 242 |
IdentityMap<int> im; |
243 | 243 |
ConvertMap<IdentityMap<int>, double> id(im); |
244 | 244 |
check(addMap(c1,id)[0] == 1.0 && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap"); |
245 | 245 |
check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0, "Something is wrong with SubMap"); |
246 | 246 |
check(mulMap(id,c2)[0] == 0 && mulMap(id,c2)[2] == 6.28, "Something is wrong with MulMap"); |
247 | 247 |
check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2] == 1.57, "Something is wrong with DivMap"); |
248 | 248 |
|
249 | 249 |
checkConcept<DoubleMap, ShiftMap<DoubleMap> >(); |
250 | 250 |
checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >(); |
251 | 251 |
checkConcept<DoubleMap, ScaleMap<DoubleMap> >(); |
252 | 252 |
checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >(); |
253 | 253 |
checkConcept<DoubleMap, NegMap<DoubleMap> >(); |
254 | 254 |
checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >(); |
255 | 255 |
checkConcept<DoubleMap, AbsMap<DoubleMap> >(); |
256 | 256 |
|
257 | 257 |
check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0, |
258 | 258 |
"Something is wrong with ShiftMap"); |
259 | 259 |
check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0, |
260 | 260 |
"Something is wrong with ShiftWriteMap"); |
261 | 261 |
check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0, |
262 | 262 |
"Something is wrong with ScaleMap"); |
263 | 263 |
check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0, |
264 | 264 |
"Something is wrong with ScaleWriteMap"); |
265 | 265 |
check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0, |
266 | 266 |
"Something is wrong with NegMap"); |
267 | 267 |
check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0, |
268 | 268 |
"Something is wrong with NegWriteMap"); |
269 | 269 |
check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0, |
270 | 270 |
"Something is wrong with AbsMap"); |
271 | 271 |
} |
272 | 272 |
|
273 | 273 |
// Logical maps: |
274 | 274 |
// - TrueMap, FalseMap |
275 | 275 |
// - AndMap, OrMap |
276 | 276 |
// - NotMap, NotWriteMap |
277 | 277 |
// - EqualMap, LessMap |
278 | 278 |
{ |
279 | 279 |
checkConcept<BoolMap, TrueMap<A> >(); |
280 | 280 |
checkConcept<BoolMap, FalseMap<A> >(); |
281 | 281 |
checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >(); |
282 | 282 |
checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >(); |
283 | 283 |
checkConcept<BoolMap, NotMap<BoolMap> >(); |
284 | 284 |
checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >(); |
285 | 285 |
checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >(); |
286 | 286 |
checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >(); |
287 | 287 |
|
288 | 288 |
TrueMap<int> tm; |
289 | 289 |
FalseMap<int> fm; |
290 | 290 |
RangeMap<bool> rm(2); |
291 | 291 |
rm[0] = true; rm[1] = false; |
292 | 292 |
check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1], |
293 | 293 |
"Something is wrong with AndMap"); |
294 | 294 |
check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1], |
295 | 295 |
"Something is wrong with OrMap"); |
296 | 296 |
check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap"); |
297 | 297 |
check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap"); |
298 | 298 |
|
299 | 299 |
ConstMap<int, double> cm(2.0); |
300 | 300 |
IdentityMap<int> im; |
301 | 301 |
ConvertMap<IdentityMap<int>, double> id(im); |
302 | 302 |
check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3], |
303 | 303 |
"Something is wrong with LessMap"); |
304 | 304 |
check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3], |
305 | 305 |
"Something is wrong with EqualMap"); |
306 | 306 |
} |
307 | 307 |
|
308 | 308 |
return 0; |
309 | 309 |
} |
0 comments (0 inline)