46#include <gtest/gtest.h>
69 static inline long copies = 0;
70 static inline long moves = 0;
75 explicit Counted(
const int x) noexcept : v(x) {}
77 Counted(
const Counted & o) : v(o.v) { copies += v >= 0 ? 1 : 0; }
78 Counted(Counted && o) noexcept : v(o.v) { moves += v >= 0 ? 1 : 0; }
83 copies += v >= 0 ? 1 : 0;
90 moves += v >= 0 ? 1 : 0;
94 bool operator<(
const Counted & o)
const noexcept {
return v < o.v; }
95 bool operator==(
const Counted & o)
const noexcept {
return v == o.v; }
101 return static_cast<size_t>(c.v) * 0x9E3779B97F4A7C15ull;
114 void append(
const Counted & c) { data.
append(c); }
122 Iterator() =
default;
123 Iterator(
const Mixed & c) : items(&c.data) {}
125 [[nodiscard]]
bool has_curr() const noexcept {
return pos < items->
size(); }
126 [[nodiscard]] Counted &
get_curr()
const {
return const_cast<Counted &
>((*items)(pos)); }
127 void next() { ++pos; }
128 void next_ne() noexcept { ++pos; }
131 [[nodiscard]] Iterator get_it()
const {
return Iterator(*
this); }
135 class MapCopies :
public ::testing::Test
148 class MapKeyCopies :
public ::testing::Test
160TYPED_TEST(MapCopies, KeysCopyNoValueAndValuesCopyEachValueOnce)
162 constexpr int n = 20;
164 for (
int i = 0; i < n; ++i)
165 (
void)
m.
insert(i, Counted(i));
167 Counted::copies = Counted::moves = 0;
169 EXPECT_EQ(
keys.size(),
static_cast<size_t>(n));
170 EXPECT_EQ(Counted::copies, 0);
171 EXPECT_EQ(Counted::moves, 0);
174 Counted::copies = Counted::moves = 0;
175 const auto values =
m.values();
176 EXPECT_EQ(values.size(),
static_cast<size_t>(n));
177 EXPECT_EQ(Counted::copies, n);
178 EXPECT_EQ(Counted::moves, 0);
180 values.for_each([&
sum] (
const Counted & c) {
sum += c.v; });
181 EXPECT_EQ(
sum, n * (n - 1) / 2);
186 constexpr int n = 20;
188 for (
int i = 0; i < n; ++i)
189 (
void)
m.
insert(Counted(i), i);
191 Counted::copies = Counted::moves = 0;
193 EXPECT_EQ(
keys.size(),
static_cast<size_t>(n));
194 EXPECT_EQ(Counted::copies, n);
195 EXPECT_EQ(Counted::moves, 0);
197 keys.for_each([&
sum] (
const Counted & c) {
sum += c.v; });
198 EXPECT_EQ(
sum, n * (n - 1) / 2);
204TEST(ItemCopies, TheListOfTheItemsOfAContainerCopiesEachOnceAndMovesNone)
206 constexpr int n = 10;
211 for (
int i = 0; i < n; ++i)
216 mixed.append(Counted(i));
219 auto expect_one_copy = [n] (
const char * what,
auto && make)
221 Counted::copies = Counted::moves = 0;
222 const auto items = make();
223 EXPECT_EQ(items.size(),
static_cast<size_t>(n)) << what;
224 EXPECT_EQ(Counted::copies, n) << what;
225 EXPECT_EQ(Counted::moves, 0) << what;
227 items.for_each([&
sum] (
const Counted & c) {
sum += c.v; });
228 EXPECT_EQ(
sum, n * (n - 1) / 2) << what;
230 expect_one_copy(
"DynList::items()", [&list] {
return list.
items(); });
231 expect_one_copy(
"DynList::keys()", [&list] {
return list.
keys(); });
232 expect_one_copy(
"DynDlist::items()", [&dlist] {
return dlist.
items(); });
233 expect_one_copy(
"to_dynlist(DynDlist)", [&dlist] {
return to_dynlist(dlist); });
234 expect_one_copy(
"to_DynList(Array)", [&array] {
return to_DynList(array); });
235 expect_one_copy(
"KeysMixin::keys()", [&mixed] {
return mixed.keys(); });
236 expect_one_copy(
"KeysMixin::items()", [&mixed] {
return mixed.items(); });
239TEST(ItemCopies, TheRanksOfAListCopyEachItemOnce)
243 for (
const int v : {5, 3, 8, 1, 9, 2})
248 const size_t expected[] = {3, 2, 4, 0, 5, 1};
251 const auto r1 =
ranks(list);
252 EXPECT_EQ(Counted::copies, 6);
254 const auto r2 =
ranks(dlist);
255 EXPECT_EQ(Counted::copies, 6);
258 EXPECT_EQ(Counted::copies, 6);
261 EXPECT_EQ(Counted::copies, 6);
263 for (
size_t i = 0; i < 6; ++i)
267 EXPECT_EQ(p1(i).second,
expected[i]);
268 EXPECT_EQ(p2(i).second,
expected[i]);
270 EXPECT_EQ(p1(0).first, Counted(5));
271 EXPECT_EQ(p2(5).first, Counted(2));
274TEST(ItemCopies, ACopyOfARandomSetCopiesEachItemOnce)
277 for (
int i = 0; i < 10; ++i)
282 EXPECT_EQ(Counted::copies, 10);
283 EXPECT_EQ(
copy.size(), 10u);
286 assigned.
append(Counted(99));
289 EXPECT_EQ(Counted::copies, 10);
290 EXPECT_EQ(assigned.
size(), 10u);
Conversion utilities between Aleph-w and STL container types.
CRTP Mixins for container functionality (DRY principle).
bool operator<(const Time &l, const Time &r)
High-level sorting functions for Aleph containers.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Dynamic map implemented with a classic binary search tree.
Dynamic map implemented with a randomized BST.
Dynamic map implemented with a red-black tree.
Dynamic map implemented with a splay tree.
Dynamic map implemented with a ranked treap.
Dynamic map implemented with a treap.
Ordered map stored as two parallel sorted contiguous arrays.
CRTP Mixin providing functional programming operations.
CRTP Mixin for extracting keys from set-like containers.
T & append(const T &item)
Insert randomly an item by copy.
size_t size() const
Return the number of items in the queue.
CRTP Mixin providing traversal operations.
Minimal std::expected-style result type for C++20.
DynList< Key > keys() const
Returns a list containing all keys in the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Singly linked list implementations with head-tail access.
TYPED_TEST(MapCopies, KeysCopyNoValueAndValuesCopyEachValueOnce)
Main namespace for Aleph-w library functions.
DynList< typename Container::Item_Type > to_dynlist(const Container &c)
DynList< T > to_DynList(const Array< T > &a)
Convert an Array to a DynList.
T Item_Type
Type of elements from the first container.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
auto pair_ranks(const Array< T > &c)
Computes (value, rank) pairs for each element in an Array.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
size_t aleph_hash_value(const AdversarialTranspositionKey< StateKey > &key) noexcept
auto get_curr() const
Return the current tuple (bounds-checked).
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
void next()
Advance all underlying iterators (bounds-checked).
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
Counted & operator=(const Counted &other)
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Aleph::DynList< T > keys() const
See items().
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.
Dynamic map with open hashing.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on hash tables.
Sorted-array map (Aleph::FlatMap), a cache-friendly ordered map.
Random access queue (bag) with O(1) random pop.