I am parallelizing an implicit sparse matrix vector multiplication. This requires translating between block and matrix indices. The blocks are defined by UnitRange
. getindex(v::UnitRange{T}, i::Integer)
maps a block index to the corresponding matrix index. The converse (matrix to block) is less obvious but findfirst
works. To speed up that very special case, I added the specialized findfirst
method below:
julia> import Base: Fix2, findfirst
julia> findfirst(pred::Fix2{<:Union{typeof(isequal),typeof(==)}, T}, range::UnitRange{T}) where T <: Real = pred.x ∈ range ? firstindex(range) + pred.x - first(range) : nothing
findfirst (generic function with 9 methods)
julia> findfirst(==(7), 5:9) # 7 is the 3rd element of the 5:9 range
3
I love that Julia makes this possible but is it the most idiomatic approach? The standard library took a different approach with the CartesianIndices
-LinearIndices
duality.