SymbolTable

Struct SymbolTable 

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

SymbolTable holds different variables. There are three types of variables: Numberic variables, strings and numeric vectors of fixed size. (see the documentation). Many but not all of the methods of the ExprTk symbol_table were implemented, and the API is sometimes different.

Implementations§

Source§

impl SymbolTable

Source

pub fn new() -> SymbolTable

Source

pub fn add_constant( &mut self, name: &str, value: c_double, ) -> Result<bool, InvalidName>

Source

pub fn add_variable( &mut self, name: &str, value: c_double, ) -> Result<Option<usize>, InvalidName>

Adds a new variable. Returns the variable ID that can later be used for set_value or None if a variable with the same name was already present. The behavior of this function differs from the one of the underlying library by not providing the (optional) is_constant option. Use add_constant() instead.

Source

pub fn value(&self, var_id: usize) -> c_double

Returns the value of a variable given its variable ID

§Panics

This function will panic if the var_id refers to an invalid (too large) variable ID.

Source

pub fn value_mut(&mut self, var_id: usize) -> &mut c_double

Returns a mutable reference to the value of a registered variable.

§Panics

This function will panic if the var_id refers to an invalid (too large) variable ID.

§Example:
use std::f64::consts::PI;
use exprtk_rs::*;

let mut symbol_table = SymbolTable::new();
let x_id = symbol_table.add_variable("x", 0.).expect("Invalid name").expect("Already present");
let mut expr = Expression::new("sin(x)", symbol_table).expect("Compile error");
assert_eq!(expr.value(), 0.);

let mut x = 0.;
while expr.symbols().value(x_id) <= 2. * PI {
    *expr.symbols_mut().value_mut(x_id) += 0.1;
    let y = expr.value();
}
Source

pub fn value_cell(&self, var_id: usize) -> &Cell<c_double>

Returns the value of a registered variable as modifiable std::cell::Cell. This is an alternative access to value_mut and allows changing the values easily. If the reference to the Cell is kept around, its value can be changed multiple times.

§Panics

This function will panic if the var_id refers to an invalid (too large) variable ID.

§Example:
use exprtk_rs::*;

let mut symbol_table = SymbolTable::new();
let id = symbol_table.add_variable("a", 2.).expect("Invalid name").expect("Already present");
let mut expr = Expression::new("a - 1", symbol_table).expect("Compile error");
assert_eq!(expr.value(), 1.);

let value = expr.symbols().value_cell(id);
value.set(4.);
assert_eq!(expr.value(), 3.);
Source

pub fn value_from_name(&self, name: &str) -> Result<c_double, InvalidName>

Returns the value of a variable (whether constant or not)

§Panics

This function will panic if the name refers to an unknown variable.

Source

pub fn add_stringvar( &mut self, name: &str, text: &str, ) -> Result<Option<usize>, InvalidName>

Adds a new string variable. Returns the variable ID that can later be used for set_string or None if a variable with the same name was already present.

Source

pub fn set_string(&mut self, var_id: usize, text: &str) -> bool

Source

pub fn string(&self, var_id: usize) -> &StringValue

Returns a reference to a string variable given its ID.

§Panics

This function will panic if the var_id refers to an invalid (too large) variable ID.

Source

pub fn string_mut(&mut self, var_id: usize) -> &mut StringValue

Returns a mutable reference to a string variable given its ID.

§Panics

This function will panic if the var_id refers to an invalid (too large) variable ID.

Source

pub fn add_vector( &mut self, name: &str, vec: &[c_double], ) -> Result<Option<usize>, InvalidName>

Adds a new vector variable. Returns the variable ID that can later be used for vector or None if a variable with the same name was already present.

Source

pub fn vector(&self, var_id: usize) -> &[c_double]

Returns a reference to a vector given its variable ID.

§Panics

This function will panic if the var_id refers to an invalid (too large) variable ID.

Source

pub fn vector_mut(&mut self, var_id: usize) -> &mut [c_double]

Returns an mutable reference to a vector given its variable ID.

§Panics

This function will panic if the var_id refers to an invalid (too large) variable ID.

Source

pub fn vector_of_cells(&self, var_id: usize) -> &[Cell<c_double>]

Returns a reference to a vector given its variable ID. The values are of the type std::cell::Cell, and can thus be modified without mutable access to SymbolTable

Source

pub fn get_var_id(&self, name: &str) -> Result<Option<usize>, InvalidName>

Returns the ‘ID’ of a variable or None if not found. The function will return Err(InvalidName) if the name is not entirely composed of ASCII characters.

Source

pub fn get_string_id(&self, name: &str) -> Result<Option<usize>, InvalidName>

Returns the ‘ID’ of a string or None if not found. The function will return Err(InvalidName) if the name is not entirely composed of ASCII characters.

Source

pub fn get_vec_id(&self, name: &str) -> Result<Option<usize>, InvalidName>

Returns the ‘ID’ of a vector or None if not found. The function will return Err(InvalidName) if the name is not entirely composed of ASCII characters.

Source

pub fn clear_variables(&mut self)

Source

pub fn clear_strings(&mut self)

Source

pub fn clear_vectors(&mut self)

Source

pub fn clear_local_constants(&mut self)

Source

pub fn clear_functions(&mut self)

Source

pub fn variable_count(&self) -> usize

Source

pub fn stringvar_count(&self) -> usize

Source

pub fn vector_count(&self) -> usize

Source

pub fn function_count(&self) -> usize

Source

pub fn add_constants(&mut self) -> bool

Source

pub fn add_pi(&mut self) -> bool

Source

pub fn add_epsilon(&mut self) -> bool

Source

pub fn add_infinity(&mut self) -> bool

Source

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

Source

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

Source

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

Source

pub fn symbol_exists(&self, name: &str) -> Result<bool, InvalidName>

Source

pub fn is_constant_node(&self, name: &str) -> Result<bool, InvalidName>

Source

pub fn is_constant_string(&self, name: &str) -> Result<bool, InvalidName>

Source§

impl SymbolTable

Source

pub fn add_func1<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>
where F: Fn(c_double) -> c_double + Clone,

Add a function with 1 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func2<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>
where F: Fn(c_double, c_double) -> c_double + Clone,

Add a function with 2 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func3<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>

Add a function with 3 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func4<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>

Add a function with 4 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func5<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>

Add a function with 5 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func6<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>

Add a function with 6 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func7<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>

Add a function with 7 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func8<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>

Add a function with 8 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func9<F>(&mut self, name: &str, func: F) -> Result<bool, InvalidName>

Add a function with 9 scalar arguments. Returns true if the function was added / false if the name was already present.

Source§

impl SymbolTable

Source

pub fn add_func10<F>( &mut self, name: &str, func: F, ) -> Result<bool, InvalidName>

Add a function with 10 scalar arguments. Returns true if the function was added / false if the name was already present.

Trait Implementations§

Source§

impl Clone for SymbolTable

Source§

fn clone(&self) -> SymbolTable

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 SymbolTable

Source§

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

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

impl Default for SymbolTable

Source§

fn default() -> Self

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

impl Drop for SymbolTable

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for SymbolTable

Source§

impl Sync for SymbolTable

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