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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# 1 "Camomile/internal/tbl31.ml"
(** Tbl31 : fast table keyed by integers *)
let poly = 0x48000000
let crc_tbl = Array.init 128 (fun i ->
let rec loop j sum =
if j < 0 then sum else
if i land (1 lsl j) <> 0 then
loop (j - 1) (sum lxor (poly lsr j))
else
loop (j - 1) sum in
loop (7 - 1) 0)
let byte3 n = n lsr 24 land 127
let byte2 n = n lsr 16 land 255
let byte1 n = n lsr 8 land 255
let byte0 n = n land 255
let (lsl) x n =
if n >= Sys.word_size then 0 else
if n <= ~- Sys.word_size then 0 else
if n < 0 then x lsr (~-n) else
x lsl n
type 'a tbl = 'a array array array array
type 'a t = 'a tbl
type 'a tagged = Tag of 'a * int
let untag (Tag (a, _)) = a
let id (Tag (_, n)) = n
let get tbl n =
let lev = Array.unsafe_get tbl (byte3 n) in
let lev = Array.unsafe_get lev (byte2 n) in
let lev = Array.unsafe_get lev (byte1 n) in
Array.unsafe_get lev (byte0 n)
module type NodeType = sig
type elt
type t
val level : int
val make : elt -> t tagged
val of_map : int -> elt -> elt IMap.t -> t tagged
val of_set : int -> elt -> ISet.t -> elt -> t tagged
end
module MakeNode (Sub : NodeType) = struct
type elt = Sub.elt
type node = Sub.t array
type t = node
let level = Sub.level + 1
module NodeHash = struct
type t = node tagged
let equal x y =
let a = untag x in
let b = untag y in
let rec loop i =
if i < 0 then true else
if a.(i) == b.(i) then loop (i - 1) else
false in
loop (if level = 3 then 127 else 255)
let hash = id
end
module NodePool = Weak.Make (NodeHash)
let pool = NodePool.create 256
let crc_hash v =
let rec loop i sum =
if i < 0 then sum else
let a = id v.(i) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte3 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte2 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte1 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte0 a) land 0x7f) in
loop (i - 1) sum in
loop (if level = 3 then 127 else 255) 0
let hashcons a =
let n = crc_hash a in
let b = Array.map untag a in
let x = Tag (b, n) in
try NodePool.find pool x with Not_found ->
NodePool.add pool x;
x
let make_raw def =
Array.make (if level = 3 then 128 else 256) (Sub.make def)
let make def = hashcons (make_raw def)
let of_map n0 def m =
let a = make_raw def in begin
if IMap.is_empty m then () else
let l = AvlTree.left_branch m in
let r = AvlTree.right_branch m in
if IMap.is_empty l && IMap.is_empty r then
let k1, k2, v = AvlTree.root m in
let i1 = (k1 - n0) lsr (8 * level) in
let n1 = n0 lor (i1 lsl (8 * level)) in
let n2 = n1 lor (1 lsl (8 * level) - 1) in
a.(i1) <- Sub.of_map n1 def (IMap.until n2 (IMap.from n1 m));
let i2 = (k2 - n0) lsr (8 * level) in
if i1 <> i2 then
let n1 = n0 lor (i2 lsl (8 * level)) in
let n2 = n1 lor (1 lsl (8 * level) - 1) in
a.(i2) <- Sub.of_map n1 def (IMap.until n2 (IMap.from n1 m));
let b = Sub.make v in
for i = i1 + 1 to i2 - 1 do a.(i) <- b done;
else ()
else
for i = 0 to if level = 3 then 127 else 255 do
let n1 = n0 lor (i lsl (8 * level)) in
let n2 = n1 lor (1 lsl (8 * level) - 1) in
let m' = IMap.until n2 (IMap.from n1 m) in
if IMap.is_empty m' then () else
a.(i) <- Sub.of_map n1 def m'
done
end;
hashcons a
let of_set n0 def s v =
let a = make_raw def in
for i = 0 to if level = 3 then 127 else 255 do
let n1 = n0 lor (i lsl (8 * level)) in
let n2 = n1 lor (1 lsl (8 * level) - 1) in
let s' = ISet.until n2 (ISet.from n1 s) in
if ISet.is_empty s' then () else
a.(i) <- Sub.of_set n1 def s' v
done;
hashcons a
end
module MakeTbl (Lev0 : NodeType) = struct
module Lev1 = MakeNode (Lev0)
module Lev2 = MakeNode (Lev1)
module Lev3 = MakeNode (Lev2)
include Lev3
let get = get
let of_map def m = untag (Lev3.of_map 0 def m)
end
module ArrayLeaf (H : Hashtbl.HashedType) = struct
type elt = H.t
type t = elt array
type node = t
let level = 0
module NodeHash = struct
type t = node tagged
let equal x y =
let a = untag x in
let b = untag y in
let rec loop i =
if i >= 255 then true else
if H.equal a.(i) b.(i) then loop (i + 1) else
false in
loop 0
let hash = id
end
module Pool = Weak.Make (NodeHash)
let pool = Pool.create 256
let crc_hash v =
let rec loop i sum =
if i < 0 then sum else
let a = H.hash v.(i) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte3 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte2 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte1 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte0 a) land 0x7f) in
loop (i - 1) sum in
loop 255 0
let hashcons a =
let n = crc_hash a in
let x = Tag (a, n) in
try Pool.find pool x with Not_found ->
Pool.add pool x;
x
let make_raw def = Array.make 256 def
let make def = hashcons (make_raw def)
let of_map n0 def m =
let a = make_raw def in
IMap.iter_range (fun n1 n2 v ->
for i = n1 - n0 to n2 - n0 do a.(i) <- v done)
m;
hashcons a
let of_set n0 def s v =
let a = make_raw def in
ISet.iter_range (fun n1 n2 ->
for i = n1 - n0 to n2 - n0 do a.(i) <- v done)
s;
hashcons a
end
module type Type = sig
type elt
type t = elt tbl
val get : elt tbl -> int -> elt
val of_map : elt -> elt IMap.t -> elt tbl
end
module Make (H : Hashtbl.HashedType) = MakeTbl(ArrayLeaf(H))
module StringContentsHash = struct
type t = Bytes.t tagged
let equal x1 x2 =
let s1 = untag x1 in
let s2 = untag x2 in
if Bytes.length s1 <> Bytes.length s2 then false else
let rec loop i =
if i < 0 then true else
if Bytes.get s1 i <> Bytes.get s2 i then false else
loop (i - 1) in
loop (Bytes.length s1 - 1)
let hash = id
end
let bytes_hash v =
let rec loop i sum =
if i < 0 then sum else
let a = Char.code (Bytes.get v i) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor a land 0x7f) in
loop (i - 1) sum in
loop (Bytes.length v - 5) 0
module BoolLeaf = struct
type elt = bool
type t = Bytes.t
let level = 0
module Pool = Weak.Make (StringContentsHash)
let pool = Pool.create 256
let hashcons s =
let n = bytes_hash s in
let x = Tag (s, n) in
try Pool.find pool x with Not_found ->
Pool.add pool x;
x
let make_raw def = Bytes.make 32 (if def then '\255' else '\000')
let make def = hashcons (make_raw def)
let boolset s k b =
let j = Char.code (Bytes.get s (k / 8)) in
let j' = if b then j lor (1 lsl (k mod 8)) else j in
Bytes.set s (k / 8) (Char.chr j')
let of_map n0 def m =
let a = make_raw def in
IMap.iter_range (fun n1 n2 v ->
for i = n1 - n0 to n2 - n0 do boolset a i v done)
m;
hashcons a
let of_set n0 def s v =
let a = make_raw def in
ISet.iter_range (fun n1 n2 ->
for i = n1 - n0 to n2 - n0 do boolset a i v done)
s;
hashcons a
end
module Bool = struct
module BoolTbl = MakeTbl (BoolLeaf)
include BoolTbl
let of_set s = untag (BoolTbl.of_set 0 false s true)
let get tbl n =
let lev = Array.unsafe_get tbl (byte3 n) in
let lev = Array.unsafe_get lev (byte2 n) in
let lev = Array.unsafe_get lev (byte1 n) in
let k = byte0 n in
let i = Char.code (Bytes.unsafe_get lev (k / 8)) in
i lsr (k mod 8) land 1 <> 0
end
module CharLeaf = struct
type elt = char
type t = Bytes.t
let level = 0
module Pool = Weak.Make (StringContentsHash)
let pool = Pool.create 256
let hashcons s =
let n = bytes_hash s in
let x = Tag (s, n) in
try Pool.find pool x with Not_found ->
Pool.add pool x;
x
let make_raw c = Bytes.make 256 c
let make c = hashcons (make_raw c)
let of_map n0 def m =
let a = make_raw def in
IMap.iter_range (fun n1 n2 v ->
for i = n1 - n0 to n2 - n0 do Bytes.set a i v done)
m;
hashcons a
let of_set n0 def s v =
let a = make_raw def in
ISet.iter_range (fun n1 n2 ->
for i = n1 - n0 to n2 - n0 do Bytes.set a i v done)
s;
hashcons a
end
module Char = struct
module CharTbl = MakeTbl (CharLeaf)
include CharTbl
let get tbl n =
let lev = Array.unsafe_get tbl (byte3 n) in
let lev = Array.unsafe_get lev (byte2 n) in
let lev = Array.unsafe_get lev (byte1 n) in
Bytes.unsafe_get lev (byte0 n)
end
module BitsContentsHash = struct
type t = Bitsvect.t tagged
let equal x1 x2 =
let a1 = untag x1 in
let a2 = untag x2 in
let rec loop i =
if i < 0 then true else
if Bitsvect.get a1 i = Bitsvect.get a2 i then loop (i - 1) else false in
loop 255
let hash = id
end
module BitsLeaf = struct
type elt = int
type t = Bitsvect.t
let level = 0
module Pool = Weak.Make (BitsContentsHash)
let pool = Pool.create 256
let hash v =
let rec loop i sum =
if i < 0 then sum else
let a = Bitsvect.get v i in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor a land 0x7f) in
loop (i - 1) sum in
loop (Bitsvect.length v - 5) 0
let hashcons a =
let n = hash a in
let x = Tag (a, n) in
try Pool.find pool x with Not_found ->
Pool.add pool x;
x
let make_raw = Bitsvect.make 256
let make def = hashcons (make_raw def)
let of_map n0 def m =
let a = make_raw def in
IMap.iter_range (fun n1 n2 v ->
for i = n1 - n0 to n2 - n0 do Bitsvect.set a i v done)
m;
hashcons a
let of_set n0 def s v =
let a = make_raw def in
ISet.iter_range (fun n1 n2 ->
for i = n1 - n0 to n2 - n0 do Bitsvect.set a i v done)
s;
hashcons a
end
module Bits = struct
include MakeTbl (BitsLeaf)
let get tbl n =
let lev = Array.unsafe_get tbl (byte3 n) in
let lev = Array.unsafe_get lev (byte2 n) in
let lev = Array.unsafe_get lev (byte1 n) in
Bitsvect.unsafe_get lev (byte0 n)
end
module BytesContentsHash = struct
type t = Bytesvect.t tagged
let equal x1 x2 =
let a1 = untag x1 in
let a2 = untag x2 in
let rec loop i =
if i < 0 then true else
if Bytesvect.get a1 i = Bytesvect.get a2 i then
loop (i - 1)
else false in
loop 255
let hash = id
end
module BytesLeaf = struct
type elt = int
type t = Bytesvect.t
let level = 0
module Pool = Weak.Make (BytesContentsHash)
let pool = Pool.create 256
let hash v =
let rec loop i sum =
if i < 0 then sum else
let a = Bytesvect.get v i in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte3 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte2 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte1 a) land 0x7f) in
let sum = sum lsr 7 lxor crc_tbl.(sum lxor (byte0 a) land 0x7f) in
loop (i - 1) sum in
loop 255 0
let hashcons a =
let n = hash a in
let x = Tag (a, n) in
try Pool.find pool x with Not_found ->
Pool.add pool x;
x
let make_raw = Bytesvect.make 256
let make def = hashcons (make_raw def)
let of_map n0 def m =
let a = make_raw def in
IMap.iter_range (fun n1 n2 v ->
for i = n1 - n0 to n2 - n0 do Bytesvect.set a i v done)
m;
hashcons a
let of_set n0 def s v =
let a = make_raw def in
ISet.iter_range (fun n1 n2 ->
for i = n1 - n0 to n2 - n0 do Bytesvect.set a i v done)
s;
hashcons a
end
module Bytes = struct
include MakeTbl (BytesLeaf)
let get tbl n =
let lev = Array.unsafe_get tbl (byte3 n) in
let lev = Array.unsafe_get lev (byte2 n) in
let lev = Array.unsafe_get lev (byte1 n) in
Bytesvect.unsafe_get lev (byte0 n)
end