Skip to content

Commit

Permalink
Merge pull request #62 from JuliaArrays/teh/iur
Browse files Browse the repository at this point in the history
Support IdentityUnitRange
  • Loading branch information
timholy authored Dec 11, 2018
2 parents 2436d83 + 7bae827 commit 2925930
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ os:

julia:
- 0.7
- 1.0
- nightly

notifications:
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
environment:
matrix:
- julia_version: 0.7
- julia_version: latest
- julia_version: 1.0
- julia_version: nightly

platform:
- x86 # 32-bit
Expand Down
11 changes: 8 additions & 3 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ VERSION < v"0.7.0-beta2.199" && __precompile__()
module OffsetArrays

using Base: Indices, tail, @propagate_inbounds
@static if !isdefined(Base, :IdentityUnitRange)
const IdentityUnitRange = Base.Slice
else
using Base: IdentityUnitRange
end

export OffsetArray, OffsetVector

Expand Down Expand Up @@ -78,9 +83,9 @@ _axes(::Tuple{}, ::Tuple{}) = ()
Base.axes1(A::OffsetArray{T,0}) where {T} = 1:1 # we only need to specialize this one

# Avoid the kw-arg on the range(r+x, length=length(r)) call in r .+ x
@inline _slice(r, x) = Base.Slice(Base._range(first(r) + x, nothing, nothing, length(r)))
@inline _slice(r, x) = IdentityUnitRange(Base._range(first(r) + x, nothing, nothing, length(r)))

const OffsetAxis = Union{Integer, UnitRange, Base.Slice{<:UnitRange}, Base.OneTo}
const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, IdentityUnitRange}
function Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T
B = similar(parent(A), T, dims)
end
Expand Down Expand Up @@ -186,6 +191,6 @@ printindices(io::IO, ind1, inds...) =
(print(io, _unslice(ind1), ", "); printindices(io, inds...))
printindices(io::IO, ind1) = print(io, _unslice(ind1))
_unslice(x) = x
_unslice(x::Base.Slice) = x.indices
_unslice(x::IdentityUnitRange) = x.indices

end # module
35 changes: 18 additions & 17 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using OffsetArrays
using Test
using DelimitedFiles
using OffsetArrays: IdentityUnitRange

@test isempty(detect_ambiguities(OffsetArrays, Base, Core))

Expand Down Expand Up @@ -42,8 +43,8 @@ y = OffsetArray(r, r)
y = OffsetArray(r, (r,))
@test axes(y) == (r,)

y = OffsetArray{Float32}(undef, (Base.Slice(-1:1),))
@test axes(y) === (Base.Slice(-1:1),)
y = OffsetArray{Float32}(undef, (IdentityUnitRange(-1:1),))
@test axes(y) === (IdentityUnitRange(-1:1),)

A0 = [1 3; 2 4]
A = OffsetArray(A0, (-1,2)) # IndexLinear
Expand Down Expand Up @@ -104,21 +105,21 @@ Ac[0,3,1] = 11
@test_throws BoundsError S[CartesianIndex(1,1),0]
@test_throws BoundsError S[CartesianIndex(1,1),2]
@test eachindex(A) == 1:4
@test eachindex(S) == CartesianIndices(Base.Slice.((0:1,3:4)))
@test eachindex(S) == CartesianIndices(IdentityUnitRange.((0:1,3:4)))

# view
S = view(A, :, 3)
@test S == OffsetArray([1,2], (A.offsets[1],))
@test S[0] == 1
@test S[1] == 2
@test_throws BoundsError S[2]
@test axes(S) === (Base.Slice(0:1),)
@test axes(S) === (IdentityUnitRange(0:1),)
S = view(A, 0, :)
@test S == OffsetArray([1,3], (A.offsets[2],))
@test S[3] == 1
@test S[4] == 3
@test_throws BoundsError S[1]
@test axes(S) === (Base.Slice(3:4),)
@test axes(S) === (IdentityUnitRange(3:4),)
S = view(A, 0:0, 4)
@test S == [3]
@test S[1] == 3
Expand All @@ -137,7 +138,7 @@ S = view(A, :, :)
@test S[0,4] == S[3] == 3
@test S[1,4] == S[4] == 4
@test_throws BoundsError S[1,1]
@test axes(S) === Base.Slice.((0:1, 3:4))
@test axes(S) === IdentityUnitRange.((0:1, 3:4))

