module CCCache:sig..end
Particularly useful for memoization. See CCCache.with_cache and CCCache.with_cache_rec
for more details.
Since 0.6
type'aequal ='a -> 'a -> bool
type'ahash ='a -> int
Typical use case: one wants to memoize a function f : 'a -> 'b. Code sample:
let f x =
print_endline "call f";
x + 1;;
let f' = with_cache (lru 256) f;;
f' 0;; (* prints *)
f' 1;; (* prints *)
f' 0;; (* doesn't print, returns cached value *)
type ('a, 'b) t
val clear : ('a, 'b) t -> unittype('a, 'b)callback =in_cache:bool -> 'a -> 'b -> unit
val with_cache : ?cb:('a, 'b) callback -> ('a, 'b) t -> ('a -> 'b) -> 'a -> 'bwith_cache c f behaves like f, but caches calls to f in the
cache c. It always returns the same value as
f x, if f x returns, or raise the same exception.
However, f may not be called if x is in the cache.cb : called after the value is generated or retrievedval with_cache_rec : ?cb:('a, 'b) callback ->
('a, 'b) t -> (('a -> 'b) -> 'a -> 'b) -> 'a -> 'bwith_cache_rec c f is a function that first, applies f to
some f' = fix f, such that recursive calls to f' are cached in c.
It is similar to CCCache.with_cache but with a function that takes as
first argument its own recursive version.
Example (memoized Fibonacci function):
let fib = with_cache_rec (lru 256)
(fun fib' n -> match n with
| 1 | 2 -> 1
| _ -> fib' (n-1) + fib' (n-2)
);;
fib 70;;
cb : called after the value is generated or retrievedval size : ('a, 'b) t -> intval iter : ('a, 'b) t -> ('a -> 'b -> unit) -> unitsize cache pairs.val dummy : ('a, 'b) tval linear : ?eq:'a equal -> int -> ('a, 'b) teq : optional equality predicate for keysval replacing : ?eq:'a equal -> ?hash:'a hash -> int -> ('a, 'b) tval lru : ?eq:'a equal -> ?hash:'a hash -> int -> ('a, 'b) tval unbounded : ?eq:'a equal -> ?hash:'a hash -> int -> ('a, 'b) tCCCache.clear is called manually.