-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hashcons.ml
54 lines (48 loc) · 1.1 KB
/
Hashcons.ml
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
type 'a hash_consed = {
node : 'a;
tag : int
}
let hash_value x = x.node
let hash_tag x = x.tag
module type Comp =
sig
type t
val equal : t -> t -> bool
val hash : t -> int
end
module type S =
sig
type t
val f : unit -> (t -> t hash_consed)
end
module Make (X : Comp) : (S with type t = X.t) =
struct
type t = X.t
type h_table = t hash_consed list array
let create n = Array.make n []
let f () =
let gen_tag = ref 0 in
let table = create 397 in
function x ->
let i = (X.hash x) mod (Array.length table) in
let rec look_and_add =
function
| [] ->
let hv = {tag = !gen_tag; node = x} in
table.(i) <- hv :: table.(i);
incr gen_tag;
hv
| hd :: tl ->
if X.equal hd.node x then
hd
else
look_and_add tl in
look_and_add table.(i)
end
let hashcons_resets = ref []
let init () = List.iter (fun f -> f()) !hashcons_resets
let register_hcons h u =
let hf = ref (h u) in
let reset () = hf := h u in
hashcons_resets := reset :: !hashcons_resets;
(function x -> !hf x)