# iteration
let a
Expand Down Expand Up @@ -178,10 +179,10 @@ B = similar(A, (3,4))
@test axes(B) === (Base.OneTo(3), Base.OneTo(4))
B = similar(A, (-3:3,1:4))
@test isa(B, OffsetArray{Int,2})
@test axes(B) === Base.Slice.((-3:3, 1:4))
@test axes(B) === IdentityUnitRange.((-3:3, 1:4))
B = similar(parent(A), (-3:3,1:4))
@test isa(B, OffsetArray{Int,2})
@test axes(B) === Base.Slice.((-3:3, 1:4))
@test axes(B) === IdentityUnitRange.((-3:3, 1:4))
@test isa([x for x in [1,2,3]], Vector{Int})
@test similar(Array{Int}, (0:0, 0:0)) isa OffsetArray{Int, 2}
@test similar(Array{Int}, (1, 1)) isa Matrix{Int}
Expand All @@ -191,13 +192,13 @@ B = similar(parent(A), (-3:3,1:4))
B = reshape(A0, -10:-9, 9:10)
@test isa(B, OffsetArray{Int,2})
@test parent(B) === A0
@test axes(B) == Base.Slice.((-10:-9, 9:10))
@test axes(B) == IdentityUnitRange.((-10:-9, 9:10))
B = reshape(A, -10:-9, 9:10)
@test isa(B, OffsetArray{Int,2})
@test pointer(parent(B)) === pointer(A0)
@test axes(B) == Base.Slice.((-10:-9, 9:10))
@test axes(B) == IdentityUnitRange.((-10:-9, 9:10))
b = reshape(A, -7:-4)
@test axes(b) == (Base.Slice(-7:-4),)
@test axes(b) == (IdentityUnitRange(-7:-4),)
@test isa(parent(b), Vector{Int})
@test pointer(parent(b)) === pointer(parent(A))
@test parent(b) == A0[:]
Expand All @@ -206,27 +207,27 @@ a = OffsetArray(rand(3,3,3), -1:1, 0:2, 3:5)
b = reshape(a, Val(2))
@test isa(b, OffsetArray{Float64,2})
@test pointer(parent(b)) === pointer(parent(a))
@test axes(b) == Base.Slice.((-1:1, 1:9))
@test axes(b) == IdentityUnitRange.((-1:1, 1:9))
b = reshape(a, Val(4))
@test isa(b, OffsetArray{Float64,4})
@test pointer(parent(b)) === pointer(parent(a))
@test axes(b) == (axes(a)..., Base.Slice(1:1))
@test axes(b) == (axes(a)..., IdentityUnitRange(1:1))

# Indexing with OffsetArray axes
i1 = OffsetArray([2,1], (-5,))
i1 = OffsetArray([2,1], -5)
b = A0[i1, 1]
@test axes(b) === (Base.Slice(-4:-3),)
@test axes(b) === (IdentityUnitRange(-4:-3),)
@test b[-4] == 2
@test b[-3] == 1
b = A0[1,i1]
@test axes(b) === (Base.Slice(-4:-3),)
@test axes(b) === (IdentityUnitRange(-4:-3),)
@test b[-4] == 3
@test b[-3] == 1
v = view(A0, i1, 1)
@test axes(v) === (Base.Slice(-4:-3),)
@test axes(v) === (IdentityUnitRange(-4:-3),)
v = view(A0, 1:1, i1)
@test axes(v) === (Base.OneTo(1), Base.Slice(-4:-3))
@test axes(v) === (Base.OneTo(1), IdentityUnitRange(-4:-3))

# logical indexing
@test A[A .> 2] == [3,4]
Expand Down

0 comments on commit 2925930

Please sign in to comment.