Skip to content

Commit 45dd9be

Browse files
committed
rebase on master
1 parent e191696 commit 45dd9be

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/axes.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,21 @@ for f in [:first, :last]
179179
@eval @inline Base.$f(r::IdOffsetRange) = eltype(r)($f(r.parent) + r.offset)
180180
end
181181

182-
@inline function Base.iterate(r::IdOffsetRange)
183-
ret = iterate(r.parent)
184-
ret === nothing && return nothing
185-
return (eltype(r)(ret[1] + r.offset), ret[2])
186-
end
187-
@inline function Base.iterate(r::IdOffsetRange, i)
188-
ret = iterate(r.parent, i)
182+
# Iteration for an IdOffsetRange
183+
@inline Base.iterate(r::IdOffsetRange, i...) = _iterate(r, i...)
184+
# In general we iterate over the parent term by term and add the offset.
185+
# This might have some performance degradation when coupled with bounds-checking
186+
# See https://github.com/JuliaArrays/OffsetArrays.jl/issues/214
187+
@inline function _iterate(r::IdOffsetRange, i...)
188+
ret = iterate(r.parent, i...)
189189
ret === nothing && return nothing
190190
return (eltype(r)(ret[1] + r.offset), ret[2])
191191
end
192+
# Base.OneTo(n) is known to be exactly equivalent to the range 1:n,
193+
# and has no specialized iteration defined for it,
194+
# so we may add the offset to the range directly and iterate over the result
195+
# This gets around the performance issue described in issue #214
196+
@inline _iterate(r::IdOffsetRange{<:Integer, <:Base.OneTo}, i...) = iterate(r.parent .+ r.offset, i...)
192197

193198
@inline function Base.getindex(r::IdOffsetRange, i::Integer)
194199
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))

test/runtests.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ end
221221
@test OrdinalRange{BigInt,BigInt}(r2) === r2
222222
end
223223
end
224-
<<<<<<< HEAD
225224

226225
@testset "Bool IdOffsetRange (issue #223)" begin
227226
for b1 in [false, true], b2 in [false, true]
@@ -345,7 +344,8 @@ end
345344
@test_throws BoundsError r[true:true:false]
346345
@test_throws BoundsError r[false:true:false]
347346
end
348-
=======
347+
end
348+
349349
@testset "iteration" begin
350350
# parent has Base.OneTo axes
351351
A = ones(4:10)
@@ -363,7 +363,6 @@ end
363363
@test C[ind] == C[4]
364364
ind, st = iterate(ax, st)
365365
@test C[ind] == C[5]
366-
>>>>>>> df172e1... specialize for Base.OneTo
367366
end
368367
end
369368

0 commit comments

Comments
 (0)