Revision ca7ff3f7
Added by LĂ©lio Brun over 1 year ago
src/tools/stateflow/semantics/memo.ml  

1  1 
open Basetypes 
2 


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

4  2  
5 
let create () = Memo (Hashtbl.create 97);;


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


6  4  
7 
let reset (Memo hashf) = 

8 
begin 

9 
Hashtbl.reset hashf 

10 
end 

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

11  6  
12 
let fold (Memo hashf) f e = 

13 
begin 

14 
Hashtbl.fold f hashf e 

15 
end;; 

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

16  8  
17 
let apply (Memo hashf) f = 

18 
fun x > 

19 
try 

20 
Log.report ~level:sf_level (fun fmt > Format.fprintf fmt "lookup 1@."); 

21 
Hashtbl.find hashf x 

22 
with Not_found > 

23 
let res = f x in 

24 
begin 

25 
Log.report ~level:sf_level (fun fmt > Format.fprintf fmt "hashing 1@."); 

26 
Hashtbl.add hashf x res; 

27 
res 

28 
end;; 

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

29  10  
30 
let apply2 (Memo hashf) f = 

31 
fun x y > 

32 
try 

33 
Log.report ~level:sf_level (fun fmt > Format.fprintf fmt "lookup 2@."); 

34 
Hashtbl.find hashf (x, y) 

35 
with Not_found > 

36 
let res = f x y in 

37 
begin 

38 
Log.report ~level:sf_level (fun fmt > Format.fprintf fmt "hashing 2@."); 

39 
Hashtbl.add hashf (x, y) res; 

40 
res 

41 
end;; 

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 

42  20  
43 
let apply3 (Memo hashf) f = 

44 
fun x y z > 

45 
try 

46 
Log.report ~level:sf_level (fun fmt > Format.fprintf fmt "lookup 3@."); 

47 
Hashtbl.find hashf (x, y, z) 

48 
with Not_found > 

49 
let res = f x y z in 

50 
begin 

51 
Log.report ~level:sf_level (fun fmt > Format.fprintf fmt "hashing 3@."); 

52 
Hashtbl.add hashf (x, y, z) res; 

53 
res 

54 
end;; 

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 
Also available in: Unified diff
reformatting