38# include <gtest/gtest.h>
52 EXPECT_EQ(
range(0, 10),
build_dynlist<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
67 auto sq = [] (
int x) {
return x * x; };
88 static constexpr size_t N = 10;
101 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne(), ++i)
137 (
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"));
163 for (
auto it = idx.get_it(); it.has_curr(); it.next_ne(), ++i)
165 auto & p = it.get_curr();
172 for (
auto it =
tidx.
get_it(); it.has_curr(); it.next_ne(), ++i)
174 auto & t = it.get_curr();
184 EXPECT_EQ(rev,
build_dynlist<size_t>(9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
189 auto p =
partition(
tbl, [] (
const size_t & i) {
return i < 5; });
212 constexpr size_t N = 20;
285 auto &
groups = result.first;
322 while (
pit.has_curr())
324 auto p =
pit.get_curr();
335 auto p =
pit2.get_curr();
476 auto * p =
find_ptr(
l, [] (
int x) {
return x == 3; });
489 const int *
cp =
find_ptr(
cl, [] (
int x) {
return x == 20; });
499 auto result =
foldr<int>(
l, 0, [] (
int a,
int b) {
return a - b; });
541 auto result =
take_while(
l, [] (
int x) {
return x < 5; });
557 auto result =
drop_while(
l, [] (
int x) {
return x < 5; });
636 [] (
const int a,
const int b) {
return std::abs(a) < std::abs(b); });
673 auto it = result.get_it();
698 auto it = result.get_it();
699 EXPECT_EQ(it.get_curr(), make_pair(1, 10)); it.next();
700 EXPECT_EQ(it.get_curr(), make_pair(2, 20)); it.next();
701 EXPECT_EQ(it.get_curr(), make_pair(3, 30)); it.next();
702 EXPECT_EQ(it.get_curr(), make_pair(4, -1)); it.next();
703 EXPECT_EQ(it.get_curr(), make_pair(5, -1));
714 auto it = result.get_it();
715 EXPECT_EQ(it.get_curr(), make_pair(1, 10)); it.next();
716 EXPECT_EQ(it.get_curr(), make_pair(2, 20)); it.next();
717 EXPECT_EQ(it.get_curr(), make_pair(0, 30)); it.next();
718 EXPECT_EQ(it.get_curr(), make_pair(0, 40));
730 return p.first + 3 == p.second;
744 EXPECT_TRUE(r2.all([] (
auto & p) { return p.second == -1; }));
761 auto it = result.get_it();
782 auto it = result.get_it();
783 auto p1 = it.get_curr();
789 it.next(); it.next(); it.next();
790 auto p4 = it.get_curr();
803 auto result =
group_by(
l, [] (
int x) {
return x; });
806 auto it = result.get_it();
826 auto result =
group_by(
l, [] (
int x) {
return x % 2; });
829 auto it = result.get_it();
843 auto result =
group_by(empty, [] (
int x) {
return x; });
863 [] (
int x) {
return x; },
870 auto it = result.get_it();
890 auto result =
maps<int>(
l, [] (
int x) {
return x * x; });
930 auto left =
l.
take(3);
931 auto right =
l.
drop(3);
962 auto result =
l.
take(3, 7);
971 for (
int i = 0; i < 5; ++i)
982 for (
int i = 0; i < 5; ++i)
998 if (x % 2 == 0) ++
count;
1009 constexpr size_t N = 10000;
1017 auto evens =
filter(
l, [] (
size_t x) {
return x % 2 == 0; });
1032 auto result =
filter(
l, [] (
int x) {
return x % 2 == 0; });
1120 [] (
int x) {
return x * x; });
1129 filter(
l, [] (
int x) {
return x % 3 == 0; }),
1130 [] (
int x) {
return "val" +
to_string(x); }
1236 for (
int i = 0; i < x; ++i)
1341 return a.
length() < b.length();
1347 return a.
length() < b.length();
1374 auto result =
foldl<string>(words,
"START", [] (
const string &
acc,
const string & x) {
1375 return "(" +
acc +
"+" + x +
")";
1388 auto result =
foldr<string>(words,
"END", [] (
const string & x,
const string &
acc) {
1389 return "(" + x +
"+" +
acc +
")";
1435 EXPECT_EQ(it.get_curr().second.size(), 3);
1438 EXPECT_EQ(it.get_curr().second.size(), 3);
1441 EXPECT_EQ(it.get_curr().second.size(), 2);
1448 constexpr size_t N = 10000;
1463 for (
int i = 0; i < 100; ++i)
1479 [] (
int x) {
return x * 2; }),
1480 [] (
int x) {
return x - 1; });
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)
Append a new item by copy.
T & get_last() const
Return the last item of the list.
T & get_first() const
Return the first 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.
constexpr bool is_empty() const noexcept
Return true if list is empty.
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 container satisfy a condition.
size_t length() const noexcept
Count the number of elements of a container.
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 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.
bool zip_all(Op &&op, const Cs &... cs)
Return true if op returns true for all tuples and containers have equal length.
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)
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.
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.
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.
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).
DynList< typename Container::Item_Type > sublist(const Container &c, size_t pos, size_t stride)
Extract a sublist using a stride.
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 streamable 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.
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.
void each(size_t start, size_t end, Op &op)
Execute an operation repeatedly over a range of indices.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
DynList< T > rep(size_t n, const T &item)
Create a sequence of repeated items.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Represents a missing value.
virtual T & get_item() override
virtual bool is_found() const noexcept override
Represents a found value (stored by reference).
virtual bool is_found() const noexcept override
virtual T & get_item() override
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 balanced binary search trees.
Open addressing hash table with double hashing.