Skip to content

Instantly share code, notes, and snippets.

@bkietz
Last active May 8, 2023 13:08
Show Gist options
  • Save bkietz/a21c28a4e0997986d02ba5253f18f592 to your computer and use it in GitHub Desktop.
Save bkietz/a21c28a4e0997986d02ba5253f18f592 to your computer and use it in GitHub Desktop.
Tiny C++17 zip ranges utility
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
#include <tuple>
#include <cstddef>
/// \brief A helper for iterating multiple ranges simultaneously, modelled after python's
/// built-in zip() function.
///
/// \code {.cpp}
/// const std::vector<SomeTable>& tables = ...
/// for (auto&& [table, name] : Zip(tables, GetNames())) {
/// static_assert(std::is_same_v<decltype(table), const SomeTable&>);
/// static_assert(std::is_same_v<decltype(name), std::string&>);
/// // temporaries (like a vector of strings) are kept alive for the
/// // duration of a loop and are safely movable).
/// RegisterTableWithName(std::move(name), &table);
/// }
/// \endcode
template <typename Ranges, typename Indices>
struct Zip;
template <typename... Ranges>
Zip(Ranges&&...) -> Zip<std::tuple<Ranges...>, std::index_sequence_for<Ranges...>>;
template <typename... Ranges, size_t... I>
struct Zip<std::tuple<Ranges...>, std::index_sequence<I...>> {
explicit Zip(Ranges... ranges) : ranges_(std::forward<Ranges>(ranges)...) {}
std::tuple<Ranges...> ranges_;
using sentinel = std::tuple<decltype(std::get<I>(ranges_).end())...>;
struct iterator : std::tuple<decltype(std::get<I>(ranges_).begin())...> {
using std::tuple<decltype(std::get<I>(ranges_).begin())...>::tuple;
auto operator*() {
return std::tuple<decltype(*std::get<I>(*this))...>{*std::get<I>(*this)...};
}
iterator& operator++() {
(++std::get<I>(*this), ...);
return *this;
}
bool operator!=(const sentinel& s) const {
bool any_iterator_at_end = (... || (std::get<I>(*this) == std::get<I>(s)));
return not any_iterator_at_end;
}
};
iterator begin() { return {std::get<I>(ranges_).begin()...}; }
sentinel end() { return {std::get<I>(ranges_).end()...}; }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment