Debian Codegraph @

clib/hashset.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** Adapted from Damien Doligez, projet Para, INRIA Rocquencourt,
12 OCaml stdlib. *)
13
14 (** The following functor is a specialized version of [Weak.Make].
15 Here, the responsibility of computing the hash function is now
16 given to the caller, which makes possible the interleaving of the
17 hash key computation and the hash-consing. *)
18
19 module type EqType = sig
20 type t
21 val eq : t -> t -> bool
22 end
23
24 type statistics = {
25 num_bindings: int;
26 num_buckets: int;
27 max_bucket_length: int;
28 bucket_histogram: int array
29 }
30
31 module type S = sig
32 type elt
33 type t
34 val create : int -> t
35 val clear : t -> unit
36 val repr : int -> elt -> t -> elt
37 val stats : t -> statistics
38 end
39
40 module Make (E : EqType) =
41 struct
42
43 type elt = E.t
44
45 let emptybucket = Weak.create 0
46
47 type t = {
48 mutable table : elt Weak.t array;
49 mutable hashes : int array array;
50 mutable limit : int; (* bucket size limit *)
51 mutable oversize : int; (* number of oversize buckets *)
52 mutable rover : int; (* for internal bookkeeping *)
53 }
54
55 let get_index t h = (h land max_int) mod (Array.length t)
56
57 let limit = 7
58 let over_limit = 2
59
60 let create sz =
61 let sz = if sz < 7 then 7 else sz in
62 let sz = if sz > Sys.max_array_length then Sys.max_array_length else sz in
63 {
64 table = Array.make sz emptybucket;
65 hashes = Array.make sz [| |];
66 limit = limit;
67 oversize = 0;
68 rover = 0;
69 }
70
71 let clear t =
72 for i = 0 to Array.length t.table - 1 do
73 t.table.(i) <- emptybucket;
74 t.hashes.(i) <- [| |];
75 done;
76 t.limit <- limit;
77 t.oversize <- 0
78
79 let iter_weak f t =
80 let rec iter_bucket i j b =
81 if i >= Weak.length b then () else
82 match Weak.check b i with
83 | true -> f b t.hashes.(j) i; iter_bucket (i+1) j b
84 | false -> iter_bucket (i+1) j b
85 in
86 for i = 0 to pred (Array.length t.table) do
87 iter_bucket 0 i (Array.unsafe_get t.table i)
88 done
89
90 let rec count_bucket i b accu =
91 if i >= Weak.length b then accu else
92 count_bucket (i+1) b (accu + (if Weak.check b i then 1 else 0))
93
94 let min x y = if x - y < 0 then x else y
95
96 let next_sz n = min (3 * n / 2 + 3) Sys.max_array_length
97 let prev_sz n = ((n - 3) * 2 + 2) / 3
98
99 let test_shrink_bucket t =
100 let bucket = t.table.(t.rover) in
101 let hbucket = t.hashes.(t.rover) in
102 let len = Weak.length bucket in
103 let prev_len = prev_sz len in
104 let live = count_bucket 0 bucket 0 in
105 if live <= prev_len then begin
106 let rec loop i j =
107 if j >= prev_len then begin
108 if Weak.check bucket i then loop (i + 1) j
109 else if Weak.check bucket j then begin
110 Weak.blit bucket j bucket i 1;
111 hbucket.(i) <- hbucket.(j);
112 loop (i + 1) (j - 1);
113 end else loop i (j - 1);
114 end;
115 in
116 loop 0 (Weak.length bucket - 1);
117 if prev_len = 0 then begin
118 t.table.(t.rover) <- emptybucket;
119 t.hashes.(t.rover) <- [| |];
120 end else begin
121 let newbucket = Weak.create prev_len in
122 Weak.blit bucket 0 newbucket 0 prev_len;
123 t.table.(t.rover) <- newbucket;
124 t.hashes.(t.rover) <- Array.sub hbucket 0 prev_len
125 end;
126 if len > t.limit && prev_len <= t.limit then t.oversize <- t.oversize - 1;
127 end;
128 t.rover <- (t.rover + 1) mod (Array.length t.table)
129
130 let rec resize t =
131 let oldlen = Array.length t.table in
132 let newlen = next_sz oldlen in
133 if newlen > oldlen then begin
134 let newt = create newlen in
135 let add_weak ob oh oi =
136 let setter nb ni _ = Weak.blit ob oi nb ni 1 in
137 let h = oh.(oi) in
138 add_aux newt setter None h (get_index newt.table h);
139 in
140 iter_weak add_weak t;
141 t.table <- newt.table;
142 t.hashes <- newt.hashes;
143 t.limit <- newt.limit;
144 t.oversize <- newt.oversize;
145 t.rover <- t.rover mod Array.length newt.table;
146 end else begin
147 t.limit <- max_int; (* maximum size already reached *)
148 t.oversize <- 0;
149 end
150
151 and add_aux t setter d h index =
152 let bucket = t.table.(index) in
153 let hashes = t.hashes.(index) in
154 let sz = Weak.length bucket in
155 let rec loop i =
156 if i >= sz then begin
157 let newsz = min (3 * sz / 2 + 3) (Sys.max_array_length - 1) in
158 if newsz <= sz then failwith "Weak.Make: hash bucket cannot grow more";
159 let newbucket = Weak.create newsz in
160 let newhashes = Array.make newsz 0 in
161 Weak.blit bucket 0 newbucket 0 sz;
162 Array.blit hashes 0 newhashes 0 sz;
163 setter newbucket sz d;
164 newhashes.(sz) <- h;
165 t.table.(index) <- newbucket;
166 t.hashes.(index) <- newhashes;
167 if sz <= t.limit && newsz > t.limit then begin
168 t.oversize <- t.oversize + 1;
169 for _i = 0 to over_limit do test_shrink_bucket t done;
170 end;
171 if t.oversize > Array.length t.table / over_limit then resize t
172 end else if Weak.check bucket i then begin
173 loop (i + 1)
174 end else begin
175 setter bucket i d;
176 hashes.(i) <- h
177 end
178 in
179 loop 0
180
181 external unsafe_weak_get : 'a Weak.t -> int -> 'a option = "caml_weak_get"
182
183 let repr h d t =
184 let table = t.table in
185 let index = get_index table h in
186 let bucket = Array.unsafe_get table index in
187 let hashes = Array.unsafe_get t.hashes index in
188 let sz = Weak.length bucket in
189 let pos = ref 0 in
190 let ans = ref None in
191 while !pos < sz && !ans == None do
192 let i = !pos in
193 if Int.equal h (Array.unsafe_get hashes i) then begin
194 match unsafe_weak_get bucket i with
195 | Some v as res when E.eq v d -> ans := res
196 | _ -> incr pos
197 end else incr pos
198 done;
199 match !ans with
200 | Some v -> v
201 | None ->
202 let () = add_aux t Weak.set (Some d) h index in
203 d
204
205 let stats t =
206 let fold accu bucket = max (count_bucket 0 bucket 0) accu in
207 let max_length = Array.fold_left fold 0 t.table in
208 let histogram = Array.make (max_length + 1) 0 in
209 let iter bucket =
210 let len = count_bucket 0 bucket 0 in
211 histogram.(len) <- succ histogram.(len)
212 in
213 let () = Array.iter iter t.table in
214 let fold (num, len, i) k = (num + k * i, len + k, succ i) in
215 let (num, len, _) = Array.fold_left fold (0, 0, 0) histogram in
216 {
217 num_bindings = num;
218 num_buckets = len;
219 max_bucket_length = Array.length histogram;
220 bucket_histogram = histogram;
221 }
222
223 end
224
225 module Combine = struct
226 (* These are helper functions to combine the hash keys in a similar
227 way as [Hashtbl.hash] does. The constants [alpha] and [beta] must
228 be prime numbers. There were chosen empirically. Notice that the
229 problem of hashing trees is hard and there are plenty of study on
230 this topic. Therefore, there must be room for improvement here. *)
231 let alpha = 65599
232 let beta = 7
233 let combine x y = x * alpha + y
234 let combine3 x y z = combine x (combine y z)
235 let combine4 x y z t = combine x (combine3 y z t)
236 let combine5 x y z t u = combine x (combine4 y z t u)
237 let combinesmall x y = beta * x + y
238 end