Expand description
§Fast Expression Evaluators
The fastest expression evaluators
§Introduction
use fee::{prelude::*, DefaultResolver};
let mut var_resolver = DefaultResolver::empty();
var_resolver.insert("p0", 10.0);
var_resolver.insert("p1", 4.0);
let mut fn_resolver = DefaultResolver::empty();
fn_resolver.insert("abs", ExprFn::new(abs));
let context = Context::new(var_resolver, fn_resolver);
let mut stack = Vec::with_capacity(10);
let expr = Expr::compile("abs((2 + 4) * 6 / (p1 + 2)) + abs(-2)", &context).unwrap();
let result = expr.eval(&context, &mut stack).unwrap();
assert_eq!(result, 8.0);
fn abs(x: &[f64]) -> f64 {
x[0].abs()
}
§Expression
A generic struct representing a mathematical expression.
Use [Expr::<T>::compile
] to parse a string into a specialized [Expr<T>
] depending
on the provided [Context
].
§Context
A struct that holds resolvers for variables and functions used in an expression.
Contexts can be locked to prevent reallocation of inner resolvers, allowing
expressions to be evaluated using raw pointers instead of name lookups for maximum performance.
§Resolvers
A [Resolver
] maps variable or function names to their values/implementations.
Available resolvers include:
DefaultResolver
: No size or naming restrictions, but slower than specialized resolvers.IndexedResolver
: No size restrictions, but requires specific naming patterns. Very fast.SmallResolver
: Restricted size, but allows arbitrary names with good performance.ConstantResolver
: Always resolves to the same value; offers the best performance.EmptyResolver
: Always resolves toNone
; useful for expressions without variables or functions.
Modules§
Structs§
- Constant
Resolver - A resolver that always returns the same value regardless of the variable or function name.
- Default
Resolver - General-purpose resolver that stores values indexed by name.
- Empty
Resolver - A resolver that does not resolve any values.
- ExprFn
- Indexed
Resolver - High-performance resolver with O(1) lookup for variables and functions.
- Ptr
- This struct holds a pointer to a value of type
T
. Used by locked resolvers to safely access and modify a value without having to resolve the name. - Small
Resolver - A fast resolver with a small, fixed-size cache.