Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

53 changes: 26 additions & 27 deletions src/fsharp/FSharp.Core/array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ namespace Microsoft.FSharp.Collections
else Some array.[array.Length-1]

[<CompiledName("Initialize")>]
let inline init count f = Array.init count f
let inline init count f = Array.init count f

[<CompiledName("ZeroCreate")>]
let zeroCreate count =
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
if count < 0 then invalidArgInputMustBeNonNegative "count" count
Array.zeroCreateUnchecked count

[<CompiledName("Create")>]
let create (count:int) (x:'T) =
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
if count < 0 then invalidArgInputMustBeNonNegative "count" count
let array: 'T[] = Array.zeroCreateUnchecked count
for i = 0 to Operators.Checked.(-) array.Length 1 do // use checked arithmetic here to satisfy FxCop
array.[i] <- x
Expand Down Expand Up @@ -110,7 +110,7 @@ namespace Microsoft.FSharp.Collections

[<CompiledName("Replicate")>]
let replicate count x =
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
if count < 0 then invalidArgInputMustBeNonNegative "count" count
let arr : 'T array = Array.zeroCreateUnchecked count
for i = 0 to arr.Length-1 do
arr.[i] <- x
Expand All @@ -128,7 +128,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("SplitAt")>]
let splitAt index (array:'T[]) =
checkNonNull "array" array
if index < 0 then invalidArg "index" (SR.GetString(SR.inputMustBeNonNegative))
if index < 0 then invalidArgInputMustBeNonNegative "index" index
if array.Length < index then raise <| InvalidOperationException (SR.GetString(SR.notEnoughElements))
if index = 0 then
let right = Array.subUnchecked 0 array.Length array
Expand All @@ -145,7 +145,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("Take")>]
let take count (array : 'T[]) =
checkNonNull "array" array
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
if count < 0 then invalidArgInputMustBeNonNegative "count" count
if count = 0 then
empty
else
Expand Down Expand Up @@ -276,7 +276,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
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])

Expand All @@ -298,7 +298,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
let res = Array.zeroCreateUnchecked array1.Length
for i = 0 to res.Length-1 do
res.[i] <- f.Invoke(array1.[i], array2.[i])
Expand All @@ -311,7 +311,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array3" array3
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
let len1 = array1.Length
if not (len1 = array2.Length && len1 = array3.Length) then invalidArg "" (SR.GetString(SR.arraysHadDifferentLengths))
if len1 <> array2.Length || len1 <> array3.Length then invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length

let res = Array.zeroCreateUnchecked len1
for i = 0 to res.Length-1 do
Expand All @@ -323,7 +323,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
let res = Array.zeroCreateUnchecked array1.Length
for i = 0 to res.Length-1 do
res.[i] <- f.Invoke(i,array1.[i], array2.[i])
Expand All @@ -341,7 +341,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array1" array1
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths));
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])

Expand Down Expand Up @@ -387,7 +387,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
let len1 = array1.Length
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
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

Expand All @@ -404,7 +404,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
let len1 = array1.Length
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
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

Expand Down Expand Up @@ -579,7 +579,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("Skip")>]
let skip count (array:'T[]) =
checkNonNull "array" array
if count > array.Length then invalidArg "count" (SR.GetString(SR.outOfRange))
if count > array.Length then invalidArgOutOfRange "count" count "array.Length" array.Length
if count = array.Length then
empty
else
Expand Down Expand Up @@ -619,7 +619,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("Windowed")>]
let windowed windowSize (array:'T[]) =
checkNonNull "array" array
if windowSize <= 0 then invalidArg "windowSize" (SR.GetString(SR.inputMustBePositive))
if windowSize <= 0 then invalidArgInputMustBePositive "windowSize" windowSize
let len = array.Length
if windowSize > len then
empty
Expand All @@ -632,7 +632,7 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("ChunkBySize")>]
let chunkBySize chunkSize (array:'T[]) =
checkNonNull "array" array
if chunkSize <= 0 then invalidArg "chunkSize" (SR.GetString(SR.inputMustBePositive))
if chunkSize <= 0 then invalidArgInputMustBePositive "chunkSize" chunkSize
let len = array.Length
if len = 0 then
empty
Expand All @@ -650,15 +650,15 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("SplitInto")>]
let splitInto count (array:_[]) =
checkNonNull "array" array
if count <= 0 then invalidArg "count" (SR.GetString(SR.inputMustBePositive))
if count <= 0 then invalidArgInputMustBePositive "count" count
Array.splitInto count array

