-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy patharray.fs
2866 lines (2144 loc) · 96.3 KB
/
array.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Collections
//#nowarn "1118" // 'Make' marked 'inline', perhaps because a recursive value was marked 'inline'
open System
open System.Diagnostics
open System.Collections.Generic
open Microsoft.FSharp.Core
open Microsoft.FSharp.Collections
open Microsoft.FSharp.Core.Operators
open Microsoft.FSharp.Core.CompilerServices
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
/// Basic operations on arrays
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<RequireQualifiedAccess>]
module Array =
let inline checkNonNull argName arg =
if isNull arg then
nullArg argName
let inline indexNotFound () =
raise (KeyNotFoundException(SR.GetString(SR.keyNotFoundAlt)))
[<CompiledName("Length")>]
let length (array: _ array) =
checkNonNull "array" array
array.Length
[<CompiledName("Last")>]
let inline last (array: 'T array) =
checkNonNull "array" array
if array.Length = 0 then
invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
array.[array.Length - 1]
[<CompiledName("TryLast")>]
let tryLast (array: 'T array) =
checkNonNull "array" array
if array.Length = 0 then
None
else
Some array.[array.Length - 1]
[<CompiledName("Initialize")>]
let inline init count initializer =
Microsoft.FSharp.Primitives.Basics.Array.init count initializer
[<CompiledName("ZeroCreate")>]
let zeroCreate count =
if count < 0 then
invalidArgInputMustBeNonNegative "count" count
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
[<CompiledName("Create")>]
let create (count: int) (value: 'T) =
if count < 0 then
invalidArgInputMustBeNonNegative "count" count
let array: 'T array =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
for i = 0 to Operators.Checked.(-) array.Length 1 do // use checked arithmetic here to satisfy FxCop
array.[i] <- value
array
[<CompiledName("TryHead")>]
let tryHead (array: 'T array) =
checkNonNull "array" array
if array.Length = 0 then
None
else
Some array.[0]
[<CompiledName("IsEmpty")>]
let isEmpty (array: 'T array) =
checkNonNull "array" array
array.Length = 0
[<CompiledName("Tail")>]
let tail (array: 'T array) =
checkNonNull "array" array
if array.Length = 0 then
invalidArg "array" (SR.GetString(SR.notEnoughElements))
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 1 (array.Length - 1) array
[<CompiledName("Empty")>]
let empty<'T> : 'T array = [||]
[<CompiledName("CopyTo")>]
let inline blit (source: 'T array) (sourceIndex: int) (target: 'T array) (targetIndex: int) (count: int) =
Array.Copy(source, sourceIndex, target, targetIndex, count)
let concatArrays (arrs: 'T array array) : 'T array =
let mutable acc = 0
for h in arrs do
acc <- acc + h.Length
let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked acc
let mutable j = 0
for i = 0 to arrs.Length - 1 do
let h = arrs.[i]
let len = h.Length
Array.Copy(h, 0, res, j, len)
j <- j + len
res
[<CompiledName("Concat")>]
let concat (arrays: seq<'T array>) =
checkNonNull "arrays" arrays
match arrays with
| :? ('T array array) as ts -> ts |> concatArrays // avoid a clone, since we only read the array
| _ -> arrays |> Seq.toArray |> concatArrays
[<CompiledName("Replicate")>]
let replicate count initial =
if count < 0 then
invalidArgInputMustBeNonNegative "count" count
let arr: 'T array =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked count
for i = 0 to arr.Length - 1 do
arr.[i] <- initial
arr
[<CompiledName("Collect")>]
let collect (mapping: 'T -> 'U array) (array: 'T array) : 'U array =
checkNonNull "array" array
let len = array.Length
let result =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked<'U array> len
for i = 0 to result.Length - 1 do
result.[i] <- mapping array.[i]
concatArrays result
[<CompiledName("SplitAt")>]
let splitAt index (array: 'T array) =
checkNonNull "array" array
if index < 0 then
invalidArgInputMustBeNonNegative "index" index
if array.Length < index then
raise <| InvalidOperationException(SR.GetString(SR.notEnoughElements))
if index = 0 then
let right =
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 array.Length array
[||], right
elif index = array.Length then
let left =
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 array.Length array
left, [||]
else
let res1 = Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 index array
let res2 =
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked index (array.Length - index) array
res1, res2
[<CompiledName("Take")>]
let take count (array: 'T array) =
checkNonNull "array" array
if count < 0 then
invalidArgInputMustBeNonNegative "count" count
if count = 0 then
empty
else
if count > array.Length then
raise <| InvalidOperationException(SR.GetString(SR.notEnoughElements))
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 count array
[<CompiledName("TakeWhile")>]
let takeWhile predicate (array: 'T array) =
checkNonNull "array" array
if array.Length = 0 then
empty
else
let mutable count = 0
while count < array.Length && predicate array.[count] do
count <- count + 1
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 count array
let inline countByImpl
(comparer: IEqualityComparer<'SafeKey>)
([<InlineIfLambda>] projection: 'T -> 'SafeKey)
([<InlineIfLambda>] getKey: 'SafeKey -> 'Key)
(array: 'T array)
=
let length = array.Length
if length = 0 then
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0
else
let dict = Dictionary comparer
// Build the groupings
for v in array do
let safeKey = projection v
let mutable prev = Unchecked.defaultof<_>
if dict.TryGetValue(safeKey, &prev) then
dict.[safeKey] <- prev + 1
else
dict.[safeKey] <- 1
let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked dict.Count
let mutable i = 0
for group in dict do
res.[i] <- getKey group.Key, group.Value
i <- i + 1
res
// We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
let countByValueType (projection: 'T -> 'Key) (array: 'T array) =
countByImpl HashIdentity.Structural<'Key> projection id array
// Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
let countByRefType (projection: 'T -> 'Key) (array: 'T array) =
countByImpl
RuntimeHelpers.StructBox<'Key>.Comparer
(projection >> RuntimeHelpers.StructBox)
(fun sb -> sb.Value)
array
[<CompiledName("CountBy")>]
let countBy (projection: 'T -> 'Key) (array: 'T array) =
checkNonNull "array" array
if typeof<'Key>.IsValueType then
countByValueType projection array
else
countByRefType projection array
[<CompiledName("Append")>]
let append (array1: 'T array) (array2: 'T array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let n1 = array1.Length
let n2 = array2.Length
let res: 'T array =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (n1 + n2)
Array.Copy(array1, 0, res, 0, n1)
Array.Copy(array2, 0, res, n1, n2)
res
[<CompiledName("Head")>]
let head (array: 'T array) =
checkNonNull "array" array
if array.Length = 0 then
invalidArg "array" LanguagePrimitives.ErrorStrings.InputArrayEmptyString
else
array.[0]
[<CompiledName("Copy")>]
let copy (array: 'T array) =
checkNonNull "array" array
(array.Clone() :?> 'T array) // this is marginally faster
//let len = array.Length
//let res = zeroCreate len
//for i = 0 to len - 1 do
// res.[i] <- array.[i]
//res
[<CompiledName("ToList")>]
let toList array =
checkNonNull "array" array
List.ofArray array
[<CompiledName("OfList")>]
let ofList list =
List.toArray list
[<CompiledName("Indexed")>]
let indexed (array: 'T array) =
checkNonNull "array" array
let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
for i = 0 to res.Length - 1 do
res.[i] <- (i, array.[i])
res
[<CompiledName("Iterate")>]
let inline iter ([<InlineIfLambda>] action) (array: 'T array) =
checkNonNull "array" array
for i = 0 to array.Length - 1 do
action array.[i]
[<CompiledName("Distinct")>]
let distinct (array: 'T array) =
checkNonNull "array" array
let temp = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
let mutable i = 0
let hashSet = HashSet<'T>(HashIdentity.Structural<'T>)
for v in array do
if hashSet.Add(v) then
temp.[i] <- v
i <- i + 1
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 i temp
[<CompiledName("Map")>]
let inline map ([<InlineIfLambda>] mapping: 'T -> 'U) (array: 'T array) =
checkNonNull "array" array
let res: 'U array =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
for i = 0 to res.Length - 1 do
res.[i] <- mapping array.[i]
res
[<CompiledName("Iterate2")>]
let iter2 action (array1: 'T array) (array2: 'U array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)
if array1.Length <> array2.Length then
invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
for i = 0 to array1.Length - 1 do
f.Invoke(array1.[i], array2.[i])
[<CompiledName("DistinctBy")>]
let distinctBy projection (array: 'T array) =
checkNonNull "array" array
let length = array.Length
if length = 0 then
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0
else
let temp = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
let mutable i = 0
let hashSet = HashSet<_>(HashIdentity.Structural<_>)
for v in array do
if hashSet.Add(projection v) then
temp.[i] <- v
i <- i + 1
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 i temp
[<CompiledName("Map2")>]
let map2 mapping (array1: 'T array) (array2: 'U array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(mapping)
if array1.Length <> array2.Length then
invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array1.Length
for i = 0 to res.Length - 1 do
res.[i] <- f.Invoke(array1.[i], array2.[i])
res
[<CompiledName("Map3")>]
let map3 mapping (array1: 'T1 array) (array2: 'T2 array) (array3: 'T3 array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
checkNonNull "array3" array3
let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(mapping)
let len1 = array1.Length
if len1 <> array2.Length || len1 <> array3.Length then
invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked len1
for i = 0 to res.Length - 1 do
res.[i] <- f.Invoke(array1.[i], array2.[i], array3.[i])
res
[<CompiledName("MapIndexed2")>]
let mapi2 mapping (array1: 'T array) (array2: 'U array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(mapping)
if array1.Length <> array2.Length then
invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array1.Length
for i = 0 to res.Length - 1 do
res.[i] <- f.Invoke(i, array1.[i], array2.[i])
res
[<CompiledName("IterateIndexed")>]
let iteri action (array: 'T array) =
checkNonNull "array" array
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)
for i = 0 to array.Length - 1 do
f.Invoke(i, array.[i])
[<CompiledName("IterateIndexed2")>]
let iteri2 action (array1: 'T array) (array2: 'U array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_, _, _, _>.Adapt(action)
if array1.Length <> array2.Length then
invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
for i = 0 to array1.Length - 1 do
f.Invoke(i, array1.[i], array2.[i])
[<CompiledName("MapIndexed")>]
let mapi (mapping: int -> 'T -> 'U) (array: 'T array) =
checkNonNull "array" array
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(mapping)
let res = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked array.Length
for i = 0 to array.Length - 1 do
res.[i] <- f.Invoke(i, array.[i])
res
[<CompiledName("MapFold")>]
let mapFold<'T, 'State, 'Result> (mapping: 'State -> 'T -> 'Result * 'State) state array =
checkNonNull "array" array
Microsoft.FSharp.Primitives.Basics.Array.mapFold mapping state array
[<CompiledName("MapFoldBack")>]
let mapFoldBack<'T, 'State, 'Result> (mapping: 'T -> 'State -> 'Result * 'State) array state =
checkNonNull "array" array
Microsoft.FSharp.Primitives.Basics.Array.mapFoldBack mapping array state
[<CompiledName("Exists")>]
let inline exists ([<InlineIfLambda>] predicate: 'T -> bool) (array: 'T array) =
checkNonNull "array" array
let mutable state = false
let mutable i = 0
while not state && i < array.Length do
state <- predicate array.[i]
i <- i + 1
state
[<CompiledName("Contains")>]
let inline contains value (array: 'T array) =
checkNonNull "array" array
let mutable state = false
let mutable i = 0
while not state && i < array.Length do
state <- value = array.[i]
i <- i + 1
state
[<CompiledName("Exists2")>]
let exists2 predicate (array1: _ array) (array2: _ array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(predicate)
let len1 = array1.Length
if len1 <> array2.Length then
invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
let rec loop i =
i < len1 && (f.Invoke(array1.[i], array2.[i]) || loop (i + 1))
loop 0
[<CompiledName("ForAll")>]
let forall (predicate: 'T -> bool) (array: 'T array) =
checkNonNull "array" array
let len = array.Length
let rec loop i =
i >= len || (predicate array.[i] && loop (i + 1))
loop 0
[<CompiledName("ForAll2")>]
let forall2 predicate (array1: _ array) (array2: _ array) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(predicate)
let len1 = array1.Length
if len1 <> array2.Length then
invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
let rec loop i =
i >= len1 || (f.Invoke(array1.[i], array2.[i]) && loop (i + 1))
loop 0
let inline groupByImpl
(comparer: IEqualityComparer<'SafeKey>)
([<InlineIfLambda>] keyf: 'T -> 'SafeKey)
([<InlineIfLambda>] getKey: 'SafeKey -> 'Key)
(array: 'T array)
=
let length = array.Length
if length = 0 then
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0
else
let dict = Dictionary<_, ResizeArray<_>> comparer
// Build the groupings
for i = 0 to length - 1 do
let v = array.[i]
let safeKey = keyf v
let mutable prev = Unchecked.defaultof<_>
if dict.TryGetValue(safeKey, &prev) then
prev.Add v
else
let prev = ResizeArray()
dict.[safeKey] <- prev
prev.Add v
// Return the array-of-arrays.
let result = Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked dict.Count
let mutable i = 0
for group in dict do
result.[i] <- getKey group.Key, group.Value.ToArray()
i <- i + 1
result
// We avoid wrapping a StructBox, because under 64 JIT we get some "hard" tailcalls which affect performance
let groupByValueType (keyf: 'T -> 'Key) (array: 'T array) =
groupByImpl HashIdentity.Structural<'Key> keyf id array
// Wrap a StructBox around all keys in case the key type is itself a type using null as a representation
let groupByRefType (keyf: 'T -> 'Key) (array: 'T array) =
groupByImpl
RuntimeHelpers.StructBox<'Key>.Comparer
(keyf >> RuntimeHelpers.StructBox)
(fun sb -> sb.Value)
array
[<CompiledName("GroupBy")>]
let groupBy (projection: 'T -> 'Key) (array: 'T array) =
checkNonNull "array" array
if typeof<'Key>.IsValueType then
groupByValueType projection array
else
groupByRefType projection array
[<CompiledName("Pick")>]
let pick chooser (array: _ array) =
checkNonNull "array" array
let rec loop i =
if i >= array.Length then
indexNotFound ()
else
match chooser array.[i] with
| None -> loop (i + 1)
| Some res -> res
loop 0
[<CompiledName("TryPick")>]
let tryPick chooser (array: _ array) =
checkNonNull "array" array
let rec loop i =
if i >= array.Length then
None
else
match chooser array.[i] with
| None -> loop (i + 1)
| res -> res
loop 0
[<CompiledName("Choose")>]
let choose (chooser: 'T -> 'U Option) (array: 'T array) =
checkNonNull "array" array
let mutable i = 0
let mutable first = Unchecked.defaultof<'U>
let mutable found = false
while i < array.Length && not found do
let element = array.[i]
match chooser element with
| None -> i <- i + 1
| Some b ->
first <- b
found <- true
if i <> array.Length then
let chunk1: 'U array =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked ((array.Length >>> 2) + 1)
chunk1.[0] <- first
let mutable count = 1
i <- i + 1
while count < chunk1.Length && i < array.Length do
let element = array.[i]
match chooser element with
| None -> ()
| Some b ->
chunk1.[count] <- b
count <- count + 1
i <- i + 1
if i < array.Length then
let chunk2: 'U array =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (array.Length - i)
count <- 0
while i < array.Length do
let element = array.[i]
match chooser element with
| None -> ()
| Some b ->
chunk2.[count] <- b
count <- count + 1
i <- i + 1
let res: 'U array =
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked (chunk1.Length + count)
Array.Copy(chunk1, res, chunk1.Length)
Array.Copy(chunk2, 0, res, chunk1.Length, count)
res
else
Microsoft.FSharp.Primitives.Basics.Array.subUnchecked 0 count chunk1
else
empty
// The filter module is a space and performance for Array.filter based optimization that uses
// a bitarray to store the results of the filtering of every element of the array. This means
// that the only additional temporary garbage that needs to be allocated is {array.Length/8} bytes.
//
// Other optimizations include:
// - arrays < 32 elements don't allocate any garbage at all
// - when the predicate yields consecutive runs of true data that is >= 32 elements (and fall
// into maskArray buckets) are copied in chunks using System.Array.Copy
module Filter =
let private populateMask<'a> (f: 'a -> bool) (src: 'a array) (maskArray: uint32 array) =
let mutable count = 0
for maskIdx = 0 to maskArray.Length - 1 do
let srcIdx = maskIdx * 32
let mutable mask = 0u
if f src.[srcIdx + 0x00] then
mask <- mask ||| (1u <<< 0x00)
count <- count + 1
if f src.[srcIdx + 0x01] then
mask <- mask ||| (1u <<< 0x01)
count <- count + 1
if f src.[srcIdx + 0x02] then
mask <- mask ||| (1u <<< 0x02)
count <- count + 1
if f src.[srcIdx + 0x03] then
mask <- mask ||| (1u <<< 0x03)
count <- count + 1
if f src.[srcIdx + 0x04] then
mask <- mask ||| (1u <<< 0x04)
count <- count + 1
if f src.[srcIdx + 0x05] then
mask <- mask ||| (1u <<< 0x05)
count <- count + 1
if f src.[srcIdx + 0x06] then
mask <- mask ||| (1u <<< 0x06)
count <- count + 1
if f src.[srcIdx + 0x07] then
mask <- mask ||| (1u <<< 0x07)
count <- count + 1
if f src.[srcIdx + 0x08] then
mask <- mask ||| (1u <<< 0x08)
count <- count + 1
if f src.[srcIdx + 0x09] then
mask <- mask ||| (1u <<< 0x09)
count <- count + 1
if f src.[srcIdx + 0x0A] then
mask <- mask ||| (1u <<< 0x0A)
count <- count + 1
if f src.[srcIdx + 0x0B] then
mask <- mask ||| (1u <<< 0x0B)
count <- count + 1
if f src.[srcIdx + 0x0C] then
mask <- mask ||| (1u <<< 0x0C)
count <- count + 1
if f src.[srcIdx + 0x0D] then
mask <- mask ||| (1u <<< 0x0D)
count <- count + 1
if f src.[srcIdx + 0x0E] then
mask <- mask ||| (1u <<< 0x0E)
count <- count + 1
if f src.[srcIdx + 0x0F] then
mask <- mask ||| (1u <<< 0x0F)
count <- count + 1
if f src.[srcIdx + 0x10] then
mask <- mask ||| (1u <<< 0x10)
count <- count + 1
if f src.[srcIdx + 0x11] then
mask <- mask ||| (1u <<< 0x11)
count <- count + 1
if f src.[srcIdx + 0x12] then
mask <- mask ||| (1u <<< 0x12)
count <- count + 1
if f src.[srcIdx + 0x13] then
mask <- mask ||| (1u <<< 0x13)
count <- count + 1
if f src.[srcIdx + 0x14] then
mask <- mask ||| (1u <<< 0x14)
count <- count + 1
if f src.[srcIdx + 0x15] then
mask <- mask ||| (1u <<< 0x15)
count <- count + 1
if f src.[srcIdx + 0x16] then
mask <- mask ||| (1u <<< 0x16)
count <- count + 1
if f src.[srcIdx + 0x17] then
mask <- mask ||| (1u <<< 0x17)
count <- count + 1
if f src.[srcIdx + 0x18] then
mask <- mask ||| (1u <<< 0x18)
count <- count + 1
if f src.[srcIdx + 0x19] then
mask <- mask ||| (1u <<< 0x19)
count <- count + 1
if f src.[srcIdx + 0x1A] then
mask <- mask ||| (1u <<< 0x1A)
count <- count + 1
if f src.[srcIdx + 0x1B] then
mask <- mask ||| (1u <<< 0x1B)
count <- count + 1
if f src.[srcIdx + 0x1C] then
mask <- mask ||| (1u <<< 0x1C)
count <- count + 1
if f src.[srcIdx + 0x1D] then
mask <- mask ||| (1u <<< 0x1D)
count <- count + 1
if f src.[srcIdx + 0x1E] then
mask <- mask ||| (1u <<< 0x1E)
count <- count + 1
if f src.[srcIdx + 0x1F] then
mask <- mask ||| (1u <<< 0x1F)
count <- count + 1
maskArray.[maskIdx] <- mask
count
#if BUILDING_WITH_LKG || NO_NULLCHECKING_LIB_SUPPORT
let private createMask<'a>
(f: 'a -> bool)
(src: 'a array)
(maskArrayOut: byref<uint32 array>)
(leftoverMaskOut: byref<uint32>)
=
#else
let private createMask<'a>
(f: 'a -> bool)
(src: array<'a>)
(maskArrayOut: byref<array<uint32> | null>)
(leftoverMaskOut: byref<uint32>)
=
#endif
let maskArrayLength = src.Length / 0x20
// null when there are less than 32 items in src array.
let maskArray =
if maskArrayLength = 0 then
null
else
Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked<uint32> maskArrayLength
let mutable count =
match maskArray with
| null -> 0
| maskArray -> populateMask f src maskArray
let leftoverMask =
match src.Length % 0x20 with
| 0 -> 0u
| _ ->
let mutable mask = 0u
let mutable elementMask = 1u
for arrayIdx = maskArrayLength * 0x20 to src.Length - 1 do
if f src.[arrayIdx] then
mask <- mask ||| elementMask
count <- count + 1
elementMask <- elementMask <<< 1
mask
maskArrayOut <- maskArray
leftoverMaskOut <- leftoverMask
count
let private populateDstViaMask<'a> (src: 'a array) (maskArray: uint32 array) (dst: 'a array) =
let mutable dstIdx = 0
let mutable batchCount = 0
for maskIdx = 0 to maskArray.Length - 1 do
let mask = maskArray.[maskIdx]
if mask = 0xFFFFFFFFu then
batchCount <- batchCount + 1
else
let srcIdx = maskIdx * 0x20
if batchCount <> 0 then
let batchSize = batchCount * 0x20
System.Array.Copy(src, srcIdx - batchSize, dst, dstIdx, batchSize)
dstIdx <- dstIdx + batchSize
batchCount <- 0
if mask <> 0u then
if mask &&& (1u <<< 0x00) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x00]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x01) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x01]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x02) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x02]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x03) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x03]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x04) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x04]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x05) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x05]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x06) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x06]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x07) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x07]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x08) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x08]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x09) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x09]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x0A) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x0A]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x0B) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x0B]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x0C) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x0C]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x0D) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x0D]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x0E) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x0E]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x0F) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x0F]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x10) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x10]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x11) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x11]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x12) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x12]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x13) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x13]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x14) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x14]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x15) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x15]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x16) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x16]
dstIdx <- dstIdx + 1
if mask &&& (1u <<< 0x17) <> 0u then
dst.[dstIdx] <- src.[srcIdx + 0x17]