Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-dry.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38# include <gtest/gtest.h>
39
40# include <vector>
41# include <ah-zip.H>
42# include <ahFunctional.H>
43# include <ah-string-utils.H>
44# include <ahSort.H>
45# include <htlist.H>
46# include <tpl_arrayHeap.H>
47# include <tpl_dynArrayHeap.H>
48# include <tpl_dynBinHeap.H>
49# include <tpl_dynDlist.H>
50# include <tpl_dynSetTree.H>
51# include <tpl_hash.H>
52# include <tpl_dynSetHash.H>
53# include <tpl_dynArray.H>
54# include <tpl_arrayQueue.H>
55# include <tpl_dynListStack.H>
56# include <tpl_dynarray_set.H>
57# include <tpl_random_queue.H>
58
59using namespace std;
60using namespace testing;
61using namespace Aleph;
62
63template <class Ctype>
64struct Container : public testing::Test
65{
66 static constexpr size_t N = 1000;
70 {
71 for (size_t i = 0; i < N; ++i)
72 {
73 c.append(i);
75 }
77 }
78};
79
81
83{
84 auto N = this->N;
85 TypeParam c = this->c;
86 EXPECT_EQ(c.size(), N);
88 EXPECT_TRUE(c.traverse([&l] (auto & k) { l.append(k); return true; }));
89 EXPECT_TRUE(zip_all([] (auto t) { return get<0>(t) == get<1>(t); },
90 this->item_list, sort(l)));
91}
92
94{
95 auto N = this->N;
96 TypeParam c = this->c;
97 EXPECT_EQ(c.size(), N);
99 c.for_each([&l] (auto & k) { l.append(k); });
100 EXPECT_TRUE(zip_all([] (auto t) { return get<0>(t) == get<1>(t); },
101 this->item_list, sort(l)));
102}
103
105{
106 auto N = this->N;
107 TypeParam c = this->c;
108 EXPECT_EQ(c.size(), N);
109 auto ptr = c.find_ptr([N] (auto & k) { return k == int(N); });
110 EXPECT_EQ(ptr, nullptr);
111 this->item_list.for_each([&c] (auto & k)
112 {
113 auto ptr = c.find_ptr([k] (auto i) { return k == i; });
114 ASSERT_NE(ptr, nullptr);
115 ASSERT_EQ(*ptr, k);
116 });
117}
118
120{
121 auto N = this->N;
122 TypeParam c = this->c;
123 EXPECT_EQ(c.size(), N);
124
125 auto idx = c.find_index([N] (auto & k) { return k == int(N); });
126 ASSERT_EQ(idx, N);
127
128 this->item_list.for_each([&c] (auto & k)
129 {
130 auto idx = c.find_index([k] (auto i) { return k == i; });
131 ASSERT_EQ(c.nth(idx), k);
132 });
133}
134
136{
137 auto N = this->N;
138 TypeParam c = this->c;
139 EXPECT_EQ(c.size(), N);
140
141 auto t = c.find_item([N] (auto & k) { return k == int(N); });
143
144 this->item_list.for_each([&c] (auto & k)
145 {
146 auto t = c.find_item([k] (auto i) { return k == i; });
148 ASSERT_EQ(get<1>(t), k);
149 });
150}
151
153{
154 //auto N = this->N;
155 auto c = this->c;
156 const DynList<int> l = to_dynlist(c); // in the same order than iterator
157 const std::vector<int> v = c.to_vector(); // test to_vector method
158 const DynList<int> l2 = c.to_dynlist(); // test to_dynlist method
159
160 ASSERT_EQ(l.size(), c.size());
161 ASSERT_EQ(v.size(), c.size());
162 ASSERT_EQ(l2.size(), c.size());
163
164 // Verify to_vector and to_dynlist produce same content
165 size_t idx = 0;
166 l2.for_each([&v, &idx](int x) {
167 EXPECT_EQ(x, v[idx++]);
168 });
169
170 auto itl = l.get_it();
171 for (auto & item : c)
172 {
173 ASSERT_EQ(item, itl.get_curr_ne());
174 itl.next_ne();
175 }
176 auto it = c.get_it();
177 EXPECT_EQ(it.get_curr_ne(), l.get_first());
178 it.reset_last();
179 EXPECT_EQ(it.get_curr_ne(), l.get_last());
180 it.reset_first();
181 EXPECT_EQ(it.get_curr_ne(), l.get_first());
182 it.reset_last();
183 EXPECT_EQ(it.get_curr_ne(), l.get_last());
184}
185
187{
188 int N = this->N;
189 auto c = this->c;
190
191 c.nappend(N);
192 auto ptr = c.find_ptr([N] (auto i) { return i == N; });
193 EXPECT_EQ(c.size(), N + 1);
194 ASSERT_NE(ptr, nullptr);
195 EXPECT_EQ(*ptr, N);
196
197 c.nappend(N + 1, N + 2, N + 3);
198 EXPECT_EQ(c.size(), N + 4);
199
200 ptr = c.find_ptr([N] (auto i) { return i == N + 1; });
201 ASSERT_NE(ptr, nullptr);
202 EXPECT_EQ(*ptr, N + 1);
203
204 ptr = c.find_ptr([N] (auto i) { return i == N + 2; });
205 ASSERT_NE(ptr, nullptr);
206 EXPECT_EQ(*ptr, N + 2);
207
208 ptr = c.find_ptr([N] (auto i) { return i == N + 3; });
209 ASSERT_NE(ptr, nullptr);
210 EXPECT_EQ(*ptr, N + 3);
211}
212
214{
215 int N = this->N;
216 auto c = this->c;
217
218 c.ninsert(N);
219 auto ptr = c.find_ptr([N] (auto i) { return i == N; });
220 EXPECT_EQ(c.size(), N + 1);
221 ASSERT_NE(ptr, nullptr);
222 EXPECT_EQ(*ptr, N);
223
224 c.ninsert(N + 1, N + 2, N + 3);
225 EXPECT_EQ(c.size(), N + 4);
226
227 ptr = c.find_ptr([N] (auto i) { return i == N + 1; });
228 ASSERT_NE(ptr, nullptr);
229 EXPECT_EQ(*ptr, N + 1);
230
231 ptr = c.find_ptr([N] (auto i) { return i == N + 2; });
232 ASSERT_NE(ptr, nullptr);
233 EXPECT_EQ(*ptr, N + 2);
234
235 ptr = c.find_ptr([N] (auto i) { return i == N + 3; });
236 ASSERT_NE(ptr, nullptr);
237 EXPECT_EQ(*ptr, N + 3);
238}
239
241{
242 int N = this->N;
243 auto c = this->c;
245 ASSERT_TRUE(c.all([&tbl] (auto i)
246 {
247 const bool ret = tbl.contains(i);
248 tbl.insert(i);
249 return not ret;
250 }));
251 EXPECT_EQ(tbl.size(), N);
252 EXPECT_EQ(sort(to_dynlist(c)), tbl.keys());
253}
254
256{
257 int N = this->N;
258 auto c = this->c;
259 auto & l = this->item_list;
260 EXPECT_TRUE(l.all([&c] (auto & i)
261 { return c.exists([i] (auto k) { return i == k; }); }));
262 EXPECT_FALSE(c.exists([N] (auto i) { return i == N; }));
263}
264
266{
267 auto c = this->c;
268 auto & l = this->item_list;
269 auto fct = [] (int i) { return i + 1; };
271 all([] (auto & p) { return p.first == p.second; }));
272 EXPECT_TRUE(zip(sort(to_dynlist(c.maps_if([] (auto i)
273 { return i < 7; }, fct))),
274 sort(l.template maps_if([] (auto i)
275 { return i < 7; }, fct))).
276 all([] (auto & p) { return p.first == p.second; }));
277}
278
280{
281 int N = this->N;
282 auto c = this->c;
283 auto sum = c.foldl(0, [] (auto & a, auto & i) { return a + i; });
284 EXPECT_EQ(sum, N*(N-1)/2);
285}
286
288{
289 auto fct = [] (int a, int i) { return a + i; };
290 auto c = this->c;
291 auto sum = c.filter([] (auto i) { return i < 8; }).foldl(0, fct);
292 EXPECT_EQ(sum, 28);
293
294 auto l = c.ptr_filter([] (auto & i) { return i < 8; });
295 sum = l.foldl(0, [] (auto a, auto ptr) { return a + *ptr; });
296 EXPECT_EQ(sum, 28);
297
298 int N = this->N;
299 auto total = N*(N-1)/2;
300 auto p = c.partition([] (auto & i) { return i < 8; });
301 auto S = p.first.foldl(0, fct) + p.second.foldl(0, fct);
302 EXPECT_EQ(S, total);
303
304 auto t = c.tpartition([] (auto & i) { return i < 8; });
305 S = get<0>(t).foldl(0, fct) + get<1>(t).foldl(0, fct);
306 EXPECT_EQ(S, total);
307
308 auto l1 = c.take(8);
309 auto l2 = c.drop(8);
310 S = l1.foldl(0, fct) + l2.foldl(0, fct);
311 EXPECT_EQ(S, total);
312
313 EXPECT_EQ(sort(c.to_dynlist()).take(8, 12),
314 build_dynlist<int>(8, 9, 10, 11, 12));
315}
316
319 nappend, ninsert, all, exists, maps, foldl,
320 filter_ops);
321
322typedef
333 >
335
337
338template <class C>
339struct CtorContainer : public ::testing::Test
340{
341 static constexpr size_t N = 10;
342 C * ptr_1 = nullptr;
343 C * ptr_2 = nullptr;
344 C * ptr_3 = nullptr;
346 {
347 ptr_1 = new C(range<int>(N));
348 ptr_2 = new C({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
349 ptr_3 = new C(ptr_1->begin(), ptr_1->end()); // Use same container for begin/end
350 }
352 {
353 delete ptr_1;
354 delete ptr_2;
355 delete ptr_3;
356 }
357};
358
360
362{
363 auto N = this->N;
364 auto ptr_1 = this->ptr_1;
365 auto ptr_2 = this->ptr_2;
366 auto ptr_3 = this->ptr_3;
367 EXPECT_EQ(ptr_1->size(), N);
368 EXPECT_EQ(ptr_2->size(), 10);
369 EXPECT_EQ(ptr_3->size(), 10);
370
371 auto l1 = to_dynlist(*ptr_1);
372 auto l2 = to_dynlist(*ptr_2);
373 auto l3 = to_dynlist(*ptr_3);
374
375 const auto r1 = range<int>(N);
376 const auto r2 = build_dynlist<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
377 const auto & r3 = r1;
378
379 ASSERT_EQ(sort(l1), r1);
380 ASSERT_EQ(sort(l2), r2);
381 ASSERT_EQ(sort(l3), r3);
382}
383
385
INSTANTIATE_TYPED_TEST_SUITE_P(traverses, Container, Ctypes)
REGISTER_TYPED_TEST_SUITE_P(Container, traverse, for_each, find_ptr, find_index_nth, find_item, iterator_operations, nappend, ninsert, all, exists, maps, foldl, filter_ops)
Types< DynList< int >, DynDlist< int >, DynArray< int >, HashSet< int, ODhashTable >, HashSet< int, OLhashTable >, DynHashTable< int, LhashTable >, DynHashTable< int, LinearHashTable >, DynSetHash< int >, DynSetTree< int, Treap >, DynSetTree< int, Treap_Rk >, DynSetTree< int, Rand_Tree >, DynSetTree< int, Splay_Tree >, DynSetTree< int, Avl_Tree >, DynSetTree< int, Rb_Tree >, Array< int >, ArrayQueue< int >, ArrayStack< int >, DynListQueue< int >, DynListStack< int >, DynArrayHeap< int >, DynBinHeap< int >, FixedQueue< int >, FixedStack< int > > Ctypes
Definition ah-dry.cc:334
TYPED_TEST_SUITE_P(Container)
TYPED_TEST_P(Container, traverse)
Definition ah-dry.cc:82
String manipulation utilities.
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
Queue implemented with a single dynamic array.
Stack implemented with simple dynamic array and with bounds verification.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
Dynamic heap (priority queue) backed by DynArray.
Dynamic heap of elements of type T ordered by a comparison functor.
Dynamic doubly linked list with O(1) size and bidirectional access.
Self-adjusting dynamic hash table.
Dynamic queue of elements of generic type T based on single linked list.
Dynamic stack of elements of generic type T based on a singly linked list.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
T & get_last() const
Return the last item of the list.
Definition htlist.H:1663
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
Dynamic set backed by balanced binary search trees with automatic memory management.
Very simple queue implemented with a contiguous array.
Fixed length stack.
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Definition ah-dry.H:1034
Aleph::DynList< T > to_dynlist() const
Convert container to DynList.
Definition ah-dry.H:968
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &op) const
Exclusive partition of container according to a filter criteria.
Definition ah-dry.H:1266
bool all(Operation &operation) const
Check if all the elements of container satisfy a condition.
Definition ah-dry.H:816
Aleph::DynList< const T * > ptr_filter(Operation &operation) const
Filter the elements of a container according to a matching criteria and return a pointer to the match...
Definition ah-dry.H:1181
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:685
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
#define N
Definition fib.C:294
MapOLhash< int, Foo > tbl
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool zip_all(Op &&op, const Cs &... cs)
Return true if op returns true for all tuples and containers have equal length.
Definition ah-zip.H:429
DynList< typename Container::Item_Type > to_dynlist(const Container &c)
Definition htlist.H:1988
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
bool traverse(Node *root, Op op)
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
T foldl(const Container &container, const T &init, Operation operation)
Classic left fold (reduce).
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
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
Container::Item_Type * find_ptr(Container &container, Pred &pred)
Find the first element satisfying pred.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
Container()
Definition ah-dry.cc:69
static constexpr size_t N
Definition ah-dry.cc:66
DynList< int > item_list
Definition ah-dry.cc:68
Ctype c
Definition ah-dry.cc:67
static constexpr size_t N
Definition ah-dry.cc:341
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.
Unified hash table interface.
Random access queue (bag) with O(1) random pop.
DynList< int > l