41#include <gtest/gtest.h>
79 constexpr bool iterator_rejects_a_constant_container =
80 not std::is_constructible_v<typename C::Iterator, const C &>
81 and std::is_constructible_v<typename C::Iterator, C &>
82 and std::is_constructible_v<typename C::Const_Iterator, const C &>;
86 using curr_t =
decltype(std::declval<const typename C::Iterator &>().get_curr());
89 using const_curr_t =
decltype(std::declval<const const_iterator_t<C> &>().
get_curr());
95 constexpr bool writes_through_call =
requires (
const C & c) { c(0) = 7; };
98 constexpr bool writes_through_curr =
requires (
It it) { it.get_curr() = 8; };
101 constexpr bool removes_through_it =
requires (
It it) { it.del(); };
104 constexpr bool const_traverse_takes_a_modifying_operation =
requires (
const C & c)
106 c.traverse([] (
typename C::Item_Type & x) { x = 9;
return true; });
110 constexpr bool const_traverse_takes_a_reading_operation =
requires (
const C & c)
112 c.traverse([] (
const typename C::Item_Type & x) {
return x == 9; });
116TEST(ConstIterator, the_iterators_that_hand_out_modifiable_references_reject_constant_containers)
120 static_assert(iterator_rejects_a_constant_container<MemArray<int>>);
121 static_assert(iterator_rejects_a_constant_container<Array<int>>);
122 static_assert(iterator_rejects_a_constant_container<ArrayStack<int>>);
123 static_assert(iterator_rejects_a_constant_container<FixedStack<int>>);
124 static_assert(iterator_rejects_a_constant_container<ArrayQueue<int>>);
125 static_assert(iterator_rejects_a_constant_container<FixedQueue<int>>);
126 static_assert(iterator_rejects_a_constant_container<Random_Set<int>>);
127 static_assert(iterator_rejects_a_constant_container<DynArray<int>>);
128 static_assert(iterator_rejects_a_constant_container<DynList<int>>);
129 static_assert(iterator_rejects_a_constant_container<DynDlist<int>>);
130 static_assert(iterator_rejects_a_constant_container<DynListQueue<int>>);
131 static_assert(iterator_rejects_a_constant_container<DynListStack<int>>);
132 static_assert(iterator_rejects_a_constant_container<DynSetTree<int>>);
133 static_assert(iterator_rejects_a_constant_container<DynMapTree<int, int>>);
134 static_assert(iterator_rejects_a_constant_container<DynSetHash<int>>);
135 static_assert(iterator_rejects_a_constant_container<DynMapHash<int, int>>);
136 static_assert(iterator_rejects_a_constant_container<ODhashTable<int>>);
137 static_assert(iterator_rejects_a_constant_container<OLhashTable<int>>);
139 static_assert(std::is_same_v<curr_t<DynList<int>>,
int &>);
140 static_assert(std::is_same_v<const_curr_t<DynList<int>>,
const int &>);
141 static_assert(std::is_same_v<curr_t<Array<int>>,
int &>);
142 static_assert(std::is_same_v<const_curr_t<Array<int>>,
const int &>);
143 static_assert(std::is_same_v<const_curr_t<DynArray<int>>,
const int &>);
147 static_assert(std::is_same_v<curr_t<ArrayHeap<int>>,
const int &>);
148 static_assert(std::is_same_v<curr_t<DynArrayHeap<int>>,
const int &>);
149 static_assert(std::is_same_v<curr_t<DynBinHeap<int>>,
const int &>);
152 static_assert(std::is_constructible_v<DynSkipList<int>::Iterator,
const DynSkipList<int> &>);
153 static_assert(std::is_same_v<
decltype(std::declval<DynSkipList<int>::Iterator &>().
get_curr()),
158 static_assert(std::is_same_v<curr_t<DynSetTree<int>>,
const int &>);
159 static_assert(std::is_same_v<
decltype(std::declval<DynMapTree<int, int>::Iterator &>().
get_curr()),
160 std::pair<int, int> &>);
161 static_assert(std::is_same_v<const_curr_t<DynMapTree<int, int>>,
const std::pair<int, int> &>);
163 static_assert(std::is_same_v<const_iterator_t<DynList<int>>
::Item_Type,
int>);
164 static_assert(std::is_same_v<const_iterator_t<DynList<int>>::Set_Type,
DynList<int>>);
165 static_assert(std::is_same_v<const_curr_t<HashSet<int>>,
const int &>);
168TEST(ConstIterator, get_it_depends_on_the_constness_of_the_container)
182 for (
auto it = cl.
get_it(); it.has_curr(); it.next_ne())
183 sum += it.get_curr();
185 EXPECT_EQ(cl.
get_it(2).get_curr(), 3);
195TEST(ConstIterator, is_as_large_as_the_iterator_that_it_wraps)
197 static_assert(const_iterator_is_as_large_as_the_iterator<MemArray<int>>);
198 static_assert(const_iterator_is_as_large_as_the_iterator<Array<int>>);
199 static_assert(const_iterator_is_as_large_as_the_iterator<ArrayQueue<int>>);
200 static_assert(const_iterator_is_as_large_as_the_iterator<DynArray<int>>);
201 static_assert(const_iterator_is_as_large_as_the_iterator<DynList<int>>);
202 static_assert(const_iterator_is_as_large_as_the_iterator<DynDlist<int>>);
203 static_assert(const_iterator_is_as_large_as_the_iterator<DynSetTree<int>>);
204 static_assert(const_iterator_is_as_large_as_the_iterator<DynSetHash<int>>);
207 static_assert(std::is_same_v<DynList<int>::Const_Iterator::Item_Type,
209 static_assert(std::is_same_v<DynList<int>::Const_Iterator::Set_Type,
213TEST(ConstIterator, reads_the_container_like_the_iterator)
216 for (
int i = 0; i < 5; ++i)
238 EXPECT_EQ(
copy.get_curr(), 1);
241 EXPECT_EQ(
copy.get_curr(), 0);
253TEST(ConstIterator, the_stl_const_iterator_is_read_only_and_random_access_on_arrays)
256 for (
int i = 0; i < 10; ++i)
260 static_assert(std::random_access_iterator<
decltype(ca.
begin())>);
261 static_assert(std::is_same_v<
decltype(*ca.
begin()),
const int &>);
262 static_assert(std::is_same_v<
decltype(*a.
begin()),
int &>);
264 EXPECT_EQ(std::count_if(ca.
begin(), ca.
end(), [] (
int x) { return x % 2 == 0; }), 5);
265 EXPECT_EQ(ca.
end() - ca.
begin(), 10);
266 EXPECT_EQ(ca.
begin()[7], 7);
267 EXPECT_TRUE(std::is_sorted(ca.
begin(), ca.
end()));
270 for (
const int & x : ca)
275 EXPECT_EQ(*std::max_element(cl.
begin(), cl.
end()), 3);
278TEST(ConstIterator, a_constant_dynarray_is_read_without_allocating)
285 for (
auto it = ca.
get_it(); it.has_curr(); it.next_ne())
286 sum += it.get_curr();
287 EXPECT_EQ(
sum, 16 * 7);
290 EXPECT_EQ(ca.
read(3), 7);
291 for (
size_t i = 0; i < 16; ++i)
292 EXPECT_FALSE(ca.
exist(i));
298 EXPECT_TRUE(ca.
exist(0));
304TEST(ConstIterator, the_accessors_of_a_constant_container_hand_out_references_to_const)
307 static_assert(std::is_same_v<decltype(std::declval<CA>()[0]),
const int &>);
308 static_assert(std::is_same_v<decltype(std::declval<CA>()(0)),
const int &>);
309 static_assert(std::is_same_v<decltype(std::declval<CA>().get_first()),
const int &>);
310 static_assert(std::is_same_v<decltype(std::declval<CA>().get_last()),
const int &>);
311 static_assert(std::is_same_v<
decltype(std::declval<Array<int> &>()[0]),
int &>);
314 static_assert(std::is_same_v<decltype(std::declval<CQ>().front()),
const int &>);
315 static_assert(std::is_same_v<decltype(std::declval<CQ>().rear()),
const int &>);
316 static_assert(std::is_same_v<decltype(std::declval<CQ>()[0]),
const int &>);
317 static_assert(std::is_same_v<decltype(std::declval<CQ>().top()),
const int &>);
318 static_assert(std::is_same_v<
decltype(std::declval<ArrayQueue<int> &>().front()),
int &>);
321 static_assert(std::is_same_v<decltype(std::declval<CL>().get_first()),
const int &>);
322 static_assert(std::is_same_v<decltype(std::declval<CL>().get_last_ne()),
const int &>);
323 static_assert(std::is_same_v<decltype(std::declval<CL>().top()),
const int &>);
324 static_assert(std::is_same_v<
decltype(std::declval<DynList<int> &>().get_first()),
int &>);
327 static_assert(std::is_same_v<decltype(std::declval<CD>().get_first()),
const int &>);
328 static_assert(std::is_same_v<decltype(std::declval<CD>()[0]),
const int &>);
329 static_assert(std::is_same_v<
decltype(std::declval<DynDlist<int> &>()[0]),
int &>);
332 static_assert(std::is_same_v<decltype(std::declval<CDA>()(0)),
const int &>);
333 static_assert(std::is_same_v<decltype(std::declval<CDA>().access(0)),
const int &>);
334 static_assert(std::is_same_v<decltype(std::declval<CDA>().top()),
const int &>);
336 static_assert(std::is_same_v<
decltype(std::declval<const DynSetTree<int> &>().
find(0)),
const int &>);
337 static_assert(std::is_same_v<
decltype(std::declval<const DynSetTree<int> &>().
search(0)),
const int *>);
338 static_assert(std::is_same_v<
decltype(std::declval<DynSetTree<int> &>().
find(0)),
int &>);
339 static_assert(std::is_same_v<
decltype(std::declval<const DynMapTree<int, int> &>().
search(0)),
340 const std::pair<int, int> *>);
341 static_assert(std::is_same_v<
decltype(std::declval<const HashSet<int> &>().get_first()),
const int &>);
342 static_assert(std::is_same_v<
decltype(std::declval<const DynSetHash<int> &>().
search(0)),
const int *>);
343 static_assert(std::is_same_v<
decltype(std::declval<DynSetHash<int> &>().
search(0)),
int *>);
344 static_assert(std::is_same_v<
decltype(std::declval<const DynMapHash<int, int> &>().
search(0)),
345 const std::pair<int, int> *>);
346 static_assert(std::is_same_v<
decltype(std::declval<const ODhashTable<int> &>().
search(0)),
const int *>);
347 static_assert(std::is_same_v<
decltype(std::declval<const OLhashTable<int> &>().
search(0)),
const int *>);
348 static_assert(std::is_same_v<
decltype(std::declval<const DynSkipList<int> &>().
search(0)),
const int *>);
349 static_assert(std::is_same_v<
decltype(std::declval<DynSkipList<int> &>().
search(0)),
int *>);
350 static_assert(std::is_same_v<
decltype(std::declval<const DynBinHeap<int> &>().top()),
const int &>);
351 static_assert(std::is_same_v<
decltype(std::declval<const DynList<int> &>().nth(0)),
const int &>);
362 EXPECT_EQ(
m.
find(1), 11);
363 EXPECT_EQ(std::as_const(
m).
search(1)->second, 11);
366TEST(ConstIterator, the_leaks_found_from_a_client_no_longer_compile)
369 static_assert(not writes_through_call<DynArray<int>>);
371 static_assert(not std::is_constructible_v<Array<int>::Iterator,
const Array<int> &>);
372 static_assert(not writes_through_curr<Array<int>::Const_Iterator>);
373 static_assert(writes_through_curr<Array<int>::Iterator>);
375 static_assert(not const_traverse_takes_a_modifying_operation<SmallVector<int, 4>>);
376 static_assert(const_traverse_takes_a_reading_operation<SmallVector<int, 4>>);
377 static_assert(not const_traverse_takes_a_modifying_operation<DynList<int>>);
378 static_assert(const_traverse_takes_a_reading_operation<DynList<int>>);
380 static_assert(not writes_through_curr<DynArray<int>::Const_Iterator>);
382 static_assert(not removes_through_it<DynList<int>::Const_Iterator>);
383 static_assert(removes_through_it<DynList<int>::Iterator>);
386TEST(ConstIterator, the_generic_code_reads_constant_containers)
393 for (
auto it =
get_pair_it(a, b); it.has_curr(); it.next_ne())
395 const auto [x,
y] = it.get_curr();
398 EXPECT_EQ(
sum, 4 + 10 + 18);
399 EXPECT_EQ(
zip(a, b).
size(), 3u);
402 odd(a, [] (
const int & x) {
return x % 2 == 1; });
403 static_assert(std::is_same_v<
decltype(odd)
::Item_Type,
int>);
405 for (; odd.has_curr(); odd.next())
410 EXPECT_TRUE(a.
all([] (
const int & x) { return x > 0; }));
411 EXPECT_EQ(*a.
find_ptr([] (
const int & x) { return x == 2; }), 2);
412 static_assert(std::is_same_v<
decltype(a.
find_ptr([] (
const int & x) {
return x == 2; })),
const int *>);
413 EXPECT_EQ(a.
nth(2), 3);
415 EXPECT_FALSE(a == b);
416 ASSERT_NE(
min_ptr(a),
nullptr);
STL-compatible iterator adapters for Aleph containers.
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
Queue implemented with a single dynamic array.
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Read-only view of an Aleph iterator: the iterator of a constant container.
auto reset_first() noexcept(noexcept(std::declval< I & >().reset_first())) -> decltype(std::declval< I & >().reset_first())
Position the view on the first item.
auto is_in_last() const noexcept(noexcept(std::declval< I & >().is_in_last())) -> decltype(std::declval< I & >().is_in_last())
true if the current item is the last one.
auto reset_last() noexcept(noexcept(std::declval< I & >().reset_last())) -> decltype(std::declval< I & >().reset_last())
Position the view on the last item.
auto get_pos() const noexcept(noexcept(std::declval< I & >().get_pos())) -> decltype(std::declval< I & >().get_pos())
Ordinal position of the current item.
auto prev() noexcept(noexcept(std::declval< I & >().prev())) -> decltype(std::declval< I & >().prev())
Move to the previous item; what the underlying iterator throws.
void swap(Const_Iterator &other) noexcept(std::is_nothrow_swappable_v< It >)
Exchange the views.
auto end() noexcept(noexcept(std::declval< I & >().end())) -> decltype(std::declval< I & >().end())
Position the view past the last item.
auto next() noexcept(noexcept(std::declval< I & >().next())) -> decltype(std::declval< I & >().next())
Move to the next item; what the underlying iterator throws.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
auto get_curr_ne() const noexcept(noexcept(std::declval< I & >().get_curr_ne())) -> const_iterator_detail::const_item_t< decltype(std::declval< I & >().get_curr_ne())>
get_curr() without the checks of the underlying iterator.
auto get_curr() const noexcept(noexcept(std::declval< I & >().get_curr())) -> const_iterator_detail::const_item_t< decltype(std::declval< I & >().get_curr())>
The current item, read-only.
Iterator on the items of array.
T & get_curr() const
Return the current item.
void set_default_initial_value(const T &value) noexcept(std::is_nothrow_copy_assignable_v< T >)
Set the default value.
Iterator get_it()
Iterator on the modifiable array, positioned on entry 0.
const T & read(const size_t i) const
Read entry i without allocating memory.
T & get_first()
Return a modifiable reference to the first item of array (as if this was a queue)
bool exist(const size_t i) const
Return true if the i-th entry is accessible.
T & get_last()
Return a modifiable reference to the last item of array (as if this was a queue)
Dynamic doubly linked list with O(1) size and bidirectional access.
Iterator on the items of list.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
T & get_last()
Return the last item of the list.
Generic key-value map implemented on top of a binary search tree.
Dynamic ordered set implemented with a Skip List.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Type & nth(const size_t n)
Return the n-th item of the container.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, Type > Type * find_ptr(Operation &operation) noexcept(mutable_search_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criterion.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Key & find(const Key &key)
Finds a key and returns a reference to it.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
constexpr bool const_iterator_is_as_large_as_the_iterator
Generic filter iterator wrapper for Aleph containers.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
T Item_Type
Type of elements from the first container.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
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.
auto get_curr() const
Return the current tuple (bounds-checked).
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
and
Check uniqueness with explicit hash + equality functors.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
auto get_pair_it(C1 &c1, C2 &c2)
Create a Pair_Iterator for two containers.
auto iterator_for(C &c)
Iterator on c positioned on its first item: the container's own iterator on a modifiable container.
const Container::Item_Type * min_ptr(const Container &container, Cmp cmp=Cmp())
Find the minimum element in a container.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Fixed-capacity binary heap and heapsort algorithms.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Dynamic array container with automatic resizing.
Array-based dynamic binary heap.
Lazy and scalable dynamic array implementation.
Dynamic binary heap with node-based storage.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Dynamic ordered set implemented with a Skip List.
Unified hash table interface.
Simple, scalable, contiguous dynamic array.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.
Random access queue (bag) with O(1) random pop.
Dynamic array with inline storage (Aleph::SmallVector).