64 val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table |
64 val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table |
65 type set = unit table |
65 type set = unit table |
66 val insert_set: key -> set -> set |
66 val insert_set: key -> set -> set |
67 val remove_set: key -> set -> set |
67 val remove_set: key -> set -> set |
68 val make_set: key list -> set |
68 val make_set: key list -> set |
69 val unsynchronized_cache: (key -> 'a) -> key -> 'a |
69 type 'a cache_ops = {apply: key -> 'a, reset: unit -> unit} |
|
70 val unsynchronized_cache: (key -> 'a) -> 'a cache_ops |
70 end; |
71 end; |
71 |
72 |
72 functor Table(Key: KEY): TABLE = |
73 functor Table(Key: KEY): TABLE = |
73 struct |
74 struct |
74 |
75 |
589 fun make_set xs = build (fold insert_set xs); |
590 fun make_set xs = build (fold insert_set xs); |
590 |
591 |
591 |
592 |
592 (* cache *) |
593 (* cache *) |
593 |
594 |
|
595 type 'a cache_ops = {apply: key -> 'a, reset: unit -> unit}; |
|
596 |
594 fun unsynchronized_cache f = |
597 fun unsynchronized_cache f = |
595 let |
598 let |
596 val cache1 = Unsynchronized.ref empty; |
599 val cache1 = Unsynchronized.ref empty; |
597 val cache2 = Unsynchronized.ref empty; |
600 val cache2 = Unsynchronized.ref empty; |
598 in |
601 fun apply x = |
599 fn x => |
|
600 (case lookup (! cache1) x of |
602 (case lookup (! cache1) x of |
601 SOME y => y |
603 SOME y => y |
602 | NONE => |
604 | NONE => |
603 (case lookup (! cache2) x of |
605 (case lookup (! cache2) x of |
604 SOME exn => Exn.reraise exn |
606 SOME exn => Exn.reraise exn |
605 | NONE => |
607 | NONE => |
606 (case Exn.result f x of |
608 (case Exn.result f x of |
607 Exn.Res y => (Unsynchronized.change cache1 (update (x, y)); y) |
609 Exn.Res y => (Unsynchronized.change cache1 (update (x, y)); y) |
608 | Exn.Exn exn => (Unsynchronized.change cache2 (update (x, exn)); Exn.reraise exn)))) |
610 | Exn.Exn exn => (Unsynchronized.change cache2 (update (x, exn)); Exn.reraise exn)))); |
609 end; |
611 fun reset () = (cache2 := empty; cache1 := empty); |
|
612 in {apply = apply, reset = reset} end; |
610 |
613 |
611 |
614 |
612 (* ML pretty-printing *) |
615 (* ML pretty-printing *) |
613 |
616 |
614 val _ = |
617 val _ = |