pnalysz
September 6, 2019, 8:34am
#1
As far as I see at the moment julia uses the same findfirst method for all AbstractArray which simply iterates through all the items. Would it not make sense to have specialized method for ranges that would find the item’s index based on start/step/stop?
Thanks,
Piotr
1 Like
There is a method for StepRange
but not for UnitRange
:
for (i, a) in pairs(A)
testf(a) && return i
end
return nothing
end
# Needed for bootstrap, and allows defining only an optimized findnext method
findfirst(testf::Function, A::Union{AbstractArray, AbstractString}) =
findnext(testf, A, first(keys(A)))
function findfirst(p::Union{Fix2{typeof(isequal),T},Fix2{typeof(==),T}}, r::StepRange{T,S}) where {T,S}
first(r) <= p.x <= last(r) || return nothing
d = convert(S, p.x - first(r))
iszero(d % step(r)) || return nothing
return d ÷ step(r) + 1
end
"""
findprev(A, i)
Find the previous index before or including `i` of a `true` element of `A`,
so yes, it could make sense. If someone is interested, just make a PR.
pnalysz
September 6, 2019, 9:10am
#3
Thank you, I overlooked it. Actually StepRange is enough for me.