-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathBelt_MapString.res
191 lines (177 loc) · 4.01 KB
/
Belt_MapString.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
type key = string
module I = Belt_internalMapString
module N = Belt_internalAVLtree
module A = Belt_Array
type t<'a> = N.t<key, 'a>
let empty = None
let isEmpty = N.isEmpty
/* let singleton = N.singleton */
let minKey = N.minKey
let minKeyUndefined = N.minKeyUndefined
let maxKey = N.maxKey
let maxKeyUndefined = N.maxKeyUndefined
let minimum = N.minimum
let minUndefined = N.minUndefined
let maximum = N.maximum
let maxUndefined = N.maxUndefined
let findFirstBy = N.findFirstBy
let forEach = N.forEach
let map = N.map
let mapWithKey = N.mapWithKey
let reduce = N.reduce
let every = N.every
let some = N.some
let keep = N.keepShared
let partition = N.partitionShared
let size = N.size
let toList = N.toList
let toArray = N.toArray
let keysToArray = N.keysToArray
let valuesToArray = N.valuesToArray
let checkInvariantInternal = N.checkInvariantInternal
let rec set = (t, newK: key, newD: _) =>
switch t {
| None => N.singleton(newK, newD)
| Some(n) =>
let k = n.N.key
if newK == k {
Some(N.updateValue(n, newD))
} else {
let v = n.N.value
if newK < k {
N.bal(set(n.N.left, newK, newD), k, v, n.N.right)
} else {
N.bal(n.N.left, k, v, set(n.N.right, newK, newD))
}
}
}
let rec update = (t, x: key, f) =>
switch t {
| None =>
switch f(None) {
| None => t
| Some(data) => N.singleton(x, data)
}
| Some(n) =>
let k = n.N.key
if x == k {
switch f(Some(n.N.value)) {
| None =>
let {N.left: l, right: r} = n
switch (l, r) {
| (None, _) => r
| (_, None) => l
| (_, Some(rn)) =>
let (kr, vr) = (ref(rn.N.key), ref(rn.N.value))
let r = N.removeMinAuxWithRef(rn, kr, vr)
N.bal(l, kr.contents, vr.contents, r)
}
| Some(data) => Some(N.updateValue(n, data))
}
} else {
let {N.left: l, right: r, value: v} = n
if x < k {
let ll = update(l, x, f)
if l === ll {
t
} else {
N.bal(ll, k, v, r)
}
} else {
let rr = update(r, x, f)
if r === rr {
t
} else {
N.bal(l, k, v, rr)
}
}
}
}
let rec removeAux = (n, x: key) => {
let {N.left: l, key: v, right: r} = n
if x == v {
switch (l, r) {
| (None, _) => r
| (_, None) => l
| (_, Some(rn)) =>
let (kr, vr) = (ref(rn.N.key), ref(rn.N.value))
let r = N.removeMinAuxWithRef(rn, kr, vr)
N.bal(l, kr.contents, vr.contents, r)
}
} else if x < v {
switch l {
| None => Some(n)
| Some(left) =>
let ll = removeAux(left, x)
if ll === l {
Some(n)
} else {
open N
bal(ll, v, n.value, r)
}
}
} else {
switch r {
| None => Some(n)
| Some(right) =>
let rr = removeAux(right, x)
N.bal(l, v, n.N.value, rr)
}
}
}
let remove = (n, x) =>
switch n {
| None => None
| Some(n) => removeAux(n, x)
}
let rec removeMany0 = (t, xs, i, len) =>
if i < len {
let ele = A.getUnsafe(xs, i)
let u = removeAux(t, ele)
switch u {
| None => u
| Some(t) => removeMany0(t, xs, i + 1, len)
}
} else {
Some(t)
}
let removeMany = (t, keys) => {
let len = A.length(keys)
switch t {
| None => None
| Some(t) => removeMany0(t, keys, 0, len)
}
}
let mergeMany = (h, arr) => {
let len = A.length(arr)
let v = ref(h)
for i in 0 to len - 1 {
let (key, value) = A.getUnsafe(arr, i)
v.contents = set(v.contents, key, value)
}
v.contents
}
/* let mergeArray = mergeMany */
let has = I.has
let cmp = I.cmp
let eq = I.eq
let get = I.get
let getUndefined = I.getUndefined
let getWithDefault = I.getWithDefault
let getExn = I.getExn
let split = I.split
let merge = I.merge
let fromArray = I.fromArray
let cmpU = cmp
let eqU = eq
let everyU = every
let findFirstByU = findFirstBy
let forEachU = forEach
let keepU = keep
let mapU = map
let mapWithKeyU = mapWithKey
let mergeU = merge
let partitionU = partition
let reduceU = reduce
let someU = some
let updateU = update