#pragma once /* * Copyright (C) 2024 Brett Terpstra * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef BLT_ITERATOR_ZIP #define BLT_ITERATOR_ZIP #include #include namespace blt { namespace iterator { template class zip_wrapper; template class zip_iterator_storage; template class zip_iterator_storage_rev; template class zip_forward_iterator { public: explicit zip_forward_iterator(Iter... iter): iter(std::make_tuple(iter...)) {} std::tuple...> operator*() const { return std::apply([](auto& ... i) { return std::make_tuple(*i...); }, iter); } friend bool operator==(const zip_forward_iterator& a, const zip_forward_iterator& b) { return a.iter == b.iter; } friend bool operator!=(const zip_forward_iterator& a, const zip_forward_iterator& b) { return !(a.iter == b.iter); } zip_forward_iterator& operator++() { std::apply([](auto& ... i) { ((++i), ...); }, iter); return *this; } zip_forward_iterator operator++(int) { auto tmp = *this; ++*this; return tmp; } auto base() { return iter; } protected: std::tuple iter; }; template class zip_bidirectional_iterator : public zip_forward_iterator { public: using zip_forward_iterator::zip_forward_iterator; zip_bidirectional_iterator& operator--() { std::apply([](auto& ... i) { ((--i), ...); }, this->iter); return *this; } zip_bidirectional_iterator operator--(int) { auto tmp = *this; --*this; return tmp; } }; template class zip_random_access_iterator : public zip_bidirectional_iterator { private: template static blt::ptrdiff_t sub(const zip_random_access_iterator& a, const zip_random_access_iterator& b, std::integer_sequence) { auto min = std::min(std::get(a.iter) - std::get(b.iter)...); return min; } public: using zip_bidirectional_iterator::zip_bidirectional_iterator; zip_random_access_iterator& operator+=(blt::ptrdiff_t n) { std::apply([n](auto& ... i) { ((i += n), ...); }, this->iter); return *this; } zip_random_access_iterator& operator-=(blt::ptrdiff_t n) { std::apply([n](auto& ... i) { ((i -= n), ...); }, this->iter); return *this; } friend zip_random_access_iterator operator+(const zip_random_access_iterator& a, blt::ptrdiff_t n) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i + n)...}; }, a.iter); } friend zip_random_access_iterator operator+(blt::ptrdiff_t n, const zip_random_access_iterator& a) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i + n)...}; }, a.iter); } friend zip_random_access_iterator operator-(const zip_random_access_iterator& a, blt::ptrdiff_t n) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i - n)...}; }, a.iter); } friend zip_random_access_iterator operator-(blt::ptrdiff_t n, const zip_random_access_iterator& a) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i - n)...}; }, a.iter); } friend blt::ptrdiff_t operator-(const zip_random_access_iterator& a, const zip_random_access_iterator& b) { return sub(a, b, std::index_sequence_for()); } auto operator[](blt::ptrdiff_t n) const { return *(*this + n); } friend bool operator<(const zip_random_access_iterator& a, const zip_random_access_iterator& b) { return b - a > 0; } friend bool operator>(const zip_random_access_iterator& a, const zip_random_access_iterator& b) { return b < a; } friend bool operator>=(const zip_random_access_iterator& a, const zip_random_access_iterator& b) { return !(a < b); // NOLINT } friend bool operator<=(const zip_random_access_iterator& a, const zip_random_access_iterator& b) { return !(a > b); // NOLINT } }; template class zip_wrapper { public: using iterator_category = std::forward_iterator_tag; using value_type = std::tuple...>; using difference_type = blt::ptrdiff_t; using pointer = value_type; using reference = value_type; explicit zip_wrapper(Iter... iter): iter(std::make_tuple(iter...)) {} std::tuple...> operator*() const { return std::apply([](auto& ... i) { return std::make_tuple(*i...); }, iter); } friend bool operator==(const zip_wrapper& a, const zip_wrapper& b) { return a.iter == b.iter; } friend bool operator!=(const zip_wrapper& a, const zip_wrapper& b) { return !(a.iter == b.iter); } zip_wrapper& operator++() { std::apply([](auto& ... i) { ((++i), ...); }, iter); return *this; } zip_wrapper operator++(int) { auto tmp = *this; ++*this; return tmp; } auto base() { return iter; } protected: std::tuple iter; }; template class zip_wrapper : public zip_wrapper { public: using zip_wrapper::zip_wrapper; using iterator_category = std::bidirectional_iterator_tag; using value_type = std::tuple...>; using difference_type = blt::ptrdiff_t; using pointer = value_type; using reference = value_type; zip_wrapper& operator--() { std::apply([](auto& ... i) { ((--i), ...); }, this->iter); return *this; } zip_wrapper operator--(int) { auto tmp = *this; --*this; return tmp; } }; template class zip_wrapper : public zip_wrapper { private: template static blt::ptrdiff_t sub(const zip_wrapper& a, const zip_wrapper& b, std::integer_sequence) { auto min = std::min(std::get(a.iter) - std::get(b.iter)...); return min; } public: using zip_wrapper::zip_wrapper; using iterator_category = std::random_access_iterator_tag; using value_type = std::tuple...>; using difference_type = blt::ptrdiff_t; using pointer = value_type; using reference = value_type; using zip_bidirectional_iterator::zip_bidirectional_iterator; zip_wrapper& operator+=(blt::ptrdiff_t n) { std::apply([n](auto& ... i) { ((i += n), ...); }, this->iter); return *this; } zip_wrapper& operator-=(blt::ptrdiff_t n) { std::apply([n](auto& ... i) { ((i -= n), ...); }, this->iter); return *this; } friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i + n)...}; }, a.iter); } friend zip_wrapper operator+(blt::ptrdiff_t n, const zip_wrapper& a) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i + n)...}; }, a.iter); } friend zip_wrapper operator-(const zip_wrapper& a, blt::ptrdiff_t n) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i - n)...}; }, a.iter); } friend zip_wrapper operator-(blt::ptrdiff_t n, const zip_wrapper& a) { return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i - n)...}; }, a.iter); } friend blt::ptrdiff_t operator-(const zip_wrapper& a, const zip_wrapper& b) { return sub(a, b, std::index_sequence_for()); } auto operator[](blt::ptrdiff_t n) const { return *(*this + n); } friend bool operator<(const zip_wrapper& a, const zip_wrapper& b) { return b - a > 0; } friend bool operator>(const zip_wrapper& a, const zip_wrapper& b) { return b < a; } friend bool operator>=(const zip_wrapper& a, const zip_wrapper& b) { return !(a < b); // NOLINT } friend bool operator<=(const zip_wrapper& a, const zip_wrapper& b) { return !(a > b); // NOLINT } }; template class zip_iterator_storage { }; template class zip_iterator_storage { }; template class zip_iterator_storage { }; template class zip_iterator_storage_rev { }; template class zip_iterator_storage_rev { }; template class zip_iterator_storage_rev { }; } } #endif //BLT_ITERATOR_ZIP