9 std::string& error, uint64_t* error_row)
12 if (!field || rows.
layout().
fields()[*field].type != std::type_index(
typeid(uint64_t))) {
13 error =
"Composite has no uint64_t field " + std::string(parent_field);
17 const std::string name(parent_field);
19 [&name](
const Composite& row) {
return row.
get<uint64_t>(name); },
28 return Region(std::vector<uint64_t> { range->first },
29 std::vector<uint64_t> { range->first + range->count - 1 });
34 std::string_view parent_field,
size_t row, std::string& error)
37 if (!field || rows.
layout().
fields()[*field].type != std::type_index(
typeid(uint64_t))) {
38 error =
"Composite has no uint64_t field " + std::string(parent_field);
42 if (!range || index.
size() != rows.
size()) {
43 error =
"subtree rows are not preorder, the row is out of range, or the index does not match";
49 for (
size_t source_row = range->first; source_row < range->first + range->count; ++source_row) {
50 const auto source = rows.
at(source_row);
51 const auto appended = source ? insertion.
append(*source) : std::nullopt;
53 error =
"Could not copy a subtree row";
56 const auto parent = index.
parent(source_row);
57 const bool written = source_row == range->first || !parent
58 ? insertion.
clear(*appended, parent_field)
59 : insertion.
set(*appended, parent_field,
static_cast<uint64_t
>(*parent - range->first));
61 error =
"Could not rebase a subtree parent link";
70 std::string& error, uint64_t* error_row)
75 return std::shared_ptr<const CompositeForest>(
size_t size() const noexcept
Number of packed elements.
const CompositeLayout & layout() const noexcept
Finalized layout shared by all elements.
std::optional< Composite > at(size_t index) const noexcept
Borrow an element by index.
Owns a sequence of values that share a CompositeLayout.
CompositeForest(CompositeArray rows, ForestIndex index, std::string parent_field)
const ForestIndex & index() const noexcept
The index of the owned rows.
const CompositeArray & rows() const noexcept
The owned rows.
static std::shared_ptr< const CompositeForest > build(CompositeArray rows, std::string_view parent_field, std::string &error, uint64_t *error_row=nullptr)
Index rows and take ownership of them.
bool set(size_t index, std::string_view field_name, T value)
Write an arithmetic or GLM field of an existing element.
bool clear(size_t index, std::string_view field_name)
Mark a named field absent in an existing element.
size_t append()
Append an empty element.
Mutable insertion adapter for a CompositeArray.
const std::vector< CompositeField > & fields() const noexcept
Declared fields in element order, with finalized byte offsets.
std::optional< size_t > find_field(std::string_view name) const noexcept
Find a field index by name, or std::nullopt if absent.
std::optional< T > get(std::string_view field_name) const noexcept
Read a present arithmetic or GLM field by exact value type.
Read view of one value in a CompositeArray.
size_t size() const noexcept
Number of rows.
std::optional< RowRange > subtree_range(size_t row) const noexcept
Consecutive rows of the subtree of row.
std::optional< uint64_t > parent(size_t row) const noexcept
Parent row, or nullopt for a root or an out of range row.
Parent, child, depth and subtree structure over rows linked by parent index.
std::optional< ForestIndex > build_forest_index(const Array &array, Parent &&parent, std::string &error, uint64_t *error_row=nullptr)
Build a ForestIndex from an array whose elements carry a parent row.
std::optional< ForestIndex > index_composite_forest(const CompositeArray &rows, std::string_view parent_field, std::string &error, uint64_t *error_row)
Index the parent links of a Composite array.
std::optional< Region > forest_subtree_region(const ForestIndex &index, size_t row)
One-axis Region covering the subtree of row, inclusive.
std::optional< CompositeArray > copy_forest_subtree(const CompositeArray &rows, const ForestIndex &index, std::string_view parent_field, size_t row, std::string &error)
Copy the subtree of row into a self-contained array.
Represents a point or span in N-dimensional space.