forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtbl_gen.ml
212 lines (179 loc) · 6.12 KB
/
hashtbl_gen.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(* Hash tables *)
module type S = sig
type key
type 'a t
val create: int -> 'a t
val clear: 'a t -> unit
val reset: 'a t -> unit
val copy: 'a t -> 'a t
val add: 'a t -> key -> 'a -> unit
val modify_or_init: 'a t -> key -> ('a -> unit) -> (unit -> 'a) -> unit
val remove: 'a t -> key -> unit
val find_exn: 'a t -> key -> 'a
val find_all: 'a t -> key -> 'a list
val find_opt: 'a t -> key -> 'a option
(** return the key found in the hashtbl.
Use case: when you find the key existed in hashtbl,
you want to use the one stored in the hashtbl.
(they are semantically equivlanent, but may have other information different)
*)
val find_key_opt: 'a t -> key -> key option
val find_default: 'a t -> key -> 'a -> 'a
val replace: 'a t -> key -> 'a -> unit
val mem: 'a t -> key -> bool
val iter: (key -> 'a -> unit) -> 'a t -> unit
val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
val length: 'a t -> int
val stats: 'a t -> Hashtbl.statistics
val of_list2: key list -> 'a list -> 'a t
end
(* We do dynamic hashing, and resize the table and rehash the elements
when buckets become too long. *)
type ('a, 'b) t =
{ mutable size: int; (* number of entries *)
mutable data: ('a, 'b) bucketlist array; (* the buckets *)
mutable seed: int; (* for randomization *)
initial_size: int; (* initial array size *)
}
and ('a, 'b) bucketlist =
| Empty
| Cons of 'a * 'b * ('a, 'b) bucketlist
let create initial_size =
let s = Ext_util.power_2_above 16 initial_size in
{ initial_size = s; size = 0; seed = 0; data = Array.make s Empty }
let clear h =
h.size <- 0;
let len = Array.length h.data in
for i = 0 to len - 1 do
h.data.(i) <- Empty
done
let reset h =
h.size <- 0;
h.data <- Array.make h.initial_size Empty
let copy h = { h with data = Array.copy h.data }
let length h = h.size
let resize indexfun h =
let odata = h.data in
let osize = Array.length odata in
let nsize = osize * 2 in
if nsize < Sys.max_array_length then begin
let ndata = Array.make nsize Empty in
h.data <- ndata; (* so that indexfun sees the new bucket count *)
let rec insert_bucket = function
Empty -> ()
| Cons(key, data, rest) ->
insert_bucket rest; (* preserve original order of elements *)
let nidx = indexfun h key in
ndata.(nidx) <- Cons(key, data, ndata.(nidx)) in
for i = 0 to osize - 1 do
insert_bucket (Array.unsafe_get odata i)
done
end
let iter f h =
let rec do_bucket = function
| Empty ->
()
| Cons(k, d, rest) ->
f k d; do_bucket rest in
let d = h.data in
for i = 0 to Array.length d - 1 do
do_bucket (Array.unsafe_get d i)
done
let fold f h init =
let rec do_bucket b accu =
match b with
Empty ->
accu
| Cons(k, d, rest) ->
do_bucket rest (f k d accu) in
let d = h.data in
let accu = ref init in
for i = 0 to Array.length d - 1 do
accu := do_bucket d.(i) !accu
done;
!accu
let rec bucket_length accu = function
| Empty -> accu
| Cons(_, _, rest) -> bucket_length (accu + 1) rest
let stats h =
let mbl =
Array.fold_left (fun m b -> max m (bucket_length 0 b)) 0 h.data in
let histo = Array.make (mbl + 1) 0 in
Array.iter
(fun b ->
let l = bucket_length 0 b in
histo.(l) <- histo.(l) + 1)
h.data;
{Hashtbl.
num_bindings = h.size;
num_buckets = Array.length h.data;
max_bucket_length = mbl;
bucket_histogram = histo }
let rec small_bucket_mem eq key (lst : _ bucketlist) =
match lst with
| Empty -> false
| Cons(k1,_,rest1) ->
eq key k1 ||
match rest1 with
| Empty -> false
| Cons(k2,_,rest2) ->
eq key k2 ||
match rest2 with
| Empty -> false
| Cons(k3,_,rest3) ->
eq key k3 ||
small_bucket_mem eq key rest3
let rec small_bucket_opt eq key (lst : _ bucketlist) : _ option =
match lst with
| Empty -> None
| Cons(k1,d1,rest1) ->
if eq key k1 then Some d1 else
match rest1 with
| Empty -> None
| Cons(k2,d2,rest2) ->
if eq key k2 then Some d2 else
match rest2 with
| Empty -> None
| Cons(k3,d3,rest3) ->
if eq key k3 then Some d3 else
small_bucket_opt eq key rest3
let rec small_bucket_key_opt eq key (lst : _ bucketlist) : _ option =
match lst with
| Empty -> None
| Cons(k1,d1,rest1) ->
if eq key k1 then Some k1 else
match rest1 with
| Empty -> None
| Cons(k2,d2,rest2) ->
if eq key k2 then Some k2 else
match rest2 with
| Empty -> None
| Cons(k3,d3,rest3) ->
if eq key k3 then Some k3 else
small_bucket_key_opt eq key rest3
let rec small_bucket_default eq key default (lst : _ bucketlist) =
match lst with
| Empty -> default
| Cons(k1,d1,rest1) ->
if eq key k1 then d1 else
match rest1 with
| Empty -> default
| Cons(k2,d2,rest2) ->
if eq key k2 then d2 else
match rest2 with
| Empty -> default
| Cons(k3,d3,rest3) ->
if eq key k3 then d3 else
small_bucket_default eq key default rest3