From 325d580a4db4d19e64b3a7af047d09d0a2843f56 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 2 Jan 2026 22:26:08 +0100 Subject: feat: rope impl improvements, bumping to release as well. Signed-off-by: Amlal El Mahrouss --- include/ocl/tproc/detail/rope_fwd.hpp | 4 +++- include/ocl/tproc/detail/rope_fwd.inl | 27 ++++++++++++++++++++++----- include/ocl/tproc/rope.hpp | 24 ++++++++++++++++++++++++ test/rope_test/crope.pred.test.cpp | 18 +++++++++++++----- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/include/ocl/tproc/detail/rope_fwd.hpp b/include/ocl/tproc/detail/rope_fwd.hpp index 0ce0457..7541795 100644 --- a/include/ocl/tproc/detail/rope_fwd.hpp +++ b/include/ocl/tproc/detail/rope_fwd.hpp @@ -28,7 +28,7 @@ namespace ocl::tproc using reference = CharT&; using const_reference = const CharT&; using pointer = std::allocator_traits::pointer; - using const_pointer = std::allocator_traits::pointer; + using const_pointer = const std::allocator_traits::pointer; using iterator_type = basic_rope*; iterator_type rbegin(); @@ -75,6 +75,8 @@ namespace ocl::tproc bool operator!=(const boost::core::basic_string_view&); bool operator==(const boost::core::basic_string_view&); + iterator_type concat(iterator_type right); + public: static constexpr size_type npos = (size_type)(-1); diff --git a/include/ocl/tproc/detail/rope_fwd.inl b/include/ocl/tproc/detail/rope_fwd.inl index 81a7f5c..01e5bbe 100644 --- a/include/ocl/tproc/detail/rope_fwd.inl +++ b/include/ocl/tproc/detail/rope_fwd.inl @@ -60,11 +60,11 @@ namespace ocl::tproc { std::allocator_traits::deallocate(alloc_, blob_, capacity_); } - + delete left_; delete right_; - left_ = nullptr; + left_ = nullptr; right_ = nullptr; } @@ -91,6 +91,7 @@ namespace ocl::tproc { throw std::out_of_range("rope index out of range"); } + return blob_[pos]; } @@ -109,6 +110,7 @@ namespace ocl::tproc } } + public: static rope_ptr concat(rope_ptr left, rope_ptr right, Allocator alloc = Allocator()) { if (!left) @@ -116,8 +118,14 @@ namespace ocl::tproc if (!right) return left; - auto* new_rope = new basic_rope(); - new_rope->impl_ = new tree_impl(left, right, alloc); + auto res = std::basic_string(left->data().data(), left->data().size()); + res += std::basic_string(right->data().data(), right->data().size()); + + auto* new_rope = new basic_rope(res); + + new_rope->impl_->left_ = left; + new_rope->impl_->right_ = right; + return new_rope; } @@ -401,13 +409,14 @@ namespace ocl::tproc return npos; size_type rope_size = impl_->size(); + if (needle.size() > rope_size) return npos; - // Brute force search for (size_type i = 0; i <= rope_size - needle.size(); ++i) { bool match = true; + for (size_type j = 0; j < needle.size(); ++j) { if (impl_->at(i + j) != needle[j]) @@ -416,9 +425,11 @@ namespace ocl::tproc break; } } + if (match) return i; } + return npos; } @@ -526,6 +537,12 @@ namespace ocl::tproc return !(*this == str); } + template + basic_rope* basic_rope::concat(basic_rope* right) + { + return impl_->concat(this, right); + } + } // namespace ocl::tproc #endif \ No newline at end of file diff --git a/include/ocl/tproc/rope.hpp b/include/ocl/tproc/rope.hpp index 26729b0..21277f2 100644 --- a/include/ocl/tproc/rope.hpp +++ b/include/ocl/tproc/rope.hpp @@ -35,6 +35,30 @@ namespace ocl::tproc::rope } }; + /// \brief ends with pred type. + template + class ends_with_pred final + { + It cond_; + + public: + ends_with_pred(const boost::core::basic_string_view& cond) + : cond_(cond) + { + } + + It* operator()(It* begin, It* end) + { + for (auto beg{begin}; beg != end; ++beg) + { + if (beg->ends_with(cond_.data())) + return beg; + } + + return end; + } + }; + template class uppercase_pred final { diff --git a/test/rope_test/crope.pred.test.cpp b/test/rope_test/crope.pred.test.cpp index 2d0ddae..b1cc59d 100644 --- a/test/rope_test/crope.pred.test.cpp +++ b/test/rope_test/crope.pred.test.cpp @@ -12,8 +12,8 @@ BOOST_AUTO_TEST_CASE(rope_should_succeed_in_find_pred) { - auto rope = ocl::tproc::crope("foo"); - auto it = ocl::tproc::rope::exact_pred{"foo"}(rope.cbegin(), rope.cend()); + auto rope = ocl::tproc::crope("Exact Sentence"); + auto it = ocl::tproc::rope::exact_pred{"Exact Sentence"}(rope.cbegin(), rope.cend()); BOOST_TEST(it != rope.cend()); @@ -22,12 +22,20 @@ BOOST_AUTO_TEST_CASE(rope_should_succeed_in_find_pred) BOOST_AUTO_TEST_CASE(rope_should_succeed_in_starts_with) { - auto rope = ocl::tproc::crope("foobar"); - + auto rope = ocl::tproc::crope("The Quick Brown Fox Jumps Over The Lazy Dog"); // find the leaf with the starting value 'foo' - auto it = ocl::tproc::rope::starts_with_pred{"foo"}(rope.cbegin(), rope.cend()); + auto it = ocl::tproc::rope::starts_with_pred{"The Quick"}(rope.cbegin(), rope.cend()); BOOST_TEST(it != rope.cend()); + auto it_end = ocl::tproc::rope::ends_with_pred{"Lazy Dog"}(rope.cbegin(), rope.cend()); + + BOOST_TEST(it_end != rope.cend()); + + ocl::io::println(it_end->data()); ocl::io::println(it->data()); + + auto new_elem = ocl::tproc::crope(", and Jumps again."); + + ocl::io::println(rope.concat(&new_elem)->data()); } -- cgit v1.2.3