Skip to content

Commit

Permalink
Add intersect(::AbstractRange, ::AbstractVector)
Browse files Browse the repository at this point in the history
Closes #41759
  • Loading branch information
barucden committed Aug 3, 2021
1 parent 4f28e6f commit b865814
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,13 @@ function intersect(r1::AbstractRange, r2::AbstractRange, r3::AbstractRange, r::A
i
end

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
intersect(vec::AbstractVector, r::AbstractRange) = intersect(r, vec)

# _findin (the index of intersection)
function _findin(r::AbstractRange{<:Integer}, span::AbstractUnitRange{<:Integer})
local ifirst
Expand Down
3 changes: 3 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ end
@test intersect(1:3, 2) === intersect(2, 1:3) === 2:2
@test intersect(1.0:3.0, 2) == intersect(2, 1.0:3.0) == [2.0]

@test intersect(1:typemax(Int), [1, 3]) === [1, 3]
@test intersect([1, 3], 1:typemax(Int)) === [1, 3]

@testset "Support StepRange with a non-numeric step" begin
start = Date(1914, 7, 28)
stop = Date(1918, 11, 11)
Expand Down

0 comments on commit b865814

Please sign in to comment.