Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Aug 7, 2023
1 parent a058cd4 commit 743e05a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 200 deletions.
18 changes: 10 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
cmake_minimum_required(VERSION 3.12)
project(ZipIterator)
project(ZipViews)

add_executable(test_zip_two test_zip_two.cpp)
target_compile_features(test_zip_two PRIVATE cxx_std_17)
option(ZIP_VIEW_BUILD_TEST "Build test" ON)

add_test(test_zip_two test_zip_two)
add_library(zip-view INTERFACE)
target_include_directories(zip-view INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(zip-view INTERFACE cxx_std_20)

if (ZIP_VIEW_BUILD_TEST)
add_executable(test-zip-view test_zip_view.cpp)
target_link_libraries(test-zip-view zip-view)

add_executable(test_zip_tuple test_zip_tuple.cpp)
target_compile_features(test_zip_tuple PRIVATE cxx_std_20)

add_test(test_zip_tuple test_zip_tuple)
add_test(test-zip-view test-zip-view)
endif()
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,72 @@
# C++20 Zip Views

This is a fork from the [ZipIterator](https://github.com/CommitThis/zip-iterator) repository, modified in order to:

- build with c++20
- implement zip and make it available on std::ranges and std::views.

List of changes and old readme can be found below.

## Disclaimer

If you have a [c++23 compiler](https://en.cppreference.com/w/cpp/compiler_support/23) that implements [std::views::zip](https://en.cppreference.com/w/cpp/ranges/zip_view), you should not use this library.

If you don't have a c++23 compiler, but you need an implementation of [std::views::zip](https://en.cppreference.com/w/cpp/ranges/zip_view) that works in your c++20 compiler, this is what you're looking for!

## Usage

### Include

Just set the root of this repo as an include directory and you're good to go, or, using CMake:

```cmake
add_subdirectory(zip-views)
target_link_libraries(your_target PRIVATE zip-view)
```

Then, in your code:

```cpp
#include <zip_view.hpp>
```

If you want to inject the zip view into the std namespace to be compatible with c++23 zip view, you can do so by defining the macro `ZIP_VIEW_INJECT_STD_VIEWS_NAMESPACE` before including the header.

```cpp
#define ZIP_VIEW_INJECT_STD_VIEWS_NAMESPACE
#include <zip_view.hpp>
```

### Example

Given two views `a` and `b`, and a container `c`, you can zip `a` and `b` into `c` like this:

```cpp
#include <zip_view.hpp>

for (auto && [x, y] : c9::zip(a, b)) {
c.push_back(x + z);
}
```

# List of Changes

[ZipIterator](https://github.com/CommitThis/zip-iterator) was coded in order to work with containers that defined `iterator` or `const_iterator` type that was a random access iterator.

I modified the old `zip_tuple.hpp` in order to work with any view, using the functions provided by the [c++20 ranges library](https://en.cppreference.com/w/cpp/ranges).

I also:
- added the possibility to inject the zip view into the std namespace to be compatible with c++23 zip view;
- added the possibility to include the repo as a subdirectory in a cmake project, and added an option to not build the tests;
- removed the `zip_two.hpp` file and its test;

Detailed edits can be found in the commit history of the repo.

# === OLD README ===

C++: Zip Iteration
==================
This document is licensed CC-BY-NC-SA 3.0, everything else is licensed under
Expand Down
25 changes: 0 additions & 25 deletions test_zip_two.cpp

This file was deleted.

12 changes: 2 additions & 10 deletions test_zip_tuple.cpp → test_zip_view.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@

#define ZIP_ITERATOR_DO_HACK_INTO_STD_VIEWS
#include "zip_tuple.hpp"
#define ZIP_VIEW_INJECT_STD_VIEWS_NAMESPACE
#include "zip_view.hpp"

#include <cassert>
#include <iostream>

#include <iostream>
#include <memory>
#include <type_traits>

#include <cxxabi.h>


auto main() -> int
{
Expand Down
153 changes: 0 additions & 153 deletions zip_two.hpp

This file was deleted.

8 changes: 4 additions & 4 deletions zip_tuple.hpp → zip_view.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ZIP_ITERATOR_ZIP_TUPLE_HPP
#define ZIP_ITERATOR_ZIP_TUPLE_HPP
#ifndef ZIP_VIEW_HPP
#define ZIP_VIEW_HPP

#include <cassert>
#include <functional>
Expand Down Expand Up @@ -117,7 +117,7 @@ class zipper
} // namespace c9::detail


#ifdef ZIP_ITERATOR_DO_HACK_INTO_STD_VIEWS
#ifdef ZIP_VIEW_INJECT_STD_VIEWS_NAMESPACE
namespace std::ranges::views {
#else
namespace c9 {
Expand All @@ -131,4 +131,4 @@ auto zip(T && ... t)

}

#endif // ZIP_ITERATOR_ZIP_TUPLE_HPP
#endif // ZIP_VIEW_HPP

0 comments on commit 743e05a

Please sign in to comment.