143#include <tclap/CmdLine.h>
151using namespace Aleph;
159 cout <<
"\n" << string(60,
'=') <<
"\n";
160 cout <<
" " << title <<
"\n";
161 cout << string(60,
'=') <<
"\n\n";
166 cout <<
"\n--- " << title <<
" ---\n";
169template <
typename Container>
172 cout << label <<
": [";
174 for (
auto it = c.get_it(); it.has_curr(); it.next())
176 if (not first) cout <<
", ";
177 cout << it.get_curr();
201 cout <<
"Iterating numbers and names together:\n";
202 for (
ZipIterator it(numbers, names); it.has_curr(); it.next())
204 auto [num, name] = it.get_curr();
205 cout <<
" " << num <<
" -> " << name << endl;
210 cout <<
"Iterating numbers, names, and letters:\n";
211 for (
ZipIterator it(numbers, names, letters); it.has_curr(); it.next())
213 auto [num, name, letter] = it.get_curr();
214 cout <<
" " << num <<
" (" << name <<
") [" << letter <<
"]" << endl;
222 cout <<
"Zipping numbers with short_list (stops at shorter):\n";
223 for (
ZipIterator it(numbers, short_list); it.has_curr(); it.next())
225 auto [a, b] = it.get_curr();
226 cout <<
" " << a <<
", " << b << endl;
242 cout <<
"prices: [1.50, 0.80, 2.30, 1.20]" << endl;
246 cout <<
"Iterating with index:\n";
249 auto [idx, fruit, price] = it.get_curr();
250 cout <<
" [" << idx <<
"] " << fruit <<
": $" << fixed << setprecision(2) << price << endl;
256 cout <<
"Created list of (index, fruit, price) tuples:\n";
257 for (
auto it = enumerated.get_it(); it.has_curr(); it.next())
259 auto [idx, fruit, price] = it.get_curr();
260 cout <<
" (" << idx <<
", " << fruit <<
", $" << price <<
")" << endl;
277 cout <<
"prices: [15.0, 5.0, 8.0]" << endl;
282 auto inventory =
t_zip(products, prices, stock);
283 cout <<
"Inventory as list of tuples:\n";
284 for (
auto it = inventory.get_it(); it.has_curr(); it.next())
286 auto [prod, price, qty] = it.get_curr();
287 cout <<
" (" << prod <<
", $" << price <<
", " << qty <<
" units)" << endl;
292 auto [prods_back, prices_back, stock_back] =
t_unzip(inventory);
293 cout <<
"Unzipped back to separate lists:\n";
295 cout <<
" prices: [";
297 for (
auto it = prices_back.get_it(); it.has_curr(); it.next())
299 if (not first) cout <<
", ";
300 cout << it.get_curr();
319 cout <<
"unit_prices: [10.0, 25.0, 5.0, 100.0]" << endl;
323 auto totals =
zip_map([](
const auto& t) {
324 return get<0>(t) * get<1>(t);
325 }, quantities, unit_prices);
327 cout <<
"Totals (qty * price):\n";
328 for (
auto it = totals.get_it(); it.has_curr(); it.next())
329 cout <<
" $" << fixed << setprecision(2) << it.get_curr() << endl;
333 auto expensive =
zip_filter([](
const auto& t) {
334 return get<0>(t) * get<1>(t) > 30;
335 }, quantities, unit_prices);
337 cout <<
"Orders > $30:\n";
338 for (
auto it = expensive.get_it(); it.has_curr(); it.next())
340 auto [qty, price] = it.get_curr();
341 cout <<
" " << qty <<
" x $" << price <<
" = $" << (qty * price) << endl;
347 cout <<
"Product descriptions:\n";
348 for (
ZipIterator it(names, quantities, unit_prices); it.has_curr(); it.next())
350 auto [name, qty, price] = it.get_curr();
351 cout <<
" " << name <<
" (" << qty <<
" @ $" << price <<
")" << endl;
363 DynList<string> cities = {
"Bogota",
"Medellin",
"Cali",
"Barranquilla",
"Cartagena"};
364 DynList<int> populations = {7400, 2500, 2200, 1200, 1000};
367 print_list(
"populations (thousands)", populations);
371 auto top3 =
zip_take(3, cities, populations);
372 cout <<
"Top 3 cities:\n";
373 for (
auto it = top3.get_it(); it.has_curr(); it.next())
375 auto [city, pop] = it.get_curr();
376 cout <<
" " << city <<
": " << pop <<
"k" << endl;
381 auto rest =
zip_drop(3, cities, populations);
382 cout <<
"Remaining cities (after dropping 3):\n";
383 for (
auto it = rest.get_it(); it.has_curr(); it.next())
385 auto [city, pop] = it.get_curr();
386 cout <<
" " << city <<
": " << pop <<
"k" << endl;
392 return get<1>(t) > 1500;
393 }, cities, populations);
395 cout <<
"Cities with population > 1500k (prefix):\n";
396 for (
auto it = big_cities.get_it(); it.has_curr(); it.next())
398 auto [city, pop] = it.get_curr();
399 cout <<
" " << city <<
": " << pop <<
"k" << endl;
404 auto all_tuples =
t_zip(cities, populations);
406 auto large = all_tuples.filter([](
const auto& t) {
407 return get<1>(t) >= 2000;
409 auto small_cities = all_tuples.filter([](
const auto& t) {
410 return get<1>(t) < 2000;
413 cout <<
"Large cities (>= 2M):\n";
414 for (
auto it = large.get_it(); it.has_curr(); it.next())
416 auto [city, pop] = it.get_curr();
417 cout <<
" " << city <<
": " << pop <<
"k" << endl;
419 cout <<
"Smaller cities (< 2M):\n";
420 for (
auto it = small_cities.get_it(); it.has_curr(); it.next())
422 auto [city, pop] = it.get_curr();
423 cout <<
" " << city <<
": " << pop <<
"k" << endl;
435 vector<int> vec1 = {1, 2, 3, 4};
436 vector<string> vec2 = {
"a",
"b",
"c",
"d"};
438 cout <<
"vec1 (std::vector): [1, 2, 3, 4]" << endl;
439 cout <<
"vec2 (std::vector): [a, b, c, d]" << endl;
443 auto zipped =
std_zip(vec1, vec2);
444 cout <<
"Zipped STL vectors (returns std::vector):\n";
445 for (
const auto& [num, letter] : zipped)
447 cout <<
" (" << num <<
", " << letter <<
")" << endl;
452 vector<double> vec3 = {1.1, 2.2, 3.3, 4.4};
453 cout <<
"vec3 (std::vector): [1.1, 2.2, 3.3, 4.4]" << endl;
455 auto triple_zip =
tzip_std(vec1, vec2, vec3);
456 cout <<
"Triple-zipped (returns std::vector):\n";
457 for (
const auto& t : triple_zip)
459 auto [num, letter, dbl] = t;
460 cout <<
" (" << num <<
", " << letter <<
", " << dbl <<
")" << endl;
482 cout <<
"list1 and list2 equal length? "
484 cout <<
"list1 and list3 equal length? "
489 cout <<
"Zipping list1 and list3:\n";
490 for (
ZipIterator it(list1, list3); it.has_curr(); it.next())
492 auto [a, b] = it.get_curr();
493 cout <<
" " << a <<
" + " << b <<
" = " << (a + b) << endl;
495 cout <<
" (Only 2 pairs produced)\n";
499 cout <<
"t_zip_eq on equal lists:\n";
502 auto result =
t_zip_eq(list1, list2);
503 cout <<
" Created " << result.size() <<
" tuples successfully\n";
505 catch (
const length_error& e)
507 cout <<
" Error: " << e.what() << endl;
510 cout <<
"\nt_zip_eq on unequal lists:\n";
513 auto result =
t_zip_eq(list1, list3);
514 cout <<
" Created " << result.size() <<
" tuples\n";
516 catch (
const length_error& e)
518 cout <<
" Caught exception: " << e.what() << endl;
531 DynList<string> regions = {
"Huila",
"NariƱo",
"Cauca",
"Tolima",
"Antioquia"};
541 cout <<
"Growth by region:\n";
542 for (
ZipIterator it(regions, bags_2023, bags_2024); it.has_curr(); it.next())
544 auto [region, y23, y24] = it.get_curr();
545 double pct = 100.0 * (y24 - y23) / y23;
546 cout <<
" " << setw(12) << left << region
547 <<
": " << y23 <<
" -> " << y24
548 <<
" (" << showpos << fixed << setprecision(1) << pct <<
"%)"
549 << noshowpos << endl;
554 auto tuples =
t_zip(regions, bags_2023, bags_2024);
555 auto good_growth = tuples.filter([](
const auto& t) {
558 return (y24 - y23) * 100.0 / y23 > 10.0;
561 cout <<
"High growth regions:\n";
562 for (
auto it = good_growth.get_it(); it.has_curr(); it.next())
564 auto [region, y23, y24] = it.get_curr();
565 double pct = 100.0 * (y24 - y23) / y23;
566 cout <<
" " << region <<
": " << showpos << fixed << setprecision(1)
567 << pct <<
"%" << noshowpos << endl;
572 int total_2023 = 0, total_2024 = 0;
573 for (
ZipIterator it(bags_2023, bags_2024); it.has_curr(); it.next())
575 auto [y23, y24] = it.get_curr();
580 cout <<
"2023 total: " << total_2023 <<
" bags" << endl;
581 cout <<
"2024 total: " << total_2024 <<
" bags" << endl;
582 cout <<
"Overall growth: " << showpos << fixed << setprecision(1)
583 << 100.0 * (total_2024 - total_2023) / total_2023 <<
"%" << noshowpos << endl;
590int main(
int argc,
char* argv[])
595 "Comprehensive zip operations example for Aleph-w.\n"
596 "Demonstrates ZipIterator, t_zip, zip_map, and more.",
600 TCLAP::ValueArg<string> sectionArg(
602 "Run only specific section: basic, enum, tuples, transform, "
603 "utilities, stl, length, practical, or 'all'",
604 false,
"all",
"section",
cmd
607 cmd.parse(argc, argv);
609 string section = sectionArg.getValue();
612 cout <<
"============================================================\n";
613 cout <<
" ALEPH-W ZIP OPERATIONS EXAMPLE\n";
614 cout <<
"============================================================\n";
640 cout <<
"\n" << string(60,
'=') <<
"\n";
641 cout <<
"Zip operations demo completed!\n";
642 cout << string(60,
'=') <<
"\n\n";
646 catch (TCLAP::ArgException& e)
648 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() << endl;
653 cerr <<
"Error: " << e.what() << endl;
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
Doubly-linked list (defined in tpl_dynList.H).
Iterator that traverses multiple containers with enumeration index.
Iterator that traverses multiple Aleph containers in lockstep.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
auto std_zip(const C1 &c1, const C2 &c2)
Build a vector of pairs from two STL containers.
bool equal_length(const Cs &...cs)
Return true if all containers have the same length.
auto zip_filter(Op op, const Cs &...cs)
Filter zipped tuples by predicate op.
auto t_zip(const Cs &...cs)
Materialize zipped tuples into a DynList.
auto t_zip_eq(const Cs &...cs)
Materialize zipped tuples; throw if containers differ in length.
auto tzip_std(const Cs &...cs)
Build a vector of tuples from 2 or more STL containers (variadic).
auto t_enum_zip(const Cs &...cs)
Materialize enumerated zipped tuples into a DynList.
auto t_unzip(const DynList< std::tuple< Ts... > > &tuples)
Unzip a list of tuples into a tuple of lists.
auto zip_map(Op &&op, const Cs &...cs)
Map op over zipped tuples with auto-deduced return type.
auto zip_take_while(Pred &&pred, const Cs &...cs)
Take tuples while predicate pred returns true.
auto zip_drop(const size_t n, const Cs &...cs)
Skip n tuples and return the rest.
auto zip_take(size_t n, const Cs &...cs)
Take at most n tuples from the zipped containers.
static void section(const string &title)
Dynamic doubly linked list implementation.
void demo_tuple_operations()
void print_subsection(const string &title)
void demo_zip_transformation()
void demo_length_checking()
void print_section(const string &title)
void demo_zip_utilities()
void print_list(const string &label, const Container &c)
void demo_practical_example()
void demo_stl_compatibility()