Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-comb-generators.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
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
56#ifndef AH_COMB_GENERATORS_H
57#define AH_COMB_GENERATORS_H
58
59#include <ah-comb.H>
60#include <ah-generator.H>
61#include <tpl_array.H>
62#include <tpl_sort_utils.H>
63
64namespace Aleph
65{
66
103template <typename T, class Compare = Aleph::less<T>>
105{
108 timsort(a, cmp);
109 co_yield a;
110 while (next_permutation(a, cmp, false))
111 co_yield a;
112}
113
156template <typename T>
158{
159 const size_t n = a.size();
160 ah_domain_error_if(k > n) << "lazy_combinations: k=" << k << " cannot exceed n=" << n;
161
162 if (k == 0)
163 {
164 co_yield Array<T>();
165 co_return;
166 }
167
169 Array<size_t> idx(k);
170 idx.putn(k);
171 for (size_t i = 0; i < k; ++i)
172 idx[i] = i;
173
174 do
175 {
177 for (size_t i = 0; i < k; ++i)
178 combo.append(a[idx[i]]);
179 co_yield combo;
180 }
181 while (next_combination_indices(idx, n, false));
182}
183
184} // end namespace Aleph
185
186#endif // AH_COMB_GENERATORS_H
Combinatorics utilities: permutations, combinations, and matrix transposition.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:527
Lazy sequence type (Aleph::Generator<T>) built on C++20 coroutines.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:365
void putn(const size_t n)
Reserve n additional logical slots in the array without value-initializing them.
Definition tpl_array.H:310
Lazy, single-pass sequence of T values produced by a coroutine.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4129
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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:891
void timsort(T *a, const size_t n, const Compare &cmp=Compare())
Timsort — adaptive, stable, natural merge sort.
Aleph::Generator< Array< T > > lazy_permutations(Array< T > a, Compare cmp=Compare())
Lazily enumerate all permutations of a, in lexicographic order.
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:813
Aleph::Generator< Array< T > > lazy_combinations(Array< T > a, size_t k)
Lazily enumerate all k-element combinations of a's elements.
static int * k
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.