mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
changed interface of Cache.S.with_cache_rec
This commit is contained in:
parent
14ca51db92
commit
f561e7af7c
3 changed files with 14 additions and 24 deletions
30
cache.ml
30
cache.ml
|
|
@ -49,10 +49,9 @@ module type S = sig
|
||||||
val with_cache : 'a t -> (key -> 'a) -> key -> 'a
|
val with_cache : 'a t -> (key -> 'a) -> key -> 'a
|
||||||
(** Wrap the function with the cache *)
|
(** Wrap the function with the cache *)
|
||||||
|
|
||||||
val with_cache_rec : int -> ((key -> 'a) -> key -> 'a) -> ('a t * (key -> 'a))
|
val with_cache_rec : 'a t -> ((key -> 'a) -> key -> 'a) -> key -> 'a
|
||||||
(** Partially apply the given function with a cached version of itself.
|
(** Partially apply the given function with a cached version of itself.
|
||||||
The cache has as size the first (int) argument.
|
It returns the specialized function. *)
|
||||||
It returns both the cache, and the specialized function *)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Signature of a cache for pairs of values *)
|
(** Signature of a cache for pairs of values *)
|
||||||
|
|
@ -83,9 +82,9 @@ module Dummy(X : sig type t end) = struct
|
||||||
|
|
||||||
let with_cache () f x = f x
|
let with_cache () f x = f x
|
||||||
|
|
||||||
let with_cache_rec size f =
|
let with_cache_rec () f x =
|
||||||
let rec f' x = f f' x in
|
let rec f' x = f f' x in
|
||||||
(), f'
|
f' x
|
||||||
end
|
end
|
||||||
|
|
||||||
module Dummy2(X : sig type t end)(Y : sig type t end) = struct
|
module Dummy2(X : sig type t end)(Y : sig type t end) = struct
|
||||||
|
|
@ -141,13 +140,10 @@ module Linear(X : EQ) = struct
|
||||||
in
|
in
|
||||||
search 0
|
search 0
|
||||||
|
|
||||||
(** Partially apply the given function with a new cache of the
|
let with_cache_rec cache f x =
|
||||||
given size. It returns both the cache, and the specialized function *)
|
|
||||||
let with_cache_rec size f =
|
|
||||||
let cache = create size in
|
|
||||||
(* make a recursive version of [f] that uses the cache *)
|
(* make a recursive version of [f] that uses the cache *)
|
||||||
let rec f' x = with_cache cache (fun x -> f f' x) x in
|
let rec f' x = with_cache cache (fun x -> f f' x) x in
|
||||||
cache, f'
|
f' x
|
||||||
end
|
end
|
||||||
|
|
||||||
module Linear2(X : EQ)(Y : EQ) = struct
|
module Linear2(X : EQ)(Y : EQ) = struct
|
||||||
|
|
@ -220,13 +216,10 @@ module Replacing(X : HASH) = struct
|
||||||
c.(i) <- Assoc (x, y);
|
c.(i) <- Assoc (x, y);
|
||||||
y
|
y
|
||||||
|
|
||||||
(** Partially apply the given function with a new cache of the
|
let with_cache_rec cache f x =
|
||||||
given size. It returns both the cache, and the specialized function *)
|
|
||||||
let with_cache_rec size f =
|
|
||||||
let cache = create size in
|
|
||||||
(* make a recursive version of [f] that uses the cache *)
|
(* make a recursive version of [f] that uses the cache *)
|
||||||
let rec f' x = with_cache cache (fun x -> f f' x) x in
|
let rec f' x = with_cache cache (fun x -> f f' x) x in
|
||||||
cache, f'
|
f' x
|
||||||
end
|
end
|
||||||
|
|
||||||
module Replacing2(X : HASH)(Y : HASH) = struct
|
module Replacing2(X : HASH)(Y : HASH) = struct
|
||||||
|
|
@ -352,11 +345,8 @@ module LRU(X : HASH) = struct
|
||||||
else insert c x y);
|
else insert c x y);
|
||||||
y
|
y
|
||||||
|
|
||||||
(** Partially apply the given function with a new cache of the
|
let with_cache_rec cache f x =
|
||||||
given size. It returns both the cache, and the specialized function *)
|
|
||||||
let with_cache_rec size f =
|
|
||||||
let cache = create size in
|
|
||||||
(* make a recursive version of [f] that uses the cache *)
|
(* make a recursive version of [f] that uses the cache *)
|
||||||
let rec f' x = with_cache cache (fun x -> f f' x) x in
|
let rec f' x = with_cache cache (fun x -> f f' x) x in
|
||||||
cache, f'
|
f' x
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -51,10 +51,9 @@ module type S = sig
|
||||||
val with_cache : 'a t -> (key -> 'a) -> key -> 'a
|
val with_cache : 'a t -> (key -> 'a) -> key -> 'a
|
||||||
(** Wrap the function with the cache *)
|
(** Wrap the function with the cache *)
|
||||||
|
|
||||||
val with_cache_rec : int -> ((key -> 'a) -> key -> 'a) -> ('a t * (key -> 'a))
|
val with_cache_rec : 'a t -> ((key -> 'a) -> key -> 'a) -> key -> 'a
|
||||||
(** Partially apply the given function with a cached version of itself.
|
(** Partially apply the given function with a cached version of itself.
|
||||||
The cache has as size the first (int) argument.
|
It returns the specialized function. *)
|
||||||
It returns both the cache, and the specialized function *)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** Signature of a cache for pairs of values *)
|
(** Signature of a cache for pairs of values *)
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ module Fibo(C : Cache.S with type key = int) = struct
|
||||||
| n ->
|
| n ->
|
||||||
fib' (n-1) + fib' (n-2)
|
fib' (n-1) + fib' (n-2)
|
||||||
in
|
in
|
||||||
let _cache, cached_fib = C.with_cache_rec size fib in
|
let cache = C.create size in
|
||||||
|
let cached_fib x = C.with_cache_rec cache fib x in
|
||||||
cached_fib
|
cached_fib
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue