Skip to content

Commit

Permalink
Address #11792 in AbstractArray.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed Jun 21, 2015
1 parent c6ceac2 commit 54e7713
Showing 1 changed file with 62 additions and 20 deletions.
82 changes: 62 additions & 20 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,15 @@ 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))
if !(_checkbounds(size(A,1), I) && _checkbounds(size(A,2), J))
throw_boundserror(A, (I, J))
end
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))
if !(_checkbounds(size(A,1), I) && _checkbounds(trailingsize(A,Val{2}), J))
throw_boundserror(A, (I, J))
end
end
@generated function checkbounds(A::AbstractArray, I::Union{Real,AbstractArray,Colon}...)
meta = Expr(:meta, :inline)
Expand Down Expand Up @@ -213,11 +217,15 @@ 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())
if doffs < 1
throw(BoundsError())
end
st = start(src)
i, dmax = doffs, length(dest)
@inbounds while !done(src, st)
i > dmax && throw(BoundsError())
if i > dmax
throw(BoundsError())
end
val, st = next(src, st)
dest[i] = val
i += 1
Expand All @@ -231,14 +239,20 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer)
end
st = start(src)
for j = 1:(soffs-1)
done(src, st) && throw(BoundsError())
if done(src, st)
throw(BoundsError())
end
_, st = next(src, st)
end
dn = done(src, st)
dn && throw(BoundsError())
if dn
throw(BoundsError())
end
i, dmax = doffs, length(dest)
@inbounds while !dn
i > dmax && throw(BoundsError())
if i > dmax
throw(BoundsError())
end
val, st = next(src, st)
dest[i] = val
i += 1
Expand All @@ -249,15 +263,21 @@ 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 && return dest
if n < 0
throw(BoundsError())
end
if n == 0
return dest
end
dmax = doffs + n - 1
if (dmax > length(dest)) | (doffs < 1) | (soffs < 1)
throw(BoundsError())
end
st = start(src)
for j = 1:(soffs-1)
done(src, st) && throw(BoundsError())
if done(src, st)
throw(BoundsError())
end
_, st = next(src, st)
end
i = doffs
Expand All @@ -266,7 +286,9 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Inte
dest[i] = val
i += 1
end
i <= dmax && throw(BoundsError())
if i <= dmax
throw(BoundsError())
end
return dest
end

Expand All @@ -288,12 +310,18 @@ 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())
if soffs > length(src)
throw(BoundsError())
end
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())
n == 0 && return dest
if n < 0
throw(BoundsError())
end
if n == 0
return dest
end
if soffs+n-1 > length(src) || doffs+n-1 > length(dest) || doffs < 1 || soffs < 1
throw(BoundsError())
end
Expand Down Expand Up @@ -990,7 +1018,9 @@ end

@generated function ind2sub{N}(dims::NTuple{N,Integer}, ind::Integer)
meta = Expr(:meta,:inline)
N==0 && return :($meta; ind==1 ? () : throw(BoundsError()))
if N==0
return :($meta; ind==1 ? () : throw(BoundsError()))
end
exprs = Expr[:(ind = ind-1)]
for i = 1:N-1
push!(exprs,:(ind2 = div(ind,dims[$i])))
Expand Down Expand Up @@ -1111,15 +1141,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 Expand Up @@ -1347,9 +1383,13 @@ function hash(a::AbstractArray, h::UInt)
h += hash(size(a))

state = start(a)
done(a, state) && return h
if done(a, state)
return h
end
x2, state = next(a, state)
done(a, state) && return hash(x2, h)
if done(a, state)
return hash(x2, h)
end

x1 = x2
while !done(a, state)
Expand All @@ -1369,6 +1409,8 @@ function hash(a::AbstractArray, h::UInt)
end
h = hash(x1, h)
end
!isequal(x2, x1) && (h = hash(x2, h))
if !isequal(x2, x1)
h = hash(x2, h)
end
return h
end

0 comments on commit 54e7713

Please sign in to comment.