BLT/include/blt/iterator/common.h

272 lines
10 KiB
C
Raw Normal View History

2024-09-30 02:38:36 -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_ITER_COMMON
#define BLT_ITERATOR_ITER_COMMON
#include <type_traits>
#include <iterator>
2024-09-30 18:03:44 -04:00
#include <blt/meta/meta.h>
2024-09-30 02:38:36 -04:00
namespace blt::iterator
{
template<typename Derived>
2024-09-30 03:32:16 -04:00
struct base_wrapper
2024-09-30 02:38:36 -04:00
{
2024-09-30 03:32:16 -04:00
base_wrapper operator++(int)
2024-09-30 02:38:36 -04:00
{
auto tmp = *this;
++*this;
return tmp;
}
2024-09-30 03:32:16 -04:00
base_wrapper operator--(int)
2024-09-30 02:38:36 -04:00
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag> ||
2024-09-30 18:03:44 -04:00
std::is_same_v<typename Derived::iterator_category,
std::random_access_iterator_tag>,
2024-09-30 02:38:36 -04:00
"Iterator must allow random access");
auto tmp = *this;
--*this;
return tmp;
}
auto operator[](blt::ptrdiff_t n) const
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return *(*this + n);
}
2024-09-30 03:32:16 -04:00
friend base_wrapper operator+(blt::ptrdiff_t n, const base_wrapper& a)
2024-09-30 02:38:36 -04:00
{
2024-09-30 03:32:16 -04:00
return a + n;
2024-09-30 02:38:36 -04:00
}
2024-09-30 03:32:16 -04:00
friend bool operator<(const base_wrapper& a, const base_wrapper& b)
2024-09-30 02:38:36 -04:00
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return b - a > 0;
}
2024-09-30 03:32:16 -04:00
friend bool operator>(const base_wrapper& a, const base_wrapper& b)
2024-09-30 02:38:36 -04:00
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return b < a;
}
2024-09-30 03:32:16 -04:00
friend bool operator>=(const base_wrapper& a, base_wrapper& b)
2024-09-30 02:38:36 -04:00
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return !(a < b); // NOLINT
}
2024-09-30 03:32:16 -04:00
friend bool operator<=(const base_wrapper& a, const base_wrapper& b)
2024-09-30 02:38:36 -04:00
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return !(a > b); // NOLINT
}
2024-09-30 03:32:16 -04:00
friend bool operator==(const base_wrapper& a, const base_wrapper& b)
2024-09-30 02:38:36 -04:00
{
2024-09-30 18:03:44 -04:00
return static_cast<const Derived&>(a).base() == static_cast<const Derived&>(b).base();
2024-09-30 02:38:36 -04:00
}
2024-09-30 03:32:16 -04:00
friend bool operator!=(const base_wrapper& a, const base_wrapper& b)
2024-09-30 02:38:36 -04:00
{
2024-09-30 18:03:44 -04:00
return !(static_cast<const Derived&>(a).base() == static_cast<const Derived&>(b).base()); // NOLINT
2024-09-30 02:38:36 -04:00
}
};
2024-09-30 03:32:16 -04:00
2024-09-30 18:03:44 -04:00
template<typename Iter, typename Derived, bool dereference = false>
2024-09-30 03:32:16 -04:00
struct passthrough_wrapper : public base_wrapper<Derived>
{
public:
explicit passthrough_wrapper(Iter iter): iter(std::move(iter))
{}
auto base() const
{
return iter;
}
friend blt::ptrdiff_t operator-(const passthrough_wrapper& a, const passthrough_wrapper& b)
{
return a.base() - b.base();
}
protected:
2024-09-30 18:03:44 -04:00
mutable Iter iter;
};
template<typename Iter, typename Derived>
struct passthrough_wrapper<Iter, Derived, true> : public passthrough_wrapper<Iter, Derived>
{
using passthrough_wrapper<Iter, Derived>::passthrough_wrapper;
meta::deref_return_t<Iter> operator*() const
{
return *this->iter;
}
2024-09-30 03:32:16 -04:00
};
2024-09-30 18:03:44 -04:00
2024-09-30 19:43:02 -04:00
namespace impl
2024-09-30 18:03:44 -04:00
{
2024-09-30 18:15:59 -04:00
template<typename Derived>
class skip_t
2024-09-30 18:03:44 -04:00
{
private:
2024-09-30 18:15:59 -04:00
template<bool check>
auto skip_base(blt::size_t n)
2024-09-30 18:03:44 -04:00
{
2024-09-30 18:15:59 -04:00
auto* d = static_cast<Derived*>(this);
auto begin = d->begin();
auto end = d->end();
if constexpr (std::is_same_v<typename Derived::iterator_category, std::forward_iterator_tag> ||
std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag>)
2024-09-30 18:03:44 -04:00
{
2024-09-30 18:15:59 -04:00
for (blt::size_t i = 0; i < n; i++)
2024-09-30 18:03:44 -04:00
{
2024-09-30 18:15:59 -04:00
if constexpr (check)
{
if (begin == end)
break;
}
++begin;
2024-09-30 18:03:44 -04:00
}
2024-09-30 18:15:59 -04:00
return Derived{std::move(begin), std::move(end)};
} else if constexpr (std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>)
2024-09-30 18:03:44 -04:00
{
2024-09-30 18:15:59 -04:00
// random access iterators can have math directly applied to them.
if constexpr (check)
{
return Derived{begin + std::min(static_cast<blt::ptrdiff_t>(n), std::distance(begin, end)), end};
} else
2024-09-30 18:03:44 -04:00
{
2024-09-30 18:15:59 -04:00
return Derived{begin + n, end};
2024-09-30 18:03:44 -04:00
}
}
}
2024-09-30 18:15:59 -04:00
public:
auto skip(blt::size_t n)
{ return skip_base<false>(n); }
2024-09-30 18:03:44 -04:00
2024-09-30 18:15:59 -04:00
auto skip_or(blt::size_t n)
{ return skip_base<true>(n); }
2024-09-30 18:03:44 -04:00
};
template<typename Derived>
class take_t
{
private:
template<bool check>
auto take_base(blt::size_t n)
{
static_assert(!std::is_same_v<typename Derived::iterator_category, std::input_iterator_tag>,
"Cannot .take() on an input iterator!");
auto* d = static_cast<Derived*>(this);
auto begin = d->begin();
auto end = d->end();
// take variant for forward and bidirectional iterators
if constexpr (std::is_same_v<typename Derived::iterator_category, std::forward_iterator_tag> ||
std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag>)
{
// with these guys we have to loop forward to move the iterators. an unfortunate inefficiency
auto new_end = begin;
for (blt::size_t i = 0; i < n; i++)
{
if constexpr (check)
{
if (new_end == end)
break;
}
++new_end;
}
return Derived{std::move(begin), std::move(new_end)};
} else if constexpr (std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>)
{
// random access iterators can have math directly applied to them.
if constexpr (check)
{
return Derived{begin, begin + std::min(static_cast<blt::ptrdiff_t>(n), std::distance(begin, end))};
} else
{
return Derived{begin, begin + n};
}
}
}
public:
auto take(blt::size_t n)
{ return take_base<false>(n); }
auto take_or(blt::size_t n)
{ return take_base<true>(n); }
};
}
2024-09-30 19:43:02 -04:00
template<typename IterBase>
class iterator_container : public impl::take_t<iterator_container<IterBase>>,
public impl::skip_t<iterator_container<IterBase>>
2024-09-30 18:03:44 -04:00
{
public:
using iterator_category = typename IterBase::iterator_category;
2024-09-30 19:43:02 -04:00
using iterator = IterBase;
2024-09-30 18:03:44 -04:00
iterator_container(IterBase begin, IterBase end): m_begin(std::move(begin)), m_end(std::move(end))
{}
template<typename Iter>
iterator_container(Iter&& begin, Iter&& end): m_begin(std::forward<Iter>(begin)), m_end(std::forward<Iter>(end))
{}
auto rev()
{
static_assert((std::is_same_v<typename IterBase::iterator_category, std::bidirectional_iterator_tag> ||
std::is_same_v<typename IterBase::iterator_category, std::random_access_iterator_tag>),
".rev() must be used with bidirectional (or better) iterators!");
2024-09-30 19:43:02 -04:00
return iterator_container<std::reverse_iterator<IterBase>>{std::reverse_iterator<IterBase>{end()},
std::reverse_iterator<IterBase>{begin()}};
2024-09-30 18:03:44 -04:00
}
auto begin() const
{
return m_begin;
}
auto end() const
{
return m_end;
}
protected:
IterBase m_begin;
IterBase m_end;
};
2024-09-30 02:38:36 -04:00
}
#endif //BLT_ITERATOR_ITER_COMMON