main
Brett 2024-10-01 17:39:43 -04:00
parent e7bfedd142
commit 2163baf57c
7 changed files with 66 additions and 138 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.1) set(BLT_VERSION 2.0.2)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -123,29 +123,75 @@ namespace blt::iterator
return *this->iter; return *this->iter;
} }
// zip_wrapper& operator++() // your_class& operator++()
// { // {
// return *this; // return *this;
// } // }
// //
// zip_wrapper& operator--() // your_class& operator--()
// { // {
// return *this; // 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_category, std::random_access_iterator_tag>, // static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
// "Iterator must allow random access"); // "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_category, std::random_access_iterator_tag>, // static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
// "Iterator must allow random access"); // "Iterator must allow random access");
// } // }
}; };
template<typename Iter, typename Func>
class map_wrapper : public passthrough_wrapper<Iter, map_wrapper<Iter, Func>>
{
public:
using iterator_category = typename std::iterator_traits<Iter>::iterator_category;
using value_type = std::invoke_result_t<Func, meta::deref_return_t<Iter>>;
using difference_type = blt::ptrdiff_t;
using pointer = value_type;
using reference = value_type;
map_wrapper(Iter iter, Func func): passthrough_wrapper<Iter, map_wrapper<Iter, Func>>(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<Iter>, "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<Iter>, "Iterator must allow random access");
return {a.iter - n, a.func};
}
private:
Func func;
};
namespace impl namespace impl
{ {
template<typename Derived> template<typename Derived>
@ -291,6 +337,14 @@ namespace blt::iterator
return enumerate_iterator_container{begin(), end(), static_cast<blt::size_t>(std::distance(begin(), end()))}; return enumerate_iterator_container{begin(), end(), static_cast<blt::size_t>(std::distance(begin(), end()))};
} }
template<typename Func>
auto map(Func 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_end, func}};
}
auto begin() const auto begin() const
{ {
return m_begin; return m_begin;

View File

@ -108,20 +108,6 @@ namespace blt
template<typename Iter> template<typename Iter>
enumerate_iterator_container(Iter, Iter, blt::size_t) -> enumerate_iterator_container<Iter>; enumerate_iterator_container(Iter, Iter, blt::size_t) -> enumerate_iterator_container<Iter>;
namespace iterator::impl
{
template<typename Derived>
class enumerate_t
{
public:
auto enumerate()
{
auto* d = static_cast<Derived*>(this);
return enumerate_iterator_container{d->begin(), d->end(), static_cast<blt::size_t>(std::distance(d->begin(), d->end()))};
}
};
}
template<typename T> template<typename T>
static inline auto enumerate(T& container) static inline auto enumerate(T& container)
{ {

View File

@ -41,6 +41,9 @@ namespace blt
template<typename... Iter> template<typename... Iter>
struct zip_wrapper; struct zip_wrapper;
template<typename Iter, typename Func>
class map_wrapper;
namespace impl namespace impl
{ {
template<typename Derived> template<typename Derived>
@ -48,12 +51,6 @@ namespace blt
template<typename Derived> template<typename Derived>
class take_t; class take_t;
template<typename Derived>
class zip_t;
template<typename Derived>
class enumerate_t;
} }
} }
} }

View File

@ -121,21 +121,6 @@ namespace blt
}; };
namespace iterator::impl
{
template<typename Derived>
class zip_t
{
public:
template<typename... Iter>
auto zip(iterator::iterator_pair<Iter>... iterator_pairs)
{
auto* d = static_cast<Derived*>(this);
return zip_iterator_container(iterator::iterator_pair<decltype(d->begin())>{d->begin(), d->end()}, iterator_pairs...);
}
};
}
/* /*
* CTAD for the zip containers * CTAD for the zip containers
*/ */

View File

@ -95,104 +95,10 @@ namespace blt::meta
template<class T> template<class T>
inline constexpr bool is_streamable_v = is_streamable<T>::value; inline constexpr bool is_streamable_v = is_streamable<T>::value;
namespace detail
{
template<typename Or>
struct value_type_helper
{
template<typename Subs>
inline static constexpr auto get(int) -> typename Subs::value_type
{
return std::declval<Subs::value_type>();
}
template<typename>
inline static constexpr Or get(...)
{
return std::declval<Or>();
}
};
template<typename Or>
struct reference_type_helper
{
template<typename Subs>
inline static constexpr auto get(int) -> typename Subs::reference
{
return std::declval<typename Subs::reference>();
}
template<typename>
inline static constexpr Or get(...)
{
return std::declval<Or>();
}
};
template<typename Or>
struct const_reference_type_helper
{
template<typename Subs>
inline static constexpr auto get(int) -> typename Subs::const_reference
{
return std::declval<typename Subs::const_reference>();
}
template<typename>
inline static constexpr Or get(...)
{
return std::declval<Or>();
}
};
template<typename Or>
struct pointer_type_helper
{
template<typename Subs>
inline static constexpr auto get(int) -> typename Subs::pointer
{
return std::declval<typename Subs::pointer>();
}
template<typename>
inline static constexpr Or get(...)
{
return std::declval<Or>();
}
};
template<typename Or>
struct difference_type_helper
{
template<typename Subs>
inline static constexpr auto get(int) -> typename Subs::difference_type
{
return std::declval<typename Subs::difference_type>();
}
template<typename>
inline static constexpr Or get(...)
{
return std::declval<Or>();
}
};
}
template<typename T, typename Or>
using value_type_t = decltype(detail::value_type_helper<Or>::template get<T>(0));
template<typename T, typename Or>
using difference_t = decltype(detail::difference_type_helper<Or>::template get<T>(0));
template<typename T, typename Or>
using pointer_t = decltype(detail::pointer_type_helper<Or>::template get<T>(0));
template<typename T, typename Or>
using reference_t = decltype(detail::reference_type_helper<Or>::template get<T>(0));
template<typename T, typename Or>
using const_reference_t = decltype(detail::const_reference_type_helper<Or>::template get<T>(0));
template<typename T> template<typename T>
struct arrow_return struct arrow_return
{ {
using type = typename std::result_of<decltype(&T::operator->)(T*)>::type; using type = typename std::invoke_result_t<decltype(&T::operator->), T*>;
}; };
template<typename T> template<typename T>
@ -208,7 +114,7 @@ namespace blt::meta
template<typename T> template<typename T>
struct deref_return struct deref_return
{ {
using type = typename std::result_of<decltype(&T::operator*)(T*)>::type; using type = typename std::invoke_result_t<decltype(&T::operator*), T&>;
}; };
template<typename T> template<typename T>

View File

@ -191,10 +191,10 @@ void test_iterate()
BLT_TRACE_STREAM << "Zip: " << a << " " << b << "\n"; BLT_TRACE_STREAM << "Zip: " << a << " " << b << "\n";
} }
BLT_TRACE("================================"); 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; 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";
} }
} }