pub struct Expression<'arena> { /* private fields */ }Expand description
Arena-aware batch builder for zero-allocation expression evaluation
This structure is similar to BatchBuilder but uses an arena for all AST allocations, eliminating dynamic memory allocation during evaluation.
Implementations§
Source§impl<'arena> Expression<'arena>
impl<'arena> Expression<'arena>
Sourcepub fn add_expression(&mut self, expr: &str) -> Result<usize, ExprError>
pub fn add_expression(&mut self, expr: &str) -> Result<usize, ExprError>
Add an expression to be evaluated
The expression is parsed immediately into the arena. Returns the index of the added expression.
Sourcepub fn add_parameter(
&mut self,
name: &str,
initial_value: Real,
) -> Result<usize, ExprError>
pub fn add_parameter( &mut self, name: &str, initial_value: Real, ) -> Result<usize, ExprError>
Add a parameter with an initial value
Returns an error if a parameter with the same name already exists. Returns the index of the added parameter.
Sourcepub fn set_param(&mut self, idx: usize, value: Real) -> Result<(), ExprError>
pub fn set_param(&mut self, idx: usize, value: Real) -> Result<(), ExprError>
Update a parameter value by index (fastest method)
Sourcepub fn set_param_by_name(
&mut self,
name: &str,
value: Real,
) -> Result<(), ExprError>
pub fn set_param_by_name( &mut self, name: &str, value: Real, ) -> Result<(), ExprError>
Update a parameter value by name (convenient but slower)
Sourcepub fn eval(&mut self, base_ctx: &Rc<EvalContext>) -> Result<(), ExprError>
pub fn eval(&mut self, base_ctx: &Rc<EvalContext>) -> Result<(), ExprError>
Evaluate all expressions with current parameter values
Sourcepub fn get_result(&self, expr_idx: usize) -> Option<Real>
pub fn get_result(&self, expr_idx: usize) -> Option<Real>
Get the result of a specific expression by index
Sourcepub fn get_all_results(&self) -> &[Real] ⓘ
pub fn get_all_results(&self) -> &[Real] ⓘ
Get all results as a slice
Sourcepub fn param_count(&self) -> usize
pub fn param_count(&self) -> usize
Get the number of parameters
Sourcepub fn expression_count(&self) -> usize
pub fn expression_count(&self) -> usize
Get the number of expressions
Sourcepub fn register_expression_function(
&mut self,
name: &str,
params: &[&str],
body: &str,
) -> Result<(), ExprError>
pub fn register_expression_function( &mut self, name: &str, params: &[&str], body: &str, ) -> Result<(), ExprError>
Register a local expression function for this batch
Expression functions are mathematical expressions that can call other functions. They are specific to this batch and take precedence over context functions.
§Arguments
name- Function nameparams- Parameter namesbody- Expression string defining the function
Sourcepub fn arena_allocated_bytes(&self) -> usize
pub fn arena_allocated_bytes(&self) -> usize
Get the current number of bytes allocated in the arena
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all expressions, parameters, results, and local functions from this batch
This allows the batch to be reused without recreating it. The arena memory used by previous expressions remains allocated but unused until the arena is reset. The evaluation engine is retained for reuse.
§Example
use bumpalo::Bump;
use exp_rs::expression::Expression;
let arena = Bump::new();
let mut batch = Expression::new(&arena);
batch.add_expression("x + 1").unwrap();
batch.add_parameter("x", 5.0).unwrap();
// Clear and reuse
batch.clear();
assert_eq!(batch.expression_count(), 0);
assert_eq!(batch.param_count(), 0);Sourcepub fn eval_simple(expr: &str, arena: &'arena Bump) -> Result<Real, ExprError>
pub fn eval_simple(expr: &str, arena: &'arena Bump) -> Result<Real, ExprError>
Evaluate a single expression without parameters
This is the simplest way to evaluate an expression that doesn’t need variables.
§Example
use bumpalo::Bump;
use exp_rs::expression::Expression;
let arena = Bump::new();
let result = Expression::eval_simple("2 + 3 * 4", &arena).unwrap();
assert_eq!(result, 14.0);Sourcepub fn eval_with_context(
expr: &str,
ctx: &Rc<EvalContext>,
arena: &'arena Bump,
) -> Result<Real, ExprError>
pub fn eval_with_context( expr: &str, ctx: &Rc<EvalContext>, arena: &'arena Bump, ) -> Result<Real, ExprError>
Evaluate a single expression with context
Use this when you have a context with pre-defined variables, constants, or functions.
§Example
use bumpalo::Bump;
use exp_rs::{expression::Expression, EvalContext};
use std::rc::Rc;
let arena = Bump::new();
let mut ctx = EvalContext::new();
ctx.set_parameter("x", 5.0);
let result = Expression::eval_with_context("x * 2", &Rc::new(ctx), &arena).unwrap();
assert_eq!(result, 10.0);Sourcepub fn eval_with_params(
expr: &str,
params: &[(&str, Real)],
ctx: &Rc<EvalContext>,
arena: &'arena Bump,
) -> Result<Real, ExprError>
pub fn eval_with_params( expr: &str, params: &[(&str, Real)], ctx: &Rc<EvalContext>, arena: &'arena Bump, ) -> Result<Real, ExprError>
Evaluate a single expression with parameters
This is convenient when you want to provide parameters inline without creating a context.
§Example
use bumpalo::Bump;
use exp_rs::{expression::Expression, EvalContext};
use std::rc::Rc;
let arena = Bump::new();
let params = [("x", 3.0), ("y", 4.0)];
let ctx = Rc::new(EvalContext::new());
let result = Expression::eval_with_params("x^2 + y^2", ¶ms, &ctx, &arena).unwrap();
assert_eq!(result, 25.0); // 3^2 + 4^2 = 25Sourcepub fn set(&mut self, name: &str, value: Real) -> Result<(), ExprError>
pub fn set(&mut self, name: &str, value: Real) -> Result<(), ExprError>
Convenience setter using string slices
This is an alias for set_param_by_name with a shorter name for convenience.
§Example
use bumpalo::Bump;
use exp_rs::expression::Expression;
let arena = Bump::new();
let mut builder = Expression::new(&arena);
builder.add_parameter("x", 0.0).unwrap();
builder.set("x", 5.0).unwrap();