Skip to content

Commit

Permalink
switch to checkindex optimization.
Browse files Browse the repository at this point in the history
the `checkindex` hack is based on the assumption that `last(ind) >= 0`

remove `iterate` specialization.
  • Loading branch information
N5N3 committed Feb 24, 2023
1 parent 3b2d9b0 commit bb84177
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 28 deletions.
2 changes: 2 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ false
checkindex(::Type{Bool}, inds::AbstractUnitRange, i) =
throw(ArgumentError("unable to check bounds for indices of type $(typeof(i))"))
checkindex(::Type{Bool}, inds::AbstractUnitRange, i::Real) = (first(inds) <= i) & (i <= last(inds))
checkindex(::Type{Bool}, inds::IdentityUnitRange, i::Real) = checkindex(Bool, inds.indices, i)
checkindex(::Type{Bool}, inds::OneTo{T}, i::T) where {T<:BitInteger} = unsigned(i - one(i)) < unsigned(last(inds))
checkindex(::Type{Bool}, inds::AbstractUnitRange, ::Colon) = true
checkindex(::Type{Bool}, inds::AbstractUnitRange, ::Slice) = true
function checkindex(::Type{Bool}, inds::AbstractUnitRange, r::AbstractRange)
Expand Down
28 changes: 0 additions & 28 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,34 +433,6 @@ find_extended_inds(::ScalarIndex, I...) = (@inline; find_extended_inds(I...))
find_extended_inds(i1, I...) = (@inline; (i1, find_extended_inds(I...)...))
find_extended_inds() = ()

function is_valid_ind(p, i, l, stride1)
fi = firstindex(p)
li = lastindex(p)
if stride1 < 0
fi <= l <= i <= li
elseif fi != 1
fi <= i <= l <= li
else
(i-1) % UInt < l % UInt <= li % UInt
end
end

function iterate(v::FastContiguousSubArray,
state = v.offset1 .+ (firstindex(v), lastindex(v)))
p = parent(v)
i, l = state
is_valid_ind(p, i, l, 1) ? (@inbounds p[i], (i+1, l)) : nothing
end

stride1(v::SubArray{T,N,P}) where {T,N,P} = P <: StridedArray ? stride(v, 1) : v.stride1

function iterate(v::FastSubArray,
state = v.offset1 .+ stride1(v) .* (firstindex(v), lastindex(v)))
p = parent(v)
i, l = state
is_valid_ind(p, i, l, stride1(v)) ? (@inbounds p[i], (i+stride1(v), l)) : nothing
end

function unsafe_convert(::Type{Ptr{T}}, V::SubArray{T,N,P,<:Tuple{Vararg{RangeIndex}}}) where {T,N,P}
return unsafe_convert(Ptr{T}, V.parent) + _memory_offset(V.parent, map(first, V.indices)...)
end
Expand Down

0 comments on commit bb84177

Please sign in to comment.