diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-30 08:27:20 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-30 08:28:15 +0200 |
| commit | 2cbec2472571a625f9d574c15589c57c535bed1a (patch) | |
| tree | d24120fd85d875efd2ede33f6a0f00865c08beeb /dev/lib/utility/chunk_string.hpp | |
| parent | ea7557ebda2dc90804223bf9b41eece46b8b4b52 (diff) | |
feat! utlity: new `chunk_string` algorithm which is faster than the
previous one.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/lib/utility/chunk_string.hpp')
| -rw-r--r-- | dev/lib/utility/chunk_string.hpp | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/dev/lib/utility/chunk_string.hpp b/dev/lib/utility/chunk_string.hpp index e04166c..5f969b4 100644 --- a/dev/lib/utility/chunk_string.hpp +++ b/dev/lib/utility/chunk_string.hpp @@ -2,7 +2,7 @@ * File: core/chunk_string.hpp * Purpose: String implementation for the OCL C++ library. * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss + * Copyright 2025, Amlal El Mahrouss */ #ifndef OCL_UTILITY_CHUNK_STRING_HPP @@ -13,22 +13,23 @@ namespace ocl { - template <typename char_type> + template <typename char_type, std::size_t max_chunk_size = 8196> class basic_chunk_string; - template <typename char_type> + template <typename char_type, std::size_t max_chunk_size> struct basic_chunk_string final { private: - std::unique_ptr<basic_chunk_string<char_type>> next_chunk_string_{}; - basic_chunk_string<char_type>* prev_chunk_string_{nullptr}; + char_type packed_chunks_[max_chunk_size / 4]; + char_type* extended_chunks_ = new char_type[max_chunk_size]; - std::basic_string<char_type> packed_chunks_{}; - int64_t chunk_total{}; + std::size_t chunk_total_{}; - constexpr const static auto max_chunk_size = 4096; + bool bad_{false}; public: + const bool& bad{bad_}; + basic_chunk_string() = default; basic_chunk_string(const char_type* in) @@ -48,21 +49,23 @@ namespace ocl basic_chunk_string& operator+=(const std::basic_string<char_type>& in) { - if (in.empty()) + if (in.empty() || bad_) return *this; - if (chunk_total > max_chunk_size) + if (chunk_total_ > max_chunk_size) { - next_chunk_string_ = std::make_unique<basic_chunk_string<char_type>>(); - *next_chunk_string_ += in; + bad_ = true; + return *this; + } - next_chunk_string_->prev_chunk_string_ = this; + const static auto size_max_chunk = max_chunk_size / 4; - return *next_chunk_string_; - } + if (chunk_total_ < size_max_chunk) + std::memcpy(packed_chunks_ + chunk_total_, in.data(), in.size()); + else + std::memcpy(extended_chunks_ + chunk_total_, in.data(), in.size()); - packed_chunks_ += in; - chunk_total += in.size(); + chunk_total_ += in.size(); return *this; } @@ -76,8 +79,8 @@ namespace ocl { ocl::io::print(packed_chunks_); - if (next_chunk_string_) - this->next_chunk_string_->print(); + if (extended_chunks_) + ocl::io::print(extended_chunks_); } }; @@ -87,4 +90,4 @@ namespace ocl fmt.print(); } } // namespace ocl -#endif // ifndef OCL_UTILITY_CHUNK_STRING_HPP
\ No newline at end of file +#endif // ifndef OCL_UTILITY_CHUNK_STRING_HPP |
