not so clever maps

main
Brett 2024-10-02 00:32:18 -04:00
parent ec02eb8419
commit bbb48a6b8f
4 changed files with 81 additions and 9 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 2.0.3) set(BLT_VERSION 2.0.4)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -25,6 +25,8 @@
#include <blt/iterator/fwddecl.h> #include <blt/iterator/fwddecl.h>
#include <blt/meta/meta.h> #include <blt/meta/meta.h>
#include <blt/meta/iterator.h> #include <blt/meta/iterator.h>
#include <functional>
#include <optional>
namespace blt::iterator namespace blt::iterator
{ {
@ -110,7 +112,7 @@ namespace blt::iterator
} }
protected: protected:
Iter iter; mutable Iter iter;
}; };
template<typename Iter, typename Derived> template<typename Iter, typename Derived>
@ -167,13 +169,15 @@ namespace blt::iterator
using pointer = value_type; using pointer = value_type;
using reference = value_type; using reference = value_type;
map_wrapper(Iter iter, Func func): deref_only_wrapper<Iter, map_wrapper<Iter, Func>>(std::move(iter)), func(std::move(func)) map_wrapper(Iter iter, Func func):
deref_only_wrapper<Iter, map_wrapper<Iter, Func>>(std::move(iter)), func(std::move(func))
{} {}
reference operator*() const reference operator*() const
{ {
return func(*this->iter); return func(*this->iter);
} }
private: private:
Func func; Func func;
}; };
@ -183,19 +187,27 @@ namespace blt::iterator
{ {
public: public:
using iterator_category = typename std::iterator_traits<Iter>::iterator_category; using iterator_category = typename std::iterator_traits<Iter>::iterator_category;
using value_type = meta::deref_return_t<Iter>; using value_type = std::conditional_t<
std::is_reference_v<meta::deref_return_t<Iter>>,
std::optional<std::reference_wrapper<std::remove_reference_t<meta::deref_return_t<Iter>>>>,
std::optional<meta::deref_return_t<Iter>>>;
using difference_type = blt::ptrdiff_t; using difference_type = blt::ptrdiff_t;
using pointer = value_type; using pointer = value_type;
using reference = value_type; using reference = value_type;
filter_wrapper(Iter iter, Pred func): deref_only_wrapper<Iter, filter_wrapper<Iter, Pred>>(std::move(iter)), func(std::move(func)) filter_wrapper(Iter iter, Pred func):
deref_only_wrapper<Iter, filter_wrapper<Iter, Pred>>(std::move(iter)), func(std::move(func))
{} {}
reference operator*() const reference operator*() const
{ {
if (!func(*this->iter))
return func(*this->iter); {
return {};
} }
return *this->iter;
}
private: private:
Pred func; Pred func;
}; };
@ -346,13 +358,21 @@ namespace blt::iterator
} }
template<typename Func> template<typename Func>
auto map(Func func) auto map(Func func) const
{ {
return iterator_container<blt::iterator::map_wrapper<IterBase, Func>>{ return iterator_container<blt::iterator::map_wrapper<IterBase, Func>>{
blt::iterator::map_wrapper<IterBase, Func>{m_begin, func}, blt::iterator::map_wrapper<IterBase, Func>{m_begin, func},
blt::iterator::map_wrapper<IterBase, Func>{m_end, func}}; blt::iterator::map_wrapper<IterBase, Func>{m_end, func}};
} }
template<typename Pred>
auto filter(Pred pred) const
{
return iterator_container<blt::iterator::filter_wrapper<IterBase, Pred>>{
blt::iterator::filter_wrapper<IterBase, Pred>{m_begin, pred},
blt::iterator::filter_wrapper<IterBase, Pred>{m_end, pred}};
}
auto begin() const auto begin() const
{ {
return m_begin; return m_begin;

@ -1 +1 @@
Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3

View File

@ -196,6 +196,58 @@ void test_iterate()
auto [a, b] = data; auto [a, b] = data;
BLT_TRACE_STREAM << "Map + Zip + Skip + Take + Enumerate (Index: " << i << ")> " << a << " " << b << "\n"; 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() int main()