Skip to content

Commit

Permalink
Improved many error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed Jun 21, 2015
1 parent 54e7713 commit e266eaa
Showing 1 changed file with 63 additions and 33 deletions.
96 changes: 63 additions & 33 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T))
length(t::AbstractArray) = prod(size(t))::Int
endof(a::AbstractArray) = length(a)
first(a::AbstractArray) = a[1]
first(a) = (s = start(a); done(a, s) ? throw(ArgumentError("collection must be non-empty")) : next(a, s)[1])
function first(arr)
str = start(arr)
if done(arr, str)
throw(ArgumentError("collection must be non-empty"))
end
next(arr, str)[1]
end
last(a) = a[end]

function stride(a::AbstractArray, i::Integer)
Expand Down Expand Up @@ -218,13 +224,13 @@ end
# if src is not an AbstractArray, moving to the offset might be O(n)
function copy!(dest::AbstractArray, doffs::Integer, src)
if doffs < 1
throw(BoundsError())
throw_boundserror(dest, doffs)
end
st = start(src)
i, dmax = doffs, length(dest)
@inbounds while !done(src, st)
if i > dmax
throw(BoundsError())
throw_boundserror(dest, i)
end
val, st = next(src, st)
dest[i] = val
Expand All @@ -234,24 +240,27 @@ function copy!(dest::AbstractArray, doffs::Integer, src)
end

function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer)
if (doffs < 1) | (soffs < 1)
throw(BoundsError())
if doffs < 1
throw_boundserror(dest, doffs)
end
if soffs < 1
throw_boundserror(src, soffs)
end
st = start(src)
for j = 1:(soffs-1)
if done(src, st)
throw(BoundsError())
throw_boundserror(src, j)
end
_, st = next(src, st)
end
dn = done(src, st)
if dn
throw(BoundsError())
throw(BoundsError(src))
end
i, dmax = doffs, length(dest)
@inbounds while !dn
if i > dmax
throw(BoundsError())
throw_boundserror(dest, i)
end
val, st = next(src, st)
dest[i] = val
Expand All @@ -264,19 +273,25 @@ end
# this method must be separate from the above since src might not have a length
function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Integer)
if n < 0
throw(BoundsError())
throw_boundserror(dest, n)
end
if n == 0
return dest
end
dmax = doffs + n - 1
if (dmax > length(dest)) | (doffs < 1) | (soffs < 1)
throw(BoundsError())
if dmax > length(dest)
throw_boundserror(dest, dmax)
end
if doffs < 1
throw_boundserror(dest, doffs)
end
if soffs < 1
throw_boundserror(src, soffs)
end
st = start(src)
for j = 1:(soffs-1)
if done(src, st)
throw(BoundsError())
throw_boundserror(src, j)
end
_, st = next(src, st)
end
Expand All @@ -287,7 +302,7 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Inte
i += 1
end
if i <= dmax
throw(BoundsError())
throw_boundserror(dest, i)
end
return dest
end
Expand All @@ -298,7 +313,7 @@ end
function copy!(dest::AbstractArray, src::AbstractArray)
n = length(src)
if n > length(dest)
throw(BoundsError())
throw_boundserror(dest, n)
end
@inbounds for i = 1:n
dest[i] = src[i]
Expand All @@ -311,19 +326,28 @@ function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray)
end
function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray, soffs::Integer)
if soffs > length(src)
throw(BoundsError())
throw_boundserror(src, soffs)
end
copy!(dest, doffs, src, soffs, length(src)-soffs+1)
end
function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray, soffs::Integer, n::Integer)
if n < 0
throw(BoundsError())
throw_boundserror(src, n)
end
if n == 0
return dest
end
if soffs+n-1 > length(src) || doffs+n-1 > length(dest) || doffs < 1 || soffs < 1
throw(BoundsError())
if soffs+n-1 > length(src)
throw_boundserror(src, soffs+n-1)
end
if doffs+n-1 > length(dest)
throw_boundserror(dest, doffs+n-1)
end
if doffs < 1
throw_boundserror(dest, doffs)
end
if soffs < 1
throw_boundserror(src, soffs)
end
@inbounds for i = 0:(n-1)
dest[doffs+i] = src[soffs+i]
Expand All @@ -334,8 +358,11 @@ end
copy(a::AbstractArray) = copy!(similar(a), a)

