MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
CompositeForest.cpp
Go to the documentation of this file.
1#include "CompositeForest.hpp"
2
4
5namespace MayaFlux::Kakshya {
6
7std::optional<ForestIndex> index_composite_forest(
8 const CompositeArray& rows, std::string_view parent_field,
9 std::string& error, uint64_t* error_row)
10{
11 const auto field = rows.layout().find_field(parent_field);
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);
14 return std::nullopt;
15 }
16
17 const std::string name(parent_field);
18 return build_forest_index(rows,
19 [&name](const Composite& row) { return row.get<uint64_t>(name); },
20 error, error_row);
21}
22
23std::optional<Region> forest_subtree_region(const ForestIndex& index, size_t row)
24{
25 const auto range = index.subtree_range(row);
26 if (!range)
27 return std::nullopt;
28 return Region(std::vector<uint64_t> { range->first },
29 std::vector<uint64_t> { range->first + range->count - 1 });
30}
31
32std::optional<CompositeArray> copy_forest_subtree(
33 const CompositeArray& rows, const ForestIndex& index,
34 std::string_view parent_field, size_t row, std::string& error)
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}
67
68std::shared_ptr<const CompositeForest> CompositeForest::build(
69 CompositeArray rows, std::string_view parent_field,
70 std::string& error, uint64_t* error_row)
71{
72 auto index = index_composite_forest(rows, parent_field, error, error_row);
73 if (!index)
74 return nullptr;
75 return std::shared_ptr<const CompositeForest>(
76 new CompositeForest(std::move(rows), std::move(*index), std::string(parent_field)));
77}
78
79}
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.
Definition Region.hpp:74