Project

General

Profile

Download (1.11 KB) Statistics
| Branch: | Tag: | Revision:
1
open Basetypes
2

    
3
type ('a, 'b) t = Memo : ('a, 'b) Hashtbl.t -> ('a, 'b) t
4

    
5
let create () = Memo (Hashtbl.create 97)
6

    
7
let reset (Memo hashf) = Hashtbl.reset hashf
8

    
9
let fold (Memo hashf) f e = Hashtbl.fold f hashf e
10

    
11
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

    
21
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
(7-7/9)