probably going to revert all of this.

v1
Brett 2024-09-30 03:32:16 -04:00
parent 0e5d8b6043
commit 5745ddba72
3 changed files with 109 additions and 149 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 1.1.1) set(BLT_VERSION 1.1.2)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -25,16 +25,16 @@
namespace blt::iterator namespace blt::iterator
{ {
template<typename Derived> template<typename Derived>
struct wrapper_base struct base_wrapper
{ {
wrapper_base operator++(int) base_wrapper operator++(int)
{ {
auto tmp = *this; auto tmp = *this;
++*this; ++*this;
return tmp; return tmp;
} }
wrapper_base operator--(int) base_wrapper operator--(int)
{ {
static_assert(std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag> || 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>, std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
@ -51,80 +51,75 @@ namespace blt::iterator
return *(*this + n); return *(*this + n);
} }
friend wrapper_base operator+(const wrapper_base& a, blt::ptrdiff_t n) friend base_wrapper operator+(blt::ptrdiff_t n, const base_wrapper& a)
{ {
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>, return a + n;
"Iterator must allow random access");
auto copy = a;
copy += n;
return copy;
} }
friend wrapper_base operator+(blt::ptrdiff_t n, const wrapper_base& a) friend bool operator<(const base_wrapper& a, const base_wrapper& b)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
auto copy = a;
copy += n;
return copy;
}
friend wrapper_base operator-(const wrapper_base& a, blt::ptrdiff_t n)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
auto copy = a;
copy -= n;
return copy;
}
friend wrapper_base operator-(blt::ptrdiff_t n, const wrapper_base& a)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
auto copy = a;
copy -= n;
return copy;
}
friend bool operator<(const wrapper_base& a, const wrapper_base& b)
{ {
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>, static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access"); "Iterator must allow random access");
return b - a > 0; return b - a > 0;
} }
friend bool operator>(const wrapper_base& a, const wrapper_base& b) friend bool operator>(const base_wrapper& a, const base_wrapper& b)
{ {
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>, static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access"); "Iterator must allow random access");
return b < a; return b < a;
} }
friend bool operator>=(const wrapper_base& a, wrapper_base& b) friend bool operator>=(const base_wrapper& a, base_wrapper& b)
{ {
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>, static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access"); "Iterator must allow random access");
return !(a < b); // NOLINT return !(a < b); // NOLINT
} }
friend bool operator<=(const wrapper_base& a, const wrapper_base& b) friend bool operator<=(const base_wrapper& a, const base_wrapper& b)
{ {
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>, static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access"); "Iterator must allow random access");
return !(a > b); // NOLINT return !(a > b); // NOLINT
} }
friend bool operator==(const wrapper_base& a, const wrapper_base& b) friend bool operator==(const base_wrapper& a, const base_wrapper& b)
{ {
return a.base() == b.base(); return a.base() == b.base();
} }
friend bool operator!=(const wrapper_base& a, const wrapper_base& b) friend bool operator!=(const base_wrapper& a, const base_wrapper& b)
{ {
return !(a.base() == b.base()); // NOLINT return !(a.base() == b.base()); // NOLINT
} }
}; };
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;
};
} }
#endif //BLT_ITERATOR_ITER_COMMON #endif //BLT_ITERATOR_ITER_COMMON

View File

