From bbb48a6b8fd929af51fcd14d34d91a85b15e2f31 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 2 Oct 2024 00:32:18 -0400 Subject: [PATCH] not so clever maps --- CMakeLists.txt | 2 +- include/blt/iterator/common.h | 34 ++++++++++++++++++----- libraries/parallel-hashmap | 2 +- tests/iterator_tests.cpp | 52 +++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 264d9bc..a55502f 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.3) +set(BLT_VERSION 2.0.4) set(BLT_TARGET BLT) diff --git a/include/blt/iterator/common.h b/include/blt/iterator/common.h index 4ffc5ea..ce2805d 100644 --- a/include/blt/iterator/common.h +++ b/include/blt/iterator/common.h @@ -25,6 +25,8 @@ #include #include #include +#include +#include namespace blt::iterator { @@ -110,7 +112,7 @@ namespace blt::iterator } protected: - Iter iter; + mutable Iter iter; }; template @@ -167,13 +169,15 @@ namespace blt::iterator using pointer = value_type; using reference = value_type; - map_wrapper(Iter iter, Func func): deref_only_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); } + private: Func func; }; @@ -183,19 +187,27 @@ namespace blt::iterator { public: using iterator_category = typename std::iterator_traits::iterator_category; - using value_type = meta::deref_return_t; + using value_type = std::conditional_t< + std::is_reference_v>, + std::optional>>>, + std::optional>>; 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)) + filter_wrapper(Iter iter, Pred func): + deref_only_wrapper>(std::move(iter)), func(std::move(func)) {} reference operator*() const { - - return func(*this->iter); + if (!func(*this->iter)) + { + return {}; + } + return *this->iter; } + private: Pred func; }; @@ -346,13 +358,21 @@ namespace blt::iterator } template - auto map(Func func) + auto map(Func func) const { return iterator_container>{ blt::iterator::map_wrapper{m_begin, func}, blt::iterator::map_wrapper{m_end, func}}; } + template + auto filter(Pred pred) const + { + return iterator_container>{ + blt::iterator::filter_wrapper{m_begin, pred}, + blt::iterator::filter_wrapper{m_end, pred}}; + } + auto begin() const { return m_begin; diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 7ef2e73..93201da 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 +Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3 diff --git a/tests/iterator_tests.cpp b/tests/iterator_tests.cpp index 9323d1e..66f787f 100644 --- a/tests/iterator_tests.cpp +++ b/tests/iterator_tests.cpp @@ -196,6 +196,58 @@ void test_iterate() auto [a, b] = data; BLT_TRACE_STREAM << "Map + Zip + Skip + Take + Enumerate (Index: " << i << ")> " << a << " " << b << "\n"; } + BLT_TRACE("================================"); + 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 << "Map + Zip + Skip + Take + Enumerate (Index: " << i << ")> " << a << " " << b << "\n"; + } + BLT_TRACE("================================"); + for (auto a : blt::iterate(array_1).map([](const blt::vec2& in) { return in.normalize(); }) + .filter([](const blt::vec2& f) { return f.x() > 0.5; })) + { + if (!a) + continue; + auto v = *a; + BLT_TRACE_STREAM << " So this one works? " << v << "\n"; + } + BLT_TRACE("================================"); + for (auto a : blt::iterate(array_1).map([](const blt::vec2& in) { return in.normalize(); }) + .enumerate().filter([](const auto& f) { return f.value.x() > 0.5; })) + { + if (!a) + continue; + auto [index, v] = *a; + BLT_TRACE_STREAM << " So this one works? (" << index << ")" << v << "\n"; + } + BLT_TRACE("================================"); +// for (auto a : blt::iterate(array_1).filter([](const auto& f) { return f.x() > 3 && f.y() < 6; }).take(2)) +// { +// if (!a) +// continue; +// auto v = *a; +// BLT_TRACE_STREAM << " How about this one?? " << v.get() << "\n"; +// } + for (auto a : blt::iterate(array_1).map([](const auto& f) { return f.x() > 3 && f.y() < 6; })) + { + BLT_TRACE_STREAM << " How about this one?? " << a << "\n"; + } + +// for (auto [value, a] : blt::iterate(array_1).map( +// [](const blt::vec2& in) { +// return in.normalize(); +// }).enumerate().filter([](const auto& v) { +// return v.index % 2 == 0; +// }).zip(array_2).skip(3)) +// { +// if (!value) +// continue; +// auto [i, v] = *value; +// BLT_TRACE_STREAM << "Enumerate Filter (Index: " << i << ")> " << v << "\n"; +// } } int main()