forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal.fs
More file actions
658 lines (576 loc) · 24.3 KB
/
Copy pathlocal.fs
File metadata and controls
658 lines (576 loc) · 24.3 KB
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
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Primitives.Basics
open Microsoft.FSharp.Core
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
open Microsoft.FSharp.Core.LanguagePrimitives.ErrorStrings
open Microsoft.FSharp.Collections
open Microsoft.FSharp.Core.Operators
open System.Diagnostics.CodeAnalysis
open System.Collections.Generic
open System.Runtime.InteropServices
#if FX_NO_ICLONEABLE
open Microsoft.FSharp.Core.ICloneableExtensions
#else
#endif
module internal List =
let arrayZeroCreate (n:int) = (# "newarr !0" type ('T) n : 'T array #)
[<SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")>]
let nonempty x = match x with [] -> false | _ -> true
let rec iter f x = match x with [] -> () | (h::t) -> f h; iter f t
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let inline setFreshConsTail cons t = cons.(::).1 <- t
let inline freshConsNoTail h = h :: (# "ldnull" : 'T list #)
let rec mapToFreshConsTail cons f x =
match x with
| [] ->
setFreshConsTail cons [];
| (h::t) ->
let cons2 = freshConsNoTail (f h)
setFreshConsTail cons cons2;
mapToFreshConsTail cons2 f t
let map f x =
match x with
| [] -> []
| [h] -> [f h]
| (h::t) ->
let cons = freshConsNoTail (f h)
mapToFreshConsTail cons f t
cons
let rec mapiToFreshConsTail cons (f:OptimizedClosures.FSharpFunc<_,_,_>) x i =
match x with
| [] ->
setFreshConsTail cons [];
| (h::t) ->
let cons2 = freshConsNoTail (f.Invoke(i,h))
setFreshConsTail cons cons2;
mapiToFreshConsTail cons2 f t (i+1)
let mapi f x =
match x with
| [] -> []
| [h] -> [f 0 h]
| (h::t) ->
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
let cons = freshConsNoTail (f.Invoke(0,h))
mapiToFreshConsTail cons f t 1
cons
let rec map2ToFreshConsTail cons (f:OptimizedClosures.FSharpFunc<_,_,_>) xs1 xs2 =
match xs1,xs2 with
| [],[] ->
setFreshConsTail cons [];
| (h1::t1),(h2::t2) ->
let cons2 = freshConsNoTail (f.Invoke(h1,h2))
setFreshConsTail cons cons2;
map2ToFreshConsTail cons2 f t1 t2
| _ -> invalidArg "xs2" (SR.GetString(SR.listsHadDifferentLengths))
let map2 f xs1 xs2 =
match xs1,xs2 with
| [],[] -> []
| (h1::t1),(h2::t2) ->
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
let cons = freshConsNoTail (f.Invoke(h1,h2))
map2ToFreshConsTail cons f t1 t2
cons
| _ -> invalidArg "xs2" (SR.GetString(SR.listsHadDifferentLengths))
let rec forall f xs1 =
match xs1 with
| [] -> true
| (h1::t1) -> f h1 && forall f t1
let rec exists f xs1 =
match xs1 with
| [] -> false
| (h1::t1) -> f h1 || exists f t1
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec revAcc xs acc =
match xs with
| [] -> acc
| h::t -> revAcc t (h::acc)
let rev xs =
match xs with
| [] -> xs
| [_] -> xs
| h1::h2::t -> revAcc t [h2;h1]
// return the last cons it the chain
let rec appendToFreshConsTail cons xs =
match xs with
| [] ->
setFreshConsTail cons xs // note, xs = []
cons
| h::t ->
let cons2 = freshConsNoTail h
setFreshConsTail cons cons2
appendToFreshConsTail cons2 t
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec collectToFreshConsTail (f:'T -> 'U list) (list:'T list) cons =
match list with
| [] ->
setFreshConsTail cons []
| h::t ->
collectToFreshConsTail f t (appendToFreshConsTail cons (f h))
let rec collect (f:'T -> 'U list) (list:'T list) =
match list with
| [] -> []
| [h] -> f h
| _ ->
let cons = freshConsNoTail (Unchecked.defaultof<'U>)
collectToFreshConsTail f list cons
cons.Tail
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec filterToFreshConsTail cons f l =
match l with
| [] ->
setFreshConsTail cons l; // note, l = nil
| h::t ->
if f h then
let cons2 = freshConsNoTail h
setFreshConsTail cons cons2;
filterToFreshConsTail cons2 f t
else
filterToFreshConsTail cons f t
let rec filter f l =
match l with
| [] -> l
| h :: ([] as nil) -> if f h then l else nil
| h::t ->
if f h then
let cons = freshConsNoTail h
filterToFreshConsTail cons f t;
cons
else
filter f t
let iteri f x =
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
let rec loop n x = match x with [] -> () | (h::t) -> f.Invoke(n,h); loop (n+1) t
loop 0 x
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec concatToFreshConsTail cons h1 l =
match l with
| [] -> setFreshConsTail cons h1
| h2::t -> concatToFreshConsTail (appendToFreshConsTail cons h1) h2 t
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec concatToEmpty l =
match l with
| [] -> []
| []::t -> concatToEmpty t
| (h::t1)::tt2 ->
let res = freshConsNoTail h
concatToFreshConsTail res t1 tt2;
res
let toArray (l:'T list) =
let len = l.Length
let res = arrayZeroCreate len
let rec loop i l =
match l with
| [] -> ()
| h::t ->
res.[i] <- h
loop (i+1) t
loop 0 l
res
let ofArray (arr:'T[]) =
let len = arr.Length
let mutable res = ([]: 'T list)
for i = len - 1 downto 0 do
res <- arr.[i] :: res
res
let inline ofSeq (e : IEnumerable<'T>) =
match e with
| :? list<'T> as l -> l
| :? ('T[]) as arr -> ofArray arr
| _ ->
use ie = e.GetEnumerator()
if not (ie.MoveNext()) then []
else
let res = freshConsNoTail ie.Current
let mutable cons = res
while ie.MoveNext() do
let cons2 = freshConsNoTail ie.Current
setFreshConsTail cons cons2
cons <- cons2
setFreshConsTail cons []
res
let concat (l : seq<_>) =
match ofSeq l with
| [] -> []
| [h] -> h
| [h1;h2] -> h1 @ h2
| l -> concatToEmpty l
let rec initToFreshConsTail cons i n f =
if i < n then
let cons2 = freshConsNoTail (f i)
setFreshConsTail cons cons2;
initToFreshConsTail cons2 (i+1) n f
else
setFreshConsTail cons []
let init count f =
if count < 0 then invalidArg "count" InputMustBeNonNegativeString
if count = 0 then []
else
let res = freshConsNoTail (f 0)
initToFreshConsTail res 1 count f
res
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec partitionToFreshConsTails consL consR p l =
match l with
| [] ->
setFreshConsTail consL l; // note, l = nil
setFreshConsTail consR l; // note, l = nil
| h::t ->
let cons' = freshConsNoTail h
if p h then
setFreshConsTail consL cons';
partitionToFreshConsTails cons' consR p t
else
setFreshConsTail consR cons';
partitionToFreshConsTails consL cons' p t
let rec partitionToFreshConsTailLeft consL p l =
match l with
| [] ->
setFreshConsTail consL l; // note, l = nil
l // note, l = nil
| h::t ->
let cons' = freshConsNoTail h
if p h then
setFreshConsTail consL cons';
partitionToFreshConsTailLeft cons' p t
else
partitionToFreshConsTails consL cons' p t;
cons'
let rec partitionToFreshConsTailRight consR p l =
match l with
| [] ->
setFreshConsTail consR l; // note, l = nil
l // note, l = nil
| h::t ->
let cons' = freshConsNoTail h
if p h then
partitionToFreshConsTails cons' consR p t;
cons'
else
setFreshConsTail consR cons';
partitionToFreshConsTailRight cons' p t
let partition p l =
match l with
| [] -> l,l
| h :: ([] as nil) -> if p h then l,nil else nil,l
| h::t ->
let cons = freshConsNoTail h
if p h
then cons, (partitionToFreshConsTailLeft cons p t)
else (partitionToFreshConsTailRight cons p t), cons
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec unzipToFreshConsTail cons1a cons1b x =
match x with
| [] ->
setFreshConsTail cons1a []
setFreshConsTail cons1b []
| ((h1,h2)::t) ->
let cons2a = freshConsNoTail h1
let cons2b = freshConsNoTail h2
setFreshConsTail cons1a cons2a;
setFreshConsTail cons1b cons2b;
unzipToFreshConsTail cons2a cons2b t
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let unzip x =
match x with
| [] ->
[],[]
| ((h1,h2)::t) ->
let res1a = freshConsNoTail h1
let res1b = freshConsNoTail h2
unzipToFreshConsTail res1a res1b t;
res1a,res1b
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec unzip3ToFreshConsTail cons1a cons1b cons1c x =
match x with
| [] ->
setFreshConsTail cons1a [];
setFreshConsTail cons1b [];
setFreshConsTail cons1c [];
| ((h1,h2,h3)::t) ->
let cons2a = freshConsNoTail h1
let cons2b = freshConsNoTail h2
let cons2c = freshConsNoTail h3
setFreshConsTail cons1a cons2a;
setFreshConsTail cons1b cons2b;
setFreshConsTail cons1c cons2c;
unzip3ToFreshConsTail cons2a cons2b cons2c t
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let unzip3 x =
match x with
| [] ->
[],[],[]
| ((h1,h2,h3)::t) ->
let res1a = freshConsNoTail h1
let res1b = freshConsNoTail h2
let res1c = freshConsNoTail h3
unzip3ToFreshConsTail res1a res1b res1c t;
res1a,res1b,res1c
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec zipToFreshConsTail cons xs1 xs2 =
match xs1,xs2 with
| [],[] ->
setFreshConsTail cons []
| (h1::t1),(h2::t2) ->
let cons2 = freshConsNoTail (h1,h2)
setFreshConsTail cons cons2;
zipToFreshConsTail cons2 t1 t2
| _ ->
invalidArg "xs2" (SR.GetString(SR.listsHadDifferentLengths))
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let zip xs1 xs2 =
match xs1,xs2 with
| [],[] -> []
| (h1::t1),(h2::t2) ->
let res = freshConsNoTail (h1,h2)
zipToFreshConsTail res t1 t2;
res
| _ ->
invalidArg "xs2" (SR.GetString(SR.listsHadDifferentLengths))
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let rec zip3ToFreshConsTail cons xs1 xs2 xs3 =
match xs1,xs2,xs3 with
| [],[],[] ->
setFreshConsTail cons [];
| (h1::t1),(h2::t2),(h3::t3) ->
let cons2 = freshConsNoTail (h1,h2,h3)
setFreshConsTail cons cons2;
zip3ToFreshConsTail cons2 t1 t2 t3
| _ ->
invalidArg "xs1" (SR.GetString(SR.listsHadDifferentLengths))
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
// tail cons cells is permitted in carefully written library code.
let zip3 xs1 xs2 xs3 =
match xs1,xs2,xs3 with
| [],[],[] ->
[]
| (h1::t1),(h2::t2),(h3::t3) ->
let res = freshConsNoTail (h1,h2,h3)
zip3ToFreshConsTail res t1 t2 t3;
res
| _ ->
invalidArg "xs1" (SR.GetString(SR.listsHadDifferentLengths))
// NOTE: This implementation is now only used for List.sortWith. We should change that to use the stable sort via arrays
// below, and remove this implementation.
module StableSortImplementation =
// Internal copy of stable sort
let rec revAppend xs1 xs2 =
match xs1 with
| [] -> xs2
| h::t -> revAppend t (h::xs2)
let half x = x >>> 1
let rec merge cmp a b acc =
match a,b with
| [], a | a,[] -> revAppend acc a
| x::a', y::b' -> if cmp x y > 0 then merge cmp a b' (y::acc) else merge cmp a' b (x::acc)
let sort2 cmp x y =
if cmp x y > 0 then [y;x] else [x;y]
let sort3 cmp x y z =
let cxy = cmp x y
let cyz = cmp y z
if cxy > 0 && cyz < 0 then
if cmp x z > 0 then [y;z;x] else [y;x;z]
elif cxy < 0 && cyz > 0 then
if cmp x z > 0 then [z;x;y] else [x;z;y]
elif cxy > 0 then
if cyz > 0 then [z;y;x]
else [y;z;x]
else
if cyz > 0 then [z;x;y]
else [x;y;z]
let trivial a = match a with [] | [_] -> true | _ -> false
(* tail recursive using a ref *)
let rec stableSortInner cmp la ar =
if la < 4 then (* sort two || three new entries *)
match !ar with
| x::y::b ->
if la = 2 then ( ar := b; sort2 cmp x y )
else begin
match b with
| z::c -> ( ar := c; sort3 cmp x y z )
| _ -> failwith "never"
end
| _ -> failwith "never"
else (* divide *)
let lb = half la
let sb = stableSortInner cmp lb ar
let sc = stableSortInner cmp (la - lb) ar
merge cmp sb sc []
let stableSort cmp (a: 'T list) =
if trivial a then a else
let ar = ref a
stableSortInner cmp a.Length ar
let sortWith cmp a = StableSortImplementation.stableSort cmp a
module internal Array =
open System
open System.Collections.Generic
#if FX_NO_ARRAY_KEY_SORT
// Mimic behavior of BCL QSort routine, used under the hood by various array sorting APIs
let qsort<'Key,'Value>(keys : 'Key[], values : 'Value[], start : int, last : int, comparer : IComparer<'Key>) =
let valuesExist =
match values with
| null -> false
| _ -> true
let swap (p1, p2) =
let tk = keys.[p1]
keys.[p1] <- keys.[p2]
keys.[p2] <- tk
if valuesExist then
let tv = values.[p1]
values.[p1] <- values.[p2]
values.[p2] <- tv
let partition (left, right, pivot) =
let value = keys.[pivot]
swap (pivot, right)
let mutable store = left
for i in left..(right - 1) do
if comparer.Compare(keys.[i],value) < 0 then
swap(i, store)
store <- store + 1
swap (store, right)
store
let rec qs (left, right) =
if left < right then
let pivot = left + (right-left)/2
let newpivot = partition(left,right,pivot)
qs(left,newpivot - 1)
qs(newpivot+1,right)
qs(start, last)
type System.Array with
static member Sort<'Key,'Value when 'Key : comparison>(keys : 'Key[], values : 'Value[], comparer : IComparer<'Key>) =
let valuesExist =
match values with
| null -> false
| _ -> true
match keys,values with
| null,_ -> raise (ArgumentNullException())
| _,_ when valuesExist && (keys.Length <> values.Length) -> raise (ArgumentException())
| _,_ ->
let comparer = match comparer with null -> LanguagePrimitives.FastGenericComparer<'Key> | _ -> comparer
qsort(keys, values, 0, keys.Length-1, comparer)
static member Sort<'Key,'Value when 'Key : comparison>(keys : 'Key[], values : 'Value[]) =
let valuesExist =
match values with
| null -> false
| _ -> true
match keys,values with
| null,_ -> raise (ArgumentNullException())
| _,_ when valuesExist && (keys.Length <> values.Length) -> raise (ArgumentException())
| _,_ ->
qsort(keys,values,0,keys.Length-1,LanguagePrimitives.FastGenericComparer<'Key>)
(*
static member Sort<'Key,'Value when 'Key : comparison>(keys : 'Key[], values : 'Value[], start : int, last : int) =
match keys with
| null -> raise (ArgumentNullException())
| _ ->
qsort(keys,values,start,last,LanguagePrimitives.FastGenericComparer<'Key>)
*)
static member Sort<'Key,'Value when 'Key : comparison>(keys : 'Key[], values : 'Value[], start : int, length : int, comparer : IComparer<'Key>) =
match keys with
| null -> raise (ArgumentNullException())
| _ ->
let comparer = match comparer with null -> LanguagePrimitives.FastGenericComparer<'Key> | _ -> comparer
qsort(keys,values,start,start+length-1,comparer)
#else
#endif
// The input parameter should be checked by callers if necessary
let inline zeroCreateUnchecked (count:int) =
(# "newarr !0" type ('T) count : 'T array #)
let inline init (count:int) (f: int -> 'T) =
if count < 0 then invalidArg "count" InputMustBeNonNegativeString
let arr = (zeroCreateUnchecked count : 'T array)
for i = 0 to count - 1 do
arr.[i] <- f i
arr
let permute indexMap (arr : _[]) =
let res = zeroCreateUnchecked arr.Length
let inv = zeroCreateUnchecked arr.Length
for i = 0 to arr.Length - 1 do
let j = indexMap i
if j < 0 || j >= arr.Length then invalidArg "indexMap" (SR.GetString(SR.notAPermutation))
res.[j] <- arr.[i]
inv.[j] <- 1uy
for i = 0 to arr.Length - 1 do
if inv.[i] <> 1uy then invalidArg "indexMap" (SR.GetString(SR.notAPermutation))
res
let unstableSortInPlaceBy (f: 'T -> 'U) (array : array<'T>) =
let len = array.Length
if len < 2 then ()
else
let keys = zeroCreateUnchecked array.Length
for i = 0 to array.Length - 1 do
keys.[i] <- f array.[i]
System.Array.Sort<_,_>(keys, array, LanguagePrimitives.FastGenericComparerCanBeNull<_>)
let unstableSortInPlace (array : array<'T>) =
let len = array.Length
if len < 2 then ()
else System.Array.Sort<_>(array, LanguagePrimitives.FastGenericComparerCanBeNull<_>)
let stableSortWithKeys (array:array<'T>) (keys:array<'Key>) =
// 'places' is an array or integers storing the permutation performed by the sort
let places = zeroCreateUnchecked array.Length
for i = 0 to array.Length - 1 do
places.[i] <- i
let cFast = LanguagePrimitives.FastGenericComparerCanBeNull<'Key>
System.Array.Sort<_,_>(keys, places, cFast)
// 'array2' is a copy of the original values
let array2 = (array.Clone() :?> array<'T>)
// 'c' is a comparer for the keys
let c = LanguagePrimitives.FastGenericComparer<'Key>
// Walk through any chunks where the keys are equal
let mutable i = 0
let len = array.Length
while i < len do
let mutable j = i
let ki = keys.[i]
while j < len && (j = i || c.Compare(ki, keys.[j]) = 0) do
j <- j + 1
// Copy the values into the result array and re-sort the chunk if needed by the original place indexes
for n = i to j - 1 do
array.[n] <- array2.[places.[n]]
if j - i >= 2 then
System.Array.Sort<_,_>(places, array, i, j-i, null)
i <- j
let stableSortInPlaceBy (f: 'T -> 'U) (array : array<'T>) =
let len = array.Length
if len < 2 then ()
else
// 'keys' is an array storing the projected keys
let keys = zeroCreateUnchecked array.Length
for i = 0 to array.Length - 1 do
keys.[i] <- f array.[i]
stableSortWithKeys array keys
let stableSortInPlace (array : array<'T>) =
let len = array.Length
if len < 2 then ()
else
let cFast = LanguagePrimitives.FastGenericComparerCanBeNull<'T>
match cFast with
| null ->
// An optimization for the cases where the keys and values coincide and do not have identity, e.g. are integers
// In this case an unstable sort is just as good as a stable sort (and faster)
System.Array.Sort<_,_>(array, null)
| _ ->
// 'keys' is an array storing the projected keys
let keys = (array.Clone() :?> array<'T>)
stableSortWithKeys array keys
let inline subUnchecked startIndex count (array : 'T[]) =
let res = zeroCreateUnchecked count : 'T[]
if count < 64 then
for i = 0 to count - 1 do
res.[i] <- array.[startIndex+i]
else
Array.Copy(array, startIndex, res, 0, count)
res