38#include <gtest/gtest.h>
121 if (x > 3)
return false;
197 auto squared = v.
maps<
int>([](
int x) {
return x * x; });
214 string expected[] = {
"1",
"2",
"3"};
223 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
236 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
244 int sum = v.
foldl<
int>(0, [](
int acc,
int x) {
return acc + x; });
255 int result = v.
foldl<
int>(42, [](
int acc,
int x) {
return acc + x; });
291 auto reversed = v.
rev();
326 auto empty = v.
drop(100);
368 int* p = v.
find_ptr([](
int x) {
return x == 3; });
380 int* p = v.
find_ptr([](
int x) {
return x == 3; });
391 auto [found, item] = v.
find_item([](
int x) {
return x > 3; });
416 auto items = v.
items();
430 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
445 auto squared = v.
maps<
int>([](
int x) {
return x * x; });
466 people.append({
"Bob", 25});
467 people.append({
"Charlie", 35});
478 int sum =
ages.foldl<
int>(0, [](
int acc,
int a) {
return acc + a; });
509 EXPECT_EQ(v.
sum(
string(
"Prefix: ")),
"Prefix: Hello World");
574 const string*
shortest = v.
min_by([](
const string& a,
const string& b) {
575 return a.length() < b.length();
586 const string*
longest = v.
max_by([](
const string& a,
const string& b) {
587 return a.length() < b.length();
652 auto is_odd = [](
int x) {
return x % 2 != 0; };
681 const int* ptr = v.
first();
705 const int* ptr = v.
last();
749 enumerated.for_each([&](
const std::pair<size_t, string>& p) {
806 auto evens = v.
filter([](
int x) {
return x % 2 == 0; });
807 int sum =
evens.foldl<
int>(0, [](
int acc,
int x) {
return acc + x; });
832 return acc + (p.first % 2 == 0 ? 1 : 0);
844 constexpr size_t N = 10000;
847 for (
size_t i = 0; i <
N; ++i)
851 size_t sum = v.
foldl<
size_t>(0, [](
size_t acc,
size_t x) {
return acc + x; });
858 auto evens = v.
filter([](
size_t x) {
return x % 2 == 0; });
882 constexpr size_t N = 1000;
885 for (
size_t i = 0; i <
N; ++i)
886 v.
append(
static_cast<int>(i * 2));
893 enumerated.for_each([&idx](
const std::pair<size_t, int>& p) {
895 EXPECT_EQ(p.second,
static_cast<int>(idx * 2));
1038 auto u = v.
unique_by([](
const string& a,
const string& b) {
1237 string s = v.
join(
string(
", "));
1244 string s = v.
join(
string(
"-"));
1251 string s = v.
join(
string(
", "));
1287 auto it =
zipped.get_it();
1360 for (
int i = 1; i <= 5; ++i)
1364 int sum = list.
foldl<
int>(0, [](
int acc,
int x) {
return acc + x; });
1368 auto evens = list.
filter([](
int x) {
return x % 2 == 0; });
1527 constexpr size_t N = 10000;
1531 for (
size_t i = 0; i <
N; ++i)
CRTP Mixins for container functionality (DRY principle).
static DynArray< int > make_dynarray(std::initializer_list< int > vals)
static Array< int > make_array(std::initializer_list< int > vals)
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
T & append()
Allocate a new entry to the end of array.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
CRTP Mixin providing functional programming operations.
size_t find_index(Predicate pred) const
Find the index of the first element satisfying a predicate.
bool all(Operation &operation) const
Test if all elements satisfy a predicate.
const Type * min_by(Compare cmp) const
Find the minimum element using a custom comparator.
auto mutable_for_each(Operation &operation) -> decltype(self())
Apply an operation to each element (mutable).
Container< Type > drop(const size_t n) const
Skip the first n elements.
Container< std::pair< size_t, Type > > enumerate() const
Enumerate elements with their indices.
Container< Container< Type > > chunk(size_t n) const
Split into chunks of fixed size.
size_t count_if(Predicate pred) const
Count elements satisfying a predicate.
DynListType to_dynlist() const
Convert container to DynList.
DynList< Type > filter(Operation &operation) const
Filter elements by a predicate.
const Type * max() const
Find the maximum element.
Container< Type > rev() const
Create a reversed copy.
Container< __Type > maps(Operation &operation) const
Transform elements using a mapping function.
Container< Container< Type > > sliding(size_t size, size_t step=1) const
Create sliding windows of fixed size.
Container< Type > take(const size_t n) const
Take the first n elements.
std::string join_str(const std::string &sep=", ") const
Join string elements with separator.
Container< Type > unique() const
Remove consecutive duplicate elements.
const Type * first() const
Get the first element.
Container< std::pair< Type, typename Other::Item_Type > > zip_with(const Other &other) const
Zip with another container.
bool none(Predicate &pred) const
Check if no element satisfies a predicate.
Type last_or(const Type &default_val) const
Get the last element or a default value.
bool has_value(const Type &val) const
Check if container has a value.
StringType join(const StringType &sep=StringType{", "}) const
Join elements into a string with separator.
bool exists(Operation &operation) const
Test if any element satisfies a predicate.
size_t index_of(const Type &val) const
Find the index of a specific value.
__Type foldl(const __Type &init, std::function< __Type(const __Type &, const Type &)> operation) const
Left fold (reduce) with initial value.
auto for_each(Operation &operation) const -> decltype(self())
Apply an operation to each element (read-only).
Type product(const Type &init) const
Compute the product of all elements.
size_t length() const noexcept
Count the number of elements.
Container< Type > unique_by(EqPred eq) const
Remove consecutive duplicates using a custom equality predicate.
Type sum(const Type &init=Type{}) const
Compute the sum of all elements.
const Type * max_by(Compare cmp) const
Find the maximum element using a custom comparator.
std::pair< DynList< Type >, DynList< Type > > partition(Operation &op) const
Partition elements by a predicate.
std::vector< Type > to_vector() const
Convert to std::vector.
Container< Type > intersperse(const Type &sep) const
Intersperse a separator between elements.
Type first_or(const Type &default_val) const
Get the first element or a default value.
const Type * last() const
Get the last element.
const Type * min() const
Find the minimum element.
constexpr bool is_empty() const noexcept
size_t size() const noexcept
Count the number of elements of the list.
CRTP Mixin for extracting keys from set-like containers.
Container< Type > items() const
Alias for keys().
Container< Type > keys() const
Extract all keys as a list.
CRTP Mixin providing element location operations.
std::tuple< bool, Type > find_item(Operation &operation)
Find element with success flag.
Type * find_ptr(Operation &operation)
Find the first element satisfying a predicate.
Type & nth_ne(const size_t n) const noexcept
Access the n-th element (unchecked).
Type & nth(const size_t n) const
Access the n-th element (bounds-checked).
CRTP Mixin providing traversal operations.
bool traverse(Operation &operation) const
Traverse all elements, applying an operation to each.
Mixin providing equality comparison for sequence containers.
bool equal_to(const Container &r) const
Equality test between this and r.
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Iterator(const MixinVector &c)
const MixinVector * container
bool has_curr() const noexcept
Simple test container using std::vector internally but with Aleph mixins.
void insert(const T &item)
bool operator==(const MixinVector &other) const
void append(const T &item)
MixinVector(std::initializer_list< T > init)
size_t size() const noexcept
bool is_empty() const noexcept
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
std::string tolower(const char *str)
Convert a C std::string to lower-case.
const Container::Item_Type * min_ptr(const Container &container, Cmp cmp=Cmp())
Find the minimum element in a container.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
bool completed() const noexcept
Return true if all underlying iterators are finished.
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.
std::decay_t< typename HeadC::Item_Type > T
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
const Container::Item_Type * max_ptr(const Container &container, Cmp cmp=Cmp())
Find the maximum element in a container.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
static std::atomic< bool > init
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Represents a missing value.
bool operator==(const EqOnlyType &other) const
bool operator<(const LtOnlyType &other) const
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.