-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
intersect
could be more efficient for mixtures of UnitRange and Vector
#41759
Comments
Would this be viable function intersect(r::AbstractRange, vec::AbstractVector)
common = Iterators.filter(x -> x ∈ r, vec)
seen = Set{eltype(vec)}(common)
return vectorfilter(_shrink_filter!(seen), common)
end ? |
Nice. Looks good to me! It's more efficient than the vec vec version julia> @btime intersect(1:typemax(Int), [1,3])
302.226 ns (8 allocations: 688 bytes)
2-element Vector{Int64}:
1
3
julia> @btime Base.intersect([1,2,3,4,5], [1,3])
654.689 ns (15 allocations: 1.11 KiB)
2-element Vector{Int64}:
1
3
julia> @btime Base.intersect(1:typemax(Int), 1:2:3)
0.044 ns (0 allocations: 0 bytes)
1:2:3 I'd also define intersect(vec::AbstractVector, r::AbstractRange) = intersect(r, vec) Could you PR this? I'd like to use this in another Base PR. Thanks! |
11 tasks
barucden
added a commit
to barucden/julia
that referenced
this issue
Aug 3, 2021
barucden
added a commit
to barucden/julia
that referenced
this issue
Aug 3, 2021
barucden
added a commit
to barucden/julia
that referenced
this issue
Aug 4, 2021
barucden
added a commit
to barucden/julia
that referenced
this issue
Aug 18, 2021
barucden
added a commit
to barucden/julia
that referenced
this issue
Aug 27, 2021
Also adds: - `intersect(::AbstractRange, ::AbstractRange)` - `intersect(::AbstractRange, ::AbstractRange)` Closes JuliaLang#41759 Co-authored-by: Ian Butterworth <[email protected]> Co-authored-by: Jeff Bezanson <[email protected]>
barucden
added a commit
to barucden/julia
that referenced
this issue
Aug 27, 2021
Also adds: - `intersect(::AbstractRange, ::AbstractRange)` - `intersect(::AbstractRange, ::AbstractRange)` Closes JuliaLang#41759 Co-authored-by: Ian Butterworth <[email protected]> Co-authored-by: Jeff Bezanson <[email protected]>
barucden
added a commit
to barucden/julia
that referenced
this issue
Aug 28, 2021
Also adds: - `intersect(::AbstractRange, ::AbstractRange)` - `intersect(::AbstractRange, ::AbstractRange)` Closes JuliaLang#41759 Co-authored-by: Ian Butterworth <[email protected]> Co-authored-by: Jeff Bezanson <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems like it should be possible to do this without collecting the full range
The text was updated successfully, but these errors were encountered: