Project

General

Profile

Download (1.11 KB) Statistics
| Branch: | Tag: | Revision:
1 93119c3f ploc
open Basetypes
2 2de7fa82 ploc
3 ca7ff3f7 Lélio Brun
type ('a, 'b) t = Memo : ('a, 'b) Hashtbl.t -> ('a, 'b) t
4 2de7fa82 ploc
5 ca7ff3f7 Lélio Brun
let create () = Memo (Hashtbl.create 97)
6 2de7fa82 ploc
7 ca7ff3f7 Lélio Brun
let reset (Memo hashf) = Hashtbl.reset hashf
8 2de7fa82 ploc
9 ca7ff3f7 Lélio Brun
let fold (Memo hashf) f e = Hashtbl.fold f hashf e
10 2de7fa82 ploc
11 ca7ff3f7 Lélio Brun
let apply (Memo hashf) f x =
12
  try
13
    Log.report ~level:sf_level (fun fmt -> Format.fprintf fmt "lookup 1@.");
14
    Hashtbl.find hashf x
15
  with Not_found ->
16
    let res = f x in
17
    Log.report ~level:sf_level (fun fmt -> Format.fprintf fmt "hashing 1@.");
18
    Hashtbl.add hashf x res;
19
    res
20 2de7fa82 ploc
21 ca7ff3f7 Lélio Brun
let apply2 (Memo hashf) f x y =
22
  try
23
    Log.report ~level:sf_level (fun fmt -> Format.fprintf fmt "lookup 2@.");
24
    Hashtbl.find hashf (x, y)
25
  with Not_found ->
26
    let res = f x y in
27
    Log.report ~level:sf_level (fun fmt -> Format.fprintf fmt "hashing 2@.");
28
    Hashtbl.add hashf (x, y) res;
29
    res
30
31
let apply3 (Memo hashf) f x y z =
32
  try
33
    Log.report ~level:sf_level (fun fmt -> Format.fprintf fmt "lookup 3@.");
34
    Hashtbl.find hashf (x, y, z)
35
  with Not_found ->
36
    let res = f x y z in
37
    Log.report ~level:sf_level (fun fmt -> Format.fprintf fmt "hashing 3@.");
38
    Hashtbl.add hashf (x, y, z) res;
39
    res