106 mat.
append(std::move(row));
121 mat.
append(std::move(row));
125 for (
size_t j = 0; j <
ncol; ++j)
128 for (
size_t i = 0; i <
nrow; ++i)
129 row.
append(mat(i)(j)->get_data());
131 ret.append(std::move(row));
137 template <
class ArrayLike>
139 size_t left,
size_t right)
noexcept
143 std::swap(a(left), a(right));
149 template <
class IndexArray>
153 const size_t k = idx.size();
155 <<
"next_combination_indices: k=" <<
k <<
" cannot exceed n=" << n;
157 for (
size_t i = 0; i <
k; ++i)
160 <<
"next_combination_indices: index " << idx(i)
161 <<
" at position " << i <<
" is outside [0, " << n <<
")";
165 <<
"next_combination_indices: indices must be strictly increasing";
169 template <
class ArrayLike,
class Compare>
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)))
197 std::swap(a(
pivot), a(succ));
202 template <
class IndexArray>
209 const size_t k = idx.size();
213 for (
size_t pos =
k; pos > 0; --pos)
215 const size_t i = pos - 1;
220 for (
size_t j = i + 1; j <
k; ++j)
221 idx(j) = idx(j - 1) + 1;
227 for (
size_t i = 0; i <
k; ++i)
248 template <
typename T>
257 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
258 mat.
append(it.get_curr());
263 for (
size_t i = 1; i <
nrow; ++i)
268 for (
size_t j = 0; j <
ncol; ++j)
271 for (
size_t i = 0; i <
nrow; ++i)
274 ret.append(std::move(row));
299 template <
template <
typename>
class C,
typename T>
311 for (
size_t i = 0; i <
nrow; ++i)
314 for (
size_t j = 0; j <
ncol; ++j)
317 for (
size_t i = 0; i <
nrow; ++i)
318 row.
append(std::move(
l(i)(j)));
319 mat.append(std::move(row));
337 template <
typename T>
351 mat.
append(std::move(row));
369 mat.
append(std::move(row));
374 for (
size_t j = 0; j <
ncol; ++j)
377 for (
size_t i = 0; i <
nrow; ++i)
400 template <
typename T,
class Op>
406 return op(sample.template
maps<T>([](
const T & i) { return i; }));
408 auto itor =
its.remove_first();
409 for (
auto it =
itor; it.has_curr(); it.next_ne())
411 auto item = it.get_curr();
463 template <
typename T,
class Op>
485 template <
typename T,
class Op>
503 template <
typename T,
class Op>
518 template <
typename T,
class Op>
535 template <
typename T>
556 template <
typename T>
587 template <
typename T,
typename Tc,
class Op = Dft_Fold_Op<Tc, T>>
604 template <
typename T,
typename Tc,
class Op = Dft_Fold_Op<Tc, T>>
622 template <
typename T>
627 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
629 const size_t sz = it.get_curr().size();
649 template <
typename T,
class Pred>
670 template <
typename T,
class Pred>
689 template <
typename T,
class Pred>
700 template <
typename T,
class Pred>
719 template <
typename T,
class Pred>
730 template <
typename T,
class Pred>
747 template <
typename T,
class Pred>
764 template <
typename T,
class Pred>
782 template <
typename R,
typename T,
class Op>
798 template <
typename R,
typename T,
class Op>
832 template <
typename T,
class Compare = Aleph::less<T>>
835 Compare
cmp = Compare(),
842 template <
typename T,
class Compare = Aleph::less<T>>
845 Compare
cmp = Compare(),
866 k = std::min(
k, n -
k);
871 for (
size_t i = 1; i <=
k; ++i)
873 size_t num = n -
k + i;
876 size_t g = std::gcd(num,
den);
880 g = std::gcd(result,
den);
884 g = std::gcd(num,
den);
889 <<
"combination_count: internal reduction failure for C("
890 << n <<
", " <<
k <<
")";
893 <<
"combination_count: overflow for C(" << n <<
", " <<
k <<
")";
921 return comb_detail::next_combination_indices_impl(idx, n,
reset_on_last);
930 return comb_detail::next_combination_indices_impl(idx, n,
reset_on_last);
937 <<
"first_combination_mask: k=" <<
k <<
" cannot exceed 64";
942 return std::numeric_limits<uint64_t>::max();
968 <<
"next_combination_mask: n=" << n <<
" cannot exceed 64";
973 <<
"next_combination_mask: for n=0, mask must be 0";
978 n == 64 ? std::numeric_limits<uint64_t>::max() : ((
uint64_t(1) << n) - 1);
981 <<
"next_combination_mask: mask has bits outside the low n bits";
983 const size_t k = std::popcount(mask);
1014 template <
class ValuesArray,
class Op>
1020 const size_t n = values.size();
1022 <<
"for_each_combination: k=" <<
k <<
" cannot exceed n=" << n;
1028 for (
size_t i = 0; i <
k; ++i)
1034 for (
size_t i = 0; i <
k; ++i)
1035 comb(i) = values(idx(i));
1062 template <
typename T,
class Op>
1070 template <
typename T,
class Op>
1086 template <
typename T>
1100 template <
typename T>
1125 template <std::
unsigned_
integral T>
1128 return n ^ (n >> 1);
1143 template <std::
unsigned_
integral T>
1146 for (
size_t shift = 1; shift <
sizeof(
T) * 8; shift <<= 1)
1167 <<
"build_gray_code: n=" << n <<
" exceeds 31-bit limit for Array";
1169 const size_t count = size_t(1) << n;
1172 for (
size_t i = 0; i <
count; ++i)
#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.
Dynamic singly linked list with functional programming support.
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)
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)
Enumerate all k-combinations of an Array<T> in lexicographic index order.
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)
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.
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)
Transpose a matrix represented as a list of lists.
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 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.