ContextNode

Struct ContextNode 

Source
pub struct ContextNode { /* private fields */ }
Expand description

A node in the context tree that stores transition statistics

Uses optimized storage for small collections and StateId for memory efficiency Implements lazy computation with caching for entropy and KL divergence

Implementations§

Source§

impl ContextNode

Source

pub fn new(interner: Arc<StringInterner>) -> Self

Create a new empty context node with string interner

Source

pub fn add_transition(&mut self, next_state: &str)

Add a transition to this context using string interning

Source

pub fn add_transition_by_id(&mut self, state_id: StateId)

Add a transition using StateId directly (internal use)

Source

pub fn total_count(&self) -> usize

Get the total number of transitions from this context

Source

pub fn get_count(&self, next_state: &str) -> usize

Get the count for a specific next state

Source

pub fn get_count_by_id(&self, state_id: StateId) -> usize

Get the count for a StateId directly (internal use)

Source

pub fn vocab_size(&self) -> usize

Get the number of unique next states

Source

pub fn get_state_counts(&self) -> impl Iterator<Item = (StateId, usize)> + '_

Get all state IDs with their counts (internal use)

Source

pub fn total_transitions(&self) -> usize

Get the sum of all transition counts (for compatibility)

Source

pub fn get_string_counts(&self) -> HashMap<String, usize>

Get all counts as strings for compatibility with performance module

Source

pub fn counts(&self) -> HashMap<String, usize>

Get counts for debugging (returns string representation)

Source

pub fn get_probability( &self, next_state: &str, config: &AnomalyGridConfig, ) -> f64

Get the probability for a specific next state using Laplace smoothing

Computes probability on-demand: P(state) = (count + α) / (total + α * |V|)

Source

pub fn get_probability_by_id( &self, state_id: StateId, config: &AnomalyGridConfig, ) -> f64

Get probability for a StateId directly (internal use)

Source

pub fn get_probability_normalized( &self, next_state: &str, config: &AnomalyGridConfig, global_vocab_size: usize, ) -> f64

Get probability with proper normalization using global vocabulary size

Source

pub fn get_probability_normalized_by_id( &self, state_id: StateId, config: &AnomalyGridConfig, global_vocab_size: usize, ) -> f64

Get probability with proper normalization using global vocabulary by StateId

Source

pub fn calculate_entropy(&mut self, config: &AnomalyGridConfig) -> f64

Calculate Shannon entropy with lazy computation and caching: H(X) = -∑ P(x) log₂ P(x)

Source

pub fn compute_entropy(&self, config: &AnomalyGridConfig) -> f64

Calculate Shannon entropy without caching (for immutable access)

Source

pub fn calculate_kl_divergence(&mut self, config: &AnomalyGridConfig) -> f64

Calculate KL divergence from uniform distribution with lazy computation and caching

Source

pub fn compute_kl_divergence(&self, config: &AnomalyGridConfig) -> f64

Calculate KL divergence from uniform distribution without caching (for immutable access)

Source

pub fn get_all_probabilities( &self, config: &AnomalyGridConfig, ) -> HashMap<String, f64>

Get all probabilities as a HashMap (for compatibility with existing code)

Note: This creates temporary storage and should be used sparingly

Source

pub fn reset(&mut self, interner: Arc<StringInterner>)

Reset the context node for reuse in memory pool

Source

pub fn clear(&mut self)

Clear the context node data for memory pool return

Source

pub fn cache_stats(&self) -> (bool, bool)

Get cache hit statistics for monitoring

Trait Implementations§

Source§

impl Clone for ContextNode

Source§

fn clone(&self) -> ContextNode

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 ContextNode

Source§

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

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

impl Default for ContextNode

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> 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> 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.