From 329eeb174bcb66f3403255d324c6c8451246a131 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 25 Sep 2024 20:52:22 -0400 Subject: [PATCH] rust style iterators --- CMakeLists.txt | 2 +- include/blt/std/iterator.h | 137 +++++++++++++++++++++---------------- 2 files changed, 78 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b12114..997dcec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/blt/std/iterator.h b/include/blt/std/iterator.h index 933bf38..79c1411 100644 --- a/include/blt/std/iterator.h +++ b/include/blt/std/iterator.h @@ -20,6 +20,7 @@ #define BLT_ITERATOR_H #include +#include #include #include #include @@ -74,47 +75,34 @@ namespace blt { return m_iter2; } - + protected: Iter1 m_iter1; Iter2 m_iter2; }; template - class enumerate_iterator_base + class enumerate_iterator_base : public pair_iterator_base { 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(std::move(iter), place) {} enumerate_item> 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 @@ -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{IterWrapper{enumerate_wrapper{std::move(begin), 0}}, - IterWrapper{enumerate_wrapper{std::move(end), container_size}}}, - container_size(container_size) + IterWrapper{enumerate_wrapper{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(IterWrapper{enumerate_wrapper{std::move(begin), begin_index}}, - IterWrapper{enumerate_wrapper{std::move(end), end_index}}), - container_size(container_size) + IterWrapper{enumerate_wrapper{std::move(end), end_index}}) {} auto rev() const @@ -235,24 +221,30 @@ namespace blt return enumerator_rev{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{begin.base(), this->end_.base(), begin.get_index(), - this->end_.get_index(), - this->container_size}; + this->end_.get_index()}; + } + + auto take(blt::size_t amount) + { + auto end = this->begin(); + for (blt::size_t i = 0; i < amount; i++) + ++end; + return enumerator{this->begin_.base(), + end.base(), + this->begin_.get_index(), + end.get_index()}; } - - protected: - blt::size_t container_size; }; template @@ -266,20 +258,30 @@ namespace blt return enumerator{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{begin.base(), - this->end_.base(), - begin.get_index(), - this->end_.get_index(), - this->container_size}; + return enumerator_rev{begin.base(), + this->end_.base().base(), + begin.get_index(), + 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{ + this->begin_.base().base(), + end.base(), + this->begin_.base().get_index(), + end.get_index()}; } }; } @@ -306,14 +308,21 @@ namespace blt { public: using iterator::enumerator_reversible>::enumerator_reversible; - - auto skip(blt::size_t offset) + + auto skip(blt::size_t amount) { - return enumerator{this->begin_.base() + offset, + return enumerator{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{this->begin_.base(), + this->begin_.base() + amount, + this->begin_.get_index(), + this->begin_.get_index() + amount}; } }; @@ -331,13 +340,21 @@ namespace blt { public: using iterator::enumerator_reversible_rev>>::enumerator_reversible_rev; - - auto skip(blt::size_t offset) + + auto skip(blt::size_t amount) { - return enumerator_rev{this->begin_.base().base() - offset, + return enumerator_rev{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{this->begin_.base().base(), + this->begin_.base().base() - amount, + this->begin_.base().get_index(), + this->begin_.base().get_index() - amount}; } };