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
impl SymbolTable
pub fn new() -> SymbolTable
pub fn add_constant( &mut self, name: &str, value: c_double, ) -> Result<bool, InvalidName>
Sourcepub fn add_variable(
&mut self,
name: &str,
value: c_double,
) -> Result<Option<usize>, InvalidName>
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.
Sourcepub fn value(&self, var_id: usize) -> c_double
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.
Sourcepub fn value_mut(&mut self, var_id: usize) -> &mut c_double
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();
}Sourcepub fn value_cell(&self, var_id: usize) -> &Cell<c_double>
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.);Sourcepub fn value_from_name(&self, name: &str) -> Result<c_double, InvalidName>
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.
Sourcepub fn add_stringvar(
&mut self,
name: &str,
text: &str,
) -> Result<Option<usize>, InvalidName>
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.
pub fn set_string(&mut self, var_id: usize, text: &str) -> bool
Sourcepub fn string(&self, var_id: usize) -> &StringValue
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.
Sourcepub fn string_mut(&mut self, var_id: usize) -> &mut StringValue
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.
Sourcepub fn add_vector(
&mut self,
name: &str,
vec: &[c_double],
) -> Result<Option<usize>, InvalidName>
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.
Sourcepub fn vector(&self, var_id: usize) -> &[c_double] ⓘ
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.
Sourcepub fn vector_mut(&mut self, var_id: usize) -> &mut [c_double] ⓘ
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.
Sourcepub fn vector_of_cells(&self, var_id: usize) -> &[Cell<c_double>]
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
Sourcepub fn get_var_id(&self, name: &str) -> Result<Option<usize>, InvalidName>
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.
Sourcepub fn get_string_id(&self, name: &str) -> Result<Option<usize>, InvalidName>
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.
Sourcepub fn get_vec_id(&self, name: &str) -> Result<Option<usize>, InvalidName>
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.
pub fn clear_variables(&mut self)
pub fn clear_strings(&mut self)
pub fn clear_vectors(&mut self)
pub fn clear_local_constants(&mut self)
pub fn clear_functions(&mut self)
pub fn variable_count(&self) -> usize
pub fn stringvar_count(&self) -> usize
pub fn vector_count(&self) -> usize
pub fn function_count(&self) -> usize
pub fn add_constants(&mut self) -> bool
pub fn add_pi(&mut self) -> bool
pub fn add_epsilon(&mut self) -> bool
pub fn add_infinity(&mut self) -> bool
pub fn get_variable_names(&self) -> Vec<String>
pub fn get_stringvar_names(&self) -> Vec<String>
pub fn get_vector_names(&self) -> Vec<String>
pub fn symbol_exists(&self, name: &str) -> Result<bool, InvalidName>
pub fn is_constant_node(&self, name: &str) -> Result<bool, InvalidName>
pub fn is_constant_string(&self, name: &str) -> Result<bool, InvalidName>
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Source§impl SymbolTable
impl SymbolTable
Sourcepub fn add_func10<F>(
&mut self,
name: &str,
func: F,
) -> Result<bool, InvalidName>
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
impl Clone for SymbolTable
Source§fn clone(&self) -> SymbolTable
fn clone(&self) -> SymbolTable
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more