rust style iterators

v1
Brett 2024-09-25 20:52:22 -04:00
parent a27651b21d
commit 329eeb174b
2 changed files with 78 additions and 61 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake)
set(BLT_VERSION 0.21.1)
set(BLT_VERSION 0.21.2)
set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT)

View File

@ -20,6 +20,7 @@
#define BLT_ITERATOR_H
#include <blt/std/types.h>
#include <blt/std/logging.h>
#include <blt/meta/meta.h>
#include <blt/meta/iterator.h>
#include <type_traits>
@ -81,40 +82,27 @@ namespace blt
};
template<typename Iter>
class enumerate_iterator_base
class enumerate_iterator_base : public pair_iterator_base<Iter, blt::size_t>
{
public:
explicit enumerate_iterator_base(Iter iter, blt::size_t place = 0): iter(std::move(iter)), index(place)
explicit enumerate_iterator_base(Iter iter, blt::size_t place = 0):
pair_iterator_base<Iter, blt::size_t>(std::move(iter), place)
{}
enumerate_item<blt::meta::deref_return_t<Iter>> operator*() const
{
return {index, *this->iter};
}
friend bool operator==(const enumerate_iterator_base& a, const enumerate_iterator_base& b)
{
return a.iter == b.iter;
}
friend bool operator!=(const enumerate_iterator_base& a, const enumerate_iterator_base& b)
{
return a.iter != b.iter;
return {this->m_iter2, *this->m_iter1};
}
auto base() const
{
return iter;
return this->iter1();
}
auto get_index() const
{
return index;
return this->iter2();
}
protected:
Iter iter;
blt::size_t index;
};
template<typename Iter>
@ -125,8 +113,8 @@ namespace blt
enumerate_forward_iterator& operator++()
{
++this->iter;
++this->index;
++this->m_iter1;
++this->m_iter2;
return *this;
}
@ -146,8 +134,8 @@ namespace blt
enumerate_bidirectional_iterator& operator--()
{
--this->iter;
--this->index;
--this->m_iter1;
--this->m_iter2;
return *this;
}
@ -220,14 +208,12 @@ namespace blt
public:
explicit enumerator_reversible(Iter begin, Iter end, blt::size_t container_size):
enumerator_base<Iter, IterWrapper>{IterWrapper{enumerate_wrapper<Iter>{std::move(begin), 0}},
IterWrapper{enumerate_wrapper<Iter>{std::move(end), container_size}}},
container_size(container_size)
IterWrapper{enumerate_wrapper<Iter>{std::move(end), container_size}}}
{}
explicit enumerator_reversible(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index, blt::size_t container_size):
explicit enumerator_reversible(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
enumerator_base<Iter, IterWrapper>(IterWrapper{enumerate_wrapper<Iter>{std::move(begin), begin_index}},
IterWrapper{enumerate_wrapper<Iter>{std::move(end), end_index}}),
container_size(container_size)
IterWrapper{enumerate_wrapper<Iter>{std::move(end), end_index}})
{}
auto rev() const
@ -235,24 +221,30 @@ namespace blt
return enumerator_rev<Iter>{this->end_.base(),
this->begin_.base(),
this->end_.get_index(),
this->begin_.get_index(),
this->container_size};
this->begin_.get_index()};
}
auto skip(blt::size_t offset)
auto skip(blt::size_t amount)
{
auto begin = this->begin();
for (blt::size_t i = 0; i < offset; i++)
auto begin = this->begin_;
for (blt::size_t i = 0; i < amount; i++)
++begin;
return enumerator<Iter>{begin.base(),
this->end_.base(),
begin.get_index(),
this->end_.get_index(),
this->container_size};
this->end_.get_index()};
}
protected:
blt::size_t container_size;
auto take(blt::size_t amount)
{
auto end = this->begin();
for (blt::size_t i = 0; i < amount; i++)
++end;
return enumerator<Iter>{this->begin_.base(),
end.base(),
this->begin_.get_index(),
end.get_index()};
}
};
template<typename Iter, typename IterWrapper>
@ -266,20 +258,30 @@ namespace blt
return enumerator<Iter>{this->end_.base().base(),
this->begin_.base().base(),
this->end_.base().get_index(),
this->begin_.base().get_index(),
this->container_size};
this->begin_.base().get_index()};
}
auto skip(blt::size_t offset)
auto skip(blt::size_t amount)
{
auto begin = this->begin();
for (blt::size_t i = 0; i < offset; i++)
auto begin = this->begin_.base();
for (blt::size_t i = 0; i < amount; i++)
--begin;
return enumerator<Iter>{begin.base(),
this->end_.base(),
return enumerator_rev<Iter>{begin.base(),
this->end_.base().base(),
begin.get_index(),
this->end_.get_index(),
this->container_size};
this->end_.base().get_index()};
}
auto take(blt::size_t amount)
{
auto end = this->begin_.base();
for (blt::size_t i = 0; i < amount; i++)
--end;
return enumerator_rev<Iter>{
this->begin_.base().base(),
end.base(),
this->begin_.base().get_index(),
end.get_index()};
}
};
}
@ -307,13 +309,20 @@ namespace blt
public:
using iterator::enumerator_reversible<Iter, iterator::enumerate_wrapper<Iter>>::enumerator_reversible;
auto skip(blt::size_t offset)
auto skip(blt::size_t amount)
{
return enumerator<Iter>{this->begin_.base() + offset,
return enumerator<Iter>{this->begin_.base() + amount,
this->end_.base(),
this->begin_.get_index() + offset,
this->end_.get_index(),
this->container_size};
this->begin_.get_index() + amount,
this->end_.get_index()};
}
auto take(blt::size_t amount)
{
return enumerator<Iter>{this->begin_.base(),
this->begin_.base() + amount,
this->begin_.get_index(),
this->begin_.get_index() + amount};
}
};
@ -332,12 +341,20 @@ namespace blt
public:
using iterator::enumerator_reversible_rev<Iter, std::reverse_iterator<iterator::enumerate_wrapper<Iter>>>::enumerator_reversible_rev;
auto skip(blt::size_t offset)
auto skip(blt::size_t amount)
{
return enumerator_rev<Iter>{this->begin_.base().base() - offset,
return enumerator_rev<Iter>{this->begin_.base().base() - amount,
this->end_.base().base(),
this->begin_.base().get_index() - offset,
this->end_.base().get_index(), this->container_size};
this->begin_.base().get_index() - amount,
this->end_.base().get_index()};
}
auto take(blt::size_t amount)
{
return enumerator_rev<Iter>{this->begin_.base().base(),
this->begin_.base().base() - amount,
this->begin_.base().get_index(),
this->begin_.base().get_index() - amount};
}
};