From 6f06647a21f7f29e99ef8e45e1d3c08db0b46038 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 13 Aug 2024 00:33:44 -0400 Subject: [PATCH] meow --- CMakeLists.txt | 2 +- include/blt/std/meta.h | 94 +++++++++++++++++ include/blt/std/ranges.h | 219 ++++++++++++++------------------------- 3 files changed, 173 insertions(+), 142 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d546358..fedec3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.18.31) +set(BLT_VERSION 0.18.32) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/std/meta.h b/include/blt/std/meta.h index 6872483..56203fb 100644 --- a/include/blt/std/meta.h +++ b/include/blt/std/meta.h @@ -94,6 +94,100 @@ 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)); #define BLT_META_MAKE_FUNCTION_CHECK(FUNC, ...)\ template \ diff --git a/include/blt/std/ranges.h b/include/blt/std/ranges.h index cb4c7af..91d7509 100644 --- a/include/blt/std/ranges.h +++ b/include/blt/std/ranges.h @@ -20,9 +20,6 @@ namespace blt { namespace itr { - BLT_META_MAKE_MEMBER_CHECK(value_type); - - BLT_META_MAKE_MEMBER_CHECK(reference); template class itr_container @@ -46,60 +43,39 @@ namespace blt End end_; }; - template - struct value_type_helper + template + class iterator_base { - template - inline static constexpr auto test() -> decltype(Subs::value_type) - { - return std::declval(); - } - - template - inline static constexpr auto test() -> decltype(std::remove_pointer_t()) - { - return std::declval>(); - } + public: + using value_type = blt::meta::value_type_t>; + using difference_type = blt::meta::difference_t; + using pointer = blt::meta::pointer_t; + using reference = blt::meta::reference_t&>; + using const_reference = blt::meta::const_reference_t&>; }; - template - struct reference_type_helper - { - template, bool> = true> - inline static constexpr auto test() -> typename Subs::reference - { - return std::declval(); - } - - template - inline static constexpr auto test() -> decltype(std::remove_cv_t>>())& - { - return std::declval&>(); - } - }; - - template - using value_type_t = decltype(value_type_helper::template test()); - template - using reference_t = decltype(reference_type_helper::template test()); - template class iterator { public: using iterator_category = std::input_iterator_tag; - using value_type = value_type_t; - using difference_type = typename TYPE_ITR::difference_type; - using pointer = typename TYPE_ITR::pointer; - using reference = reference_t; - using const_reference = const reference_t; + using value_type = typename iterator_base::value_type; + using difference_type = typename iterator_base::difference_type; + using pointer = typename iterator_base::pointer; + using reference = typename iterator_base::reference; + using const_reference = typename iterator_base::const_reference; private: - blt::size_t index = 0; + blt::size_t index; TYPE_ITR current; public: - explicit iterator(TYPE_ITR current): current(std::move(current)) + explicit iterator(TYPE_ITR current, blt::size_t index): index(index), current(std::move(current)) {} + auto iter() + { + return current; + } + iterator& operator++() { ++index; @@ -127,106 +103,67 @@ namespace blt return {index, *current}; }; }; - -// template> -// class iterator; -// -// template -// class iterator -// { -// public: -// using iterator_category = std::input_iterator_tag; -// using value_type = typename TYPE_ITR::value_type; -// using difference_type = typename TYPE_ITR::difference_type; -// using pointer = typename TYPE_ITR::pointer; -// using reference = typename TYPE_ITR::reference; -// using const_reference = const typename TYPE_ITR::reference; -// private: -// blt::size_t index = 0; -// TYPE_ITR current; -// public: -// explicit iterator(TYPE_ITR current): current(std::move(current)) -// {} -// -// iterator& operator++() -// { -// ++index; -// ++current; -// return *this; -// } -// -// bool operator==(iterator other) const -// { -// return current == other.current; -// } -// -// bool operator!=(iterator other) const -// { -// return current != other.current; -// } -// -// std::pair operator*() const -// { -// return {index, *current}; -// }; -// -// std::pair operator*() -// { -// return {index, *current}; -// }; -// }; -// -// template -// class iterator -// { -// public: -// using iterator_category = std::input_iterator_tag; -// using value_type = std::remove_pointer_t; -// using difference_type = std::ptrdiff_t; -// using pointer = TYPE_ITR; -// using reference = std::remove_pointer_t&; -// using const_reference = const std::remove_pointer_t&; -// private: -// blt::size_t index = 0; -// TYPE_ITR current; -// public: -// explicit iterator(TYPE_ITR current): current(std::move(current)) -// {} -// -// iterator& operator++() -// { -// ++index; -// ++current; -// return *this; -// } -// -// bool operator==(iterator other) const -// { -// return current == other.current; -// } -// -// bool operator!=(iterator other) const -// { -// return current != other.current; -// } -// -// std::pair operator*() const -// { -// return {index, *current}; -// }; -// -// std::pair operator*() -// { -// return {index, *current}; -// }; -// }; + + template + class reverse_iterator + { + public: + using iterator_category = std::input_iterator_tag; + using value_type = typename iterator_base::value_type; + using difference_type = typename iterator_base::difference_type; + using pointer = typename iterator_base::pointer; + using reference = typename iterator_base::reference; + using const_reference = typename iterator_base::const_reference; + private: + blt::size_t index; + TYPE_ITR current; + public: + explicit reverse_iterator(TYPE_ITR current, blt::size_t index): index(index), current(std::move(current)) + {} + + auto iter() + { + return current; + } + + reverse_iterator& operator++() + { + --index; + --current; + return *this; + } + + bool operator==(reverse_iterator other) const + { + return current == other.current; + } + + bool operator!=(reverse_iterator other) const + { + return current != other.current; + } + + std::pair operator*() const + { + return {index, *current}; + }; + + std::pair operator*() + { + return {index, *current}; + }; + }; } - template + template typename iterator = itr::iterator> class enumerator { public: - explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin)), end_(std::move(end)) + explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin), 0), end_(std::move(end), 0) + {} + + explicit enumerator(TYPE_ITR begin, TYPE_ITR end, blt::size_t i_begin, blt::size_t i_end): + begin_(std::move(begin), i_begin), end_(std::move(end), i_end) {} itr::iterator begin() @@ -240,8 +177,8 @@ namespace blt } private: - itr::iterator begin_; - itr::iterator end_; + iterator begin_; + iterator end_; }; template