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
impl ContextNode
Sourcepub fn new(interner: Arc<StringInterner>) -> Self
pub fn new(interner: Arc<StringInterner>) -> Self
Create a new empty context node with string interner
Sourcepub fn add_transition(&mut self, next_state: &str)
pub fn add_transition(&mut self, next_state: &str)
Add a transition to this context using string interning
Sourcepub fn add_transition_by_id(&mut self, state_id: StateId)
pub fn add_transition_by_id(&mut self, state_id: StateId)
Add a transition using StateId directly (internal use)
Sourcepub fn total_count(&self) -> usize
pub fn total_count(&self) -> usize
Get the total number of transitions from this context
Sourcepub fn get_count_by_id(&self, state_id: StateId) -> usize
pub fn get_count_by_id(&self, state_id: StateId) -> usize
Get the count for a StateId directly (internal use)
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Get the number of unique next states
Sourcepub fn get_state_counts(&self) -> impl Iterator<Item = (StateId, usize)> + '_
pub fn get_state_counts(&self) -> impl Iterator<Item = (StateId, usize)> + '_
Get all state IDs with their counts (internal use)
Sourcepub fn total_transitions(&self) -> usize
pub fn total_transitions(&self) -> usize
Get the sum of all transition counts (for compatibility)
Sourcepub fn get_string_counts(&self) -> HashMap<String, usize>
pub fn get_string_counts(&self) -> HashMap<String, usize>
Get all counts as strings for compatibility with performance module
Sourcepub fn counts(&self) -> HashMap<String, usize>
pub fn counts(&self) -> HashMap<String, usize>
Get counts for debugging (returns string representation)
Sourcepub fn get_probability(
&self,
next_state: &str,
config: &AnomalyGridConfig,
) -> f64
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|)
Sourcepub fn get_probability_by_id(
&self,
state_id: StateId,
config: &AnomalyGridConfig,
) -> f64
pub fn get_probability_by_id( &self, state_id: StateId, config: &AnomalyGridConfig, ) -> f64
Get probability for a StateId directly (internal use)
Sourcepub fn get_probability_normalized(
&self,
next_state: &str,
config: &AnomalyGridConfig,
global_vocab_size: usize,
) -> f64
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
Sourcepub fn get_probability_normalized_by_id(
&self,
state_id: StateId,
config: &AnomalyGridConfig,
global_vocab_size: usize,
) -> f64
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
Sourcepub fn calculate_entropy(&mut self, config: &AnomalyGridConfig) -> f64
pub fn calculate_entropy(&mut self, config: &AnomalyGridConfig) -> f64
Calculate Shannon entropy with lazy computation and caching: H(X) = -∑ P(x) log₂ P(x)
Sourcepub fn compute_entropy(&self, config: &AnomalyGridConfig) -> f64
pub fn compute_entropy(&self, config: &AnomalyGridConfig) -> f64
Calculate Shannon entropy without caching (for immutable access)
Sourcepub fn calculate_kl_divergence(&mut self, config: &AnomalyGridConfig) -> f64
pub fn calculate_kl_divergence(&mut self, config: &AnomalyGridConfig) -> f64
Calculate KL divergence from uniform distribution with lazy computation and caching
Sourcepub fn compute_kl_divergence(&self, config: &AnomalyGridConfig) -> f64
pub fn compute_kl_divergence(&self, config: &AnomalyGridConfig) -> f64
Calculate KL divergence from uniform distribution without caching (for immutable access)
Sourcepub fn get_all_probabilities(
&self,
config: &AnomalyGridConfig,
) -> HashMap<String, f64>
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
Sourcepub fn reset(&mut self, interner: Arc<StringInterner>)
pub fn reset(&mut self, interner: Arc<StringInterner>)
Reset the context node for reuse in memory pool
Sourcepub fn cache_stats(&self) -> (bool, bool)
pub fn cache_stats(&self) -> (bool, bool)
Get cache hit statistics for monitoring
Trait Implementations§
Source§impl Clone for ContextNode
impl Clone for ContextNode
Source§fn clone(&self) -> ContextNode
fn clone(&self) -> ContextNode
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ContextNode
impl Debug for ContextNode
Auto Trait Implementations§
impl Freeze for ContextNode
impl RefUnwindSafe for ContextNode
impl Send for ContextNode
impl Sync for ContextNode
impl Unpin for ContextNode
impl UnwindSafe for ContextNode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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