Skip to content

Commit

Permalink
return the ptr_ by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasviGoyal committed Jun 8, 2022
1 parent e230047 commit 0125584
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions studies/growable_buffer/growable_buffer_panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GrowableBuffer {
reserved_.push_back(initial);
}

const std::unique_ptr<PRIMITIVE>
const std::unique_ptr<PRIMITIVE>&
ptr() const {
return ptr_[0];
}
Expand Down Expand Up @@ -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<length_[index])
return ptr_[index].get()[at%length_[index]];
return getitem_at_nowrap(at - length_[index], index+1);
}*/

PRIMITIVE
getitem_at_nowrap(int64_t at) const {
return ptr_[0].get()[at];
return ptr_[floor(at/initial_)].get()[at%initial_];
}

void
concatenate() {
auto ptr = std::unique_ptr<PRIMITIVE>(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<void*>(ptr_[i].get()), length_[i]*sizeof(PRIMITIVE));
next_panel += length_[i];
if (!is_contiguous()) {
auto ptr = std::unique_ptr<PRIMITIVE>(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<void*>(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);
Expand Down

0 comments on commit 0125584

Please sign in to comment.