Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ cmake_minimum_required(VERSION 3.12)
project(cpp_zip)
set(PROJECT_VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CXX_EXTENSIONS NO)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
include(warnings)

include_directories(include)

add_library(msd_zip INTERFACE)
target_compile_features(msd_zip INTERFACE cxx_std_17)
target_include_directories(msd_zip INTERFACE include)

option(ENABLE_TESTS "Build tests" OFF)
Expand All @@ -23,3 +18,5 @@ if (ENABLE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()

install(DIRECTORY include/ DESTINATION include)
33 changes: 25 additions & 8 deletions include/msd/zip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class zip_iterator {
* @param offset The number of positions to advance.
* @return A new `zip_iterator` advanced by the specified offset.
*/
zip_iterator operator+(const std::size_t offset) const
zip_iterator operator+(const difference_type offset) const
{
auto iterator = *this;
iterator.advance(std::index_sequence_for<Iterators...>{}, offset);
Expand Down Expand Up @@ -130,7 +130,7 @@ class zip_iterator {
* @param offset The number of positions to move back.
* @return A new `zip_iterator` moved back by the specified offset.
*/
zip_iterator operator-(const int offset) const
zip_iterator operator-(const difference_type offset) const
{
auto iterator = *this;
iterator.advance(std::index_sequence_for<Iterators...>{}, -offset);
Expand Down Expand Up @@ -187,7 +187,7 @@ class zip_iterator {
* @param offset The number of positions to advance.
*/
template <std::size_t... I>
void advance(std::index_sequence<I...>, const int offset)
void advance(std::index_sequence<I...>, const difference_type offset)
{
((std::advance(std::get<I>(iterators_), offset)), ...);
}
Expand Down Expand Up @@ -326,7 +326,7 @@ class zip {
value_type back()
{
assert(!empty()); // LCOV_EXCL_LINE
return *std::prev(begin() + size());
return *std::prev(begin() + static_cast<typename iterator::difference_type>(size()));
}

/**
Expand All @@ -338,7 +338,7 @@ class zip {
value_type back() const
{
assert(!empty()); // LCOV_EXCL_LINE
return *std::prev(begin() + size());
return *std::prev(begin() + static_cast<typename const_iterator::difference_type>(size()));
}

/**
Expand All @@ -351,7 +351,7 @@ class zip {
value_type operator[](const std::size_t offset) const
{
assert(offset < size());
return *std::next(begin(), offset);
return *std::next(begin(), static_cast<typename const_iterator::difference_type>(offset));
}

private:
Expand Down Expand Up @@ -380,7 +380,23 @@ class zip {
template <typename Iterator, std::size_t... I>
Iterator end_impl(std::index_sequence<I...>) const
{
return std::next(Iterator{std::get<I>(containers_).begin()...}, size());
const auto zip_size = size();
return Iterator{advance_iterator(std::get<I>(containers_).begin(), zip_size)...};
}

/**
* @brief Advances an iterator by a non-negative offset.
*
* @tparam Iterator Type of the iterator to advance.
* @param iterator The iterator to advance.
* @param offset The number of positions to advance.
* @return The advanced iterator.
*/
template <typename Iterator>
static Iterator advance_iterator(Iterator iterator, const std::size_t offset)
{
std::advance(iterator, static_cast<typename std::iterator_traits<Iterator>::difference_type>(offset));
return iterator;
}

/**
Expand All @@ -393,7 +409,8 @@ class zip {
template <std::size_t... I>
std::size_t size_impl(std::index_sequence<I...>) const
{
return std::min({std::distance(std::get<I>(containers_).begin(), std::get<I>(containers_).end())...});
return static_cast<std::size_t>(
std::min({std::distance(std::get<I>(containers_).begin(), std::get<I>(containers_).end())...}));
}

/**
Expand Down
14 changes: 12 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ endif ()
# Test macro
function(package_add_test TESTNAME)
add_executable(${TESTNAME} ${ARGN})
set_target_properties(${TESTNAME} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)

set_target_warnings(${TESTNAME} PRIVATE)
target_link_libraries(${TESTNAME} gtest gtest_main gmock)
target_link_libraries(${TESTNAME} msd_zip gtest gtest_main gmock)

if (ENABLE_COVERAGE)
target_link_libraries(${TESTNAME} -lgcov -lubsan)
Expand Down Expand Up @@ -67,6 +72,11 @@ if (ENABLE_BENCHMARKS)
endif ()

add_executable(zip_benchmark zip_benchmark.cpp)
target_link_libraries(zip_benchmark benchmark)
set_target_properties(zip_benchmark PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
target_link_libraries(zip_benchmark msd_zip benchmark)
set_target_warnings(zip_benchmark PRIVATE)
endif ()
Loading