-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmono-array-or-vector.sml
364 lines (361 loc) · 7.78 KB
/
mono-array-or-vector.sml
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
(*
MonoArrayOrVector
Generic Implementations of Monomorphic Random-Access Sequence Functions
Copyright 2011 Christopher Cramer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*)
functor MonoArrayOrVector (T: sig
type t
type x
val length: t -> int
val sub: t * int -> x
val tabulate: int * (int -> x) -> t
val foldl: (x * 'a -> 'a) -> 'a -> t -> 'a
val foldr: (x * 'a -> 'a) -> 'a -> t -> 'a
val concat: t list -> t
end) = struct
fun isEmpty t = T.length t = 0
fun hd t =
if isEmpty t then raise Subscript
else T.sub (t, 0)
fun appr f t =
let
fun loop i =
if i < 0 then ()
else (
f (T.sub (t, i))
; loop (i - 1)
)
in
loop (T.length t - 1)
end
fun appri f t =
let
fun loop i =
if i < 0 then ()
else (
f (i, T.sub (t, i))
; loop (i - 1)
)
in
loop (T.length t - 1)
end
fun reducel f t =
let
val n = T.length t
fun loop (i, a) =
if i >= n then a
else loop (i + 1, f (T.sub (t, i), a))
in
if n = 0 then raise Empty
else loop (1, T.sub (t, 0))
end
fun reducer f t =
let
val n = T.length t
fun loop (i, a) =
if i < 0 then a
else loop (i - 1, f (T.sub (t, i), a))
in
if n = 0 then raise Empty
else loop (n - 2, T.sub (t, n - 1))
end
fun findr f t =
let
fun loop i =
if i < 0 then NONE
else
let
val x = T.sub (t, i)
in
if f x then SOME x
else loop (i - 1)
end
in
loop (T.length t - 1)
end
fun findri f t =
let
fun loop i =
if i < 0 then NONE
else
let
val x = (i, T.sub (t, i))
in
if f x then SOME x
else loop (i - 1)
end
in
loop (T.length t - 1)
end
fun toShadow t = Shadow.fromRandom {
sub = fn i => T.sub (t, i)
, length = fn () => T.length t
}
fun revToShadow t = Shadow.rev (toShadow t)
fun toList t = T.foldr (fn (x, y) => x :: y) nil t
fun revToList t = T.foldl (fn (x, y) => x :: y) nil t
fun toTabulated tabulate t = tabulate (T.length t, fn i => T.sub (t, i))
fun revToTabulated tabulate t =
let
val n = T.length t
in
tabulate (n, fn i => T.sub (t, n - 1 - i))
end
fun empty () = T.tabulate (0, fn _ => raise Fail "impossible")
fun single x = T.tabulate (1, fn _ => x)
fun unfold f a =
let
fun construct (n, l) =
let
val l = ref l
in
T.tabulate (
n
, fn _ => (case !l of
nil => raise Fail "impossible"
| x :: y => (
l := y
; x
)
)
)
end
fun loop (n, l, a) = case f a of
NONE => construct (n, l)
| SOME (x, a) => loop (n + 1, x :: l, a)
in
loop (0, nil, a)
end
fun unfoldn f (n, a) =
let
val a = ref a
in
T.tabulate (
n
, fn _ =>
let
val (x, b) = f (!a)
in
a := b
; x
end
)
end
fun splitAt (t, n) = (
T.tabulate (n, fn i => T.sub (t, i))
, T.tabulate (T.length t - n, fn i => T.sub (t, n + i))
)
fun split f t =
let
val n = T.length t
fun loop i =
if i >= n then (t, empty ())
else if f (T.sub (t, i)) then loop (i + 1)
else splitAt (t, i)
in
loop 0
end
fun drop f t =
let
val n = T.length t
fun loop i =
if i >= n then empty ()
else if f (T.sub (t, i)) then loop (i + 1)
else T.tabulate (n - i, fn j => T.sub (t, i + j))
in
loop 0
end
fun extract (t, i, n) = T.tabulate (
case n of
NONE => T.length t - i
| SOME n => n
, fn j => T.sub (t, i + j)
)
fun trim (t, n) = extract (t, n, NONE)
fun limit (t, n) = extract (t, 0, SOME n)
fun take f t =
let
val n = T.length t
fun loop i =
if i >= n then t
else if f (T.sub (t, i)) then loop (i + 1)
else limit (t, i)
in
loop 0
end
fun tokens f t =
let
val n = T.length t
fun token (i, j, l) =
let
fun add () = extract (t, i, SOME (j - i)) :: l
in
if j >= n then List.rev (add ())
else if f (T.sub (t, j)) then separator (j + 1, add ())
else token (i, j + 1, l)
end
and separator (i, l) =
if i >= n then List.rev l
else if f (T.sub (t, i)) then separator (i + 1, l)
else token (i, i + 1, l)
in
separator (0, nil)
end
fun fields f t =
let
val n = T.length t
fun field (i, j, l) =
let
fun add () = extract (t, i, SOME (j - i)) :: l
in
if j >= n then List.rev (add ())
else if f (T.sub (t, j)) then field (j + 1, j + 1, add ())
else field (i, j + 1, l)
end
in
field (0, 0, nil)
end
fun translate f t =
let
val n = T.length t
fun loop (i, l) =
if i >= n then T.concat (List.rev l)
else loop (i + 1, f (T.sub (t, i)) :: l)
in
loop (0, nil)
end
fun insert (t, i, x) = T.tabulate (
T.length t + 1
, fn j =>
if j < i then T.sub (t, j)
else if j = i then x
else T.sub (t, j - 1)
)
fun delete (t, i) = T.tabulate (
T.length t - 1
, fn j =>
if j < i then T.sub (t, j)
else T.sub (t, j + 1)
)
fun fromShadow s = case Shadow.access s of
Shadow.Random => T.tabulate (Shadow.length s, fn i => Shadow.sub (s, i))
| Shadow.Sequential => unfoldn
(fn s => valOf (Shadow.getItem s))
(Shadow.length s, s)
fun fromRevShadow s = fromShadow (Shadow.rev s)
fun fromLengthSub (length, sub) x = T.tabulate (length x, fn i => sub (x, i))
fun fromRevLengthSub (length, sub) x =
let
val n = length x
in
T.tabulate (n, fn i => sub (x, n - 1 - i))
end
fun getItem t =
let
val n = T.length t
in
if n = 0 then NONE
else SOME (
T.sub (t, 0)
, T.tabulate (n - 1, fn i => T.sub (t, i + 1))
)
end
fun ungetItem (t, x) = T.tabulate (
T.length t + 1
, fn
0 => x
| i => T.sub (t, i - 1)
)
fun tl t =
let
val n = T.length t
in
if n = 0 then raise Empty
else T.tabulate (n - 1, fn i => T.sub (t, i + 1))
end
fun append (a, b) =
let
val n = T.length a
in
T.tabulate (
n + T.length b
, fn i =>
if i < n then T.sub (a, i)
else T.sub (b, i - n)
)
end
fun filter f t =
let
val n = T.length t
fun return (m, l) = unfoldn (valOf o List.getItem) (m, l)
fun loop (i, m, l) =
if i < 0 then return (m, l)
else
let
val x = T.sub (t, i)
in
if f x then loop (i - 1, m + 1, x :: l)
else loop (i - 1, m, l)
end
in
loop (n - 1, 0, nil)
end
fun mapPartial f t =
let
val n = T.length t
fun return (m, l) = unfoldn (valOf o List.getItem) (m, l)
fun loop (i, m, l) =
if i < 0 then return (m, l)
else case f (T.sub (t, i)) of
NONE => loop (i - 1, m, l)
| SOME x => loop (i - 1, m + 1, x :: l)
in
loop (n - 1, 0, nil)
end
fun partition f t =
let
val n = T.length t
fun construct (n, l) = unfoldn (valOf o List.getItem) (n, l)
fun return (yesN, yesL, noN, noL) = (
construct (yesN, yesL)
, construct (noN, noL)
)
fun loop (i, yesN, yesL, noN, noL) =
if i < 0 then return (yesN, yesL, noN, noL)
else
let
val x = T.sub (t, i)
in
if f x then loop (
i - 1
, yesN + 1, x :: yesL
, noN, noL
) else loop (
i - 1
, yesN, yesL
, noN + 1, x :: noL
)
end
in
loop (n - 1, 0, nil, 0, nil)
end
fun concatWith t l =
let
fun loop (l, r) = case l of
nil => empty ()
| x :: nil => T.concat (List.rev (x :: l))
| x :: (y as (z :: _)) => loop (y, z :: t :: r)
in
loop (l, nil)
end
end