Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed Jun 22, 2015
1 parent fc604d1 commit 0775d2d
Showing 1 changed file with 66 additions and 39 deletions.
105 changes: 66 additions & 39 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ 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)
done(arr, str) && throw(ArgumentError("collection must be non-empty"))
next(arr, str)[1]
end
last(a) = a[end]

function stride(a::AbstractArray, i::Integer)
Expand Down Expand Up @@ -153,11 +157,13 @@ checkbounds(A::AbstractArray, I::AbstractVector{Bool}) = length(A) == length(I)
checkbounds(A::AbstractArray, I) = (@_inline_meta; _checkbounds(length(A), I) || throw_boundserror(A, I))
function checkbounds(A::AbstractMatrix, I::Union{Real,AbstractArray,Colon}, J::Union{Real,AbstractArray,Colon})
@_inline_meta
(_checkbounds(size(A,1), I) && _checkbounds(size(A,2), J)) || throw_boundserror(A, (I, J))
(_checkbounds(size(A,1), I) && _checkbounds(size(A,2), J)) ||
throw_boundserror(A, (I, J))
end
function checkbounds(A::AbstractArray, I::Union{Real,AbstractArray,Colon}, J::Union{Real,AbstractArray,Colon})
@_inline_meta
(_checkbounds(size(A,1), I) && _checkbounds(trailingsize(A,Val{2}), J)) || throw_boundserror(A, (I, J))
(_checkbounds(size(A,1), I) && _checkbounds(trailingsize(A,Val{2}), J)) ||
throw_boundserror(A, (I, J))
end
@generated function checkbounds(A::AbstractArray, I::Union{Real,AbstractArray,Colon}...)
meta = Expr(:meta, :inline)
Expand Down Expand Up @@ -213,11 +219,11 @@ end

# if src is not an AbstractArray, moving to the offset might be O(n)
function copy!(dest::AbstractArray, doffs::Integer, src)
doffs < 1 && throw(BoundsError())
doffs < 1 && throw(BoundsError(dest, doffs))
st = start(src)
i, dmax = doffs, length(dest)
@inbounds while !done(src, st)
i > dmax && throw(BoundsError())
i > dmax && throw(BoundsError(dest, i))
val, st = next(src, st)
dest[i] = val
i += 1
Expand All @@ -227,18 +233,19 @@ end

function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer)
if (doffs < 1) | (soffs < 1)
throw(BoundsError())
doffs < 1 && throw(BoundsError(dest, doffs))
throw(BoundsError(src, soffs))
end
st = start(src)
for j = 1:(soffs-1)
done(src, st) && throw(BoundsError())
done(src, st) && throw(BoundsError(src, j))
_, st = next(src, st)
end
dn = done(src, st)
dn && throw(BoundsError())
dn && throw(BoundsError(src))
i, dmax = doffs, length(dest)
@inbounds while !dn
i > dmax && throw(BoundsError())
i > dmax && throw(BoundsError(dest, i))
val, st = next(src, st)
dest[i] = val
i += 1
Expand All @@ -249,15 +256,17 @@ 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)
n < 0 && throw(BoundsError())
n < 0 && throw(BoundsError(dest, n))
n == 0 && return dest
dmax = doffs + n - 1
if (dmax > length(dest)) | (doffs < 1) | (soffs < 1)
throw(BoundsError())
dmax > length(dest) && throw(BoundsError(dest, dmax))
doffs < 1 && throw(BoundsError(dest, doffs))
throw(BoundsError(src, soffs))
end
st = start(src)
for j = 1:(soffs-1)
done(src, st) && throw(BoundsError())
done(src, st) && throw(BoundsError(src, j))
_, st = next(src, st)
end
i = doffs
Expand All @@ -266,7 +275,7 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Inte
dest[i] = val
i += 1
end
i <= dmax && throw(BoundsError())
i <= dmax && throw(BoundsError(dest, i))
return dest
end

Expand All @@ -275,9 +284,7 @@ end