[<CompiledName("Zip")>]
let zip (array1: _[]) (array2: _[]) =
checkNonNull "array1" array1
checkNonNull "array2" array2
let len1 = array1.Length
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
if len1 <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
let res = Array.zeroCreateUnchecked len1
for i = 0 to res.Length-1 do
res.[i] <- (array1.[i],array2.[i])
Expand All @@ -670,8 +670,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array2" array2
checkNonNull "array3" array3
let len1 = array1.Length
if len1 <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
if len1 <> array3.Length then invalidArg "array3" (SR.GetString(SR.arraysHadDifferentLengths))
if len1 <> array2.Length || len1 <> array3.Length then invalidArg3ArraysDifferent "array1" "array2" "array3" len1 array2.Length array3.Length
let res = Array.zeroCreateUnchecked len1
for i = 0 to res.Length-1 do
res.[i] <- (array1.[i],array2.[i],array3.[i])
Expand Down Expand Up @@ -763,7 +762,7 @@ namespace Microsoft.FSharp.Collections
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
let mutable res = acc
let len = array1.Length
if len <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
if len <> array2.Length then invalidArgDifferentArrayLength "array1" len "array2" array2.Length
for i = len-1 downto 0 do
res <- f.Invoke(array1.[i],array2.[i],res)
res
Expand All @@ -774,7 +773,7 @@ namespace Microsoft.FSharp.Collections
checkNonNull "array2" array2
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt(f)
let mutable state = acc
if array1.Length <> array2.Length then invalidArg "array2" (SR.GetString(SR.arraysHadDifferentLengths))
if array1.Length <> array2.Length then invalidArgDifferentArrayLength "array1" array1.Length "array2" array2.Length
for i = 0 to array1.Length-1 do
state <- f.Invoke(state,array1.[i],array2.[i])
state
Expand Down Expand Up @@ -1041,9 +1040,9 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("GetSubArray")>]
let sub (array:'T[]) (startIndex:int) (count:int) =
checkNonNull "array" array
if startIndex < 0 then invalidArg "startIndex" (SR.GetString(SR.inputMustBeNonNegative))
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
if startIndex + count > array.Length then invalidArg "count" (SR.GetString(SR.outOfRange))
if startIndex < 0 then invalidArgInputMustBeNonNegative "startIndex" startIndex
if count < 0 then invalidArgInputMustBeNonNegative "count" count
if startIndex + count > array.Length then invalidArgOutOfRange "count" count "array.Length" array.Length
Array.subUnchecked startIndex count array

[<CompiledName("Item")>]
Expand All @@ -1067,8 +1066,8 @@ namespace Microsoft.FSharp.Collections
[<CompiledName("Fill")>]
let fill (target:'T[]) (targetIndex:int) (count:int) (x:'T) =
checkNonNull "target" target
if targetIndex < 0 then invalidArg "targetIndex" (SR.GetString(SR.inputMustBeNonNegative))
if count < 0 then invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
if targetIndex < 0 then invalidArgInputMustBeNonNegative "targetIndex" targetIndex
if count < 0 then invalidArgInputMustBeNonNegative "count" count
for i = targetIndex to targetIndex + count - 1 do
target.[i] <- x

Expand Down
32 changes: 21 additions & 11 deletions src/fsharp/FSharp.Core/array2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace Microsoft.FSharp.Collections

[<CompiledName("ZeroCreate")>]
let zeroCreate (n:int) (m:int) =
if n < 0 then invalidArg "n" (SR.GetString(SR.inputMustBeNonNegative))
if m < 0 then invalidArg "m" (SR.GetString(SR.inputMustBeNonNegative))
if n < 0 then invalidArgInputMustBeNonNegative "length1" n
if m < 0 then invalidArgInputMustBeNonNegative "length2" m
(# "newarr.multi 2 !0" type ('T) n m : 'T[,] #)

[<CompiledName("ZeroCreateBased")>]
Expand Down Expand Up @@ -130,17 +130,27 @@ namespace Microsoft.FSharp.Collections
init (length1 array) (length2 array) (fun i j -> array.[b1+i,b2+j])

[<CompiledName("CopyTo")>]
let blit (source : 'T[,]) sourceIndex1 sourceIndex2 (target : 'T[,]) targetIndex1 targetIndex2 count1 count2 =
let blit (source : 'T[,]) sourceIndex1 sourceIndex2 (target: 'T[,]) targetIndex1 targetIndex2 count1 count2 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gather that error message will now report bound per dimensions when there is a mismatch, should tests be added to assert this behaviour does what is intended wrt. new error messages?

checkNonNull "source" source
checkNonNull "target" target
if sourceIndex1 < source.GetLowerBound(0) then invalidArg "sourceIndex1" (SR.GetString(SR.outOfRange))
if sourceIndex2 < source.GetLowerBound(1) then invalidArg "sourceIndex2" (SR.GetString(SR.outOfRange))
if targetIndex1 < target.GetLowerBound(0) then invalidArg "targetIndex1" (SR.GetString(SR.outOfRange))
if targetIndex2 < target.GetLowerBound(1) then invalidArg "targetIndex2" (SR.GetString(SR.outOfRange))
if sourceIndex1 + count1 > (length1 source) + source.GetLowerBound(0) then invalidArg "count1" (SR.GetString(SR.outOfRange))
if sourceIndex2 + count2 > (length2 source) + source.GetLowerBound(1) then invalidArg "count2" (SR.GetString(SR.outOfRange))
if targetIndex1 + count1 > (length1 target) + target.GetLowerBound(0) then invalidArg "count1" (SR.GetString(SR.outOfRange))
if targetIndex2 + count2 > (length2 target) + target.GetLowerBound(1) then invalidArg "count2" (SR.GetString(SR.outOfRange))

let sourceX0, sourceY0 = source.GetLowerBound 0 , source.GetLowerBound 1
let sourceXN, sourceYN = (length1 source) + sourceX0, (length2 source) + sourceY0
let targetX0, targetY0 = target.GetLowerBound 0 , target.GetLowerBound 1
let targetXN, targetYN = (length1 target) + targetX0, (length2 target) + targetY0

if sourceIndex1 < sourceX0 then invalidArgOutOfRange "sourceIndex1" sourceIndex1 "source axis-0 lower bound" sourceX0
if sourceIndex2 < sourceY0 then invalidArgOutOfRange "sourceIndex2" sourceIndex2 "source axis-1 lower bound" sourceY0
if targetIndex1 < targetX0 then invalidArgOutOfRange "targetIndex1" targetIndex1 "target axis-0 lower bound" targetX0
if targetIndex2 < targetY0 then invalidArgOutOfRange "targetIndex2" targetIndex2 "target axis-1 lower bound" targetY0
if sourceIndex1 + count1 > sourceXN then
invalidArgOutOfRange "count1" count1 ("source axis-0 end index = " + string(sourceIndex1+count1) + " source axis-0 upper bound") sourceXN
if sourceIndex2 + count2 > sourceYN then
invalidArgOutOfRange "count2" count2 ("source axis-1 end index = " + string(sourceIndex2+count2) + " source axis-1 upper bound") sourceYN
if targetIndex1 + count1 > targetXN then
invalidArgOutOfRange "count1" count1 ("target axis-0 end index = " + string(targetIndex1+count1) + " target axis-0 upper bound") targetXN
if targetIndex2 + count2 > targetYN then
invalidArgOutOfRange "count2" count2 ("target axis-1 end index = " + string(targetIndex2+count2) + " target axis-1 upper bound") targetYN

for i = 0 to count1 - 1 do
for j = 0 to count2 - 1 do
Expand Down
18 changes: 9 additions & 9 deletions src/fsharp/FSharp.Core/array3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ namespace Microsoft.FSharp.Collections

[<CompiledName("ZeroCreate")>]
let zeroCreate (n1:int) (n2:int) (n3:int) =
if n1 < 0 then invalidArg "n1" (SR.GetString(SR.inputMustBeNonNegative))
if n2 < 0 then invalidArg "n2" (SR.GetString(SR.inputMustBeNonNegative))
if n3 < 0 then invalidArg "n3" (SR.GetString(SR.inputMustBeNonNegative))
if n1 < 0 then invalidArgInputMustBeNonNegative "n1" n1
if n2 < 0 then invalidArgInputMustBeNonNegative "n2" n2
if n3 < 0 then invalidArgInputMustBeNonNegative "n3" n3
(# "newarr.multi 3 !0" type ('T) n1 n2 n3 : 'T[,,] #)

[<CompiledName("Create")>]
Expand Down Expand Up @@ -127,10 +127,10 @@ namespace Microsoft.FSharp.Collections

[<CompiledName("ZeroCreate")>]
let zeroCreate (n1:int) (n2:int) (n3:int) (n4:int) =
if n1 < 0 then invalidArg "n1" (SR.GetString(SR.inputMustBeNonNegative))
if n2 < 0 then invalidArg "n2" (SR.GetString(SR.inputMustBeNonNegative))
if n3 < 0 then invalidArg "n3" (SR.GetString(SR.inputMustBeNonNegative))
if n4 < 0 then invalidArg "n4" (SR.GetString(SR.inputMustBeNonNegative))
if n1 < 0 then invalidArgInputMustBeNonNegative "n1" n1
if n2 < 0 then invalidArgInputMustBeNonNegative "n2" n2
if n3 < 0 then invalidArgInputMustBeNonNegative "n3" n3
if n4 < 0 then invalidArgInputMustBeNonNegative "n4" n4
(# "newarr.multi 4 !0" type ('T) n1 n2 n3 n4 : 'T[,,,] #)

[<CompiledName("Create")>]
Expand All @@ -156,7 +156,7 @@ namespace Microsoft.FSharp.Collections


[<CompiledName("Get")>]
let get (array: 'T[,,,]) n1 n2 n3 n4 = array.[n1,n2,n3,n4]
let get (array: 'T[,,,]) n1 n2 n3 n4 = array.[n1,n2,n3,n4]

[<CompiledName("Set")>]
let set (array: 'T[,,,]) n1 n2 n3 n4 x = array.[n1,n2,n3,n4] <- x
let set (array: 'T[,,,]) n1 n2 n3 n4 x = array.[n1,n2,n3,n4] <- x
Loading