Skip to main content

TreeArena

Struct TreeArena 

Source
pub struct TreeArena {
    pub feature_idx: Vec<u32>,
    pub threshold: Vec<f64>,
    pub left: Vec<NodeId>,
    pub right: Vec<NodeId>,
    pub leaf_value: Vec<f64>,
    pub is_leaf: Vec<bool>,
    pub depth: Vec<u16>,
    pub sample_count: Vec<u64>,
    pub categorical_mask: Vec<Option<u64>>,
}
Available on crate feature alloc only.
Expand description

Structure-of-Arrays arena storage for tree nodes.

All node properties are stored in parallel vectors for cache efficiency. Internal nodes have feature_idx + threshold + children. Leaf nodes have leaf_value.

§Invariants

  • All vectors have the same length at all times.
  • A leaf node has is_leaf[id] == true, left[id] == NONE, right[id] == NONE.
  • An internal node has is_leaf[id] == false and valid left/right children.
  • depth[id] is set at allocation time and never changes.

Fields§

§feature_idx: Vec<u32>

Feature index used for splitting (only meaningful for internal nodes).

§threshold: Vec<f64>

Split threshold (samples with feature <= threshold go left).

§left: Vec<NodeId>

Left child NodeId (NONE for leaves).

§right: Vec<NodeId>

Right child NodeId (NONE for leaves).

§leaf_value: Vec<f64>

Leaf prediction value.

§is_leaf: Vec<bool>

Whether this node is a leaf.

§depth: Vec<u16>

Depth of this node in the tree (root = 0).

§sample_count: Vec<u64>

Number of samples routed through this node.

§categorical_mask: Vec<Option<u64>>

Categorical split bitmask. For categorical splits, bit i set means category i routes left. None for continuous splits (threshold-based).

Implementations§

Source§

impl TreeArena

Source

pub fn new() -> Self

Create an empty arena with no pre-allocated capacity.

Source

pub fn with_capacity(cap: usize) -> Self

Create an empty arena with pre-allocated capacity for cap nodes.

This avoids reallocation when the maximum tree size is known upfront.

Source

pub fn add_leaf(&mut self, depth: u16) -> NodeId

Allocate a new leaf node with value 0.0 at the given depth.

Returns the NodeId of the newly created leaf.

Source

pub fn split_leaf( &mut self, leaf_id: NodeId, feature_idx: u32, threshold: f64, left_value: f64, right_value: f64, ) -> (NodeId, NodeId)

Convert a leaf node into an internal (split) node, creating two new leaf children.

The parent’s is_leaf flag is cleared and its feature_idx/threshold are set. Two fresh leaf nodes are allocated at depth = parent.depth + 1 with the specified initial values.

§Panics

Panics if leaf_id does not reference a current leaf node.

§Returns

(left_id, right_id) – the NodeIds of the two new children.

Source

pub fn split_leaf_categorical( &mut self, leaf_id: NodeId, feature_idx: u32, threshold: f64, left_value: f64, right_value: f64, mask: u64, ) -> (NodeId, NodeId)

Split a leaf using a categorical bitmask instead of a threshold.

The mask is a u64 where bit i set means category i goes left. The threshold is still stored as the midpoint of the split partition for backward compatibility, but routing uses the bitmask.

§Panics

Panics if leaf_id does not reference a current leaf node.

Source

pub fn get_categorical_mask(&self, id: NodeId) -> Option<u64>

Return the categorical bitmask for a split node, if it’s a categorical split.

Source

pub fn is_leaf(&self, id: NodeId) -> bool

Returns true if the node at id is a leaf.

Source

pub fn predict(&self, id: NodeId) -> f64

Return the leaf prediction value.

§Panics

Panics if id references an internal (non-leaf) node.

Source

pub fn set_leaf_value(&mut self, id: NodeId, value: f64)

Set the leaf prediction value for a leaf node.

§Panics

Panics if id does not reference a leaf node.

Source

pub fn get_depth(&self, id: NodeId) -> u16

Return the depth of the node.

Source

pub fn get_feature_idx(&self, id: NodeId) -> u32

Return the feature index used for splitting at this node.

Source

pub fn get_threshold(&self, id: NodeId) -> f64

Return the split threshold for this node.

Source

pub fn get_left(&self, id: NodeId) -> NodeId

Return the left child NodeId.

Source

pub fn get_right(&self, id: NodeId) -> NodeId

Return the right child NodeId.

Source

pub fn get_sample_count(&self, id: NodeId) -> u64

Return the sample count for this node.

Source

pub fn increment_sample_count(&mut self, id: NodeId)

Increment the sample count for this node by one.

Source

pub fn n_nodes(&self) -> usize

Total number of allocated nodes (internal + leaf).

Source

pub fn n_leaves(&self) -> usize

Count of leaf nodes currently in the arena.

Source

pub fn reset(&mut self)

Clear all storage, returning the arena to an empty state.

Allocated memory is retained (capacity is preserved) so the arena can be reused without reallocating.

Trait Implementations§

Source§

impl Clone for TreeArena

Source§

fn clone(&self) -> TreeArena

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TreeArena

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TreeArena

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.