diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 02813fc716b5fd..4ee1efa595167c 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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) @@ -154,11 +158,13 @@ checkbounds(A::AbstractArray, I::AbstractVector{Bool}) = length(A) == length(I) checkbounds(A::AbstractArray, I::Union{Real,AbstractArray,Colon}) = (@_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) @@ -216,11 +222,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 @@ -230,18 +236,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 @@ -252,15 +259,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 @@ -269,7 +278,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 @@ -278,9 +287,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 @@ -290,15 +297,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] @@ -308,9 +322,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) @@ -326,9 +343,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) @@ -690,7 +710,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) @@ -722,7 +742,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) @@ -761,8 +781,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 @@ -777,7 +798,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] @@ -817,9 +839,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 @@ -853,13 +874,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] @@ -875,12 +896,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] @@ -907,7 +928,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) @@ -1125,7 +1146,10 @@ 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 @@ -1133,7 +1157,10 @@ function map(f, iters...) 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