Aleph-w 3.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
35// These iterators offered next() but not next_ne() and so were rejected by
36// generic code written against AlephIterator.
38static_assert(AlephIterator<decltype(uni_zip_it(std::declval<DynList<int> &>(),
39 std::declval<std::vector<int> &>()))>);
40static_assert(AlephIterator<decltype(stl_zip_it(std::declval<std::vector<int> &>(),
41 std::declval<std::list<int> &>()))>);
43
44// AlephIterable: the single "iterate it the Aleph way" test of both functional
45// layers. The iterator is built from the container, so types without
46// get_it() qualify, and get_curr() need not be const (chained hash tables).
51
53// Aleph containers expose begin()/end() but no value_type: not STL-iterable
54// for dispatch purposes, exactly as before.
56
57namespace U = uni_functional_detail;
58namespace Z = uni_zip_detail;
59namespace F = stl_detail;
60
64// Both layers now share one definition.
71
73{
74 DynList<int> l = {1, 2, 3};
75 static_assert(AlephSequence<decltype(l)>);
76 size_t n = 0;
77 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
78 ++n;
79 EXPECT_EQ(n, l.size());
80}
81
82// uni_* used to call get_it(), which MemArray and the raw hash tables lack.
84{
86 for (int i : {1, 2, 3})
87 m.append(i);
88 int sum = 0;
89 uni_for_each([&sum](int x) { sum += x; }, m);
90 EXPECT_EQ(sum, 6);
91}
92
93// The zip wrapper read get_curr() through a const iterator, which the chained
94// hash tables' iterators do not offer.
96{
99 DynList<int> l = {10};
100 int pairs = 0;
101 for (auto it = uni_zip_it(l, t); it.has_curr(); it.next())
102 {
103 auto [x, bucket] = it.get_curr();
104 EXPECT_EQ(x, 10);
105 EXPECT_EQ(bucket->get_key(), 7);
106 ++pairs;
107 }
108 EXPECT_EQ(pairs, 1);
109}
110
111namespace
112{
113 // Generic code written only against AlephIterator.
114 template <AlephIterator It>
115 size_t count_with_next_ne(It it)
116 {
117 size_t n = 0;
118 for (; it.has_curr(); it.next_ne())
119 ++n;
120 return n;
121 }
122}
123
125{
127 for (int i = 0; i < 4; ++i)
128 l.insert(i, i * 10);
129
131 for (DynSlist<int>::Iterator it(l); it.has_curr(); it.next())
132 by_next.append(it.get_curr());
133 for (DynSlist<int>::Iterator it(l); it.has_curr(); it.next_ne())
134 by_next_ne.append(it.get_curr());
136 EXPECT_EQ(by_next, DynList<int>({0, 10, 20, 30}));
137
139 DynSlist<int> empty;
141}
142
144{
145 DynList<int> a = {1, 2, 3};
146 std::vector<int> b = {4, 5};
147 std::list<int> c = {6, 7, 8};
148 // Zips stop at the shortest sequence.
151 EXPECT_EQ(count_with_next_ne(StlEnumerateIterator<std::list<int>>(c)), 3u);
152}
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
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
T & insert(const T &item)
Definition htlist.H:1220
T & append(const T &item)
Definition htlist.H:1271
Iterator specialized for DynSlist returning payload references.
Dynamic list of elements of type T implemented with a singly linked list of nodes.
Generic filter iterator wrapper.
void next()
Advances the iterator to the next filtered element.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
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:384
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1065
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() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:228
Key * append(const Key &key)
Alias for insert() (copy version).
Definition hashDry.H:389
#define TEST(name)
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
constexpr auto stl_zip_it(const Containers &... cs)
Get a zip iterator over STL containers.
Definition ah-stl-zip.H:440
and
Check uniqueness with explicit hash + equality functors.
auto uni_zip_it(const Containers &... cs)
Get a unified zip iterator.
void uni_for_each(Op &&op, const Container &c)
Apply operation to each element (for_each).
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:838
Detect if a container has reverse iterators.
Detect if a type has a size() method.
Detect if a type is hashable via std::hash.
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.
Linear hashing with dynamic bucket expansion.
Simple, scalable, contiguous dynamic array.
DynList< int > l