Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test-dry.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
33# include <iostream>
34# include <vector>
35# include <ahSort.H>
36# include <htlist.H>
37# include <tpl_arrayHeap.H>
38# include <tpl_dynArrayHeap.H>
39# include <tpl_dynBinHeap.H>
40# include <tpl_dynDlist.H>
41# include <tpl_dynSetTree.H>
42# include <tpl_olhash.H>
43# include <tpl_odhash.H>
44# include <tpl_dynSetHash.H>
45# include <tpl_dynArray.H>
46# include <tpl_arrayQueue.H>
47# include <tpl_dynListStack.H>
48# include <tpl_dynarray_set.H>
49# include <tpl_random_queue.H>
50
51# include <cassert>
52using namespace std;
53
54# include <cassert>
55using namespace Aleph;
56
57template <class C>
59{
60 C c = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
61
62 const C a = c;
63
64 c.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
65
66 a.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
67
68 assert(c.all([] (auto i) { return i >= 0; }));
69 assert(a.all([] (auto i) { return i >= 0; }));
70
71 int vals[11]; int k = 0;
72 c.for_each([&k, &vals] (int i) { vals[k++] = i; });
73
74 c.for_each([&c, vals] (int i) { assert(c.nth_ne(i) == vals[i]); });
75
76 k = 0;
77 a.for_each([&k, &vals] (int i) { vals[k++] = i; });
78 a.for_each([&a, vals] (int i) { assert(a.nth_ne(i) == vals[i]); });
79
80 assert(c.find_ptr([] (int i) { return i == 5; }));
81 assert(a.find_ptr([] (int i) { return i == 5; }));
82 assert(not c.find_ptr([] (int i) { return i == 15; }));
83 assert(not a.find_ptr([] (int i) { return i == 15; }));
84 assert(get<0>(c.find_item([] (int i) { return i == 5; })));
85 assert(get<0>(c.find_item([] (int i) { return i == 5; })) and
86 get<1>(c.find_item([] (int i) { return i == 5; })) == 5);
87 assert(get<0>(a.find_item([] (int i) { return i == 5; })));
88 assert(get<0>(a.find_item([] (int i) { return i == 5; })) and
89 get<1>(a.find_item([] (int i) { return i == 5; })) == 5);
90
91}
92
93template <class C>
95{
96 C c = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
97 const C a = c;
98 c.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
99 a.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
100
101 C c1 = DynList<typename C::Item_Type>({ 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 });
102 c1.for_each([] (int i) { cout << " " << i; }); cout << endl;
103
104 const C c2 = DynList<typename C::Item_Type>({ 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 });
105 c2.for_each([] (int i) { cout << " " << i; }); cout << endl;
106
107 vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
108
109 C c3(v.begin(), v.end());
110 c3.for_each([] (int i) { cout << " " << i; }); cout << endl;
111
112 const C c4(v.begin(), v.end());
113 c4.for_each([] (int i) { cout << " " << i; }); cout << endl;
114}
115
116template <class C>
118{
119 C c = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
120 const C a = c;
121
122 c.for_each([] (int i) { cout << " " << i; }); cout << endl;
123 a.for_each([] (int i) { cout << " " << i; }); cout << endl;
124
125 assert(c.all([&a] (int i) { return a.exists([i] (int k)
126 { return k == i; }); }));
127 assert(a.all([&c] (int i) { return c.exists([i] (int k)
128 { return k == i; }); }));
129
130 assert(c.exists([] (int i) { return i == 9; }));
131 assert(a.exists([] (int i) { return i == 9; }));
132
133 assert(c.all([&c] (int i)
134 { return c.exists([i] (int k) { return i == k; }); }));
135 assert(a.all([&a] (int i)
136 { return a.exists([i] (int k) { return i == k; }); }));
137
138 C cm = c.maps([] (int i) { return 10*i; });
139 assert(cm.all([&c] (int k)
140 {
141 return c.exists([k] (int i) { return 10*i == k; });
142 }));
143
144 const C ccm = a.maps([] (int i) { return 10*i; });
145 assert(ccm.all([a] (int k)
146 {
147 return a.exists([k] (int i) { return 10*i == k; });
148 }));
149
150 {
151 auto m = c.template maps<string>([] (int i) { return to_string(i); });
152 }
153
154 cout << "S1 = "
155 << c.template foldl<int>(0, [] (auto a, auto i) { return a + i; })
156 << endl
157 << "S2 = "
158 << ccm.template foldl<int>(0, [] (int a, int i) { return a + i; })
159 << endl
160 << "S3 = " << c.fold(0, [] (auto a, auto i) { return a + i; })
161 << endl
162 << "S4 = " << a.fold(0, [] (auto a, auto i) { return a + i; })
163 << endl
164 << endl;
165
166 assert(eq(build_dynlist<int>(0, 1, 2, 3, 4, 5),
167 sort(c.filter([] (int i) { return i < 6; }))));
168 assert(eq(build_dynlist<int>(0, 1, 2, 3, 4, 5),
169 sort(a.filter([] (int i) { return i < 6; }))));
170
171 c.pfilter([] (int i) { return i < 6; }).for_each([] (auto p)
172 {
173 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
174 });
175 cout << endl;
176 a.pfilter([] (int i) { return i < 6; }).for_each([] (auto p)
177 {
178 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
179 });
180 cout << endl
181 << endl;
182
183 auto cmp_tup = [] (std::tuple<size_t,size_t> t1, std::tuple<size_t,size_t> t2)
184 {
185 return get<0>(t1) < get<0>(t2);
186 };
187
188 auto l1 = sort(c.pfilter([] (int i) { return i < 6; }), cmp_tup);
189 auto l2 = sort(a.pfilter([] (int i) { return i < 6; }), cmp_tup);
190
191 l1.for_each([] (auto p)
192 {
193 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
194 });
195 cout << endl;
196 l2.for_each([] (auto p)
197 {
198 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
199 });
200 cout << endl
201 << endl;
202
203 auto eq_tup = [] (std::tuple<size_t,size_t> t1, std::tuple<size_t,size_t> t2)
204 {
205 return get<0>(t1) == get<0>(t2);
206 };
207 assert(eq(l1 ,l2, eq_tup));
208
209 auto p = c.partition([] (int i) { return i < 6; });
210 assert(eq(sort(p.first), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
211 eq(sort(p.second), build_dynlist<int>(6, 7, 8, 9)));
212 p = a.partition([] (int i) { return i < 6; });
213 assert(eq(sort(p.first), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
214 eq(sort(p.second), build_dynlist<int>(6, 7, 8, 9)));
215
216 auto t = c.tpartition([] (int i) { return i < 6; });
217 assert(eq(sort(get<0>(t)), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
218 eq(sort(get<1>(t)), build_dynlist<int>(6, 7, 8, 9)));
219 t = a.tpartition([] (int i) { return i < 6; });
220 assert(eq(sort(get<0>(t)), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
221 eq(sort(get<1>(t)), build_dynlist<int>(6, 7, 8, 9)));
222
223 assert(c.length() == 10);
224 assert(a.length() == 10);
225
226 c.take(3).for_each([] (auto i) { cout << i << " "; });
227 cout << endl;
228 a.take(3).for_each([] (auto i) { cout << i << " "; });
229 cout << endl
230 << endl;
231
232 auto cc = c;
233 auto ca = a;
234
235 c.take(3).for_each([] (auto i) { cout << i << " "; });
236 cout << endl;
237 cc.take(3).for_each([] (auto i) { cout << i << " "; });
238 cout << endl
239 << endl;
240
241 assert(eq(sort(join(c.take(3), c.drop(3))), sort(c.items())));
242 assert(eq(sort(join(a.take(3), a.drop(3))), sort(a.items())));
243
244 cout << "All test were passed!" << endl
245 << endl
246 << endl;
247}
248
249template <class C>
250void tests()
251{
252 cout << "Testing for " << typeid(C).name() << endl
253 << endl;
254
255 find_test<C>();
258
259 cout << "Ended tests for " << typeid(C).name() << endl
260 << endl;
261}
262
287
288
289
290
High-level sorting functions for Aleph containers.
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
T fold(const T &init, Operation &operation) const
Simplified version of foldl() where the folded type is the same type of elements stored in the contai...
Definition ah-dry.H:1383
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:796
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
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
and
Check uniqueness with explicit hash + equality functors.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
Definition ahAlgo.H:76
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
STL namespace.
void functional_test()
Definition test-dry.C:117
void ctors_test()
Definition test-dry.C:94
void find_test()
Definition test-dry.C:58
int main()
Definition test-dry.C:263
void tests()
Definition test-dry.C:250
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
DynList< int > l1
DynList< int > l2
static int * k
Fixed-capacity binary heap and heapsort algorithms.
Circular queue implementations backed by arrays.
Array-based dynamic binary heap.
Lazy and scalable dynamic array implementation.
Dynamic binary heap with node-based storage.
Dynamic doubly linked list implementation.
Dynamic stack implementation based on linked lists.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Array-based dynamic set.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.
Random access queue (bag) with O(1) random pop.