Aleph-w 7.10.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 auto idx = Array<size_t>::create(k);
170 for (size_t i = 0; i < k; ++i)
171 idx[i] = i;
172
173 do
174 {
175 auto combo = Array<T>::create_reserved(k);
176 for (size_t i = 0; i < k; ++i)
177 combo.append(a[idx[i]]);
178 co_yield combo;
179 }
180 while (next_combination_indices(idx, n, false));
181}
182
183} // end namespace Aleph
184
185#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:670
Lazy sequence type (Aleph::Generator<T>) built on C++20 coroutines.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
Definition tpl_array.H:194
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
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:4196
Main namespace for Aleph-w library functions.
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
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:800
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.