81namespace comb_detail {
113 for (
auto row_it =
l.
get_it(1); row_it.has_curr(); row_it.next_ne(), ++nrow)
115 const HTList &lrow = row_it.get_curr();
128 for (
size_t j = 0; j < ncol; ++j)
131 for (
size_t i = 0; i < nrow; ++i)
140template <
class ArrayLike>
141inline void reverse_range(ArrayLike &a,
size_t left,
size_t right)
noexcept
145 std::swap(a(left), a(right));
151template <
class IndexArray>
152inline void validate_combination_indices(
const IndexArray &idx,
const size_t n)
154 const size_t k = idx.size();
157 for (
size_t i = 0; i <
k; ++i)
160 <<
"next_combination_indices: index " << idx(i) <<
" at position " << i
161 <<
" is outside [0, " << n <<
")";
166 <<
"next_combination_indices: indices must be strictly increasing";
171template <
class ArrayLike,
class Compare>
172[[nodiscard]]
inline bool next_permutation_impl(ArrayLike &a, Compare
cmp,
const bool reset_on_last)
174 const size_t n = a.size();
179 for (
size_t i = n - 1; i > 0; --i)
180 if (
cmp(a(i - 1), a(i)))
194 while (not
cmp(a(pivot), a(succ)))
197 std::swap(a(pivot), a(succ));
202template <
class IndexArray>
203[[nodiscard]]
inline bool next_combination_indices_impl(IndexArray &idx,
const size_t n,
204 const bool reset_on_last)
206 validate_combination_indices(idx, n);
208 const size_t k = idx.size();
212 for (
size_t pos =
k; pos > 0; --pos)
214 const size_t i = pos - 1;
215 const size_t max_here = n - (
k - i);
216 if (idx(i) < max_here)
219 for (
size_t j = i + 1; j <
k; ++j)
220 idx(j) = idx(j - 1) + 1;
226 for (
size_t i = 0; i <
k; ++i)
257 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
258 mat.
append(it.get_curr());
260 const size_t nrow = mat.
size();
261 const size_t ncol = mat[0].
size();
263 for (
size_t i = 1; i < nrow; ++i)
264 assert(mat[i].
size() == ncol);
268 for (
size_t j = 0; j < ncol; ++j)
271 for (
size_t i = 0; i < nrow; ++i)
293template <
template <
typename>
class C,
typename T>
299 const size_t nrow =
l.
size();
305 for (
size_t i = 0; i < nrow; ++i)
306 assert(
l(i).
size() == ncol);
308 for (
size_t j = 0; j < ncol; ++j)
311 for (
size_t i = 0; i < nrow; ++i)
312 row.append(std::move(
l(i)(j)));
313 mat.append(std::move(
row));
369 for (
size_t j = 0; j < ncol; ++j)
372 for (
size_t i = 0; i < nrow; ++i)
395template <
typename T,
class Op>
396static inline bool traverse_perm_impl(
DynList<T> &sample,
400 return op(sample.template maps<T>([](
const T &i)
405 auto itor = its.remove_first();
406 for (
auto it = itor; it.has_curr(); it.next_ne())
408 auto item = it.get_curr();
410 if (not traverse_perm_impl(sample, its, op))
442template <
typename T,
class Op>
451 const DynList<IT> lcpy =
l.template maps<IT>([](
const auto &
l)
459 return traverse_perm_impl(ll, its, op);
464template <
typename T,
class Op>
481template <
typename T,
class Op>
493template <
typename T,
class Op>
542 combs.
insert(std::move(comb));
545 return combs.template maps<DynList<T>>([](
const DynList<T> &comb)
566template <
typename T,
typename Tc,
class Op = Dft_Fold_Op<Tc, T>>
581template <
typename T,
typename Tc,
class Op = Dft_Fold_Op<Tc, T>>
604 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
606 const size_t sz = it.get_curr().size();
626template <
typename T,
class Pred>
645template <
typename T,
class Pred>
664template <
typename T,
class Pred>
673template <
typename T,
class Pred>
692template <
typename T,
class Pred>
701template <
typename T,
class Pred>
718template <
typename T,
class Pred>
733template <
typename T,
class Pred>
751template <
typename R,
typename T,
class Op>
765template <
typename R,
typename T,
class Op>
769 return map_perm<R>(
l, op);
799template <
typename T,
class Compare = Aleph::less<T>>
801 const bool reset_on_last =
true)
803 return comb_detail::next_permutation_impl(a,
cmp, reset_on_last);
807template <
typename T,
class Compare = Aleph::less<T>>
809 const bool reset_on_last =
true)
811 return comb_detail::next_permutation_impl(a,
cmp, reset_on_last);
829 k = std::min(
k, n -
k);
834 for (
size_t i = 1; i <=
k; ++i)
836 size_t num = n -
k + i;
839 size_t g = std::gcd(num, den);
843 g = std::gcd(result, den);
847 g = std::gcd(num, den);
852 <<
"combination_count: internal reduction failure for C(" << n <<
", " <<
k <<
")";
855 <<
"combination_count: overflow for C(" << n <<
", " <<
k <<
")";
879 const bool reset_on_last =
true)
881 return comb_detail::next_combination_indices_impl(idx, n, reset_on_last);
886 const bool reset_on_last =
true)
888 return comb_detail::next_combination_indices_impl(idx, n, reset_on_last);
899 return std::numeric_limits<uint64_t>::max();
900 return (
static_cast<uint64_t
>(1) <<
k) - 1;
920 const bool reset_on_last =
true)
930 const uint64_t domain_mask =
931 n == 64 ? std::numeric_limits<uint64_t>::max() : ((uint64_t(1) << n) - 1);
934 <<
"next_combination_mask: mask has bits outside the low n bits";
936 const size_t k = std::popcount(mask);
944 const uint64_t c = mask & (~mask + 1);
945 const uint64_t
r = mask + c;
954 const uint64_t
next = (((
r ^ mask) >> 2) / c) |
r;
955 if (
next & ~domain_mask)
967template <
class ValuesArray,
class Op>
968[[nodiscard]]
static inline bool for_each_combination_impl(
const ValuesArray &values,
969 const size_t k, Op &&op)
971 const size_t n = values.size();
978 for (
size_t i = 0; i <
k; ++i)
984 for (
size_t i = 0; i <
k; ++i)
985 comb(i) = values(idx(i));
1012template <
typename T,
class Op>
1015 return for_each_combination_impl(values,
k, std::forward<Op>(op));
1019template <
typename T,
class Op>
1022 return for_each_combination_impl(values,
k, std::forward<Op>(op));
1034template <
typename T>
1047template <
typename T>
1070template <std::
unsigned_
integral T>
1073 return n ^ (n >> 1);
1087template <std::
unsigned_
integral T>
1090 for (
size_t shift = 1; shift <
sizeof(
T) * 8; shift <<= 1)
1109 ah_domain_error_if(n > 31) <<
"build_gray_code: n=" << n <<
" exceeds 31-bit limit for Array";
1111 const size_t count = size_t(1) << n;
1114 for (
size_t i = 0; i <
count; ++i)
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
STL-compatible iterator adapters for Aleph containers.
Standard functor implementations and comparison objects.
High-level sorting functions for Aleph 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.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Read-only view of an Aleph iterator: the iterator of a constant container.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
T & insert(const T &item)
T remove_first()
See remove().
size_t size() const noexcept
Return the number of items of the list, in constant time.
DynList & swap(DynList &l) noexcept
Slinknc * remove_head()
Remove the first node of the list; the caller takes the node, and its item.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool has_curr() const noexcept
Single linked list of nodes.
constexpr bool is_empty() const noexcept
Link of a single linked list non-circular and without header node.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Singly linked list implementations with head-tail access.
Freq_Node * pred
Predecessor node in level-order traversal.
void reverse_range(T *a, size_t lo, size_t hi) noexcept(std::is_nothrow_swappable_v< T >)
Reverse elements in [lo, hi).
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>.
DynList< R > map_perm(const DynList< DynList< T > > &l, Op &op)
Transform each permutation via a mapping operation.
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.
T fold_perm(const T &init, const DynList< DynList< Tc > > &l, Op &op)
Left-fold over all permutations.
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).
DynList< DynList< T > > filter_perm(const DynList< DynList< T > > &l, Pred &pred)
Filter permutations that satisfy a predicate.
std::decay_t< typename HeadC::Item_Type > T
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)
*/
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.
void next()
Advance all underlying iterators (bounds-checked).
static std::atomic< bool > init
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
AVL binary search tree with nodes without a virtual destructor.
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.