48#include <gtest/gtest.h>
68 std::vector<int>
vi = {1, 2, 3, 4, 5};
69 std::vector<std::string>
vs = {
"a",
"b",
"c",
"d",
"e"};
70 std::list<double>
ld = {1.1, 2.2, 3.3, 4.4, 5.5};
76 for (
auto [i, s] :
stl_zip(vi, vs))
88 for (
auto [i, s, d] :
stl_zip(vi, vs, ld))
113 std::vector<int> empty_vec;
116 for (
auto [i, s] :
stl_zip(empty_vec, vs))
127 std::vector<int>
single = {42};
128 std::vector<std::string>
single_s = {
"answer"};
148 std::vector<int> empty_vec;
162 std::vector<int> v = {1, 2, 3};
163 std::list<int>
l = {10, 20, 30};
165 std::vector<int>
sums;
167 sums.push_back(a + b);
177 std::vector<int> v = {1, 2, 3};
178 std::set<int> s = {100, 200, 300};
181 for (
auto [a, b] :
stl_zip(v, s))
191 std::vector<int> v = {1, 2, 3};
192 std::deque<std::string> d = {
"x",
"y",
"z"};
195 for (
auto [num, str] :
stl_zip(v, d))
206 std::array<int, 3> a1 = {1, 2, 3};
207 std::array<int, 3> a2 = {10, 20, 30};
221 std::vector<int> v1 = {1, 2};
222 std::vector<int> v2 = {10, 20};
223 std::vector<int>
v3 = {100, 200};
224 std::vector<int>
v4 = {1000, 2000};
247 std::vector<int>
xs = {1, 2, 3};
248 std::vector<int>
ys = {10, 20, 30};
251 return std::get<0>(t) < std::get<1>(t);
259 std::vector<int>
xs = {1, 200, 3};
260 std::vector<int>
ys = {10, 20, 30};
263 return std::get<0>(t) < std::get<1>(t);
280 std::vector<int>
xs = {1, 2, 3};
281 std::vector<int>
ys = {10, 20, 30};
284 return std::get<0>(t) < std::get<1>(t);
301 std::vector<int>
xs = {1, 2, 3};
302 std::vector<int>
ys = {10, 20, 30};
305 return std::get<0>(t) == 2;
313 std::vector<int>
xs = {1, 2, 3};
314 std::vector<int>
ys = {10, 20, 30};
317 return std::get<0>(t) == 99;
334 std::vector<int>
xs = {1, 2, 3};
335 std::vector<int>
ys = {10, 20, 30};
338 return std::get<0>(t) > 2;
346 std::vector<int>
xs = {1, 2, 3};
347 std::vector<int>
ys = {10, 20, 30};
350 return std::get<0>(t) > 100;
362 std::vector<int>
xs = {1, 2, 3};
363 std::vector<int>
ys = {10, 20, 30};
365 std::vector<int>
sums;
367 sums.push_back(std::get<0>(t) + std::get<1>(t));
378 std::vector<int>
xs = {10, 20, 30};
379 std::vector<std::string>
ys = {
"a",
"b",
"c"};
398 std::vector<int>
prices = {10, 20, 30};
402 return acc + std::get<0>(t) * std::get<1>(t);
411 std::vector<int>
nums = {1, 2, 3};
412 std::vector<std::string>
strs = {
"a",
"b",
"c"};
414 std::string result =
stl_zip_foldl(std::string(
""), [](std::string
acc,
auto t) {
415 return acc + std::to_string(std::get<0>(t)) + std::get<1>(t);
423 std::vector<int>
xs = {1, 2, 3};
424 std::vector<int>
ys = {10, 20, 30};
427 return acc + std::get<0>(t) + std::get<1>(t);
439 std::vector<int>
xs = {1, 2, 3};
440 std::vector<int>
ys = {10, 20, 30};
443 return std::get<0>(t) + std::get<1>(t);
454 std::vector<int>
xs = {1, 2, 3};
455 std::vector<int>
ys = {10, 20, 30};
458 return std::to_string(std::get<0>(t)) +
"+" + std::to_string(std::get<1>(t));
473 std::vector<int>
xs = {1, 2, 3, 4, 5};
474 std::vector<int>
ys = {10, 20, 30, 40, 50};
477 return std::get<0>(t) % 2 == 0;
485 std::vector<int>
xs = {1, 3, 5};
486 std::vector<int>
ys = {10, 30, 50};
489 return std::get<0>(t) % 2 == 0;
501 std::vector<int>
xs = {1, 2, 3, 4, 5};
502 std::vector<std::string>
ys = {
"a",
"b",
"c",
"d",
"e"};
505 return std::get<0>(t) == 3;
515 std::vector<int>
xs = {1, 2, 3};
516 std::vector<int>
ys = {10, 20, 30};
519 return std::get<0>(t) == 99;
531 std::vector<int>
xs = {1, 2, 3, 4, 5};
532 std::vector<int>
ys = {10, 20, 30, 40, 50};
535 return std::get<0>(t) % 2 == 0;
543 std::vector<int>
xs = {1, 2, 3, 4, 5};
544 std::vector<int>
ys = {10, 20, 30, 40, 50};
554 std::vector<int>
xs = {1, 2, 3};
555 std::vector<int>
ys = {10, 20, 30};
562 std::vector<int>
xs = {1, 2, 3};
563 std::vector<int>
ys = {10, 20};
574 std::vector<int>
xs = {1, 2, 3, 4, 5};
575 std::vector<std::string>
ys = {
"a",
"b",
"c",
"d",
"e"};
586 std::vector<int>
xs = {1, 2, 3};
587 std::vector<int>
ys = {10, 20, 30};
600 std::vector<int>
xs = {1, 2, 3, 4, 5};
601 std::vector<int>
ys = {10, 20, 30, 40, 50};
612 std::vector<int>
xs = {1, 2};
613 std::vector<int>
ys = {10, 20};
622 std::vector<int>
xs = {1, 2, 3, 4, 5};
623 std::vector<int>
ys = {10, 20, 30, 40, 50};
634 std::vector<int>
xs = {1, 2, 3};
635 std::vector<int>
ys = {10, 20, 30};
648 std::vector<int>
xs = {1, 2, 3, 4, 5};
649 std::vector<std::string>
ys = {
"a",
"b",
"c",
"d",
"e"};
652 return std::get<0>(t) % 2 == 0;
665 std::vector<int>
xs = {1, 2, 3};
666 std::vector<std::string>
ys = {
"a",
"b",
"c"};
681 std::vector<int>
xs = {1, 2, 3};
682 std::vector<int>
ys = {10, 20, 30};
685 return std::get<0>(t) < std::get<1>(t);
693 std::vector<int>
xs = {1, 200, 3};
694 std::vector<int>
ys = {10, 20, 30};
699 return std::get<0>(t) < std::get<1>(t);
712 std::vector<int> a = {1, 2, 3};
713 std::vector<int> b = {1, 2, 3};
714 std::vector<int> c = {1, 2, 3};
716 bool result =
stl_zip_cmp([](
auto x,
auto y) {
return x ==
y; }, a, b, c);
723 std::vector<int> a = {1, 2, 3};
724 std::vector<int> b = {1, 99, 3};
725 std::vector<int> c = {1, 2, 3};
727 bool result =
stl_zip_cmp([](
auto x,
auto y) {
return x ==
y; }, a, b, c);
747 std::vector<int> empty;
748 std::vector<int>
nonempty = {1, 2, 3};
756 std::vector<int>
xs(1000);
757 std::vector<int>
ys(1000);
762 return acc + std::get<0>(t) + std::get<1>(t);
777 std::vector<std::string>
names = {
"Alice",
"Bob",
"Charlie"};
797 std::vector<int> empty;
816 std::vector<int>
xs = {1, 2, 3, 10, 4, 5};
817 std::vector<int>
ys = {10, 20, 30, 100, 40, 50};
820 return std::get<0>(t) < 10;
830 std::vector<int>
xs = {10, 20, 30};
831 std::vector<int>
ys = {1, 2, 3};
834 return std::get<0>(t) < 5;
842 std::vector<int>
xs = {1, 2, 3, 10, 4, 5};
843 std::vector<int>
ys = {10, 20, 30, 100, 40, 50};
846 return std::get<0>(t) < 10;
855 std::vector<int>
xs = {1, 2, 3};
856 std::vector<int>
ys = {10, 20, 30};
859 return std::get<0>(t) < 100;
871 std::vector<int>
xs = {1, 2, 3};
872 std::vector<std::string>
ys = {
"a",
"b",
"c"};
892 std::vector<int>
xs = {1, 2, 3};
893 std::vector<std::string>
ys = {
"a",
"b",
"c"};
904 std::vector<int>
xs = {1, 2, 3, 4, 5};
905 std::vector<int>
ys = {10, 20, 30, 40, 50};
908 return std::get<0>(t) % 2 == 0;
917 std::vector<int>
xs = {1, 2, 3, 4, 5};
918 std::vector<int>
ys = {10, 20, 30, 40, 50};
921 return std::get<0>(t) == 3;
929 std::vector<int>
xs = {1, 2, 3};
930 std::vector<int>
ys = {10, 20, 30};
933 return std::get<0>(t) == 99;
945 std::vector<std::pair<int, std::string>>
pairs = {{1,
"a"}, {2,
"b"}, {3,
"c"}};
957 std::vector<std::tuple<int, double, char>>
tuples = {
958 {1, 1.1,
'a'}, {2, 2.2,
'b'}, {3, 3.3,
'c'}
977 std::vector<int> v = {1, 2, 3, 4, 5};
990 std::vector<int> v = {42};
1008 std::vector<int> v = {1, 3, 6, 10};
1020 std::vector<int>
sorted = {1, 2, 3, 4, 5};
1021 std::vector<int>
unsorted = {1, 3, 2, 4, 5};
1029 std::vector<int>
with_dup = {1, 2, 2, 3};
1030 std::vector<int>
no_dup = {1, 2, 3, 4};
1042 std::vector<int>
xs = {10, 20, 30};
1043 std::vector<int>
ys = {1, 2, 3};
1046 return std::to_string(i) +
":" + std::to_string(std::get<0>(t) + std::get<1>(t));
1057 std::vector<int>
xs = {10, 20, 30, 40, 50};
1058 std::vector<std::string>
ys = {
"a",
"b",
"c",
"d",
"e"};
1072 std::vector<int>
xs = {1, 2, 3, 4};
1073 std::vector<int>
ys = {10, 20, 30, 40};
1076 return acc + std::get<0>(t);
1089 std::vector<int>
xs = {1, 2, 3};
1090 std::vector<int>
ys = {10, 20, 30};
1093 return acc * std::get<0>(t) * std::get<1>(t);
1105 std::vector<int>
xs = {1, 2, 3, 4, 5};
1106 std::vector<std::string>
ys = {
"a",
"b",
"c",
"d",
"e"};
1108 auto result =
stl_zip_find_mapi([](
size_t i,
auto t) -> std::optional<std::string> {
1109 if (std::get<0>(t) == 3)
1110 return "found at " + std::to_string(i);
1111 return std::nullopt;
1120 std::vector<int>
xs = {1, 2, 3};
1121 std::vector<int>
ys = {10, 20, 30};
1124 if (std::get<0>(t) > 100)
1125 return std::get<0>(t);
1126 return std::nullopt;
1134 std::vector<int>
xs = {1, 2, 3};
1135 std::vector<int>
ys = {10, 20, 30};
1142 std::vector<int>
xs = {1, 2, 3, 4};
1143 std::vector<int>
ys = {10, 20, 30};
1150 std::vector<int>
xs = {1, 2, 3};
1151 std::vector<int>
ys = {10, 20, 30};
1154 return std::get<0>(t) * 10 == std::get<1>(t);
1162 std::vector<int>
xs = {1, 2, 3};
1163 std::vector<std::string>
ys = {
"a",
"b",
"c"};
1171 std::vector<std::string> keys = {
"one",
"two",
"three"};
1172 std::vector<int> values = {1, 2, 3};
1174 auto result =
stl_zip_assoc(std::string(
"two"), keys, values);
1183 std::vector<std::string> keys = {
"one",
"two",
"three"};
1184 std::vector<int> values = {1, 2, 3};
1186 auto result =
stl_zip_assoc(std::string(
"four"), keys, values);
1193 std::vector<int>
xs = {3, 1, 4, 1, 5};
1194 std::vector<int>
ys = {30, 10, 40, 10, 50};
1204 std::vector<int>
xs = {3, 1, 4, 1, 5};
1205 std::vector<int>
ys = {30, 10, 40, 10, 50};
1215 std::vector<int>
xs = {3, 1, 4, 1, 5};
1216 std::vector<int>
ys = {30, 10, 40, 10, 50};
1221 EXPECT_EQ(std::get<0>(result->first), 1);
1222 EXPECT_EQ(std::get<0>(result->second), 5);
1227 std::vector<int> empty;
1228 std::vector<int>
ys = {1, 2, 3};
1238 ::testing::InitGoogleTest(&
argc,
argv);
Lazy zip iterators and functional operations for STL containers.
TEST_F(StlZipViewTest, BasicIteration)
void empty() noexcept
empty the list
size_t size() const noexcept
Count the number of elements of the list.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
std::vector< std::string > vs
Main namespace for Aleph-w library functions.
auto stl_zip_filteri(Pred &&pred, const Containers &... cs)
Filter with index (filteri in ML).
auto stl_adjacent_map(Op &&op, const Container &c)
Apply function to adjacent pairs of elements.
bool stl_zip_exists(Pred &&pred, const Containers &... cs)
Check if predicate holds for any zipped tuple.
bool stl_zip_equal_by(Eq &&eq, const Containers &... cs)
Check equality with custom comparator.
bool stl_adjacent_exists(Pred &&pred, const Container &c)
Check if predicate holds for any adjacent pair.
auto stl_zip_find_last(Pred &&pred, const Containers &... cs)
Find last tuple satisfying predicate.
bool stl_zip_traverse(Pred &&pred, const Containers &... cs)
Traverse while predicate returns true.
bool stl_zip_all_eq(Pred &&pred, const Containers &... cs)
Check if predicate holds for all tuples AND containers have equal length.
auto stl_zip_min_max(const Containers &... cs)
Get both min and max in a single pass.
auto stl_zip_max(const Containers &... cs)
Get maximum tuple according to comparator.
bool stl_adjacent_all(Pred &&pred, const Container &c)
Check if predicate holds for all adjacent pairs.
auto stl_zip_take_while(Pred &&pred, const Containers &... cs)
Take tuples while predicate returns true.
size_t stl_zip_count(Pred &&pred, const Containers &... cs)
Count tuples satisfying predicate.
bool stl_zip_equal_length(const Containers &... cs)
Check if all containers have equal length.
auto stl_zip_drop_while(Pred &&pred, const Containers &... cs)
Skip tuples while predicate returns true, then return the rest.
bool stl_zip_cmp(Cmp &&cmp, const Containers &... cs)
Compare elements across containers using a comparator.
auto stl_zip_find_first(Pred &&pred, const Containers &... cs)
Find first tuple satisfying predicate.
bool stl_zip_mem(const Tuple &target, const Containers &... cs)
Check if a tuple exists in the zipped sequence (mem in ML).
auto stl_unzip_tuple(const std::vector< std::tuple< Ts... > > &tuples)
Unzip a vector of tuples into a tuple of vectors.
auto stl_zip_first(const Containers &... cs)
Get first tuple from zipped containers.
auto stl_zip_mapi(Op &&op, const Containers &... cs)
Map with index (mapi in ML).
size_t stl_zip_find_index(Pred &&pred, const Containers &... cs)
Find index of first tuple satisfying predicate.
bool stl_zip_none(Pred &&pred, const Containers &... cs)
Check if no tuple satisfies the predicate.
auto stl_zip_scan_left(T init, Op &&op, const Containers &... cs)
Scan left (scan_left in ML) - fold with intermediate results.
constexpr auto stl_enumerate(const Container &c)
Create an enumerate view over a container.
auto stl_zip_map(Op &&op, const Containers &... cs)
Map operation over zipped tuples, returning a vector.
bool stl_zip_equal(const Containers &... cs)
Check equality of zipped sequences.
auto stl_zip_find_mapi(Op &&op, const Containers &... cs)
Find and map with index (find_mapi in ML).
auto stl_zip_assoc(const Key &key, const Containers &... cs)
Find value associated with key in zipped pairs (assoc in ML).
auto stl_zip_filter(Pred &&pred, const Containers &... cs)
Filter zipped tuples by predicate.
auto stl_unzip(const std::vector< std::pair< T, U > > &pairs)
Unzip a vector of pairs into two vectors.
auto stl_zip_drop(const size_t n, const Containers &... cs)
Skip first n tuples, return the rest.
T stl_zip_foldl(T init, Op &&op, const Containers &... cs)
Left fold over zipped tuples.
auto stl_zip_min(const Containers &... cs)
Get minimum tuple according to comparator.
T stl_zip_reduce(T init, Op &&op, const Containers &... cs)
Alias for stl_zip_foldl (alternative name)
auto stl_zip_nth(const size_t n, const Containers &... cs)
Get n-th tuple from zipped containers.
auto stl_adjacent(const Container &c)
Zip adjacent (consecutive) elements of a container.
auto stl_zip_partition(Pred &&pred, const Containers &... cs)
Partition tuples by predicate.
constexpr auto stl_zip(const Containers &... cs)
Create a lazy zip view over STL containers.
auto stl_zip_take(const size_t n, const Containers &... cs)
Take first n tuples from zipped containers.
auto stl_zip_last(const Containers &... cs)
Get last tuple from zipped containers.
size_t stl_zip_length(const Containers &... cs)
Count total tuples (minimum length of containers).
void stl_zip_for_each(Op &&op, const Containers &... cs)
Apply operation to each zipped tuple (for side effects).
auto stl_zip_to_vector(const Containers &... cs)
Materialize zipped tuples into a vector.
void stl_zip_for_each_indexed(Op &&op, const Containers &... cs)
Apply operation to each tuple with its index.
bool stl_zip_all(Pred &&pred, const Containers &... cs)
Check if predicate holds for all zipped tuples.
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.
bool stl_zip_any(Pred &&pred, const Containers &... cs)
Alias for stl_zip_exists (Python/JS style name)
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.