146#include <tclap/CmdLine.h>
154using namespace Aleph;
162 cout <<
"\n" << string(60,
'=') <<
"\n";
164 cout << string(60,
'=') <<
"\n\n";
172template <
typename Container>
175 cout << label <<
": [";
177 for (
auto it = c.get_it(); it.has_curr(); it.next())
180 cout << it.get_curr();
196 cout <<
"Unlike traditional range() which allocates immediately,\n";
197 cout <<
"lazy_range() generates values on demand.\n\n";
202 cout <<
"lazy_range(1, 10): ";
207 cout <<
"lazy_range(5): ";
215 cout <<
"Processing lazy_range(1, 1000000) but stopping at 5:\n";
220 if (++
count >= 5)
break;
222 cout <<
"\n(Only 5 values were generated, not 1 million!)\n";
227 cout <<
"First 10 values from lazy_iota(100): ";
232 if (++
count >= 10)
break;
245 cout <<
"Views are lazy, composable transformations.\n";
246 cout <<
"No allocation until you iterate or materialize.\n\n";
251 cout <<
"Even numbers in [1, 20]: ";
259 cout <<
"Squares of [1, 10]: ";
267 cout <<
"First 5 of [1, 100]: ";
272 cout <<
"Skip first 95 of [1, 100]: ";
280 cout <<
"First 5 primes (brute force): ";
282 if (n < 2)
return false;
283 for (
int i = 2; i <=
sqrt(n); i++)
284 if (n % i == 0)
return false;
301 cout <<
"Convert lazy ranges to Aleph containers using pipe adaptors.\n\n";
307 | std::views::transform([](
int x) {
return x * 10; })
316 | std::views::filter([](
int x) {
return x % 2 == 1; })
337 lazy_range(1, 5) | std::views::transform([](
int x) {
return x * 0.5; })
350 cout <<
"Aleph containers work with std::ranges algorithms.\n\n";
354 for (
int i = 1; i <= 10; i++)
362 bool has_even = std::ranges::any_of(
numbers, [](
int x) {
return x % 2 == 0; });
368 auto count_gt5 = std::ranges::count_if(
numbers, [](
int x) {
return x > 5; });
374 cout <<
"Filtered (even): ";
379 cout <<
"Transformed (squared): ";
403 auto fib =
lazy_range(15) | std::views::transform([&a, &b](
int)
mutable {
411 cout <<
"Fibonacci: ";
421 if (n % 15 == 0)
cout <<
"FizzBuzz ";
422 else if (n % 3 == 0)
cout <<
"Fizz ";
423 else if (n % 5 == 0)
cout <<
"Buzz ";
424 else cout << n <<
" ";
437 cout <<
"Raw readings: ";
439 cout << it.get_curr() <<
" ";
444 | std::views::filter([](
double x) {
return x >= 0; })
445 | std::views::transform([](
double c) {
return c * 9/5 + 32; })
448 cout <<
"Valid readings in Fahrenheit: ";
472 cout <<
"Lazy evaluation can be more efficient when:\n";
473 cout <<
"- You don't need all elements\n";
474 cout <<
"- You're chaining multiple operations\n";
475 cout <<
"- Working with large or infinite sequences\n\n";
483 | std::views::filter([](
int x) {
return x > 1000; })
484 | std::views::take(1))
486 cout << x <<
" (computed ~32 squares)" <<
endl;
490 cout <<
"\nWith lazy evaluation, we only compute what's needed.\n";
491 cout <<
"No intermediate containers are created.\n";
500 cout <<
"Your compiler does not support C++20 ranges.\n\n";
501 cout <<
"Requirements:\n";
502 cout <<
" - C++20 or later (-std=c++20)\n";
503 cout <<
" - GCC 12+ with libstdc++, OR\n";
504 cout <<
" - Clang 14+ with libc++ (-stdlib=libc++)\n\n";
506 cout <<
"Alternative: Use ahFunctional.H for functional operations:\n";
507 cout <<
" - range(start, end) - eager range generation\n";
508 cout <<
" - filter(container, pred)\n";
509 cout <<
" - maps(container, func)\n";
510 cout <<
" - foldl(container, init, op)\n";
524 "C++20 Ranges example for Aleph-w.\n"
525 "Demonstrates lazy evaluation and range adaptors.",
531 "Run only specific section: lazy, views, materialize, aleph, "
532 "practical, perf, or 'all'",
533 false,
"all",
"section",
cmd
541 cout <<
"============================================================\n";
542 cout <<
" ALEPH-W C++20 RANGES EXAMPLE\n";
543 cout <<
"============================================================\n";
546 cout <<
"\nALEPH_HAS_RANGES = 1 (C++20 ranges supported)\n";
566 cout <<
"\nALEPH_HAS_RANGES = 0 (C++20 ranges not available)\n";
570 cout <<
"\n" << string(60,
'=') <<
"\n";
571 cout <<
"Ranges demo completed!\n";
572 cout << string(60,
'=') <<
"\n\n";
576 catch (TCLAP::ArgException& e)
578 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() <<
endl;
583 cerr <<
"Error: " << e.what() <<
endl;
C++20 Ranges support and adaptors for Aleph-w containers.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
Itor2 transform(Itor1 sourceBeg, Itor1 sourceEnd, Itor2 destBeg, UnaryFunc op)
Transform elements using a unary operation.
void next()
Advance all underlying iterators (bounds-checked).
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.
void print_subsection(const string &title)
void print_section(const string &title)
void print_container(const string &label, const Container &c)
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.