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 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
28# include <iostream>
29# include <vector>
30# include <ahSort.H>
31# include <htlist.H>
32# include <tpl_arrayHeap.H>
33# include <tpl_dynArrayHeap.H>
34# include <tpl_dynBinHeap.H>
35# include <tpl_dynDlist.H>
36# include <tpl_dynSetTree.H>
37# include <tpl_olhash.H>
38# include <tpl_odhash.H>
39# include <tpl_dynSetHash.H>
40# include <tpl_dynArray.H>
41# include <tpl_arrayQueue.H>
42# include <tpl_dynListStack.H>
43# include <tpl_dynarray_set.H>
44# include <tpl_random_queue.H>
45
46# include <cassert>
47using namespace std;
48
49# include <cassert>
50using namespace Aleph;
51
52template <class C>
54{
55 C c = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
56
57 const C a = c;
58
59 c.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
60
61 a.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
62
63 assert(c.all([] (auto i) { return i >= 0; }));
64 assert(a.all([] (auto i) { return i >= 0; }));
65
66 int vals[11]; int k = 0;
67 c.for_each([&k, &vals] (int i) { vals[k++] = i; });
68
69 c.for_each([&c, vals] (int i) { assert(c.nth_ne(i) == vals[i]); });
70
71 k = 0;
72 a.for_each([&k, &vals] (int i) { vals[k++] = i; });
73 a.for_each([&a, vals] (int i) { assert(a.nth_ne(i) == vals[i]); });
74
75 assert(c.find_ptr([] (int i) { return i == 5; }));
76 assert(a.find_ptr([] (int i) { return i == 5; }));
77 assert(not c.find_ptr([] (int i) { return i == 15; }));
78 assert(not a.find_ptr([] (int i) { return i == 15; }));
79 assert(get<0>(c.find_item([] (int i) { return i == 5; })));
80 assert(get<0>(c.find_item([] (int i) { return i == 5; })) and
81 get<1>(c.find_item([] (int i) { return i == 5; })) == 5);
82 assert(get<0>(a.find_item([] (int i) { return i == 5; })));
83 assert(get<0>(a.find_item([] (int i) { return i == 5; })) and
84 get<1>(a.find_item([] (int i) { return i == 5; })) == 5);
85
86}
87
88template <class C>
90{
91 C c = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
92 const C a = c;
93 c.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
94 a.traverse([] (auto i) { cout << " " << i; return true; }); cout << endl;
95
96 C c1 = DynList<typename C::Item_Type>({ 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 });
97 c1.for_each([] (int i) { cout << " " << i; }); cout << endl;
98
99 const C c2 = DynList<typename C::Item_Type>({ 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 });
100 c2.for_each([] (int i) { cout << " " << i; }); cout << endl;
101
102 vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
103
104 C c3(v.begin(), v.end());
105 c3.for_each([] (int i) { cout << " " << i; }); cout << endl;
106
107 const C c4(v.begin(), v.end());
108 c4.for_each([] (int i) { cout << " " << i; }); cout << endl;
109}
110
111template <class C>
113{
114 C c = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
115 const C a = c;
116
117 c.for_each([] (int i) { cout << " " << i; }); cout << endl;
118 a.for_each([] (int i) { cout << " " << i; }); cout << endl;
119
120 assert(c.all([&a] (int i) { return a.exists([i] (int k)
121 { return k == i; }); }));
122 assert(a.all([&c] (int i) { return c.exists([i] (int k)
123 { return k == i; }); }));
124
125 assert(c.exists([] (int i) { return i == 9; }));
126 assert(a.exists([] (int i) { return i == 9; }));
127
128 assert(c.all([&c] (int i)
129 { return c.exists([i] (int k) { return i == k; }); }));
130 assert(a.all([&a] (int i)
131 { return a.exists([i] (int k) { return i == k; }); }));
132
133 C cm = c.maps([] (int i) { return 10*i; });
134 assert(cm.all([&c] (int k)
135 {
136 return c.exists([k] (int i) { return 10*i == k; });
137 }));
138
139 const C ccm = a.maps([] (int i) { return 10*i; });
140 assert(ccm.all([a] (int k)
141 {
142 return a.exists([k] (int i) { return 10*i == k; });
143 }));
144
145 {
146 auto m = c.template maps<string>([] (int i) { return to_string(i); });
147 }
148
149 cout << "S1 = "
150 << c.template foldl<int>(0, [] (auto a, auto i) { return a + i; })
151 << endl
152 << "S2 = "
153 << ccm.template foldl<int>(0, [] (int a, int i) { return a + i; })
154 << endl
155 << "S3 = " << c.fold(0, [] (auto a, auto i) { return a + i; })
156 << endl
157 << "S4 = " << a.fold(0, [] (auto a, auto i) { return a + i; })
158 << endl
159 << endl;
160
161 assert(eq(build_dynlist<int>(0, 1, 2, 3, 4, 5),
162 sort(c.filter([] (int i) { return i < 6; }))));
163 assert(eq(build_dynlist<int>(0, 1, 2, 3, 4, 5),
164 sort(a.filter([] (int i) { return i < 6; }))));
165
166 c.pfilter([] (int i) { return i < 6; }).for_each([] (auto p)
167 {
168 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
169 });
170 cout << endl;
171 a.pfilter([] (int i) { return i < 6; }).for_each([] (auto p)
172 {
173 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
174 });
175 cout << endl
176 << endl;
177
178 auto cmp_tup = [] (std::tuple<size_t,size_t> t1, std::tuple<size_t,size_t> t2)
179 {
180 return get<0>(t1) < get<0>(t2);
181 };
182
183 auto l1 = sort(c.pfilter([] (int i) { return i < 6; }), cmp_tup);
184 auto l2 = sort(a.pfilter([] (int i) { return i < 6; }), cmp_tup);
185
186 l1.for_each([] (auto p)
187 {
188 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
189 });
190 cout << endl;
191 l2.for_each([] (auto p)
192 {
193 cout << "(" << get<0>(p) << "," << get<1>(p) << ")";
194 });
195 cout << endl
196 << endl;
197
198 auto eq_tup = [] (std::tuple<size_t,size_t> t1, std::tuple<size_t,size_t> t2)
199 {
200 return get<0>(t1) == get<0>(t2);
201 };
202 assert(eq(l1 ,l2, eq_tup));
203
204 auto p = c.partition([] (int i) { return i < 6; });
205 assert(eq(sort(p.first), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
206 eq(sort(p.second), build_dynlist<int>(6, 7, 8, 9)));
207 p = a.partition([] (int i) { return i < 6; });
208 assert(eq(sort(p.first), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
209 eq(sort(p.second), build_dynlist<int>(6, 7, 8, 9)));
210
211 auto t = c.tpartition([] (int i) { return i < 6; });
212 assert(eq(sort(get<0>(t)), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
213 eq(sort(get<1>(t)), build_dynlist<int>(6, 7, 8, 9)));
214 t = a.tpartition([] (int i) { return i < 6; });
215 assert(eq(sort(get<0>(t)), build_dynlist<int>(0, 1, 2, 3, 4, 5)) and
216 eq(sort(get<1>(t)), build_dynlist<int>(6, 7, 8, 9)));
217
218 assert(c.length() == 10);
219 assert(a.length() == 10);
220
221 c.take(3).for_each([] (auto i) { cout << i << " "; });
222 cout << endl;
223 a.take(3).for_each([] (auto i) { cout << i << " "; });
224 cout << endl
225 << endl;
226
227 auto cc = c;
228 auto ca = a;
229
230 c.take(3).for_each([] (auto i) { cout << i << " "; });
231 cout << endl;
232 cc.take(3).for_each([] (auto i) { cout << i << " "; });
233 cout << endl
234 << endl;
235
236 assert(eq(sort(join(c.take(3), c.drop(3))), sort(c.items())));
237 assert(eq(sort(join(a.take(3), a.drop(3))), sort(a.items())));
238
239 cout << "All test were passed!" << endl
240 << endl
241 << endl;
242}
243
244template <class C>
245void tests()
246{
247 cout << "Testing for " << typeid(C).name() << endl
248 << endl;
249
250 find_test<C>();
253
254 cout << "Ended tests for " << typeid(C).name() << endl
255 << endl;
256}
257
282
283
284
285
High-level sorting functions for Aleph containers.
Dynamic doubly linked list with O(1) size and bidirectional access.
Dynamic singly linked list with functional programming support.
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:1267
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:779
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
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.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:227
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:112
void ctors_test()
Definition test-dry.C:89
void find_test()
Definition test-dry.C:53
int main()
Definition test-dry.C:258
void tests()
Definition test-dry.C:245
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.