summaryrefslogtreecommitdiffstats
path: root/otherlibs/labltk/browser/jg_memo.ml
diff options
context:
space:
mode:
authorJacques Garrigue <garrigue at math.nagoya-u.ac.jp>1999-12-10 09:40:51 +0000
committerJacques Garrigue <garrigue at math.nagoya-u.ac.jp>1999-12-10 09:40:51 +0000
commitc5d28c52dd8fa81cc7b09c39b3fa751c1578a7d8 (patch)
treec3594e4a904e2345f8ad087dd32c5f28e375c303 /otherlibs/labltk/browser/jg_memo.ml
parente6343bfb447c8fb170981d46b5debb7855d95e47 (diff)
various improvements
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@2684 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'otherlibs/labltk/browser/jg_memo.ml')
-rw-r--r--otherlibs/labltk/browser/jg_memo.ml28
1 files changed, 16 insertions, 12 deletions
diff --git a/otherlibs/labltk/browser/jg_memo.ml b/otherlibs/labltk/browser/jg_memo.ml
index 43a5eb15b..0387b6398 100644
--- a/otherlibs/labltk/browser/jg_memo.ml
+++ b/otherlibs/labltk/browser/jg_memo.ml
@@ -1,17 +1,21 @@
(* $Id$ *)
-class ['a,'b] c fun:(f : 'a -> 'b) = object
- val hash = Hashtbl.create 7
- method get key =
- try Hashtbl.find hash :key
+type ('a, 'b) assoc_list =
+ Nil
+ | Cons of 'a * 'b * ('a, 'b) assoc_list
+
+let rec assq :key = function
+ Nil -> raise Not_found
+ | Cons (a, b, l) ->
+ if key == a then b else assq :key l
+
+let fast fun:f =
+ let memo = ref Nil in
+ fun key ->
+ try assq :key !memo
with Not_found ->
let data = f key in
- Hashtbl.add hash :key :data;
+ memo := Cons(key, data, !memo);
data
- method clear = Hashtbl.clear hash
- method reget key =
- Hashtbl.remove :key hash;
- let data = f key in
- Hashtbl.add hash :key :data;
- data
-end
+
+