1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
// BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE
#ifndef AWKWARD_GROWABLEBUFFER_H_
#define AWKWARD_GROWABLEBUFFER_H_
#include "awkward/BuilderOptions.h"
#include <cstring>
#include <vector>
#include <memory>
#include <numeric>
#include <cmath>
#include <complex>
#include <iostream>
#include <utility>
#include <stdexcept>
#include <stdint.h>
namespace awkward {
template <template <class...> class TT, class... Args>
std::true_type is_tt_impl(TT<Args...>);
template <template <class...> class TT>
std::false_type is_tt_impl(...);
template <template <class...> class TT, class T>
using is_tt = decltype(is_tt_impl<TT>(std::declval<typename std::decay<T>::type>()));
template <typename PRIMITIVE>
/// @class Panel
///
/// Creates a contiguous, one-dimensional panel.
class Panel {
public:
/// @brief Creates a Panel by allocating a new panel, taking a
/// #reserved number of slots.
///
/// @param reserved Currently allocated number of elements in the panel.
Panel(size_t reserved)
: ptr_(std::unique_ptr<PRIMITIVE[]>(new PRIMITIVE[reserved])),
length_(0),
reserved_(reserved),
next_(nullptr) {}
/// @brief Creates a Panel from a full set of parameters.
///
/// @param ptr Unique reference to the panel data.
/// @param length Currently number used of elements in the panel.
/// @param reserved Currently allocated number of elements in the panel.
Panel(std::unique_ptr<PRIMITIVE[]> ptr, size_t length, size_t reserved)
: ptr_(std::move(ptr)),
length_(length),
reserved_(reserved),
next_(nullptr) {}
/// @brief Deletes a Panel.
///
/// Unchain the pointers to avoid a stack overflow when
/// a recursive implicit destructor is invoked.
~Panel() {
for (std::unique_ptr<Panel> current = std::move(next_);
current;
current = std::move(current->next_));
}
/// @brief Overloads [] operator to access elements like an array.
PRIMITIVE& operator[](size_t i) { return ptr_.get()[i]; }
/// @brief Creates a new panel with slots equal to #reserved and
/// appends it after the current panel.
Panel*
append_panel(size_t reserved) {
next_ = std::move(std::unique_ptr<Panel>(new Panel(reserved)));
return next_.get();
}
/// @brief Inserts one `datum` into the panel.
void
fill_panel(PRIMITIVE datum) {
ptr_.get()[length_++] = datum;
}
/// @brief Pointer to the next panel.
std::unique_ptr<Panel>&
next() {
return next_;
}
/// @brief Currently used number of elements in the panel.
size_t
current_length() {
return length_;
}
/// @brief Currently allocated number of elements in the panel.
size_t
reserved() {
return reserved_;
}
/// @brief Unique pointer to the panel data.
std::unique_ptr<PRIMITIVE[]>&
data() {
return ptr_;
}
/// @brief Copies the data from a panel to one contiguously allocated `to_ptr`.
///
/// @param to_ptr One contiguously allocated panel.
/// @param offset Distance between `to_ptr` and the pointer to the destination where the
/// accumulated data is copied.
/// @param from Distance between `ptr` and pointer to the source of the data to be copied.
/// @param length Length of the data to be copied.
void
append(PRIMITIVE* to_ptr, size_t offset, size_t from, int64_t length) const noexcept {
memcpy(to_ptr + offset,
reinterpret_cast<void*>(ptr_.get() + from),
length * sizeof(PRIMITIVE) - from);
}
/// @brief Copies and concatenates the accumulated data from multiple panels `ptr_` to one
/// contiguously allocated `to_ptr`.
///
/// @param to_ptr One contiguously allocated panel.
/// @param offset Distance between `to_ptr` and the pointer to the destination where the
/// accumulated data is copied.
/// @param from Distance between `ptr` and pointer to the source of the data to be copied.
void
concatenate_to_from(PRIMITIVE* to_ptr, size_t offset, size_t from) const noexcept {
memcpy(to_ptr + offset,
reinterpret_cast<void*>(ptr_.get() + from),
length_ * sizeof(PRIMITIVE) - from);
if (next_) {
next_->concatenate_to(to_ptr, offset + length_);
}
}
/// @brief Copies and concatenates the accumulated data from multiple panels `ptr_` to one
/// contiguously allocated `to_ptr`.
///
/// @param to_ptr One contiguously allocated panel.
/// @param offset Distance between `to_ptr` and the pointer to the destination where the
/// accumulated data is copied.
void
concatenate_to(PRIMITIVE* to_ptr, size_t offset) const noexcept {
memcpy(to_ptr + offset,
reinterpret_cast<void*>(ptr_.get()),
length_ * sizeof(PRIMITIVE));
if (next_) {
next_->concatenate_to(to_ptr, offset + length_);
}
}
/// @brief Fills (one panel) GrowableBuffer<TO_PRIMITIVE> with the
/// elements of (possibly multi-panels) GrowableBuffer<PRIMITIVE>.
///
/// Changes the data type from `PRIMITIVE` to `TO_PRIMITIVE`/
template <typename TO_PRIMITIVE>
typename std::enable_if<(!awkward::is_tt<std::complex, TO_PRIMITIVE>::value &&
!awkward::is_tt<std::complex, PRIMITIVE>::value) ||
(awkward::is_tt<std::complex, TO_PRIMITIVE>::value &&
awkward::is_tt<std::complex, PRIMITIVE>::value)>::type
copy_as(TO_PRIMITIVE* to_ptr, size_t offset) {
for (size_t i = 0; i < length_; i++) {
to_ptr[offset++] = static_cast<TO_PRIMITIVE>(ptr_.get()[i]);
}
if (next_) {
next_->copy_as(to_ptr, offset);
}
}
template <typename TO_PRIMITIVE>
typename std::enable_if<!awkward::is_tt<std::complex, TO_PRIMITIVE>::value &&
awkward::is_tt<std::complex, PRIMITIVE>::value>::type
copy_as(TO_PRIMITIVE* to_ptr, size_t offset) {
for (size_t i = 0; i < length_; i++) {
to_ptr[offset++] = static_cast<TO_PRIMITIVE>(ptr_.get()[i].real());
to_ptr[offset++] = static_cast<TO_PRIMITIVE>(ptr_.get()[i].imag());
}
if (next_) {
next_->copy_as(to_ptr, offset);
}
}
/// @brief 'copy_as' specialization of a 'std::complex' template type.
/// Fills (one panel) GrowableBuffer<std::complex> with the
/// elements of (possibly multi-panels) GrowableBuffer<PRIMITIVE>.
///
/// Changes the data type from `PRIMITIVE` to `std::complex`/
template <typename TO_PRIMITIVE>
typename std::enable_if<awkward::is_tt<std::complex, TO_PRIMITIVE>::value &&
!awkward::is_tt<std::complex, PRIMITIVE>::value>::type
copy_as(TO_PRIMITIVE* to_ptr, size_t offset) {
for (size_t i = 0; i < length_; i++) {
double val = static_cast<double>(ptr_.get()[i]);
to_ptr[offset++] = TO_PRIMITIVE(val);
}
if (next_) {
next_->copy_as(to_ptr, offset);
}
}
private:
/// @brief Unique pointer to the panel data.
std::unique_ptr<PRIMITIVE[]> ptr_;
/// @brief The length of the panel data.
size_t length_;
/// @brief Reserved size of the panel.
size_t reserved_;
/// @brief Pointer to the next Panel.
std::unique_ptr<Panel> next_;
};
/// @class GrowableBuffer
///
/// @brief Discontiguous, one-dimensional buffer (which consists of
/// multiple contiguous, one-dimensional panels) that can grow
/// indefinitely by calling #append.
///
/// Configured by BuilderOptions, the buffer starts by reserving
/// {@link BuilderOptions#initial initial} slots.
/// When the number of slots used reaches the number reserved, a new
/// panel is allocated that is
/// {@link BuilderOptions#resize resize} times larger.
///
/// When {@link ArrayBuilder#to_buffers ArrayBuilder::to_buffers} is called,
/// these buffers are copied to the new Content array.
template <typename PRIMITIVE>
class GrowableBuffer {
public:
/// @brief Creates an empty GrowableBuffer.
///
/// @param options Initial size configuration for building a panel.
static GrowableBuffer<PRIMITIVE>
empty(const BuilderOptions& options) {
return empty(options, 0);
}
/// @brief Creates an empty GrowableBuffer with a minimum reservation.
///
/// @param options Initial size configuration for building a panel.
/// @param minreserve The initial reservation will be the maximum
/// of `minreserve` and
/// {@link BuilderOptions#initial initial}.
static GrowableBuffer<PRIMITIVE>
empty(const BuilderOptions& options, int64_t minreserve) {
int64_t actual = options.initial();
if (actual < minreserve) {
actual = minreserve;
}
return GrowableBuffer(
options,
std::unique_ptr<PRIMITIVE[]>(new PRIMITIVE[(size_t)actual]),
0,
actual);
}
/// @brief Creates a GrowableBuffer in which all elements are initialized to `0`.
///
/// @param options Initial size configuration for building a panel.
/// @param length The number of elements to initialize (and the
/// GrowableBuffer's initial #length).
///
/// This is similar to NumPy's
/// [zeros](https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html).
static GrowableBuffer<PRIMITIVE>
zeros(const BuilderOptions& options, int64_t length) {
int64_t actual = options.initial();
if (actual < length) {
actual = length;
}
auto ptr = std::unique_ptr<PRIMITIVE[]>(new PRIMITIVE[(size_t)actual]);
PRIMITIVE* rawptr = ptr.get();
for (int64_t i = 0; i < length; i++) {
rawptr[i] = 0;
}
return GrowableBuffer(options, std::move(ptr), length, actual);
}
/// @brief Creates a GrowableBuffer in which all elements are initialized
/// to a given value.
///
/// @param options Initial size configuration for building a panel.
/// @param value The initialization value.
/// @param length The number of elements to initialize (and the
/// GrowableBuffer's initial #length).
///
/// This is similar to NumPy's
/// [full](https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html).
static GrowableBuffer<PRIMITIVE>
full(const BuilderOptions& options, PRIMITIVE value, int64_t length) {
int64_t actual = options.initial();
if (actual < length) {
actual = length;
}
auto ptr = std::unique_ptr<PRIMITIVE[]>(new PRIMITIVE[(size_t)actual]);
PRIMITIVE* rawptr = ptr.get();
for (int64_t i = 0; i < length; i++) {
rawptr[i] = value;
}
return GrowableBuffer<PRIMITIVE>(options, std::move(ptr), length, actual);
}
/// @brief Creates a GrowableBuffer in which the elements are initialized
/// to numbers counting from `0` to `length`.
///
/// @param options Initial size configuration for building a panel.
/// @param length The number of elements to initialize (and the
/// GrowableBuffer's initial #length).
///
/// This is similar to NumPy's
/// [arange](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html).
static GrowableBuffer<PRIMITIVE>
arange(const BuilderOptions& options, int64_t length) {
int64_t actual = options.initial();
if (actual < length) {
actual = length;
}
auto ptr = std::unique_ptr<PRIMITIVE[]>(new PRIMITIVE[(size_t)actual]);
PRIMITIVE* rawptr = ptr.get();
for (int64_t i = 0; i < length; i++) {
rawptr[i] = (PRIMITIVE)i;
}
return GrowableBuffer(options, std::move(ptr), length, actual);
}
/// @brief Takes a (possibly multi-panels) GrowableBuffer<PRIMITIVE>
/// and makes another (one panel) GrowableBuffer<TO_PRIMITIVE>.
///
/// Used to change the data type of buffer content from `PRIMITIVE`
/// to `TO_PRIMITIVE` for building arrays.
template <typename TO_PRIMITIVE>
static GrowableBuffer<TO_PRIMITIVE>
copy_as(const GrowableBuffer<PRIMITIVE>& other) {
int64_t len = (int64_t)other.length();
int64_t actual =
(len < other.options_.initial()) ? other.options_.initial() : len;
if (!awkward::is_tt<std::complex, TO_PRIMITIVE>::value &&
awkward::is_tt<std::complex, PRIMITIVE>::value) {
len *= 2;
actual *= 2;
}
auto ptr =
std::unique_ptr<TO_PRIMITIVE[]>(new TO_PRIMITIVE[(size_t)actual]);
TO_PRIMITIVE* rawptr = ptr.get();
other.panel_->copy_as(rawptr, 0);
return GrowableBuffer<TO_PRIMITIVE>(
BuilderOptions(actual, other.options().resize()),
std::move(ptr),
len,
actual);
}
/// @brief Creates a GrowableBuffer from a full set of parameters.
///
/// @param options Initial size configuration for building a panel.
/// @param ptr Reference-counted pointer to the array buffer.
/// @param length Currently used number of elements.
/// @param reserved Currently allocated number of elements.
///
/// Although the #length increments every time #append is called,
/// it is always less than or equal to #reserved because of
/// allocations of new panels.
GrowableBuffer(const BuilderOptions& options,
std::unique_ptr<PRIMITIVE[]> ptr,
int64_t length,
int64_t reserved)
: options_(options),
length_(0),
panel_(std::unique_ptr<Panel<PRIMITIVE>>(new Panel<PRIMITIVE>(
std::move(ptr), (size_t)length, (size_t)reserved))),
ptr_(panel_.get()) {}
/// @brief Creates a GrowableBuffer by allocating a new buffer, taking an
/// options #reserved from #options.
///
/// @param options Initial size configuration for building a panel.
GrowableBuffer(const BuilderOptions& options)
: GrowableBuffer(options,
std::unique_ptr<PRIMITIVE[]>(
new PRIMITIVE[(size_t)options.initial()]),
0,
options.initial()) {}
/// @brief Move constructor
///
/// panel_ is move-only.
GrowableBuffer(GrowableBuffer&& other) noexcept
: options_(other.options_),
length_(other.length_),
panel_(std::move(other.panel_)),
ptr_(other.ptr_) {}
/// @brief Currently used number of elements.
///
/// Although the #length increments every time #append is called,
/// it is always less than or equal to #reserved because of
/// allocations of new panels.
size_t
length() const {
return length_ + ptr_->current_length();
}
/// @brief Return options of this GrowableBuffer.
const BuilderOptions&
options() const {
return options_;
}
/// @brief Discards accumulated data, the #reserved returns to
/// options.initial(), and a new #ptr is allocated.
void
clear() {
panel_ = std::move(std::unique_ptr<Panel<PRIMITIVE>>(
new Panel<PRIMITIVE>((size_t)options_.initial())));
ptr_ = panel_.get();
length_ = 0;
}
/// @brief Last element in last panel
PRIMITIVE
last() const {
if (ptr_->current_length() == 0) {
throw std::runtime_error("Buffer is empty");
} else {
return (*ptr_)[ptr_->current_length() - 1];
}
}
/// @brief Currently used number of bytes.
size_t
nbytes() const {
return length() * sizeof(PRIMITIVE);
}
/// @brief Inserts one `datum` into the panel, possibly triggering
/// allocation of a new panel.
///
/// This increases the #length by 1; if the new #length is larger than
/// #reserved, a new panel will be allocated.
void
append(PRIMITIVE datum) {
if (ptr_->current_length() == ptr_->reserved()) {
add_panel((size_t)ceil(options_.initial() * options_.resize()));
}
fill_panel(datum);
}
/// @brief Inserts an entire array into the panel(s), possibly triggering
/// allocation of a new panel.
///
/// If the size is larger than the empty slots in the current panel, then,
/// first, the empty slots are filled and then a new panel will be allocated
/// for the rest of the array elements.
void
extend(const PRIMITIVE* ptr, size_t size) {
size_t unfilled_items = ptr_->reserved() - ptr_->current_length();
if (size > unfilled_items) {
for (size_t i = 0; i < unfilled_items; i++) {
fill_panel(ptr[i]);
}
add_panel(size - unfilled_items > ptr_->reserved() ? size - unfilled_items
: ptr_->reserved());
for (size_t i = unfilled_items; i < size; i++) {
fill_panel(ptr[i]);
}
} else {
for (size_t i = 0; i < size; i++) {
fill_panel(ptr[i]);
}
}
}
/// @brief Like append, but the type signature returns the reference to `PRIMITIVE`.
PRIMITIVE&
append_and_get_ref(PRIMITIVE datum) {
append(datum);
return (*ptr_)[ptr_->current_length() - 1];
}
/// @brief Copies and concatenates all accumulated data from multiple panels to one
/// contiguously allocated `external_pointer`.
void
concatenate(PRIMITIVE* external_pointer) const noexcept {
if (external_pointer) {
panel_->concatenate_to(external_pointer, 0);
}
}
/// @brief Moves all accumulated data from multiple panels to one
/// contiguously allocated `external_pointer`. The panels are deleted,
/// and a new #ptr is allocated.
void
move_to(PRIMITIVE* to_ptr) noexcept {
size_t next_offset = 0;
while(panel_) {
memcpy(to_ptr + next_offset,
reinterpret_cast<void*>(panel_.get()->data().get()),
panel_.get()->current_length() * sizeof(PRIMITIVE));
next_offset += panel_.get()->current_length();
panel_ = std::move(panel_.get()->next());
}
clear();
}
/// @brief Copies and concatenates all accumulated data from multiple panels to one
/// contiguously allocated `external_pointer`.
void
concatenate_from(PRIMITIVE* external_pointer, size_t to, size_t from) const noexcept {
if (external_pointer) {
panel_->concatenate_to_from(external_pointer, to, from);
}
}
/// @brief Copies data from a panel to one contiguously allocated `external_pointer`.
void
append(PRIMITIVE* external_pointer, size_t offset, size_t from, int64_t length) const noexcept {
if (external_pointer) {
panel_->append(external_pointer, offset, from, length);
}
}
private:
/// @brief Fills the data into the panel one by one.
void
fill_panel(PRIMITIVE datum) {
ptr_->fill_panel(datum);
}
/// @brief Adds a new panel with slots equal to #reserved.
/// and updates the current panel pointer to it.
void
add_panel(size_t reserved) {
length_ += ptr_->current_length();
ptr_ = ptr_->append_panel(reserved);
}
/// @brief Initial size configuration for building a panel.
const BuilderOptions options_;
/// @brief Filled panels data length.
size_t length_;
/// @brief The first panel.
std::unique_ptr<Panel<PRIMITIVE>> panel_;
/// @brief A pointer to a current panel.
///
/// Points to the address of the first byte of the current panel.
Panel<PRIMITIVE>* ptr_;
};
} // namespace awkward
#endif // AWKWARD_GROWABLEBUFFER_H_
|