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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <iostream>
28# include <string>
29# include <tpl_agraph.H>
30# include <tpl_dynMapTree.H>
31
32using namespace std;
33
34size_t V = 1000;
35
36template <class GT>
38{
39 GT g;
41 for (int i = 0; i < (int) V; ++i)
42 nodes(i) = g.insert_node(i);
43
44 for (int i = 0; i < (int) V - 1; ++i)
45 {
46 typename GT::Node * src = nodes(i);
47 for (int j = i + 1; j < (int) V; ++j)
48 g.insert_arc(src, nodes(j), i + j);
49 }
50
51 return g;
52}
53
54template <class GT>
55bool check(GT & g)
56{
58 for (size_t i = 0; i < V; ++i)
59 seen(i) = false;
60
61 size_t count = 0;
62 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next())
63 {
64 const auto info = it.get_curr()->get_info();
65 if (info >= V)
66 {
67 cout << "Inconsistencia en el nodo " << info << endl;
68 abort();
69 }
70
71 if (seen(info))
72 {
73 cout << "Nodo duplicado " << info << endl;
74 abort();
75 }
76
77 seen(info) = true;
78 ++count;
79 }
80
81 for (size_t i = 0; i < V; ++i)
82 if (not seen(i))
83 {
84 cout << "Falta el nodo " << i << endl;
85 abort();
86 }
87
88 if (count != V)
89 {
90 cout << "Cantidad incorrecta de nodos " << count << endl;
91 abort();
92 }
93
94 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next())
95 {
96 typename GT::Arc * a = it.get_curr();
97 typename GT::Node * src = g.get_src_node(a);
98 typename GT::Node * tgt = g.get_tgt_node(a);
99 if (a->get_info() != src->get_info() + tgt->get_info())
100 {
101 cout << "Inconsistencia en el arco " << a->get_info() << endl;
102 abort();
103 }
104 }
105
106 return true;
107}
108
109template <class GT>
110void test()
111{
112 cout << "R value ctor test" << endl;
114 check(lg);
115 cout << "done" << endl << endl;
116
117 {
118 cout << "L value ctor test" << endl;
119 GT ng = lg;
120 check(ng);
121 cout << "done" << endl << endl;
122 }
123
124 {
125 cout << "L value = test" << endl;
126 GT lg1;
127 lg1 = lg;
128 check(lg1);
129 cout << "done" << endl << endl;
130 }
131
132 cout << "R value = test" << endl;
134 check(lg);
135 cout << "done" << endl << endl;
136}
137
138
139template <class L>
140L create_list(int beg = 0, int end = V - 1)
141{
142 L l;
143 for (int i = beg; i <= end; ++i)
144 l.append(i);
145
146 return l;
147}
148
149
150template <class L>
151bool check_list(const L & l)
152{
153 int i = l.get_first();
154 for (typename L::Iterator it(l); it.has_curr(); it.next())
155 if (it.get_curr() != i++)
156 {
157 cout << "Inconsistencia en el nodo " << i - 1
158 << "(" << it.get_curr() << ")" << endl;
159 abort();
160 }
161
162 return true;
163}
164
165template <class L>
166void print_list(const L & l)
167{
168 for (typename L::Iterator it(l); it.has_curr(); it.next())
169 cout << it.get_curr() << " ";
170 cout << endl;
171}
172
173template <class L>
175{
176 cout << "R value ctor test" << endl;
177 L l = create_list <L> ();
178 check_list(l);
179 cout << "done" << endl << endl;
180
181 {
182 cout << "L value ctor test" << endl;
183 L ll = l;
184 check_list(ll);
185 cout << "done" << endl << endl;
186 }
187
188 {
189 cout << "L value = test" << endl;
190 L ll1;
191 ll1 = l;
193 cout << "done" << endl << endl;
194 }
195
196 cout << "R value = test" << endl;
197 l = create_list <L> ();
198 check_list(l);
199 cout << "done" << endl << endl;
200
201 {
202 cout << "R value list append test" << endl;
203 l.append(create_list <DynList<>> (V, 2*V - 1));
204 check_list(l);
205 cout << endl;
206 }
207
208 {
209 cout << "R value list insert test" << endl;
210 l.insert(create_list <DynList<>> (-V, -1));
211 check_list(l);
212 cout << endl;
213 }
214
215 {
216 cout << "L value list append test" << endl;
217 L ll = create_list <DynList<>> (2*V, 3*V - 1);
218 l.append(ll);
219 check_list(l);
220 cout << endl;
221 }
222 print_list(l);
223 {
224 cout << "L value list insert test" << endl;
225 L ll = create_list <DynList<>> (-2*V-1, -V - 1);
226 l.insert(ll);
227 print_list(l);
228 check_list(l);
229 cout << endl;
230 }
231}
232
233
234template <class Tree>
235void test_map_tree(size_t n)
236{
237 cout << "Probando con contenedor tipo arbol" << endl;
238
239 typedef void (*Print)(Tree & t);
240 Print print = [/* Lambda */] (Tree & t)
241 {
242 t.for_each([/* Lambda */] (const std::pair<int, int> & p)
243 {
244 cout << p.first << "," << p.second << " ";
245 });
246 } ;
247
248 Tree (*create_tree)(int) = [/* Lambda */] (int n) -> Tree
249 {
250 Tree t;
251 for (int i = 0; i < n; ++i)
252 t.insert(i, i+1);
253 return t;
254 };
255
256
257 Tree tree;
258 for (int i = 0; i < (int) n; ++i)
259 tree.insert(i, i);
260
261 Tree t1 = tree;
262
263 Tree t2 = (*create_tree)(n);
264
265 t2 = (*create_tree)(2*n);
266
267 print(t2) ;
268
269 t1 = t2;
270
271 print(t1);
272
273 cout << endl;
274
275 cout << "Probando diferentes combinaciones de insert\n"
276 << endl
277 << "L val L val\n";
278
279 Tree tt;
280 int i = n + 1, j = n + 2;
281 tt.insert(i, j);
282
283 cout << "\n\nL val R val\n";
284 i++;
285 tt.insert(i, j + 1);
286
287 cout << "\n\nR val L val\n";
288 tt.insert(i + 3, j);
289
290 cout << "\n\nR val R val\n";
291 tt.insert(i + 6, j + 7);
292
293 cout << endl << endl;
294 (*print)(tt); cout << endl;
295}
296
297int main(int argc, char *argv[])
298{
299 if (argc > 1)
300 {
301 try
302 {
303 const int tmp = std::stoi(argv[1]);
304 if (tmp <= 0)
305 {
306 std::cout << "V must be positive" << std::endl;
307 return 1;
308 }
309 V = static_cast<size_t>(tmp);
310 }
311 catch (const std::exception & e)
312 {
313 std::cerr << "Error parsing V: " << e.what() << std::endl;
314 return 1;
315 }
316 }
317
318 if (V == 0) // Should not happen with default V=1000 and above check
319 {
320 cout << "V must be positive" << endl;
321 return 1;
322 }
323
325
326 //return 0;
327
328 cout << "Testing DynList" << endl;
330 cout << endl;
331
332 cout << "Testing List_Graph" << endl;
334 cout << endl;
335
336 cout << "Testing List_Digraph" << endl;
338 cout << endl;
339
340 cout << "Testing List_SGraph" << endl;
342 cout << endl;
343
344 cout << "Testing List_SDigraph" << endl;
346 cout << endl;
347
348 cout << "Testing Array_Graph" << endl;
350 cout << endl;
351
352 cout << "Testing Array_Digraph" << endl;
354 cout << endl;
355
356}
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Dynamic singly linked list with functional programming support.
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:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:737
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:743
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Definition quadtree.H:237
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
@ 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:888
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