From 2163baf57c25c6763aa1feeabb74ad2dbad3499a Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 1 Oct 2024 17:39:43 -0400 Subject: [PATCH] mapping --- CMakeLists.txt | 2 +- include/blt/iterator/common.h | 62 ++++++++++++++++++-- include/blt/iterator/enumerate.h | 14 ----- include/blt/iterator/fwddecl.h | 9 +-- include/blt/iterator/zip.h | 15 ----- include/blt/meta/meta.h | 98 +------------------------------- tests/iterator_tests.cpp | 4 +- 7 files changed, 66 insertions(+), 138 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cc50be..e20c34e 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.1) +set(BLT_VERSION 2.0.2) set(BLT_TARGET BLT) diff --git a/include/blt/iterator/common.h b/include/blt/iterator/common.h index 37d8c8f..683ad4b 100644 --- a/include/blt/iterator/common.h +++ b/include/blt/iterator/common.h @@ -123,29 +123,75 @@ namespace blt::iterator return *this->iter; } -// zip_wrapper& operator++() +// your_class& operator++() // { // return *this; // } // -// zip_wrapper& operator--() +// your_class& operator--() // { // return *this; // } // -// friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n) +// friend your_class operator+(const your_class& a, blt::ptrdiff_t n) // { // static_assert(std::is_same_v, // "Iterator must allow random access"); // } // -// friend zip_wrapper operator-(const zip_wrapper& a, blt::ptrdiff_t n) +// 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 map_wrapper : public passthrough_wrapper> + { + public: + using iterator_category = typename std::iterator_traits::iterator_category; + using value_type = std::invoke_result_t>; + using difference_type = blt::ptrdiff_t; + using pointer = value_type; + using reference = value_type; + + map_wrapper(Iter iter, Func func): passthrough_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; + }; + namespace impl { template @@ -291,6 +337,14 @@ namespace blt::iterator return enumerate_iterator_container{begin(), end(), static_cast(std::distance(begin(), end()))}; } + template + auto map(Func func) + { + return iterator_container>{ + blt::iterator::map_wrapper{m_begin, func}, + blt::iterator::map_wrapper{m_end, func}}; + } + auto begin() const { return m_begin; diff --git a/include/blt/iterator/enumerate.h b/include/blt/iterator/enumerate.h index b793f7b..76e74a2 100644 --- a/include/blt/iterator/enumerate.h +++ b/include/blt/iterator/enumerate.h @@ -108,20 +108,6 @@ namespace blt template enumerate_iterator_container(Iter, Iter, blt::size_t) -> enumerate_iterator_container; - namespace iterator::impl - { - template - class enumerate_t - { - public: - auto enumerate() - { - auto* d = static_cast(this); - return enumerate_iterator_container{d->begin(), d->end(), static_cast(std::distance(d->begin(), d->end()))}; - } - }; - } - template static inline auto enumerate(T& container) { diff --git a/include/blt/iterator/fwddecl.h b/include/blt/iterator/fwddecl.h index 36378fa..0499342 100644 --- a/include/blt/iterator/fwddecl.h +++ b/include/blt/iterator/fwddecl.h @@ -41,6 +41,9 @@ namespace blt template struct zip_wrapper; + template + class map_wrapper; + namespace impl { template @@ -48,12 +51,6 @@ namespace blt template class take_t; - - template - class zip_t; - - template - class enumerate_t; } } } diff --git a/include/blt/iterator/zip.h b/include/blt/iterator/zip.h index bc60275..df26e43 100644 --- a/include/blt/iterator/zip.h +++ b/include/blt/iterator/zip.h @@ -121,21 +121,6 @@ namespace blt }; - namespace iterator::impl - { - template - class zip_t - { - public: - template - auto zip(iterator::iterator_pair... iterator_pairs) - { - auto* d = static_cast(this); - return zip_iterator_container(iterator::iterator_pairbegin())>{d->begin(), d->end()}, iterator_pairs...); - } - }; - } - /* * CTAD for the zip containers */ diff --git a/include/blt/meta/meta.h b/include/blt/meta/meta.h index e7619a0..c356988 100644 --- a/include/blt/meta/meta.h +++ b/include/blt/meta/meta.h @@ -95,104 +95,10 @@ namespace blt::meta template inline constexpr bool is_streamable_v = is_streamable::value; - namespace detail - { - template - struct value_type_helper - { - template - inline static constexpr auto get(int) -> typename Subs::value_type - { - return std::declval(); - } - - template - inline static constexpr Or get(...) - { - return std::declval(); - } - }; - - template - struct reference_type_helper - { - template - inline static constexpr auto get(int) -> typename Subs::reference - { - return std::declval(); - } - - template - inline static constexpr Or get(...) - { - return std::declval(); - } - }; - - template - struct const_reference_type_helper - { - template - inline static constexpr auto get(int) -> typename Subs::const_reference - { - return std::declval(); - } - - template - inline static constexpr Or get(...) - { - return std::declval(); - } - }; - - template - struct pointer_type_helper - { - template - inline static constexpr auto get(int) -> typename Subs::pointer - { - return std::declval(); - } - - template - inline static constexpr Or get(...) - { - return std::declval(); - } - }; - - template - struct difference_type_helper - { - template - inline static constexpr auto get(int) -> typename Subs::difference_type - { - return std::declval(); - } - - template - inline static constexpr Or get(...) - { - return std::declval(); - } - }; - } - - template - using value_type_t = decltype(detail::value_type_helper::template get(0)); - template - using difference_t = decltype(detail::difference_type_helper::template get(0)); - template - using pointer_t = decltype(detail::pointer_type_helper::template get(0)); - template - using reference_t = decltype(detail::reference_type_helper::template get(0)); - template - using const_reference_t = decltype(detail::const_reference_type_helper::template get(0)); - template struct arrow_return { - using type = typename std::result_of)(T*)>::type; + using type = typename std::invoke_result_t), T*>; }; template @@ -208,7 +114,7 @@ namespace blt::meta template struct deref_return { - using type = typename std::result_of::type; + using type = typename std::invoke_result_t; }; template diff --git a/tests/iterator_tests.cpp b/tests/iterator_tests.cpp index 183be9b..9323d1e 100644 --- a/tests/iterator_tests.cpp +++ b/tests/iterator_tests.cpp @@ -191,10 +191,10 @@ void test_iterate() BLT_TRACE_STREAM << "Zip: " << a << " " << b << "\n"; } BLT_TRACE("================================"); - for (auto [i, data] : blt::iterate(array_1).zip(list_1).skip(3).take(4).enumerate()) + for (auto [i, data] : blt::iterate(array_1).map([](const blt::vec2& in) { return in.normalize(); }).zip(list_1).skip(3).take(4).enumerate()) { auto [a, b] = data; - BLT_TRACE_STREAM << "Zip (" << i << "): " << a << " " << b << "\n"; + BLT_TRACE_STREAM << "Map + Zip + Skip + Take + Enumerate (Index: " << i << ")> " << a << " " << b << "\n"; } }