MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ copy_forest_subtree()

MAYAFLUX_API std::optional< CompositeArray > MayaFlux::Kakshya::copy_forest_subtree ( const CompositeArray & rows,
const ForestIndex & index,
std::string_view parent_field,
size_t row,
std::string & error )
nodiscard

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.

Parameters
rowsSource array the index was built from.
indexIndex of rows.
parent_fieldName of the uint64_t parent field.
rowRoot of the subtree to copy.
errorReceives the reason when nullopt is returned.
Returns
The copy, or nullopt when the rows are not preorder, row is out of range, or the field is missing or not uint64_t.

Definition at line 32 of file CompositeForest.cpp.

35{
36 const auto field = rows.layout().find_field(parent_field);
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 }
41 const auto range = index.subtree_range(row);
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 }
65 return copy;
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.
Definition Analysis.cpp:473
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.

References MayaFlux::Kakshya::CompositeInsertion::append(), MayaFlux::Kakshya::CompositeArray::at(), MayaFlux::Kakshya::CompositeInsertion::clear(), MayaFlux::Kakshya::CompositeLayout::fields(), MayaFlux::Kakshya::CompositeLayout::find_field(), MayaFlux::Kakshya::CompositeArray::layout(), MayaFlux::Kakshya::ForestIndex::parent(), MayaFlux::Kakshya::CompositeInsertion::set(), MayaFlux::Kakshya::CompositeArray::size(), MayaFlux::Kakshya::ForestIndex::size(), and MayaFlux::Kakshya::ForestIndex::subtree_range().

Referenced by MayaFlux::Kakshya::CompositeForest::copy_subtree(), and MayaFlux::Kakshya::CompositeContainer::materialize_subtree().

Here is the call graph for this function:
Here is the caller graph for this function: