-
Notifications
You must be signed in to change notification settings - Fork 84
/
belt_SetInt.ml
163 lines (163 loc) · 5.11 KB
/
belt_SetInt.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
module I = Belt_internalSetInt
module N = Belt_internalAVLset
module A = Belt_Array
type value = I.value
type t = I.t
let empty = N.empty
let isEmpty = N.isEmpty
let minimum = N.minimum
let minUndefined = N.minUndefined
let maximum = N.maximum
let maxUndefined = N.maxUndefined
let forEach = N.forEach
let forEachU = N.forEachU
let reduce = N.reduce
let reduceU = N.reduceU
let every = N.every
let everyU = N.everyU
let some = N.some
let someU = N.someU
let keep = N.keepShared
let keepU = N.keepSharedU
let partition = N.partitionShared
let partitionU = N.partitionSharedU
let size = N.size
let toList = N.toList
let toArray = N.toArray
let fromSortedArrayUnsafe = N.fromSortedArrayUnsafe
let checkInvariantInternal = N.checkInvariantInternal
let rec add (t : t) (x : value) =
(match N.toOpt t with
| None -> N.singleton x
| Some nt ->
let v = N.value nt in
if x = v
then t
else
(let (l,r) = let open N in ((left nt), (right nt)) in
if x < v
then let ll = add l x in (if ll == l then t else N.bal ll v r)
else (let rr = add r x in if rr == r then t else N.bal l v rr)) :
t)
let mergeMany h arr =
let len = A.length arr in
let v = ref h in
for i = 0 to len - 1 do
(let key = A.getUnsafe arr i in v := (add (!v) key))
done;
!v
let rec remove (t : t) (x : value) =
(match N.toOpt t with
| None -> t
| Some n ->
let (l,v,r) = let open N in ((left n), (value n), (right n)) in
if x = v
then
(match ((N.toOpt l), (N.toOpt r)) with
| (None ,_) -> r
| (_,None ) -> l
| (_,Some rn) ->
let v = ref (N.value rn) in
let r = N.removeMinAuxWithRef rn v in N.bal l (!v) r)
else
if x < v
then (let ll = remove l x in if ll == l then t else N.bal ll v r)
else (let rr = remove r x in if rr == r then t else N.bal l v rr) :
t)
let removeMany h arr =
let len = A.length arr in
let v = ref h in
for i = 0 to len - 1 do
(let key = A.getUnsafe arr i in v := (remove (!v) key))
done;
!v
let fromArray = I.fromArray
let cmp = I.cmp
let eq = I.eq
let get = I.get
let getUndefined = I.getUndefined
let getExn = I.getExn
let subset = I.subset
let has = I.has
let rec splitAuxNoPivot (n : _ N.node) (x : value) =
(let (l,v,r) = let open N in ((left n), (value n), (right n)) in
if x = v
then (l, r)
else
if x < v
then
(match N.toOpt l with
| None -> (N.empty, (N.return n))
| Some l ->
let (ll,rl) = splitAuxNoPivot l x in (ll, (N.joinShared rl v r)))
else
(match N.toOpt r with
| None -> ((N.return n), N.empty)
| Some r ->
let (lr,rr) = splitAuxNoPivot r x in ((N.joinShared l v lr), rr)) :
(t* t))
let rec splitAuxPivot (n : _ N.node) (x : value) pres =
(let (l,v,r) = let open N in ((left n), (value n), (right n)) in
if x = v
then (pres := true; (l, r))
else
if x < v
then
(match N.toOpt l with
| None -> (N.empty, (N.return n))
| Some l ->
let (ll,rl) = splitAuxPivot l x pres in
(ll, (N.joinShared rl v r)))
else
(match N.toOpt r with
| None -> ((N.return n), N.empty)
| Some r ->
let (lr,rr) = splitAuxPivot r x pres in
((N.joinShared l v lr), rr)) : (t* t))
let split (t : t) (x : value) =
match N.toOpt t with
| None -> ((N.empty, N.empty), false)
| Some n ->
let pres = ref false in let v = splitAuxPivot n x pres in (v, (!pres))
let rec union (s1 : t) (s2 : t) =
match let open N in ((toOpt s1), (toOpt s2)) with
| (None ,_) -> s2
| (_,None ) -> s1
| (Some n1,Some n2) ->
let (h1,h2) = let open N in ((height n1), (height n2)) in
if h1 >= h2
then
(if h2 = 1
then add s1 (N.value n2)
else
(let (l1,v1,r1) =
let open N in ((left n1), (value n1), (right n1)) in
let (l2,r2) = splitAuxNoPivot n2 v1 in
N.joinShared (union l1 l2) v1 (union r1 r2)))
else
if h1 = 1
then add s2 (N.value n1)
else
(let (l2,v2,r2) = let open N in ((left n2), (value n2), (right n2)) in
let (l1,r1) = splitAuxNoPivot n1 v2 in
N.joinShared (union l1 l2) v2 (union r1 r2))
let rec intersect (s1 : t) (s2 : t) =
match let open N in ((toOpt s1), (toOpt s2)) with
| (None ,_)|(_,None ) -> N.empty
| (Some n1,Some n2) ->
let (l1,v1,r1) = let open N in ((left n1), (value n1), (right n1)) in
let pres = ref false in
let (l2,r2) = splitAuxPivot n2 v1 pres in
let ll = intersect l1 l2 in
let rr = intersect r1 r2 in
if !pres then N.joinShared ll v1 rr else N.concatShared ll rr
let rec diff (s1 : t) (s2 : t) =
match let open N in ((toOpt s1), (toOpt s2)) with
| (None ,_)|(_,None ) -> s1
| (Some n1,Some n2) ->
let (l1,v1,r1) = let open N in ((left n1), (value n1), (right n1)) in
let pres = ref false in
let (l2,r2) = splitAuxPivot n2 v1 pres in
let ll = diff l1 l2 in
let rr = diff r1 r2 in
if !pres then N.concatShared ll rr else N.joinShared ll v1 rr