@ -29,7 +29,7 @@ namespace blt
namespace iterator namespace iterator
{ {
template<typename... Iter> template<typename... Iter>
class zip_wrapper : public wrapper_base<zip_wrapper<Iter...>> struct zip_wrapper : public base_wrapper<zip_wrapper<Iter...>>
{ {
public: public:
using iterator_category = meta::lowest_iterator_category_t<Iter...>; using iterator_category = meta::lowest_iterator_category_t<Iter...>;
@ -58,16 +58,18 @@ namespace blt
return *this; return *this;
} }
zip_wrapper& operator+=(blt::ptrdiff_t n) friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n)
{ {
std::apply([n](auto& ... i) { ((i += n), ...); }, this->iter); static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
return *this; "Iterator must allow random access");
return std::apply([n](auto& ... i) { return zip_wrapper((i + n)...); }, a.iter);
} }
zip_wrapper& operator-=(blt::ptrdiff_t n) friend zip_wrapper operator-(const zip_wrapper& a, blt::ptrdiff_t n)
{ {
std::apply([n](auto& ... i) { ((i -= n), ...); }, this->iter); static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
return *this; "Iterator must allow random access");
return std::apply([n](auto& ... i) { return zip_wrapper((i - n)...); }, a.iter);
} }
friend blt::ptrdiff_t operator-(const zip_wrapper& a, const zip_wrapper& b) friend blt::ptrdiff_t operator-(const zip_wrapper& a, const zip_wrapper& b)
@ -93,112 +95,78 @@ namespace blt
} }
}; };
BLT_META_MAKE_FUNCTION_CHECK(base); template<typename Iter, bool checked>
struct skip_wrapper : public passthrough_wrapper<Iter, skip_wrapper<Iter, checked>>
template<typename Iter>
auto get_base(Iter iter)
{ {
if constexpr (has_func_base_v<Iter>)
{
return std::move(iter).base();
} else
{
return std::move(iter);
}
}
template<typename Derived>
class take_impl
{
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{get_base(std::move(begin)), get_base(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{get_base(begin),
get_base(begin + std::min(static_cast<blt::ptrdiff_t>(n), std::distance(begin, end)))};
} else
{
return Derived{get_base(begin), get_base(begin + n)};
}
}
}
public: public:
auto take(blt::size_t n) using iterator_category = typename std::iterator_traits<Iter>::iterator_category;
{ return take_base<false>(n); } 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;
auto take_or(blt::size_t n) explicit skip_wrapper(Iter iter, blt::size_t n): passthrough_wrapper<Iter, skip_wrapper<Iter, checked>>(std::move(iter)), skip(n)
{ return take_base<true>(n); } {}
};
template<typename Derived> skip_wrapper& operator++()
class skip_impl
{ {
if constexpr (std::is_same_v<iterator_category, std::random_access_iterator_tag>)
{
if (skip > 0)
{
this->base() += skip;
skip = 0;
}
} else
{
while (skip > 0)
{
++this->base();
--skip;
}
}
++this->base();
return *this;
}
skip_wrapper& operator--()
{
if constexpr (std::is_same_v<iterator_category, std::random_access_iterator_tag>)
{
if (skip > 0)
{
this->base() -= skip;
skip = 0;
}
} else
{
while (skip > 0)
{
--this->base();
--skip;
}
}
--this->base();
return *this;
}
friend skip_wrapper operator+(const skip_wrapper& a, blt::ptrdiff_t n)
{
static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return {a.base() + static_cast<blt::ptrdiff_t>(a.skip) + n, 0};
}
friend skip_wrapper operator-(const skip_wrapper& a, blt::ptrdiff_t n)
{
static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
return {a.base() - static_cast<blt::ptrdiff_t>(a.skip) - n, 0};
}
private: private:
template<bool check> blt::size_t skip;
auto skip_base(blt::size_t n)
{
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::random_access_iterator_tag>)
{
if constexpr (check)
{
return Derived{begin + std::min(static_cast<blt::ptrdiff_t>(n), std::distance(begin, end))};
} else
{
return Derived{begin + n, end};
}
} else
{
for (blt::size_t i = 0; i < n; i++)
{
if constexpr (check)
{
if (begin == end)
break;
}
++begin;
}
return Derived{begin, end};
}
}
public:
void skip(blt::size_t n)
{ return skip_base<false>(n); }
void skip_or(blt::size_t n)
{ return skip_base<true>(n); }
}; };
} }
@ -221,7 +189,7 @@ namespace blt
class zip_iterator_storage_rev; class zip_iterator_storage_rev;
template<typename... Iter> template<typename... Iter>
class zip_iterator_storage : public iterator::take_impl<zip_iterator_storage<Iter...>> class zip_iterator_storage
{ {
public: public:
using iterator_category = meta::lowest_iterator_category_t<Iter...>; using iterator_category = meta::lowest_iterator_category_t<Iter...>;
@ -243,12 +211,9 @@ namespace blt
} }
auto skip(blt::size_t n) auto skip(blt::size_t n)
{
if constexpr (std::is_same_v<iterator_category, std::input_iterator_tag>)
{ {
} }
}
auto begin() const auto begin() const
{ {
@ -266,7 +231,7 @@ namespace blt
}; };
template<typename... Iter> template<typename... Iter>
class zip_iterator_storage_rev : public iterator::take_impl<zip_iterator_storage_rev<Iter...>> class zip_iterator_storage_rev
{ {
public: public:
using iterator_category = meta::lowest_iterator_category_t<Iter...>; using iterator_category = meta::lowest_iterator_category_t<Iter...>;
@ -329,7 +294,7 @@ namespace blt
template<typename... Container> template<typename... Container>
auto zip(Container& ... container) auto zip(Container& ... container)
{ {
return blt::zip_iterator_storage{iterator_pair{container.begin(), container.end()}...}; return zip_iterator_storage{iterator_pair{container.begin(), container.end()}...};
} }
} }