Skip to main content

SymbolTable

Struct SymbolTable 

Source
pub struct SymbolTable<T: Copy> { /* private fields */ }
Expand description

A symbol table containing variables, constants, functions, and arrays.

The symbol table is the runtime environment for expression evaluation. It stores all named values that can be referenced in expressions.

§Example

use exprkit::SymbolTable;

let mut table = SymbolTable::<f64>::new();

// Add variables
table.add_variable("x", 1.0);
table.add_variable("y", 2.0);

// Add constants
table.add_constant("pi", std::f64::consts::PI);

// Add arrays
table.add_vector("data", vec![1.0, 2.0, 3.0]);

// Add custom functions
table.add_function("square", Box::new(|args: &[f64]| args[0] * args[0]));

Implementations§

Source§

impl<T: From<f64> + Into<f64> + Copy> SymbolTable<T>

Source

pub fn new() -> Self

Creates a new empty symbol table.

Source

pub fn add_variable(&mut self, name: &str, initial: T) -> Slot

Adds a variable with the given name and initial value.

Returns a Slot that can be used for direct access to the variable’s value.

Source

pub fn add_vector(&mut self, name: &str, data: Vec<T>)

Adds a vector (array) with the given name and data.

Source

pub fn array_exists(&self, name: &str) -> bool

Source

pub fn string_exists(&self, name: &str) -> bool

Source

pub fn ensure_variable(&mut self, name: &str) -> Slot

Source

pub fn ensure_array(&mut self, name: &str, size: usize)

Source

pub fn variable_slot(&self, name: &str) -> Option<Slot>

Source

pub fn array_len(&self, name: &str) -> Option<usize>

Source

pub fn array_data(&self, name: &str) -> Option<&[T]>

Source

pub fn set_slot(&mut self, slot: Slot, value: T)

Source

pub fn get_slot(&self, slot: Slot) -> T

Source

pub fn set_value(&mut self, name: &str, value: T)

Source

pub fn set_null(&mut self, name: &str)

Source

pub fn value_of(&self, name: &str) -> Option<T>

Source

pub fn set_string_value(&mut self, name: &str, value: String)

Source

pub fn add_string_variable(&mut self, name: &str, value: String)

Source

pub fn get_string_value(&self, name: &str) -> Option<&String>

Source

pub fn get_value(&self, name: &str) -> Option<Value>

Source

pub fn variable_names(&self) -> Vec<String>

Source

pub fn remove_variable(&mut self, name: &str)

Source

pub fn array_names(&self) -> Vec<String>

Source

pub fn remove_array(&mut self, name: &str)

Source

pub fn string_variable_names(&self) -> Vec<String>

Source

pub fn remove_string_variable(&mut self, name: &str)

Source

pub fn reverse_array(&mut self, name: &str)

Source

pub fn reverse_array_range(&mut self, name: &str, start: usize, end: usize)

Source

pub fn array_value(&mut self, name: &str, index: usize) -> Option<T>

Source

pub fn set_array_value(&mut self, name: &str, index: usize, value: T)

Source

pub fn set_array_from_vec(&mut self, name: &str, values: &[f64])

Source

pub fn snapshot_array(&self, name: &str) -> Option<Vec<f64>>

Source

pub fn write_back_array(&mut self, name: &str, values: &[f64])

Source

pub fn fill_array(&mut self, name: &str, value: T)

Source

pub fn apply_array_op(&mut self, name: &str, op: Op, rhs: T)

Source

pub fn apply_array_vector_op(&mut self, name: &str, op: Op, values: &[f64])

Source

pub fn add_constant(&mut self, name: &str, value: T)

Source

pub fn mark_constant(&mut self, name: &str)

Source

pub fn add_string_constant(&mut self, name: &str, value: String)

Source

pub fn add_user_function(&mut self, name: &str, params: Vec<String>, body: Node)

Source

pub fn get_user_function(&self, name: &str) -> Option<&(Vec<String>, Node)>

Source

pub fn child(&self) -> Self

Source

pub fn merge_child_updates(&mut self, child: &Self)

Source

pub fn add_function(&mut self, name: &str, f: Box<dyn Fn(&[T]) -> T>)

Source

pub fn get_function(&self, name: &str) -> Option<Rc<Box<dyn Fn(&[T]) -> T>>>

Source

pub fn add_constants(&mut self)

Source

pub fn record_unary(&mut self, op: Op, operand: T, result: T)

Source

pub fn record_binary(&mut self, op: Op, lhs: T, rhs: T, result: T)

Source

pub fn record_function_call(&mut self, name: &str, args: &[T], result: T)

Source

pub fn trace(&self) -> &[TraceEvent<T>]

Source

pub fn take_trace(&mut self) -> Vec<TraceEvent<T>>

Source

pub fn clear_trace(&mut self)

Source

pub fn sum_array(&self, name: &str) -> Option<T>

Source

pub fn is_constant(&self, name: &str) -> bool

Source

pub fn enable_vector_bounds_check(&mut self, enable: bool)

Source

pub fn next_random(&mut self) -> f64

Source

pub fn next_gaussian(&mut self) -> f64

Trait Implementations§

Source§

impl<T: From<f64> + Into<f64> + Copy> Default for SymbolTable<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SymbolTable<T>

§

impl<T> !RefUnwindSafe for SymbolTable<T>

§

impl<T> !Send for SymbolTable<T>

§

impl<T> !Sync for SymbolTable<T>

§

impl<T> Unpin for SymbolTable<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SymbolTable<T>

§

impl<T> !UnwindSafe for SymbolTable<T>

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