BLT/include/blt/iterator/zip.h

177 lines
6.5 KiB
C
Raw Normal View History

2024-09-28 16:50:19 -04:00
#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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_ITERATOR_ZIP
#define BLT_ITERATOR_ZIP
2024-09-30 02:38:36 -04:00
#include <blt/iterator/common.h>
2024-09-29 18:26:34 -04:00
#include <blt/meta/meta.h>
#include <blt/meta/iterator.h>
2024-09-28 16:50:19 -04:00
#include <tuple>
namespace blt
{
2024-09-28 18:31:49 -04:00
namespace iterator
{
template<typename... Iter>
2024-09-30 03:32:16 -04:00
struct zip_wrapper : public base_wrapper<zip_wrapper<Iter...>>
2024-09-28 18:31:49 -04:00
{
public:
2024-09-30 02:38:36 -04:00
using iterator_category = meta::lowest_iterator_category_t<Iter...>;
2024-09-29 18:26:34 -04:00
using value_type = std::tuple<meta::deref_return_t<Iter>...>;
2024-09-28 18:31:49 -04:00
using difference_type = blt::ptrdiff_t;
using pointer = value_type;
using reference = value_type;
2024-09-30 18:03:44 -04:00
explicit zip_wrapper(std::tuple<Iter...> iter): iter(std::move(iter))
{}
2024-09-28 18:31:49 -04:00
explicit zip_wrapper(Iter... iter): iter(std::make_tuple(iter...))
{}
2024-09-29 18:26:34 -04:00
std::tuple<meta::deref_return_t<Iter>...> operator*() const
2024-09-28 18:31:49 -04:00
{
2024-09-29 18:26:34 -04:00
return std::apply([](auto& ... i) { return std::tuple<meta::deref_return_t<Iter>...>{*i...}; }, iter);
2024-09-28 18:31:49 -04:00
}
zip_wrapper& operator++()
{
std::apply([](auto& ... i) { ((++i), ...); }, iter);
return *this;
}
zip_wrapper& operator--()
{
std::apply([](auto& ... i) { ((--i), ...); }, this->iter);
return *this;
}
2024-09-30 03:32:16 -04:00
friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n)
2024-09-28 18:31:49 -04:00
{
2024-09-30 03:32:16 -04:00
static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return std::apply([n](auto& ... i) { return zip_wrapper((i + n)...); }, a.iter);
2024-09-28 18:31:49 -04:00
}
2024-09-30 03:32:16 -04:00
friend zip_wrapper operator-(const zip_wrapper& a, blt::ptrdiff_t n)
2024-09-28 18:31:49 -04:00
{
2024-09-30 03:32:16 -04:00
static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return std::apply([n](auto& ... i) { return zip_wrapper((i - n)...); }, a.iter);
2024-09-28 18:31:49 -04:00
}
friend blt::ptrdiff_t operator-(const zip_wrapper& a, const zip_wrapper& b)
{
return sub(a, b, std::index_sequence_for<Iter...>());
}
2024-09-30 18:03:44 -04:00
auto base() const
2024-09-28 18:31:49 -04:00
{
2024-09-30 02:38:36 -04:00
return iter;
2024-09-28 18:31:49 -04:00
}
2024-09-30 02:38:36 -04:00
protected:
std::tuple<Iter...> iter;
2024-09-28 18:31:49 -04:00
2024-09-30 02:38:36 -04:00
template<typename T, T... n>
static blt::ptrdiff_t sub(const zip_wrapper& a, const zip_wrapper& b,
std::integer_sequence<T, n...>)
2024-09-28 18:31:49 -04:00
{
2024-09-30 02:38:36 -04:00
blt::ptrdiff_t min = std::numeric_limits<blt::ptrdiff_t>::max();
((min = std::min(min, std::get<n>(a.iter) - std::get<n>(b.iter))), ...);
return min;
2024-09-28 18:31:49 -04:00
}
};
2024-09-30 19:43:02 -04:00
// template<typename Iter>
// struct zip_wrapper<Iter> : public Iter
// {
// using iterator_category = typename std::iterator_traits<Iter>::iterator_category;
// using value_type = typename std::iterator_traits<Iter>::value_type;
// using difference_type = typename std::iterator_traits<Iter>::difference_type;
// using pointer = typename std::iterator_traits<Iter>::pointer;
// using reference = typename std::iterator_traits<Iter>::reference;
//
// using Iter::Iter;
// };
2024-09-29 18:26:34 -04:00
}
template<typename Iter>
struct iterator_pair
{
using type = Iter;
2024-09-28 18:31:49 -04:00
2024-09-29 18:26:34 -04:00
iterator_pair(Iter begin, Iter end): begin(std::move(begin)), end(std::move(end))
{}
2024-09-28 18:31:49 -04:00
2024-09-29 18:26:34 -04:00
Iter begin;
Iter end;
};
template<typename... Iter>
2024-09-30 19:43:02 -04:00
class zip_iterator_container : public iterator::iterator_container<iterator::zip_wrapper<Iter...>>
2024-09-29 18:26:34 -04:00
{
public:
2024-09-30 18:03:44 -04:00
using iterator::iterator_container<iterator::zip_wrapper<Iter...>>::iterator_container;
2024-09-29 18:26:34 -04:00
2024-09-30 19:43:02 -04:00
explicit zip_iterator_container(iterator_pair<Iter>... iterator_pairs):
2024-09-30 18:03:44 -04:00
iterator::iterator_container<iterator::zip_wrapper<Iter...>>(iterator::zip_wrapper<Iter...>{std::move(iterator_pairs.begin)...},
iterator::zip_wrapper<Iter...>{std::move(iterator_pairs.end)...})
2024-09-29 18:26:34 -04:00
{}
2024-09-30 19:43:02 -04:00
2024-09-29 18:26:34 -04:00
};
2024-09-30 19:43:02 -04:00
namespace impl
2024-09-30 18:57:15 -04:00
{
2024-09-30 19:43:02 -04:00
template<typename Derived>
class zip_t
{
public:
template<typename... Iter>
auto zip(iterator_pair<Iter>... iterator_pairs)
{
zip_iterator_container(iterator::zip_wrapper<Iter...>{std::move(iterator_pairs.begin)...},
iterator::zip_wrapper<Iter...>{std::move(iterator_pairs.end)...});
}
};
}
2024-09-30 18:57:15 -04:00
2024-09-29 18:26:34 -04:00
/*
* CTAD for the zip containers
*/
template<typename... Iter>
2024-09-30 19:43:02 -04:00
zip_iterator_container(iterator_pair<Iter>...) -> zip_iterator_container<Iter...>;
2024-09-29 18:26:34 -04:00
template<typename... Iter>
2024-09-30 19:43:02 -04:00
zip_iterator_container(std::initializer_list<Iter>...) -> zip_iterator_container<Iter...>;
2024-09-28 18:31:49 -04:00
2024-09-29 18:26:34 -04:00
/*
* Helper functions for creating zip containers
*/
template<typename... Container>
auto zip(Container& ... container)
{
2024-09-30 19:43:02 -04:00
return zip_iterator_container{iterator_pair{container.begin(), container.end()}...};
2024-09-29 18:26:34 -04:00
}
2024-09-28 16:50:19 -04:00
}
#endif //BLT_ITERATOR_ZIP