3#ifndef AWKWARD_GROWABLEBUFFER_H_
4#define AWKWARD_GROWABLEBUFFER_H_
20 template <
typename PRIMITIVE>
32 : ptr_(std::unique_ptr<PRIMITIVE[]>(new PRIMITIVE[
reserved])),
43 : ptr_(std::move(ptr)),
49 PRIMITIVE&
operator[](
size_t i) {
return ptr_.get()[i]; }
55 next_ = std::move(std::unique_ptr<Panel>(
new Panel(
reserved)));
62 ptr_.get()[length_++] = datum;
66 std::unique_ptr<Panel>&
91 append(PRIMITIVE* to_ptr,
size_t offset,
size_t from, int64_t length)
const noexcept {
92 memcpy(to_ptr + offset,
93 reinterpret_cast<void*
>(ptr_.get() + from),
94 length *
sizeof(PRIMITIVE) - from);
106 memcpy(to_ptr + offset,
107 reinterpret_cast<void*
>(ptr_.get() + from),
108 length_ *
sizeof(PRIMITIVE) - from);
110 next_->concatenate_to(to_ptr, offset + length_);
122 memcpy(to_ptr + offset,
123 reinterpret_cast<void*
>(ptr_.get()),
124 length_ *
sizeof(PRIMITIVE));
126 next_->concatenate_to(to_ptr, offset + length_);
134 template <
typename TO_PRIMITIVE>
136 copy_as(TO_PRIMITIVE* to_ptr,
size_t offset) {
137 for (
size_t i = 0; i < length_; i++) {
138 to_ptr[offset++] =
static_cast<TO_PRIMITIVE
>(ptr_.get()[i]);
141 next_->copy_as(to_ptr, offset);
147 std::unique_ptr<PRIMITIVE[]> ptr_;
156 std::unique_ptr<Panel> next_;
173 template <
typename PRIMITIVE>
193 if (actual < minreserve) {
198 std::unique_ptr<PRIMITIVE[]>(
new PRIMITIVE[(
size_t)actual]),
217 auto ptr = std::unique_ptr<PRIMITIVE[]>(
new PRIMITIVE[(
size_t)actual]);
218 PRIMITIVE* rawptr = ptr.get();
219 for (int64_t i = 0; i <
length; i++) {
241 auto ptr = std::unique_ptr<PRIMITIVE[]>(
new PRIMITIVE[(
size_t)actual]);
242 PRIMITIVE* rawptr = ptr.get();
243 for (int64_t i = 0; i <
length; i++) {
264 auto ptr = std::unique_ptr<PRIMITIVE[]>(
new PRIMITIVE[(
size_t)actual]);
265 PRIMITIVE* rawptr = ptr.get();
266 for (int64_t i = 0; i <
length; i++) {
267 rawptr[i] = (PRIMITIVE)i;
277 template <
typename TO_PRIMITIVE>
285 std::unique_ptr<TO_PRIMITIVE[]>(
new TO_PRIMITIVE[(
size_t)actual]);
286 TO_PRIMITIVE* rawptr = ptr.get();
288 other.panel_->copy_as(rawptr, 0);
308 std::unique_ptr<PRIMITIVE[]> ptr,
313 panel_(std::unique_ptr<
Panel<PRIMITIVE>>(new
Panel<PRIMITIVE>(
314 std::move(ptr), (size_t)
length, (size_t)reserved))),
315 ptr_(panel_.get()) {}
323 std::unique_ptr<PRIMITIVE[]>(
324 new PRIMITIVE[(size_t)
options.initial()]),
332 : options_(other.options_),
333 length_(other.length_),
334 panel_(std::move(other.panel_)),
344 return length_ + ptr_->current_length();
365 if (ptr_->current_length() == 0) {
366 throw std::runtime_error(
"Buffer is empty");
368 return (*ptr_)[ptr_->current_length() - 1];
375 return length() *
sizeof(PRIMITIVE);
385 if (ptr_->current_length() == ptr_->reserved()) {
386 add_panel((
size_t)ceil(ptr_->reserved() * options_.
resize()));
398 extend(
const PRIMITIVE* ptr,
size_t size) {
399 size_t unfilled_items = ptr_->reserved() - ptr_->current_length();
400 if (size > unfilled_items) {
401 for (
size_t i = 0; i < unfilled_items; i++) {
404 add_panel(size - unfilled_items > ptr_->reserved() ? size - unfilled_items
406 for (
size_t i = unfilled_items; i < size; i++) {
410 for (
size_t i = 0; i < size; i++) {
420 return (*ptr_)[ptr_->current_length() - 1];
427 if (external_pointer) {
428 panel_->concatenate_to(external_pointer, 0);
436 if (external_pointer) {
437 panel_->concatenate_to_from(external_pointer, to, from);
443 append(PRIMITIVE* external_pointer,
size_t offset,
size_t from, int64_t
length)
const noexcept {
444 if (external_pointer) {
445 panel_->append(external_pointer, offset, from,
length);
452 fill_panel(PRIMITIVE datum) {
453 ptr_->fill_panel(datum);
459 add_panel(
size_t reserved) {
460 length_ += ptr_->current_length();
461 ptr_ = ptr_->append_panel(reserved);
471 std::unique_ptr<Panel<PRIMITIVE>> panel_;
476 Panel<PRIMITIVE>* ptr_;
Discontiguous, one-dimensional buffer (which consists of multiple contiguous, one-dimensional panels)...
Definition: GrowableBuffer.h:174
static GrowableBuffer< PRIMITIVE > zeros(const BuilderOptions &options, int64_t length)
Creates a GrowableBuffer in which all elements are initialized to 0.
Definition: GrowableBuffer.h:212
void extend(const PRIMITIVE *ptr, size_t size)
Inserts an entire array into the panel(s), possibly triggering allocation of a new panel.
Definition: GrowableBuffer.h:398
static GrowableBuffer< TO_PRIMITIVE > copy_as(const GrowableBuffer< PRIMITIVE > &other)
Takes a (possibly multi-panels) GrowableBuffer<PRIMITIVE> and makes another (one panel) GrowableBuffe...
Definition: GrowableBuffer.h:279
PRIMITIVE last() const
Last element in last panel.
Definition: GrowableBuffer.h:364
void concatenate_from(PRIMITIVE *external_pointer, size_t to, size_t from) const noexcept
Copies and concatenates all accumulated data from multiple panels to one contiguously allocated exter...
Definition: GrowableBuffer.h:435
size_t nbytes() const
Currently used number of bytes.
Definition: GrowableBuffer.h:374
GrowableBuffer(GrowableBuffer &&other) noexcept
Move constructor.
Definition: GrowableBuffer.h:331
static GrowableBuffer< PRIMITIVE > full(const BuilderOptions &options, PRIMITIVE value, int64_t length)
Creates a GrowableBuffer in which all elements are initialized to a given value.
Definition: GrowableBuffer.h:236
const BuilderOptions & options() const
Return options of this GrowableBuffer.
Definition: GrowableBuffer.h:349
void concatenate(PRIMITIVE *external_pointer) const noexcept
Copies and concatenates all accumulated data from multiple panels to one contiguously allocated exter...
Definition: GrowableBuffer.h:426
PRIMITIVE & append_and_get_ref(PRIMITIVE datum)
Like append, but the type signature returns the reference to PRIMITIVE.
Definition: GrowableBuffer.h:418
size_t length() const
Currently used number of elements.
Definition: GrowableBuffer.h:343
static GrowableBuffer< PRIMITIVE > empty(const BuilderOptions &options, int64_t minreserve)
Creates an empty GrowableBuffer with a minimum reservation.
Definition: GrowableBuffer.h:191
void append(PRIMITIVE datum)
Inserts one datum into the panel, possibly triggering allocation of a new panel.
Definition: GrowableBuffer.h:384
static GrowableBuffer< PRIMITIVE > arange(const BuilderOptions &options, int64_t length)
Creates a GrowableBuffer in which the elements are initialized to numbers counting from 0 to length.
Definition: GrowableBuffer.h:259
void append(PRIMITIVE *external_pointer, size_t offset, size_t from, int64_t length) const noexcept
Copies data from a panel to one contiguously allocated external_pointer.
Definition: GrowableBuffer.h:443
GrowableBuffer(const BuilderOptions &options, std::unique_ptr< PRIMITIVE[]> ptr, int64_t length, int64_t reserved)
Creates a GrowableBuffer from a full set of parameters.
Definition: GrowableBuffer.h:307
static GrowableBuffer< PRIMITIVE > empty(const BuilderOptions &options)
Creates an empty GrowableBuffer.
Definition: GrowableBuffer.h:180
void clear()
Discards accumulated data, the #reserved returns to options.initial(), and a new #ptr is allocated.
Definition: GrowableBuffer.h:356
GrowableBuffer(const BuilderOptions &options)
Creates a GrowableBuffer by allocating a new buffer, taking an options #reserved from options.
Definition: GrowableBuffer.h:321
Definition: GrowableBuffer.h:24
void fill_panel(PRIMITIVE datum)
Inserts one datum into the panel.
Definition: GrowableBuffer.h:61
Panel * append_panel(size_t reserved)
Creates a new panel with slots equal to reserved and appends it after the current panel.
Definition: GrowableBuffer.h:54
Panel(std::unique_ptr< PRIMITIVE[]> ptr, size_t length, size_t reserved)
Creates a Panel from a full set of parameters.
Definition: GrowableBuffer.h:42
size_t current_length()
Currently used number of elements in the panel.
Definition: GrowableBuffer.h:73
std::unique_ptr< Panel > & next()
Pointer to the next panel.
Definition: GrowableBuffer.h:67
size_t reserved()
Currently allocated number of elements in the panel.
Definition: GrowableBuffer.h:79
void append(PRIMITIVE *to_ptr, size_t offset, size_t from, int64_t length) const noexcept
Copies the data from a panel to one contiguously allocated to_ptr.
Definition: GrowableBuffer.h:91
void concatenate_to_from(PRIMITIVE *to_ptr, size_t offset, size_t from) const noexcept
Copies and concatenates the accumulated data from multiple panels ptr_ to one contiguously allocated ...
Definition: GrowableBuffer.h:105
Panel(size_t reserved)
Creates a Panel by allocating a new panel, taking a reserved number of slots.
Definition: GrowableBuffer.h:31
PRIMITIVE & operator[](size_t i)
Overloads [] operator to access elements like an array.
Definition: GrowableBuffer.h:49
void copy_as(TO_PRIMITIVE *to_ptr, size_t offset)
Fills (one panel) GrowableBuffer<TO_PRIMITIVE> with the elements of (possibly multi-panels) GrowableB...
Definition: GrowableBuffer.h:136
void concatenate_to(PRIMITIVE *to_ptr, size_t offset) const noexcept
Copies and concatenates the accumulated data from multiple panels ptr_ to one contiguously allocated ...
Definition: GrowableBuffer.h:121
int64_t len(const T &self)
Definition: content.h:46
Definition: BitMaskedArray.h:15
Options< int64_t, double > BuilderOptions
Definition: BuilderOptions.h:63
Container for all configuration options needed by ArrayBuilder, GrowableBuffer, LayoutBuilder and the...
Definition: BuilderOptions.h:20
int64_t initial() const noexcept
The initial number of reserved entries for a GrowableBuffer.
Definition: BuilderOptions.h:41
double resize() const noexcept
The factor with which a GrowableBuffer is resized when its length reaches its reserved.
Definition: BuilderOptions.h:49