37# include <gtest/gtest.h>
54 EXPECT_EQ(
range(0, 10),
build_dynlist<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
69 auto sq = [] (
int x) {
return x * x; };
90 static constexpr size_t N = 10;
103 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne(), ++i)
139 (
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"));
165 for (
auto it = idx.get_it(); it.has_curr(); it.next_ne(), ++i)
167 auto & p = it.get_curr();
174 for (
auto it =
tidx.get_it(); it.has_curr(); it.next_ne(), ++i)
176 auto & t = it.get_curr();
186 EXPECT_EQ(rev,
build_dynlist<size_t>(9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
191 auto p =
partition(
tbl, [] (
const size_t & i) {
return i < 5; });
214 constexpr size_t N = 20;
246 EXPECT_TRUE(z.all([] (
auto & p) { return p.first == p.second; }));
254 EXPECT_TRUE(z.all([] (
auto & p) { return get<0>(p) == get<1>(p); }));
262 EXPECT_TRUE(z.all([] (
auto & p) { return p.first == p.second; }));
272 EXPECT_TRUE(z.all([] (
auto & p) { return get<0>(p) == get<1>(p); }));
287 auto &
groups = result.first;
289 auto it =
groups.get_it();
324 while (
pit.has_curr())
326 auto p =
pit.get_curr();
337 auto p =
pit2.get_curr();
478 auto * p =
find_ptr(
l, [] (
int x) {
return x == 3; });
491 const int *
cp =
find_ptr(
cl, [] (
int x) {
return x == 20; });
501 auto result =
foldr<int>(
l, 0, [] (
int a,
int b) {
return a - b; });
543 auto result =
take_while(
l, [] (
int x) {
return x < 5; });
559 auto result =
drop_while(
l, [] (
int x) {
return x < 5; });
638 [] (
const int a,
const int b) {
return std::abs(a) < std::abs(b); });
675 auto it = result.get_it();
700 auto it = result.get_it();
701 EXPECT_EQ(it.get_curr(), make_pair(1, 10)); it.next();
702 EXPECT_EQ(it.get_curr(), make_pair(2, 20)); it.next();
703 EXPECT_EQ(it.get_curr(), make_pair(3, 30)); it.next();
704 EXPECT_EQ(it.get_curr(), make_pair(4, -1)); it.next();
705 EXPECT_EQ(it.get_curr(), make_pair(5, -1));
716 auto it = result.get_it();
717 EXPECT_EQ(it.get_curr(), make_pair(1, 10)); it.next();
718 EXPECT_EQ(it.get_curr(), make_pair(2, 20)); it.next();
719 EXPECT_EQ(it.get_curr(), make_pair(0, 30)); it.next();
720 EXPECT_EQ(it.get_curr(), make_pair(0, 40));
732 return p.first + 3 == p.second;
746 EXPECT_TRUE(r2.all([] (
auto & p) { return p.second == -1; }));
750 EXPECT_TRUE(
r3.all([] (
auto & p) { return p.first == -1; }));
763 auto it = result.get_it();
784 auto it = result.get_it();
785 auto p1 = it.get_curr();
791 it.next(); it.next(); it.next();
792 auto p4 = it.get_curr();
805 auto result =
group_by(
l, [] (
int x) {
return x; });
808 auto it = result.get_it();
828 auto result =
group_by(
l, [] (
int x) {
return x % 2; });
831 auto it = result.get_it();
845 auto result =
group_by(empty, [] (
int x) {
return x; });
865 [] (
int x) {
return x; },
872 auto it = result.get_it();
892 auto result =
maps<int>(
l, [] (
int x) {
return x * x; });
932 auto left =
l.
take(3);
933 auto right =
l.
drop(3);
964 auto result =
l.
take(3, 7);
973 for (
int i = 0; i < 5; ++i)
984 for (
int i = 0; i < 5; ++i)
1000 if (x % 2 == 0) ++
count;
1011 constexpr size_t N = 10000;
1019 auto evens =
filter(
l, [] (
size_t x) {
return x % 2 == 0; });
1034 auto result =
filter(
l, [] (
int x) {
return x % 2 == 0; });
1088 people.append({
"Bob", 25});
1089 people.append({
"Charlie", 35});
1090 people.append({
"Diana", 28});
1122 [] (
int x) {
return x * x; });
1131 filter(
l, [] (
int x) {
return x % 3 == 0; }),
1132 [] (
int x) {
return "val" +
to_string(x); }
1238 for (
int i = 0; i < x; ++i)
1338 names.append(
"Alice");
1339 names.append(
"Charlie");
1343 return a.length() < b.length();
1349 return a.length() < b.length();
1376 auto result =
foldl<string>(words,
"START", [] (
const string &
acc,
const string & x) {
1377 return "(" +
acc +
"+" + x +
")";
1390 auto result =
foldr<string>(words,
"END", [] (
const string & x,
const string &
acc) {
1391 return "(" + x +
"+" +
acc +
")";
1435 auto it =
groups.get_it();
1437 EXPECT_EQ(it.get_curr().second.size(), 3);
1440 EXPECT_EQ(it.get_curr().second.size(), 3);
1443 EXPECT_EQ(it.get_curr().second.size(), 2);
1450 constexpr size_t N = 10000;
1460 for (
auto it =
squared.get_it(); it.has_curr() &&
count < 100; it.next_ne(), ++
count)
1465 for (
int i = 0; i < 100; ++i)
1481 [] (
int x) {
return x * 2; }),
1482 [] (
int x) {
return x - 1; });
1502 if (a.size() != b.size())
return false;
1503 for (
size_t i = 0; i < a.size(); ++i)
1505 const auto ca =
static_cast<unsigned char>(a[i]);
1506 const auto cb =
static_cast<unsigned char>(b[i]);
1507 if (std::tolower(
ca) != std::tolower(
cb))
return false;
1525 lower.reserve(s.size());
1527 lower.push_back(
static_cast<char>(std::tolower(
static_cast<unsigned char>(c))));
1528 return std::hash<string>()(
lower);
1643 auto hash = [&
hash_calls] (
const string & s) ->
size_t
TEST_F(TreeContainer, pointers)
String manipulation utilities.
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
T & append()
Allocate a new entry to the end of array.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
T & get_last() const
Return the last item of the list.
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
size_t size() const noexcept
Count the number of elements of the list.
Aleph::DynList< T > take(const size_t n) const
Return a list with the first n elements seen in the container during its traversal.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Aleph::DynList< T > drop(const size_t n) const
Drop the first n elements seen in the container during its traversal.
Type & nth(const size_t n)
Return the n-th item of the container.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
MapOLhash< int, Foo > tbl
Main namespace for Aleph-w library functions.
std::pair< DynList< T >, size_t > unique_sequential(const Container< T > &c, Equal &eq)
Extract unique consecutive items.
std::tuple< bool, size_t, typename C1::Item_Type, typename C2::Item_Type > are_eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Detailed equality check returning mismatch position and values.
DynList< T > flatten(const C2< C1< T > > &c)
Flatten a nested container of one level.
DynList< typename Container::Item_Type > to_dynlist(const Container &c)
Container< T > nrange(const T start, const T end, const size_t n)
Generate exactly n values evenly spaced between [start, end].
auto group_by_reduce(const Container< T > &c, KeyFunc key_func, Reducer reducer) -> DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, std::invoke_result_t< Reducer, const DynList< T > & > > >
Group consecutive elements and apply a reducer to each group.
std::pair< const typename Container::Item_Type *, const typename Container::Item_Type * > minmax_ptr(const Container &container, Cmp cmp=Cmp())
Find both min and max elements in a single pass.
const Container::Item_Type * min_ptr(const Container &container, Cmp cmp=Cmp())
Find the minimum element in a container.
DynList< std::pair< std::optional< typename Container1::Item_Type >, std::optional< typename Container2::Item_Type > > > zip_longest_opt(const Container1 &a, const Container2 &b)
Zip two containers using optionals for missing values.
auto unzip(const Container &l)
Separate a list of pairs into two lists.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
void reverse(Itor beg, Itor end)
Reverse elements in a range.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
DynList< typename Container::Item_Type > drop_while(const Container &c, Pred pred)
Skip elements while predicate is true (drop_while).
auto set_range(const T start, const T end, const T step, Op &op) -> Container< std::decay_t< decltype(op(start))> >
Generate a range [start, end] and apply an operation to each value.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zipEq(const Container1 &a, const Container2 &b)
Zip two containers; throw if lengths differ.
size_t size(Node *root) noexcept
std::pair< TgtContainer< typename SrcContainer::Item_Type >, TgtContainer< typename SrcContainer::Item_Type > > partition(const SrcContainer &c, std::function< bool(const typename SrcContainer::Item_Type &)> operation)
Partition a container into two based on a predicate.
bool containers_eq(const C1 &c1, const C2 &c2, Eq e)
bool all_unique(const Container &container, Equal &eq)
Check if all elements in a container are unique using a comparator.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
bool is_inside(const T &val, const DynList< T > &values)
Check if a value is present in a list.
DynList< typename Container::Item_Type > take_while(const Container &c, Pred pred)
Return elements while predicate is true (take_while).
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.
Pair_Iterator< typename C1::Iterator, typename C2::Iterator > get_pair_it(const C1 &c1, const C2 &c2)
Create a Pair_Iterator for two containers.
auto enumerate(const Container &c)
Return pairs of (element, index).
size_t append_in_container(C &c, Args ... args)
Append multiple items into a container.
bool is_equal(const T &val)
Variadic check for equality against multiple values.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
auto gen_seq_list_tuples(const Container &c, size_t n)
Generate all sequential tuples (sliding windows) of size n.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
DynList< T > build_dynlist(Args ... args)
Build a DynList with the given items.
void each(const size_t start, const size_t end, Op &op)
Execute an operation repeatedly over a range of indices.
DynList< T > rep(const size_t n, const T &item)
Create a sequence of repeated items.
auto flat_map(const Container &container, Op op) -> DynList< typename std::decay_t< decltype(op(std::declval< typename Container::Item_Type >()))>::Item_Type >
Apply operation and flatten results (flatMap/concatMap).
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > tzip(const Container1 &a, const Container2 &b)
Zip two containers into a list of tuples.
bool zip_all(Op &&op, const Cs &...cs)
Return true if op returns true for all tuples and containers have equal length.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
DynList< typename Container::Item_Type * > pointers_list(Container &c)
Create a list of pointers to items in a container.
DynList< std::pair< size_t, typename Container::Key_Type > > indexes(const Container &c)
Return pairs of (index, key).
bool lesser(const C1 &c1, const C2 &c2, Cmp cmp=Cmp())
Lexicographical comparison between two containers.
TgtC assign_container(const SrcC &srcc)
Convert one container type to another.
DynList< std::tuple< size_t, typename Container::Item_Type > > enumerate_tuple(const Container &container)
Zip containers with an index (enumerate with zip).
const Container::Item_Type * max_ptr(const Container &container, Cmp cmp=Cmp())
Find the maximum element in a container.
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > tzipEq(const Container1 &a, const Container2 &b)
Zip two containers into tuples; throw if lengths differ.
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
std::pair< DynList< DynList< T > >, size_t > sequential_groups(const Container< T > &c, Equal &eq)
Group consecutive equal elements together.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
size_t insert_in_container(C &c, Args ... args)
Insert multiple items into a container.
DynList< std::tuple< size_t, typename Container::Key_Type > > tindexes(const Container &c)
Return tuples of (index, key).
Itor::difference_type count_if(Itor beg, const Itor &end, Operation op)
Count elements satisfying a predicate.
DynList< T > scanl_sum(const Container &container, const T &init)
Compute running sums (scanl with addition).
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > tzip_longest(const Container1 &a, const Container2 &b, const typename Container1::Item_Type &fill_a=typename Container1::Item_Type(), const typename Container2::Item_Type &fill_b=typename Container2::Item_Type())
Zip two containers into tuples, padding the shorter one.
std::string concat(const Args &... args)
Concatenate multiple arguments into a single std::string.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip_longest(const Container1 &a, const Container2 &b, const typename Container1::Item_Type &fill_a=typename Container1::Item_Type(), const typename Container2::Item_Type &fill_b=typename Container2::Item_Type())
Zip two containers, padding the shorter one with a fill value.
DynList< typename Container::Item_Type > sublist(const Container &c, size_t pos, const size_t stride)
Extract a sublist using a stride.
Container::Item_Type * find_ptr(Container &container, Pred &pred)
Find the first element satisfying pred.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
void enum_for_each(const Container &container, Operation &operation)
Apply an operation to each element and its index.
DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, DynList< T > > > group_by(const Container< T > &c, KeyFunc key_func)
Group consecutive elements by a key function.
Container< T > contiguous_range(T start, const size_t n)
Generate n contiguous values starting from start.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
size_t remove_from_container(C &c, Args ... args)
Remove multiple items from a container.
std::tuple< Container< T1 >, Container< T2 > > tunzip(const Container< std::tuple< T1, T2 > > &l)
Separate a list of tuples into two containers.
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.
Represents a missing value.
bool is_found() const noexcept override
Represents a found value (stored by reference).
bool is_found() const noexcept override
bool operator()(const string &a, const string &b) const
Performs a case-insensitive comparison of two strings.
size_t operator()(const string &s) const
Produces a case-insensitive hash for a string by hashing its lowercase form.
Helper for float comparison with tolerance.
bool operator()(float a, float b) const
Compares two floating-point values for equality within the configured tolerance.
FloatToleranceEqual(float tol=0.001f)
Constructs a FloatToleranceEqual comparator with a specified tolerance.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
bool operator==(const Person &other) const
static constexpr size_t N
Lazy and scalable dynamic array implementation.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Open addressing hash table with double hashing.