function copy!(dest::AbstractArray, src::AbstractArray)
n = length(src)
if n > length(dest)
throw(BoundsError())
end
n > length(dest) && throw(BoundsError(dest, n))
@inbounds for i = 1:n
dest[i] = src[i]
end
Expand All @@ -287,15 +294,22 @@ end
function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray)
copy!(dest, doffs, src, 1, length(src))
end

function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray, soffs::Integer)
soffs > length(src) && throw(BoundsError())
soffs > length(src) && throw(BoundsError(src, soffs))
copy!(dest, doffs, src, soffs, length(src)-soffs+1)
end
function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray, soffs::Integer, n::Integer)
n < 0 && throw(BoundsError())

function copy!(dest::AbstractArray, doffs::Integer,
src::AbstractArray, soffs::Integer,
n::Integer)
n < 0 && throw(BoundsError(src, n))
n == 0 && return dest
if soffs+n-1 > length(src) || doffs+n-1 > length(dest) || doffs < 1 || soffs < 1
throw(BoundsError())
soffs+n-1 > length(src) && throw(BoundsError(src, soffs+n-1))
doffs+n-1 > length(dest) && throw(BoundsError(dest, doffs+n-1))
doffs < 1 && throw(BoundsError(dest, doffs))
throw(BoundsError(src, soffs))
end
@inbounds for i = 0:(n-1)
dest[doffs+i] = src[soffs+i]
Expand All @@ -305,9 +319,12 @@ 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})
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"))
length(ir_dest) != length(ir_src) &&
throw(ArgumentError("source ($(length(ir_src))) and destination ($(length(ir_dest))) must have same size"))
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 @@ -323,9 +340,12 @@ function copy!{R,S}(B::AbstractVecOrMat{R}, ir_dest::Range{Int}, jr_dest::Range{
return B
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})
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"))
length(ir_dest) != length(jr_src) &&
throw(ArgumentError("source ($(length(ir_src))) and destination ($(length(ir_dest))) must have same size"))
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 @@ -676,7 +696,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 @@ -708,7 +728,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 @@ -747,8 +767,9 @@ 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)"))
if dims2cat[d] == 0
dimsC[d] == currentdim ||
throw(DimensionMismatch("mismatch in dimension $(d)"))
else
dimsC[d] += currentdim
catsizes[i,dims2cat[d]] = currentdim
Expand All @@ -763,7 +784,8 @@ function cat_t(catdims, typeC::Type, X...)

offsets = zeros(Int,length(catdims))
for i=1:nargs
cat_one = [ dims2cat[d]==0 ? (1:dimsC[d]) : (offsets[dims2cat[d]]+(1:catsizes[i,dims2cat[d]])) for d=1:ndimsC]
cat_one = [ dims2cat[d] == 0 ? (1:dimsC[d]) : (offsets[dims2cat[d]]+(1:catsizes[i,dims2cat[d]]))
for d=1:ndimsC ]
C[cat_one...] = X[i]
for k = 1:length(catdims)
offsets[k] += catsizes[i,k]
Expand Down Expand Up @@ -803,9 +825,8 @@ typed_hcat(T::Type, A::AbstractArray...) = cat_t(2, T, A...)
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"))
end
mod(n,nbc) != 0 &&
throw(ArgumentError("all rows must have the same number of block columns ($nbc)"))
nbr = div(n,nbc)
hvcat(ntuple(i->nbc, nbr), as...)
end
Expand Down Expand Up @@ -839,13 +860,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 @@ -861,12 +882,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 @@ -893,7 +914,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 Expand Up @@ -1111,15 +1132,21 @@ function map(f, iters...)
nxtvals = cell(len)
cont = true
for idx in 1:len
done(iters[idx], states[idx]) && (cont = false; break)
if done(iters[idx], states[idx])
cont = false
break
end
end
while cont
for idx in 1:len
nxtvals[idx],states[idx] = next(iters[idx], states[idx])
end
push!(result, f(nxtvals...))
for idx in 1:len
done(iters[idx], states[idx]) && (cont = false; break)
if done(iters[idx], states[idx])
cont = false
break
end
end
end
result
Expand Down

0 comments on commit 0775d2d

Please sign in to comment.