Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-comb.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31#ifndef COMB_H
32#define COMB_H
33
62#include <algorithm>
63#include <bit>
64#include <cstdint>
65#include <limits>
66#include <numeric>
67
68#include <ah-errors.H>
69
70#include <htlist.H>
71#include <tpl_dynDlist.H>
72#include <tpl_dynArray.H>
73#include <tpl_array.H>
74#include <tpl_dynSetTree.H>
75#include <ahFunction.H>
76#include <ahSort.H>
77#include <ah-iterator.H>
78
79namespace Aleph {
81namespace comb_detail {
95template <typename T>
96[[nodiscard]] inline DynList<DynList<T>> transpose_impl(DynList<DynList<T>> &l)
97{
98 if (l.is_empty())
99 return {};
100
102
103 size_t ncol = 0;
104 {
105 const HTList &lrow = l.get_first();
107 for (HTList::Iterator it(lrow); it.has_curr(); it.next_ne(), ++ncol)
108 row.append(static_cast<Snodenc<T> *>(it.get_curr()));
109 mat.append(std::move(row));
110 }
111
112 size_t nrow = 1;
113 for (auto row_it = l.get_it(1); row_it.has_curr(); row_it.next_ne(), ++nrow)
114 {
115 const HTList &lrow = row_it.get_curr();
117 row.reserve(ncol);
118 size_t col = 0;
119 for (HTList::Iterator it(lrow); it.has_curr(); it.next_ne(), ++col)
120 row.append(static_cast<Snodenc<T> *>(it.get_curr()));
121
122 assert(col == ncol);
123
124 mat.append(std::move(row));
125 }
126
128 for (size_t j = 0; j < ncol; ++j)
129 {
131 for (size_t i = 0; i < nrow; ++i)
132 row.append(mat(i)(j)->get_data());
133
134 ret.append(std::move(row));
135 }
136
137 return ret;
138}
139
140template <class ArrayLike>
141inline void reverse_range(ArrayLike &a, size_t left, size_t right) noexcept
142{
143 while (left < right)
144 {
145 std::swap(a(left), a(right));
146 ++left;
147 --right;
148 }
149}
150
151template <class IndexArray>
152inline void validate_combination_indices(const IndexArray &idx, const size_t n)
153{
154 const size_t k = idx.size();
155 ah_domain_error_if(k > n) << "next_combination_indices: k=" << k << " cannot exceed n=" << n;
156
157 for (size_t i = 0; i < k; ++i)
158 {
159 ah_out_of_range_error_if(idx(i) >= n)
160 << "next_combination_indices: index " << idx(i) << " at position " << i
161 << " is outside [0, " << n << ")";
162
163 if (i > 0)
164 {
165 ah_domain_error_if(idx(i - 1) >= idx(i))
166 << "next_combination_indices: indices must be strictly increasing";
167 }
168 }
169}
170
171template <class ArrayLike, class Compare>
172[[nodiscard]] inline bool next_permutation_impl(ArrayLike &a, Compare cmp, const bool reset_on_last)
173{
174 const size_t n = a.size();
175 if (n < 2)
176 return false;
177
178 size_t pivot = n;
179 for (size_t i = n - 1; i > 0; --i)
180 if (cmp(a(i - 1), a(i)))
181 {
182 pivot = i - 1;
183 break;
184 }
185
186 if (pivot == n)
187 {
188 if (reset_on_last)
189 reverse_range(a, 0, n - 1);
190 return false;
191 }
192
193 size_t succ = n - 1;
194 while (not cmp(a(pivot), a(succ)))
195 --succ;
196
197 std::swap(a(pivot), a(succ));
198 reverse_range(a, pivot + 1, n - 1);
199 return true;
200}
201
202template <class IndexArray>
203[[nodiscard]] inline bool next_combination_indices_impl(IndexArray &idx, const size_t n,
204 const bool reset_on_last)
205{
206 validate_combination_indices(idx, n);
207
208 const size_t k = idx.size();
209 if (k == 0)
210 return false;
211
212 for (size_t pos = k; pos > 0; --pos)
213 {
214 const size_t i = pos - 1;
215 const size_t max_here = n - (k - i);
216 if (idx(i) < max_here)
217 {
218 ++idx(i);
219 for (size_t j = i + 1; j < k; ++j)
220 idx(j) = idx(j - 1) + 1;
221 return true;
222 }
223 }
224
225 if (reset_on_last)
226 for (size_t i = 0; i < k; ++i)
227 idx(i) = i;
228
229 return false;
230}
231} // namespace comb_detail
232
234
249template <typename T>
250[[nodiscard]] inline DynList<DynList<T>> transpose(const DynList<DynList<T>> &l)
251{
252 if (l.is_empty())
253 return {};
254
255 Array<Array<T>> mat;
256
257 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
258 mat.append(it.get_curr());
259
260 const size_t nrow = mat.size();
261 const size_t ncol = mat[0].size();
262
263 for (size_t i = 1; i < nrow; ++i)
264 assert(mat[i].size() == ncol);
265
267
268 for (size_t j = 0; j < ncol; ++j)
269 {
271 for (size_t i = 0; i < nrow; ++i)
272 row.append(mat(i)(j));
273
274 ret.append(std::move(row));
275 }
276
277 return ret;
278}
279
293template <template <typename> class C, typename T>
294inline void in_place_transpose(C<C<T>> &l)
295
296{
297 C<C<T>> mat;
298
299 const size_t nrow = l.size();
300 if (nrow == 0)
301 return;
302
303 const size_t ncol = l.get_first().size();
304
305 for (size_t i = 0; i < nrow; ++i)
306 assert(l(i).size() == ncol);
307
308 for (size_t j = 0; j < ncol; ++j)
309 {
310 C<T> row;
311 for (size_t i = 0; i < nrow; ++i)
312 row.append(std::move(l(i)(j)));
313 mat.append(std::move(row));
314 }
315 l.swap(mat);
316}
317
331template <typename T>
333{
334 if (l.is_empty())
335 return;
336
338
339 size_t ncol = 0;
340
341 {
342 DynList<T> lrow = l.remove_first();
344 for (; not lrow.is_empty(); ++ncol)
345 row.append(lrow.remove_head());
346 mat.append(std::move(row));
347 }
348
349 size_t nrow = 1;
350 for (; not l.is_empty(); ++nrow)
351 {
352 DynList<T> lrow = l.remove_first();
354 row.reserve(ncol);
355 size_t col = 0;
356 while (not lrow.is_empty())
357 {
358 row.append(lrow.remove_head());
359 ++col;
360 }
361
362 assert(col == ncol);
363
364 mat.append(std::move(row));
365 }
366
367 assert(l.is_empty());
368
369 for (size_t j = 0; j < ncol; ++j)
370 {
372 for (size_t i = 0; i < nrow; ++i)
373 {
374 Slinknc *node_ptr = mat(i)(j);
375 row.__append(static_cast<Snodenc<T> *>(node_ptr)); // counts it
376 }
377 l.append(std::move(row));
378 }
379}
380
382
395template <typename T, class Op>
396static inline bool traverse_perm_impl(DynList<T> &sample,
398{
399 if (its.is_empty())
400 return op(sample.template maps<T>([](const T &i)
401 {
402 return i;
403 }));
404
405 auto itor = its.remove_first();
406 for (auto it = itor; it.has_curr(); it.next_ne())
407 {
408 auto item = it.get_curr();
409 sample.insert(item);
410 if (not traverse_perm_impl(sample, its, op))
411 {
412 sample.remove_first();
413 its.insert(itor);
414 return false;
415 }
416 sample.remove_first();
417 }
418 its.insert(itor);
419
420 return true;
421}
422
424
442template <typename T, class Op>
443inline bool traverse_perm(const DynList<DynList<T>> &l, Op &op)
444
445{
447 DynList<IT> its;
448
449 { // This block allows getting a constant copy of l and then reverse
450 // it. At the end of block lcpy memory is freed
451 const DynList<IT> lcpy = l.template maps<IT>([](const auto &l)
452 {
453 return l.get_it();
454 });
455 its = lcpy.rev();
456 }
457
458 DynList<T> ll;
459 return traverse_perm_impl(ll, its, op);
460}
461
464template <typename T, class Op>
465inline bool traverse_perm(const DynList<DynList<T>> &l, Op &&op)
466{
467 return traverse_perm(l, op);
468}
469
481template <typename T, class Op>
482inline void for_each_perm(const DynList<DynList<T>> &l, Op &op)
483{
484 traverse_perm(l, [&op](const auto &row)
485 {
486 op(row);
487 return true;
488 });
489}
490
493template <typename T, class Op>
494inline void for_each_perm(const DynList<DynList<T>> &l, Op &&op)
495{
496 return for_each_perm(l, op);
497}
498
509template <typename T>
510[[nodiscard]]
512{
514 for_each_perm(l, [&ret](const DynList<T> &perm)
515 {
516 ret.append(perm);
517 });
518 return ret;
519}
520
533template <typename T>
534[[nodiscard]]
536{
538
539 for_each_perm(l, [&combs](const DynList<T> &perm)
540 {
541 DynList<T> comb = sort(perm);
542 combs.insert(std::move(comb));
543 });
544
545 return combs.template maps<DynList<T>>([](const DynList<T> &comb)
546 {
547 return comb;
548 });
549}
550
566template <typename T, typename Tc, class Op = Dft_Fold_Op<Tc, T>>
567[[nodiscard]]
568T fold_perm(const T &init, const DynList<DynList<Tc>> &l, Op &op)
569{
570 T acu = init;
571 traverse_perm(l, [&op, &acu](const auto &l)
572 {
573 acu = op(acu, l);
574 return true;
575 });
576 return acu;
577}
578
581template <typename T, typename Tc, class Op = Dft_Fold_Op<Tc, T>>
582[[nodiscard]]
583T fold_perm(const T &init, const DynList<DynList<Tc>> &l, Op &&op)
584{
585 return fold_perm(init, l, op);
586}
587
599template <typename T>
600[[nodiscard]]
602{
603 size_t count = 1;
604 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
605 {
606 const size_t sz = it.get_curr().size();
607 if (sz == 0)
608 return 0; // Any empty list results in no permutations
609 count *= sz;
610 }
611 return count;
612}
613
626template <typename T, class Pred>
627[[nodiscard]]
628bool exists_perm(const DynList<DynList<T>> &l, Pred &pred)
629{
630 bool found = false;
631 traverse_perm(l, [&pred, &found](const DynList<T> &perm)
632 {
633 if (pred(perm))
634 {
635 found = true;
636 return false; // stop
637 }
638 return true;
639 });
640 return found;
641}
642
645template <typename T, class Pred>
646[[nodiscard]]
647bool exists_perm(const DynList<DynList<T>> &l, Pred &&pred)
648{
649 return exists_perm(l, pred);
650}
651
664template <typename T, class Pred>
665[[nodiscard]]
666bool all_perm(const DynList<DynList<T>> &l, Pred &pred)
667{
668 return traverse_perm(l, pred);
669}
670
673template <typename T, class Pred>
674[[nodiscard]]
675bool all_perm(const DynList<DynList<T>> &l, Pred &&pred)
676{
677 return all_perm(l, pred);
678}
679
692template <typename T, class Pred>
693[[nodiscard]]
694bool none_perm(const DynList<DynList<T>> &l, Pred &pred)
695{
696 return not exists_perm(l, pred);
697}
698
701template <typename T, class Pred>
702[[nodiscard]]
703bool none_perm(const DynList<DynList<T>> &l, Pred &&pred)
704{
705 return none_perm(l, pred);
706}
707
718template <typename T, class Pred>
719[[nodiscard]]
721{
723 for_each_perm(l, [&ret, &pred](const DynList<T> &perm)
724 {
725 if (pred(perm))
726 ret.append(perm);
727 });
728 return ret;
729}
730
733template <typename T, class Pred>
734[[nodiscard]]
736{
737 return filter_perm(l, pred);
738}
739
751template <typename R, typename T, class Op>
752[[nodiscard]]
754{
755 DynList<R> ret;
756 for_each_perm(l, [&ret, &op](const DynList<T> &perm)
757 {
758 ret.append(op(perm));
759 });
760 return ret;
761}
762
765template <typename R, typename T, class Op>
766[[nodiscard]]
768{
769 return map_perm<R>(l, op);
770}
771
799template <typename T, class Compare = Aleph::less<T>>
800[[nodiscard]] inline bool next_permutation(Array<T> &a, Compare cmp = Compare(),
801 const bool reset_on_last = true)
802{
803 return comb_detail::next_permutation_impl(a, cmp, reset_on_last);
804}
805
807template <typename T, class Compare = Aleph::less<T>>
808[[nodiscard]] inline bool next_permutation(DynArray<T> &a, Compare cmp = Compare(),
809 const bool reset_on_last = true)
810{
811 return comb_detail::next_permutation_impl(a, cmp, reset_on_last);
812}
813
824[[nodiscard]] inline size_t combination_count(size_t n, size_t k)
825{
826 if (k > n)
827 return 0;
828
829 k = std::min(k, n - k);
830 if (k == 0)
831 return 1;
832
833 size_t result = 1;
834 for (size_t i = 1; i <= k; ++i)
835 {
836 size_t num = n - k + i;
837 size_t den = i;
838
839 size_t g = std::gcd(num, den);
840 num /= g;
841 den /= g;
842
843 g = std::gcd(result, den);
844 result /= g;
845 den /= g;
846
847 g = std::gcd(num, den);
848 num /= g;
849 den /= g;
850
851 ah_runtime_error_if(den != 1)
852 << "combination_count: internal reduction failure for C(" << n << ", " << k << ")";
853
854 ah_runtime_error_if(result > std::numeric_limits<size_t>::max() / num)
855 << "combination_count: overflow for C(" << n << ", " << k << ")";
856 result *= num;
857 }
858
859 return result;
860}
861
878[[nodiscard]] inline bool next_combination_indices(Array<size_t> &idx, const size_t n,
879 const bool reset_on_last = true)
880{
881 return comb_detail::next_combination_indices_impl(idx, n, reset_on_last);
882}
883
885[[nodiscard]] inline bool next_combination_indices(DynArray<size_t> &idx, const size_t n,
886 const bool reset_on_last = true)
887{
888 return comb_detail::next_combination_indices_impl(idx, n, reset_on_last);
889}
890
892[[nodiscard]] inline uint64_t first_combination_mask(const size_t k)
893{
894 ah_out_of_range_error_if(k > 64) << "first_combination_mask: k=" << k << " cannot exceed 64";
895
896 if (k == 0)
897 return 0;
898 if (k == 64)
899 return std::numeric_limits<uint64_t>::max();
900 return (static_cast<uint64_t>(1) << k) - 1;
901}
902
919[[nodiscard]] inline bool next_combination_mask(uint64_t &mask, const size_t n,
920 const bool reset_on_last = true)
921{
922 ah_out_of_range_error_if(n > 64) << "next_combination_mask: n=" << n << " cannot exceed 64";
923
924 if (n == 0)
925 {
926 ah_domain_error_if(mask != 0) << "next_combination_mask: for n=0, mask must be 0";
927 return false;
928 }
929
930 const uint64_t domain_mask =
931 n == 64 ? std::numeric_limits<uint64_t>::max() : ((uint64_t(1) << n) - 1);
932
933 ah_domain_error_if(mask & ~domain_mask)
934 << "next_combination_mask: mask has bits outside the low n bits";
935
936 const size_t k = std::popcount(mask);
937 if (k == 0)
938 {
939 if (reset_on_last)
940 mask = 0;
941 return false;
942 }
943
944 const uint64_t c = mask & (~mask + 1); // isolate least significant set bit
945 const uint64_t r = mask + c;
946
947 if (r == 0)
948 {
949 if (reset_on_last)
951 return false;
952 }
953
954 const uint64_t next = (((r ^ mask) >> 2) / c) | r;
955 if (next & ~domain_mask)
956 {
957 if (reset_on_last)
959 return false;
960 }
961
962 mask = next;
963 return true;
964}
965
967template <class ValuesArray, class Op>
968[[nodiscard]] static inline bool for_each_combination_impl(const ValuesArray &values,
969 const size_t k, Op &&op)
970{
971 const size_t n = values.size();
972 ah_domain_error_if(k > n) << "for_each_combination: k=" << k << " cannot exceed n=" << n;
973
974 if (k == 0)
976
978 for (size_t i = 0; i < k; ++i)
979 idx(i) = i;
980
981 while (true)
982 {
984 for (size_t i = 0; i < k; ++i)
985 comb(i) = values(idx(i));
986
987 if (not op(comb))
988 return false;
989
990 if (not next_combination_indices(idx, n, false))
991 break;
992 }
993
994 return true;
995}
997
1012template <typename T, class Op>
1013[[nodiscard]] inline bool for_each_combination(const Array<T> &values, const size_t k, Op &&op)
1014{
1015 return for_each_combination_impl(values, k, std::forward<Op>(op));
1016}
1017
1019template <typename T, class Op>
1020[[nodiscard]] inline bool for_each_combination(const DynArray<T> &values, const size_t k, Op &&op)
1021{
1022 return for_each_combination_impl(values, k, std::forward<Op>(op));
1023}
1024
1034template <typename T>
1035[[nodiscard]] inline Array<Array<T>> build_combinations(const Array<T> &values, const size_t k)
1036{
1037 Array<Array<T>> ret;
1038 (void) for_each_combination(values, k, [&ret](const Array<T> &comb)
1039 {
1040 ret.append(comb);
1041 return true;
1042 });
1043 return ret;
1044}
1045
1047template <typename T>
1048[[nodiscard]] inline Array<Array<T>> build_combinations(const DynArray<T> &values, const size_t k)
1049{
1050 Array<Array<T>> ret;
1051 (void) for_each_combination(values, k, [&ret](const Array<T> &comb)
1052 {
1053 ret.append(comb);
1054 return true;
1055 });
1056 return ret;
1057}
1058
1070template <std::unsigned_integral T>
1071[[nodiscard]] constexpr T bin_to_gray(const T n) noexcept
1072{
1073 return n ^ (n >> 1);
1074}
1075
1087template <std::unsigned_integral T>
1088[[nodiscard]] constexpr T gray_to_bin(T g) noexcept
1089{
1090 for (size_t shift = 1; shift < sizeof(T) * 8; shift <<= 1)
1091 g ^= (g >> shift);
1092 return g;
1093}
1094
1107[[nodiscard]] inline Array<uint32_t> build_gray_code(const size_t n)
1108{
1109 ah_domain_error_if(n > 31) << "build_gray_code: n=" << n << " exceeds 31-bit limit for Array";
1110
1111 const size_t count = size_t(1) << n;
1112 Array<uint32_t> ret;
1113 ret.reserve(count);
1114 for (size_t i = 0; i < count; ++i)
1115 ret.append(bin_to_gray(static_cast<uint32_t>(i)));
1116
1117 return ret;
1118}
1119} // namespace Aleph
1120
1121#endif // COMB_H
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.
Definition ah-errors.H:727
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:415
STL-compatible iterator adapters for Aleph containers.
Standard functor implementations and comparison objects.
High-level sorting functions for Aleph containers.
size_t row
Definition ca-c-api.h:115
size_t size_t col
Definition ca-c-api.h:116
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
Read-only view of an Aleph iterator: the iterator of a constant container.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
DynList & rev() noexcept
Definition htlist.H:1734
T remove_first()
See remove().
Definition htlist.H:1473
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
DynList & swap(DynList &l) noexcept
Definition htlist.H:1253
Slinknc * remove_head()
Remove the first node of the list; the caller takes the node, and its item.
Definition htlist.H:2001
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
Definition htlist.H:969
Single linked list of nodes.
Definition htlist.H:420
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Link of a single linked list non-circular and without header node.
Definition htlist.H:95
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
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.
Definition ah-comb.H:694
DynList< DynList< T > > build_combs(const DynList< DynList< T > > &l)
Build the set of unique combinations from a list of lists.
Definition ah-comb.H:535
bool for_each_combination(const Array< T > &values, const size_t k, Op &&op)
*‍/
Definition ah-comb.H:1013
Array< Array< T > > build_combinations(const Array< T > &values, const size_t k)
Materialize all k-combinations of Array<T>.
Definition ah-comb.H:1035
DynList< R > map_perm(const DynList< DynList< T > > &l, Op &op)
Transform each permutation via a mapping operation.
Definition ah-comb.H:753
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.
Definition ah-comb.H:878
T fold_perm(const T &init, const DynList< DynList< Tc > > &l, Op &op)
Left-fold over all permutations.
Definition ah-comb.H:568
size_t perm_count(const DynList< DynList< T > > &l)
Count the total number of permutations from a list of lists.
Definition ah-comb.H:601
bool traverse_perm(const DynList< DynList< T > > &l, Op &op)
*‍/
Definition ah-comb.H:443
constexpr T gray_to_bin(T g) noexcept
Convert a Gray code number to its binary representation.
Definition ah-comb.H:1088
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).
Definition ah-comb.H:919
DynList< DynList< T > > filter_perm(const DynList< DynList< T > > &l, Pred &pred)
Filter permutations that satisfy a predicate.
Definition ah-comb.H:720
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
Array< uint32_t > build_gray_code(const size_t n)
Generate the sequence of n-bit Gray codes.
Definition ah-comb.H:1107
bool next_permutation(Array< T > &a, Compare cmp=Compare(), const bool reset_on_last=true)
Compute the next lexicographic permutation of an Array.
Definition ah-comb.H:800
void for_each_perm(const DynList< DynList< T > > &l, Op &op)
Apply a procedure to every permutation produced by traverse_perm.
Definition ah-comb.H:482
DynList< DynList< T > > transpose(const DynList< DynList< T > > &l)
*‍/
Definition ah-comb.H:250
uint64_t first_combination_mask(const size_t k)
Build the first k-of-64 combination mask (k low bits set).
Definition ah-comb.H:892
size_t combination_count(size_t n, size_t k)
Compute n choose k with overflow checks.
Definition ah-comb.H:824
DynList< DynList< T > > build_perms(const DynList< DynList< T > > &l)
Materialize all permutations from a list of lists.
Definition ah-comb.H:511
void in_place_transpose(C< C< T > > &l)
In-place transpose of a rectangular matrix stored as a nested container.
Definition ah-comb.H:294
bool exists_perm(const DynList< DynList< T > > &l, Pred &pred)
Check if any permutation satisfies a predicate.
Definition ah-comb.H:628
constexpr T bin_to_gray(const T n) noexcept
Convert a binary number to its Gray code representation.
Definition ah-comb.H:1071
bool all_perm(const DynList< DynList< T > > &l, Pred &pred)
Check if all permutations satisfy a predicate.
Definition ah-comb.H:666
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
static std::atomic< bool > init
Definition hash-fct.C:54
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:831
static int * k
Gsl_Compatible_Rng r
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.
DynList< int > l