diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c64224fa5..06e734b04f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,9 @@ target_compile_features(awkward-parent INTERFACE cxx_std_11) # C++ dependencies (header-only): RapidJSON and dlpack target_include_directories(awkward-parent INTERFACE rapidjson/include dlpack/include) +# C++ dependencies (header-only): GrowableBuffer +target_include_directories(awkward-parent INTERFACE src/awkward/_v2/cpp-headers/) + # First tier: cpu-kernels (object files, static library, and dynamic library). add_library(awkward-cpu-kernels-objects OBJECT ${CPU_KERNEL_SOURCES}) set_target_properties(awkward-cpu-kernels-objects PROPERTIES POSITION_INDEPENDENT_CODE ON) @@ -91,7 +94,12 @@ target_link_libraries(awkward PUBLIC awkward-parent) include(CTest) if(BUILD_TESTING) + add_subdirectory(tests-cpp) add_subdirectory(tests) + + addtest_nolibs(test_1542-growable-buffer tests-cpp/test_1542-growable-buffer.cpp) + addtest(test_1542-array-builder tests-cpp/test_1542-array-builder.cpp) + endif() option(PYBUILD "Build Python modules") diff --git a/include/awkward/builder/ArrayBuilder.h b/include/awkward/builder/ArrayBuilder.h index ad072a5def..f9d5f28039 100644 --- a/include/awkward/builder/ArrayBuilder.h +++ b/include/awkward/builder/ArrayBuilder.h @@ -11,9 +11,6 @@ #include "awkward/builder/Builder.h" namespace awkward { - class Content; - using ContentPtr = std::shared_ptr; - class ArrayBuilderOptions; class Builder; using BuilderPtr = std::shared_ptr; @@ -26,9 +23,9 @@ namespace awkward { public: /// @brief Creates an ArrayBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. - ArrayBuilder(const ArrayBuilderOptions& options); + ArrayBuilder(const int64_t initial); /// @brief Copy the current snapshot into the BuffersContainer and /// return a Form as a std::string (JSON). diff --git a/include/awkward/builder/ArrayBuilderOptions.h b/include/awkward/builder/ArrayBuilderOptions.h deleted file mode 100644 index fab800aab0..0000000000 --- a/include/awkward/builder/ArrayBuilderOptions.h +++ /dev/null @@ -1,43 +0,0 @@ -// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE - -#ifndef AWKWARD_ARRAYBUILDEROPTIONS_H_ -#define AWKWARD_ARRAYBUILDEROPTIONS_H_ - -#include "awkward/common.h" - -namespace awkward { - /// @class ArrayBuilderOptions - /// - /// @brief Container for all configuration options needed by ArrayBuilder, - /// GrowableBuffer, and the Builder subclasses. - class LIBAWKWARD_EXPORT_SYMBOL ArrayBuilderOptions { - public: - /// @brief Creates an ArrayBuilderOptions from a full set of parameters. - /// - /// @param initial The initial number of - /// {@link GrowableBuffer#reserved reserved} entries for a GrowableBuffer. - /// @param resize The factor with which a GrowableBuffer is resized - /// when its {@link GrowableBuffer#length length} reaches its - /// {@link GrowableBuffer#reserved reserved}. - ArrayBuilderOptions(int64_t initial, double resize); - - /// @brief The initial number of - /// {@link GrowableBuffer#reserved reserved} entries for a GrowableBuffer. - int64_t - initial() const; - - /// @brief The factor with which a GrowableBuffer is resized - /// when its {@link GrowableBuffer#length length} reaches its - /// {@link GrowableBuffer#reserved reserved}. - double - resize() const; - - private: - /// See #initial. - int64_t initial_; - /// See #resize. - double resize_; - }; -} - -#endif // AWKWARD_ARRAYBUILDEROPTIONS_H_ diff --git a/include/awkward/builder/BoolBuilder.h b/include/awkward/builder/BoolBuilder.h index 36c3084822..33f6186f6a 100644 --- a/include/awkward/builder/BoolBuilder.h +++ b/include/awkward/builder/BoolBuilder.h @@ -7,11 +7,10 @@ #include #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class BoolBuilder /// @@ -19,17 +18,17 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL BoolBuilder: public Builder { public: /// @brief Create an empty BoolBuilder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create a BoolBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param buffer Contains the accumulated boolean values. - BoolBuilder(const ArrayBuilderOptions& options, + BoolBuilder(const int64_t initial, GrowableBuffer buffer); /// @brief User-friendly name of this class: `"BoolBuilder"`. @@ -99,13 +98,13 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const GrowableBuffer& buffer() const { return buffer_; } private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer buffer_; }; diff --git a/include/awkward/builder/Builder.h b/include/awkward/builder/Builder.h index d0cb77dace..28067343bc 100644 --- a/include/awkward/builder/Builder.h +++ b/include/awkward/builder/Builder.h @@ -10,8 +10,6 @@ #include "awkward/common.h" namespace awkward { - class Content; - using ContentPtr = std::shared_ptr; class Builder; using BuilderPtr = std::shared_ptr; @@ -31,6 +29,9 @@ namespace awkward { /// @brief Create an array initialized to a given fill value. virtual void full_buffer(const std::string& name, int64_t length, int64_t value, const std::string& dtype) = 0; + + virtual void* + empty_buffer(const std::string& name, int64_t num_bytes) = 0; }; /// @class Builder diff --git a/include/awkward/builder/Complex128Builder.h b/include/awkward/builder/Complex128Builder.h index 74193898c6..ed58ebe916 100644 --- a/include/awkward/builder/Complex128Builder.h +++ b/include/awkward/builder/Complex128Builder.h @@ -4,8 +4,7 @@ #define AWKWARD_COMPLEX128BUILDER_H_ #include "awkward/common.h" -#include "awkward/builder/ArrayBuilderOptions.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" #include @@ -17,33 +16,33 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL Complex128Builder: public Builder { public: /// @brief Create an empty Complex128Builder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create a Complex128Builder from an existing Int64Builder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param old The Int64Builder's buffer. static const BuilderPtr - fromint64(const ArrayBuilderOptions& options, - GrowableBuffer old); + fromint64(const int64_t initial, + const GrowableBuffer& old); /// @brief Create a Complex128Builder from an existing Float64Builder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param old The Float64Builder's buffer. static const BuilderPtr - fromfloat64(const ArrayBuilderOptions& options, - GrowableBuffer old); + fromfloat64(const int64_t initial, + const GrowableBuffer& old); /// @brief Create a Complex128Builder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param buffer Contains the accumulated real numbers. - Complex128Builder(const ArrayBuilderOptions& options, + Complex128Builder(const int64_t initial, GrowableBuffer> buffer); /// @brief User-friendly name of this class: `"Complex128Builder"`. @@ -113,13 +112,13 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const GrowableBuffer>& buffer() const { return buffer_; } private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer> buffer_; }; diff --git a/include/awkward/builder/DatetimeBuilder.h b/include/awkward/builder/DatetimeBuilder.h index 770681fbdc..9c4e4840cb 100644 --- a/include/awkward/builder/DatetimeBuilder.h +++ b/include/awkward/builder/DatetimeBuilder.h @@ -4,11 +4,10 @@ #define AWKWARD_DATETIMEBUILDER_H_ #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class DatetimeBuilder /// @@ -16,17 +15,17 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL DatetimeBuilder: public Builder { public: /// @brief Create an empty DatetimeBuilder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options, const std::string& units); + fromempty(const int64_t initial, const std::string& units); /// @brief Create an DatetimeBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param buffer Contains the accumulated integers. - DatetimeBuilder(const ArrayBuilderOptions& options, + DatetimeBuilder(const int64_t initial, GrowableBuffer content, const std::string& units); @@ -97,8 +96,8 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const std::string& units() const; @@ -108,7 +107,7 @@ namespace awkward { const std::string& unit() const { return units_; } private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer content_; const std::string units_; }; diff --git a/include/awkward/builder/Float64Builder.h b/include/awkward/builder/Float64Builder.h index 1478c1ca04..9085f58061 100644 --- a/include/awkward/builder/Float64Builder.h +++ b/include/awkward/builder/Float64Builder.h @@ -6,11 +6,10 @@ #include #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class Float64Builder /// @@ -18,25 +17,25 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL Float64Builder: public Builder { public: /// @brief Create an empty Float64Builder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create a Float64Builder from an existing Int64Builder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param old The Int64Builder's buffer. static const BuilderPtr - fromint64(const ArrayBuilderOptions& options, - GrowableBuffer old); + fromint64(const int64_t initial, + const GrowableBuffer& old); /// @brief Create a Float64Builder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param buffer Contains the accumulated real numbers. - Float64Builder(const ArrayBuilderOptions& options, + Float64Builder(const int64_t initial, GrowableBuffer buffer); /// @brief Contains the accumulated real numbers (`double`). @@ -110,11 +109,11 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer buffer_; }; diff --git a/include/awkward/builder/GrowableBuffer.h b/include/awkward/builder/GrowableBuffer.h deleted file mode 100644 index 1c1ddfbdc5..0000000000 --- a/include/awkward/builder/GrowableBuffer.h +++ /dev/null @@ -1,163 +0,0 @@ -// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE - -#ifndef AWKWARD_GROWABLEBUFFER_H_ -#define AWKWARD_GROWABLEBUFFER_H_ - -#include "awkward/common.h" -#include "awkward/builder/ArrayBuilderOptions.h" -#include "awkward/kernel-dispatch.h" - -#include - -namespace awkward { - /// @class GrowableBuffer - /// - /// @brief Contiguous, one-dimensional array that can grow indefinitely - /// by calling #append. - /// - /// Configured by ArrayBuilderOptions, the buffer starts by reserving - /// {@link ArrayBuilderOptions#initial ArrayBuilderOptions::initial} slots. - /// When the number of slots used reaches the number reserved, a new - /// buffer is allocated that is - /// {@link ArrayBuilderOptions#resize ArrayBuilderOptions::resize} times - /// larger. Thus, a logarithmic number of reallocations are needed as - /// data grow. - /// - /// When {@link ArrayBuilder#snapshot ArrayBuilder::snapshot} is called, - /// these buffers are copied to the new Content array. - /// - /// If a GrowableBuffer resizes itself by allocating a new array, it - /// copies the buffer it owns. - template - class LIBAWKWARD_EXPORT_SYMBOL GrowableBuffer { - using UniquePtrDeleter = decltype(kernel::array_deleter()); - using UniquePtr = std::unique_ptr; - public: - /// @brief Creates an empty GrowableBuffer. - /// - /// @param options Configuration options for building an array. - static GrowableBuffer - empty(const ArrayBuilderOptions& options); - - /// @brief Creates an empty GrowableBuffer with a minimum reservation. - /// - /// @param options Configuration options for building an array. - /// @param minreserve The initial reservation will be the maximum - /// of `minreserve` and - /// {@link ArrayBuilderOptions#initial ArrayBuilderOptions::initial}. - static GrowableBuffer - empty(const ArrayBuilderOptions& options, size_t minreserve); - - /// @brief Creates a GrowableBuffer in which all elements are initialized - /// to a given value. - /// - /// @param options Configuration options for building an array. - /// @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 - full(const ArrayBuilderOptions& options, T value, size_t length); - - /// @brief Creates a GrowableBuffer in which the elements are initialized - /// to numbers counting from `0` to `length`. - /// - /// @param options Configuration options for building an array. - /// @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 - arange(const ArrayBuilderOptions& options, size_t length); - - /// @brief Creates a GrowableBuffer from a full set of parameters. - /// - /// @param options Configuration options for building an array. - /// @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 reallocations. - GrowableBuffer(const ArrayBuilderOptions& options, - GrowableBuffer::UniquePtr ptr, - size_t length, - size_t reserved); - - /// @brief Creates a GrowableBuffer by allocating a new buffer, taking an - /// initial #reserved from - /// {@link ArrayBuilderOptions#initial ArrayBuilderOptions::initial}. - GrowableBuffer(const ArrayBuilderOptions& options); - - /// @brief Reference to a unique pointer to the array buffer. - const GrowableBuffer::UniquePtr& - ptr() const; - - /// @brief FIXME: needed for uproot.cpp - remove when it's gone. - /// Note, it transfers ownership of the array buffer. - GrowableBuffer::UniquePtr - get_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 reallocations. - size_t - length() const; - - /// @brief Changes the #length in-place and possibly reallocate. - /// - /// If the `newlength` is larger than #reserved, #ptr is reallocated. - void - set_length(size_t newlength); - - /// @brief 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 reallocations. - size_t - reserved() const; - - /// @brief Possibly changes the #reserved and reallocate. - /// - /// The parameter only guarantees that at least `minreserved` is reserved; - /// if an amount less than #reserved is requested, nothing changes. - /// - /// If #reserved actually changes, #ptr is reallocated. - void - set_reserved(size_t minreserved); - - /// @brief Discards accumulated data, the #reserved returns to - /// {@link ArrayBuilderOptions#initial ArrayBuilderOptions::initial}, - /// and a new #ptr is allocated. - void - clear(); - - /// @brief Inserts one `datum` into the array, possibly triggering a - /// reallocation. - /// - /// This increases the #length by 1; if the new #length is larger than - /// #reserved, a new #ptr will be allocated. - void - append(T datum); - - /// @brief Returns the element at a given position in the array, without - /// handling negative indexing or bounds-checking. - T - getitem_at_nowrap(int64_t at) const; - - private: - const ArrayBuilderOptions options_; - // @brief See #ptr. - UniquePtr ptr_; - // @brief See #length. - size_t length_; - // @brief See #reserved. - size_t reserved_; - }; -} - -#endif // AWKWARD_GROWABLEBUFFER_H_ diff --git a/include/awkward/builder/Int64Builder.h b/include/awkward/builder/Int64Builder.h index 02dd44d0fb..59f5e54a21 100644 --- a/include/awkward/builder/Int64Builder.h +++ b/include/awkward/builder/Int64Builder.h @@ -4,11 +4,10 @@ #define AWKWARD_INT64BUILDER_H_ #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class Int64Builder /// @@ -16,17 +15,17 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL Int64Builder: public Builder { public: /// @brief Create an empty Int64Builder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create an Int64Builder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param buffer Contains the accumulated integers. - Int64Builder(const ArrayBuilderOptions& options, + Int64Builder(const int64_t initial, GrowableBuffer buffer); /// @brief Contains the accumulated integers. @@ -100,11 +99,11 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer buffer_; }; } diff --git a/include/awkward/builder/ListBuilder.h b/include/awkward/builder/ListBuilder.h index b9a42469ed..d22554f314 100644 --- a/include/awkward/builder/ListBuilder.h +++ b/include/awkward/builder/ListBuilder.h @@ -6,11 +6,10 @@ #include #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class ListBuilder /// @@ -18,14 +17,14 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL ListBuilder: public Builder { public: /// @brief Create an empty ListBuilder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create a ListBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param offsets Contains the accumulated offsets (like /// {@link ListOffsetArrayOf#offsets ListOffsetArray::offsets}). @@ -33,7 +32,7 @@ namespace awkward { /// @param begun If `true`, the ListBuilder is in a state after /// #beginlist and before #endlist and is #active; if `false`, /// it is not. - ListBuilder(const ArrayBuilderOptions& options, + ListBuilder(const int64_t initial, GrowableBuffer offsets, const BuilderPtr& content, bool begun); @@ -106,8 +105,8 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const GrowableBuffer& buffer() const { return offsets_; } @@ -119,7 +118,7 @@ namespace awkward { maybeupdate(const BuilderPtr builder); private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer offsets_; BuilderPtr content_; bool begun_; diff --git a/include/awkward/builder/OptionBuilder.h b/include/awkward/builder/OptionBuilder.h index 280a2660d9..8a0b7b546f 100644 --- a/include/awkward/builder/OptionBuilder.h +++ b/include/awkward/builder/OptionBuilder.h @@ -6,11 +6,10 @@ #include #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class OptionBuilder /// @@ -18,32 +17,32 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL OptionBuilder: public Builder { public: /// @brief Create an OptionBuilder from a number of nulls (all missing). - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param nullcount Length of the purely missing data to create. /// @param content Builder for the non-missing data. static const BuilderPtr - fromnulls(const ArrayBuilderOptions& options, + fromnulls(const int64_t initial, int64_t nullcount, const BuilderPtr& content); /// @brief Create an OptionBuilder from an existing builder (all /// non-missing). - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param content Builder for the non-missing data. static const BuilderPtr - fromvalids(const ArrayBuilderOptions& options, + fromvalids(const int64_t initial, const BuilderPtr& content); /// @brief Create a OptionBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param index Contains the accumulated index (like /// {@link IndexedArrayOf#index IndexedOptionArray::index}). /// @param content Builder for the non-missing data. - OptionBuilder(const ArrayBuilderOptions& options, + OptionBuilder(const int64_t initial, GrowableBuffer index, const BuilderPtr content); @@ -124,7 +123,6 @@ namespace awkward { maybeupdate(const BuilderPtr builder); private: - const ArrayBuilderOptions options_; GrowableBuffer index_; BuilderPtr content_; }; diff --git a/include/awkward/builder/RecordBuilder.h b/include/awkward/builder/RecordBuilder.h index fbefc31990..93f7b0e6f5 100644 --- a/include/awkward/builder/RecordBuilder.h +++ b/include/awkward/builder/RecordBuilder.h @@ -6,11 +6,10 @@ #include #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class RecordBuilder /// @@ -18,14 +17,14 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL RecordBuilder: public Builder { public: /// @brief Create an empty RecordBuilder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create a RecordBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param contents A Builder for each record field. /// @param keys Names for each record field. @@ -37,7 +36,7 @@ namespace awkward { /// `false` otherwise. /// @param nextindex The next field index to fill with data. /// @param nexttotry The next field index to check against a key string. - RecordBuilder(const ArrayBuilderOptions& options, + RecordBuilder(const int64_t initial, const std::vector& contents, const std::vector& keys, const std::vector& pointers, @@ -124,8 +123,8 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const std::vector& keys() const { return keys_; } @@ -145,7 +144,7 @@ namespace awkward { void field_check(const char* key); - const ArrayBuilderOptions options_; + const int64_t initial_; std::vector contents_; std::vector keys_; std::vector pointers_; diff --git a/include/awkward/builder/StringBuilder.h b/include/awkward/builder/StringBuilder.h index 725ef564a0..ce45811f0d 100644 --- a/include/awkward/builder/StringBuilder.h +++ b/include/awkward/builder/StringBuilder.h @@ -4,11 +4,10 @@ #define AWKWARD_STRINGBUILDER_H_ #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class StringBuilder /// @@ -16,17 +15,17 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL StringBuilder: public Builder { public: /// @brief Create an empty StringBuilder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param encoding If `nullptr`, the string is an unencoded bytestring; /// if `"utf-8"`, it is encoded with variable-width UTF-8. /// Currently, no other encodings have been defined. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options, const char* encoding); + fromempty(const int64_t initial, const char* encoding); /// @brief Create a StringBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param offsets Contains the accumulated offsets (like /// {@link ListOffsetArrayOf#offsets ListOffsetArray::offsets}). @@ -35,7 +34,7 @@ namespace awkward { /// @param encoding If `nullptr`, the string is an unencoded bytestring; /// if `"utf-8"`, it is encoded with variable-width UTF-8. /// Currently, no other encodings have been defined. - StringBuilder(const ArrayBuilderOptions& options, + StringBuilder(const int64_t initial, GrowableBuffer offsets, GrowableBuffer content, const char* encoding); @@ -113,15 +112,15 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const GrowableBuffer& buffer() const { return offsets_; } const GrowableBuffer& content() const { return content_; } private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer offsets_; GrowableBuffer content_; const char* encoding_; diff --git a/include/awkward/builder/TupleBuilder.h b/include/awkward/builder/TupleBuilder.h index f67ed6596e..503cfb3c6e 100644 --- a/include/awkward/builder/TupleBuilder.h +++ b/include/awkward/builder/TupleBuilder.h @@ -6,11 +6,10 @@ #include #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class TupleBuilder /// @@ -18,21 +17,21 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL TupleBuilder: public Builder { public: /// @brief Create an empty TupleBuilder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create a TupleBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param contents A Builder for each tuple field. /// @param length Length of accumulated array (same as #length). /// @param begun If `true`, the TupleBuilder is in an active state; /// `false` otherwise. /// @param nextindex The next field index to fill with data. - TupleBuilder(const ArrayBuilderOptions& options, + TupleBuilder(const int64_t initial, const std::vector& contents, int64_t length, bool begun, @@ -110,8 +109,8 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const std::vector& contents() const { return contents_; } @@ -123,7 +122,7 @@ namespace awkward { maybeupdate(int64_t i, const BuilderPtr builder); private: - const ArrayBuilderOptions options_; + const int64_t initial_; std::vector contents_; int64_t length_; bool begun_; diff --git a/include/awkward/builder/UnionBuilder.h b/include/awkward/builder/UnionBuilder.h index e216396220..e0f18bf7d4 100644 --- a/include/awkward/builder/UnionBuilder.h +++ b/include/awkward/builder/UnionBuilder.h @@ -6,11 +6,10 @@ #include #include "awkward/common.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; class TupleBuilder; class RecordBuilder; @@ -20,19 +19,19 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL UnionBuilder: public Builder { public: static const BuilderPtr - fromsingle(const ArrayBuilderOptions& options, + fromsingle(const int64_t initial, const BuilderPtr& firstcontent); /// @brief Create a UnionBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param tags Contains the accumulated tags (like /// {@link UnionArrayOf#tags UnionArray::tags}). /// @param index Contains the accumulated index (like /// {@link UnionArrayOf#index UnionArray::index}). /// @param contents A Builder for each of the union's possibilities. - UnionBuilder(const ArrayBuilderOptions& options, + UnionBuilder(const int64_t initial, GrowableBuffer tags, GrowableBuffer index, std::vector& contents); @@ -105,8 +104,8 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const GrowableBuffer& tags() const { return tags_; } GrowableBuffer& tags_buffer() { return tags_; } @@ -120,7 +119,7 @@ namespace awkward { int8_t current() { return current_;} private: - const ArrayBuilderOptions options_; + const int64_t initial_; GrowableBuffer tags_; GrowableBuffer index_; std::vector contents_; diff --git a/include/awkward/builder/UnknownBuilder.h b/include/awkward/builder/UnknownBuilder.h index e1a98374ab..64f8bc3169 100644 --- a/include/awkward/builder/UnknownBuilder.h +++ b/include/awkward/builder/UnknownBuilder.h @@ -9,7 +9,6 @@ #include "awkward/builder/Builder.h" namespace awkward { - class ArrayBuilderOptions; /// @class UnknownBuilder /// @@ -17,17 +16,17 @@ namespace awkward { class LIBAWKWARD_EXPORT_SYMBOL UnknownBuilder: public Builder { public: /// @brief Create an empty UnknownBuilder. - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. static const BuilderPtr - fromempty(const ArrayBuilderOptions& options); + fromempty(const int64_t initial); /// @brief Create a ListBuilder from a full set of parameters. /// - /// @param options Configuration options for building an array; + /// @param initial Configuration initial for building an array; /// these are passed to every Builder's constructor. /// @param nullcount The number of null values encountered so far. - UnknownBuilder(const ArrayBuilderOptions& options, int64_t nullcount); + UnknownBuilder(const int64_t initial, int64_t nullcount); /// @brief User-friendly name of this class: `"UnknownBuilder"`. const std::string @@ -96,13 +95,13 @@ namespace awkward { const BuilderPtr endrecord() override; - const ArrayBuilderOptions& - options() const { return options_; } + const int64_t + initial() const { return initial_; } const int64_t nullcount() const { return nullcount_; } private: - const ArrayBuilderOptions options_; + const int64_t initial_; int64_t nullcount_; }; } diff --git a/include/awkward/io/json.h b/include/awkward/io/json.h index fc708e9ab9..1c78a47ea0 100644 --- a/include/awkward/io/json.h +++ b/include/awkward/io/json.h @@ -10,7 +10,6 @@ #include "awkward/common.h" #include "awkward/builder/Builder.h" #include "awkward/builder/ArrayBuilder.h" -#include "awkward/builder/ArrayBuilderOptions.h" namespace awkward { /// @class ToJson diff --git a/include/awkward/layoutbuilder/LayoutBuilder.h b/include/awkward/layoutbuilder/LayoutBuilder.h index 63878ee323..195f65a81d 100644 --- a/include/awkward/layoutbuilder/LayoutBuilder.h +++ b/include/awkward/layoutbuilder/LayoutBuilder.h @@ -11,7 +11,6 @@ #include namespace awkward { - class ArrayBuilderOptions; using ForthOutputBufferMap = std::map>; @@ -65,12 +64,12 @@ namespace awkward { /// @brief Creates an LayoutBuilder from a full set of parameters. /// /// @param form The Form that defines the Array to be build. - /// @param options The Array builder options. + /// @param initial The Array builder initial. /// @param vm_init If 'true' the Virtual Machine is instantiated on /// construction. If 'false' an external Virtial Machine must be connected /// to the builder. The flag is used for debugging. LayoutBuilder(const std::string& json_form, - const ArrayBuilderOptions& options, + const int64_t initial, bool vm_init = true); const std::string& diff --git a/include/awkward/python/content.h b/include/awkward/python/content.h index b6ca2596c9..b4fbcbc819 100644 --- a/include/awkward/python/content.h +++ b/include/awkward/python/content.h @@ -80,6 +80,15 @@ namespace { return container_; } + void* + empty_buffer(const std::string& name, int64_t num_bytes) override { + py::object pyarray = py::module::import("numpy").attr("empty")(num_bytes, "u1"); + py::array_t rawarray = pyarray.cast>(); + py::buffer_info rawinfo = rawarray.request(); + container_[py::str(name)] = pyarray; + return rawinfo.ptr; + } + void copy_buffer(const std::string& name, const void* source, int64_t num_bytes) override { py::object pyarray = py::module::import("numpy").attr("empty")(num_bytes, "u1"); @@ -101,6 +110,11 @@ namespace { class EmptyBuffersContainer: public ak::BuffersContainer { public: + void* + empty_buffer(const std::string& name, int64_t num_bytes) override { + return nullptr; + } + void copy_buffer(const std::string& name, const void* source, int64_t num_bytes) override { } diff --git a/src/awkward/_v2/cpp-headers/GrowableBuffer.h b/src/awkward/_v2/cpp-headers/awkward/GrowableBuffer.h similarity index 58% rename from src/awkward/_v2/cpp-headers/GrowableBuffer.h rename to src/awkward/_v2/cpp-headers/awkward/GrowableBuffer.h index 852a20cc78..9f2eb8a9a2 100644 --- a/src/awkward/_v2/cpp-headers/GrowableBuffer.h +++ b/src/awkward/_v2/cpp-headers/awkward/GrowableBuffer.h @@ -7,8 +7,97 @@ #include #include #include +#include +#include namespace awkward { + + template + class Panel { + public: + Panel(size_t reserved) + : ptr_(std::unique_ptr(new PRIMITIVE[reserved])), + length_(0), + reserved_(reserved), + next_(nullptr) { } + + Panel(std::unique_ptr ptr, size_t length, size_t reserved) + : ptr_(std::move(ptr)), + length_(length), + reserved_(reserved), + next_(nullptr) { + } + + PRIMITIVE &operator[](size_t i) { + return ptr_.get()[i]; + } + + Panel* + append_panel(size_t reserved) { + next_ = std::move(std::unique_ptr( + new Panel(reserved))); + return next_.get(); + } + + /// @brief Inserts one `datum` into the panel. + void + fill_panel(PRIMITIVE datum) { + ptr_.get()[length_++] = datum; + } + + std::unique_ptr& + next() { + return next_; + } + + size_t + length() { + if (next_) { + return next_->length() + length_; + } + else { + return length_; + } + } + + size_t + reserved() { + return reserved_; + } + + void + concatenate_to(PRIMITIVE* to_ptr, size_t offset) const noexcept { + memcpy(to_ptr + offset, reinterpret_cast(ptr_.get()), length_ * sizeof(PRIMITIVE)); + if (next_) { + next_->concatenate_to(to_ptr, offset + length_); + } + } + + template + void + copy_as(TO_PRIMITIVE* to_ptr, size_t offset) { + for (size_t i = 0; i < length_; i++) { + to_ptr[offset++] = static_cast(ptr_.get()[i]); + } + if (next_) { + next_->copy_as(to_ptr, offset); + } + } + + private: + /// @brief Unique pointer to the panel data. + std::unique_ptr 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 next_; + }; + /// @class GrowableBuffer /// /// @brief Discontiguous, one-dimensional buffer which consists of @@ -28,7 +117,7 @@ namespace awkward { /// /// @param initial Initial size configuration for building a panel. static GrowableBuffer - empty(size_t initial) { + empty(int64_t initial) { return empty(initial, 0); } @@ -38,13 +127,13 @@ namespace awkward { /// @param minreserve The initial reservation will be the maximum /// of `minreserve` and #initial. static GrowableBuffer - empty(size_t initial, size_t minreserve) { - size_t actual = initial; + empty(int64_t initial, int64_t minreserve) { + int64_t actual = initial; if (actual < minreserve) { actual = minreserve; } return GrowableBuffer(initial, - std::unique_ptr(new PRIMITIVE[actual]), + std::unique_ptr(new PRIMITIVE[actual]), 0, actual); } @@ -57,14 +146,14 @@ namespace awkward { /// This is similar to NumPy's /// [zeros](https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html). static GrowableBuffer - zeros(size_t initial, size_t length) { - size_t actual = initial; + zeros(int64_t initial, int64_t length) { + int64_t actual = initial; if (actual < length) { actual = length; } - auto ptr = std::unique_ptr(new PRIMITIVE[actual]); + auto ptr = std::unique_ptr(new PRIMITIVE[actual]); PRIMITIVE* rawptr = ptr.get(); - for (size_t i = 0; i < length; i++) { + for (int64_t i = 0; i < length; i++) { rawptr[i] = 0; } return GrowableBuffer(initial, std::move(ptr), length, actual); @@ -81,14 +170,14 @@ namespace awkward { /// This is similar to NumPy's /// [full](https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html). static GrowableBuffer - full(size_t initial, PRIMITIVE value, size_t length) { - size_t actual = initial; + full(int64_t initial, PRIMITIVE value, int64_t length) { + int64_t actual = initial; if (actual < length) { actual = length; } - auto ptr = std::unique_ptr(new PRIMITIVE[actual]); + auto ptr = std::unique_ptr(new PRIMITIVE[actual]); PRIMITIVE* rawptr = ptr.get(); - for (size_t i = 0; i < length; i++) { + for (int64_t i = 0; i < length; i++) { rawptr[i] = value; } return GrowableBuffer(initial, std::move(ptr), length, actual); @@ -104,19 +193,38 @@ namespace awkward { /// This is similar to NumPy's /// [arange](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html). static GrowableBuffer - arange(size_t initial, size_t length) { - size_t actual = initial; + arange(int64_t initial, int64_t length) { + int64_t actual = initial; if (actual < length) { actual = length; } - auto ptr = std::unique_ptr(new PRIMITIVE[actual]); + auto ptr = std::unique_ptr(new PRIMITIVE[actual]); PRIMITIVE* rawptr = ptr.get(); - for (size_t i = 0; i < length; i++) { + for (int64_t i = 0; i < length; i++) { rawptr[i] = (PRIMITIVE)i; } return GrowableBuffer(initial, std::move(ptr), length, actual); } + /// @brief Takes a (possibly multi-panels) GrowableBuffer + /// and makes another (one panel) GrowableBuffer. + /// + /// Used to change the data type of buffer content from `PRIMITIVE` + /// to `TO_PRIMITIVE` for building arrays. + template + static GrowableBuffer + copy_as(const GrowableBuffer& other) { + auto len = other.length(); + int64_t actual = (len < other.initial_) ? other.initial_ : len; + + auto ptr = std::unique_ptr(new TO_PRIMITIVE[actual]); + TO_PRIMITIVE* rawptr = ptr.get(); + + other.panel_->copy_as(rawptr, 0); + + return GrowableBuffer(actual, std::move(ptr), len, actual); + } + /// @brief Creates a GrowableBuffer from a full set of parameters. /// /// @param initial Initial size configuration for building a panel. @@ -127,54 +235,47 @@ namespace awkward { /// 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(size_t initial, - std::unique_ptr ptr, - size_t length, - size_t reserved) - : initial_(initial) { - ptr_.push_back(std::move(ptr)); - length_.push_back(length); - reserved_.push_back(reserved); + GrowableBuffer(int64_t initial, + std::unique_ptr ptr, + int64_t length, + int64_t reserved) + : initial_(initial), + panel_(std::unique_ptr>(new Panel(std::move(ptr), (size_t)length, (size_t)reserved))), + ptr_(panel_.get()) { } /// @brief Creates a GrowableBuffer by allocating a new buffer, taking an /// initial #reserved from #initial. - GrowableBuffer(size_t initial) + GrowableBuffer(int64_t initial) : GrowableBuffer(initial, - std::unique_ptr(new PRIMITIVE[initial]), + std::unique_ptr(new PRIMITIVE[initial]), 0, initial) { } - /// @brief Currently used number of elements. + /// @brief Move constructor /// - /// 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 std::accumulate(length_.begin(), length_.end(), (size_t)0); - } + /// panel_ is move-only + GrowableBuffer(GrowableBuffer&& other) noexcept + : initial_(other.initial_), + panel_(std::move(other.panel_)), + ptr_(other.ptr_) { } - /// @brief Currently allocated number of elements. + /// @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 - reserved() const { - return std::accumulate(reserved_.begin(), reserved_.end(), (size_t)0); + int64_t + length() const { + return (int64_t)panel_->length(); } /// @brief Discards accumulated data, the #reserved returns to /// initial, and a new #ptr is allocated. void clear() { - length_.clear(); - length_.push_back(0); - reserved_.clear(); - reserved_.push_back(initial_); - ptr_.clear(); - ptr_.push_back(std::unique_ptr(new PRIMITIVE[initial_])); + panel_ = std::move(std::unique_ptr>(new Panel((size_t)initial_))); + ptr_ = panel_.get(); } /// @brief Inserts one `datum` into the panel, possibly triggering @@ -184,8 +285,8 @@ namespace awkward { /// #reserved, a new panel will be allocated. void append(PRIMITIVE datum) { - if (length_[ptr_.size()-1] == reserved_[ptr_.size()-1]) { - add_panel(reserved_[ptr_.size()-1]); + if (ptr_->length() == ptr_->reserved()) { + add_panel((size_t)ceil(ptr_->reserved() * 1.5)); } fill_panel(datum); } @@ -198,13 +299,13 @@ namespace awkward { /// for the rest of the array elements. void extend(PRIMITIVE* ptr, size_t size) { - size_t empty_slots = reserved_[ptr_.size()-1] - length_[ptr_.size()-1]; + size_t empty_slots = ptr_->reserved() - ptr_->length(); if (size > empty_slots) { for (size_t i = 0; i < empty_slots; i++) { fill_panel(ptr[i]); } - add_panel(size - empty_slots > reserved_[ptr_.size()-1] ? - size - empty_slots : reserved_[ptr_.size()-1]); + add_panel(size - empty_slots > ptr_->reserved() ? + size - empty_slots : ptr_->reserved()); for (size_t i = empty_slots; i < size; i++) { fill_panel(ptr[i]); } @@ -219,78 +320,40 @@ namespace awkward { /// @brief Like append, but the type signature returns the reference to `PRIMITIVE`. PRIMITIVE& append_and_get_ref(PRIMITIVE datum) { append(datum); - return (&*ptr_.back())[length_.back()]; + return (*ptr_)[ptr_->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 { - size_t next_panel = 0; - for (size_t i = 0; i < ptr_.size(); i++) { - memcpy(external_pointer + next_panel, reinterpret_cast(ptr_[i].get()), length_[i]*sizeof(PRIMITIVE)); - next_panel += length_[i]; + if (external_pointer) { + panel_->concatenate_to(external_pointer, 0); } } - /// @brief Checks whether the array is contiguous. - size_t is_contiguous() { - return (ptr_.size() == 1); - } - - /// @brief Takes this (possibly multi-panels) GrowableBuffer - /// and makes another (one panel) GrowableBuffer. - /// - /// Used to change the data type of buffer content from `PRIMITIVE` - /// to `TO_PRIMITIVE` for building arrays. - template - GrowableBuffer - copy_as() { - auto len = length(); - auto ptr = std::unique_ptr(new TO_PRIMITIVE[len]); - TO_PRIMITIVE* rawptr = ptr.get(); - int64_t k = 0; - for (size_t i = 0; i < ptr_.size(); i++) { - for (int64_t j = 0; j < length_[i]; j++) { - rawptr[k] = static_cast(ptr_[i].get()[j]); - k++; - } - } - return GrowableBuffer(len, std::move(ptr), len, len); - } private: /// @brief Inserts one `datum` into the panel. void fill_panel(PRIMITIVE datum) { - ptr_[ptr_.size()-1].get()[length_[ptr_.size()-1]] = datum; - length_[ptr_.size()-1]++; + ptr_->fill_panel(datum); } /// @brief Creates a new panel with slots equal to #reserved. + /// and updates the current panel pointer to it. void add_panel(size_t reserved) { - ptr_.push_back(std::unique_ptr(new PRIMITIVE[reserved])); - length_.push_back(0); - reserved_.push_back(reserved); + ptr_ = ptr_->append_panel(reserved); } /// @brief Initial size configuration for building a panel. - size_t initial_; - - /// @brief Vector of unique pointers to the panels. - std::vector> ptr_; + int64_t initial_; - /// @brief Vector containing the lengths of the panels. - /// - /// Each index of this vector is aligned with the index of the - /// vector of unique pointers to the panels. - std::vector length_; + /// @brief The first panel. + std::unique_ptr> panel_; - /// @brief Vector containing the reserved sizes of the panels. - /// - /// Each index of this vector is aligned with the index of the - /// vector of unique pointers to the panels. - std::vector reserved_; + /// @brief A pointer to a current panel. + Panel* ptr_; }; } diff --git a/src/libawkward/builder/ArrayBuilder.cpp b/src/libawkward/builder/ArrayBuilder.cpp index a3b34b2b03..333eb1994b 100644 --- a/src/libawkward/builder/ArrayBuilder.cpp +++ b/src/libawkward/builder/ArrayBuilder.cpp @@ -7,15 +7,14 @@ #include "awkward/common.h" #include "awkward/Content.h" -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/Builder.h" #include "awkward/builder/UnknownBuilder.h" #include "awkward/builder/ArrayBuilder.h" namespace awkward { - ArrayBuilder::ArrayBuilder(const ArrayBuilderOptions& options) - : builder_(UnknownBuilder::fromempty(options)) { } + ArrayBuilder::ArrayBuilder(const int64_t initial) + : builder_(UnknownBuilder::fromempty(initial)) { } const std::string ArrayBuilder::to_buffers(BuffersContainer& container, int64_t& form_key_id) const { diff --git a/src/libawkward/builder/ArrayBuilderOptions.cpp b/src/libawkward/builder/ArrayBuilderOptions.cpp deleted file mode 100644 index 804dc9a190..0000000000 --- a/src/libawkward/builder/ArrayBuilderOptions.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE - -#include "awkward/builder/ArrayBuilderOptions.h" - -namespace awkward { - ArrayBuilderOptions::ArrayBuilderOptions(int64_t initial, double resize) - : initial_(initial) - , resize_(resize) { } - - int64_t - ArrayBuilderOptions::initial() const { - return initial_; - } - - double - ArrayBuilderOptions::resize() const { - return resize_; - } -} diff --git a/src/libawkward/builder/BoolBuilder.cpp b/src/libawkward/builder/BoolBuilder.cpp index 99e38b57f3..76144355a9 100644 --- a/src/libawkward/builder/BoolBuilder.cpp +++ b/src/libawkward/builder/BoolBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/UnionBuilder.h" @@ -12,14 +11,14 @@ namespace awkward { const BuilderPtr - BoolBuilder::fromempty(const ArrayBuilderOptions& options) { - return std::make_shared(options, - std::move(GrowableBuffer::empty(options))); + BoolBuilder::fromempty(const int64_t initial) { + return std::make_shared(initial, + std::move(GrowableBuffer::empty(initial))); } - BoolBuilder::BoolBuilder(const ArrayBuilderOptions& options, + BoolBuilder::BoolBuilder(const int64_t initial, GrowableBuffer buffer) - : options_(options) + : initial_(initial) , buffer_(std::move(buffer)) { } const std::string @@ -32,9 +31,10 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-data", - buffer_.ptr().get(), - (int64_t)(buffer_.length() * sizeof(bool))); + buffer_.concatenate( + reinterpret_cast( + container.empty_buffer(form_key.str() + "-data", + buffer_.length() * (int64_t)sizeof(bool)))); return "{\"class\": \"NumpyArray\", \"primitive\": \"bool\", \"form_key\": \"" + form_key.str() + "\"}"; @@ -57,7 +57,7 @@ namespace awkward { const BuilderPtr BoolBuilder::null() { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } @@ -70,49 +70,49 @@ namespace awkward { const BuilderPtr BoolBuilder::integer(int64_t x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->integer(x); return std::move(out); } const BuilderPtr BoolBuilder::real(double x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->real(x); return std::move(out); } const BuilderPtr BoolBuilder::complex(std::complex x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->complex(x); return std::move(out); } const BuilderPtr BoolBuilder::datetime(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } const BuilderPtr BoolBuilder::timedelta(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } const BuilderPtr BoolBuilder::string(const char* x, int64_t length, const char* encoding) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } const BuilderPtr BoolBuilder::beginlist() { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -126,7 +126,7 @@ namespace awkward { const BuilderPtr BoolBuilder::begintuple(int64_t numfields) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -147,7 +147,7 @@ namespace awkward { const BuilderPtr BoolBuilder::beginrecord(const char* name, bool check) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/Complex128Builder.cpp b/src/libawkward/builder/Complex128Builder.cpp index 56e6b3bfa2..6690a01992 100644 --- a/src/libawkward/builder/Complex128Builder.cpp +++ b/src/libawkward/builder/Complex128Builder.cpp @@ -11,44 +11,30 @@ namespace awkward { const BuilderPtr - Complex128Builder::fromempty(const ArrayBuilderOptions& options) { - return std::make_shared(options, - GrowableBuffer>::empty(options)); - } - - const BuilderPtr - Complex128Builder::fromint64(const ArrayBuilderOptions& options, - GrowableBuffer old) { - GrowableBuffer> buffer = - GrowableBuffer>::empty(options, old.reserved()); - int64_t* oldraw = old.ptr().get(); - std::complex* newraw = buffer.ptr().get(); - for (size_t i = 0; i < 2*old.length(); i++) { - newraw[i] = {static_cast(oldraw[i]), 0}; - } - buffer.set_length(old.length()); - old.clear(); - return std::make_shared(options, std::move(buffer)); - } - - const BuilderPtr - Complex128Builder::fromfloat64(const ArrayBuilderOptions& options, - GrowableBuffer old) { - GrowableBuffer> buffer = - GrowableBuffer>::empty(options, old.reserved()); - double* oldraw = old.ptr().get(); - std::complex* newraw = buffer.ptr().get(); - for (size_t i = 0; i < old.length(); i++) { - newraw[i] = std::complex(oldraw[i], 0); - } - buffer.set_length(old.length()); - old.clear(); - return std::make_shared(options, std::move(buffer)); - } - - Complex128Builder::Complex128Builder(const ArrayBuilderOptions& options, + Complex128Builder::fromempty(const int64_t initial) { + return std::make_shared(initial, + GrowableBuffer>::empty(initial)); + } + + const BuilderPtr + Complex128Builder::fromint64(const int64_t initial, + const GrowableBuffer& old) { + return std::make_shared( + initial, + std::move(GrowableBuffer::copy_as>(old))); + } + + const BuilderPtr + Complex128Builder::fromfloat64(const int64_t initial, + const GrowableBuffer& old) { + return std::make_shared( + initial, + std::move(GrowableBuffer::copy_as>(old))); + } + + Complex128Builder::Complex128Builder(const int64_t initial, GrowableBuffer> buffer) - : options_(options) + : initial_(initial) , buffer_(std::move(buffer)) { } const std::string @@ -61,9 +47,10 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-data", - buffer_.ptr().get(), - (int64_t)(buffer_.length() * sizeof(double)) * 2); + void* ptr = container.empty_buffer(form_key.str() + "-data", + buffer_.length() * (int64_t)sizeof(std::complex)); + + buffer_.concatenate(reinterpret_cast*>(ptr)); return "{\"class\": \"NumpyArray\", \"primitive\": \"complex128\", \"form_key\": \"" + form_key.str() + "\"}"; @@ -86,14 +73,14 @@ namespace awkward { const BuilderPtr Complex128Builder::null() { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } const BuilderPtr Complex128Builder::boolean(bool x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } @@ -118,28 +105,28 @@ namespace awkward { const BuilderPtr Complex128Builder::datetime(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } const BuilderPtr Complex128Builder::timedelta(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } const BuilderPtr Complex128Builder::string(const char* x, int64_t length, const char* encoding) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } const BuilderPtr Complex128Builder::beginlist() { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -153,7 +140,7 @@ namespace awkward { const BuilderPtr Complex128Builder::begintuple(int64_t numfields) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -174,7 +161,7 @@ namespace awkward { const BuilderPtr Complex128Builder::beginrecord(const char* name, bool check) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/DatetimeBuilder.cpp b/src/libawkward/builder/DatetimeBuilder.cpp index 7e06b87444..436649ec87 100644 --- a/src/libawkward/builder/DatetimeBuilder.cpp +++ b/src/libawkward/builder/DatetimeBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/Complex128Builder.h" #include "awkward/builder/Float64Builder.h" #include "awkward/builder/OptionBuilder.h" @@ -16,17 +15,17 @@ namespace awkward { const BuilderPtr - DatetimeBuilder::fromempty(const ArrayBuilderOptions& options, const std::string& units) { - GrowableBuffer content = GrowableBuffer::empty(options); - return std::make_shared(options, + DatetimeBuilder::fromempty(const int64_t initial, const std::string& units) { + GrowableBuffer content = GrowableBuffer::empty(initial); + return std::make_shared(initial, std::move(content), units); } - DatetimeBuilder::DatetimeBuilder(const ArrayBuilderOptions& options, + DatetimeBuilder::DatetimeBuilder(const int64_t initial, GrowableBuffer content, const std::string& units) - : options_(options) + : initial_(initial) , content_(std::move(content)) , units_(units) { } @@ -40,9 +39,10 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-data", - content_.ptr().get(), - (int64_t)(content_.length() * sizeof(int64_t))); + content_.concatenate( + reinterpret_cast( + container.empty_buffer(form_key.str() + "-data", + content_.length() * (int64_t)sizeof(int64_t)))); std::string primitive(units_); @@ -82,35 +82,35 @@ namespace awkward { const BuilderPtr DatetimeBuilder::null() { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } const BuilderPtr DatetimeBuilder::boolean(bool x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } const BuilderPtr DatetimeBuilder::integer(int64_t x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->integer(x); return std::move(out); } const BuilderPtr DatetimeBuilder::real(double x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->real(x); return std::move(out); } const BuilderPtr DatetimeBuilder::complex(std::complex x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->complex(x); return std::move(out); } @@ -122,7 +122,7 @@ namespace awkward { return nullptr; } else { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } @@ -135,7 +135,7 @@ namespace awkward { return nullptr; } else { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } @@ -143,14 +143,14 @@ namespace awkward { const BuilderPtr DatetimeBuilder::string(const char* x, int64_t length, const char* encoding) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } const BuilderPtr DatetimeBuilder::beginlist() { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -164,7 +164,7 @@ namespace awkward { const BuilderPtr DatetimeBuilder::begintuple(int64_t numfields) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -185,7 +185,7 @@ namespace awkward { const BuilderPtr DatetimeBuilder::beginrecord(const char* name, bool check) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/Float64Builder.cpp b/src/libawkward/builder/Float64Builder.cpp index 7f133b0d26..556ade79c2 100644 --- a/src/libawkward/builder/Float64Builder.cpp +++ b/src/libawkward/builder/Float64Builder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/Complex128Builder.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/UnionBuilder.h" @@ -13,29 +12,22 @@ namespace awkward { const BuilderPtr - Float64Builder::fromempty(const ArrayBuilderOptions& options) { - return std::make_shared(options, - GrowableBuffer::empty(options)); + Float64Builder::fromempty(const int64_t initial) { + return std::make_shared(initial, + GrowableBuffer::empty(initial)); } const BuilderPtr - Float64Builder::fromint64(const ArrayBuilderOptions& options, - GrowableBuffer old) { - GrowableBuffer buffer = - GrowableBuffer::empty(options, old.reserved()); - int64_t* oldraw = old.ptr().get(); - double* newraw = buffer.ptr().get(); - for (size_t i = 0; i < old.length(); i++) { - newraw[i] = (double)oldraw[i]; - } - buffer.set_length(old.length()); - old.clear(); - return std::make_shared(options, std::move(buffer)); + Float64Builder::fromint64(const int64_t initial, + const GrowableBuffer& old) { + return std::make_shared( + initial, + std::move(GrowableBuffer::copy_as(old))); } - Float64Builder::Float64Builder(const ArrayBuilderOptions& options, + Float64Builder::Float64Builder(const int64_t initial, GrowableBuffer buffer) - : options_(options) + : initial_(initial) , buffer_(std::move(buffer)) { } GrowableBuffer @@ -54,9 +46,10 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-data", - buffer_.ptr().get(), - (int64_t)(buffer_.length() * sizeof(double))); + buffer_.concatenate( + reinterpret_cast( + container.empty_buffer(form_key.str() + "-data", + buffer_.length() * (int64_t)sizeof(double)))); return "{\"class\": \"NumpyArray\", \"primitive\": \"float64\", \"form_key\": \"" + form_key.str() + "\"}"; @@ -79,14 +72,14 @@ namespace awkward { const BuilderPtr Float64Builder::null() { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } const BuilderPtr Float64Builder::boolean(bool x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } @@ -105,35 +98,35 @@ namespace awkward { const BuilderPtr Float64Builder::complex(std::complex x) { - BuilderPtr out = Complex128Builder::fromfloat64(options_, std::move(buffer_)); + BuilderPtr out = Complex128Builder::fromfloat64(initial_, std::move(buffer_)); out.get()->complex(x); return std::move(out); } const BuilderPtr Float64Builder::datetime(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } const BuilderPtr Float64Builder::timedelta(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } const BuilderPtr Float64Builder::string(const char* x, int64_t length, const char* encoding) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } const BuilderPtr Float64Builder::beginlist() { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -147,7 +140,7 @@ namespace awkward { const BuilderPtr Float64Builder::begintuple(int64_t numfields) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -168,7 +161,7 @@ namespace awkward { const BuilderPtr Float64Builder::beginrecord(const char* name, bool check) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/GrowableBuffer.cpp b/src/libawkward/builder/GrowableBuffer.cpp deleted file mode 100644 index a4d373723b..0000000000 --- a/src/libawkward/builder/GrowableBuffer.cpp +++ /dev/null @@ -1,164 +0,0 @@ -// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE - -#include "awkward/builder/ArrayBuilderOptions.h" - -#include "awkward/builder/GrowableBuffer.h" -#include "awkward/kernel-utils.h" - -#include -#include -#include - -namespace awkward { - template - GrowableBuffer - GrowableBuffer::empty(const ArrayBuilderOptions& options) { - return GrowableBuffer::empty(options, 0); - } - - template - GrowableBuffer - GrowableBuffer::empty(const ArrayBuilderOptions& options, - size_t minreserve) { - size_t actual = (size_t)options.initial(); - if (actual < (size_t)minreserve) { - actual = (size_t)minreserve; - } - return GrowableBuffer(options, - UniquePtr(reinterpret_cast(awkward_malloc((int64_t)(actual*sizeof(T))))), - 0, actual); - } - - template - GrowableBuffer - GrowableBuffer::full(const ArrayBuilderOptions& options, - T value, - size_t length) { - size_t actual = (size_t)options.initial(); - if (actual < length) { - actual = length; - } - UniquePtr ptr(reinterpret_cast(awkward_malloc((int64_t)(actual*sizeof(T))))); - T* rawptr = ptr.get(); - for (size_t i = 0; i < length; i++) { - rawptr[i] = value; - } - return GrowableBuffer(options, std::move(ptr), length, actual); - } - - template - GrowableBuffer - GrowableBuffer::arange(const ArrayBuilderOptions& options, - size_t length) { - size_t actual = (size_t)options.initial(); - if (actual < length) { - actual = length; - } - UniquePtr ptr(reinterpret_cast(awkward_malloc((int64_t)(actual*sizeof(T))))); - T* rawptr = ptr.get(); - for (size_t i = 0; i < length; i++) { - rawptr[i] = (T)i; - } - return GrowableBuffer(options, std::move(ptr), length, actual); - } - - template - GrowableBuffer::GrowableBuffer(const ArrayBuilderOptions& options, - UniquePtr ptr, - size_t length, - size_t reserved) - : options_(options) - , ptr_(std::move(ptr)) - , length_(length) - , reserved_(reserved) { } - - template - GrowableBuffer::GrowableBuffer(const ArrayBuilderOptions& options) - : GrowableBuffer(options, - UniquePtr(reinterpret_cast(awkward_malloc(options.initial()*(int64_t)sizeof(T)))), - 0, - (size_t) options.initial()) { } - - template - const typename GrowableBuffer::UniquePtr& - GrowableBuffer::ptr() const { - return ptr_; - } - - template - typename GrowableBuffer::UniquePtr - GrowableBuffer::get_ptr() { - return std::move(ptr_); - } - - template - size_t - GrowableBuffer::length() const { - return length_; - } - - template - void - GrowableBuffer::set_length(size_t newlength) { - if (newlength > reserved_) { - set_reserved(newlength); - } - length_ = newlength; - } - - template - size_t - GrowableBuffer::reserved() const { - return reserved_; - } - - template - void - GrowableBuffer::set_reserved(size_t minreserved) { - if (minreserved > reserved_) { - UniquePtr ptr(reinterpret_cast(awkward_malloc((int64_t)(minreserved*sizeof(T))))); - memcpy(ptr.get(), ptr_.get(), length_ * sizeof(T)); - ptr_ = std::move(ptr); - reserved_ = minreserved; - } - } - - template - void - GrowableBuffer::clear() { - length_ = 0; - reserved_ = (size_t) options_.initial(); - ptr_ = UniquePtr(reinterpret_cast(awkward_malloc(options_.initial()*(int64_t)sizeof(T)))); - } - - template - void - GrowableBuffer::append(T datum) { - if (length_ == reserved_) { - set_reserved((size_t)ceil(reserved_ * options_.resize())); - } - ptr_.get()[length_] = datum; - length_++; - } - - template - T - GrowableBuffer::getitem_at_nowrap(int64_t at) const { - return ptr_.get()[at]; - } - - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer; - template class EXPORT_TEMPLATE_INST GrowableBuffer>; - template class EXPORT_TEMPLATE_INST GrowableBuffer>; - -} diff --git a/src/libawkward/builder/Int64Builder.cpp b/src/libawkward/builder/Int64Builder.cpp index 8932a55abf..c4fce8bc65 100644 --- a/src/libawkward/builder/Int64Builder.cpp +++ b/src/libawkward/builder/Int64Builder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/Complex128Builder.h" #include "awkward/builder/Float64Builder.h" #include "awkward/builder/OptionBuilder.h" @@ -14,14 +13,14 @@ namespace awkward { const BuilderPtr - Int64Builder::fromempty(const ArrayBuilderOptions& options) { - return std::make_shared(options, - GrowableBuffer::empty(options)); + Int64Builder::fromempty(const int64_t initial) { + return std::make_shared(initial, + GrowableBuffer::empty(initial)); } - Int64Builder::Int64Builder(const ArrayBuilderOptions& options, + Int64Builder::Int64Builder(const int64_t initial, GrowableBuffer buffer) - : options_(options) + : initial_(initial) , buffer_(std::move(buffer)) { } GrowableBuffer @@ -40,9 +39,10 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-data", - buffer_.ptr().get(), - (int64_t)(buffer_.length() * sizeof(int64_t))); + buffer_.concatenate( + reinterpret_cast( + container.empty_buffer(form_key.str() + "-data", + buffer_.length() * (int64_t)sizeof(int64_t)))); return "{\"class\": \"NumpyArray\", \"primitive\": \"int64\", \"form_key\": \"" + form_key.str() + "\"}"; @@ -65,14 +65,14 @@ namespace awkward { const BuilderPtr Int64Builder::null() { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } const BuilderPtr Int64Builder::boolean(bool x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } @@ -85,42 +85,42 @@ namespace awkward { const BuilderPtr Int64Builder::real(double x) { - BuilderPtr out = Float64Builder::fromint64(options_, std::move(buffer_)); + BuilderPtr out = Float64Builder::fromint64(initial_, buffer_); out.get()->real(x); return std::move(out); } const BuilderPtr Int64Builder::complex(std::complex x) { - BuilderPtr out = Complex128Builder::fromint64(options_, std::move(buffer_)); + BuilderPtr out = Complex128Builder::fromint64(initial_, buffer_); out.get()->complex(x); return std::move(out); } const BuilderPtr Int64Builder::datetime(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } const BuilderPtr Int64Builder::timedelta(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } const BuilderPtr Int64Builder::string(const char* x, int64_t length, const char* encoding) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } const BuilderPtr Int64Builder::beginlist() { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -134,7 +134,7 @@ namespace awkward { const BuilderPtr Int64Builder::begintuple(int64_t numfields) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -155,7 +155,7 @@ namespace awkward { const BuilderPtr Int64Builder::beginrecord(const char* name, bool check) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/ListBuilder.cpp b/src/libawkward/builder/ListBuilder.cpp index eb8921d3a7..888e44e3e0 100644 --- a/src/libawkward/builder/ListBuilder.cpp +++ b/src/libawkward/builder/ListBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/UnionBuilder.h" #include "awkward/builder/UnknownBuilder.h" @@ -13,20 +12,20 @@ namespace awkward { const BuilderPtr - ListBuilder::fromempty(const ArrayBuilderOptions& options) { - GrowableBuffer offsets = GrowableBuffer::empty(options); + ListBuilder::fromempty(const int64_t initial) { + GrowableBuffer offsets = GrowableBuffer::empty(initial); offsets.append(0); - return std::make_shared(options, + return std::make_shared(initial, std::move(offsets), - UnknownBuilder::fromempty(options), + UnknownBuilder::fromempty(initial), false); } - ListBuilder::ListBuilder(const ArrayBuilderOptions& options, + ListBuilder::ListBuilder(const int64_t initial, GrowableBuffer offsets, const BuilderPtr& content, bool begun) - : options_(options) + : initial_(initial) , offsets_(std::move(offsets)) , content_(content) , begun_(begun) { } @@ -41,9 +40,10 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-offsets", - offsets_.ptr().get(), - (int64_t)(offsets_.length() * sizeof(int64_t))); + void* ptr = container.empty_buffer(form_key.str() + "-offsets", + offsets_.length() * (int64_t)sizeof(int64_t)); + + offsets_.concatenate(reinterpret_cast(ptr)); return "{\"class\": \"ListOffsetArray\", \"offsets\": \"i64\", \"content\": " + content_.get()->to_buffers(container, form_key_id) + ", \"form_key\": \"" @@ -70,7 +70,7 @@ namespace awkward { const BuilderPtr ListBuilder::null() { if (!begun_) { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } @@ -83,7 +83,7 @@ namespace awkward { const BuilderPtr ListBuilder::boolean(bool x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } @@ -96,7 +96,7 @@ namespace awkward { const BuilderPtr ListBuilder::integer(int64_t x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->integer(x); return std::move(out); } @@ -109,7 +109,7 @@ namespace awkward { const BuilderPtr ListBuilder::real(double x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->real(x); return std::move(out); } @@ -122,7 +122,7 @@ namespace awkward { const BuilderPtr ListBuilder::complex(std::complex x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->complex(x); return std::move(out); } @@ -135,7 +135,7 @@ namespace awkward { const BuilderPtr ListBuilder::datetime(int64_t x, const std::string& unit) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } @@ -148,7 +148,7 @@ namespace awkward { const BuilderPtr ListBuilder::timedelta(int64_t x, const std::string& unit) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } @@ -161,7 +161,7 @@ namespace awkward { const BuilderPtr ListBuilder::string(const char* x, int64_t length, const char* encoding) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } @@ -202,7 +202,7 @@ namespace awkward { const BuilderPtr ListBuilder::begintuple(int64_t numfields) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -241,7 +241,7 @@ namespace awkward { const BuilderPtr ListBuilder::beginrecord(const char* name, bool check) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/OptionBuilder.cpp b/src/libawkward/builder/OptionBuilder.cpp index 49cffbfe58..c8700854e7 100644 --- a/src/libawkward/builder/OptionBuilder.cpp +++ b/src/libawkward/builder/OptionBuilder.cpp @@ -4,36 +4,34 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" namespace awkward { const BuilderPtr - OptionBuilder::fromnulls(const ArrayBuilderOptions& options, + OptionBuilder::fromnulls(const int64_t initial, int64_t nullcount, const BuilderPtr& content) { return std::make_shared( - options, - GrowableBuffer::full(options, + initial, + GrowableBuffer::full(initial, -1, - (size_t)nullcount), + nullcount), content); } const BuilderPtr - OptionBuilder::fromvalids(const ArrayBuilderOptions& options, + OptionBuilder::fromvalids(const int64_t initial, const BuilderPtr& content) { - return std::make_shared(options, - GrowableBuffer::arange(options, (size_t)content->length()), + return std::make_shared(initial, + GrowableBuffer::arange(initial, content->length()), content); } - OptionBuilder::OptionBuilder(const ArrayBuilderOptions& options, + OptionBuilder::OptionBuilder(const int64_t initial, GrowableBuffer index, const BuilderPtr content) - : options_(options) - , index_(std::move(index)) + : index_(std::move(index)) , content_(content) { } const std::string @@ -46,9 +44,10 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-index", - index_.ptr().get(), - (int64_t)(index_.length() * sizeof(int64_t))); + void* ptr = container.empty_buffer(form_key.str() + "-index", + index_.length() * (int64_t)sizeof(int64_t)); + + index_.concatenate(reinterpret_cast(ptr)); return "{\"class\": \"IndexedOptionArray\", \"index\": \"i64\", \"content\": " + content_.get()->to_buffers(container, form_key_id) + ", \"form_key\": \"" diff --git a/src/libawkward/builder/RecordBuilder.cpp b/src/libawkward/builder/RecordBuilder.cpp index a64d03df08..cfdd728bf8 100644 --- a/src/libawkward/builder/RecordBuilder.cpp +++ b/src/libawkward/builder/RecordBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/UnionBuilder.h" #include "awkward/builder/UnknownBuilder.h" @@ -14,8 +13,8 @@ namespace awkward { const BuilderPtr - RecordBuilder::fromempty(const ArrayBuilderOptions& options) { - return std::make_shared(options, + RecordBuilder::fromempty(const int64_t initial) { + return std::make_shared(initial, std::vector(), std::vector(), std::vector(), @@ -27,7 +26,7 @@ namespace awkward { -1); } - RecordBuilder::RecordBuilder(const ArrayBuilderOptions& options, + RecordBuilder::RecordBuilder(const int64_t initial, const std::vector& contents, const std::vector& keys, const std::vector& pointers, @@ -37,7 +36,7 @@ namespace awkward { bool begun, int64_t nextindex, int64_t nexttotry) - : options_(options) + : initial_(initial) , contents_(contents) , keys_(keys) , pointers_(pointers) @@ -116,7 +115,7 @@ namespace awkward { const BuilderPtr RecordBuilder::null() { if (!begun_) { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } @@ -137,7 +136,7 @@ namespace awkward { const BuilderPtr RecordBuilder::boolean(bool x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } @@ -158,7 +157,7 @@ namespace awkward { const BuilderPtr RecordBuilder::integer(int64_t x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->integer(x); return std::move(out); } @@ -179,7 +178,7 @@ namespace awkward { const BuilderPtr RecordBuilder::real(double x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->real(x); return std::move(out); } @@ -200,7 +199,7 @@ namespace awkward { const BuilderPtr RecordBuilder::complex(std::complex x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->complex(x); return std::move(out); } @@ -221,7 +220,7 @@ namespace awkward { const BuilderPtr RecordBuilder::datetime(int64_t x, const std::string& unit) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } @@ -242,7 +241,7 @@ namespace awkward { const BuilderPtr RecordBuilder::timedelta(int64_t x, const std::string& unit) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } @@ -263,7 +262,7 @@ namespace awkward { const BuilderPtr RecordBuilder::string(const char* x, int64_t length, const char* encoding) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } @@ -287,7 +286,7 @@ namespace awkward { const BuilderPtr RecordBuilder::beginlist() { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -328,7 +327,7 @@ namespace awkward { const BuilderPtr RecordBuilder::begintuple(int64_t numfields) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -406,7 +405,7 @@ namespace awkward { nexttotry_ = 0; } else if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } @@ -464,13 +463,13 @@ namespace awkward { nextindex_ = keys_size_; nexttotry_ = 0; if (length_ == 0) { - contents_.push_back(UnknownBuilder::fromempty(options_)); + contents_.push_back(UnknownBuilder::fromempty(initial_)); } else { contents_.push_back( - OptionBuilder::fromnulls(options_, + OptionBuilder::fromnulls(initial_, length_, - UnknownBuilder::fromempty(options_))); + UnknownBuilder::fromempty(initial_))); } keys_.push_back(std::string(key)); pointers_.push_back(key); @@ -510,13 +509,13 @@ namespace awkward { nextindex_ = keys_size_; nexttotry_ = 0; if (length_ == 0) { - contents_.emplace_back(UnknownBuilder::fromempty(options_)); + contents_.emplace_back(UnknownBuilder::fromempty(initial_)); } else { contents_.emplace_back( - OptionBuilder::fromnulls(options_, + OptionBuilder::fromnulls(initial_, length_, - UnknownBuilder::fromempty(options_))); + UnknownBuilder::fromempty(initial_))); } keys_.emplace_back(std::string(key)); pointers_.emplace_back(nullptr); diff --git a/src/libawkward/builder/StringBuilder.cpp b/src/libawkward/builder/StringBuilder.cpp index 0560415840..3f20f019f4 100644 --- a/src/libawkward/builder/StringBuilder.cpp +++ b/src/libawkward/builder/StringBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/UnionBuilder.h" #include "awkward/util.h" @@ -13,22 +12,22 @@ namespace awkward { const BuilderPtr - StringBuilder::fromempty(const ArrayBuilderOptions& options, + StringBuilder::fromempty(const int64_t initial, const char* encoding) { - GrowableBuffer offsets = GrowableBuffer::empty(options); + GrowableBuffer offsets = GrowableBuffer::empty(initial); offsets.append(0); - GrowableBuffer content = GrowableBuffer::empty(options); - return std::make_shared(options, + GrowableBuffer content = GrowableBuffer::empty(initial); + return std::make_shared(initial, std::move(offsets), std::move(content), encoding); } - StringBuilder::StringBuilder(const ArrayBuilderOptions& options, + StringBuilder::StringBuilder(const int64_t initial, GrowableBuffer offsets, GrowableBuffer content, const char* encoding) - : options_(options) + : initial_(initial) , offsets_(std::move(offsets)) , content_(std::move(content)) , encoding_(encoding) { } @@ -50,13 +49,15 @@ namespace awkward { outer_form_key << "node" << (form_key_id++); inner_form_key << "node" << (form_key_id++); - container.copy_buffer(outer_form_key.str() + "-offsets", - offsets_.ptr().get(), - (int64_t)(offsets_.length() * sizeof(int64_t))); + offsets_.concatenate( + reinterpret_cast( + container.empty_buffer(outer_form_key.str() + "-offsets", + offsets_.length() * (int64_t)sizeof(int64_t)))); - container.copy_buffer(inner_form_key.str() + "-data", - content_.ptr().get(), - (int64_t)(content_.length() * sizeof(uint8_t))); + content_.concatenate( + reinterpret_cast( + container.empty_buffer(inner_form_key.str() + "-data", + content_.length() * (int64_t)sizeof(uint8_t)))); std::string char_parameter; std::string string_parameter; @@ -99,49 +100,49 @@ namespace awkward { const BuilderPtr StringBuilder::null() { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } const BuilderPtr StringBuilder::boolean(bool x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } const BuilderPtr StringBuilder::integer(int64_t x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->integer(x); return std::move(out); } const BuilderPtr StringBuilder::real(double x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->real(x); return std::move(out); } const BuilderPtr StringBuilder::complex(std::complex x) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->complex(x); return std::move(out); } const BuilderPtr StringBuilder::datetime(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } const BuilderPtr StringBuilder::timedelta(int64_t x, const std::string& unit) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } @@ -164,7 +165,7 @@ namespace awkward { const BuilderPtr StringBuilder::beginlist() { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -178,7 +179,7 @@ namespace awkward { const BuilderPtr StringBuilder::begintuple(int64_t numfields) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -199,7 +200,7 @@ namespace awkward { const BuilderPtr StringBuilder::beginrecord(const char* name, bool check) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/TupleBuilder.cpp b/src/libawkward/builder/TupleBuilder.cpp index f72470137a..cc063fa996 100644 --- a/src/libawkward/builder/TupleBuilder.cpp +++ b/src/libawkward/builder/TupleBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/UnionBuilder.h" #include "awkward/builder/UnknownBuilder.h" @@ -13,20 +12,20 @@ namespace awkward { const BuilderPtr - TupleBuilder::fromempty(const ArrayBuilderOptions& options) { - return std::make_shared(options, + TupleBuilder::fromempty(const int64_t initial) { + return std::make_shared(initial, std::vector(), -1, false, -1); } - TupleBuilder::TupleBuilder(const ArrayBuilderOptions& options, + TupleBuilder::TupleBuilder(const int64_t initial, const std::vector& contents, int64_t length, bool begun, size_t nextindex) - : options_(options) + : initial_(initial) , contents_(contents) , length_(length) , begun_(begun) @@ -82,7 +81,7 @@ namespace awkward { const BuilderPtr TupleBuilder::null() { if (!begun_) { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } @@ -103,7 +102,7 @@ namespace awkward { const BuilderPtr TupleBuilder::boolean(bool x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->boolean(x); return std::move(out); } @@ -124,7 +123,7 @@ namespace awkward { const BuilderPtr TupleBuilder::integer(int64_t x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->integer(x); return std::move(out); } @@ -145,7 +144,7 @@ namespace awkward { const BuilderPtr TupleBuilder::real(double x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->real(x); return std::move(out); } @@ -166,7 +165,7 @@ namespace awkward { const BuilderPtr TupleBuilder::complex(std::complex x) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->complex(x); return std::move(out); } @@ -187,7 +186,7 @@ namespace awkward { const BuilderPtr TupleBuilder::datetime(int64_t x, const std::string& unit) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->datetime(x, unit); return std::move(out); } @@ -208,7 +207,7 @@ namespace awkward { const BuilderPtr TupleBuilder::timedelta(int64_t x, const std::string& unit) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->timedelta(x, unit); return std::move(out); } @@ -229,7 +228,7 @@ namespace awkward { const BuilderPtr TupleBuilder::string(const char* x, int64_t length, const char* encoding) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->string(x, length, encoding); return std::move(out); } @@ -253,7 +252,7 @@ namespace awkward { const BuilderPtr TupleBuilder::beginlist() { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginlist(); return std::move(out); } @@ -295,7 +294,7 @@ namespace awkward { TupleBuilder::begintuple(int64_t numfields) { if (length_ == -1) { for (int64_t i = 0; i < numfields; i++) { - contents_.push_back(BuilderPtr(UnknownBuilder::fromempty(options_))); + contents_.push_back(BuilderPtr(UnknownBuilder::fromempty(initial_))); } length_ = 0; } @@ -305,7 +304,7 @@ namespace awkward { nextindex_ = -1; } else if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->begintuple(numfields); return std::move(out); } @@ -381,7 +380,7 @@ namespace awkward { const BuilderPtr TupleBuilder::beginrecord(const char* name, bool check) { if (!begun_) { - BuilderPtr out = UnionBuilder::fromsingle(options_, shared_from_this()); + BuilderPtr out = UnionBuilder::fromsingle(initial_, shared_from_this()); out.get()->beginrecord(name, check); return std::move(out); } diff --git a/src/libawkward/builder/UnionBuilder.cpp b/src/libawkward/builder/UnionBuilder.cpp index 9d3ee159f2..c9e1d3c337 100644 --- a/src/libawkward/builder/UnionBuilder.cpp +++ b/src/libawkward/builder/UnionBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/BoolBuilder.h" #include "awkward/builder/DatetimeBuilder.h" @@ -20,20 +19,20 @@ namespace awkward { const BuilderPtr - UnionBuilder::fromsingle(const ArrayBuilderOptions& options, + UnionBuilder::fromsingle(const int64_t initial, const BuilderPtr& firstcontent) { std::vector contents({ firstcontent }); - return std::make_shared(options, - GrowableBuffer::full(options, 0, (size_t)firstcontent->length()), - GrowableBuffer::arange(options, (size_t)firstcontent->length()), + return std::make_shared(initial, + GrowableBuffer::full(initial, 0, firstcontent->length()), + GrowableBuffer::arange(initial, firstcontent->length()), contents); } - UnionBuilder::UnionBuilder(const ArrayBuilderOptions& options, + UnionBuilder::UnionBuilder(const int64_t initial, GrowableBuffer tags, GrowableBuffer index, std::vector& contents) - : options_(options) + : initial_(initial) , tags_(std::move(tags)) , index_(std::move(index)) , contents_(contents) @@ -49,13 +48,15 @@ namespace awkward { std::stringstream form_key; form_key << "node" << (form_key_id++); - container.copy_buffer(form_key.str() + "-tags", - tags_.ptr().get(), - (int64_t)(tags_.length() * sizeof(int8_t))); + tags_.concatenate( + reinterpret_cast( + container.empty_buffer(form_key.str() + "-tags", + tags_.length() * (int64_t)sizeof(int8_t)))); - container.copy_buffer(form_key.str() + "-index", - index_.ptr().get(), - (int64_t)(index_.length() * sizeof(int64_t))); + index_.concatenate( + reinterpret_cast( + container.empty_buffer(form_key.str() + "-index", + index_.length() * (int64_t)sizeof(int64_t)))); std::stringstream out; out << "{\"class\": \"UnionArray\", \"tags\": \"i8\", \"index\": \"i64\", \"contents\": ["; @@ -91,7 +92,7 @@ namespace awkward { const BuilderPtr UnionBuilder::null() { if (current_ == -1) { - BuilderPtr out = OptionBuilder::fromvalids(options_, shared_from_this()); + BuilderPtr out = OptionBuilder::fromvalids(initial_, shared_from_this()); out.get()->null(); return std::move(out); } @@ -108,7 +109,7 @@ namespace awkward { return dynamic_cast(p.get()); }); if (tofill == contents_.end()) { - contents_.emplace_back(BoolBuilder::fromempty(options_)); + contents_.emplace_back(BoolBuilder::fromempty(initial_)); tofill = --contents_.end(); } int8_t i = (int8_t)std::distance(contents_.begin(), tofill); @@ -130,7 +131,7 @@ namespace awkward { return dynamic_cast(p.get()); }); if (tofill == contents_.end()) { - contents_.emplace_back(Int64Builder::fromempty(options_)); + contents_.emplace_back(Int64Builder::fromempty(initial_)); tofill = --contents_.end(); } int8_t i = (int8_t)std::distance(contents_.begin(), tofill); @@ -157,11 +158,11 @@ namespace awkward { }); if (tofill != contents_.end()) { *tofill = Float64Builder::fromint64( - options_, + initial_, std::move(static_cast(tofill->get())->buffer())); } else { - contents_.emplace_back(Float64Builder::fromempty(options_)); + contents_.emplace_back(Float64Builder::fromempty(initial_)); tofill = --contents_.end(); } } @@ -189,7 +190,7 @@ namespace awkward { }); if (tofill != contents_.end()) { *tofill = std::move(Complex128Builder::fromfloat64( - options_, + initial_, std::move(static_cast(tofill->get())->buffer()))); } } @@ -199,11 +200,11 @@ namespace awkward { }); if (tofill != contents_.end()) { *tofill = std::move(Complex128Builder::fromint64( - options_, + initial_, std::move(static_cast(tofill->get())->buffer()))); } else { - contents_.emplace_back(Complex128Builder::fromempty(options_)); + contents_.emplace_back(Complex128Builder::fromempty(initial_)); tofill = --contents_.end(); } } @@ -227,7 +228,7 @@ namespace awkward { return raw != 0 && raw->units() == unit; }); if (tofill == contents_.end()) { - contents_.emplace_back(DatetimeBuilder::fromempty(options_, unit)); + contents_.emplace_back(DatetimeBuilder::fromempty(initial_, unit)); tofill = --contents_.end(); } int8_t i = (int8_t)std::distance(contents_.begin(), tofill); @@ -250,7 +251,7 @@ namespace awkward { return raw != 0 && raw->units() == unit; }); if (tofill == contents_.end()) { - contents_.emplace_back(DatetimeBuilder::fromempty(options_, unit)); + contents_.emplace_back(DatetimeBuilder::fromempty(initial_, unit)); tofill = --contents_.end(); } int8_t i = (int8_t)std::distance(contents_.begin(), tofill); @@ -273,7 +274,7 @@ namespace awkward { return raw != 0 && raw->encoding() == encoding; }); if (tofill == contents_.end()) { - contents_.emplace_back(StringBuilder::fromempty(options_, encoding)); + contents_.emplace_back(StringBuilder::fromempty(initial_, encoding)); tofill = --contents_.end(); } int8_t i = (int8_t)std::distance(contents_.begin(), tofill); @@ -295,7 +296,7 @@ namespace awkward { return dynamic_cast(p.get()); }); if (tofill == contents_.end()) { - contents_.emplace_back(ListBuilder::fromempty(options_)); + contents_.emplace_back(ListBuilder::fromempty(initial_)); tofill = --contents_.end(); } tofill->get()->beginlist(); @@ -334,7 +335,7 @@ namespace awkward { return raw != nullptr && (raw->length() == -1 || raw->numfields() == numfields); }); if (tofill == contents_.end()) { - contents_.emplace_back(TupleBuilder::fromempty(options_)); + contents_.emplace_back(TupleBuilder::fromempty(initial_)); tofill = --contents_.end(); } tofill->get()->begintuple(numfields); @@ -388,7 +389,7 @@ namespace awkward { (!check && raw->nameptr() == name))); }); if (tofill == contents_.end()) { - contents_.emplace_back(RecordBuilder::fromempty(options_)); + contents_.emplace_back(RecordBuilder::fromempty(initial_)); tofill = --contents_.end(); } tofill->get()->beginrecord(name, check); diff --git a/src/libawkward/builder/UnknownBuilder.cpp b/src/libawkward/builder/UnknownBuilder.cpp index 92a4def29e..d1650de243 100644 --- a/src/libawkward/builder/UnknownBuilder.cpp +++ b/src/libawkward/builder/UnknownBuilder.cpp @@ -4,7 +4,6 @@ #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/builder/OptionBuilder.h" #include "awkward/builder/BoolBuilder.h" #include "awkward/builder/DatetimeBuilder.h" @@ -20,13 +19,13 @@ namespace awkward { const BuilderPtr - UnknownBuilder::fromempty(const ArrayBuilderOptions& options) { - return std::make_shared(options, 0); + UnknownBuilder::fromempty(const int64_t initial) { + return std::make_shared(initial, 0); } - UnknownBuilder::UnknownBuilder(const ArrayBuilderOptions& options, + UnknownBuilder::UnknownBuilder(const int64_t initial, int64_t nullcount) - : options_(options) + : initial_(initial) , nullcount_(nullcount) { } const std::string @@ -81,9 +80,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::boolean(bool x) { - BuilderPtr out = BoolBuilder::fromempty(options_); + BuilderPtr out = BoolBuilder::fromempty(initial_); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->boolean(x); return std::move(out); @@ -91,9 +90,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::integer(int64_t x) { - BuilderPtr out = Int64Builder::fromempty(options_); + BuilderPtr out = Int64Builder::fromempty(initial_); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->integer(x); return std::move(out); @@ -101,9 +100,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::real(double x) { - BuilderPtr out = Float64Builder::fromempty(options_); + BuilderPtr out = Float64Builder::fromempty(initial_); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->real(x); return std::move(out); @@ -111,9 +110,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::complex(std::complex x) { - BuilderPtr out = Complex128Builder::fromempty(options_); + BuilderPtr out = Complex128Builder::fromempty(initial_); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->complex(x); return std::move(out); @@ -121,9 +120,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::datetime(int64_t x, const std::string& unit) { - BuilderPtr out = DatetimeBuilder::fromempty(options_, unit); + BuilderPtr out = DatetimeBuilder::fromempty(initial_, unit); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->datetime(x, unit); return std::move(out); @@ -131,9 +130,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::timedelta(int64_t x, const std::string& unit) { - BuilderPtr out = DatetimeBuilder::fromempty(options_, unit); + BuilderPtr out = DatetimeBuilder::fromempty(initial_, unit); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->timedelta(x, unit); return std::move(out); @@ -141,9 +140,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::string(const char* x, int64_t length, const char* encoding) { - BuilderPtr out = StringBuilder::fromempty(options_, encoding); + BuilderPtr out = StringBuilder::fromempty(initial_, encoding); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->string(x, length, encoding); return std::move(out); @@ -151,9 +150,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::beginlist() { - BuilderPtr out = ListBuilder::fromempty(options_); + BuilderPtr out = ListBuilder::fromempty(initial_); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->beginlist(); return std::move(out); @@ -168,9 +167,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::begintuple(int64_t numfields) { - BuilderPtr out = TupleBuilder::fromempty(options_); + BuilderPtr out = TupleBuilder::fromempty(initial_); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->begintuple(numfields); return std::move(out); @@ -192,9 +191,9 @@ namespace awkward { const BuilderPtr UnknownBuilder::beginrecord(const char* name, bool check) { - BuilderPtr out = RecordBuilder::fromempty(options_); + BuilderPtr out = RecordBuilder::fromempty(initial_); if (nullcount_ != 0) { - out = OptionBuilder::fromnulls(options_, nullcount_, out); + out = OptionBuilder::fromnulls(initial_, nullcount_, out); } out.get()->beginrecord(name, check); return std::move(out); diff --git a/src/libawkward/io/uproot.cpp b/src/libawkward/io/uproot.cpp index bb8802b5ff..aaa2270973 100644 --- a/src/libawkward/io/uproot.cpp +++ b/src/libawkward/io/uproot.cpp @@ -9,8 +9,7 @@ #include "awkward/array/NumpyArray.h" #include "awkward/array/ListOffsetArray.h" #include "awkward/array/EmptyArray.h" -#include "awkward/builder/ArrayBuilderOptions.h" -#include "awkward/builder/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include "awkward/io/uproot.h" @@ -54,12 +53,12 @@ namespace awkward { uint8_t* data_ptr = reinterpret_cast(data.data()); int32_t* byte_offsets_ptr = byte_offsets.data(); - ArrayBuilderOptions options(1024, 1.5); + const int64_t initial(1024); Index64 offsets1(byte_offsets.length()); int64_t* offsets1_ptr = offsets1.data(); - GrowableBuffer offsets2 = GrowableBuffer::empty(options); - GrowableBuffer content = GrowableBuffer::empty(options); + GrowableBuffer offsets2 = GrowableBuffer::empty(initial); + GrowableBuffer content = GrowableBuffer::empty(initial); offsets1_ptr[0] = 0; offsets2.append(0); @@ -90,11 +89,14 @@ namespace awkward { offsets1_ptr[entry + 1] = offsets1_ptr[entry] + count; } + std::unique_ptr ptr(new T[(size_t)content.length()]); + content.concatenate(ptr.get()); + std::vector shape = { (ssize_t)content.length() }; std::vector strides = { (ssize_t)sizeof(T) }; ContentPtr outcontent = std::make_shared(Identities::none(), util::Parameters(), - std::move(content.get_ptr()), + std::move(ptr), shape, strides, 0, @@ -103,7 +105,9 @@ namespace awkward { dtype, kernel::lib::cpu); - Index64 outoffsets2(std::move(offsets2.get_ptr()), 0, offsets2.length(), kernel::lib::cpu); + std::unique_ptr offsets_ptr(new int64_t[(size_t)offsets2.length()]); + offsets2.concatenate(offsets_ptr.get()); + Index64 outoffsets2(std::move(offsets_ptr), 0, offsets2.length(), kernel::lib::cpu); ContentPtr outlist2 = std::make_shared(Identities::none(), util::Parameters(), outoffsets2, diff --git a/src/libawkward/layoutbuilder/LayoutBuilder.cpp b/src/libawkward/layoutbuilder/LayoutBuilder.cpp index 1e9b8082fa..2fb5a76ffe 100644 --- a/src/libawkward/layoutbuilder/LayoutBuilder.cpp +++ b/src/libawkward/layoutbuilder/LayoutBuilder.cpp @@ -3,7 +3,6 @@ #define FILENAME(line) FILENAME_FOR_EXCEPTIONS("src/libawkward/layoutbuilder/LayoutBuilder.cpp", line) #include "awkward/layoutbuilder/LayoutBuilder.h" -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/layoutbuilder/BitMaskedArrayBuilder.h" #include "awkward/layoutbuilder/ByteMaskedArrayBuilder.h" @@ -191,10 +190,10 @@ namespace awkward { template LayoutBuilder::LayoutBuilder(const std::string& json_form, - const ArrayBuilderOptions& options, + const int64_t initial, bool vm_init) : json_form_(json_form), - initial_(options.initial()), + initial_(initial), builder_(nullptr), vm_(nullptr), vm_input_data_("data"), diff --git a/src/python/content.cpp b/src/python/content.cpp index 3d2f7b3bac..acd05ee007 100644 --- a/src/python/content.cpp +++ b/src/python/content.cpp @@ -8,7 +8,6 @@ #include "awkward/type/Type.h" #include "awkward/Reducer.h" -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/layoutbuilder/BitMaskedArrayBuilder.h" #include "awkward/layoutbuilder/ByteMaskedArrayBuilder.h" @@ -1025,8 +1024,8 @@ getitem(const ak::ArrayBuilder& self, const py::object& obj) { py::class_ make_ArrayBuilder(const py::handle& m, const std::string& name) { return (py::class_(m, name.c_str()) - .def(py::init([](int64_t initial, double resize) -> ak::ArrayBuilder { - return ak::ArrayBuilder(ak::ArrayBuilderOptions(initial, resize)); + .def(py::init([](const int64_t initial, double resize) -> ak::ArrayBuilder { + return ak::ArrayBuilder(initial); }), py::arg("initial") = 1024, py::arg("resize") = 1.5) .def_property_readonly("_ptr", [](const ak::ArrayBuilder* self) -> size_t { @@ -1043,6 +1042,8 @@ make_ArrayBuilder(const py::handle& m, const std::string& name) { return py::str(self.to_buffers(container, form_key_id)); }) .def("to_buffers", [](const ak::ArrayBuilder& self) -> py::object { + std::cout << "ArrayBuilder to_buffers" << std::endl; + ::NumpyBuffersContainer container; int64_t form_key_id = 0; std::string form = self.to_buffers(container, form_key_id); @@ -1053,6 +1054,7 @@ make_ArrayBuilder(const py::handle& m, const std::string& name) { return out; }) .def("snapshot", [](const ak::ArrayBuilder& self) -> py::object { + std::cout << "ArrayBuilder snapshot" << std::endl; return ::builder_snapshot(self.builder()); }) .def("__getitem__", &getitem) @@ -1523,8 +1525,8 @@ template py::class_> make_LayoutBuilder(const py::handle& m, const std::string& name) { return (py::class_>(m, name.c_str()) - .def(py::init([](const std::string& form, int64_t initial, double resize, bool vm_init) -> ak::LayoutBuilder { - return ak::LayoutBuilder(form, ak::ArrayBuilderOptions(initial, resize), vm_init); + .def(py::init([](const std::string& form, const int64_t initial, double resize, bool vm_init) -> ak::LayoutBuilder { + return ak::LayoutBuilder(form, initial, vm_init); }), py::arg("form"), py::arg("initial") = 8, py::arg("resize") = 1.5, py::arg("vm_init") = true) .def_property_readonly("_ptr", [](const ak::LayoutBuilder* self) -> size_t { diff --git a/src/python/io.cpp b/src/python/io.cpp index 69fa8c523d..e7e3ab1219 100644 --- a/src/python/io.cpp +++ b/src/python/io.cpp @@ -5,7 +5,6 @@ #include #include -#include "awkward/builder/ArrayBuilderOptions.h" #include "awkward/io/json.h" #include "awkward/io/uproot.h" diff --git a/tests-cpp/CMakeLists.txt b/tests-cpp/CMakeLists.txt new file mode 100644 index 0000000000..a87b784ba0 --- /dev/null +++ b/tests-cpp/CMakeLists.txt @@ -0,0 +1,9 @@ +macro(addtest_nolibs name filename) + if(BUILD_TESTING) + add_executable(${name} ${filename}) + target_link_libraries(${name} PRIVATE awkward-parent) + set_target_properties(${name} PROPERTIES CXX_VISIBILITY_PRESET hidden) + set_target_properties(${name} PROPERTIES VISIBILITY_INLINES_HIDDEN ON) + add_test(${name} ${name}) + endif() +endmacro(addtest_nolibs) diff --git a/tests-cpp/test_1542-array-builder.cpp b/tests-cpp/test_1542-array-builder.cpp new file mode 100644 index 0000000000..a704e6b832 --- /dev/null +++ b/tests-cpp/test_1542-array-builder.cpp @@ -0,0 +1,29 @@ +// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE + +#include "awkward/builder/ArrayBuilder.h" + +#include +#include + +int main(int /* argc */, const char ** /* argv */) { + + auto a = awkward::ArrayBuilder(10); + for (int64_t i = 0; i < 100; i++) { + a.integer(1); + a.integer(2); + } + assert(a.length() == 200); + + for (int64_t i = 0; i < 20; i++) { + a.real(3.3); + } + assert(a.length() == 220); + + for (int64_t i = 0; i < 2000; i++) { + a.integer(4); + a.integer(5); + } + assert(a.length() == 4220); + + return 0; +} diff --git a/tests-cpp/test-growable-buffer.cpp b/tests-cpp/test_1542-growable-buffer.cpp similarity index 88% rename from tests-cpp/test-growable-buffer.cpp rename to tests-cpp/test_1542-growable-buffer.cpp index 01cc52fe7d..793411debb 100644 --- a/tests-cpp/test-growable-buffer.cpp +++ b/tests-cpp/test_1542-growable-buffer.cpp @@ -1,6 +1,6 @@ // BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE -#include "../src/awkward/_v2/cpp-headers/GrowableBuffer.h" +#include "awkward/GrowableBuffer.h" #include #include @@ -162,6 +162,24 @@ void test_extend() { } } +void test_append_and_get_ref() { + size_t data_size = 15; + size_t initial = 5; + double data[15] = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, + 2.1, 2.2, 2.3, 2.4, 2.5, 2.6}; + + int val; + int& ref = val; + + auto buffer = awkward::GrowableBuffer::empty(initial); + + for (size_t i = 0; i < buffer.length(); i++) { + ref = buffer.append_and_get_ref(data[i]); + assert(ref == data[i]); + assert(val == data[i]); + } +} + int main(int /* argc */, const char ** /* argv */) { test_full(); test_arange(); @@ -172,6 +190,7 @@ int main(int /* argc */, const char ** /* argv */) { test_double(); test_complex(); test_extend(); + test_append_and_get_ref(); return 0; }