pub struct ContextTree {
pub max_order: usize,
/* private fields */
}
Expand description
Context tree for storing variable-order Markov chain contexts
Uses trie-based storage for memory efficiency through prefix sharing
Fields§
§max_order: usize
Maximum context order (length)
Implementations§
Source§impl ContextTree
impl ContextTree
Sourcepub fn new(max_order: usize) -> AnomalyGridResult<Self>
pub fn new(max_order: usize) -> AnomalyGridResult<Self>
Create a new context tree with specified maximum order
Sourcepub fn with_interner(
max_order: usize,
interner: Arc<StringInterner>,
) -> AnomalyGridResult<Self>
pub fn with_interner( max_order: usize, interner: Arc<StringInterner>, ) -> AnomalyGridResult<Self>
Create a new context tree with existing string interner
Sourcepub fn build_from_sequence(
&mut self,
sequence: &[String],
config: &AnomalyGridConfig,
) -> AnomalyGridResult<()>
pub fn build_from_sequence( &mut self, sequence: &[String], config: &AnomalyGridConfig, ) -> AnomalyGridResult<()>
Build the context tree from a training sequence
§Complexity
- Time: O(n × max_order × |alphabet|) where n = sequence length
- Space: O(|alphabet|^max_order) in worst case
§Performance Guarantees
- Memory usage is bounded by config.memory_limit if set
- Processing time scales linearly with sequence length
- Uses string interning to reduce memory duplication
Sourcepub fn get_transition_probability(
&self,
context: &[String],
next_state: &str,
) -> Option<f64>
pub fn get_transition_probability( &self, context: &[String], next_state: &str, ) -> Option<f64>
Get the transition probability for a given context and next state
Sourcepub fn get_transition_probability_with_config(
&self,
context: &[String],
next_state: &str,
config: &AnomalyGridConfig,
) -> Option<f64>
pub fn get_transition_probability_with_config( &self, context: &[String], next_state: &str, config: &AnomalyGridConfig, ) -> Option<f64>
Get the transition probability with custom config
Sourcepub fn get_transition_probability_normalized(
&self,
context: &[String],
next_state: &str,
config: &AnomalyGridConfig,
global_state_mapping: &HashMap<String, usize>,
) -> Option<f64>
pub fn get_transition_probability_normalized( &self, context: &[String], next_state: &str, config: &AnomalyGridConfig, global_state_mapping: &HashMap<String, usize>, ) -> Option<f64>
Get the transition probability with proper normalization using global vocabulary
Sourcepub fn get_context_node(&self, context: &[String]) -> Option<&ContextNode>
pub fn get_context_node(&self, context: &[String]) -> Option<&ContextNode>
Get a context node for the given context
Sourcepub fn get_context_count(&self, context: &[String]) -> Option<usize>
pub fn get_context_count(&self, context: &[String]) -> Option<usize>
Get the total count for a given context (for adaptive context selection)
Sourcepub fn get_contexts_of_order(&self, order: usize) -> Vec<Vec<String>>
pub fn get_contexts_of_order(&self, order: usize) -> Vec<Vec<String>>
Get all contexts of a specific order
Sourcepub fn context_count(&self) -> usize
pub fn context_count(&self) -> usize
Get the number of contexts stored
Sourcepub fn interner(&self) -> &Arc<StringInterner>
pub fn interner(&self) -> &Arc<StringInterner>
Get access to the string interner
Source§impl ContextTree
Context pruning for memory optimization
impl ContextTree
Context pruning for memory optimization
Sourcepub fn prune_low_frequency_contexts(&mut self, _min_count: usize) -> usize
pub fn prune_low_frequency_contexts(&mut self, _min_count: usize) -> usize
Remove contexts with low frequency counts
This removes contexts that have been observed fewer than min_count
times,
which can significantly reduce memory usage for large alphabets.
Note: Currently disabled for trie-based storage - will be reimplemented
Sourcepub fn prune_low_entropy_contexts(&mut self, _min_entropy: f64) -> usize
pub fn prune_low_entropy_contexts(&mut self, _min_entropy: f64) -> usize
Remove contexts with low entropy (deterministic contexts)
This removes contexts where the entropy is below the threshold, indicating highly predictable transitions.
Note: Currently disabled for trie-based storage - will be reimplemented
Sourcepub fn limit_context_count(&mut self, _max_contexts: usize) -> usize
pub fn limit_context_count(&mut self, _max_contexts: usize) -> usize
Keep only the most frequent contexts up to a maximum count
This is useful for memory-constrained environments where you want to keep only the most important contexts.
Note: Currently disabled for trie-based storage - will be reimplemented
Sourcepub fn estimate_memory_usage(&self) -> usize
pub fn estimate_memory_usage(&self) -> usize
Estimate memory usage of the context tree
This provides an estimate of the total memory used by the context tree, including the trie structure, context nodes, and transition counts.
Sourcepub fn get_context_statistics(&self) -> ContextStatistics
pub fn get_context_statistics(&self) -> ContextStatistics
Get context statistics for analysis
Returns detailed statistics about the context tree structure, including distribution by order and memory usage patterns.
Trait Implementations§
Source§impl Clone for ContextTree
impl Clone for ContextTree
Source§fn clone(&self) -> ContextTree
fn clone(&self) -> ContextTree
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for ContextTree
impl RefUnwindSafe for ContextTree
impl Send for ContextTree
impl Sync for ContextTree
impl Unpin for ContextTree
impl UnwindSafe for ContextTree
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