Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-generator.H File Reference

Lazy sequence type (Aleph::Generator<T>) built on C++20 coroutines. More...

#include <coroutine>
#include <exception>
#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>
#include <ah-errors.H>
Include dependency graph for ah-generator.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::Generator< T >
 Lazy, single-pass sequence of T values produced by a coroutine. More...
 
struct  Aleph::Generator< T >::promise_type
 The promise type the compiler instantiates for a coroutine returning Generator<T>. More...
 
class  Aleph::Generator< T >::iterator
 Single-pass input iterator over the yielded sequence. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Detailed Description

Lazy sequence type (Aleph::Generator<T>) built on C++20 coroutines.

Aleph::Generator<T> is a single-pass, lazily-evaluated sequence produced by a coroutine that co_yields values of type T. It is the reusable building block behind the lazy counterparts of Aleph's eager enumerators: combinatorics (ah-comb.H), tree traversals (tpl_binNodeUtils.H) and graph traversals (graph-traverse.H).

Aleph::Generator<int> countdown(int n)
{
for (; n > 0; --n)
co_yield n;
}
for (int i : countdown(3)) // prints 3 2 1, one at a time
std::cout << i << '\n';
Lazy, single-pass sequence of T values produced by a coroutine.
STL namespace.
Note
This is a native Aleph implementation, not an alias of C++23 std::generator<T>. Coroutines are a C++20 language feature — Aleph::Generator needs no C++23 library support and behaves identically on every standard the library targets (17 is unsupported for coroutine use, 20 and 23 behave the same). This mirrors the Aleph::FlatSet/Aleph::FlatMap decision (see tpl_flat_set.H): a smaller, uniform surface beats aliasing a standard facility whose semantics (allocator support, elements_of, reference-type customization) would otherwise leak through and vary by -std. The ALEPH_HAS_STD_GENERATOR macro in ah-cpp-compat.H remains available for consumers who specifically want std::generator itself.
Laziness vs. performance
Prefer Aleph::Generator for ergonomics and memory savings when the full sequence would be expensive to materialize or is only partially consumed (early break). It is not a drop-in replacement for hand-written iterators on hot paths: each resume is a coroutine-frame resumption, which most compilers cannot fully inline away.
Author
Leandro Rabindranath Leon

Definition in file ah-generator.H.