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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
type +'a hash_consed = {
hkey : int;
tag : int;
node : 'a }
let gentag =
let r = ref 0 in
fun () -> incr r; !r
type 'a t = {
mutable table : 'a hash_consed Weak.t array;
mutable totsize : int;
mutable limit : int;
}
let create sz =
let sz = if sz < 7 then 7 else sz in
let sz = if sz > Sys.max_array_length then Sys.max_array_length else sz in
let emptybucket = Weak.create 0 in
{ table = Array.make sz emptybucket;
totsize = 0;
limit = 3; }
let clear t =
let emptybucket = Weak.create 0 in
for i = 0 to Array.length t.table - 1 do t.table.(i) <- emptybucket done;
t.totsize <- 0;
t.limit <- 3
let iter f t =
let rec iter_bucket i b =
if i >= Weak.length b then () else
match Weak.get b i with
| Some v -> f v; iter_bucket (i+1) b
| None -> iter_bucket (i+1) b
in
Array.iter (iter_bucket 0) t.table
let count t =
let rec count_bucket i b accu =
if i >= Weak.length b then accu else
count_bucket (i+1) b (accu + (if Weak.check b i then 1 else 0))
in
Array.fold_right (count_bucket 0) t.table 0
let next_sz n = min (3*n/2 + 3) (Sys.max_array_length - 1)
let rec resize t =
let oldlen = Array.length t.table in
let newlen = next_sz oldlen in
if newlen > oldlen then begin
let newt = create newlen in
newt.limit <- t.limit + 100;
iter (fun d -> add newt d) t;
t.table <- newt.table;
end
and add t d =
let index = d.hkey mod (Array.length t.table) in
let bucket = t.table.(index) in
let sz = Weak.length bucket in
let rec loop i =
if i >= sz then begin
let newsz = min (3 * sz / 2 + 3) (Sys.max_array_length - 1) in
if newsz <= sz then
failwith "Hashcons.Make: hash bucket cannot grow more";
let newbucket = Weak.create newsz in
Weak.blit bucket 0 newbucket 0 sz;
Weak.set newbucket i (Some d);
t.table.(index) <- newbucket;
t.totsize <- t.totsize + (newsz - sz);
if t.totsize > t.limit * Array.length t.table then resize t;
end else begin
if Weak.check bucket i
then loop (i+1)
else Weak.set bucket i (Some d)
end
in
loop 0
let hashcons t d =
let hkey = Hashtbl.hash d land max_int in
let index = hkey mod (Array.length t.table) in
let bucket = t.table.(index) in
let sz = Weak.length bucket in
let rec loop i =
if i >= sz then begin
let hnode = { hkey = hkey; tag = gentag (); node = d } in
add t hnode;
hnode
end else begin
match Weak.get bucket i with
| Some v when v.node = d ->
begin match Weak.get bucket i with
| Some v -> v
| None -> loop (i+1)
end
| _ -> loop (i+1)
end
in
loop 0
let stats t =
let len = Array.length t.table in
let lens = Array.map Weak.length t.table in
Array.sort compare lens;
let totlen = Array.fold_left ( + ) 0 lens in
(len, count t, totlen, lens.(0), lens.(len/2), lens.(len-1))
module type HashedType =
sig
type t
val equal : t -> t -> bool
val hash : t -> int
end
module type S =
sig
type key
type t
val create : int -> t
val clear : t -> unit
val hashcons : t -> key -> key hash_consed
val iter : (key hash_consed -> unit) -> t -> unit
val stats : t -> int * int * int * int * int * int
end
module Make(H : HashedType) : (S with type key = H.t) = struct
type key = H.t
type data = H.t hash_consed
type t = {
mutable table : data Weak.t array;
mutable totsize : int;
mutable limit : int;
}
let emptybucket = Weak.create 0
let create sz =
let sz = if sz < 7 then 7 else sz in
let sz = if sz > Sys.max_array_length then Sys.max_array_length else sz in
{
table = Array.make sz emptybucket;
totsize = 0;
limit = 3;
}
let clear t =
for i = 0 to Array.length t.table - 1 do
t.table.(i) <- emptybucket
done;
t.totsize <- 0;
t.limit <- 3
let iter f t =
let rec iter_bucket i b =
if i >= Weak.length b then () else
match Weak.get b i with
| Some v -> f v; iter_bucket (i+1) b
| None -> iter_bucket (i+1) b
in
Array.iter (iter_bucket 0) t.table
let count t =
let rec count_bucket i b accu =
if i >= Weak.length b then accu else
count_bucket (i+1) b (accu + (if Weak.check b i then 1 else 0))
in
Array.fold_right (count_bucket 0) t.table 0
let next_sz n = min (3*n/2 + 3) (Sys.max_array_length - 1)
let rec resize t =
let oldlen = Array.length t.table in
let newlen = next_sz oldlen in
if newlen > oldlen then begin
let newt = create newlen in
newt.limit <- t.limit + 100;
iter (fun d -> add newt d) t;
t.table <- newt.table;
end
and add t d =
let index = d.hkey mod (Array.length t.table) in
let bucket = t.table.(index) in
let sz = Weak.length bucket in
let rec loop i =
if i >= sz then begin
let newsz = min (3 * sz / 2 + 3) (Sys.max_array_length - 1) in
if newsz <= sz then
failwith "Hashcons.Make: hash bucket cannot grow more";
let newbucket = Weak.create newsz in
Weak.blit bucket 0 newbucket 0 sz;
Weak.set newbucket i (Some d);
t.table.(index) <- newbucket;
t.totsize <- t.totsize + (newsz - sz);
if t.totsize > t.limit * Array.length t.table then resize t;
end else begin
if Weak.check bucket i
then loop (i+1)
else Weak.set bucket i (Some d)
end
in
loop 0
let hashcons t d =
let hkey = H.hash d land max_int in
let index = hkey mod (Array.length t.table) in
let bucket = t.table.(index) in
let sz = Weak.length bucket in
let rec loop i =
if i >= sz then begin
let hnode = { hkey = hkey; tag = gentag (); node = d } in
add t hnode;
hnode
end else begin
match Weak.get bucket i with
| Some v when H.equal v.node d ->
begin match Weak.get bucket i with
| Some v -> v
| None -> loop (i+1)
end
| _ -> loop (i+1)
end
in
loop 0
let stats t =
let len = Array.length t.table in
let lens = Array.map Weak.length t.table in
Array.sort compare lens;
let totlen = Array.fold_left ( + ) 0 lens in
(len, count t, totlen, lens.(0), lens.(len/2), lens.(len-1))
end
let unsigned_lt n m = n >= 0 && (m < 0 || n < m)
module Hmap = struct
type 'a key = 'a hash_consed
type ('a, 'b) t =
| Empty
| Leaf of 'a key * 'b
| Branch of int * int * ('a, 'b) t * ('a, 'b) t
let empty = Empty
let is_empty = function Empty -> true | _ -> false
let zero_bit k m = (k land m) == 0
let rec mem k = function
| Empty -> false
| Leaf (j,_) -> k.tag == j.tag
| Branch (_, m, l, r) -> mem k (if zero_bit k.tag m then l else r)
let rec find k = function
| Empty -> raise Not_found
| Leaf (j,x) -> if k.tag == j.tag then x else raise Not_found
| Branch (_, m, l, r) -> find k (if zero_bit k.tag m then l else r)
let rec find_opt k = function
| Empty -> None
| Leaf (j,x) -> if k.tag == j.tag then Some x else None
| Branch (_, m, l, r) -> find_opt k (if zero_bit k.tag m then l else r)
let singleton k v = Leaf(k,v)
let lowest_bit x = x land (-x)
let branching_bit p0 p1 = lowest_bit (p0 lxor p1)
let mask p m = p land (m-1)
let join (p0,t0,p1,t1) =
let m = branching_bit p0 p1 in
if zero_bit p0 m then
Branch (mask p0 m, m, t0, t1)
else
Branch (mask p0 m, m, t1, t0)
let match_prefix k p m = (mask k m) == p
let add k x t =
let rec ins = function
| Empty -> Leaf (k,x)
| Leaf (j,_) as t ->
if j.tag == k.tag then
Leaf (k,x)
else
join (k.tag, Leaf (k,x), j.tag, t)
| Branch (p,m,t0,t1) as t ->
if match_prefix k.tag p m then
if zero_bit k.tag m then
Branch (p, m, ins t0, t1)
else
Branch (p, m, t0, ins t1)
else
join (k.tag, Leaf (k,x), p, t)
in
ins t
let branch = function
| (_,_,Empty,t) -> t
| (_,_,t,Empty) -> t
| (p,m,t0,t1) -> Branch (p,m,t0,t1)
let remove k t =
let rec rmv = function
| Empty -> Empty
| Leaf (j,_) as t -> if k.tag == j.tag then Empty else t
| Branch (p,m,t0,t1) as t ->
if match_prefix k.tag p m then
if zero_bit k.tag m then
branch (p, m, rmv t0, t1)
else
branch (p, m, t0, rmv t1)
else
t
in
rmv t
let rec update k f = function
| Empty -> (match f None with Some v -> Leaf(k,v) | None -> Empty)
| Leaf (j,x) as t ->
if k.tag == j.tag then match f (Some x) with
| None -> Empty
| Some x -> Leaf(j,x)
else (match f None with
| None -> t
| Some x -> join (k.tag, Leaf (k,x), j.tag, t))
| Branch (p, m, t0, t1) as t ->
if match_prefix k.tag p m then
if zero_bit k.tag m then
branch (p, m, update k f t0, t1)
else
branch (p, m, t0, update k f t1)
else match f None with
| None -> t
| Some x -> join (k.tag, Leaf(k,x), p, t)
let rec iter f = function
| Empty -> ()
| Leaf (k,x) -> f k x
| Branch (_,_,t0,t1) -> iter f t0; iter f t1
let rec cardinal = function
| Empty -> 0
| Leaf(_,_) -> 1
| Branch(_,_,l,r) -> cardinal l + cardinal r
let rec map f = function
| Empty -> Empty
| Leaf (k,x) -> Leaf (k, f x)
| Branch (p,m,t0,t1) -> Branch (p, m, map f t0, map f t1)
let rec mapi f = function
| Empty -> Empty
| Leaf (k,x) -> Leaf (k, f k x)
| Branch (p,m,t0,t1) -> Branch (p, m, mapi f t0, mapi f t1)
let rec fold f s accu = match s with
| Empty -> accu
| Leaf (k,x) -> f k x accu
| Branch (_,_,t0,t1) -> fold f t0 (fold f t1 accu)
let rec exists f = function
| Empty -> false
| Leaf (k,v) -> f k v
| Branch(_,_,l,r) -> exists f l || exists f r
let rec for_all f = function
| Empty -> true
| Leaf (k,v) -> f k v
| Branch(_,_,l,r) -> for_all f l && for_all f r
let rec filter f = function
| Empty -> Empty
| Leaf(k,v) as t -> if f k v then t else Empty
| Branch(p,m,t0,t1) -> branch(p, m, filter f t0, filter f t1)
let rec filter_map f = function
| Empty -> Empty
| Leaf(k,v) -> (match f k v with Some v' -> Leaf(k,v') | None -> Empty)
| Branch(p,m,t0,t1) -> branch(p, m, filter_map f t0, filter_map f t1)
let split k m =
fold
(fun k' v (lt, data, gt) ->
if k.tag = k'.tag then (lt, Some v, gt)
else if k.tag < k'.tag then (lt, data, add k' v gt)
else (add k' v lt, data, gt))
m (empty, None, empty)
let bindings s =
let rec bindings_aux acc = function
| Empty -> acc
| Leaf (k,v) -> (k,v) :: acc
| Branch (_,_,l,r) -> bindings_aux (bindings_aux acc l) r
in
bindings_aux [] s
let to_seq s =
let rec to_seq_aux acc = function
| Empty -> acc
| Leaf (k,v) -> Seq.cons (k,v) acc
| Branch (_,_,l,r) -> to_seq_aux (to_seq_aux acc l) r
in
to_seq_aux Seq.empty s
let partition f m = fold (fun k v (m_true, m_false) ->
if f k v then (add k v m_true, m_false) else (m_true, add k v m_false)
) m (Empty,Empty)
let rec choose = function
| Empty -> raise Not_found
| Leaf (k, v) -> (k, v)
| Branch (_, _, t0, _) -> choose t0
let rec choose_opt = function
| Empty -> None
| Leaf (k, v) -> Some (k, v)
| Branch (_, _, t0, _) -> choose_opt t0
let rec equal equal_v t1 t2 = match t1, t2 with
| Empty, Empty -> true
| Leaf (k1,v1), Leaf (k2,v2) -> k1.tag == k2.tag && equal_v v1 v2
| Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
p1 = p2 && m1 = m2 && equal equal_v l1 l2 && equal equal_v r1 r2
| _ -> false
let rec compare compare_v t1 t2 = match t1,t2 with
| Empty, Empty -> 0
| Empty, _ -> -1
| _, Empty -> 1
| Leaf (k1,v1), Leaf (k2,v2) ->
let cmp = Int.compare k1.tag k2.tag in
if cmp = 0 then compare_v v1 v2 else cmp
| Leaf _, Branch _ -> -1
| Branch _, Leaf _ -> 1
| Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
let cmp = Int.compare p1 p2 in
if cmp <> 0 then cmp else
let cmp = Int.compare m1 m2 in
if cmp <> 0 then cmp else
let cmp = compare compare_v l1 l2 in
if cmp <> 0 then cmp else
compare compare_v r1 r2
let merge f l r =
let merge_l t = filter_map (fun k v -> f k (Some v) None) t in
let merge_r t = filter_map (fun k v -> f k None (Some v)) t in
let rec merge_aux l r = match l, r with
| Empty, t -> merge_r t
| t, Empty -> merge_l t
| Leaf (k,v1), t ->
filter_map (
fun k' v -> f k' (if k.tag = k'.tag then (Some v1) else None) (Some v)
) t
| t, Leaf (k,v2) ->
filter_map (
fun k' v -> f k' (Some v) (if k.tag = k'.tag then (Some v2) else None)
) t
| (Branch (p,m,l0,l1) as l), (Branch (q,n,r0,r1) as r) ->
if m = n && match_prefix q p m
then branch (p, m, merge_aux l0 r0, merge_aux l1 r1)
else if unsigned_lt m n && match_prefix q p m then
if zero_bit q m
then branch (p, m, merge_aux l0 r, merge_l l1)
else branch (p, m, merge_l l0, merge_aux l1 r)
else if unsigned_lt n m && match_prefix p q n then
if zero_bit p n
then branch (q, n, merge_aux l r0, merge_r r1)
else branch (q, n, merge_r r0, merge_aux l r1)
else
join (p, merge_l l, q, merge_r r)
in merge_aux l r
let rec union f l r = match l, r with
| Empty, t
| t, Empty -> t
| Leaf (k,v1), t ->
update k (function None -> Some v1 | Some v2 -> f k v1 v2) t
| t, Leaf (k,v2) ->
update k (function None -> Some v2 | Some v1 -> f k v1 v2) t
| (Branch (p,m,s0,s1) as s), (Branch (q,n,t0,t1) as t) ->
if m = n && match_prefix q p m
then branch (p, m, union f s0 t0, union f s1 t1)
else if unsigned_lt m n && match_prefix q p m then
if zero_bit q m
then branch (p, m, union f s0 t, s1)
else branch (p, m, s0, union f s1 t)
else if unsigned_lt n m && match_prefix p q n then
if zero_bit p n
then branch (q, n, union f s t0, t1)
else branch (q, n, t0, union f s t1)
else
join (p, s, q, t)
let min_binding_opt m =
fold
(fun k v b ->
match b with
| None -> Some (k, v)
| Some (k', _) -> if k'.tag <= k.tag then b else Some (k, v))
m None
let min_binding m = match min_binding_opt m with
| Some x -> x
| None -> raise Not_found
let max_binding_opt m =
fold
(fun k v b ->
match b with
| None -> Some (k, v)
| Some (k', _) -> if k'.tag >= k.tag then b else Some (k, v))
m None
let max_binding m = match max_binding_opt m with
| Some x -> x
| None -> raise Not_found
let find_first_opt f m =
fold
(fun k v acc ->
match acc with
| None -> if f k then Some (k, v) else None
| Some (k', _) ->
if k'.tag <= k.tag then acc else
if f k then Some (k, v) else acc)
m None
let find_first f m = match find_first_opt f m with
| Some x -> x
| None -> raise Not_found
let find_last_opt f m =
fold
(fun k v acc ->
match acc with
| None -> if f k then Some (k, v) else None
| Some (k', _) ->
if k'.tag >= k.tag then acc else
if f k then Some (k, v) else acc)
m None
let find_last f m = match find_last_opt f m with
| Some x -> x
| None -> raise Not_found
let add_seq seq m = Seq.fold_left (fun m (k, v) -> add k v m) m seq
let of_seq s = add_seq s Empty
let find_any (type a b) f (m : (a, b) t) =
let exception Found of (a key * b) in
try
iter (fun k v -> if f k v then raise (Found (k, v))) m;
raise Not_found
with Found x -> x
let find_any_opt (type a b) f (m : (a, b) t) =
let exception Found of (a key * b) in
try
iter (fun k v -> if f k v then raise (Found (k, v))) m;
None
with Found x -> Some x
let is_singleton = function
| Leaf(k,v) -> Some (k,v)
| _ -> None
end
module Hset = struct
type 'a elt = 'a hash_consed
type 'a t =
| Empty
| Leaf of 'a hash_consed
| Branch of int * int * 'a t * 'a t
let empty = Empty
let is_empty = function Empty -> true | _ -> false
let singleton k = Leaf k
let zero_bit k m = (k land m) == 0
let rec mem k = function
| Empty -> false
| Leaf j -> k.tag == j.tag
| Branch (_, m, l, r) -> mem k (if zero_bit k.tag m then l else r)
let find k s = if mem k s then k else raise Not_found
let find_opt k s = if mem k s then Some k else None
let lowest_bit x = x land (-x)
let branching_bit p0 p1 = lowest_bit (p0 lxor p1)
let mask p m = p land (m-1)
let join (p0,t0,p1,t1) =
let m = branching_bit p0 p1 in
if zero_bit p0 m then
Branch (mask p0 m, m, t0, t1)
else
Branch (mask p0 m, m, t1, t0)
let match_prefix k p m = (mask k m) == p
let add k t =
let rec ins = function
| Empty -> Leaf k
| Leaf j as t ->
if j.tag == k.tag then t else join (k.tag, Leaf k, j.tag, t)
| Branch (p,m,t0,t1) as t ->
if match_prefix k.tag p m then
if zero_bit k.tag m then
Branch (p, m, ins t0, t1)
else
Branch (p, m, t0, ins t1)
else
join (k.tag, Leaf k, p, t)
in
ins t
let branch = function
| (_,_,Empty,t) -> t
| (_,_,t,Empty) -> t
| (p,m,t0,t1) -> Branch (p,m,t0,t1)
let remove k t =
let rec rmv = function
| Empty -> Empty
| Leaf j as t -> if k.tag == j.tag then Empty else t
| Branch (p,m,t0,t1) as t ->
if match_prefix k.tag p m then
if zero_bit k.tag m then
branch (p, m, rmv t0, t1)
else
branch (p, m, t0, rmv t1)
else
t
in
rmv t
let rec merge = function
| Empty, t -> t
| t, Empty -> t
| Leaf k, t -> add k t
| t, Leaf k -> add k t
| (Branch (p,m,s0,s1) as s), (Branch (q,n,t0,t1) as t) ->
if m == n && match_prefix q p m then
Branch (p, m, merge (s0,t0), merge (s1,t1))
else if unsigned_lt m n && match_prefix q p m then
if zero_bit q m then
Branch (p, m, merge (s0,t), s1)
else
Branch (p, m, s0, merge (s1,t))
else if unsigned_lt n m && match_prefix p q n then
if zero_bit p n then
Branch (q, n, merge (s,t0), t1)
else
Branch (q, n, t0, merge (s,t1))
else
join (p, s, q, t)
let union s t = merge (s,t)
let rec subset s1 s2 = match (s1,s2) with
| Empty, _ -> true
| _, Empty -> false
| Leaf k1, _ -> mem k1 s2
| Branch _, Leaf _ -> false
| Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
if m1 == m2 && p1 == p2 then
subset l1 l2 && subset r1 r2
else if unsigned_lt m2 m1 && match_prefix p1 p2 m2 then
if zero_bit p1 m2 then
subset l1 l2 && subset r1 l2
else
subset l1 r2 && subset r1 r2
else
false
let rec inter s1 s2 = match (s1,s2) with
| Empty, _ -> Empty
| _, Empty -> Empty
| Leaf k1, _ -> if mem k1 s2 then s1 else Empty
| _, Leaf k2 -> if mem k2 s1 then s2 else Empty
| Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
if m1 == m2 && p1 == p2 then
merge (inter l1 l2, inter r1 r2)
else if unsigned_lt m1 m2 && match_prefix p2 p1 m1 then
inter (if zero_bit p2 m1 then l1 else r1) s2
else if unsigned_lt m2 m1 && match_prefix p1 p2 m2 then
inter s1 (if zero_bit p1 m2 then l2 else r2)
else
Empty
let rec diff s1 s2 = match (s1,s2) with
| Empty, _ -> Empty
| _, Empty -> s1
| Leaf k1, _ -> if mem k1 s2 then Empty else s1
| _, Leaf k2 -> remove k2 s1
| Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
if m1 == m2 && p1 == p2 then
merge (diff l1 l2, diff r1 r2)
else if unsigned_lt m1 m2 && match_prefix p2 p1 m1 then
if zero_bit p2 m1 then
merge (diff l1 s2, r1)
else
merge (l1, diff r1 s2)
else if unsigned_lt m2 m1 && match_prefix p1 p2 m2 then
if zero_bit p1 m2 then diff s1 l2 else diff s1 r2
else
s1
let rec cardinal = function
| Empty -> 0
| Leaf _ -> 1
| Branch (_,_,t0,t1) -> cardinal t0 + cardinal t1
let rec iter f = function
| Empty -> ()
| Leaf k -> f k
| Branch (_,_,t0,t1) -> iter f t0; iter f t1
let rec fold f s accu = match s with
| Empty -> accu
| Leaf k -> f k accu
| Branch (_,_,t0,t1) -> fold f t0 (fold f t1 accu)
let rec for_all p = function
| Empty -> true
| Leaf k -> p k
| Branch (_,_,t0,t1) -> for_all p t0 && for_all p t1
let rec exists p = function
| Empty -> false
| Leaf k -> p k
| Branch (_,_,t0,t1) -> exists p t0 || exists p t1
let rec filter pr = function
| Empty -> Empty
| Leaf k as t -> if pr k then t else Empty
| Branch (p,m,t0,t1) -> branch (p, m, filter pr t0, filter pr t1)
let partition p s =
let rec part (t,f as acc) = function
| Empty -> acc
| Leaf k -> if p k then (add k t, f) else (t, add k f)
| Branch (_,_,t0,t1) -> part (part acc t0) t1
in
part (Empty, Empty) s
let rec choose = function
| Empty -> raise Not_found
| Leaf k -> k
| Branch (_, _,t0,_) -> choose t0
let rec choose_opt = function
| Empty -> None
| Leaf k -> Some k
| Branch (_, _,t0,_) -> choose_opt t0
let elements s =
let rec elements_aux acc = function
| Empty -> acc
| Leaf k -> k :: acc
| Branch (_,_,l,r) -> elements_aux (elements_aux acc l) r
in
elements_aux [] s
let to_seq s =
let rec to_seq_aux acc = function
| Empty -> acc
| Leaf k -> Seq.cons k acc
| Branch (_,_,l,r) -> to_seq_aux (to_seq_aux acc r) l
in
to_seq_aux Seq.empty s
let split elt s =
fold (fun elt' (lt, present, gt) ->
if elt'.tag < elt.tag then (add elt' lt, present, gt) else
if elt'.tag > elt.tag then (lt, present, add elt' gt) else
(lt, true, gt)
) s (Empty, false, Empty)
let map f s = fold (fun elt s -> add (f elt) s) s Empty
let filter_map f s = fold (fun elt s ->
match f elt with
| None -> s
| Some elt' -> add elt' s)
s Empty
let add_seq seq s = Seq.fold_left (fun s elt -> add elt s) s seq
let of_seq seq = add_seq seq Empty
let of_list list = List.fold_left (fun s elt -> add elt s) Empty list
let rec min_elt = function
| Empty -> raise Not_found
| Leaf k -> k
| Branch (_,_,s,t) -> min (min_elt s) (min_elt t)
let min_elt_opt = function
| Empty -> None
| x -> Some (min_elt x)
let rec max_elt = function
| Empty -> raise Not_found
| Leaf k -> k
| Branch (_,_,s,t) -> max (max_elt s) (max_elt t)
let max_elt_opt = function
| Empty -> None
| x -> Some (max_elt x)
let find_first_opt f s =
fold
(fun elt acc ->
match acc with
| None -> if f elt then Some elt else None
| Some witness ->
if witness.tag <= elt.tag then acc else
if f elt then Some elt else acc)
s None
let find_first f s =
match find_first_opt f s with
| Some elt -> elt
| None -> raise Not_found
let find_last_opt f s =
fold
(fun elt acc ->
match acc with
| None -> if f elt then Some elt else None
| Some witness ->
if witness.tag >= elt.tag then acc else
if f elt then Some elt else acc)
s None
let find_last f s =
match find_last_opt f s with
| Some elt -> elt
| None -> raise Not_found
let rec equal l r = match (l, r) with
| Empty, Empty -> true
| Leaf l, Leaf r -> l.tag == r.tag
| Branch (ai, aj, al, ar), Branch (bi, bj, bl, br) ->
ai == bi && aj == bj && equal al bl && equal ar br
| _ -> false
let rec compare l r = match (l, r) with
| Empty, Empty -> 0
| Empty, _ -> -1
| _, Empty -> 1
| Leaf l, Leaf r -> Int.compare l.tag r.tag
| Leaf _, _ -> -1
| _, Leaf _ -> 1
| Branch (ai, aj, al, ar), Branch (bi, bj, bl, br) ->
let cmp = Int.compare ai bi in
if cmp <> 0 then cmp else
let cmp = Int.compare aj bj in
if cmp <> 0 then cmp else
let cmp = compare al bl in
if cmp <> 0 then cmp else
compare ar br
let _make l = List.fold_right add l empty
let rec intersect s1 s2 = match (s1,s2) with
| Empty, _ -> false
| _, Empty -> false
| Leaf k1, _ -> mem k1 s2
| _, Leaf k2 -> mem k2 s1
| Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
if m1 == m2 && p1 == p2 then
intersect l1 l2 || intersect r1 r2
else if unsigned_lt m1 m2 && match_prefix p2 p1 m1 then
intersect (if zero_bit p2 m1 then l1 else r1) s2
else if unsigned_lt m2 m1 && match_prefix p1 p2 m2 then
intersect s1 (if zero_bit p1 m2 then l2 else r2)
else
false
let disjoint s1 s2 = not (intersect s1 s2)
let find_any (type a) f (s : a t) =
let exception Found of a elt in
try
iter (fun elt -> if f elt then raise (Found elt)) s;
raise Not_found
with Found elt -> elt
let find_any_opt (type a) f (s : a t) =
let exception Found of a elt in
try
iter (fun elt -> if f elt then raise (Found elt)) s;
None
with Found elt -> Some elt
let bind f s = fold (fun elt s -> union (f elt) s) s empty
let is_singleton = function
| Leaf elt -> Some elt
| _ -> None
end