diff --git a/CMakeLists.txt b/CMakeLists.txt index e20c34e..264d9bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 2.0.2) +set(BLT_VERSION 2.0.3) set(BLT_TARGET BLT) diff --git a/include/blt/iterator/common.h b/include/blt/iterator/common.h index 683ad4b..4ffc5ea 100644 --- a/include/blt/iterator/common.h +++ b/include/blt/iterator/common.h @@ -122,32 +122,43 @@ namespace blt::iterator { return *this->iter; } - -// your_class& operator++() -// { -// return *this; -// } -// -// your_class& operator--() -// { -// return *this; -// } -// -// friend your_class operator+(const your_class& a, blt::ptrdiff_t n) -// { -// static_assert(std::is_same_v, -// "Iterator must allow random access"); -// } -// -// friend your_class operator-(const your_class& a, blt::ptrdiff_t n) -// { -// static_assert(std::is_same_v, -// "Iterator must allow random access"); -// } + }; + + template + class deref_only_wrapper : public passthrough_wrapper + { + public: + using passthrough_wrapper::passthrough_wrapper; + + deref_only_wrapper& operator++() + { + ++this->iter; + return *this; + } + + deref_only_wrapper& operator--() + { + --this->iter; + return *this; + } + + deref_only_wrapper& operator+(blt::ptrdiff_t n) + { + static_assert(meta::is_random_access_iterator_v, "Iterator must allow random access"); + this->iter = this->iter + n; + return *this; + } + + deref_only_wrapper& operator-(blt::ptrdiff_t n) + { + static_assert(meta::is_random_access_iterator_v, "Iterator must allow random access"); + this->iter = this->iter - n; + return *this; + } }; template - class map_wrapper : public passthrough_wrapper> + class map_wrapper : public deref_only_wrapper> { public: using iterator_category = typename std::iterator_traits::iterator_category; @@ -156,42 +167,39 @@ namespace blt::iterator using pointer = value_type; using reference = value_type; - map_wrapper(Iter iter, Func func): passthrough_wrapper>(std::move(iter)), func(std::move(func)) + map_wrapper(Iter iter, Func func): deref_only_wrapper>(std::move(iter)), func(std::move(func)) {} reference operator*() const { return func(*this->iter); } - - map_wrapper& operator++() - { - ++this->iter; - return *this; - } - - map_wrapper& operator--() - { - --this->iter; - return *this; - } - - friend map_wrapper operator+(const map_wrapper& a, blt::ptrdiff_t n) - { - static_assert(meta::is_random_access_iterator_v, "Iterator must allow random access"); - return {a.iter + n, a.func}; - } - - friend map_wrapper operator-(const map_wrapper& a, blt::ptrdiff_t n) - { - static_assert(meta::is_random_access_iterator_v, "Iterator must allow random access"); - return {a.iter - n, a.func}; - } - private: Func func; }; + template + class filter_wrapper : public deref_only_wrapper> + { + public: + using iterator_category = typename std::iterator_traits::iterator_category; + using value_type = meta::deref_return_t; + using difference_type = blt::ptrdiff_t; + using pointer = value_type; + using reference = value_type; + + filter_wrapper(Iter iter, Pred func): deref_only_wrapper>(std::move(iter)), func(std::move(func)) + {} + + reference operator*() const + { + + return func(*this->iter); + } + private: + Pred func; + }; + namespace impl { template diff --git a/include/blt/iterator/fwddecl.h b/include/blt/iterator/fwddecl.h index 0499342..f0e80ea 100644 --- a/include/blt/iterator/fwddecl.h +++ b/include/blt/iterator/fwddecl.h @@ -44,6 +44,9 @@ namespace blt template class map_wrapper; + template + class filter_wrapper; + namespace impl { template