1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
type +'a hash_consed =
{ node : 'a
; tag : int
}
let get_initial_cache_size, set_initial_cache_size, reset_initial_cache_size =
let default = 512 in
let initial_cache_size = ref default in
( (fun () -> !initial_cache_size)
, (fun size -> initial_cache_size := size)
, fun () -> initial_cache_size := default )
module type Cache = sig
type key
type !'a t
val create : int -> 'a t
val clear : 'a t -> unit
val add : 'a t -> key -> 'a -> unit
val find : 'a t -> key -> 'a
val length : 'a t -> int
val stats : 'a t -> Hashtbl.statistics
end
module Mk (Cache : Cache) : sig
type key = Cache.key
val clear : unit -> unit
val hashcons : key -> key hash_consed
val stats : unit -> Hashtbl.statistics
val length : unit -> int
end = struct
type key = Cache.key
let tbl = Cache.create (get_initial_cache_size ())
let count = ref ~-1
let gen_tag () =
incr count;
!count
let hashcons node =
match Cache.find tbl node with
| exception Not_found ->
let tag = gen_tag () in
let v = { tag; node } in
Cache.add tbl node v;
v
| v -> v
let clear () = Cache.clear tbl
let stats () = Cache.stats tbl
let length () = Cache.length tbl
end
module Make (H : Hashtbl.HashedType) = struct
include Mk (Ephemeron.K1.Make (H))
end
module MakeStrong (H : Hashtbl.HashedType) = struct
include Mk (Hashtbl.Make (H))
end
module Fake (H : Hashtbl.HashedType) = struct
include (Mk (struct
type key = H.t
type 'a t = | Unit
let create (_size : int) = Unit
let clear Unit = ()
let add (Unit : 'a t) (_v: key) (_ : 'a) = ()
let find Unit (_v: key) = raise_notrace Not_found
let length Unit = 0
let stats Unit = {
Hashtbl.num_bindings = 0;
num_buckets = 0;
max_bucket_length = 0;
bucket_histogram = [||]
}
end
))
end