Copy the subtree of row into a self-contained array.
Rows are copied in order and their parent links are rebased to the copy: the subtree root becomes a root and every other row points at its parent's new position. A copy made through a container Region keeps the original links instead, so it is not a forest on its own.
35{
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);
39 return std::nullopt;
40 }
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";
44 return std::nullopt;
45 }
46
47 CompositeArray copy(rows.
layout());
48 CompositeInsertion insertion(copy);
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;
52 if (!appended) {
53 error = "Could not copy a subtree row";
54 return std::nullopt;
55 }
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));
60 if (!written) {
61 error =
"Could not rebase a subtree parent link";
62 return std::nullopt;
63 }
64 }
66}
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.
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.
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.
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
std::vector< double > range(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Value range (max - min) per window.
CopiedText copy(std::string_view text, const LayoutResult &layout, size_t a, size_t b)
Copy a byte range: the text it spans, and the quads that render it.