Skip to main content

RandomCutTree

Struct RandomCutTree 

Source
pub struct RandomCutTree<const D: usize> { /* private fields */ }
Expand description

Incrementally-maintained random cut tree over up to capacity distinct D-dimensional points.

§Examples

use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
use anomstream_core::RandomCutTree;

let mut tree = RandomCutTree::<2>::new(8).unwrap();
let p = [1.0_f64, 2.0];
let points = vec![p];
let mut rng = ChaCha8Rng::seed_from_u64(42);
tree.add(0, &p, &points, &mut rng).unwrap();
assert!(tree.root().unwrap().is_leaf());

Implementations§

Source§

impl<const D: usize> RandomCutTree<D>

Source

pub fn new(capacity: u32) -> RcfResult<Self>

Build an empty tree with room for capacity distinct points.

§Errors

Returns RcfError::InvalidConfig when D == 0, capacity == 0, or capacity exceeds the store limit.

Source

pub fn root(&self) -> Option<NodeRef>

Root node reference, or None when the tree is empty.

Source

pub const fn dimension(&self) -> usize

Configured dimensionality (compile-time D).

Source

pub fn distinct_point_count(&self) -> usize

Number of distinct points currently stored (each leaf counts once regardless of its mass).

Source

pub fn store(&self) -> &NodeStore<D>

Borrow the underlying node store. Used by tests and persistence.

Source

pub fn contains(&self, point_idx: usize) -> bool

Whether the tree currently stores point_idx.

Source

pub fn leaf_of(&self, point_idx: usize) -> Option<NodeRef>

NodeRef of the leaf currently mapped to point_idx, or None when the reservoir has evicted it (or never admitted it). Exposed so forest-level scoring paths that need to walk the leaf’s ancestor chain (e.g. codisp-style probe scoring) can locate the leaf without re-traversing from the root.

Source

pub fn max_depth(&self) -> Option<usize>

Maximum depth from the root to any leaf, or None when the tree is empty. Used by tests and diagnostics to verify the expected O(log n) depth bound under uniform-random inserts.

Source

pub fn add<R, P>( &mut self, point_idx: usize, point: &[f64], points: &P, rng: &mut R, ) -> RcfResult<()>
where R: Rng + ?Sized, P: PointAccessor<D> + ?Sized,

Insert point (registered under point_idx in the caller’s point store) into the tree.

When an identical point is already present, the existing leaf’s mass is incremented and point_idx is mapped to that same leaf — duplicate-point handling per Guha 2016.

§Errors
Source

pub fn delete<P>(&mut self, point_idx: usize, points: &P) -> RcfResult<()>
where P: PointAccessor<D> + ?Sized,

Remove the leaf currently mapped to point_idx. When the leaf has mass > 1 (duplicate point), only the mass is decremented and the leaf is preserved.

§Errors
Source

pub fn codisp_stateless(&self, point: &[f64]) -> RcfResult<f64>

Non-mutating codisp estimate — walks root → leaf following the stored cuts and accumulates the maximum per-depth ratio sibling_mass / subtree_mass across the descent path.

Matches the shape of the mutating crate::RandomCutForest::score_codisp walk but without inserting the probe into the reservoir. The classical codisp promises a frozen baseline (AWS / rrcf); the mutating path’s insert+delete cycle leaves reservoir points evicted permanently, eroding that baseline across long eval streams (observed on NAB rogue_hold: score drifts from 0.69 to 0.20 after ~5 k probes).

This path preserves the frozen-baseline promise exactly, takes &self so it parallelises across trees, and costs O(depth · D) per call — typically cheaper than the mutating walk since there is no reservoir housekeeping.

§Errors
Source

pub fn traverse<V: Visitor<D>>( &self, point: &[f64], visitor: V, ) -> RcfResult<V::Output>

Walk from the root to the leaf matching point along the stored cuts, dispatching callbacks to visitor. Returns the visitor’s final output.

§Errors

Trait Implementations§

Source§

impl<const D: usize> Debug for RandomCutTree<D>

Source§

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

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

impl<'de, const D: usize> Deserialize<'de> for RandomCutTree<D>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<const D: usize> Serialize for RandomCutTree<D>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<const D: usize> Freeze for RandomCutTree<D>

§

impl<const D: usize> RefUnwindSafe for RandomCutTree<D>

§

impl<const D: usize> Send for RandomCutTree<D>

§

impl<const D: usize> Sync for RandomCutTree<D>

§

impl<const D: usize> Unpin for RandomCutTree<D>

§

impl<const D: usize> UnsafeUnpin for RandomCutTree<D>

§

impl<const D: usize> UnwindSafe for RandomCutTree<D>

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,