127 for (
size_t j = 0; j <
ncol; ++j)
130 for (
size_t i = 0; i <
nrow; ++i)
133 ret.append(std::move(
row));
139template <
class ArrayLike>
144 std::swap(a(left), a(right));
150template <
class IndexArray>
153 const size_t k = idx.size();
156 for (
size_t i = 0; i <
k; ++i)
159 <<
"next_combination_indices: index " << idx(i) <<
" at position " << i
160 <<
" is outside [0, " << n <<
")";
164 <<
"next_combination_indices: indices must be strictly increasing";
168template <
class ArrayLike,
class Compare>
171 const size_t n = a.size();
176 for (
size_t i = n - 1; i > 0; --i)
177 if (
cmp(a(i - 1), a(i)))
194 std::swap(a(
pivot), a(succ));
199template <
class IndexArray>
205 const size_t k = idx.size();
209 for (
size_t pos =
k; pos > 0; --pos)
211 const size_t i = pos - 1;
216 for (
size_t j = i + 1; j <
k; ++j)
217 idx(j) = idx(j - 1) + 1;
223 for (
size_t i = 0; i <
k; ++i)
254 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
255 mat.
append(it.get_curr());
260 for (
size_t i = 1; i <
nrow; ++i)
265 for (
size_t j = 0; j <
ncol; ++j)
268 for (
size_t i = 0; i <
nrow; ++i)
271 ret.append(std::move(
row));
290template <
template <
typename>
class C,
typename T>
302 for (
size_t i = 0; i <
nrow; ++i)
305 for (
size_t j = 0; j <
ncol; ++j)
308 for (
size_t i = 0; i <
nrow; ++i)
309 row.append(std::move(
l(i)(j)));
310 mat.append(std::move(
row));
355 row.append(
lrow.remove_head());
366 for (
size_t j = 0; j <
ncol; ++j)
369 for (
size_t i = 0; i <
nrow; ++i)
392template <
typename T,
class Op>
397 return op(sample.template
maps<T>([](
const T &i)
402 auto itor =
its.remove_first();
403 for (
auto it =
itor; it.has_curr(); it.next_ne())
405 auto item = it.get_curr();
439template <
typename T,
class Op>
463template <
typename T,
class Op>
480template <
typename T,
class Op>
494template <
typename T,
class Op>
567template <
typename T,
typename Tc,
class Op = Dft_Fold_Op<Tc, T>>
584template <
typename T,
typename Tc,
class Op = Dft_Fold_Op<Tc, T>>
607 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
609 const size_t sz = it.get_curr().size();
629template <
typename T,
class Pred>
650template <
typename T,
class Pred>
669template <
typename T,
class Pred>
680template <
typename T,
class Pred>
699template <
typename T,
class Pred>
710template <
typename T,
class Pred>
727template <
typename T,
class Pred>
744template <
typename T,
class Pred>
762template <
typename R,
typename T,
class Op>
778template <
typename R,
typename T,
class Op>
812template <
typename T,
class Compare = Aleph::less<T>>
820template <
typename T,
class Compare = Aleph::less<T>>
842 k = std::min(
k, n -
k);
847 for (
size_t i = 1; i <=
k; ++i)
849 size_t num = n -
k + i;
852 size_t g = std::gcd(num,
den);
856 g = std::gcd(result,
den);
860 g = std::gcd(num,
den);
865 <<
"combination_count: internal reduction failure for C(" << n <<
", " <<
k <<
")";
868 <<
"combination_count: overflow for C(" << n <<
", " <<
k <<
")";
894 return comb_detail::next_combination_indices_impl(idx, n,
reset_on_last);
901 return comb_detail::next_combination_indices_impl(idx, n,
reset_on_last);
912 return std::numeric_limits<uint64_t>::max();
913 return (
static_cast<uint64_t>(1) <<
k) - 1;
944 n == 64 ? std::numeric_limits<uint64_t>::max() : ((
uint64_t(1) << n) - 1);
947 <<
"next_combination_mask: mask has bits outside the low n bits";
949 const size_t k = std::popcount(mask);
980template <
class ValuesArray,
class Op>
982 const size_t k, Op &&op)
984 const size_t n = values.size();
991 for (
size_t i = 0; i <
k; ++i)
997 for (
size_t i = 0; i <
k; ++i)
998 comb(i) = values(idx(i));
1025template <
typename T,
class Op>
1032template <
typename T,
class Op>
1047template <
typename T>
1060template <
typename T>
1083template <std::
unsigned_
integral T>
1086 return n ^ (n >> 1);
1100template <std::
unsigned_
integral T>
1103 for (
size_t shift = 1; shift <
sizeof(
T) * 8; shift <<= 1)
1122 ah_domain_error_if(n > 31) <<
"build_gray_code: n=" << n <<
" exceeds 31-bit limit for Array";
1124 const size_t count = size_t(1) << n;
1127 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.
Standard functor implementations and comparison objects.
High-level sorting functions for Aleph containers.
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
T & append()
Allocate a new entry to the end of array.
Iterator on the items of list.
Doubly-linked list (defined in tpl_dynList.H).
T & insert(const T &item)
T & append(const T &item)
T & get_first() const
Return the first item of the list.
DynList & swap(DynList &l) noexcept
Dynamic set backed by balanced binary search trees with automatic memory management.
bool has_curr() const noexcept
Single linked list of nodes.
constexpr bool is_empty() const noexcept
size_t size() const noexcept
Count the number of elements of the list.
Link of a single linked list non-circular and without header node.
auto get_it() const
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)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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
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.