Skip to content

Commit d6c85ea

Browse files
committed
comparewith and concat
1 parent 4853293 commit d6c85ea

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/fsharp/FSharp.Core/array.fs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,21 @@ namespace Microsoft.FSharp.Collections
8686
[<CompiledName("CopyTo")>]
8787
let inline blit (source : 'T[]) (sourceIndex:int) (target: 'T[]) (targetIndex:int) (count:int) =
8888
Array.Copy(source, sourceIndex, target, targetIndex, count)
89-
90-
let rec concatAddLengths (arrs:'T[][]) i acc =
91-
if i >= arrs.Length then acc
92-
else concatAddLengths arrs (i+1) (acc + arrs.[i].Length)
93-
94-
let rec concatBlit (arrs:'T[][]) i j (tgt:'T[]) =
95-
if i < arrs.Length then
96-
let h = arrs.[i]
97-
let len = h.Length
98-
Array.Copy(h, 0, tgt, j, len)
99-
concatBlit arrs (i+1) (j+len) tgt
89+
90+
let concatArrays (arrs : 'T[][]) : 'T[] =
91+
let mutable acc = 0
92+
for h in arrs do
93+
acc <- acc + h.Length
94+
95+
let res = Array.zeroCreateUnchecked acc
10096

101-
let concatArrays (arrs : 'T[][]) =
102-
let res = Array.zeroCreateUnchecked (concatAddLengths arrs 0 0)
103-
concatBlit arrs 0 0 res
104-
res
97+
let mutable j = 0
98+
for i = 0 to arrs.Length-1 do
99+
let h = arrs.[i]
100+
let len = h.Length
101+
Array.Copy(h,0,res,j,len)
102+
j <- j + len
103+
res
105104

106105
[<CompiledName("Concat")>]
107106
let concat (arrays: seq<'T[]>) =
@@ -1001,22 +1000,26 @@ namespace Microsoft.FSharp.Collections
10011000
let inline compareWith (comparer:'T -> 'T -> int) (array1: 'T[]) (array2: 'T[]) =
10021001
checkNonNull "array1" array1
10031002
checkNonNull "array2" array2
1004-
1003+
10051004
let length1 = array1.Length
10061005
let length2 = array2.Length
1007-
let minLength = Operators.min length1 length2
1008-
1009-
let rec loop index =
1010-
if index = minLength then
1011-
if length1 = length2 then 0
1012-
elif length1 < length2 then -1
1013-
else 1
1014-
else
1015-
let result = comparer array1.[index] array2.[index]
1016-
if result <> 0 then result else
1017-
loop (index+1)
1006+
1007+
let mutable i = 0
1008+
let mutable result = 0
1009+
1010+
if length1 < length2 then
1011+
while i < array1.Length && result = 0 do
1012+
result <- comparer array1.[i] array2.[i]
1013+
i <- i + 1
1014+
else
1015+
while i < array2.Length && result = 0 do
1016+
result <- comparer array1.[i] array2.[i]
1017+
i <- i + 1
10181018

1019-
loop 0
1019+
if result <> 0 then result
1020+
elif length1 = length2 then 0
1021+
elif length1 < length2 then -1
1022+
else 1
10201023

10211024
[<CompiledName("GetSubArray")>]
10221025
let sub (array:'T[]) (startIndex:int) (count:int) =

0 commit comments

Comments
 (0)