Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
zip_utils_example.C
Go to the documentation of this file.
1
145#include <iostream>
146#include <iomanip>
147#include <string>
148#include <vector>
149#include <list>
150#include <set>
151
152#include <tclap/CmdLine.h>
153
154#include <htlist.H>
155#include <tpl_dynArray.H>
156#include <ah-zip-utils.H>
157
158using namespace std;
159using namespace Aleph;
160
161// =============================================================================
162// Helper functions
163// =============================================================================
164
165void print_section(const string& title)
166{
167 cout << "\n" << string(60, '=') << "\n";
168 cout << " " << title << "\n";
169 cout << string(60, '=') << "\n\n";
170}
171
172void print_subsection(const string& title)
173{
174 cout << "\n--- " << title << " ---\n";
175}
176
177// =============================================================================
178// 1. Mixed Container Demo (The Key Feature!)
179// =============================================================================
180
182{
183 print_section("MIXED CONTAINER OPERATIONS");
184
185 cout << "The key feature of ah-zip-utils.H is mixing STL and Aleph containers!\n\n";
186
187 // STL containers
188 vector<int> stl_ids = {101, 102, 103, 104};
189 list<string> stl_names = {"Juan", "Maria", "Carlos", "Ana"};
190
191 // Aleph container
192 DynList<double> aleph_scores = {85.5, 92.3, 78.0, 95.8};
193
194 cout << "STL vector<int> ids: [101, 102, 103, 104]" << endl;
195 cout << "STL list<string> names: [Juan, Maria, Carlos, Ana]" << endl;
196 cout << "Aleph DynList<double>: [85.5, 92.3, 78.0, 95.8]" << endl;
197
198 // Mix all three!
199 print_subsection("uni_zip() - Mix STL and Aleph");
200 cout << "Zipping vector + list + DynList together:\n";
201 for (auto it = uni_zip_it(stl_ids, stl_names, aleph_scores); it.has_curr(); it.next())
202 {
203 auto [id, name, score] = it.get_curr();
204 cout << " ID " << id << ": " << setw(8) << left << name
205 << " Score: " << fixed << setprecision(1) << score << endl;
206 }
207
208 // Using uni_zip_it for lazy iteration
209 print_subsection("uni_zip_it() - Lazy iteration");
210 cout << "Using unified iterator:\n";
211 for (auto it = uni_zip_it(stl_ids, stl_names); it.has_curr(); it.next())
212 {
213 auto [id, name] = it.get_curr();
214 cout << " " << id << " -> " << name << endl;
215 }
216}
217
218// =============================================================================
219// 2. Predicates with Mixed Containers
220// =============================================================================
221
223{
224 print_section("PREDICATES WITH MIXED CONTAINERS");
225
226 vector<int> stl_quantities = {10, 25, 5, 30};
227 DynList<double> aleph_prices = {100.0, 50.0, 200.0, 25.0};
228
229 cout << "STL vector quantities: [10, 25, 5, 30]" << endl;
230 cout << "Aleph DynList prices: [100, 50, 200, 25]" << endl;
231
232 // uni_zip_all - all tuples satisfy predicate
233 print_subsection("uni_zip_all()");
234 bool all_valuable = uni_zip_all([](const auto& t) {
235 return get<0>(t) * get<1>(t) > 100; // qty * price > 100
237 cout << "All orders > $100? " << (all_valuable ? "yes" : "no") << endl;
238
239 // uni_zip_exists
240 print_subsection("uni_zip_exists()");
241 bool has_expensive = uni_zip_exists([](const auto& t) {
242 return get<0>(t) * get<1>(t) > 500;
244 cout << "Exists order > $500? " << (has_expensive ? "yes" : "no") << endl;
245
246 // uni_zip_none
247 print_subsection("uni_zip_none()");
248 bool none_cheap = uni_zip_none([](const auto& t) {
249 return get<0>(t) * get<1>(t) < 50;
251 cout << "No orders < $50? " << (none_cheap ? "yes" : "no") << endl;
252
253 // Show all orders
254 cout << "\nAll orders:\n";
255 uni_zip_for_each([](const auto& t) {
256 int qty = get<0>(t);
257 double price = get<1>(t);
258 cout << " " << qty << " x $" << price << " = $" << (qty * price) << endl;
260}
261
262// =============================================================================
263// 3. Transformations
264// =============================================================================
265
267{
268 print_section("TRANSFORMATIONS");
269
270 // Colombian departments and their coffee production
271 vector<string> stl_depts = {"Huila", "Nariño", "Cauca", "Tolima"};
272 DynList<int> aleph_production = {150, 85, 72, 65}; // thousands of bags
273
274 cout << "STL vector depts: [Huila, Nariño, Cauca, Tolima]" << endl;
275 cout << "Aleph DynList bags(k): [150, 85, 72, 65]" << endl;
276
277 // uni_zip_map (returns std::vector)
278 print_subsection("uni_zip_map()");
279 auto reports = uni_zip_map([](const auto& t) {
280 return get<0>(t) + ": " + to_string(get<1>(t)) + "k bags";
282
283 cout << "Production reports:\n";
284 for (const auto& r : reports)
285 cout << " " << r << endl;
286
287 // uni_zip_filter (returns std::vector)
288 print_subsection("uni_zip_filter()");
289 auto major_producers = uni_zip_filter([](const auto& t) {
290 return get<1>(t) >= 80; // >= 80k bags
292
293 cout << "Major producers (>= 80k bags):\n";
294 for (const auto& [dept, bags] : major_producers)
295 cout << " " << dept << ": " << bags << "k bags" << endl;
296
297 // uni_zip_mapi (with index, returns std::vector)
298 print_subsection("uni_zip_mapi() - with index");
299 auto ranked = uni_zip_mapi([](size_t idx, const auto& t) {
300 return "#" + to_string(idx + 1) + " " + get<0>(t);
302
303 cout << "Ranked by position:\n";
304 for (const auto& r : ranked)
305 cout << " " << r << endl;
306}
307
308// =============================================================================
309// 4. Utilities
310// =============================================================================
311
313{
314 print_section("UTILITIES");
315
316 list<string> stl_cities = {"Bogota", "Medellin", "Cali", "Barranquilla", "Cartagena"};
317 DynList<int> aleph_temps = {14, 24, 25, 28, 29}; // avg temp in Celsius
318
319 cout << "STL list cities: [Bogota, Medellin, Cali, Barranquilla, Cartagena]" << endl;
320 cout << "Aleph DynList temps(C): [14, 24, 25, 28, 29]" << endl;
321
322 // uni_zip_take (returns std::vector)
323 print_subsection("uni_zip_take()");
325 cout << "First 3 cities:\n";
326 for (const auto& [city, temp] : top3)
327 cout << " " << city << ": " << temp << "°C" << endl;
328
329 // uni_zip_drop (returns std::vector)
330 print_subsection("uni_zip_drop()");
332 cout << "Remaining cities:\n";
333 for (const auto& [city, temp] : rest)
334 cout << " " << city << ": " << temp << "°C" << endl;
335
336 // uni_zip_min and uni_zip_max (return std::optional)
337 print_subsection("uni_zip_min() / uni_zip_max()");
338
339 // Find city with min temperature
341 if (min_opt)
342 {
343 auto [city, temp] = *min_opt;
344 cout << "Coldest: " << city << " at " << temp << "°C" << endl;
345 }
346
348 if (max_opt)
349 {
350 auto [city, temp] = *max_opt;
351 cout << "Hottest: " << city << " at " << temp << "°C" << endl;
352 }
353}
354
355// =============================================================================
356// 5. Advanced Operations
357// =============================================================================
358
360{
361 print_section("ADVANCED OPERATIONS");
362
363 vector<int> vec_a = {1, 2, 3, 4, 5};
364 DynList<int> list_b = {10, 20, 30, 40, 50};
365
366 cout << "STL vector a: [1, 2, 3, 4, 5]" << endl;
367 cout << "Aleph DynList b: [10, 20, 30, 40, 50]" << endl;
368
369 // uni_zip_scan_left - running computation (returns std::vector)
370 print_subsection("uni_zip_scan_left()");
371 auto running_sums = uni_zip_scan_left(0, [](int acc, const auto& t) {
372 return acc + get<0>(t) + get<1>(t);
373 }, vec_a, list_b);
374
375 cout << "Running sum of (a[i] + b[i]):\n ";
376 for (int val : running_sums)
377 cout << val << " ";
378 cout << endl;
379
380 // uni_zip_equal_length
381 print_subsection("uni_zip_equal_length()");
382 vector<int> short_vec = {1, 2};
383 cout << "vec_a and list_b same length? "
384 << (uni_zip_equal_length(vec_a, list_b) ? "yes" : "no") << endl;
385 cout << "vec_a and short_vec same length? "
386 << (uni_zip_equal_length(vec_a, short_vec) ? "yes" : "no") << endl;
387
388 // uni_zip returns a view that can be iterated
389 print_subsection("uni_zip() - iterate view");
390 cout << "Zipped pairs: ";
391 for (auto it = uni_zip_it(vec_a, list_b); it.has_curr(); it.next())
392 {
393 auto [x, y] = it.get_curr();
394 cout << "(" << x << "," << y << ") ";
395 }
396 cout << endl;
397}
398
399// =============================================================================
400// 6. STL to Aleph Conversion
401// =============================================================================
402
404{
405 print_section("STL TO ALEPH CONVERSION");
406
407 vector<string> stl_products = {"Cafe", "Panela", "Arroz", "Frijol"};
408 vector<double> stl_prices = {25.0, 8.0, 12.0, 15.0};
409
410 cout << "STL vectors:\n";
411 cout << " products: [Cafe, Panela, Arroz, Frijol]" << endl;
412 cout << " prices: [25.0, 8.0, 12.0, 15.0]" << endl;
413
414 // Convert to DynList of tuples
415 print_subsection("uni_zip_to_dynlist()");
417
418 cout << "Converted to DynList<tuple>:\n";
419 for (auto it = aleph_result.get_it(); it.has_curr(); it.next())
420 {
421 auto [product, price] = it.get_curr();
422 cout << " " << product << ": $" << price << endl;
423 }
424
425 // Convert to std::vector
426 print_subsection("uni_zip_to_vector()");
427 DynList<int> aleph_ids = {1, 2, 3, 4};
429
430 cout << "Converted to std::vector<tuple>:\n";
431 for (const auto& [id, price] : stl_result)
432 cout << " ID " << id << ": $" << price << endl;
433}
434
435// =============================================================================
436// 7. Practical Example
437// =============================================================================
438
440{
441 print_section("PRACTICAL: Student Grade Analysis");
442
443 // Data from different sources (simulating real-world scenario)
444 // STL container from database
445 vector<string> db_students = {"Sofia", "Andres", "Valentina", "Santiago", "Isabella"};
446 // Aleph container from processing
447 DynList<double> processed_math = {4.2, 3.8, 4.5, 3.5, 4.8};
448 // Another STL container
449 list<double> spanish_grades = {4.0, 4.2, 4.3, 3.9, 4.6};
450
451 cout << "Data from mixed sources:\n";
452 cout << " DB (vector): [Sofia, Andres, Valentina, Santiago, Isabella]" << endl;
453 cout << " Processed (DynList): [4.2, 3.8, 4.5, 3.5, 4.8] (Math)" << endl;
454 cout << " Input (list): [4.0, 4.2, 4.3, 3.9, 4.6] (Spanish)" << endl;
455
456 // Calculate averages (uni_zip_map returns std::vector)
457 print_subsection("Calculate averages");
458 auto averages = uni_zip_map([](const auto& t) {
459 string name = get<0>(t);
460 double math = get<1>(t);
461 double spanish = get<2>(t);
462 double avg = (math + spanish) / 2.0;
463 return make_tuple(name, avg);
465
466 cout << "Student averages:\n";
467 for (const auto& [name, avg] : averages) // std::vector iteration
468 {
469 cout << " " << setw(10) << left << name << ": "
470 << fixed << setprecision(2) << avg << endl;
471 }
472
473 // Find honors students (avg >= 4.3)
474 print_subsection("Honors students (avg >= 4.3)");
475 auto honors = uni_zip_filter([](const auto& t) {
476 double math = get<1>(t);
477 double spanish = get<2>(t);
478 return (math + spanish) / 2.0 >= 4.3;
480
481 cout << "Honors list:\n";
482 for (const auto& t : honors) // std::vector iteration
483 {
484 auto [name, math, spanish] = t;
485 double avg = (math + spanish) / 2.0;
486 cout << " " << name << " (avg: " << fixed << setprecision(2) << avg << ")" << endl;
487 }
488
489 // Count passing students
490 print_subsection("Statistics");
491 size_t total = 0;
492 size_t passing = 0;
493 uni_zip_for_each([&](const auto& t) {
494 total++;
495 if ((get<1>(t) + get<2>(t)) / 2.0 >= 3.0)
496 passing++;
498
499 cout << "Total students: " << total << endl;
500 cout << "Passing (avg >= 3.0): " << passing << endl;
501 cout << "Pass rate: " << (100.0 * passing / total) << "%" << endl;
502}
503
504// =============================================================================
505// Main
506// =============================================================================
507
508int main(int argc, char* argv[])
509{
510 try
511 {
512 TCLAP::CmdLine cmd(
513 "Unified zip utilities example for Aleph-w.\n"
514 "Demonstrates mixing STL and Aleph containers in zip operations.",
515 ' ', "1.0"
516 );
517
518 TCLAP::ValueArg<string> sectionArg(
519 "s", "section",
520 "Run only specific section: mixed, predicates, transform, "
521 "utilities, advanced, conversion, practical, or 'all'",
522 false, "all", "section", cmd
523 );
524
525 cmd.parse(argc, argv);
526
527 string section = sectionArg.getValue();
528
529 cout << "\n";
530 cout << "============================================================\n";
531 cout << " ALEPH-W UNIFIED ZIP UTILITIES EXAMPLE\n";
532 cout << " (Mix STL and Aleph containers!)\n";
533 cout << "============================================================\n";
534
535 if (section == "all" or section == "mixed")
537
538 if (section == "all" or section == "predicates")
540
541 if (section == "all" or section == "transform")
543
544 if (section == "all" or section == "utilities")
546
547 if (section == "all" or section == "advanced")
549
550 if (section == "all" or section == "conversion")
552
553 if (section == "all" or section == "practical")
555
556 cout << "\n" << string(60, '=') << "\n";
557 cout << "Unified zip utilities demo completed!\n";
558 cout << string(60, '=') << "\n\n";
559
560 return 0;
561 }
562 catch (TCLAP::ArgException& e)
563 {
564 cerr << "Error: " << e.error() << " for argument " << e.argId() << endl;
565 return 1;
566 }
567 catch (exception& e)
568 {
569 cerr << "Error: " << e.what() << endl;
570 return 1;
571 }
572}
573
Unified zip operations for both STL and Aleph containers.
int main()
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
Singly linked list implementations with head-tail access.
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
auto uni_zip_scan_left(T init, Op &&op, const Containers &... cs)
Scan left - fold with intermediate results.
auto uni_zip_take(size_t n, const Containers &... cs)
Take first n tuples.
bool uni_zip_equal_length(const Containers &... cs)
Check if all containers have equal length.
bool uni_zip_exists(Pred &&pred, const Containers &... cs)
Check if predicate holds for any zipped tuple.
auto uni_zip_to_vector(const Containers &... cs)
Materialize zipped tuples into a vector.
bool uni_zip_none(Pred &&pred, const Containers &... cs)
Check if no tuple satisfies the predicate.
auto uni_zip_mapi(Op &&op, const Containers &... cs)
Map with index (mapi in ML).
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
auto uni_zip_min(const Containers &... cs)
Get minimum tuple.
auto uni_zip_to_dynlist(const Containers &... cs)
Materialize a zipped view into a DynList of tuples.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
void uni_zip_for_each(Op &&op, const Containers &... cs)
Apply operation to each zipped tuple.
auto uni_zip_max(const Containers &... cs)
Get maximum tuple.
auto uni_zip_it(const Containers &... cs)
Get a unified zip iterator.
bool uni_zip_all(Pred &&pred, const Containers &... cs)
Check if predicate holds for all zipped tuples.
auto uni_zip_filter(Pred &&pred, const Containers &... cs)
Filter zipped tuples by predicate.
auto uni_zip_map(Op &&op, const Containers &... cs)
Map operation over zipped tuples.
auto uni_zip_drop(size_t n, const Containers &... cs)
Skip first n tuples, return the rest.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Lazy and scalable dynamic array implementation.
void print_subsection(const string &title)
void demo_conversion()
void print_section(const string &title)
void demo_predicates()
void demo_practical()
void demo_transformations()
void demo_utilities()
void demo_advanced()
void demo_mixed_containers()