function copy!{R,S}(B::AbstractVecOrMat{R}, ir_dest::Range{Int}, jr_dest::Range{Int}, A::AbstractVecOrMat{S}, ir_src::Range{Int}, jr_src::Range{Int})
if length(ir_dest) != length(ir_src) || length(jr_dest) != length(jr_src)
throw(ArgumentError("source and destination must have same size"))
if length(ir_dest) != length(ir_src)
throw(ArgumentError("source ($(length(ir_src))) and destination ($(length(ir_dest))) must have same size"))
end
if length(jr_dest) != length(jr_src)
throw(ArgumentError("source ($(length(jr_src))) and destination ($(length(jr_dest))) must have same size"))
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
Expand All @@ -352,8 +379,11 @@ function copy!{R,S}(B::AbstractVecOrMat{R}, ir_dest::Range{Int}, jr_dest::Range{
end

function copy_transpose!{R,S}(B::AbstractVecOrMat{R}, ir_dest::Range{Int}, jr_dest::Range{Int}, A::AbstractVecOrMat{S}, ir_src::Range{Int}, jr_src::Range{Int})
if length(ir_dest) != length(jr_src) || length(jr_dest) != length(ir_src)
throw(ArgumentError("source and destination must have same size"))
if length(ir_dest) != length(jr_src)
throw(ArgumentError("source ($(length(ir_src))) and destination ($(length(ir_dest))) must have same size"))
end
if length(jr_dest) != length(ir_src)
throw(ArgumentError("source ($(length(jr_src))) and destination ($(length(jr_dest))) must have same size"))
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
Expand Down Expand Up @@ -704,7 +734,7 @@ function hcat{T}(A::AbstractVecOrMat{T}...)
for j = 1:nargs
Aj = A[j]
if size(Aj, 1) != nrows
throw(ArgumentError("number of rows must match"))
throw(ArgumentError("number of rows of A[$j] ($(size(Aj,1))) must match $nrows"))
end
dense &= isa(Aj,Array)
nd = ndims(Aj)
Expand Down Expand Up @@ -736,7 +766,7 @@ function vcat{T}(A::AbstractMatrix{T}...)
ncols = size(A[1], 2)
for j = 2:nargs
if size(A[j], 2) != ncols
throw(ArgumentError("number of columns must match"))
throw(ArgumentError("number of columns of A[$j] ($(size(A[j],2))) must match $ncols"))
end
end
B = similar(full(A[1]), nrows, ncols)
Expand Down Expand Up @@ -775,11 +805,11 @@ function cat_t(catdims, typeC::Type, X...)
for i = 2:nargs
for d = 1:ndimsC
currentdim = (d <= ndimsX[i] ? size(X[i],d) : 1)
if dims2cat[d]==0
dimsC[d] == currentdim || throw(DimensionMismatch("mismatch in dimension $(d)"))
else
if dims2cat[d] != 0
dimsC[d] += currentdim
catsizes[i,dims2cat[d]] = currentdim
elseif dimsC[d] != currentdim
throw(DimensionMismatch("mismatch in dimension $(d)"))
end
end
end
Expand Down Expand Up @@ -832,7 +862,7 @@ function hvcat(nbc::Integer, as...)
# nbc = # of block columns
n = length(as)
if mod(n,nbc) != 0
throw(ArgumentError("all rows must have the same number of block columns"))
throw(ArgumentError("all rows must have the same number of block columns ($nbc)"))
end
nbr = div(n,nbc)
hvcat(ntuple(i->nbc, nbr), as...)
Expand Down Expand Up @@ -867,13 +897,13 @@ function hvcat{T}(rows::Tuple{Vararg{Int}}, as::AbstractMatrix{T}...)
throw(ArgumentError("mismatched height in block row $(i)"))
end
if c-1+szj > nc
throw(ArgumentError("block row $(i) has mismatched number of columns"))
throw(ArgumentError("block row $(i) has mismatched number of columns: $(c-1+szj) $nc"))
end
out[r:r-1+szi, c:c-1+szj] = Aj
c += szj
end
if c != nc+1
throw(ArgumentError("block row $(i) has mismatched number of columns"))
throw(ArgumentError("block row $(i) has mismatched number of columns $(c-1) $nc"))
end
r += szi
a += rows[i]
Expand All @@ -889,12 +919,12 @@ function hvcat{T<:Number}(rows::Tuple{Vararg{Int}}, xs::T...)

a = Array(T, nr, nc)
if length(a) != length(xs)
throw(ArgumentError("argument count does not match specified shape"))
throw(ArgumentError("argument count does not match specified shape $(length(a)) != $(length(xs))"))
end
k = 1
@inbounds for i=1:nr
if nc != rows[i]
throw(ArgumentError("row $(i) has mismatched number of columns"))
throw(ArgumentError("row $(i) has mismatched number of columns $(rows[i]) != $nc"))
end
for j=1:nc
a[i,j] = xs[k]
Expand All @@ -921,7 +951,7 @@ function typed_hvcat(T::Type, rows::Tuple{Vararg{Int}}, xs::Number...)
nc = rows[1]
for i = 2:nr
if nc != rows[i]
throw(ArgumentError("row $(i) has mismatched number of columns"))
throw(ArgumentError("row $(i) has mismatched number of columns $(rows[i]) != $nc"))
end
end
len = length(xs)
Expand Down

0 comments on commit e266eaa

Please sign in to comment.