44#include <gtest/gtest.h>
114 std::vector<int> v = {1, 2, 3, 4, 5};
122 std::vector<std::string> v = {
"a",
"b",
"c"};
123 std::vector<std::string>
results;
125 results.push_back(std::to_string(i) +
":" + s);
134 std::vector<int> v = {1, 2, 3, 4, 5};
145 std::vector<int> v = {1, 2, 3};
146 auto strings =
stl_map([](
int x) {
return std::to_string(x); }, v);
154 std::vector<std::string> v = {
"a",
"b",
"c"};
156 return std::to_string(i) + s;
165 std::vector<int> v = {1, 2, 3, 4, 5, 6};
175 std::vector<int> v = {10, 20, 30, 40, 50};
190 std::vector<int> v = {1, 2, 3, 4, 5};
197 std::vector<int> v = {1, 2, 3, 4};
205 std::vector<int> v = {1, 2, 3};
213 std::vector<int> v = {1, 2, 3};
214 std::string result =
stl_foldr(std::string(
""), [](
int x, std::string
acc) {
215 return std::to_string(x) +
acc;
222 std::vector<int> v = {1, 2, 3, 4};
235 std::vector<int> v = {1, 2, 3};
252 std::vector<int> v = {2, 4, 6, 8};
258 std::vector<int> v = {2, 3, 6, 8};
264 std::vector<int> v = {1, 2, 3, 4, 5};
270 std::vector<int> v = {1, 2, 3, 4, 5};
276 std::vector<int> v = {1, 3, 5, 7};
286 std::vector<int> v = {1, 2, 3, 4, 5};
287 auto result =
stl_find([](
int x) {
return x > 3; }, v);
295 std::vector<int> v = {1, 2, 3};
296 auto result =
stl_find([](
int x) {
return x > 10; }, v);
303 std::vector<int> v = {1, 2, 3, 4, 5};
304 auto result =
stl_find_last([](
int x) {
return x % 2 == 0; }, v);
312 std::vector<std::string> v = {
"a",
"b",
"c",
"d"};
313 auto result =
stl_find_index([](
const std::string& s) {
return s ==
"c"; }, v);
321 std::vector<int> v = {1, 2, 3, 4, 5};
322 auto result =
stl_find_mapi([](
size_t i,
int x) -> std::optional<std::string> {
324 return "found at " + std::to_string(i);
334 std::vector<int> v = {1, 2, 3, 4, 5};
345 std::vector<int> v = {1, 2, 3, 4, 5, 6};
352 std::vector<int> v = {1, 2, 2, 3, 2, 4};
362 std::vector<int> v = {1, 2, 3, 4, 5};
372 std::vector<int> v = {1, 2, 3, 4, 5};
382 std::vector<int> v = {1, 2, 3, 4, 5};
392 std::vector<int> v = {1, 2, 3, 10, 4, 5};
401 std::vector<int> v = {1, 2, 3, 10, 4, 5};
414 std::vector<int> v = {10, 20, 30};
431 std::vector<int> v = {10, 20, 30};
440 std::vector<int> v = {10, 20, 30, 40, 50};
453 std::vector<int> v = {3, 1, 4, 1, 5, 9};
462 std::vector<int> v = {3, 1, 4, 1, 5, 9};
471 std::vector<int> v = {3, 1, 4, 1, 5, 9};
481 std::vector<std::string> v = {
"hello",
"a",
"world"};
482 auto result =
stl_min_by([](
const std::string& s) {
return s.length(); }, v);
490 std::vector<std::string> v = {
"hello",
"a",
"world"};
491 auto result =
stl_max_by([](
const std::string& s) {
return s.length(); }, v);
503 std::vector<int> v = {1, 2, 3, 4, 5};
509 std::vector<int> v = {1, 2, 3, 4};
519 std::vector<int> v = {1, 2, 3, 4, 5, 6};
532 std::vector<int> v1 = {1, 2, 3};
533 std::vector<std::string> v2 = {
"a",
"b",
"c"};
544 std::vector<std::pair<int, std::string>> v = {{1,
"a"}, {2,
"b"}, {3,
"c"}};
556 std::vector<std::string> v = {
"a",
"b",
"c"};
572 std::vector<int> v1 = {1, 2, 3};
573 std::vector<int> v2 = {1, 2, 3};
580 std::vector<int> v1 = {1, 2, 3};
581 std::vector<int> v2 = {1, 2, 4};
588 std::vector<int> v1 = {1, 2, 3};
589 std::vector<int> v2 = {1, 2, 3};
596 std::vector<int> v1 = {1, 2, 3};
597 std::vector<int> v2 = {1, 2, 4};
604 std::vector<int> v1 = {1, 2, 4};
605 std::vector<int> v2 = {1, 2, 3};
616 std::vector<int> v = {1, 2, 3, 4, 5};
625 std::vector<int> v = {3, 1, 4, 1, 5, 9};
634 std::vector<std::string> v = {
"hello",
"a",
"world"};
635 auto result =
stl_sort_by([](
const std::string& a,
const std::string& b) {
636 return a.length() < b.length();
649 std::vector<int> v = {1, 1, 2, 2, 2, 3, 3};
660 std::vector<int> v = {1, 2, 1, 3, 2, 4};
672 std::vector<int> v1 = {1, 2, 3};
673 std::vector<int> v2 = {4, 5, 6};
684 std::vector<std::vector<int>> v = {{1, 2}, {3, 4}, {5}};
695 std::vector<int> v = {1, 2, 3};
697 return std::vector<int>{x, x * 10};
713 std::vector<int> v = {1, 1, 2, 2, 2, 3};
725 std::vector<std::string> v = {
"a",
"bb",
"c",
"dd",
"eee"};
727 auto result =
stl_group_by([](
const std::string& s) {
return s.length(); }, v);
738 std::list<int>
l = {1, 2, 3, 4, 5};
754 std::deque<int> d = {1, 2, 3, 4, 5};
766 std::vector<int> v = {1, 2, 3};
774 std::vector<int> v = {1, 2, 3};
788 std::vector<int> v = {1, 2, 3, 4};
801 std::vector<int> v = {1, 2, 3, 4, 5};
810 std::vector<int> v = {1, 2, 3};
819 std::vector<std::vector<int>>
sets = {{1, 2}, {3, 4}};
828 std::vector<int> v = {1, 2, 3};
841 std::vector<int> v = {1, 2, 3, 4, 5};
852 std::vector<int> v = {1, 2, 3, 4, 5};
863 std::vector<int> v = {1, 2, 3};
866 EXPECT_EQ(result, (std::vector<int>{1, 0, 2, 0, 3}));
871 std::vector<int> v = {1, 2, 3, 4, 5};
874 EXPECT_EQ(first, (std::vector<int>{1, 2}));
875 EXPECT_EQ(second, (std::vector<int>{3, 4, 5}));
880 std::vector<int> v = {1, 2, 3, 10, 4, 5};
881 auto [first, second] =
stl_span([](
int x) {
return x < 10; }, v);
883 EXPECT_EQ(first, (std::vector<int>{1, 2, 3}));
884 EXPECT_EQ(second, (std::vector<int>{10, 4, 5}));
889 std::vector<int> v = {1, 2, 3, 4, 5};
892 EXPECT_EQ(result, (std::vector<int>{1, 2, 3, 4}));
897 std::vector<int> v = {1, 2, 3, 4, 5};
900 EXPECT_EQ(result, (std::vector<int>{2, 3, 4, 5}));
905 std::vector<int> v = {1, 2, 2, 3, 3, 3};
919 std::vector<int> v = {1, 2, 3, 4, 5, 6};
920 auto result =
stl_reject([](
int x) {
return x % 2 == 0; }, v);
922 EXPECT_EQ(result, (std::vector<int>{1, 3, 5}));
932 std::vector<int>
large;
934 for (
int i = 0; i < 1000; ++i)
935 large.push_back(i % 100);
951 for (
int i = 0; i < 10000; ++i)
961 std::vector<int>
large;
963 for (
int i = 0; i < 1000; ++i)
964 large.push_back(i % 100);
970 for (
const auto & [val,
count] : result)
976 std::vector<int>
large;
978 for (
int i = 0; i < 1000; ++i)
985 for (
const auto & [key,
group] : result)
995 std::vector<int> empty;
1002 std::vector<int>
single = {42};
1010 std::vector<int>
all_same(100, 42);
1025 std::vector<int> empty;
1032 std::vector<int>
single = {42};
1041 std::vector<int> empty;
1042 auto result =
stl_group_by([](
int x) {
return x; }, empty);
1048 std::vector<int>
single = {42};
1057 std::vector<int> v = {10, 20, 30, 40, 50};
1058 auto result =
stl_group_by([](
int x) {
return x % 10; }, v);
1072 for (
int i = 0; i < 64; ++i)
1081 std::vector<int>
small = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
1109 size_t operator()(
const Point & p)
const
1111 return std::hash<int>{}(p.x) ^ (std::hash<int>{}(p.y) << 1);
1131 std::vector<Point> points = {{1, 2}, {3, 4}, {1, 2}, {5, 6}, {3, 4}};
1141 std::vector<Point> points;
1142 points.reserve(200);
1143 for (
int i = 0; i < 200; ++i)
1144 points.push_back({i % 50, i % 25});
1154 std::vector<Point> points = {{1, 2}, {3, 4}, {1, 2}, {5, 6}, {1, 2}};
1159 auto it = std::find_if(result.begin(), result.end(),
1160 [](
const auto & p) { return p.first.x == 1 && p.first.y == 2; });
1167 std::vector<Point> points = {
1168 {1, 1}, {-1, 1}, {-1, -1}, {1, -1},
1169 {2, 3}, {-2, 3}, {-2, -3}, {2, -3}
1173 if (p.x >= 0 && p.y >= 0)
return 1;
1174 if (p.x < 0 && p.y >= 0)
return 2;
1175 if (p.x < 0 && p.y < 0)
return 3;
1182 for (
const auto & [
quad,
pts] : result)
1195 static int move_count;
1196 static int copy_count;
1198 MoveTracker(
int v = 0) :
value(v) {}
1211 MoveTracker & operator=(
const MoveTracker &
other)
1218 MoveTracker & operator=(MoveTracker &&
other)
noexcept
1238int MoveTracker::move_count = 0;
1239int MoveTracker::copy_count = 0;
1245struct hash<MoveTracker>
1256 std::vector<int> v = {1, 2, 3};
1257 std::string
prefix =
"num_";
1260 return prefix + std::to_string(x);
1270 std::vector<int> v = {1, 2, 3, 4, 5};
1275 auto result =
stl_filter([&call_count, threshold](
int x)
mutable {
1277 return x > threshold;
1286 std::vector<std::string> words = {
"hello",
" ",
"world"};
1288 auto result =
stl_foldl(std::string{}, [](std::string
acc,
const std::string &
w) {
1301 std::list<int>
l = {1, 2, 1, 3, 2, 4};
1308 std::deque<int> d = {1, 2, 1, 3, 2, 4};
1315 std::list<int>
l = {1, 2, 2, 3, 3, 3};
1322 std::list<std::string>
l = {
"a",
"bb",
"ccc",
"dd",
"e"};
1323 auto result =
stl_group_by([](
const std::string & s) {
return s.length(); },
l);
1333 std::vector<int> v = {5, 3, 5, 1, 3, 7, 1, 5};
1348 for (
int i = 199; i >= 0; --i)
1349 v.push_back(i % 100);
1362 std::vector<int> v = {5, 3, 5, 1, 3, 7, 1, 5};
1378 std::vector<int> v = {15, 23, 31, 42, 54};
1379 auto result =
stl_group_by([](
int x) {
return x % 10; }, v);
1394 std::vector<std::string> v = {
"apple",
"banana",
"apple",
"cherry",
"banana"};
1405 std::vector<std::string> v;
1407 for (
int i = 0; i < 200; ++i)
1408 v.push_back(
"str_" + std::to_string(i % 50));
1416 std::vector<std::string> v = {
"a",
"bb",
"ccc",
"dd",
"eee",
"f"};
1417 auto result =
stl_group_by([](
const std::string & s) {
return s.length(); }, v);
1424 std::vector<std::string> v = {
"apple",
"apricot",
"banana",
"blueberry",
"cherry"};
1425 auto result =
stl_group_by([](
const std::string & s) {
return s[0]; }, v);
1437 for (
int i = 0; i < 63; ++i)
1438 v.push_back(i % 30);
1447 for (
int i = 0; i < 64; ++i)
1448 v.push_back(i % 30);
1457 for (
int i = 0; i < 65; ++i)
1458 v.push_back(i % 30);
1470 std::vector<int> v = {1, 2, 1, 3, 2, 4};
1479 std::vector<int> v = {1, 2, 3, 4, 5, 6, 1, 2, 3};
1488 std::vector<int> v = {1, 2, 3, 4, 5, 6};
1491 return std::make_pair(p.first,
stl_foldl(0, std::plus<int>{}, p.second));
1501#include <forward_list>
1505 std::forward_list<int>
fl = {1, 2, 3, 4, 5};
1506 auto result =
stl_map([](
int x) {
return x * 2; },
fl);
1515 std::forward_list<int>
fl = {1, 2, 3, 4, 5, 6};
1516 auto result =
stl_filter([](
int x) {
return x % 2 == 0; },
fl);
1524 std::forward_list<int>
fl = {1, 2, 3, 4, 5};
1532 std::forward_list<int>
fl = {1, 2, 1, 3, 2, 4};
1540 std::forward_list<int>
fl = {1, 2, 2, 3, 3, 3};
1548 std::forward_list<int>
fl = {1, 2, 3, 4, 5, 6};
1556 std::forward_list<int>
fl = {1, 2, 3, 4, 5};
1565 std::forward_list<int>
fl = {1, 2, 3, 4, 5};
1575 std::forward_list<int>
fl = {1, 2, 3, 4, 5};
1603 std::vector<NonHashable> v = {{1, 2}, {3, 4}, {1, 2}, {5, 6}};
1612 std::vector<NonHashable> v;
1614 for (
int i = 0; i < 200; ++i)
1615 v.push_back({i % 50, i % 25});
1625 std::vector<NonHashable> v = {{1, 2}, {3, 4}, {1, 2}, {1, 2}};
1630 auto it = std::find_if(result.begin(), result.end(),
1631 [](
const auto & p) { return p.first.x == 1 && p.first.y == 2; });
1639 std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
1643 return NonHashable{x % 2, x % 3};
1657struct StatefulCallable
1662 explicit StatefulCallable(
int t) : threshold(t) {}
1664 bool operator()(
int x)
1667 return x > threshold;
1671struct StatefulMapper
1676 explicit StatefulMapper(
int m) : multiplier(
m) {}
1678 int operator()(
int x)
1681 return x * multiplier;
1689 std::vector<int> v = {1, 2, 3, 4, 5};
1692 StatefulCallable
pred(3);
1702 std::vector<int> v = {1, 2, 3};
1704 StatefulMapper
mapper(10);
1715 std::vector<int> v = {1, 2, 3, 4, 5};
1732 std::vector<int> v = {2, 4, 6, 8, 10};
1735 bool result =
stl_all([&call_count](
int x) {
1746 std::vector<int> v = {1, 2, 3, 4, 5};
1749 bool result =
stl_exists([&call_count](
int x) {
1764 std::vector<int> empty;
1771 std::vector<int> empty;
1778 std::vector<int> empty;
1785 std::vector<int> empty;
1792 std::vector<int> empty;
1799 std::vector<int> empty;
1800 auto result =
stl_scan_left(42, [](
int a,
int b) {
return a + b; }, empty);
1808 std::vector<int> empty;
1809 auto result =
stl_scan_right(42, [](
int a,
int b) {
return a + b; }, empty);
1817 std::vector<int> empty;
1824 std::vector<int> empty;
1831 std::vector<int> empty;
1843 std::vector<int>
large(64);
1851 std::vector<int>
small = {1, 2, 3, 4, 5};
1860 ::testing::InitGoogleTest(&
argc,
argv);
Functional programming utilities for C++ Standard Library containers.
size_t size_t int32_t value
Node belonging to a double circular linked list with header node.
Doubly-linked list (defined in tpl_dynList.H).
Represents a point with rectangular coordinates in a 2D plane.
bool operator==(const Point &point) const noexcept
Checks for exact equality between two points.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
auto stl_first(const Container &c)
Get first element.
std::vector< T > stl_scan_left(T init, Op &&op, const Container &c)
Scan left - fold with all intermediate results.
auto stl_span(Pred &&pred, const Container &c)
Split at predicate boundary (span in Haskell).
auto stl_take_last(size_t n, const Container &c)
Take last n elements.
auto stl_chunks(size_t n, const Container &c)
Split container into chunks of size n (each_slice in Ruby).
int stl_compare(const Container1 &c1, const Container2 &c2)
Compare two containers lexicographically.
auto stl_min_max(const Container &c)
Get both min and max in a single pass.
auto stl_last(const Container &c)
Get last element.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
bool completed() const noexcept
Return true if all underlying iterators are finished.
auto stl_distinct(const Container &c)
Remove all duplicates (keeps first occurrence).
auto stl_max(const Container &c)
Get maximum element.
size_t size(Node *root) noexcept
auto stl_flatten(const Container &c)
Flatten a container of containers.
auto stl_reject(Pred &&pred, const Container &c)
Filter out elements (reject in Ruby, opposite of filter).
auto stl_filter(Pred &&pred, const Container &c)
Filter elements satisfying predicate.
auto stl_arrangements(size_t k, const Container &c)
Generate all k-arrangements (k-permutations) of a container.
auto stl_drop(size_t n, const Container &c)
Drop first n elements, return the rest.
auto stl_enumerate_to_pairs(const Container &c)
Enumerate container (return pairs of index and element).
bool stl_equal(const Container1 &c1, const Container2 &c2)
Check equality of two containers.
auto stl_concat(const Container1 &c1, const Container2 &c2)
Concatenate two containers.
auto stl_combinations(size_t k, const Container &c)
Generate all k-combinations of a container.
auto stl_take_while(Pred &&pred, const Container &c)
Take elements while predicate is true.
auto stl_find_mapi(Op &&op, const Container &c)
Find and map with index (find_mapi in ML).
auto stl_nth(const size_t n, const Container &c)
Get n-th element.
auto stl_map(Op &&op, const Container &c)
Map operation - transform each element.
bool stl_mem(const T &target, const Container &c)
Check if element exists in container (mem in ML).
std::optional< size_t > stl_find_index(Pred &&pred, const Container &c)
Find index of first element satisfying predicate.
T stl_foldl(T init, Op &&op, const Container &c)
Left fold (foldl) - reduce from left to right.
auto stl_min_by(Key &&key, const Container &c)
Get minimum element by key function.
std::vector< T > stl_linspace(T start, T end, size_t n)
Generate n evenly spaced values between start and end.
bool stl_exists(Pred &&pred, const Container &c)
Check if any element satisfies predicate.
auto stl_take(size_t n, const Container &c)
Take first n elements.
auto stl_generate(size_t n, Gen &&gen)
Generate a vector using a generator function.
std::vector< T > stl_range(T start, T end, T step=1)
Generate a range of values [start, end] with given step.
auto stl_init(const Container &c)
Get all elements except the last (init in Haskell).
bool stl_traverse_permutations(Op &&op, const Container &c)
Traverse all permutations of a container.
size_t stl_count_value(const T &target, const Container &c)
Count occurrences of a value.
auto stl_sort(const Container &c)
Return sorted copy of container.
static void prefix(Node *root, DynList< Node * > &acc)
auto stl_permutations(const Container &c)
Generate all permutations of a container.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
auto stl_tally(const Container &c)
Count occurrences of each element (tally in Ruby, frequencies).
bool stl_all(Pred &&pred, const Container &c)
Check if all elements satisfy predicate.
auto stl_partition(Pred &&pred, const Container &c)
Partition elements by predicate.
auto stl_unique(const Container &c)
Remove consecutive duplicates.
auto stl_mapi(Op &&op, const Container &c)
Map with index (mapi in ML).
size_t stl_count(Pred &&pred, const Container &c)
Count elements satisfying predicate.
void stl_for_each_indexed(Op &&op, const Container &c)
Apply operation to each element with index.
auto stl_sum(const Container &c)
Sum all elements.
auto stl_sliding_window(size_t n, const Container &c)
Sliding window of size n over container (each_cons in Ruby).
auto stl_max_by(Key &&key, const Container &c)
Get maximum element by key function.
auto stl_group_by(Key &&key, const Container &c)
Group elements by key function.
auto stl_min(const Container &c)
Get minimum element.
bool stl_none(Pred &&pred, const Container &c)
Check if no element satisfies predicate.
auto stl_reverse(const Container &c)
Return reversed copy of container.
auto stl_flat_map(Op &&op, const Container &c)
Flat map - map then flatten.
std::vector< T > stl_rep(size_t n, const T &value)
Generate a vector of n repeated values.
T stl_foldr(T init, Op &&op, const Container &c)
Right fold (foldr) - reduce from right to left.
std::vector< T > stl_scan_right(T init, Op &&op, const Container &c)
Scan right - right fold with all intermediate results.
auto stl_unzip_pairs(const Container &c)
Unzip pairs into two vectors.
auto stl_drop_while(Pred &&pred, const Container &c)
Drop elements while predicate is true, return the rest.
auto stl_intersperse(const T &sep, const Container &c)
Insert element between each pair (intersperse in Haskell).
auto stl_cartesian_product(const std::vector< std::vector< T > > &containers)
Generate cartesian product of multiple containers.
auto stl_product(const Container &c)
Product of all elements.
auto stl_tail(const Container &c)
Get all elements except the first (tail in Haskell).
auto stl_filteri(Pred &&pred, const Container &c)
Filter with index (filteri in ML).
auto stl_find(Pred &&pred, const Container &c)
Find first element satisfying predicate.
void stl_for_each(Op &&op, const Container &c)
Apply operation to each element (for_each).
auto stl_split_at(size_t n, const Container &c)
Split at position n, returning (take n, drop n) in one pass.
auto stl_sort_by(Cmp &&cmp, const Container &c)
Return sorted copy using custom comparator.
auto stl_power_set(const Container &c)
Generate power set (all subsets) of a container.
auto stl_group(const Container &c)
Group consecutive equal elements.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
auto stl_zip_to_pairs(const Container1 &c1, const Container2 &c2)
Zip two containers into pairs.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
auto stl_find_last(Pred &&pred, const Container &c)
Find last element satisfying predicate.
size_t operator()(const MoveTracker &m) const
size_t operator()(const Point &p) const
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)