-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathBelt_MutableSetString.res
333 lines (302 loc) · 8.89 KB
/
Belt_MutableSetString.res
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
/* 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)
*/
module I = Belt_internalSetString
module S = Belt_SortArrayString
module N = Belt_internalAVLset
module A = Belt_Array
/** The type of the set elements. */
type value = I.value
/** The type of sets. */
type t = {mutable data: I.t}
let rec remove0 = (nt, x: value) => {
let k = nt.N.value
if x == k {
let {N.left: l, right: r} = nt
switch (l, r) {
| (None, _) => r
| (_, None) => l
| (Some(_), Some(nr)) =>
nt.right = N.removeMinAuxWithRootMutate(nt, nr)
Some(N.balMutate(nt))
}
} else if x < k {
switch nt.left {
| None => Some(nt)
| Some(l) =>
nt.left = remove0(l, x)
Some(N.balMutate(nt))
}
} else {
switch nt.right {
| None => Some(nt)
| Some(r) =>
nt.right = remove0(r, x)
Some(N.balMutate(nt))
}
}
}
let remove = (d, v) => {
let oldRoot = d.data
switch oldRoot {
| None => ()
| Some(oldRoot2) =>
let newRoot = remove0(oldRoot2, v)
if newRoot !== oldRoot {
d.data = newRoot
}
}
}
let rec removeMany0 = (t, xs, i, len) =>
if i < len {
let ele = A.getUnsafe(xs, i)
let u = remove0(t, ele)
switch u {
| None => None
| Some(t) => removeMany0(t, xs, i + 1, len)
}
} else {
Some(t)
}
let removeMany = (d: t, xs) => {
let oldRoot = d.data
switch oldRoot {
| None => ()
| Some(nt) =>
let len = A.length(xs)
d.data = removeMany0(nt, xs, 0, len)
}
}
let rec removeCheck0 = (nt, x: value, removed) => {
let k = nt.N.value
if x == k {
let () = removed.contents = true
let {N.left: l, right: r} = nt
switch (l, r) {
| (None, _) => r
| (_, None) => l
| (Some(_), Some(nr)) =>
nt.right = N.removeMinAuxWithRootMutate(nt, nr)
Some(N.balMutate(nt))
}
} else if x < k {
switch nt.left {
| None => Some(nt)
| Some(l) =>
nt.left = removeCheck0(l, x, removed)
Some(N.balMutate(nt))
}
} else {
switch nt.right {
| None => Some(nt)
| Some(r) =>
nt.right = removeCheck0(r, x, removed)
Some(N.balMutate(nt))
}
}
}
let removeCheck = (d: t, v) => {
let oldRoot = d.data
switch oldRoot {
| None => false
| Some(oldRoot2) =>
let removed = ref(false)
let newRoot = removeCheck0(oldRoot2, v, removed)
if newRoot !== oldRoot {
d.data = newRoot
}
removed.contents
}
}
let rec addCheck0 = (t, x: value, added) =>
switch t {
| None =>
added.contents = true
N.singleton(x)
| Some(nt) =>
let k = nt.N.value
if x == k {
t
} else {
let {N.left: l, right: r} = nt
if x < k {
let ll = addCheck0(l, x, added)
nt.left = ll
} else {
nt.right = addCheck0(r, x, added)
}
Some(N.balMutate(nt))
}
}
let addCheck = (m: t, e) => {
let oldRoot = m.data
let added = ref(false)
let newRoot = addCheck0(oldRoot, e, added)
if newRoot !== oldRoot {
m.data = newRoot
}
added.contents
}
let add = (d, k) => {
let oldRoot = d.data
let v = I.addMutate(oldRoot, k)
if v !== oldRoot {
d.data = v
}
}
let addArrayMutate = (t, xs) => {
let v = ref(t)
for i in 0 to A.length(xs) - 1 {
v.contents = I.addMutate(v.contents, A.getUnsafe(xs, i))
}
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 forEach = (d, f) => N.forEach(d.data, f)
let reduce = (d, acc, cb) => N.reduce(d.data, acc, cb)
let every = (d, p) => N.every(d.data, p)
let some = (d, p) => N.some(d.data, p)
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)
let i = S.binarySearch(arr, key)
let len = A.length(arr)
if i < 0 {
let next = -i - 1
(
(
{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 keep = (d, p) => {data: N.keepCopy(d.data, p)}
let partition = (d, p) => {
let (a, b) = N.partitionCopy(d.data, p)
({data: a}, {data: b})
}
let subset = (a, b) => I.subset(a.data, b.data)
let intersect = (dataa, datab) => {
let (dataa, datab) = (dataa.data, datab.data)
switch (dataa, datab) {
| (None, _) => make()
| (_, None) => make()
| (Some(dataa0), Some(datab0)) =>
let (sizea, sizeb) = (N.lengthNode(dataa0), N.lengthNode(datab0))
let totalSize = sizea + sizeb
let tmp = A.makeUninitializedUnsafe(totalSize)
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)
) {
make()
} else {
let tmp2 = A.makeUninitializedUnsafe(Pervasives.min(sizea, sizeb))
let k = S.intersect(tmp, 0, sizea, tmp, sizea, sizeb, tmp2, 0)
{data: N.fromSortedArrayAux(tmp2, 0, k)}
}
}
}
let diff = (dataa, datab): t => {
let (dataa, datab) = (dataa.data, datab.data)
switch (dataa, datab) {
| (None, _) => make()
| (_, None) => {data: N.copy(dataa)}
| (Some(dataa0), Some(datab0)) =>
let (sizea, sizeb) = (N.lengthNode(dataa0), N.lengthNode(datab0))
let totalSize = sizea + sizeb
let tmp = A.makeUninitializedUnsafe(totalSize)
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)
) {
{data: N.copy(dataa)}
} else {
let tmp2 = A.makeUninitializedUnsafe(sizea)
let k = S.diff(tmp, 0, sizea, tmp, sizea, sizeb, tmp2, 0)
{data: N.fromSortedArrayAux(tmp2, 0, k)}
}
}
}
let union = (dataa: t, datab: t): t => {
let (dataa, datab) = (dataa.data, datab.data)
switch (dataa, datab) {
| (None, _) => {data: N.copy(datab)}
| (_, None) => {data: N.copy(dataa)}
| (Some(dataa0), Some(datab0)) =>
let (sizea, sizeb) = (N.lengthNode(dataa0), N.lengthNode(datab0))
let totalSize = sizea + sizeb
let tmp = A.makeUninitializedUnsafe(totalSize)
ignore(N.fillArray(dataa0, 0, tmp))
ignore(N.fillArray(datab0, sizea, tmp))
if A.getUnsafe(tmp, sizea - 1) < A.getUnsafe(tmp, sizea) {
{data: N.fromSortedArrayAux(tmp, 0, totalSize)}
} else {
let tmp2 = A.makeUninitializedUnsafe(totalSize)
let k = S.union(tmp, 0, sizea, tmp, sizea, sizeb, tmp2, 0)
{data: N.fromSortedArrayAux(tmp2, 0, k)}
}
}
}
let has = (d, x) => I.has(d.data, x)
let copy = d => {data: N.copy(d.data)}
let everyU = every
let forEachU = forEach
let keepU = keep
let partitionU = partition
let reduceU = reduce
let someU = some