-
Notifications
You must be signed in to change notification settings - Fork 790
/
ResizeArray.fs
449 lines (312 loc) · 11.9 KB
/
ResizeArray.fs
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Internal.Utilities
open FSharp.Core.OptimizedClosures
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module internal ResizeArray =
let length (arr: ResizeArray<'T>) = arr.Count
let get (arr: ResizeArray<'T>) (n: int) = arr[n]
let set (arr: ResizeArray<'T>) (n: int) (x: 'T) = arr[n] <- x
let create (n: int) x =
ResizeArray<_>(seq { for _ in 1..n -> x })
let init (n: int) (f: int -> 'T) =
ResizeArray<_>(seq { for i in 0 .. n - 1 -> f i })
let blit (arr1: ResizeArray<'T>) start1 (arr2: ResizeArray<'T>) start2 len =
if start1 < 0 then
invalidArg "start1" "index must be positive"
if start2 < 0 then
invalidArg "start2" "index must be positive"
if len < 0 then
invalidArg "len" "length must be positive"
if start1 + len > length arr1 then
invalidArg "start1" "(start1+len) out of range"
if start2 + len > length arr2 then
invalidArg "start2" "(start2+len) out of range"
for i = 0 to len - 1 do
arr2[start2 + i] <- arr1[start1 + i]
let concat (arrs: ResizeArray<'T> list) =
ResizeArray<_>(
seq {
for arr in arrs do
for x in arr do
yield x
}
)
let append (arr1: ResizeArray<'T>) (arr2: ResizeArray<'T>) = concat [ arr1; arr2 ]
let sub (arr: ResizeArray<'T>) start len =
if start < 0 then
invalidArg "start" "index must be positive"
if len < 0 then
invalidArg "len" "length must be positive"
if start + len > length arr then
invalidArg "len" "length must be positive"
ResizeArray<_>(seq { for i in start .. start + len - 1 -> arr[i] })
let fill (arr: ResizeArray<'T>) (start: int) (len: int) (x: 'T) =
if start < 0 then
invalidArg "start" "index must be positive"
if len < 0 then
invalidArg "len" "length must be positive"
if start + len > length arr then
invalidArg "len" "length must be positive"
for i = start to start + len - 1 do
arr[i] <- x
let copy (arr: ResizeArray<'T>) = ResizeArray<_>(arr)
let toList (arr: ResizeArray<_>) =
let mutable res = []
for i = length arr - 1 downto 0 do
res <- arr[i] :: res
res
let ofList (l: _ list) =
let len = l.Length
let res = ResizeArray<_>(len)
let rec add =
function
| [] -> ()
| e :: l ->
res.Add(e)
add l
add l
res
let iter f (arr: ResizeArray<_>) =
for i = 0 to arr.Count - 1 do
f arr[i]
let map f (arr: ResizeArray<_>) =
let len = length arr
let res = ResizeArray<_>(len)
for i = 0 to len - 1 do
res.Add(f arr[i])
res
let mapi f (arr: ResizeArray<_>) =
let f = FSharpFunc<_, _, _>.Adapt(f)
let len = length arr
let res = ResizeArray<_>(len)
for i = 0 to len - 1 do
res.Add(f.Invoke(i, arr[i]))
res
let iteri f (arr: ResizeArray<_>) =
let f = FSharpFunc<_, _, _>.Adapt(f)
for i = 0 to arr.Count - 1 do
f.Invoke(i, arr[i])
let exists (f: 'T -> bool) (arr: ResizeArray<'T>) =
let len = length arr
let rec loop i = i < len && (f arr[i] || loop (i + 1))
loop 0
let forall f (arr: ResizeArray<_>) =
let len = length arr
let rec loop i = i >= len || (f arr[i] && loop (i + 1))
loop 0
let indexNotFound () =
raise (System.Collections.Generic.KeyNotFoundException("An index satisfying the predicate was not found in the collection"))
let find f (arr: ResizeArray<_>) =
let rec loop i =
if i >= length arr then indexNotFound ()
elif f arr[i] then arr[i]
else loop (i + 1)
loop 0
let tryPick f (arr: ResizeArray<_>) =
let rec loop i =
if i >= length arr then
None
else
match f arr[i] with
| None -> loop (i + 1)
| res -> res
loop 0
let tryFind f (arr: ResizeArray<_>) =
let rec loop i =
if i >= length arr then None
elif f arr[i] then Some arr[i]
else loop (i + 1)
loop 0
let iter2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) =
let f = FSharpFunc<_, _, _>.Adapt(f)
let len1 = length arr1
if len1 <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
for i = 0 to len1 - 1 do
f.Invoke(arr1[i], arr2[i])
let map2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) =
let f = FSharpFunc<_, _, _>.Adapt(f)
let len1 = length arr1
if len1 <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
let res = ResizeArray<_>(len1)
for i = 0 to len1 - 1 do
res.Add(f.Invoke(arr1[i], arr2[i]))
res
let choose f (arr: ResizeArray<_>) =
let res = ResizeArray<_>()
for i = 0 to length arr - 1 do
match f arr[i] with
| None -> ()
| Some b -> res.Add(b)
res
let filter f (arr: ResizeArray<_>) =
let res = ResizeArray<_>()
for i = 0 to length arr - 1 do
let x = arr[i]
if f x then
res.Add(x)
res
let partition f (arr: ResizeArray<_>) =
let res1 = ResizeArray<_>()
let res2 = ResizeArray<_>()
for i = 0 to length arr - 1 do
let x = arr[i]
if f x then res1.Add(x) else res2.Add(x)
res1, res2
let rev (arr: ResizeArray<_>) =
let len = length arr
let res = ResizeArray<_>(len)
for i = len - 1 downto 0 do
res.Add(arr[i])
res
let foldBack (f: 'T -> 'State -> 'State) (arr: ResizeArray<'T>) (acc: 'State) =
let mutable res = acc
let len = length arr
for i = len - 1 downto 0 do
res <- f (get arr i) res
res
let fold (f: 'State -> 'T -> 'State) (acc: 'State) (arr: ResizeArray<'T>) =
let mutable res = acc
let len = length arr
for i = 0 to len - 1 do
res <- f res (get arr i)
res
let toArray (arr: ResizeArray<'T>) = arr.ToArray()
let ofArray (arr: 'T[]) = ResizeArray<_>(arr)
let toSeq (arr: ResizeArray<'T>) = Seq.readonly arr
let sort f (arr: ResizeArray<'T>) = arr.Sort(System.Comparison(f))
let sortBy f (arr: ResizeArray<'T>) =
arr.Sort(System.Comparison(fun x y -> compare (f x) (f y)))
let exists2 f (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) =
let len1 = length arr1
if len1 <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
let rec loop i =
i < len1 && (f arr1[i] arr2[i] || loop (i + 1))
loop 0
let findIndex f (arr: ResizeArray<_>) =
let rec go n =
if n >= length arr then indexNotFound ()
elif f arr[n] then n
else go (n + 1)
go 0
let findIndexi f (arr: ResizeArray<_>) =
let rec go n =
if n >= length arr then indexNotFound ()
elif f n arr[n] then n
else go (n + 1)
go 0
let foldSub f acc (arr: ResizeArray<_>) start fin =
let mutable res = acc
for i = start to fin do
res <- f res arr[i]
res
let foldBackSub f (arr: ResizeArray<_>) start fin acc =
let mutable res = acc
for i = fin downto start do
res <- f arr[i] res
res
let reduce f (arr: ResizeArray<_>) =
let arrn = length arr
if arrn = 0 then
invalidArg "arr" "the input array may not be empty"
else
foldSub f arr[0] arr 1 (arrn - 1)
let reduceBack f (arr: ResizeArray<_>) =
let arrn = length arr
if arrn = 0 then
invalidArg "arr" "the input array may not be empty"
else
foldBackSub f arr 0 (arrn - 2) arr[arrn - 1]
let fold2 f (acc: 'T) (arr1: ResizeArray<'T1>) (arr2: ResizeArray<'T2>) =
let f = FSharpFunc<_, _, _, _>.Adapt(f)
let mutable res = acc
let len = length arr1
if len <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
for i = 0 to len - 1 do
res <- f.Invoke(res, arr1[i], arr2[i])
res
let foldBack2 f (arr1: ResizeArray<'T1>) (arr2: ResizeArray<'T2>) (acc: 'b) =
let f = FSharpFunc<_, _, _, _>.Adapt(f)
let mutable res = acc
let len = length arr1
if len <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
for i = len - 1 downto 0 do
res <- f.Invoke(arr1[i], arr2[i], res)
res
let forall2 f (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) =
let len1 = length arr1
if len1 <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
let rec loop i =
i >= len1 || (f arr1[i] arr2[i] && loop (i + 1))
loop 0
let isEmpty (arr: ResizeArray<_>) = length (arr: ResizeArray<_>) = 0
let iteri2 f (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) =
let f = FSharpFunc<_, _, _, _>.Adapt(f)
let len1 = length arr1
if len1 <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
for i = 0 to len1 - 1 do
f.Invoke(i, arr1[i], arr2[i])
let mapi2 (f: int -> 'T -> 'b -> 'c) (arr1: ResizeArray<'T>) (arr2: ResizeArray<'b>) =
let f = FSharpFunc<_, _, _, _>.Adapt(f)
let len1 = length arr1
if len1 <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
init len1 (fun i -> f.Invoke(i, arr1[i], arr2[i]))
let scanBackSub f (arr: ResizeArray<'T>) start fin acc =
let f = FSharpFunc<_, _, _>.Adapt(f)
let mutable state = acc
let res = create (2 + fin - start) acc
for i = fin downto start do
state <- f.Invoke(arr[i], state)
res[i - start] <- state
res
let scanSub f acc (arr: ResizeArray<'T>) start fin =
let f = FSharpFunc<_, _, _>.Adapt(f)
let mutable state = acc
let res = create (fin - start + 2) acc
for i = start to fin do
state <- f.Invoke(state, arr[i])
res[i - start + 1] <- state
res
let scan f acc (arr: ResizeArray<'T>) =
let arrn = length arr
scanSub f acc arr 0 (arrn - 1)
let scanBack f (arr: ResizeArray<'T>) acc =
let arrn = length arr
scanBackSub f arr 0 (arrn - 1) acc
let singleton x =
let res = ResizeArray<_>(1)
res.Add(x)
res
let tryFindIndex f (arr: ResizeArray<'T>) =
let rec go n =
if n >= length arr then None
elif f arr[n] then Some n
else go (n + 1)
go 0
let tryFindIndexi f (arr: ResizeArray<'T>) =
let rec go n =
if n >= length arr then None
elif f n arr[n] then Some n
else go (n + 1)
go 0
let zip (arr1: ResizeArray<_>) (arr2: ResizeArray<_>) =
let len1 = length arr1
if len1 <> length arr2 then
invalidArg "arr2" "the arrays have different lengths"
init len1 (fun i -> arr1[i], arr2[i])
let unzip (arr: ResizeArray<_>) =
let len = length arr
let res1 = ResizeArray<_>(len)
let res2 = ResizeArray<_>(len)
for i = 0 to len - 1 do
let x, y = arr[i]
res1.Add(x)
res2.Add(y)
res1, res2