Skip to content

Commit 7bae827

Browse files
committed
Update for Base.IdentityUnitRange
1 parent f455239 commit 7bae827

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/OffsetArrays.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ VERSION < v"0.7.0-beta2.199" && __precompile__()
33
module OffsetArrays
44

55
using Base: Indices, tail, @propagate_inbounds
6+
@static if !isdefined(Base, :IdentityUnitRange)
7+
const IdentityUnitRange = Base.Slice
8+
else
9+
using Base: IdentityUnitRange
10+
end
611

712
export OffsetArray, OffsetVector
813

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

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

83-
const OffsetAxis = Union{Integer, UnitRange, Base.Slice{<:UnitRange}, Base.OneTo}
88+
const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, IdentityUnitRange}
8489
function Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T
8590
B = similar(parent(A), T, dims)
8691
end
@@ -186,6 +191,6 @@ printindices(io::IO, ind1, inds...) =
186191
(print(io, _unslice(ind1), ", "); printindices(io, inds...))
187192
printindices(io::IO, ind1) = print(io, _unslice(ind1))
188193
_unslice(x) = x
189-
_unslice(x::Base.Slice) = x.indices
194+
_unslice(x::IdentityUnitRange) = x.indices
190195

191196
end # module

test/runtests.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OffsetArrays
22
using Test
33
using DelimitedFiles
4+
using OffsetArrays: IdentityUnitRange
45

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

@@ -42,8 +43,8 @@ y = OffsetArray(r, r)
4243
y = OffsetArray(r, (r,))
4344
@test axes(y) == (r,)
4445

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

4849
A0 = [1 3; 2 4]
4950
A = OffsetArray(A0, (-1,2)) # IndexLinear
@@ -104,21 +105,21 @@ Ac[0,3,1] = 11
104105
@test_throws BoundsError S[CartesianIndex(1,1),0]
105106
@test_throws BoundsError S[CartesianIndex(1,1),2]
106107
@test eachindex(A) == 1:4
107-
@test eachindex(S) == CartesianIndices(Base.Slice.((0:1,3:4)))
108+
@test eachindex(S) == CartesianIndices(IdentityUnitRange.((0:1,3:4)))
108109

109110
# view
110111
S = view(A, :, 3)
111112
@test S == OffsetArray([1,2], (A.offsets[1],))
112113
@test S[0] == 1
113114
@test S[1] == 2
114115
@test_throws BoundsError S[2]
115-
@test axes(S) === (Base.Slice(0:1),)
116+
@test axes(S) === (IdentityUnitRange(0:1),)
116117
S = view(A, 0, :)
117118
@test S == OffsetArray([1,3], (A.offsets[2],))
118119
@test S[3] == 1
119120
@test S[4] == 3
120121
@test_throws BoundsError S[1]
121-
@test axes(S) === (Base.Slice(3:4),)
122+
@test axes(S) === (IdentityUnitRange(3:4),)
122123
S = view(A, 0:0, 4)
123124
@test S == [3]
124125
@test S[1] == 3
@@ -137,7 +138,7 @@ S = view(A, :, :)
137138
@test S[0,4] == S[3] == 3
138139
@test S[1,4] == S[4] == 4
139140
@test_throws BoundsError S[1,1]
140-
@test axes(S) === Base.Slice.((0:1, 3:4))
141+
@test axes(S) === IdentityUnitRange.((0:1, 3:4))
141142

142143
# iteration
143144
let a
@@ -178,10 +179,10 @@ B = similar(A, (3,4))
178179
@test axes(B) === (Base.OneTo(3), Base.OneTo(4))
179180
B = similar(A, (-3:3,1:4))
180181
@test isa(B, OffsetArray{Int,2})
181-
@test axes(B) === Base.Slice.((-3:3, 1:4))
182+
@test axes(B) === IdentityUnitRange.((-3:3, 1:4))
182183
B = similar(parent(A), (-3:3,1:4))
183184
@test isa(B, OffsetArray{Int,2})
184-
@test axes(B) === Base.Slice.((-3:3, 1:4))
185+
@test axes(B) === IdentityUnitRange.((-3:3, 1:4))
185186
@test isa([x for x in [1,2,3]], Vector{Int})
186187
@test similar(Array{Int}, (0:0, 0:0)) isa OffsetArray{Int, 2}
187188
@test similar(Array{Int}, (1, 1)) isa Matrix{Int}
@@ -191,13 +192,13 @@ B = similar(parent(A), (-3:3,1:4))
191192
B = reshape(A0, -10:-9, 9:10)
192193
@test isa(B, OffsetArray{Int,2})
193194
@test parent(B) === A0
194-
@test axes(B) == Base.Slice.((-10:-9, 9:10))
195+
@test axes(B) == IdentityUnitRange.((-10:-9, 9:10))
195196
B = reshape(A, -10:-9, 9:10)
196197
@test isa(B, OffsetArray{Int,2})
197198
@test pointer(parent(B)) === pointer(A0)
198-
@test axes(B) == Base.Slice.((-10:-9, 9:10))
199+
@test axes(B) == IdentityUnitRange.((-10:-9, 9:10))
199200
b = reshape(A, -7:-4)
200-
@test axes(b) == (Base.Slice(-7:-4),)
201+
@test axes(b) == (IdentityUnitRange(-7:-4),)
201202
@test isa(parent(b), Vector{Int})
202203
@test pointer(parent(b)) === pointer(parent(A))
203204
@test parent(b) == A0[:]
@@ -206,27 +207,27 @@ a = OffsetArray(rand(3,3,3), -1:1, 0:2, 3:5)
206207
b = reshape(a, Val(2))
207208
@test isa(b, OffsetArray{Float64,2})
208209
@test pointer(parent(b)) === pointer(parent(a))
209-
@test axes(b) == Base.Slice.((-1:1, 1:9))
210+
@test axes(b) == IdentityUnitRange.((-1:1, 1:9))
210211
b = reshape(a, Val(4))
211212
@test isa(b, OffsetArray{Float64,4})
212213
@test pointer(parent(b)) === pointer(parent(a))
213-
@test axes(b) == (axes(a)..., Base.Slice(1:1))
214+
@test axes(b) == (axes(a)..., IdentityUnitRange(1:1))
214215

215216
# Indexing with OffsetArray axes
216217
i1 = OffsetArray([2,1], (-5,))
217218
i1 = OffsetArray([2,1], -5)
218219
b = A0[i1, 1]
219-
@test axes(b) === (Base.Slice(-4:-3),)
220+
@test axes(b) === (IdentityUnitRange(-4:-3),)
220221
@test b[-4] == 2
221222
@test b[-3] == 1
222223
b = A0[1,i1]
223-
@test axes(b) === (Base.Slice(-4:-3),)
224+
@test axes(b) === (IdentityUnitRange(-4:-3),)
224225
@test b[-4] == 3
225226
@test b[-3] == 1
226227
v = view(A0, i1, 1)
227-
@test axes(v) === (Base.Slice(-4:-3),)
228+
@test axes(v) === (IdentityUnitRange(-4:-3),)
228229
v = view(A0, 1:1, i1)
229-
@test axes(v) === (Base.OneTo(1), Base.Slice(-4:-3))
230+
@test axes(v) === (Base.OneTo(1), IdentityUnitRange(-4:-3))
230231

231232
# logical indexing
232233
@test A[A .> 2] == [3,4]

0 commit comments

Comments
 (0)