-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathbelt_MutableSetInt.ml
342 lines (292 loc) · 9.06 KB
/
belt_MutableSetInt.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
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
# 1 "others/setm.cppo.ml"
(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(** This module is [`Belt.MutableSet`]() specialized with key type to be a primitive type.
It is more efficient in general, the API is the same with [`Belt_MutableSet`]() except its key type is fixed,
and identity is not needed(using the built-in one)
*)
# 31 "others/setm.cppo.ml"
module I = Belt_internalSetInt
module S = Belt_SortArrayInt
# 39 "others/setm.cppo.ml"
module N = Belt_internalAVLset
module A = Belt_Array
type value = I.value
(** The type of the set elements. *)
type t = {
mutable data : I.t
}
(** The type of sets. *)
let rec remove0 nt (x : value)=
let k = nt.N.value in
if x = k then
let {N.left = l; right = r} = nt in
match l, r with
| None, _ -> r
| _, None -> l
| Some _, Some nr ->
nt.right <- (N.removeMinAuxWithRootMutate nt nr);
Some (N.balMutate nt)
else
begin
if x < k then
match nt.left with
| None -> Some nt
| Some l ->
nt.left <- (remove0 l x );
Some (N.balMutate nt)
else
match nt.right with
| None -> Some nt
| Some r ->
nt.right <- (remove0 r x);
Some (N.balMutate nt)
end
let remove d v =
let oldRoot = d.data in
match oldRoot with
| None -> ()
| Some oldRoot2 ->
let newRoot = remove0 oldRoot2 v in
if newRoot != oldRoot then
d.data <- newRoot
let rec removeMany0 t xs i len =
if i < len then
let ele = A.getUnsafe xs i in
let u = remove0 t ele in
match u with
| None -> None
| Some t -> removeMany0 t xs (i+1) len
else Some t
let removeMany (d : t) xs =
let oldRoot = d.data in
match oldRoot with
| None -> ()
| Some nt ->
let len = A.length xs in
d.data <- removeMany0 nt xs 0 len
let rec removeCheck0 nt (x : value) removed =
let k = nt.N.value in
if x = k then
let () = removed .contents<- true in
let {N.left = l; right = r} = nt in
match l, r with
| None, _ -> r
| _ , None -> l
| Some _, Some nr ->
nt.right <- (N.removeMinAuxWithRootMutate nt nr);
Some (N.balMutate nt)
else
begin
if x < k then
match nt.left with
| None -> Some nt
| Some l ->
nt.left <- (removeCheck0 l x removed);
Some (N.balMutate nt)
else
match nt.right with
| None -> Some nt
| Some r ->
nt.right <- (removeCheck0 r x removed);
Some (N.balMutate nt)
end
let removeCheck (d : t) v =
let oldRoot = d.data in
match oldRoot with
| None -> false
| Some oldRoot2 ->
let removed = ref false in
let newRoot = removeCheck0 oldRoot2 v removed in
if newRoot != oldRoot then
d.data <- newRoot ;
removed.contents
let rec addCheck0 t (x : value) added =
match t with
| None ->
added .contents<- true;
N.singleton x
| Some nt ->
let k = nt.N.value in
if x = k then t
else
let {N.left = l; right = r} = nt in
(if x < k then
let ll = addCheck0 l x added in
nt.left <- ll
else
nt.right <- (addCheck0 r x added );
);
Some (N.balMutate nt)
let addCheck (m : t) e =
let oldRoot = m.data in
let added = ref false in
let newRoot = addCheck0 oldRoot e added in
if newRoot != oldRoot then
m.data <- newRoot;
added.contents
let add d k =
let oldRoot = d.data in
let v = I.addMutate oldRoot k in
if v != oldRoot then
d.data <- v
let addArrayMutate t xs =
let v = ref t in
for i = 0 to A.length xs - 1 do
v.contents<- I.addMutate v.contents (A.getUnsafe xs i)
done ;
v.contents
let mergeMany d arr =
d.data <- addArrayMutate (d.data) arr
let make () = {data = None}
let isEmpty d =
N.isEmpty (d.data)
let minimum d =
N.minimum (d.data)
let minUndefined d =
N.minUndefined (d.data)
let maximum d = N.maximum (d.data)
let maxUndefined d = N.maxUndefined (d.data)
let forEachU d f = N.forEachU (d.data) f
let forEach d f = forEachU d (fun[@bs] a -> f a)
let reduceU d acc cb = N.reduceU (d.data) acc cb
let reduce d acc cb = reduceU d acc (fun[@bs] a b -> cb a b)
let everyU d p = N.everyU (d.data) p
let every d p = everyU d (fun[@bs] a -> p a)
let someU d p = N.someU (d.data) p
let some d p = someU d (fun [@bs] a -> p a)
let size d =
N.size (d.data)
let toList d =
N.toList (d.data)
let toArray d =
N.toArray (d.data)
let fromSortedArrayUnsafe xs =
{data = N.fromSortedArrayUnsafe xs}
let checkInvariantInternal d =
N.checkInvariantInternal (d.data)
let fromArray xs =
{ data = I.fromArray xs}
let cmp d0 d1 =
I.cmp (d0.data) (d1.data)
let eq d0 d1 =
I.eq (d0.data) (d1.data)
let get d x =
I.get (d.data) x
let getUndefined d x =
I.getUndefined (d.data) x
let getExn d x =
I.getExn (d.data) x
let split d key =
let arr = N.toArray (d.data) in
let i = S.binarySearch arr key in
let len = A.length arr in
if i < 0 then
let next = - i -1 in
(
{data = N.fromSortedArrayAux arr 0 next}
,
{data = N.fromSortedArrayAux arr next (len - next)}
), false
else
(
{data = N.fromSortedArrayAux arr 0 i}
,
{data = (N.fromSortedArrayAux arr (i+1) (len - i - 1))}
), true
let keepU d p =
{data = (N.keepCopyU (d.data) p )}
let keep d p = keepU d (fun[@bs] a -> p a)
let partitionU d p =
let a , b = N.partitionCopyU (d.data) p in
{data = a}, {data = b}
let partition d p = partitionU d (fun[@bs] a -> p a)
let subset a b = I.subset a.data b.data
let intersect dataa datab =
let dataa, datab = dataa.data, datab.data in
match dataa, datab with
| None, _ -> make ()
| _, None -> make ()
| Some dataa0, Some datab0 ->
let sizea, sizeb =
N.lengthNode dataa0, N.lengthNode datab0 in
let totalSize = sizea + sizeb in
let tmp = A.makeUninitializedUnsafe totalSize in
ignore (N.fillArray dataa0 0 tmp) ;
ignore (N.fillArray datab0 sizea tmp);
if ((A.getUnsafe tmp (sizea - 1) <
A.getUnsafe tmp sizea))
||
(
(A.getUnsafe tmp (totalSize - 1) <
A.getUnsafe tmp 0)
)
then make ()
else
let tmp2 = A.makeUninitializedUnsafe (Pervasives.min sizea sizeb) in
let k = S.intersect tmp 0 sizea tmp sizea sizeb tmp2 0 in
{data = (N.fromSortedArrayAux tmp2 0 k)}
let diff dataa datab : t =
let dataa, datab = dataa.data, datab.data in
match dataa, datab with
| None, _ -> make ()
| _, None -> {data = N.copy dataa}
| Some dataa0, Some datab0 ->
let sizea, sizeb = N.lengthNode dataa0, N.lengthNode datab0 in
let totalSize = sizea + sizeb in
let tmp = A.makeUninitializedUnsafe totalSize in
ignore (N.fillArray dataa0 0 tmp);
ignore (N.fillArray datab0 sizea tmp);
if ( (A.getUnsafe tmp (sizea - 1)) <
(A.getUnsafe tmp sizea))
||
(A.getUnsafe tmp (totalSize - 1)
< A.getUnsafe tmp 0)
then {data = N.copy dataa}
else
let tmp2 = A.makeUninitializedUnsafe sizea in
let k = S.diff tmp 0 sizea tmp sizea sizeb tmp2 0 in
{data = (N.fromSortedArrayAux tmp2 0 k)}
let union (dataa : t) (datab : t) : t =
let dataa, datab = dataa.data, datab.data in
match dataa, datab with
| None, _ -> {data = (N.copy datab)}
| _, None -> {data = (N.copy dataa)}
| Some dataa0, Some datab0
->
let sizea, sizeb = N.lengthNode dataa0, N.lengthNode datab0 in
let totalSize = sizea + sizeb in
let tmp = A.makeUninitializedUnsafe totalSize in
ignore (N.fillArray dataa0 0 tmp);
ignore (N.fillArray datab0 sizea tmp);
if
(A.getUnsafe tmp (sizea - 1) <
A.getUnsafe tmp sizea) then
{data = (N.fromSortedArrayAux tmp 0 totalSize) }
else
let tmp2 = A.makeUninitializedUnsafe totalSize in
let k = S.union tmp 0 sizea tmp sizea sizeb tmp2 0 in
{data = (N.fromSortedArrayAux tmp2 0 k) }
let has d x = I.has (d.data) x
let copy d = {data = (N.copy (d.data))}