71#include <tclap/CmdLine.h>
85 cout <<
"\n" << string(60,
'=') <<
"\n";
86 cout <<
" " << title <<
"\n";
87 cout << string(60,
'=') <<
"\n\n";
92 cout <<
"\n--- " << title <<
" ---\n";
98 cout << label <<
": [";
100 for (
auto it =
l.
get_it(); it.has_curr(); it.next())
102 if (not first) cout <<
", ";
103 cout << it.get_curr();
112 cout << label <<
":" << endl;
114 for (
auto it = mat.get_it(); it.has_curr(); it.next(),
row++)
116 cout <<
" [" <<
row <<
"]: [";
118 for (
auto jt = it.get_curr().get_it(); jt.has_curr(); jt.next())
120 if (not first) cout <<
", ";
121 cout << jt.get_curr();
169 cout <<
"Given lists of choices, enumerate all combinations.\n";
170 cout <<
"This is the CARTESIAN PRODUCT, not mathematical permutations.\n\n";
177 cout <<
"Choices:" << endl;
178 cout <<
" Colors: [rojo, azul, verde]" << endl;
179 cout <<
" Sizes: [S, M, L]" << endl;
183 cout <<
"All color-size combinations:\n";
186 cout <<
" " << ++
count <<
": ";
188 for (
auto it = perm.
get_it(); it.has_curr(); it.next())
190 if (not first) cout <<
"-";
191 cout << it.get_curr();
199 cout <<
"Total permutations: " <<
perm_count(choices) << endl;
200 cout <<
" (3 colors × 3 sizes = 9)" << endl;
209 cout <<
"Binary digits: [0,1] × [0,1] × [0,1]\n";
210 cout <<
"All 3-bit binary numbers:\n";
213 for (
auto it = perm.
get_it(); it.has_curr(); it.next())
214 cout << it.get_curr();
217 cout <<
"Total: " <<
perm_count(digits) <<
" (2³ = 8)" << endl;
233 cout <<
"Two dice: [1-6] × [1-6] = 36 outcomes\n";
240 for (
auto it = roll.
get_it(); it.has_curr(); it.next())
241 sum += it.get_curr();
244 cout <<
"Exists roll with sum = 12? " << (has_double_six ?
"yes" :
"no") << endl;
248 for (
auto it = roll.
get_it(); it.has_curr(); it.next())
249 sum += it.get_curr();
252 cout <<
"Exists roll with sum = 15? " << (has_sum_15 ?
"yes" :
"no") << endl;
258 for (
auto it = roll.
get_it(); it.has_curr(); it.next())
259 if (it.get_curr() <= 0)
return false;
262 cout <<
"All rolls have positive values? " << (all_positive ?
"yes" :
"no") << endl;
266 for (
auto it = roll.
get_it(); it.has_curr(); it.next())
267 sum += it.get_curr();
270 cout <<
"All rolls have sum > 10? " << (all_sum_gt_10 ?
"yes" :
"no") << endl;
276 for (
auto it = roll.
get_it(); it.has_curr(); it.next())
277 if (it.get_curr() == 0)
return true;
280 cout <<
"No roll contains a zero? " << (none_zero ?
"yes" :
"no") << endl;
295 cout <<
"Lists: [1,2,3] × [10,20,30]\n\n";
302 for (
auto it = perm.
get_it(); it.has_curr(); it.next())
303 sum += it.get_curr();
305 cout <<
" Checking: ";
307 for (
auto it = perm.
get_it(); it.has_curr(); it.next())
309 if (not first) cout <<
"+";
310 cout << it.get_curr();
313 cout <<
" = " <<
sum;
317 cout <<
" > 25, STOP!" << endl;
324 cout <<
"\nFound sum > 25? " << (found ?
"yes" :
"no") << endl;
339 cout <<
"Values: [1,2] × [3,4]\n";
340 cout <<
"Permutations: (1,3), (1,4), (2,3), (2,4)\n\n";
347 for (
auto it = perm.
get_it(); it.has_curr(); it.next())
349 cout <<
" Product: " <<
product <<
", Running total: " << (acc +
product) << endl;
353 cout <<
"\nTotal (1×3 + 1×4 + 2×3 + 2×4) = " << total << endl;
354 cout <<
"Expected: 3 + 4 + 6 + 8 = 21" << endl;
370 cout <<
"Menu choices:" << endl;
371 cout <<
" Bebida: [cafe, te]" << endl;
372 cout <<
" Comida: [arepa, empanada]" << endl;
373 cout <<
" Extra: [postre]" << endl;
379 cout <<
"All possible orders (" << all_combos.size() <<
" total):\n";
381 for (
auto it = all_combos.get_it(); it.has_curr(); it.next())
383 cout <<
" " << ++n <<
": ";
384 auto& combo = it.get_curr();
386 for (
auto jt = combo.get_it(); jt.has_curr(); jt.next())
388 if (not first) cout <<
" + ";
389 cout << jt.get_curr();
404 cout <<
"Generate all test configurations for a system.\n\n";
411 cout <<
"Options:" << endl;
412 cout <<
" Build: [debug, release]" << endl;
413 cout <<
" Arch: [x86, x64, arm]" << endl;
414 cout <<
" Platform: [linux, windows]" << endl;
417 cout <<
"Total: " <<
perm_count(config_options) <<
" configurations\n\n";
421 cout <<
" " << setw(2) << ++n <<
". ";
423 for (
auto it = config.
get_it(); it.has_curr(); it.next())
425 if (not first) cout <<
"-";
426 cout << it.get_curr();
434 size_t linux_count = 0;
436 for (
auto it = config.
get_it(); it.has_curr(); it.next())
437 if (it.get_curr() ==
"linux")
443 cout <<
"Linux configurations: " << linux_count << endl;
450int main(
int argc,
char* argv[])
455 "Combinatorics example for Aleph-w.\n"
456 "Demonstrates transpose, permutations, and combinations.",
460 TCLAP::ValueArg<string> sectionArg(
462 "Run only specific section: transpose, perm, predicates, "
463 "traverse, fold, build, practical, or 'all'",
464 false,
"all",
"section",
cmd
467 cmd.parse(argc, argv);
469 string section = sectionArg.getValue();
472 cout <<
"============================================================\n";
473 cout <<
" ALEPH-W COMBINATORICS EXAMPLE\n";
474 cout <<
"============================================================\n";
497 cout <<
"\n" << string(60,
'=') <<
"\n";
498 cout <<
"Combinatorics demo completed!\n";
499 cout << string(60,
'=') <<
"\n\n";
503 catch (TCLAP::ArgException& e)
505 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() << endl;
510 cerr <<
"Error: " << e.what() << endl;
Combinatorics utilities: permutations, combinations, and matrix transposition.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
void print_subsection(const string &title)
void print_matrix(const string &label, const DynList< DynList< T > > &mat)
void print_section(const string &title)
void demo_perm_predicates()
void print_list(const string &label, const DynList< T > &l)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
bool none_perm(const DynList< DynList< T > > &l, Pred &pred)
Check if no permutation satisfies a predicate.
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)
*/
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)
*/
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
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.
bool all_perm(const DynList< DynList< T > > &l, Pred &pred)
Check if all permutations satisfy a predicate.
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.
static void section(const string &title)