xml_3dm/node/match_area.rs
1//! Match area for tagging nodes in matched subtrees.
2//!
3//! A `MatchArea` is used to tag nodes that belong to the same matched subtree.
4//! It contains a reference to the root of the subtree and an accumulated
5//! information size metric.
6
7use std::cell::{Cell, RefCell};
8use std::rc::Weak;
9
10use super::NodeInner;
11
12/// A weak reference to a node, used to avoid reference cycles.
13pub type WeakNodeRef = Weak<RefCell<NodeInner>>;
14
15/// Tags nodes in the same matched subtree.
16///
17/// This is used during the matching phase to track which nodes belong
18/// to a matched region and their combined information size.
19#[derive(Debug)]
20pub struct MatchArea {
21 /// Accumulated information bytes in this subtree.
22 /// Uses Cell for interior mutability since we need to update this
23 /// through shared references (Rc<MatchArea>).
24 info_bytes: Cell<i32>,
25 /// Weak reference to the root node of this match area.
26 /// The root is always a branch node.
27 root: WeakNodeRef,
28}
29
30impl MatchArea {
31 /// Creates a new match area with the given root node.
32 pub fn new(root: WeakNodeRef) -> Self {
33 MatchArea {
34 info_bytes: Cell::new(0),
35 root,
36 }
37 }
38
39 /// Returns a reference to the root node weak reference.
40 pub fn root(&self) -> &WeakNodeRef {
41 &self.root
42 }
43
44 /// Returns the root as a strong reference if it still exists.
45 pub fn root_strong(&self) -> Option<super::NodeRef> {
46 self.root.upgrade()
47 }
48
49 /// Adds to the accumulated information bytes.
50 pub fn add_info_bytes(&self, bytes: i32) {
51 self.info_bytes.set(self.info_bytes.get() + bytes);
52 }
53
54 /// Returns the accumulated information bytes.
55 pub fn info_bytes(&self) -> i32 {
56 self.info_bytes.get()
57 }
58}