Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test-rvalues.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <iostream>
33# include <string>
34# include <tpl_agraph.H>
35# include <tpl_dynMapTree.H>
36
37using namespace std;
38
39size_t V = 1000;
40
41template <class GT>
43{
44 GT g;
46 for (int i = 0; i < (int) V; ++i)
47 nodes(i) = g.insert_node(i);
48
49 for (int i = 0; i < (int) V - 1; ++i)
50 {
51 typename GT::Node * src = nodes(i);
52 for (int j = i + 1; j < (int) V; ++j)
53 g.insert_arc(src, nodes(j), i + j);
54 }
55
56 return g;
57}
58
59template <class GT>
60bool check(GT & g)
61{
63 for (size_t i = 0; i < V; ++i)
64 seen(i) = false;
65
66 size_t count = 0;
67 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next())
68 {
69 const auto info = it.get_curr()->get_info();
70 if (info >= V)
71 {
72 cout << "Inconsistencia en el nodo " << info << endl;
73 abort();
74 }
75
76 if (seen(info))
77 {
78 cout << "Nodo duplicado " << info << endl;
79 abort();
80 }
81
82 seen(info) = true;
83 ++count;
84 }
85
86 for (size_t i = 0; i < V; ++i)
87 if (not seen(i))
88 {
89 cout << "Falta el nodo " << i << endl;
90 abort();
91 }
92
93 if (count != V)
94 {
95 cout << "Cantidad incorrecta de nodos " << count << endl;
96 abort();
97 }
98
99 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next())
100 {
101 typename GT::Arc * a = it.get_curr();
102 typename GT::Node * src = g.get_src_node(a);
103 typename GT::Node * tgt = g.get_tgt_node(a);
104 if (a->get_info() != src->get_info() + tgt->get_info())
105 {
106 cout << "Inconsistencia en el arco " << a->get_info() << endl;
107 abort();
108 }
109 }
110
111 return true;
112}
113
114template <class GT>
115void test()
116{
117 cout << "R value ctor test" << endl;
119 check(lg);
120 cout << "done" << endl << endl;
121
122 {
123 cout << "L value ctor test" << endl;
124 GT ng = lg;
125 check(ng);
126 cout << "done" << endl << endl;
127 }
128
129 {
130 cout << "L value = test" << endl;
131 GT lg1;
132 lg1 = lg;
133 check(lg1);
134 cout << "done" << endl << endl;
135 }
136
137 cout << "R value = test" << endl;
139 check(lg);
140 cout << "done" << endl << endl;
141}
142
143
144template <class L>
145L create_list(int beg = 0, int end = V - 1)
146{
147 L l;
148 for (int i = beg; i <= end; ++i)
149 l.append(i);
150
151 return l;
152}
153
154
155template <class L>
156bool check_list(const L & l)
157{
158 int i = l.get_first();
159 for (typename L::Iterator it(l); it.has_curr(); it.next())
160 if (it.get_curr() != i++)
161 {
162 cout << "Inconsistencia en el nodo " << i - 1
163 << "(" << it.get_curr() << ")" << endl;
164 abort();
165 }
166
167 return true;
168}
169
170template <class L>
171void print_list(const L & l)
172{
173 for (typename L::Iterator it(l); it.has_curr(); it.next())
174 cout << it.get_curr() << " ";
175 cout << endl;
176}
177
178template <class L>
180{
181 cout << "R value ctor test" << endl;
182 L l = create_list <L> ();
183 check_list(l);
184 cout << "done" << endl << endl;
185
186 {
187 cout << "L value ctor test" << endl;
188 L ll = l;
189 check_list(ll);
190 cout << "done" << endl << endl;
191 }
192
193 {
194 cout << "L value = test" << endl;
195 L ll1;
196 ll1 = l;
198 cout << "done" << endl << endl;
199 }
200
201 cout << "R value = test" << endl;
202 l = create_list <L> ();
203 check_list(l);
204 cout << "done" << endl << endl;
205
206 {
207 cout << "R value list append test" << endl;
208 l.append(create_list <DynList<>> (V, 2*V - 1));
209 check_list(l);
210 cout << endl;
211 }
212
213 {
214 cout << "R value list insert test" << endl;
215 l.insert(create_list <DynList<>> (-V, -1));
216 check_list(l);
217 cout << endl;
218 }
219
220 {
221 cout << "L value list append test" << endl;
222 L ll = create_list <DynList<>> (2*V, 3*V - 1);
223 l.append(ll);
224 check_list(l);
225 cout << endl;
226 }
227 print_list(l);
228 {
229 cout << "L value list insert test" << endl;
230 L ll = create_list <DynList<>> (-2*V-1, -V - 1);
231 l.insert(ll);
232 print_list(l);
233 check_list(l);
234 cout << endl;
235 }
236}
237
238
239template <class Tree>
240void test_map_tree(size_t n)
241{
242 cout << "Probando con contenedor tipo arbol" << endl;
243
244 typedef void (*Print)(Tree & t);
245 Print print = [/* Lambda */] (Tree & t)
246 {
247 t.for_each([/* Lambda */] (const std::pair<int, int> & p)
248 {
249 cout << p.first << "," << p.second << " ";
250 });
251 } ;
252
253 Tree (*create_tree)(int) = [/* Lambda */] (int n) -> Tree
254 {
255 Tree t;
256 for (int i = 0; i < n; ++i)
257 t.insert(i, i+1);
258 return t;
259 };
260
261
262 Tree tree;
263 for (int i = 0; i < (int) n; ++i)
264 tree.insert(i, i);
265
266 Tree t1 = tree;
267
268 Tree t2 = (*create_tree)(n);
269
270 t2 = (*create_tree)(2*n);
271
272 print(t2) ;
273
274 t1 = t2;
275
276 print(t1);
277
278 cout << endl;
279
280 cout << "Probando diferentes combinaciones de insert\n"
281 << endl
282 << "L val L val\n";
283
284 Tree tt;
285 int i = n + 1, j = n + 2;
286 tt.insert(i, j);
287
288 cout << "\n\nL val R val\n";
289 i++;
290 tt.insert(i, j + 1);
291
292 cout << "\n\nR val L val\n";
293 tt.insert(i + 3, j);
294
295 cout << "\n\nR val R val\n";
296 tt.insert(i + 6, j + 7);
297
298 cout << endl << endl;
299 (*print)(tt); cout << endl;
300}
301
302int main(int argc, char *argv[])
303{
304 if (argc > 1)
305 {
306 try
307 {
308 const int tmp = std::stoi(argv[1]);
309 if (tmp <= 0)
310 {
311 std::cout << "V must be positive" << std::endl;
312 return 1;
313 }
314 V = static_cast<size_t>(tmp);
315 }
316 catch (const std::exception & e)
317 {
318 std::cerr << "Error parsing V: " << e.what() << std::endl;
319 return 1;
320 }
321 }
322
323 if (V == 0) // Should not happen with default V=1000 and above check
324 {
325 cout << "V must be positive" << endl;
326 return 1;
327 }
328
330
331 //return 0;
332
333 cout << "Testing DynList" << endl;
335 cout << endl;
336
337 cout << "Testing List_Graph" << endl;
339 cout << endl;
340
341 cout << "Testing List_Digraph" << endl;
343 cout << endl;
344
345 cout << "Testing List_SGraph" << endl;
347 cout << endl;
348
349 cout << "Testing List_SDigraph" << endl;
351 cout << endl;
352
353 cout << "Testing Array_Graph" << endl;
355 cout << endl;
356
357 cout << "Testing Array_Digraph" << endl;
359 cout << endl;
360
361}
int main()
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
T & insert(const T &item)
Definition htlist.H:1220
T & append(const T &item)
Definition htlist.H:1271
T & get_first() const
Return the first item of the list.
Definition htlist.H:1375
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:525
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:605
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:637
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:536
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Definition quadtree.H:281
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
@ Tree
Basic arc (in spanning tree).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
void print(const DynList< Parc< Net > > &sp)
Print a semi-path to stdout.
Definition tpl_net.H:887
STL namespace.
void test_map_tree(size_t n)
GT create_graph()
bool check_list(const L &l)
void test_list()
void test()
bool check(GT &g)
L create_list(int beg=0, int end=V - 1)
size_t V
void print_list(const L &l)
Array-based graph implementation.
Dynamic key-value map based on balanced binary search trees.
DynList< int > l