Skip to content

Commit 6c8cd0e

Browse files
authored
Merge pull request #59 from JuliaArrays/teh/unsafe_getindex
Don't extend Base.unsafe_getindex
2 parents 4d2f8cc + 9e3705c commit 6c8cd0e

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

src/core.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ axistype{name,T}(::Type{Axis{name,T}}) = T
6060
# Pass indexing and related functions straight through to the wrapped value
6161
# TODO: should Axis be an AbstractArray? AbstractArray{T,0} for scalar T?
6262
Base.getindex(A::Axis, i...) = A.val[i...]
63-
Base.unsafe_getindex(A::Axis, i...) = Base.unsafe_getindex(A, i...)
6463
Base.eltype{_,T}(::Type{Axis{_,T}}) = eltype(T)
6564
Base.size(A::Axis) = size(A.val)
6665
Base.indices(A::Axis) = indices(A.val)

src/indexing.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Idx = Union{Real,Colon,AbstractArray{Int}}
22

3-
using Base: ViewIndex, unsafe_getindex, unsafe_setindex!
3+
using Base: ViewIndex
44

55
# Defer IndexStyle to the wrapped array
66
@compat Base.IndexStyle{T,N,D,Ax}(::Type{AxisArray{T,N,D,Ax}}) = IndexStyle(D)
@@ -162,20 +162,20 @@ function axisindexes(::Type{Dimensional}, ax::Range, idx::RepeatedInterval)
162162
n = length(idx.offsets)
163163
idxs = unsafe_searchsorted(ax, idx.window)
164164
offsets = [searchsortednearest(ax, idx.offsets[i]) for i=1:n]
165-
AxisArray(RepeatedRangeMatrix(idxs, offsets), Axis{:sub}(unsafe_getindex(ax, idxs)), Axis{:rep}(ax[offsets]))
165+
AxisArray(RepeatedRangeMatrix(idxs, offsets), Axis{:sub}(inbounds_getindex(ax, idxs)), Axis{:rep}(ax[offsets]))
166166
end
167167

168168
# We also have special datatypes to represent intervals about indices
169169
axisindexes(::Type{Dimensional}, ax::AbstractVector, idx::IntervalAtIndex) = searchsorted(ax, idx.window + ax[idx.index])
170170
function axisindexes(::Type{Dimensional}, ax::Range, idx::IntervalAtIndex)
171171
idxs = unsafe_searchsorted(ax, idx.window)
172-
AxisArray(idxs + idx.index, Axis{:sub}(unsafe_getindex(ax, idxs)))
172+
AxisArray(idxs + idx.index, Axis{:sub}(inbounds_getindex(ax, idxs)))
173173
end
174174
axisindexes(::Type{Dimensional}, ax::AbstractVector, idx::RepeatedIntervalAtIndexes) = error("repeated intervals might select a varying number of elements for non-range axes; use a repeated Range of indices instead")
175175
function axisindexes(::Type{Dimensional}, ax::Range, idx::RepeatedIntervalAtIndexes)
176176
n = length(idx.indexes)
177177
idxs = unsafe_searchsorted(ax, idx.window)
178-
AxisArray(RepeatedRangeMatrix(idxs, idx.indexes), Axis{:sub}(unsafe_getindex(ax, idxs)), Axis{:rep}(ax[idx.indexes]))
178+
AxisArray(RepeatedRangeMatrix(idxs, idx.indexes), Axis{:sub}(inbounds_getindex(ax, idxs)), Axis{:rep}(ax[idx.indexes]))
179179
end
180180

181181
# Categorical axes may be indexed by their elements

src/search.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,28 @@ function Base.searchsorted(a::Range, I::ClosedInterval)
3737
end
3838

3939
# When running with "--check-bounds=yes" (like on Travis), the bounds-check isn't elided
40-
@inline function Base.unsafe_getindex{T}(v::Range{T}, i::Integer)
40+
@inline function inbounds_getindex{T}(v::Range{T}, i::Integer)
4141
convert(T, first(v) + (i-1)*step(v))
4242
end
43-
@inline function Base.unsafe_getindex{T<:Integer}(r::StepRange, s::Range{T})
44-
st = oftype(r.start, r.start + (first(s)-1)*step(r))
43+
@inline function inbounds_getindex{T<:Integer}(r::Range, s::Range{T})
44+
f = first(r)
45+
st = oftype(f, f + (first(s)-1)*step(r))
4546
range(st, step(r)*step(s), length(s))
4647
end
4748
if VERSION < v"0.6.0-dev.2390"
4849
include_string("""
49-
@inline function Base.unsafe_getindex{T}(r::FloatRange{T}, i::Integer)
50+
@inline function inbounds_getindex{T}(r::FloatRange{T}, i::Integer)
5051
convert(T, (r.start + (i-1)*r.step)/r.divisor)
5152
end
52-
@inline function Base.unsafe_getindex(r::FloatRange, s::OrdinalRange)
53+
@inline function inbounds_getindex(r::FloatRange, s::OrdinalRange)
5354
FloatRange(r.start + (first(s)-1)*r.step, step(s)*r.step, length(s), r.divisor)
5455
end
5556
""")
5657
else
5758
include_string("""
58-
@inline function Base.unsafe_getindex(r::StepRangeLen, s::OrdinalRange)
59-
vfirst = unsafe_getindex(r, first(s))
59+
@inline inbounds_getindex(r::StepRangeLen, i::Integer) = Base.unsafe_getindex(r, i)
60+
@inline function inbounds_getindex(r::StepRangeLen, s::OrdinalRange)
61+
vfirst = Base.unsafe_getindex(r, first(s))
6062
StepRangeLen(vfirst, r.step*step(s), length(s))
6163
end
6264
""")
@@ -65,12 +67,12 @@ end
6567
function unsafe_searchsortedlast{T<:Number}(a::Range{T}, x::Number)
6668
step(a) == 0 && throw(ArgumentError("ranges with a zero step are unsupported"))
6769
n = round(Integer,(x-first(a))/step(a))+1
68-
isless(x, unsafe_getindex(a, n)) ? n-1 : n
70+
isless(x, inbounds_getindex(a, n)) ? n-1 : n
6971
end
7072
function unsafe_searchsortedfirst{T<:Number}(a::Range{T}, x::Number)
7173
step(a) == 0 && throw(ArgumentError("ranges with a zero step are unsupported"))
7274
n = round(Integer,(x-first(a))/step(a))+1
73-
isless(unsafe_getindex(a, n), x) ? n+1 : n
75+
isless(inbounds_getindex(a, n), x) ? n+1 : n
7476
end
7577
function unsafe_searchsortedlast{T<:Integer}(a::Range{T}, x::Number)
7678
step(a) == 0 && throw(ArgumentError("ranges with a zero step are unsupported"))

test/REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
OffsetArrays
2-
Unitful
2+
Unitful 0.1.0

test/indexing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ D[1,1,1,1,1] = 10
3737

3838
# Linear indexing across multiple dimensions drops tracking of those dims
3939
@test A[:].axes[1].val == 1:length(A)
40+
# TODO: remove the next 4 lines when we no longer feel we need to test for this
41+
VERSION >= v"0.6.0-dev" && info("partial linear indexing deprecation warning is expected")
4042
B = A[1:2,:]
4143
@test B.axes[1].val == A.axes[1].val[1:2]
4244
@test B.axes[2].val == 1:Base.trailingsize(A,2)

0 commit comments

Comments
 (0)