Skip to content

Commit 611adb6

Browse files
timholytkelman
authored andcommitted
More fixes for non-1 arrays
(cherry picked from commit 4776d9a) ref #21251
1 parent ba6d805 commit 611adb6

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
@@ -125,7 +125,7 @@ julia> length(A)
125125
length(t::AbstractArray) = prod(size(t))
126126
_length(A::AbstractArray) = prod(map(unsafe_length, indices(A))) # circumvent missing size
127127
_length(A) = length(A)
128-
endof(a::AbstractArray) = length(a)
128+
endof(a::AbstractArray) = last(linearindices(a))
129129
first(a::AbstractArray) = a[first(eachindex(a))]
130130

131131
function first(itr)

base/array.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,26 +690,30 @@ function lexcmp(a::Array{UInt8,1}, b::Array{UInt8,1})
690690
return c < 0 ? -1 : c > 0 ? +1 : cmp(length(a),length(b))
691691
end
692692

693-
function reverse(A::AbstractVector, s=1, n=length(A))
693+
function reverse(A::AbstractVector, s=first(linearindices(A)), n=last(linearindices(A)))
694694
B = similar(A)
695-
for i = 1:s-1
695+
for i = first(linearindices(A)):s-1
696696
B[i] = A[i]
697697
end
698698
for i = s:n
699699
B[i] = A[n+s-i]
700700
end
701-
for i = n+1:length(A)
701+
for i = n+1:last(linearindices(A))
702702
B[i] = A[i]
703703
end
704704
return B
705705
end
706-
reverseind(a::AbstractVector, i::Integer) = length(a) + 1 - i
706+
function reverseind(a::AbstractVector, i::Integer)
707+
li = linearindices(a)
708+
first(li) + last(li) - i
709+
end
707710

708-
function reverse!(v::AbstractVector, s=1, n=length(v))
711+
function reverse!(v::AbstractVector, s=first(linearindices(v)), n=last(linearindices(v)))
712+
liv = linearindices(v)
709713
if n <= s # empty case; ok
710-
elseif !(1 s endof(v))
714+
elseif !(first(liv) s last(liv))
711715
throw(BoundsError(v, s))
712-
elseif !(1 n endof(v))
716+
elseif !(first(liv) n last(liv))
713717
throw(BoundsError(v, n))
714718
end
715719
r = n

test/offsetarray.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,19 @@ am = map(identity, a)
268268

269269
# other functions
270270
v = OffsetArray(v0, (-3,))
271+
@test endof(v) == 1
271272
@test_approx_eq v v
272273
@test parent(v') == v0'
273274
@test indices(v') === (1:1,-2:1)
275+
rv = reverse(v)
276+
@test indices(rv) == indices(v)
277+
@test rv[1] == v[-2]
278+
@test rv[0] == v[-1]
279+
@test rv[-1] == v[0]
280+
@test rv[-2] == v[1]
281+
cv = copy(v)
282+
@test reverse!(cv) == rv
283+
274284
A = OffsetArray(rand(4,4), (-3,5))
275285
@test_approx_eq A A
276286
@test maximum(A) == maximum(parent(A))

0 commit comments

Comments
 (0)