-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathbelt_MutableQueue.ml
222 lines (187 loc) · 5.69 KB
/
belt_MutableQueue.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
(**************************************************************************)
(* *)
(* 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 BuckleScript Authors *)
module A = Belt_Array
type 'a node = {
content: 'a;
mutable next: 'a cell
}
and 'a cell = 'a node Js.null
and 'a t = {
mutable length: int;
mutable first: 'a cell;
mutable last: 'a cell
} [@@bs.deriving abstract]
let null = Js.null
let return = Js.Null.return
let make () =
t
~length: 0
~first:null
~last:null
let clear q =
lengthSet q 0;
firstSet q null;
lastSet q null
let add q x =
let cell = return @@ node
~content:x
~next:null in
match Js.nullToOption (lastGet q )with
| None ->
lengthSet q 1;
firstSet q cell;
lastSet q cell
| Some last ->
lengthSet q (lengthGet q + 1);
nextSet last cell;
lastSet q cell
let peek q =
match Js.nullToOption (firstGet q ) with
| None -> None
| Some v -> Some (contentGet v)
let peekUndefined q =
match Js.nullToOption (firstGet q ) with
| None -> Js.undefined
| Some v -> Js.Undefined.return (contentGet v)
let peekExn q =
match Js.nullToOption (firstGet q ) with
| None -> [%assert "Belt.Queue.Empty"]
| Some v -> contentGet v
let pop q =
match Js.nullToOption (firstGet q ) with
| None -> None
| Some x ->
let next = nextGet x in
if next = Js.null then
begin (* only one element*)
clear q;
Some (contentGet x)
end
else begin
lengthSet q (lengthGet q - 1);
firstSet q next;
Some(contentGet x)
end
let popExn q =
match Js.nullToOption (firstGet q ) with
| None -> [%assert "Empty"]
| Some x ->
let next = nextGet x in
if next = Js.null then
begin (* only one element*)
clear q;
contentGet x
end
else begin
lengthSet q (lengthGet q - 1);
firstSet q next;
contentGet x
end
let popUndefined q =
match Js.nullToOption (firstGet q ) with
| None -> Js.undefined
| Some x ->
let next = nextGet x in
if next = Js.null then
begin (* only one element*)
clear q;
Js.Undefined.return (contentGet x)
end
else begin
lengthSet q (lengthGet q - 1);
firstSet q next;
Js.Undefined.return (contentGet x)
end
let rec copyAux qRes prev cell =
match Js.nullToOption cell with
| None -> lastSet qRes prev; qRes
| Some x ->
let content = contentGet x in
let res = return @@ node ~content ~next:null in
begin match Js.nullToOption prev with
| None -> firstSet qRes res
| Some p -> nextSet p res
end;
copyAux qRes res (nextGet x)
let copy q =
copyAux (t ~length:(lengthGet q) ~first:null ~last:null) null (firstGet q)
let rec copyMapAux qRes prev cell f =
match Js.nullToOption cell with
| None -> lastSet qRes prev; qRes
| Some x ->
let content = f (contentGet x) [@bs] in
let res = return @@ node ~content ~next:null in
begin match Js.nullToOption prev with (*TODO: optimize to remove such check*)
| None -> firstSet qRes res
| Some p -> nextSet p res
end;
copyMapAux qRes res (nextGet x) f
let mapU q f =
copyMapAux (t ~length:(lengthGet q) ~first:null ~last:null) null (firstGet q) f
let map q f = mapU q (fun [@bs] a -> f a)
let isEmpty q =
lengthGet q = 0
let size q =
lengthGet q
let rec iterAux cell f =
match Js.nullToOption cell with
| None -> ()
| Some x ->
f (contentGet x) [@bs];
iterAux (nextGet x) f
let forEachU q f =
iterAux (firstGet q) f
let forEach q f = forEachU q (fun[@bs] a -> f a)
let rec foldAux f accu cell =
match Js.nullToOption cell with
| None -> accu
| Some x ->
let accu = f accu (contentGet x) [@bs] in
foldAux f accu (nextGet x)
let reduceU q accu f =
foldAux f accu (firstGet q)
let reduce q accu f = reduceU q accu (fun[@bs] a b -> f a b)
let transfer q1 q2 =
if lengthGet q1 > 0 then
match Js.nullToOption (lastGet q2) with
| None ->
lengthSet q2 (lengthGet q1);
firstSet q2 (firstGet q1);
lastSet q2 (lastGet q1);
clear q1
| Some l ->
lengthSet q2 (lengthGet q2 + lengthGet q1);
nextSet l (firstGet q1);
lastSet q2 (lastGet q1);
clear q1
let rec fillAux i arr cell =
match Js.nullToOption cell with
| None -> ()
| Some x ->
A.setUnsafe arr i (contentGet x);
fillAux (i + 1) arr (nextGet x)
let toArray x =
let v = A.makeUninitializedUnsafe (lengthGet x) in
fillAux 0 v (firstGet x);
v
(*TODO: optimize *)
let fromArray arr =
let q = make () in
for i = 0 to A.length arr - 1 do
add q (A.getUnsafe arr i)
done ;
q