Expand description
Cached function wrapper for expensive function evaluations.
CachedFunction wraps a user-supplied function Fn(&[I]) -> V with
thread-safe memoization. On first call for a given multi-index, the
function is evaluated and the result is cached; subsequent calls return
the cached value.
The internal cache key type is automatically selected based on the total
index space size (up to 1024 bits by default). For larger index spaces,
use CachedFunction::with_key_type with a custom CacheKey
implementation.
§Examples
use tensor4all_tcicore::CachedFunction;
let cf = CachedFunction::new(
|idx: &[usize]| idx[0] + idx[1],
&[3, 4],
).unwrap();
assert_eq!(cf.eval(&[1, 2]), 3);
assert_eq!(cf.num_evals(), 1);
assert_eq!(cf.eval(&[1, 2]), 3);
assert_eq!(cf.num_cache_hits(), 1);Modules§
- cache_
key - Cache key trait for flat-index computation.
- error
- Error types for cache key operations.
- index_
int - Index integer trait for multi-index elements.
Structs§
- Cached
Function - A wrapper that caches function evaluations for multi-index inputs.