From f561e7af7c56a8f641f7d076d45b164bc22c5a83 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 7 Mar 2013 18:12:35 +0100 Subject: [PATCH] changed interface of Cache.S.with_cache_rec --- cache.ml | 30 ++++++++++-------------------- cache.mli | 5 ++--- tests/benchs.ml | 3 ++- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/cache.ml b/cache.ml index 4b581ff6..373d0140 100644 --- a/cache.ml +++ b/cache.ml @@ -49,10 +49,9 @@ module type S = sig val with_cache : 'a t -> (key -> 'a) -> key -> 'a (** 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. - The cache has as size the first (int) argument. - It returns both the cache, and the specialized function *) + It returns the specialized function. *) end (** 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_rec size f = + let with_cache_rec () f x = let rec f' x = f f' x in - (), f' + f' x end module Dummy2(X : sig type t end)(Y : sig type t end) = struct @@ -141,13 +140,10 @@ module Linear(X : EQ) = struct in search 0 - (** Partially apply the given function with a new cache of the - given size. It returns both the cache, and the specialized function *) - let with_cache_rec size f = - let cache = create size in + let with_cache_rec cache f x = (* make a recursive version of [f] that uses the cache *) let rec f' x = with_cache cache (fun x -> f f' x) x in - cache, f' + f' x end module Linear2(X : EQ)(Y : EQ) = struct @@ -220,13 +216,10 @@ module Replacing(X : HASH) = struct c.(i) <- Assoc (x, y); y - (** Partially apply the given function with a new cache of the - given size. It returns both the cache, and the specialized function *) - let with_cache_rec size f = - let cache = create size in + let with_cache_rec cache f x = (* make a recursive version of [f] that uses the cache *) let rec f' x = with_cache cache (fun x -> f f' x) x in - cache, f' + f' x end module Replacing2(X : HASH)(Y : HASH) = struct @@ -352,11 +345,8 @@ module LRU(X : HASH) = struct else insert c x y); y - (** Partially apply the given function with a new cache of the - given size. It returns both the cache, and the specialized function *) - let with_cache_rec size f = - let cache = create size in + let with_cache_rec cache f x = (* make a recursive version of [f] that uses the cache *) let rec f' x = with_cache cache (fun x -> f f' x) x in - cache, f' + f' x end diff --git a/cache.mli b/cache.mli index 78f2b770..bfead17f 100644 --- a/cache.mli +++ b/cache.mli @@ -51,10 +51,9 @@ module type S = sig val with_cache : 'a t -> (key -> 'a) -> key -> 'a (** 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. - The cache has as size the first (int) argument. - It returns both the cache, and the specialized function *) + It returns the specialized function. *) end (** Signature of a cache for pairs of values *) diff --git a/tests/benchs.ml b/tests/benchs.ml index 92ddf552..def296d9 100644 --- a/tests/benchs.ml +++ b/tests/benchs.ml @@ -14,7 +14,8 @@ module Fibo(C : Cache.S with type key = int) = struct | n -> fib' (n-1) + fib' (n-2) 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 end