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

Comprehensive example of zip operations in Aleph-w. More...

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <tclap/CmdLine.h>
#include <htlist.H>
#include <tpl_dynDlist.H>
#include <ah-zip.H>
#include <ahFunctional.H>
Include dependency graph for zip_example.C:

Go to the source code of this file.

Functions

void print_section (const string &title)
 
void print_subsection (const string &title)
 
template<typename Container >
void print_list (const string &label, const Container &c)
 
void demo_basic_zip ()
 
void demo_enum_zip ()
 
void demo_tuple_operations ()
 
void demo_zip_transformation ()
 
void demo_zip_utilities ()
 
void demo_stl_compatibility ()
 
void demo_length_checking ()
 
void demo_practical_example ()
 
int main (int argc, char *argv[])
 

Detailed Description

Comprehensive example of zip operations in Aleph-w.

This program demonstrates zip operations from ah-zip.H, which provide powerful tools for working with multiple containers simultaneously. Zip operations are inspired by Python's zip() function and are essential for parallel processing of related data.

What is Zipping?

Zipping combines multiple containers element-wise, creating tuples:

Container 1: [a, b, c]
Container 2: [1, 2, 3]
Zipped: [(a,1), (b,2), (c,3)]

Key insight: Process related data from multiple sources together, maintaining correspondence between elements.

Features Demonstrated

Basic Zip Iteration

Iterate multiple containers simultaneously:

Use case: Process related data from multiple sources together

Tuple Creation

Create and manipulate tuples:

  • **t_zip()**: Create list of tuples from containers
  • **t_unzip()**: Split tuple list back into separate containers
  • **t_enum_zip()**: Create tuples with index: (i, elem₁, elem₂)

Zip Functional Operations

Apply functional operations to zipped data:

  • **zip_map()**: Transform tuples (apply function to each tuple)
  • **zip_filter()**: Keep tuples satisfying predicate
  • **zip_take(n)**: Take first n tuples
  • **zip_drop(n)**: Skip first n tuples
  • **zip_partition(pred)**: Split into two groups by predicate

STL Compatibility

  • **std_zip()**: Zip STL containers (std::vector, std::list, etc.)
  • Works seamlessly with standard library containers

Common Use Cases

Parallel Processing

// Process names and ages together
auto zipped = zip(names, ages);
zip_for_each(zipped, [](auto name, auto age) {
cout << name << " is " << age << " years old\n";
});
void zip_for_each(Op &&op, const Cs &... cs)
Apply op to every zipped tuple.
Definition ah-zip.H:392
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.

Data Transformation

// Combine and transform data
auto result = zip_map(prices, quantities,
[](auto price, auto qty) { return price * qty; });
auto zip_map(Op &&op, const Cs &... cs)
Map op over zipped tuples with auto-deduced return type.
Definition ah-zip.H:833

Filtering Related Data

// Keep only valid pairs
auto valid = zip_filter(names, scores,
[](auto name, auto score) { return score >= 60; });
auto zip_filter(Op op, const Cs &... cs)
Filter zipped tuples by predicate op.
Definition ah-zip.H:895

Indexed Operations

// Process with index
enum_zip_for_each(data, [](size_t i, auto elem) {
cout << "Element " << i << ": " << elem << "\n";
});

Advantages

Type safety: Compile-time checking of container compatibility ✅ Efficiency: Single pass through containers ✅ Readability: Clear intent (process related data together) ✅ Composability: Works with other functional operations

Comparison with Manual Loops

Manual approach:

for (size_t i = 0; i < min(names.size(), ages.size()); i++) {
process(names[i], ages[i]);
}
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4111

Zip approach:

zip_for_each(names, ages, process);

More concise, safer (no index errors), clearer intent!

Usage Examples

# Run all zip demonstrations
./zip_example
# Run specific section
./zip_example -s basic # Basic zip operations
./zip_example -s enum # Zip with indices / enumeration
./zip_example -s tuples # Tuple creation and manipulation
./zip_example -s transform # Transform zipped data
./zip_example -s utilities # Utility operations
./zip_example -s stl # STL container compatibility
./zip_example -s length # Length checking
./zip_example -s practical # Practical example
# Show help
./zip_example --help
See also
ah-zip.H Main zip operations header (Aleph containers only)
zip_utils_example.C Unified zip (works with STL and Aleph)
functional_example.C General functional programming
Author
Leandro Rabindranath León
Date
2024

Definition in file zip_example.C.

Function Documentation

◆ demo_basic_zip()

void demo_basic_zip ( )

Definition at line 187 of file zip_example.C.

References Aleph::maps(), print_list(), print_section(), and print_subsection().

Referenced by main().

◆ demo_enum_zip()

void demo_enum_zip ( )

◆ demo_length_checking()

void demo_length_checking ( )

◆ demo_practical_example()

◆ demo_stl_compatibility()

void demo_stl_compatibility ( )

Definition at line 431 of file zip_example.C.

References Aleph::maps(), print_section(), print_subsection(), Aleph::std_zip(), and Aleph::tzip_std().

Referenced by main().

◆ demo_tuple_operations()

void demo_tuple_operations ( )

◆ demo_zip_transformation()

void demo_zip_transformation ( )

◆ demo_zip_utilities()

◆ main()

◆ print_list()

template<typename Container >
void print_list ( const string &  label,
const Container c 
)

◆ print_section()

◆ print_subsection()