MorphologicalAttributeFilters
Public API documentation
Loading...
Searching...
No Matches
WeightedTreeView.hpp
1#pragma once
2
3#include "MorphologicalTree.hpp"
4#include "../utils/Altitude.hpp"
5
6#include <cstddef>
7#include <span>
8#include <stdexcept>
9
10namespace mmcfilters {
11
23template<AltitudeValue T>
25private:
26 const MorphologicalTree* topology_ = nullptr;
27 AltitudeSpan<T> altitude_;
28 std::size_t topologyMutationVersion_ = 0;
29
33 void validateAltitudeBufferShape() const {
34 if (altitude_.size() != static_cast<std::size_t>(topology_->getNumInternalNodeSlots())) {
35 throw std::runtime_error("WeightedTreeView altitude size must match the dense internal-node domain.");
36 }
37 }
38
39public:
42
50 : topology_(&topology), altitude_(altitude), topologyMutationVersion_(topology.getMutationVersion()) {
51 validateAltitudeBufferShape();
52 }
53
61 : topology_(&topology), altitude_(std::span<const T>(altitude)), topologyMutationVersion_(topology.getMutationVersion()) {
62 validateAltitudeBufferShape();
63 }
64
69
74 return *topology_;
75 }
76
81 return altitude_;
82 }
83
87 void requireTopologyUnchanged(const char* context) const {
88 topology().requireMutationVersion(topologyMutationVersion_, context);
89 }
90
95 if (nodeId < 0 || static_cast<std::size_t>(nodeId) >= altitude_.size()) {
96 throw std::invalid_argument("WeightedTreeView::getAltitude requires a valid internal NodeId.");
97 }
98 return altitude_[static_cast<std::size_t>(nodeId)];
99 }
100
108 const MorphologicalTree& tree = topology();
109 if (!tree.isAlive(nodeId)) {
110 throw std::invalid_argument("WeightedTreeView::getNodeResidue requires a live internal NodeId.");
111 }
114 return static_cast<AltitudeDiff<T>>(getAltitude(nodeId));
115 }
116 return static_cast<AltitudeDiff<T>>(getAltitude(nodeId)) -
118 }
119};
120
121} // namespace mmcfilters
constexpr NodeId InvalidNode
Sentinel value used to denote an invalid node identifier.
Definition Common.hpp:25
Mutable morphological tree built directly on proper parts and dense node ids.
int getNumInternalNodeSlots() const
Returns the size of the dense internal-node id domain.
bool isAlive(NodeId nodeId) const
Tests whether a node slot currently represents a live node.
NodeId getNodeParent(NodeId nodeId) const
Returns the direct parent of nodeId.
Non-owning view pairing a topology with an external altitude span.
const MorphologicalTree & topology() const noexcept
Returns the borrowed tree topology.
T getAltitude(NodeId nodeId) const
Returns the altitude associated with an internal node id.
AltitudeDiff< T > getNodeResidue(NodeId nodeId) const
Returns the altitude difference between a live node and its parent.
WeightedTreeView(const MorphologicalTree &topology, const AltitudeBuffer< T > &altitude)
Builds a view over a caller-owned altitude buffer.
void requireTopologyUnchanged(const char *context) const
Throws if the borrowed topology changed since view construction.
WeightedTreeView(const MorphologicalTree &topology, AltitudeSpan< T > altitude)
Builds a view over caller-owned topology and altitude storage.
WeightedTreeView(const MorphologicalTree &, AltitudeBuffer< T > &&)=delete
Rejects temporary altitude buffers that would leave a dangling span.
AltitudeSpan< T > altitude() const noexcept
Returns the borrowed altitude span indexed by internal NodeId.
Owning result for one computed scalar attribute layout and buffer.