1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
//! Evaluation Namespaces used for Variable-lookups and custom Functions. //! //! Several Evaluation Namespace types are defined, each with their own advantages: //! * [`EmptyNamespace`](#emptynamespace) -- Useful when you know that your //! expressions don't need to look up any variables. //! * BTreeMap -- A simple way to define variables and functions with a map. //! Type aliases: [StringToF64Namespace](#stringtof64namespace), //! [StrToF64Namespace](#strtof64namespace), //! [StringToCallbackNamespace](#stringtocallbacknamespace), //! [StrToCallbackNamespace](#strtocallbacknamespace) //! * [`FnMut(&str,Vec<f64>) -> Option<f64>`](#callback-fnmutstrvec---option) -- //! Define variables and custom functions using a callback function. //! * [`CachedCallbackNamespace`](#cachedcallbacknamespace) -- Like the above //! callback-based Namespace, but results are cached so the callback is not //! queried more than once for a given variable. //! * Vec<BTreeMap<String,f64>> -- Define variables with layered maps. //! Each layer is a separate 'scope'. Higher layers take precedence //! over lower layers. Very useful for creating scoped higher-level-languages. //! Type alias: [LayeredStringToF64Namespace](#layeredstringtof64namespace) //! //! # Examples //! //! ## EmptyNamespace //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut ns = fasteval::EmptyNamespace; //! //! let val = fasteval::ez_eval("sin(pi()/2)", &mut ns)?; //! assert_eq!(val, 1.0); //! //! Ok(()) //! } //! ``` //! //! ## StringToF64Namespace //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut ns = fasteval::StringToF64Namespace::new(); //! ns.insert("x".to_string(), 2.0); //! //! let val = fasteval::ez_eval("x * (x + 1)", &mut ns)?; //! assert_eq!(val, 6.0); //! //! Ok(()) //! } //! ``` //! //! ## StrToF64Namespace //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut ns = fasteval::StrToF64Namespace::new(); //! ns.insert("x", 2.0); //! //! let val = fasteval::ez_eval("x * (x + 1)", &mut ns)?; //! assert_eq!(val, 6.0); //! //! Ok(()) //! } //! ``` //! //! ## Callback: FnMut(&str,Vec<f64>) -> Option<f64> //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut num_lookups = 0; //! let mut cb = |name:&str, args:Vec<f64>| -> Option<f64> { //! num_lookups += 1; //! match name { //! "x" => Some(2.0), //! _ => None, //! } //! }; //! //! let val = fasteval::ez_eval("x * (x + 1)", &mut cb)?; //! assert_eq!(val, 6.0); //! assert_eq!(num_lookups, 2); // Notice that 'x' was looked-up twice. //! //! Ok(()) //! } //! ``` //! //! ## StringToCallbackNamespace //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut ns = fasteval::StringToCallbackNamespace::new(); //! ns.insert("x".to_string(), Box::new(|_args| 2.0)); //! ns.insert("double".to_string(), Box::new(|args| { //! args.get(0).map(|arg0| arg0*2.0).unwrap_or(std::f64::NAN) //! })); //! //! let val = fasteval::ez_eval("double(x + 1) + 1", &mut ns)?; //! assert_eq!(val, 7.0); //! //! Ok(()) //! } //! ``` //! //! ## StrToCallbackNamespace //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut ns = fasteval::StrToCallbackNamespace::new(); //! ns.insert("x", Box::new(|_args| 2.0)); //! ns.insert("double", Box::new(|args| { //! args.get(0).map(|arg0| arg0*2.0).unwrap_or(std::f64::NAN) //! })); //! //! let val = fasteval::ez_eval("double(x + 1) + 1", &mut ns)?; //! assert_eq!(val, 7.0); //! //! Ok(()) //! } //! ``` //! //! ## CachedCallbackNamespace //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut num_lookups = 0; //! let val = { //! let cb = |name:&str, args:Vec<f64>| -> Option<f64> { //! num_lookups += 1; //! match name { //! "x" => { //! // Pretend that it is very expensive to calculate this, //! // and that's why we want to use the CachedCallbackNamespace cache. //! for i in 0..1000000 { /* do work */ } // Fake Work for this example. //! Some(2.0) //! } //! _ => None, //! } //! }; //! let mut ns = fasteval::CachedCallbackNamespace::new(cb); //! //! fasteval::ez_eval("x * (x + 1)", &mut ns)? //! }; //! assert_eq!(val, 6.0); //! assert_eq!(num_lookups, 1); // Notice that only 1 lookup occurred. //! // The second 'x' value was cached. //! //! Ok(()) //! } //! ``` //! //! ## LayeredStringToF64Namespace //! ``` //! fn main() -> Result<(), fasteval::Error> { //! let mut layer1 = fasteval::StringToF64Namespace::new(); //! layer1.insert("x".to_string(), 2.0); //! layer1.insert("y".to_string(), 3.0); //! //! let mut layers : fasteval::LayeredStringToF64Namespace = vec![layer1]; //! //! let val = fasteval::ez_eval("x * y", &mut layers)?; //! assert_eq!(val, 6.0); //! //! // Let's add another layer which shadows the previous one: //! let mut layer2 = fasteval::StringToF64Namespace::new(); //! layer2.insert("x".to_string(), 3.0); //! layers.push(layer2); //! //! let val = fasteval::ez_eval("x * y", &mut layers)?; //! assert_eq!(val, 9.0); //! //! // Remove the top layer and we'll be back to what we had before: //! layers.pop(); //! //! let val = fasteval::ez_eval("x * y", &mut layers)?; //! assert_eq!(val, 6.0); //! //! Ok(()) //! } //! ``` //! //! ## Custom Namespace Types //! //! If the pre-defined Namespace types aren't perfect for your application, you //! can create your own namespace type -- just implemenet the `EvalNamespace` //! trait (and maybe the `Cached` and `Layered` traits too). Also, as //! `fasteval` becomes more mature and is used for more real-life things, I //! will continue to add more useful Namespace types. //! //! Here are a few ideas of possibly-useful custom Namespace types: //! //! * Vec<Fn(&str,Vec<f64>)->Option<f64>> -- This would be a `Layered` //! namespace, with each layer having its own callback. Really powerful! //! //! * CachedCallbacksNamespace -- Same as above, but with a cache for each //! layer. Good for expensive look-ups. use crate::error::Error; use std::collections::BTreeMap; //---- Types: /// All `fasteval` Namespaces must implement the `EvalNamespace` trait. pub trait EvalNamespace { /// Perform a variable/function lookup. /// /// May return cached values. fn lookup(&mut self, name:&str, args:Vec<f64>, keybuf:&mut String) -> Option<f64>; } /// Cache operations for `EvalNamespace`s. /// /// Implement this trait if your Namespace type uses a cache. pub trait Cached { /// Creates a new cached entry. If an entry with the same name already /// exists, an [`AlreadyExists` Error](../error/enum.Error.html#variant.AlreadyExists) is returned. fn cache_create(&mut self, name:String, val:f64) -> Result<(),Error>; /// Sets a cached entry. It doesn't matter whether or not a previous value /// existed with this name. fn cache_set( &mut self, name:String, val:f64); /// Clear all cached entries. Values will be recalculated and cached /// again the next time they are looked up. fn cache_clear(&mut self); } //// I don't want to put this into the public API until it is needed. // pub trait Layered { // fn push(&mut self); // fn pop(&mut self); // } /// Use `EmptyNamespace` when you know that you won't be looking up any variables. /// /// It is a zero-sized type, which means it gets optimized-away at compile time. /// /// [See module-level documentation for example.](index.html#emptynamespace) /// pub struct EmptyNamespace; /// `CachedCallbackNamespace` is useful when your variable/function lookups are expensive. /// /// Each variable+args combo will only be looked up once, and then it will be /// cached and re-used for subsequent lookups. /// /// [See module-level documentation for example.](index.html#cachedcallbacknamespace) /// pub struct CachedCallbackNamespace<'a> { cache:BTreeMap<String,f64>, cb :Box<dyn FnMut(&str, Vec<f64>)->Option<f64> + 'a>, // I think a reference would be more efficient than a Box, but then I would need to use a funky 'let cb=|n|{}; Namespace::new(&cb)' syntax. The Box results in a super convenient pass-the-cb-by-value API interface. } //// I am commenting these out until I need them in real-life. //// (I don't want to add things to the public API until necessary.) // pub struct CachedLayeredNamespace<'a> { // caches:Vec<BTreeMap<String,f64>>, // cb :Box<dyn FnMut(&str, Vec<f64>)->Option<f64> + 'a>, // } // pub struct Bubble<'a,NS> where NS:EvalNamespace+Layered+'a { // ns :&'a mut NS, // count:usize, // } //---- Impls: #[inline(always)] fn key_from_nameargs<'a,'b:'a>(keybuf:&'a mut String, name:&'b str, args:&[f64]) -> &'a str { if args.is_empty() { name } else { keybuf.clear(); keybuf.reserve(name.len() + args.len()*20); keybuf.push_str(name); for f in args { keybuf.push_str(" , "); keybuf.push_str(&f.to_string()); }; keybuf.as_str() } } /// Type alias for `BTreeMap<String,f64>` pub type StringToF64Namespace = BTreeMap<String,f64>; impl EvalNamespace for StringToF64Namespace { #[inline] fn lookup(&mut self, name:&str, args:Vec<f64>, keybuf:&mut String) -> Option<f64> { let key = key_from_nameargs(keybuf, name, &args); self.get(key).copied() } } /// Type alias for `BTreeMap<&'static str,f64>` pub type StrToF64Namespace = BTreeMap<&'static str,f64>; impl EvalNamespace for StrToF64Namespace { #[inline] fn lookup(&mut self, name:&str, args:Vec<f64>, keybuf:&mut String) -> Option<f64> { let key = key_from_nameargs(keybuf, name, &args); self.get(key).copied() } } /// Type alias for `BTreeMap<String, Box<dyn FnMut(Vec<f64>)->f64>>` /// /// This namespace type provides a very convenient way to register variables /// and custom functions. It is a bit slower than a pure callback, but it has /// isolation and composition advantages. pub type StringToCallbackNamespace<'a> = BTreeMap<String, Box<dyn FnMut(Vec<f64>)->f64 + 'a>>; impl EvalNamespace for StringToCallbackNamespace<'_> { #[inline] fn lookup(&mut self, name:&str, args:Vec<f64>, _keybuf:&mut String) -> Option<f64> { if let Some(f) = self.get_mut(name) { Some(f(args)) } else { None } } } /// Type alias for `BTreeMap<&'static str, Box<dyn FnMut(Vec<f64>)->f64>>` /// /// This namespace type provides a very convenient way to register variables /// and custom functions. It is a bit slower than a pure callback, but it has /// isolation and composition advantages. pub type StrToCallbackNamespace<'a> = BTreeMap<&'static str, Box<dyn FnMut(Vec<f64>)->f64 + 'a>>; impl EvalNamespace for StrToCallbackNamespace<'_> { #[inline] fn lookup(&mut self, name:&str, args:Vec<f64>, _keybuf:&mut String) -> Option<f64> { if let Some(f) = self.get_mut(name) { Some(f(args)) } else { None } } } /// Type alias for `Vec<BTreeMap<String,f64>>` pub type LayeredStringToF64Namespace = Vec<BTreeMap<String,f64>>; impl EvalNamespace for LayeredStringToF64Namespace { #[inline] fn lookup(&mut self, name:&str, args:Vec<f64>, keybuf:&mut String) -> Option<f64> { let key = key_from_nameargs(keybuf, name, &args); for map in self.iter().rev() { if let Some(&val) = map.get(key) { return Some(val); } } None } } // I'm not making a type alias for this because of the un-name-ability of closures: impl<F> EvalNamespace for F where F:FnMut(&str,Vec<f64>)->Option<f64> { #[inline] fn lookup(&mut self, name:&str, args:Vec<f64>, _keybuf:&mut String) -> Option<f64> { self(name,args) } } impl EvalNamespace for EmptyNamespace { /// Always returns `None`, indicating that the variable is undefined. #[inline] fn lookup(&mut self, _name:&str, _args:Vec<f64>, _keybuf:&mut String) -> Option<f64> { None } } impl EvalNamespace for CachedCallbackNamespace<'_> { /// Returns a cached value if possible, otherwise delegates to the callback function. fn lookup(&mut self, name:&str, args:Vec<f64>, keybuf:&mut String) -> Option<f64> { let key = key_from_nameargs(keybuf, name, &args); if let Some(&val) = self.cache.get(key) { return Some(val); } match (self.cb)(name,args) { Some(val) => { self.cache.insert(key.to_string(),val); Some(val) } None => None, } } } impl Cached for CachedCallbackNamespace<'_> { fn cache_create(&mut self, name:String, val:f64) -> Result<(),Error> { if self.cache.contains_key(&name) { return Err(Error::AlreadyExists); } self.cache.insert(name, val); Ok(()) } fn cache_set(&mut self, name:String, val:f64) { self.cache.insert(name, val); } fn cache_clear(&mut self) { self.cache = BTreeMap::new(); } } impl<'a> CachedCallbackNamespace<'a> { #[inline] pub fn new<F>(cb:F) -> Self where F:FnMut(&str,Vec<f64>)->Option<f64> + 'a { CachedCallbackNamespace{ cache:BTreeMap::new(), cb :Box::new(cb), } } } //// I am not ready to make this part of the public API yet. // impl EvalNamespace for CachedLayeredNamespace<'_> { // fn lookup(&mut self, name:&str, args:Vec<f64>, keybuf:&mut String) -> Option<f64> { // let key = key_from_nameargs(keybuf, name, &args); // // for map in self.caches.iter().rev() { // if let Some(&val) = map.get(key) { return Some(val); } // } // // match (self.cb)(name,args) { // Some(val) => { // // I'm using this panic-free 'match' structure for performance: // match self.caches.last_mut() { // Some(m_ref) => { m_ref.insert(key.to_string(),val); } // None => (), // unreachable // } // Some(val) // } // None => None, // } // } // fn cache_create(&mut self, name:String, val:f64) -> Result<(),Error> { // match self.caches.last_mut() { // Some(cur_layer) => { // if cur_layer.contains_key(&name) { return Err(Error::AlreadyExists); } // cur_layer.insert(name, val); // } // None => return Err(Error::Unreachable), // }; // Ok(()) // } // fn cache_set(&mut self, name:String, val:f64) { // match self.caches.last_mut() { // Some(m_ref) => { m_ref.insert(name, val); } // None => (), // unreachable // } // } // fn cache_clear(&mut self) { // self.caches = Vec::with_capacity(self.caches.len()); // Assume the future usage will be similar to historical usage. // self.push(); // } // } // impl Layered for CachedLayeredNamespace<'_> { // #[inline] // fn push(&mut self) { // self.caches.push(BTreeMap::new()); // } // #[inline] // fn pop(&mut self) { // self.caches.pop(); // } // } // impl<'a> CachedLayeredNamespace<'a> { // #[inline] // pub fn new<F>(cb:F) -> Self where F:FnMut(&str,Vec<f64>)->Option<f64> + 'a { // let mut ns = CachedLayeredNamespace{ // caches:Vec::with_capacity(2), // cb :Box::new(cb), // }; // ns.push(); // ns // } // } //// I am not ready to make this part of the public API yet. // impl<NS> Bubble<'_,NS> where NS:EvalNamespace+Layered { // pub fn new<'a>(ns:&'a mut NS) -> Bubble<'a,NS> { // Bubble{ // ns, // count:0, // } // } // } // impl<NS> Drop for Bubble<'_,NS> where NS:EvalNamespace+Layered { // fn drop(&mut self) { // while self.count>0 { // self.pop(); // } // } // } // impl<NS> EvalNamespace for Bubble<'_,NS> where NS:EvalNamespace+Layered { // #[inline] // fn lookup(&mut self, name:&str, args:Vec<f64>, keybuf:&mut String) -> Option<f64> { // self.ns.lookup(name,args,keybuf) // } // #[inline] // fn cache_create(&mut self, name:String, val:f64) -> Result<(),Error> { // self.ns.cache_create(name,val) // } // #[inline] // fn cache_set(&mut self, name:String, val:f64) { // self.ns.cache_set(name,val) // } // #[inline] // fn cache_clear(&mut self) { // self.ns.cache_clear() // } // } // impl<NS> Layered for Bubble<'_,NS> where NS:EvalNamespace+Layered { // #[inline] // fn push(&mut self) { // self.ns.push(); // self.count = self.count+1; // } // #[inline] // fn pop(&mut self) { // if self.count>0 { // self.count = self.count+1; // self.ns.pop(); // } // } // } //// Commented out until we start using a layered namespace again. // #[cfg(test)] // mod internal_tests { // use super::*; // // #[test] // fn bubble() { // let mut ns = CachedLayeredNamespace::new(|_,_| None); // assert_eq!(ns.caches.len(), 1); // { // let mut bub = Bubble::new(&mut ns); bub.push(); // assert_eq!(bub.ns.caches.len(), 2); // bub.push(); // assert_eq!(bub.ns.caches.len(), 3); // bub.push(); // assert_eq!(bub.ns.caches.len(), 4); // bub.pop(); // assert_eq!(bub.ns.caches.len(), 3); // } // assert_eq!(ns.caches.len(), 1); // } // }