48#include <gtest/gtest.h>
57std::vector<T> vec_of(
const Array<T> &a)
63TEST(CombGenerators, PermutationsOfEmptyAndSingleton)
67 { (void) p; ++
count; }
70 std::vector<std::vector<int>> seen;
72 seen.push_back(vec_of(p));
73 EXPECT_EQ(seen, (std::vector<std::vector<int>>{{7}}));
76TEST(CombGenerators, PermutationsMatchStdNextPermutationCount)
78 std::vector<int> ref = {1, 2, 3, 4};
79 size_t eager_count = 0;
82 while (std::next_permutation(ref.begin(), ref.end()));
84 size_t lazy_count = 0;
86 { (void) p; ++lazy_count; }
88 EXPECT_EQ(eager_count, 24u);
89 EXPECT_EQ(lazy_count, eager_count);
92TEST(CombGenerators, PermutationsMatchStdNextPermutationContent)
94 std::vector<std::vector<int>> eager;
95 std::vector<int> ref = {1, 2, 3};
96 std::sort(ref.begin(), ref.end());
99 while (std::next_permutation(ref.begin(), ref.end()));
101 std::vector<std::vector<int>> lazy;
103 lazy.push_back(vec_of(p));
105 EXPECT_EQ(lazy, eager);
108TEST(CombGenerators, PermutationsWithDuplicatesCollapseLikeStd)
110 std::vector<int> ref = {1, 1, 2};
111 std::sort(ref.begin(), ref.end());
112 size_t eager_count = 0;
115 while (std::next_permutation(ref.begin(), ref.end()));
117 size_t lazy_count = 0;
119 { (void) p; ++lazy_count; }
121 EXPECT_EQ(eager_count, 3u);
122 EXPECT_EQ(lazy_count, eager_count);
125TEST(CombGenerators, PermutationsAreDistinctAsMultiset)
127 std::set<std::vector<int>> seen;
129 seen.insert(vec_of(p));
130 EXPECT_EQ(seen.size(), 24u);
133TEST(CombGenerators, PermutationsEarlyBreakStopsWork)
145TEST(CombGenerators, CombinationsKZeroYieldsOneEmptyCombination)
147 std::vector<std::vector<int>> seen;
149 seen.push_back(vec_of(c));
150 ASSERT_EQ(seen.size(), 1u);
151 EXPECT_TRUE(seen[0].empty());
154TEST(CombGenerators, CombinationsKEqualsNYieldsWholeSetOnce)
156 std::vector<std::vector<int>> seen;
158 seen.push_back(vec_of(c));
159 ASSERT_EQ(seen.size(), 1u);
160 EXPECT_EQ(seen[0], (std::vector<int>{1, 2, 3}));
163TEST(CombGenerators, CombinationsKGreaterThanNThrows)
173TEST(CombGenerators, CombinationsCountMatchesCombinationCount)
176 for (
size_t k = 0;
k <= a.size(); ++
k)
180 { (void) c; ++
count; }
185TEST(CombGenerators, CombinationsMatchBruteForceBitmaskEnumeration)
188 const size_t n = a.
size();
194 std::vector<std::vector<int>>
expected;
195 for (uint64_t mask = 0; mask < (1u << n); ++mask)
197 if (
static_cast<size_t>(std::popcount(mask)) !=
k)
199 std::vector<int> combo;
200 for (
size_t i = 0; i < n; ++i)
201 if (mask & (1u << i))
202 combo.push_back(a[i]);
207 std::vector<std::vector<int>> got;
209 got.push_back(vec_of(c));
210 std::sort(got.begin(), got.end());
215TEST(CombGenerators, CombinationsAreDistinct)
218 std::set<std::vector<int>> seen;
224TEST(CombGenerators, CombinationsPreserveRelativeOrderOfSource)
227 std::vector<std::vector<char>> seen;
229 seen.push_back(vec_of(c));
230 EXPECT_EQ(seen, (std::vector<std::vector<char>>{{
'a',
'b'}, {
'a',
'c'}, {
'b',
'c'}}));
233TEST(CombGenerators, CombinationSnapshotsSurviveGeneratorAdvancement)
237 auto it = gen.
begin();
238 ASSERT_NE(it, gen.
end());
242 ASSERT_NE(it, gen.
end());
245 EXPECT_EQ(vec_of(first), (std::vector<int>{1, 2}));
246 EXPECT_EQ(vec_of(second), (std::vector<int>{1, 3}));
Lazy (coroutine-based) permutation and combination enumeration.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Lazy, single-pass sequence of T values produced by a coroutine.
static std::default_sentinel_t end() noexcept
End sentinel, per the C++20 sentinel-based range model.
iterator begin() &
Begin iterating: resumes the coroutine to its first co_yield.
Minimal std::expected-style result type for C++20.
Main namespace for Aleph-w library functions.
Aleph::Generator< Array< T > > lazy_permutations(Array< T > a, Compare cmp=Compare())
Lazily enumerate all permutations of a, in lexicographic order.
size_t combination_count(size_t n, size_t k)
Compute n choose k with overflow checks.
Aleph::Generator< Array< T > > lazy_combinations(Array< T > a, size_t k)
Lazily enumerate all k-element combinations of a's elements.
std::vector< typename Container::Item_Type > to_stdvector(const Container &c)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.