From c5b8830ff0357ab7f08b69d09acf2d8c4a17913e Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 23 Sep 2024 18:07:31 -0400 Subject: [PATCH] redoing enumerator --- CMakeLists.txt | 2 +- include/blt/std/ranges.h | 100 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37de36f..c4437f4 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.20.18) +set(BLT_VERSION 0.20.19) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/std/ranges.h b/include/blt/std/ranges.h index 4898666..c18756c 100644 --- a/include/blt/std/ranges.h +++ b/include/blt/std/ranges.h @@ -18,6 +18,106 @@ namespace blt { + + template + class enumerate_base + { + public: + explicit enumerate_base(Iter iter): iter(std::move(iter)) + {} + + protected: + Iter iter; + }; + + template + class enumerate_base_fwd : public enumerate_base + { + public: + explicit enumerate_base_fwd(Iter iter): enumerate_base(std::move(iter)), index(0) + {} + + explicit enumerate_base_fwd(Iter iter, blt::size_t place): enumerate_base(std::move(iter)), index(place) + {} + + std::pair> operator*() const + { + return {index, *this->iter}; + } + + enumerate_base_fwd& operator++() + { + ++this->iter; + ++index; + return *this; + } + + enumerate_base_fwd operator++(int) + { + auto tmp = *this; + ++*this; + return tmp; + } + + bool operator==(const enumerate_base_fwd& other) const + { + return other.iter == this->iter; + } + + friend bool operator==(const enumerate_base_fwd& a, const enumerate_base_fwd& b) + { + return a.iter == b.iter; + } + + protected: + blt::size_t index; + }; + + template + class enumerate_base_bidirectional : public enumerate_base_fwd + { + using enumerate_base_fwd::enumerate_base_fwd; + + enumerate_base_bidirectional& operator--() + { + --this->iter; + --this->index; + return *this; + } + + enumerate_base_bidirectional operator--(int) + { + auto tmp = *this; + --*this; + return tmp; + } + }; + + template> + class enumerate_wrapper; + + template + class enumerate_wrapper, bool>>> + { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = typename std::iterator_traits::value_type; + using difference_type = typename std::iterator_traits::difference_type; + using pointer = typename std::iterator_traits::pointer; + using reference = typename std::iterator_traits::reference; + public: + explicit enumerate_wrapper(Iter iter): iter(std::move(iter)), index(0) + {} + + explicit enumerate_wrapper(Iter iter, blt::size_t place): iter(std::move(iter)), index(place) + {} + + + private: + Iter iter; + blt::size_t index; + }; + namespace itr {