From e6d096a486ebc9354f78cc8e1e3b8a4fdd1c1bda Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 10 Dec 2023 13:21:26 -0500 Subject: [PATCH] move ranges around (utility over memory) TOOD: add reverse --- include/blt/std/format.h | 4 ++ include/blt/std/memory.h | 79 -------------------------------------- include/blt/std/utility.h | 74 +++++++++++++++++++++++++++++++++++ tests/src/memory_test.cpp | 3 -- tests/src/utility_test.cpp | 9 +++++ 5 files changed, 87 insertions(+), 82 deletions(-) diff --git a/include/blt/std/format.h b/include/blt/std/format.h index f39fb8e..9871426 100755 --- a/include/blt/std/format.h +++ b/include/blt/std/format.h @@ -308,6 +308,10 @@ namespace blt::string std::vector createTable(bool top = false, bool bottom = false); }; + struct tree_node { + std::vector children; + }; + class BinaryTreeFormatter { public: diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index adfc648..aff8eb9 100755 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -102,85 +102,6 @@ namespace blt } } - template - struct range_itr - { - private: - T current; - public: - using iterator_category = std::bidirectional_iterator_tag; - using difference_type = T; - using value_type = T; - using pointer = T*; - using reference = T&; - - explicit range_itr(T current): current(current) - {} - - value_type operator*() const - { return current; } - - value_type operator->() - { return current; } - - range_itr& operator++() - { - current++; - return *this; - } - - range_itr& operator--() - { - current--; - return *this; - } - - range_itr operator++(int) - { - auto tmp = *this; - ++(*this); - return tmp; - } - - range_itr operator--(int) - { - auto tmp = *this; - --(*this); - return tmp; - } - - friend bool operator==(const range_itr& a, const range_itr& b) - { - return a.current == b.current; - } - - friend bool operator!=(const range_itr& a, const range_itr& b) - { - return a.current != b.current; - } - }; - - template - struct range - { - private: - T _begin; - T _end; - public: - range(T begin, T end): _begin(begin), _end(end) - {} - - range_itr begin() - { - return range_itr{_begin}; - } - - range_itr end() - { - return range_itr{_end}; - } - }; - template struct ptr_iterator { diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index db55a0e..fcdb818 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -134,6 +134,80 @@ namespace blt { return enumerator{container.begin(), container.end()}; } + + template + struct range + { + public: + struct range_itr + { + public: + using iterator_category = std::bidirectional_iterator_tag; + using difference_type = T; + using value_type = T; + using pointer = T*; + using reference = T&; + private: + T current; + bool forward; + public: + + explicit range_itr(T current, bool forward): current(current), forward(forward) + {} + + value_type operator*() const + { return current; } + + value_type operator->() + { return current; } + + range_itr& operator++() + { + if (forward) + current++; + else + current--; + return *this; + } + + range_itr& operator--() + { + if (forward) + current--; + else + current++; + return *this; + } + + friend bool operator==(const range_itr& a, const range_itr& b) + { + return a.current == b.current; + } + + friend bool operator!=(const range_itr& a, const range_itr& b) + { + return a.current != b.current; + } + }; + private: + T _begin; + T _end; + T offset = 0; + public: + range(T begin, T end): _begin(begin), _end(end), offset(end < begin ? 1 : 0) + {} + + range_itr begin() + { + return range_itr(_begin - offset, offset == 0); + } + + range_itr end() + { + // not sure if i like this + return range_itr(_end - offset, offset == 0); + } + }; #if defined(__GNUC__) || defined(__llvm__) diff --git a/tests/src/memory_test.cpp b/tests/src/memory_test.cpp index 0680e1a..9eb7dee 100644 --- a/tests/src/memory_test.cpp +++ b/tests/src/memory_test.cpp @@ -101,8 +101,5 @@ void blt::test::memory::access() void blt::test::memory::ranges() { - blt::range range(0, 10); - for (auto r : range) - BLT_TRACE_STREAM << r; BLT_TRACE_STREAM << '\n'; } diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index 3bca1a2..8309bc7 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -148,5 +148,14 @@ void blt::test::utility::run() testEnumerate(in); } + for (auto r : blt::range(0, 10)) + BLT_TRACE_STREAM << r << " "; + BLT_TRACE_STREAM << "\n"; + + for (auto r : blt::range(10, 0)) + BLT_TRACE_STREAM << r << " "; + BLT_TRACE_STREAM << "\n"; + + fuckered(); }