Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow offset ranges when comparing ranges #54825

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ axes1(S::IdentityUnitRange) = S
axes(S::IdentityUnitRange{<:OneTo}) = (S.indices,)
axes1(S::IdentityUnitRange{<:OneTo}) = S.indices

values(S::IdentityUnitRange) = values(S.indices)
first(S::IdentityUnitRange) = first(S.indices)
last(S::IdentityUnitRange) = last(S.indices)
size(S::IdentityUnitRange) = (length(S.indices),)
Expand Down
20 changes: 4 additions & 16 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1111,19 +1111,22 @@ function show(io::IO, r::StepRangeLen)
end

function ==(r::T, s::T) where {T<:AbstractRange}
firstindex(r) == firstindex(s) || return false
isempty(r) && return isempty(s)
_has_length_one(r) && return _has_length_one(s) & (first(r) == first(s))
(first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
end

function ==(r::OrdinalRange, s::OrdinalRange)
firstindex(r) == firstindex(s) || return false
isempty(r) && return isempty(s)
_has_length_one(r) && return _has_length_one(s) & (first(r) == first(s))
(first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))
end

==(r::AbstractUnitRange, s::AbstractUnitRange) =
(isempty(r) & isempty(s)) | ((first(r) == first(s)) & (last(r) == last(s)))
(firstindex(r) == firstindex(s)) &
((isempty(r) & isempty(s)) | ((first(r) == first(s)) & (last(r) == last(s))))

==(r::OneTo, s::OneTo) = last(r) == last(s)

Expand All @@ -1139,21 +1142,6 @@ end
_has_length_one(r::OrdinalRange) = first(r) == last(r)
_has_length_one(r::AbstractRange) = isone(length(r))

function ==(r::AbstractRange, s::AbstractRange)
lr = length(r)
if lr != length(s)
return false
elseif iszero(lr)
return true
end
yr, ys = iterate(r), iterate(s)
while yr !== nothing
yr[1] == ys[1] || return false
yr, ys = iterate(r, yr[2]), iterate(s, ys[2])
end
return true
end

intersect(r::OneTo, s::OneTo) = OneTo(min(r.stop,s.stop))
union(r::OneTo, s::OneTo) = OneTo(max(r.stop,s.stop))

Expand Down
7 changes: 5 additions & 2 deletions test/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,13 @@ module TestOffsetArray
using .Main.OffsetArrays
using Test

Base.values(r::OffsetArrays.IdOffsetRange) = first(r):last(r)
axes1based(a) = map(values, axes(a))

A = OffsetArray(rand(2, 3), -1, -1)
R = CartesianIndices(A)
@test R == CartesianIndices((0:1, 0:2))
@test axes(R) == (0:1, 0:2)
@test first(R) == first(CartesianIndices((0:1, 0:2))) && last(R) == last(CartesianIndices((0:1, 0:2)))
@test axes1based(R) == (0:1, 0:2)
for i in eachindex(A)
@test A[i] == A[R[i]]
end
Expand Down
27 changes: 15 additions & 12 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ using LinearAlgebra
using Base: IdentityUnitRange
using Test

Base.values(r::IdOffsetRange) = first(r):last(r)
axes1based(a) = map(values, axes(a))

if !isdefined(@__MODULE__, :T24Linear)
include("testhelpers/arrayindexingtypes.jl")
end
Expand Down Expand Up @@ -484,7 +487,7 @@ I = findall(!iszero, z)
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/92
A92 = OffsetArray(reshape(1:27, 3, 3, 3), -2, -2, -2)
B92 = view(A92, :, :, -1:0)
@test axes(B92) == (-1:1, -1:1, 1:2)
@test axes1based(B92) == (-1:1, -1:1, 1:2)
@test sum(B92, dims=(2,3)) == OffsetArray(reshape([51,57,63], Val(3)), -2, -2, 0)
B92 = view(A92, :, :, Base.IdentityUnitRange(-1:0))
@test sum(B92, dims=(2,3)) == OffsetArray(reshape([51,57,63], Val(3)), -2, -2, -2)
Expand Down Expand Up @@ -718,19 +721,19 @@ end
@test S[0,4] == S[3] == 3
@test S[1,4] == S[4] == 4
@test_throws BoundsError S[1,1]
@test axes(S) == OffsetArrays.IdOffsetRange.((0:1, 3:4))
@test axes1based(S) == (0:1, 3:4)
S = view(A, axes(A, 1), 3)
@test S == A[:, 3]
@test S[0] == 1
@test S[1] == 2
@test_throws BoundsError S[length(S)]
@test axes(S) == (OffsetArrays.IdOffsetRange(0:1), )
@test axes1based(S) == (0:1,)
S = view(A, 1, axes(A, 2))
@test S == A[1, :]
@test S[3] == 2
@test S[4] == 4
@test_throws BoundsError S[1]
@test axes(S) == (OffsetArrays.IdOffsetRange(3:4), )
@test axes1based(S) == (3:4,)

