From 0125584c807ecacdfe0921bbe192b95a9153b230 Mon Sep 17 00:00:00 2001 From: Manasvi Goyal <55101825+ManasviGoyal@users.noreply.github.com> Date: Wed, 8 Jun 2022 07:44:02 +0530 Subject: [PATCH] return the ptr_ by reference --- .../growable_buffer_panels.cpp | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/studies/growable_buffer/growable_buffer_panels.cpp b/studies/growable_buffer/growable_buffer_panels.cpp index 7671cffd89..de6712d7c7 100644 --- a/studies/growable_buffer/growable_buffer_panels.cpp +++ b/studies/growable_buffer/growable_buffer_panels.cpp @@ -16,7 +16,7 @@ class GrowableBuffer { reserved_.push_back(initial); } - const std::unique_ptr + const std::unique_ptr& ptr() const { return ptr_[0]; } @@ -64,34 +64,26 @@ class GrowableBuffer { fill_panel(datum); } - // when length of each panel is different - /*PRIMITIVE - getitem_at_nowrap(int64_t at, int64_t index) const { - if (at(new PRIMITIVE[length()]); - size_t new_length = length(); - int64_t next_panel = 0; - for (int64_t i = 0; i < ptr_.size(); i++) { - std::cout << ptr_[i].get()[0] << ", " << length_[i] << std::endl; - memcpy(ptr.get() + next_panel, reinterpret_cast(ptr_[i].get()), length_[i]*sizeof(PRIMITIVE)); - next_panel += length_[i]; + if (!is_contiguous()) { + auto ptr = std::unique_ptr(new PRIMITIVE[length()]); + size_t new_length = length(); + int64_t next_panel = 0; + for (int64_t i = 0; i < ptr_.size(); i++) { + memcpy(ptr.get() + next_panel, reinterpret_cast(ptr_[i].get()), length_[i]*sizeof(PRIMITIVE)); + next_panel += length_[i]; + } + clear(); + ptr_[0] = std::move(ptr); + length_[0] = new_length; } - clear(); - ptr_[0] = std::move(ptr); - length_[0] = new_length; - } + } int64_t is_contiguous() { return (ptr_.size() == 1);