Skip to content

Commit

Permalink
Merge pull request #21251 from JuliaLang/teh/non1
Browse files Browse the repository at this point in the history
More fixes for non-1 arrays
  • Loading branch information
timholy committed Apr 4, 2017
2 parents 18e864b + 4776d9a commit e1d6cdd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ julia> length(A)
length(t::AbstractArray) = (@_inline_meta; prod(size(t)))
_length(A::AbstractArray) = (@_inline_meta; prod(map(unsafe_length, indices(A)))) # circumvent missing size
_length(A) = (@_inline_meta; length(A))
endof(a::AbstractArray) = (@_inline_meta; length(a))
endof(a::AbstractArray) = (@_inline_meta; last(linearindices(a)))
first(a::AbstractArray) = a[first(eachindex(a))]

"""
Expand Down
18 changes: 11 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1040,26 +1040,30 @@ function =={T<:BitInteger}(a::Array{T,1}, b::Array{T,1})
:memcmp, Int32, (Ptr{T}, Ptr{T}, UInt), a, b, sizeof(T) * len)
end

function reverse(A::AbstractVector, s=1, n=length(A))
function reverse(A::AbstractVector, s=first(linearindices(A)), n=last(linearindices(A)))
B = similar(A)
for i = 1:s-1
for i = first(linearindices(A)):s-1
B[i] = A[i]
end
for i = s:n
B[i] = A[n+s-i]
end
for i = n+1:length(A)
for i = n+1:last(linearindices(A))
B[i] = A[i]
end
return B
end
reverseind(a::AbstractVector, i::Integer) = length(a) + 1 - i
function reverseind(a::AbstractVector, i::Integer)
li = linearindices(a)
first(li) + last(li) - i
end

function reverse!(v::AbstractVector, s=1, n=length(v))
function reverse!(v::AbstractVector, s=first(linearindices(v)), n=last(linearindices(v)))
liv = linearindices(v)
if n <= s # empty case; ok
elseif !(1 s endof(v))
elseif !(first(liv) s last(liv))
throw(BoundsError(v, s))
elseif !(1 n endof(v))
elseif !(first(liv) n last(liv))
throw(BoundsError(v, n))
end
r = n
Expand Down
10 changes: 10 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,18 @@ am = map(identity, a)

# other functions
v = OffsetArray(v0, (-3,))
@test endof(v) == 1
@test v v
@test indices(v') === (Base.OneTo(1),-2:1)
rv = reverse(v)
@test indices(rv) == indices(v)
@test rv[1] == v[-2]
@test rv[0] == v[-1]
@test rv[-1] == v[0]
@test rv[-2] == v[1]
cv = copy(v)
@test reverse!(cv) == rv

A = OffsetArray(rand(4,4), (-3,5))
@test A A
@test indices(A') === (6:9, -2:1)
Expand Down

0 comments on commit e1d6cdd

Please sign in to comment.