BLT/include/blt/iterator/common.h

126 lines
4.1 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>
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> ||
std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"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
{
return a.base() == b.base();
}
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
{
return !(a.base() == b.base()); // NOLINT
}
};
2024-09-30 03:32:16 -04:00
template<typename Iter, typename Derived>
struct passthrough_wrapper : public base_wrapper<Derived>
{
public:
explicit passthrough_wrapper(Iter iter): iter(std::move(iter))
{}
meta::deref_return_t<Iter> operator*() const
{
return *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:
Iter iter;
};
2024-09-30 02:38:36 -04:00
}
#endif //BLT_ITERATOR_ITER_COMMON