Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_container_concepts_test.cc
Go to the documentation of this file.
1#include <list>
2#include <map>
3#include <string>
4#include <vector>
5
6#include <gtest/gtest.h>
7
8#include <ah-stl-functional.H>
9#include <ah-stl-zip.H>
10#include <ah-uni-functional.H>
11#include <ah-zip-utils.H>
12#include <htlist.H>
13#include <tpl_array.H>
14#include <tpl_dynArray.H>
15#include <tpl_dynDlist.H>
16#include <tpl_dynMapTree.H>
17#include <tpl_dynSlist.H>
18#include <tpl_dynSetTree.H>
19#include <tpl_flat_set.H>
20#include <tpl_lhash.H>
21#include <tpl_memArray.H>
22
23// Canonical container/iterator hierarchy (Phase 3 of aleph-concepts-plan.md)
24// and the detectors that replaced the former void_t traits. The detector
25// values pinned here are the ones the void_t versions produced: they select
26// iteration strategies, so a change would silently switch behavior.
27
28using namespace Aleph;
29
30static_assert(AlephSequence<DynList<int>> and AlephSequence<DynDlist<int>>);
31static_assert(AlephSequence<Array<int>> and AlephSequence<DynArray<int>>);
32static_assert(AlephIterableContainer<DynMapTree<int, int>> and not AlephSequence<DynMapTree<int, int>>);
33static_assert(AlephTraversable<FlatSet<int>> and not AlephIterableContainer<FlatSet<int>>);
34static_assert(AlephIterator<DynList<int>::Iterator>);
35
36// get_curr_ne() gives what get_curr() gives, but for a derived iterator that
37// redefines get_curr() alone, as this one, whose get_curr_ne() is still that
38// of the nodes
39namespace
40{
41 struct Items_Of_Dnodes : Dnode<int>::Iterator
42 {
44
45 int & get_curr() const { return Dnode<int>::Iterator::get_curr()->get_data(); }
46 };
47}
48static_assert(AlephUncheckedIterator<DynList<int>::Iterator>
49 and AlephUncheckedIterator<DynDlist<int>::Iterator>
50 and AlephUncheckedIterator<Array<int>::Iterator>
51 and AlephUncheckedIterator<Dnode<int>::Iterator>);
52static_assert(AlephIterator<Items_Of_Dnodes>
53 and not AlephUncheckedIterator<Items_Of_Dnodes>);
54// These iterators offered next() but not next_ne() and so were rejected by
55// generic code written against AlephIterator.
56static_assert(AlephIterator<Slist<int>::Iterator> and AlephIterator<DynSlist<int>::Iterator>);
57static_assert(AlephIterator<decltype(uni_zip_it(std::declval<DynList<int> &>(),
58 std::declval<std::vector<int> &>()))>);
59static_assert(AlephIterator<decltype(stl_zip_it(std::declval<std::vector<int> &>(),
60 std::declval<std::list<int> &>()))>);
61static_assert(AlephIterator<StlEnumerateIterator<std::vector<int>>>);
62
63// AlephIterable: the single "iterate it the Aleph way" test of both functional
64// layers. The iterator is built from the container, so types without
65// get_it() qualify, and get_curr() need not be const (chained hash tables).
66static_assert(AlephIterable<DynList<int>> and AlephIterable<DynSetTree<int>>);
67static_assert(AlephIterable<MemArray<int>> and AlephIterable<LhashTable<int>>);
68static_assert(not AlephIterable<std::vector<int>> and not AlephIterable<FlatSet<int>>);
69static_assert(not AlephTraversable<std::vector<int>> and not AlephIterator<int>);
70
71static_assert(StlIterableContainer<std::vector<int>> and StlIterableContainer<std::map<int, int>>);
72// Aleph containers expose begin()/end() but no value_type: not STL-iterable
73// for dispatch purposes, exactly as before.
74static_assert(not StlIterableContainer<DynList<int>>);
75
76namespace U = uni_functional_detail;
77namespace Z = uni_zip_detail;
78namespace F = stl_detail;
79
83// Both layers now share one definition.
90
91TEST(AhContainerConceptsTest, HierarchyHoldsForLiveContainers)
92{
93 DynList<int> l = {1, 2, 3};
94 static_assert(AlephSequence<decltype(l)>);
95 size_t n = 0;
96 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
97 ++n;
98 EXPECT_EQ(n, l.size());
99}
100
101// uni_* used to call get_it(), which MemArray and the raw hash tables lack.
102TEST(AhContainerConceptsTest, UniFunctionalIteratesContainersWithoutGetIt)
103{
105 for (int i : {1, 2, 3})
106 m.append(i);
107 int sum = 0;
108 uni_for_each([&sum](int x) { sum += x; }, m);
109 EXPECT_EQ(sum, 6);
110}
111
112// The zip wrapper read get_curr() through a const iterator, which the chained
113// hash tables' iterators do not offer.
114TEST(AhContainerConceptsTest, ZipIteratesChainedHashTables)
115{
118 DynList<int> l = {10};
119 int pairs = 0;
120 for (auto it = uni_zip_it(l, t); it.has_curr(); it.next())
121 {
122 auto [x, bucket] = it.get_curr();
123 EXPECT_EQ(x, 10);
124 EXPECT_EQ(bucket->get_key(), 7);
125 ++pairs;
126 }
127 EXPECT_EQ(pairs, 1);
128}
129
130namespace
131{
132 // Generic code written only against AlephIterator.
133 template <AlephIterator It>
134 size_t count_with_next_ne(It it)
135 {
136 size_t n = 0;
137 for (; it.has_curr(); it.next_ne())
138 ++n;
139 return n;
140 }
141}
142
143TEST(AhContainerConceptsTest, NextNeMatchesNextOnSlistIterators)
144{
146 for (int i = 0; i < 4; ++i)
147 l.insert(i, i * 10);
148
149 DynList<int> by_next, by_next_ne;
150 for (DynSlist<int>::Iterator it(l); it.has_curr(); it.next())
151 by_next.append(it.get_curr());
152 for (DynSlist<int>::Iterator it(l); it.has_curr(); it.next_ne())
153 by_next_ne.append(it.get_curr());
154 EXPECT_EQ(by_next, by_next_ne);
155 EXPECT_EQ(by_next, DynList<int>({0, 10, 20, 30}));
156
157 EXPECT_EQ(count_with_next_ne(Slist<int>::Iterator(l)), 4u);
158 DynSlist<int> empty;
159 EXPECT_EQ(count_with_next_ne(DynSlist<int>::Iterator(empty)), 0u);
160}
161
162TEST(AhContainerConceptsTest, ZipAdaptorsWorkInGenericIteratorCode)
163{
164 DynList<int> a = {1, 2, 3};
165 std::vector<int> b = {4, 5};
166 std::list<int> c = {6, 7, 8};
167 // Zips stop at the shortest sequence.
168 EXPECT_EQ(count_with_next_ne(uni_zip_it(a, b)), 2u);
169 EXPECT_EQ(count_with_next_ne(stl_zip_it(b, c)), 2u);
170 EXPECT_EQ(count_with_next_ne(StlEnumerateIterator<std::list<int>>(c)), 3u);
171}
Functional programming utilities for C++ Standard Library containers.
Lazy zip iterators and functional operations for STL containers.
Unified functional programming utilities for both STL and Aleph containers.
Unified zip operations for both STL and Aleph containers.
size_t size_t int32_t value
Definition ca-c-api.h:116
Dnode< T > * get_curr() const
Return a pointer to the current node.
Definition tpl_dnode.H:318
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition tpl_dnode.H:266
T & get_curr() const
Return the current item.
Definition htlist.H:1679
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Iterator specialized for DynSlist returning payload references.
Dynamic list of elements of type T implemented with a singly linked list of nodes.
void next()
Advances the iterator to the next filtered element.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Bucket * insert(Bucket *bucket)
Inserts bucket into the table and returns its address if the key is not already in the table; otherwi...
Definition tpl_lhash.H:503
bool has_curr() const noexcept
Definition htlist.H:969
Simple, scalable and fast dynamic array.
Iterator over singly linked nodes.
Definition tpl_slist.H:174
Iterator that pairs each element with its index.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Key * append(const Key &key)
Alias for insert() (copy version).
Definition hashDry.H:733
#define TEST(name)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
constexpr auto stl_zip_it(const Containers &... cs)
Get a zip iterator over STL containers.
Definition ah-stl-zip.H:440
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:192
and
Check uniqueness with explicit hash + equality functors.
void uni_for_each(Op &&op, const Container &c)
Apply operation to each element (for_each).
auto uni_zip_it(const Containers &...cs)
Get a unified zip iterator.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Definition tpl_lhash.H:1008
Detect if a container has reverse iterators.
Detect if a type has a size() method.
Detect if a type is hashable via std::hash.
Trait to detect if a type has an Aleph-style Iterator interface.
Trait to detect if a type has an STL-style iterator interface (begin/end).
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.
Dynamic singly linked list.
Sorted-array set (Aleph::FlatSet), a cache-friendly ordered set.
Hash tables with collision resolution by separate chaining.
Simple, scalable, contiguous dynamic array.
DynList< int > l