From f99e6b3db98a693ce57edca281134560efdce1a3 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 29 Jul 2024 19:34:03 -0400 Subject: [PATCH] make expanding buffer externally expandable with copy! --- CMakeLists.txt | 2 +- arg_parse_variant_map.drawio | 20 ++++++++++---------- include/blt/std/memory.h | 23 ++++++++++++++--------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e3c388f..d49806f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.18.14) +set(BLT_VERSION 0.18.15) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/arg_parse_variant_map.drawio b/arg_parse_variant_map.drawio index ac232d8..509f3a5 100644 --- a/arg_parse_variant_map.drawio +++ b/arg_parse_variant_map.drawio @@ -4,7 +4,7 @@ - + @@ -12,7 +12,7 @@ - + @@ -23,25 +23,25 @@ - + - + - + - + - + @@ -56,13 +56,13 @@ - + - + - + diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index 164e2da..69a9909 100644 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -376,14 +376,14 @@ namespace blt constexpr inline T& operator[](size_t index) { if (index >= size()) - expand(index); + allocate_for(index); return buffer_[index]; } constexpr inline const T& operator[](size_t index) const { if (index >= size()) - expand(index); + allocate_for(index); return buffer_[index]; } @@ -461,13 +461,10 @@ namespace blt { delete_this(buffer_, size()); } - - private: - void expand(blt::size_t size) + + void expand(blt::size_t new_size) { - size = std::max(size_, size); - size = blt::mem::next_byte_allocation(size); - T* new_buffer = new T[size]; + T* new_buffer = new T[new_size]; if (buffer_ != nullptr) { if constexpr (std::is_trivially_copyable_v) @@ -486,7 +483,15 @@ namespace blt delete[] buffer_; } buffer_ = new_buffer; - size_ = size; + size_ = new_size; + } + + private: + void allocate_for(blt::size_t accessing_index) + { + accessing_index = std::max(size_, accessing_index); + accessing_index = blt::mem::next_byte_allocation(accessing_index); + expand(accessing_index); } inline void delete_this(T* buffer, blt::size_t)