Skip to content

Commit e1d6cdd

Browse files
authored
Merge pull request #21251 from JuliaLang/teh/non1
More fixes for non-1 arrays
2 parents 18e864b + 4776d9a commit e1d6cdd

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

base/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ julia> length(A)
124124
length(t::AbstractArray) = (@_inline_meta; prod(size(t)))
125125
_length(A::AbstractArray) = (@_inline_meta; prod(map(unsafe_length, indices(A)))) # circumvent missing size
126126
_length(A) = (@_inline_meta; length(A))
127-
endof(a::AbstractArray) = (@_inline_meta; length(a))
127+
endof(a::AbstractArray) = (@_inline_meta; last(linearindices(a)))
128128
first(a::AbstractArray) = a[first(eachindex(a))]
129129

130130
"""

base/array.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,26 +1040,30 @@ function =={T<:BitInteger}(a::Array{T,1}, b::Array{T,1})
10401040
:memcmp, Int32, (Ptr{T}, Ptr{T}, UInt), a, b, sizeof(T) * len)
10411041
end
10421042

1043-
function reverse(A::AbstractVector, s=1, n=length(A))
1043+
function reverse(A::AbstractVector, s=first(linearindices(A)), n=last(linearindices(A)))
10441044
B = similar(A)
1045-
for i = 1:s-1
1045+
for i = first(linearindices(A)):s-1
10461046
B[i] = A[i]
10471047
end
10481048
for i = s:n
10491049
B[i] = A[n+s-i]
10501050
end
1051-
for i = n+1:length(A)
1051+
for i = n+1:last(linearindices(A))
10521052
B[i] = A[i]
10531053
end
10541054
return B
10551055
end
1056-
reverseind(a::AbstractVector, i::Integer) = length(a) + 1 - i
1056+
function reverseind(a::AbstractVector, i::Integer)
1057+
li = linearindices(a)
1058+
first(li) + last(li) - i
1059+
end
10571060

1058-
function reverse!(v::AbstractVector, s=1, n=length(v))
1061+
function reverse!(v::AbstractVector, s=first(linearindices(v)), n=last(linearindices(v)))
1062+
liv = linearindices(v)
10591063
if n <= s # empty case; ok
1060-
elseif !(1 s endof(v))
1064+
elseif !(first(liv) s last(liv))
10611065
throw(BoundsError(v, s))
1062-
elseif !(1 n endof(v))
1066+
elseif !(first(liv) n last(liv))
10631067
throw(BoundsError(v, n))
10641068
end
10651069
r = n

test/offsetarray.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,18 @@ am = map(identity, a)
278278

279279
# other functions
280280
v = OffsetArray(v0, (-3,))
281+
@test endof(v) == 1
281282
@test v v
282283
@test indices(v') === (Base.OneTo(1),-2:1)
284+
rv = reverse(v)
285+
@test indices(rv) == indices(v)
286+
@test rv[1] == v[-2]
287+
@test rv[0] == v[-1]
288+
@test rv[-1] == v[0]
289+
@test rv[-2] == v[1]
290+
cv = copy(v)
291+
@test reverse!(cv) == rv
292+
283293
A = OffsetArray(rand(4,4), (-3,5))
284294
@test A A
285295
@test indices(A') === (6:9, -2:1)

0 commit comments

Comments
 (0)