38# include <gtest/gtest.h>
55 std::ostringstream oss;
68std::string seq_to_string(
const Seq & s)
70 std::ostringstream oss;
71 for (
size_t i = 0; i < s.size(); ++i)
80std::string vector_to_string(
const std::vector<int> & v)
82 std::ostringstream oss;
83 for (
size_t i = 0; i < v.size(); ++i)
93TEST(AhComb, TransposeEmpty)
102TEST(AhComb, TransposeRectangular)
120 ASSERT_EQ(m2.size(), 3u);
124TEST(AhComb, InPlaceTransposeDynArray)
146 ASSERT_EQ(
m.
size(), 3u);
147 ASSERT_EQ(
m(0).
size(), 2u);
148 EXPECT_EQ(
m(0)(0), 1);
149 EXPECT_EQ(
m(0)(1), 4);
150 EXPECT_EQ(
m(1)(0), 2);
151 EXPECT_EQ(
m(1)(1), 5);
152 EXPECT_EQ(
m(2)(0), 3);
153 EXPECT_EQ(
m(2)(1), 6);
156TEST(AhComb, TraversePermEnumeratesAll)
162 std::set<std::string> perms;
165 perms.
insert(list_to_string(p));
170 EXPECT_EQ(perms.size(), 4u);
171 EXPECT_TRUE(perms.contains(
"1,10"));
172 EXPECT_TRUE(perms.contains(
"2,10"));
173 EXPECT_TRUE(perms.contains(
"1,20"));
174 EXPECT_TRUE(perms.contains(
"2,20"));
177TEST(AhComb, TraversePermEarlyStop)
201 EXPECT_EQ(perms.size(), 2u);
203 std::set<std::string> s;
204 perms.for_each([&](
const DynList<int> & p) { s.insert(list_to_string(p)); });
205 EXPECT_TRUE(s.contains(
"1,10"));
206 EXPECT_TRUE(s.contains(
"2,10"));
209TEST(AhComb, BuildCombsDeduplicates)
219 EXPECT_EQ(combs.size(), 3u);
221 std::set<std::string> s;
222 combs.for_each([&](
const DynList<int> & c) { s.insert(list_to_string(c)); });
223 EXPECT_TRUE(s.contains(
"1,1"));
224 EXPECT_TRUE(s.contains(
"1,2"));
225 EXPECT_TRUE(s.contains(
"2,2"));
228TEST(AhComb, FoldPermAccumulates)
236 auto sum = fold_perm<size_t, int>(0u,
l, [](
size_t a,
const DynList<int> & p)
244TEST(AhComb, TraversePermEmptyInputCallsOnce)
255 EXPECT_EQ(calls, 1u);
258TEST(AhComb, BuildPermsEmptyInputReturnsOneEmptyPermutation)
262 ASSERT_EQ(perms.size(), 1u);
263 EXPECT_TRUE(perms.get_first().is_empty());
266TEST(AhComb, BuildCombsEmptyInputReturnsOneEmptyCombination)
270 ASSERT_EQ(combs.size(), 1u);
271 EXPECT_TRUE(combs.get_first().is_empty());
274TEST(AhComb, TransposeWithZeroColumns)
284 EXPECT_TRUE(m2.is_empty());
288TEST(AhComb, NonRectangularTransposeDies)
318TEST(AhComb, PermCountWithEmptyList)
327TEST(AhComb, PermCountSingleList)
335TEST(AhComb, ExistsPermFindsMatch)
363TEST(AhComb, ExistsPermStopsEarly)
376 EXPECT_EQ(calls, 1u);
423 EXPECT_EQ(calls, 1u);
426TEST(AhComb, NonePermNoneSatisfy)
440TEST(AhComb, NonePermSomeSatisfy)
465 EXPECT_EQ(filtered.size(), 4u);
479 EXPECT_TRUE(filtered.is_empty());
493 EXPECT_EQ(filtered.size(), 2u);
509 EXPECT_EQ(sums.size(), 4u);
512 sums.for_each([&s](
int v) { s.insert(v); });
514 EXPECT_TRUE(s.contains(11));
515 EXPECT_TRUE(s.contains(12));
516 EXPECT_TRUE(s.contains(21));
517 EXPECT_TRUE(s.contains(22));
526 auto strs = map_perm<std::string>(
l, [](
const DynList<int> & p)
528 return list_to_string(p);
531 EXPECT_EQ(strs.size(), 2u);
533 std::set<std::string> s;
534 strs.for_each([&s](
const std::string & v) { s.insert(v); });
535 EXPECT_TRUE(s.contains(
"1,10"));
536 EXPECT_TRUE(s.contains(
"2,10"));
545 std::set<std::string> perms;
548 perms.
insert(list_to_string(p));
551 EXPECT_EQ(perms.size(), 2u);
552 EXPECT_TRUE(perms.contains(
"1,10"));
553 EXPECT_TRUE(perms.contains(
"2,10"));
567 EXPECT_EQ(calls, 1u);
570TEST(AhComb, TraversePermThreeLists)
577 std::set<std::string> perms;
580 perms.
insert(list_to_string(p));
584 EXPECT_EQ(perms.size(), 8u);
587TEST(AhComb, TraversePermSingleElement)
592 std::set<std::string> perms;
595 perms.
insert(list_to_string(p));
599 EXPECT_EQ(perms.size(), 1u);
600 EXPECT_TRUE(perms.contains(
"42"));
603TEST(AhComb, TransposeSingleRow)
609 EXPECT_EQ(t.size(), 3u);
612 for (
auto it = t.get_it(); it.has_curr(); it.next_ne(), ++idx)
614 EXPECT_EQ(it.get_curr().size(), 1u);
615 EXPECT_EQ(it.get_curr().get_first(),
static_cast<int>(idx));
619TEST(AhComb, TransposeSingleColumn)
627 EXPECT_EQ(t.size(), 1u);
628 EXPECT_EQ(t.get_first().size(), 3u);
634TEST(AhComb, InPlaceTransposeSingleRow)
640 EXPECT_EQ(
m.
size(), 3u);
644 EXPECT_EQ(it.get_curr().get_first(), 1);
646 EXPECT_EQ(it.get_curr().get_first(), 2);
648 EXPECT_EQ(it.get_curr().get_first(), 3);
673 EXPECT_EQ(combs.size(), 1u);
679TEST(AhComb, BuildCombsThreeLists)
690 EXPECT_EQ(combs.size(), 4u);
693TEST(AhComb, FoldPermMultiplyFirstElements)
708TEST(AhComb, StressTestManyPermutations)
723 EXPECT_EQ(p.
size(), 5u);
728 EXPECT_EQ(
count, 243u);
731TEST(AhComb, NodiscardBuildPerms)
738 EXPECT_EQ(perms.size(), 2u);
741TEST(AhComb, NodiscardBuildCombs)
748 EXPECT_EQ(combs.size(), 2u);
751TEST(AhComb, NodiscardTranspose)
758 EXPECT_EQ(t.size(), 2u);
774TEST(AhComb, NodiscardPermCount)
781 EXPECT_EQ(
count, 2u);
784TEST(AhComb, NodiscardExistsPerm)
794TEST(AhComb, NextPermutationArrayBasicLexicographicOrder)
798 std::vector<std::string> seen;
799 seen.push_back(seq_to_string(a));
801 seen.push_back(seq_to_string(a));
803 const std::vector<std::string>
expected = {
804 "1,2,3",
"1,3,2",
"2,1,3",
"2,3,1",
"3,1,2",
"3,2,1"
809 EXPECT_EQ(seq_to_string(a),
"1,2,3");
812TEST(AhComb, NextPermutationArrayHandlesDuplicatesWithoutRepeats)
816 std::vector<std::string> seen;
817 seen.push_back(seq_to_string(a));
819 seen.push_back(seq_to_string(a));
821 const std::vector<std::string>
expected = {
822 "1,1,2",
"1,2,1",
"2,1,1"
825 EXPECT_EQ(seq_to_string(a),
"1,1,2");
828TEST(AhComb, NextPermutationArrayMatchesStdWithCustomComparator)
831 std::vector<int> v = {3, 2, 1};
835 EXPECT_EQ(seq_to_string(a), vector_to_string(v));
838 const bool hv = std::next_permutation(v.begin(), v.end(),
839 std::greater<int>());
847TEST(AhComb, NextPermutationResetOnLastCanBeDisabled)
851 EXPECT_EQ(seq_to_string(a),
"3,2,1");
854TEST(AhComb, NextPermutationDynArray)
866 EXPECT_EQ(
count, 6u);
867 EXPECT_EQ(seq_to_string(a),
"1,2,3");
870TEST(AhComb, NextCombinationIndicesArrayEnumeratesAll)
875 std::vector<std::array<size_t, 3>> seen;
878 seen.push_back({idx(0), idx(1), idx(2)});
883 const std::vector<std::array<size_t, 3>>
expected = {
884 {0, 1, 2}, {0, 1, 3}, {0, 1, 4}, {0, 2, 3}, {0, 2, 4},
885 {0, 3, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}
890 EXPECT_EQ(idx(0), 0u);
891 EXPECT_EQ(idx(1), 1u);
892 EXPECT_EQ(idx(2), 2u);
895TEST(AhComb, NextCombinationIndicesResetOnLastCanBeDisabled)
899 EXPECT_EQ(idx(0), 2u);
900 EXPECT_EQ(idx(1), 3u);
901 EXPECT_EQ(idx(2), 4u);
904TEST(AhComb, NextCombinationIndicesValidation)
916TEST(AhComb, NextCombinationIndicesDynArray)
927 EXPECT_EQ(
count, 6u);
928 EXPECT_EQ(idx(0), 0u);
929 EXPECT_EQ(idx(1), 1u);
932TEST(AhComb, CombinationCountBasicCases)
943TEST(AhComb, CombinationCountOverflowThrows)
945 if (
sizeof(
size_t) >= 8)
951TEST(AhComb, CombinationMaskEnumerationAndReset)
956 std::vector<uint64_t> seen;
959 seen.push_back(mask);
964 const std::vector<uint64_t>
expected = {
965 0x07, 0x0b, 0x0d, 0x0e, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c
968 EXPECT_EQ(mask, uint64_t(0x07));
971TEST(AhComb, CombinationMaskValidation)
976 mask = (uint64_t(1) << 7);
980TEST(AhComb, ForEachCombinationArrayAndEarlyStop)
984 std::vector<std::string> seen;
987 seen.push_back(seq_to_string(c));
992 const std::vector<std::string>
expected = {
993 "10,20",
"10,30",
"10,40",
"20,30",
"20,40",
"30,40"
1004 EXPECT_EQ(calls, 3u);
1007TEST(AhComb, ForEachCombinationEdgeCases)
1015 EXPECT_EQ(c.
size(), 0u);
1019 EXPECT_EQ(calls, 1u);
1022 [](
const Array<int> &) {
return true; }), std::domain_error);
1025TEST(AhComb, BuildCombinationsArrayAndDynArray)
1029 ASSERT_EQ(combs.size(), 4u);
1030 EXPECT_EQ(seq_to_string(combs(0)),
"1,2,3");
1031 EXPECT_EQ(seq_to_string(combs(1)),
"1,2,4");
1032 EXPECT_EQ(seq_to_string(combs(2)),
"1,3,4");
1033 EXPECT_EQ(seq_to_string(combs(3)),
"2,3,4");
1043 ASSERT_EQ(combs_dyn.size(), 6u);
1044 EXPECT_EQ(seq_to_string(combs_dyn(0)),
"1,2");
1045 EXPECT_EQ(seq_to_string(combs_dyn(5)),
"3,4");
1075 EXPECT_EQ(filtered.size(), 2u);
1085 EXPECT_EQ(mapped.size(), 2u);
1091 const std::vector<uint32_t> expected_gray = {
1092 0, 1, 3, 2, 6, 7, 5, 4
1095 for (uint32_t i = 0; i < expected_gray.size(); ++i)
1098 EXPECT_EQ(g, expected_gray[i]) <<
"bin_to_gray mismatch at i=" << i;
1099 EXPECT_EQ(
gray_to_bin(g), i) <<
"gray_to_bin mismatch at g=" << g;
1108 ASSERT_EQ(gray.size(), 16u);
1111 for (
size_t i = 1; i < gray.size(); ++i)
1113 uint32_t
diff = gray[i] ^ gray[i-1];
1116 <<
"Not a Gray code sequence at i=" << i
1117 <<
" (values: " << gray[i-1] <<
", " << gray[i] <<
")";
1121 uint32_t diff_last = gray[gray.size()-1] ^ gray[0];
1122 EXPECT_TRUE(diff_last > 0 && (diff_last & (diff_last - 1)) == 0);
1127 uint64_t val = 0x123456789ABCDEF0ULL;
1132TEST(AhComb, BuildGrayCodeThrowsOnTooLarge)
Combinatorics utilities: permutations, combinations, and matrix transposition.
Functional programming utilities for Aleph-w containers.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & insert(const T &item)
T & get_last()
Return the last item of the list.
size_t size() const noexcept
Return the number of items of the list, in constant time.
constexpr bool is_empty() const noexcept
Minimal std::expected-style result type for C++20.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
constexpr bool is_empty() const noexcept
Checks if the table is empty.
Main namespace for Aleph-w library functions.
bool none_perm(const DynList< DynList< T > > &l, Pred &pred)
Check if no permutation satisfies a predicate.
DynList< DynList< T > > build_combs(const DynList< DynList< T > > &l)
Build the set of unique combinations from a list of lists.
bool for_each_combination(const Array< T > &values, const size_t k, Op &&op)
*/
Array< Array< T > > build_combinations(const Array< T > &values, const size_t k)
Materialize all k-combinations of Array<T>.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool next_combination_indices(Array< size_t > &idx, const size_t n, const bool reset_on_last=true)
Advance an index-combination [i0 < i1 < ... < i(k-1)] to the next one.
size_t perm_count(const DynList< DynList< T > > &l)
Count the total number of permutations from a list of lists.
bool traverse_perm(const DynList< DynList< T > > &l, Op &op)
*/
constexpr T gray_to_bin(T g) noexcept
Convert a Gray code number to its binary representation.
bool next_combination_mask(uint64_t &mask, const size_t n, const bool reset_on_last=true)
Advance a fixed-popcount bitmask to the next combination (Gosper hack).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
DynList< DynList< T > > filter_perm(const DynList< DynList< T > > &l, Pred &pred)
Filter permutations that satisfy a predicate.
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Array< uint32_t > build_gray_code(const size_t n)
Generate the sequence of n-bit Gray codes.
bool next_permutation(Array< T > &a, Compare cmp=Compare(), const bool reset_on_last=true)
Compute the next lexicographic permutation of an Array.
void for_each_perm(const DynList< DynList< T > > &l, Op &op)
Apply a procedure to every permutation produced by traverse_perm.
DynList< DynList< T > > transpose(const DynList< DynList< T > > &l)
*/
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.
uint64_t first_combination_mask(const size_t k)
Build the first k-of-64 combination mask (k low bits set).
size_t combination_count(size_t n, size_t k)
Compute n choose k with overflow checks.
DynList< DynList< T > > build_perms(const DynList< DynList< T > > &l)
Materialize all permutations from a list of lists.
void in_place_transpose(C< C< T > > &l)
In-place transpose of a rectangular matrix stored as a nested container.
bool exists_perm(const DynList< DynList< T > > &l, Pred &pred)
Check if any permutation satisfies a predicate.
constexpr T bin_to_gray(const T n) noexcept
Convert a binary number to its Gray code representation.
bool all_perm(const DynList< DynList< T > > &l, Pred &pred)
Check if all permutations satisfy a predicate.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)