Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/serengti/cpp_iterzip

Header-only C++17 implementation for a python-style zip iterator.
https://github.com/serengti/cpp_iterzip

cpp17 iterator

Last synced: 5 days ago
JSON representation

Header-only C++17 implementation for a python-style zip iterator.

Awesome Lists containing this project

README

        

# iterzip for C++17

Header-only implementation for a python-style `zip` iterator.
Simplifies iteration over multiple containers at once.
Supports all standard iterator categories and conforms to `std::iterator_traits`.

## Usage

Supports all iterator-conforming containers.
This, of course, includes all STL containers such as `std::array`, `std::vector`, `std::list` etc.
You can `zip` arbitrary many containers and iterate them all at once!

```c++
#include "iterzip.hpp"
using iterzip::zip;

std::array a{1,2,3,4,5};
std::array b{6,7,8,9,10};

for(auto [x,y] : zip(a,b)) {
std::cout << x << ", " << y << std::endl;
}
```
Will produce the following output
```
1, 6
2, 7
3, 8
4, 9
5, 10
```

### Containers of different size
For containers with different sizes, the smallest container determines the length of of the `zip` iteration.

```c++
std::array d{1,2,3,4};
std::array e{5,6};

for(auto [x,y] : zip(d,e)) {
std::cout << x << ", " << y << std::endl;
}
```
Will produce the following output
```
1, 5
2, 6
```

Specifically, two `zip` iterators are equal (`operator!=` is false) as soon as at least one of the individual iterators are equal.

### Higher iterator acategories

If the containers support it, it is possible to use higher iterator categories' functions.
For example, the `iterator::operator[]` on two `std::array`s:
```c++
std::array d{1,2};
std::array e{3,4};

auto zipped = zip(d, e);
// only works with random access iterators
std::cout << std::get<0>(zipped.begin()[1]) << std::endl; // outputs 2 (which is the second element in the first container.)
```

## Iterator traits for zip iterators

The `zip` iterator category depends on the given input containers' iterators.
To achieve that, the `iterzip` namespace provides a natural extension of `std::iterator_traits`, called `iterzip::iterator_traits`.
The chosen iterator category is always the lowest category of all input iterator types.

```c++
using T1 = std::list::iterator;
using T2 = std::array::iterator;
using cat = typename iterzip::iterator_traits::iterator_category;
// cat == std::bidirectional_iterator_tag;
```

The zipped `value_type` is a `std::tuple` where the `X` are the individual iterators' `value_type`.
The `reference` typedef is `std::tuple` and `pointer` is mapped to `reference*`.

## Compiling / Building / Including

In your project, simply include the header file `iterzip.hpp` and make sure you have support for C++17.

To build the example file provided in the repository, run
```bash
$ gcc main.cpp -o main -lstdc++ -std=c++17
$ ./main
```

The implementation of iterzip uses fold expressions, which are only available after C++17.