A0 = collect(reshape(1:24, 2, 3, 4))
A = OffsetArray(A0, (-1,2,1))
Expand All @@ -739,7 +742,7 @@ end
@test S[0, 1, 2] == A[0, 3, 2]
@test S[0, 2, 2] == A[0, 4, 2]
@test S[1, 1, 2] == A[1, 3, 2]
@test axes(S) == (OffsetArrays.IdOffsetRange(0:1), Base.OneTo(2), OffsetArrays.IdOffsetRange(2:5))
@test axes1based(S) == (0:1, Base.OneTo(2), 2:5)
end

@testset "Zero-index indexing" begin
Expand All @@ -759,7 +762,7 @@ end
s = -2:2:4
r = 5:8
y = OffsetArray(s, r)
@test axes(y) == (r,)
@test axes1based(y) == (r,)
@test step(y) == step(s)

a = OffsetVector(3:4, 10:11)
Expand Down Expand Up @@ -827,12 +830,12 @@ end
@test stack(ten .+ nought') == ten .+ nought'
@test stack(x^2 for x in ten) == ten.^2

@test axes(stack(nought for _ in ten)) == (0:2, 10:13)
@test axes(stack([nought for _ in ten])) == (0:2, 10:13)
@test axes(stack(nought for _ in ten; dims=1)) == (10:13, 0:2)
@test axes(stack((x, x^2) for x in nought)) == (1:2, 0:2)
@test axes(stack(x -> x[end-1:end], ten for _ in nought, _ in nought)) == (1:2, 0:2, 0:2)
@test axes(stack([ten[end-1:end] for _ in nought, _ in nought])) == (1:2, 0:2, 0:2)
@test axes1based(stack(nought for _ in ten)) == (0:2, 10:13)
@test axes1based(stack([nought for _ in ten])) == (0:2, 10:13)
@test axes1based(stack(nought for _ in ten; dims=1)) == (10:13, 0:2)
@test axes1based(stack((x, x^2) for x in nought)) == (1:2, 0:2)
@test axes1based(stack(x -> x[end-1:end], ten for _ in nought, _ in nought)) == (1:2, 0:2, 0:2)
@test axes1based(stack([ten[end-1:end] for _ in nought, _ in nought])) == (1:2, 0:2, 0:2)
end

@testset "issue #41630: replace_ref_begin_end!/@view on offset-like arrays" begin
Expand Down
13 changes: 7 additions & 6 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1074,24 +1074,25 @@ end

# comparing and hashing ranges
@testset "comparing and hashing ranges" begin
Rs = AbstractRange[1:1, 1:1:1, 1:2, 1:1:2,
Rs = AbstractRange[1:1, 1:1:1, 2:3, 2:1:3,
map(Int32,1:3:17), map(Int64,1:3:17), 1:0, 1:-1:0, 17:-3:0,
0.0:0.1:1.0, map(Float32,0.0:0.1:1.0),map(Float32,LinRange(0.0, 1.0, 11)),
1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994,
range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20)),
3:2, 5:-2:7, range(0.0, step=2.0, length=0), 3//2:3//2:0//1, LinRange(2,3,0),
Base.OneTo(1), 1:1, 1:-3:1, 1//1:1//3:1//1, range(1.0, step=2.5, length=1),
LinRange(1,1,1), LinRange(1,1,2)]
LinRange(1,1,1), LinRange(1,1,2), Base.IdentityUnitRange(2:3), Base.IdentityUnitRange(2:1)]
for r in Rs
local r
ar = Vector(r)
@test r == ar
@test isequal(r,ar)
@test hash(r) == hash(ar)
@test (r == ar) == isone(firstindex(r))
@test isequal(r, ar) == isone(firstindex(r))
@test (hash(r) == hash(ar)) == isone(firstindex(r))
for s in Rs
as = Vector(s)
@test isequal(r,s) == (hash(r)==hash(s))
@test (r==s) == (ar==as)
axes_eq = firstindex(r) == firstindex(s) && lastindex(r) == lastindex(s)
@test (r == s) == (ar == as && axes_eq)
end
end
end
Expand Down
Loading