38# include <gtest/gtest.h>
55 std::ostringstream
oss;
70 std::ostringstream
oss;
71 for (
size_t i = 0; i < s.size(); ++i)
82 std::ostringstream
oss;
83 for (
size_t i = 0; i < v.size(); ++i)
158 std::set<std::string>
perms;
199 std::set<std::string> s;
217 std::set<std::string> s;
508 sums.for_each([&s](
int v) { s.insert(v); });
529 std::set<std::string> s;
530 strs.for_each([&s](
const std::string & v) { s.insert(v); });
541 std::set<std::string>
perms;
573 std::set<std::string>
perms;
588 std::set<std::string>
perms;
608 for (
auto it = t.get_it(); it.has_curr(); it.next_ne(), ++idx)
611 EXPECT_EQ(it.get_curr().get_first(),
static_cast<int>(idx));
793 std::vector<std::string>
seen;
798 const std::vector<std::string>
expected = {
799 "1,2,3",
"1,3,2",
"2,1,3",
"2,3,1",
"3,1,2",
"3,2,1"
811 std::vector<std::string>
seen;
816 const std::vector<std::string>
expected = {
817 "1,1,2",
"1,2,1",
"2,1,1"
826 std::vector<int> v = {3, 2, 1};
833 const bool hv = std::next_permutation(v.begin(), v.end(),
834 std::greater<int>());
870 std::vector<std::array<size_t, 3>>
seen;
873 seen.push_back({idx(0), idx(1), idx(2)});
878 const std::vector<std::array<size_t, 3>>
expected = {
879 {0, 1, 2}, {0, 1, 3}, {0, 1, 4}, {0, 2, 3}, {0, 2, 4},
880 {0, 3, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}
940 if (
sizeof(
size_t) >= 8)
951 std::vector<uint64_t>
seen;
954 seen.push_back(mask);
959 const std::vector<uint64_t>
expected = {
960 0x07, 0x0b, 0x0d, 0x0e, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c
979 std::vector<std::string>
seen;
987 const std::vector<std::string>
expected = {
988 "10,20",
"10,30",
"10,40",
"20,30",
"20,40",
"30,40"
1017 [](
const Array<int> &) {
return true; }), std::domain_error);
1087 0, 1, 3, 2, 6, 7, 5, 4
1106 for (
size_t i = 1; i <
gray.size(); ++i)
1111 <<
"Not a Gray code sequence at i=" << i
1112 <<
" (values: " <<
gray[i-1] <<
", " <<
gray[i] <<
")";
1122 uint64_t val = 0x123456789ABCDEF0ULL;
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.
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
T & get_last() const
Return the last item of the list.
T & get_first() const
Return the first item of the list.
constexpr bool is_empty() const noexcept
size_t size() const noexcept
Count the number of elements of the list.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
auto get_it() const
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)
Enumerate all k-combinations of an Array<T> in lexicographic index order.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
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
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)
Traverse all the possible permutations that can be done of a list of lists and on each permutation pe...
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).
DynList< DynList< T > > filter_perm(const DynList< DynList< T > > &l, Pred &pred)
Filter permutations that satisfy a predicate.
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.
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)
Transpose a matrix represented as a list of lists.
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)