ContextTree

Struct ContextTree 

Source
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

Source

pub fn new(max_order: usize) -> AnomalyGridResult<Self>

Create a new context tree with specified maximum order

Source

pub fn with_interner( max_order: usize, interner: Arc<StringInterner>, ) -> AnomalyGridResult<Self>

Create a new context tree with existing string interner

Source

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
Source

pub fn get_transition_probability( &self, context: &[String], next_state: &str, ) -> Option<f64>

Get the transition probability for a given context and next state

Source

pub fn get_transition_probability_with_config( &self, context: &[String], next_state: &str, config: &AnomalyGridConfig, ) -> Option<f64>

Get the transition probability with custom config

Source

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

Source

pub fn get_context_node(&self, context: &[String]) -> Option<&ContextNode>

Get a context node for the given context

Source

pub fn get_context_count(&self, context: &[String]) -> Option<usize>

Get the total count for a given context (for adaptive context selection)

Source

pub fn get_contexts_of_order(&self, order: usize) -> Vec<Vec<String>>

Get all contexts of a specific order

Source

pub fn context_count(&self) -> usize

Get the number of contexts stored

Source

pub fn interner(&self) -> &Arc<StringInterner>

Get access to the string interner

Source

pub fn contexts(&self) -> HashMap<Vec<String>, ContextNode>

Get all contexts as a HashMap for compatibility with existing code

Note: This creates a temporary HashMap and should be used sparingly for compatibility with existing tests and code that expects the old interface

Source§

impl ContextTree

Context pruning for memory optimization

Source

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

Source

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

Source

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

Source

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.

Source

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

Source§

fn clone(&self) -> ContextTree

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 ContextTree

Source§

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

Formats the value using the given formatter. 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.