-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathBelt_MutableQueue.res
228 lines (203 loc) · 4.46 KB
/
Belt_MutableQueue.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
/* ************************************************************************ */
/* */
/* OCaml */
/* */
/* Francois Pottier, projet Cristal, INRIA Rocquencourt */
/* Jeremie Dimino, Jane Street Europe */
/* */
/* Copyright 2002 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/* ************************************************************************ */
/* Adapted significantly by ReScript Authors */
module A = Belt_Array
type rec node<'a> = {
content: 'a,
mutable next: cell<'a>,
}
and cell<'a> = option<node<'a>>
and t<'a> = {
mutable length: int,
mutable first: cell<'a>,
mutable last: cell<'a>,
}
let make = () => {
length: 0,
first: None,
last: None,
}
let clear = q => {
q.length = 0
q.first = None
q.last = None
}
let add = (q, x) => {
let cell = Some({
content: x,
next: None,
})
switch q.last {
| None =>
/* TODO: better names for intermediate var */
q.length = 1
q.first = cell
q.last = cell
| Some(last) =>
q.length = q.length + 1
last.next = cell
q.last = cell
}
}
let peek = q =>
switch q.first {
/* same here could be v */
| None => None
| Some(v) => Some(v.content)
}
let peekUndefined = q =>
switch q.first {
| None => Js.undefined
| Some(v) => Js.Undefined.return(v.content)
}
let peekExn = q =>
switch q.first {
| None => raise(Not_found)
| Some(v) => v.content
}
let pop = q =>
switch q.first {
| None => None
| Some(x) =>
let next = x.next
if next == None {
/* only one element */
clear(q)
Some(x.content)
} else {
q.length = q.length - 1
q.first = next
Some(x.content)
}
}
let popExn = q =>
/* TO fix */
switch q.first {
| None => raise(Not_found)
| Some(x) =>
let next = x.next
if next == None {
/* only one element */
clear(q)
x.content
} else {
q.length = q.length - 1
q.first = next
x.content
}
}
let popUndefined = q =>
switch q.first {
| None => Js.undefined
| Some(x) =>
let next = x.next
if next == None {
/* only one element */
clear(q)
Js.Undefined.return(x.content)
} else {
q.length = q.length - 1
q.first = next
Js.Undefined.return(x.content)
}
}
let rec copyAux = (qRes, prev, cell) =>
switch cell {
| None =>
qRes.last = prev
qRes
| Some(x) =>
let content = x.content
let res = Some({content, next: None})
switch prev {
| None => qRes.first = res
| Some(p) => p.next = res
}
copyAux(qRes, res, x.next)
}
let copy = q => copyAux({length: q.length, first: None, last: None}, None, q.first)
let rec copyMapAux = (qRes, prev, cell, f) =>
switch cell {
| None =>
qRes.last = prev
qRes
| Some(x) =>
let content = f(x.content)
let res = Some({content, next: None})
switch prev {
/* TODO: optimize to remove such check */
| None => qRes.first = res
| Some(p) => p.next = res
}
copyMapAux(qRes, res, x.next, f)
}
let map = (q, f) => copyMapAux({length: q.length, first: None, last: None}, None, q.first, f)
let isEmpty = q => q.length == 0
let size = q => q.length
let rec iterAux = (cell, f) =>
switch cell {
| None => ()
| Some(x) =>
f(x.content)
iterAux(x.next, f)
}
let forEach = (q, f) => iterAux(q.first, f)
let rec foldAux = (f, accu, cell) =>
switch cell {
| None => accu
| Some(x) =>
let accu = f(accu, x.content)
foldAux(f, accu, x.next)
}
let reduce = (q, accu, f) => foldAux(f, accu, q.first)
let transfer = (q1, q2) =>
if q1.length > 0 {
switch q2.last {
| None =>
q2.length = q1.length
q2.first = q1.first
q2.last = q1.last
clear(q1)
| Some(l) =>
q2.length = q2.length + q1.length
l.next = q1.first
q2.last = q1.last
clear(q1)
}
}
let rec fillAux = (i, arr, cell) =>
switch cell {
| None => ()
| Some(x) =>
A.setUnsafe(arr, i, x.content)
fillAux(i + 1, arr, x.next)
}
let toArray = x => {
let v = A.makeUninitializedUnsafe(x.length)
fillAux(0, v, x.first)
v
}
/* TODO: optimize */
let fromArray = arr => {
let q = make()
for i in 0 to A.length(arr) - 1 {
add(q, A.getUnsafe(arr, i))
}
q
}
let forEachU = forEach
let mapU = map
let reduceU = reduce