Skip to content

Commit

Permalink
Restrict findin for ranges to integer ranges. Support ranges with ste…
Browse files Browse the repository at this point in the history
…p 0 in findin.
  • Loading branch information
GunnarFarneback committed May 15, 2013
1 parent d9c995c commit 82b6419
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function intersect(r::Ranges, s::Ranges...)
end

# findin (the index of intersection)
function findin(r::Ranges, span::Range1)
function findin{T1<:Integer, T2<:Integer}(r::Ranges{T1}, span::Range1{T2})
local ifirst
local ilast
fspan = first(span)
Expand All @@ -243,9 +243,12 @@ function findin(r::Ranges, span::Range1)
if sr > 0
ifirst = fr >= fspan ? 1 : iceil((fspan-fr)/sr)+1
ilast = lr <= lspan ? length(r) : length(r) - iceil((lr-lspan)/sr)
else
elseif sr < 0
ifirst = fr <= lspan ? 1 : iceil((lspan-fr)/sr)+1
ilast = lr >= fspan ? length(r) : length(r) - iceil((lr-fspan)/sr)
else
ifirst = fr >= fspan ? 1 : length(r)+1
ilast = fr <= lspan ? length(r) : 0
end
ifirst:ilast
end
Expand Down
6 changes: 6 additions & 0 deletions test/corelib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ let
r = 15:-2:-38
@test findin(r, span) == 1:6
end
@test isempty(findin(5+0*(1:6), 2:4))
@test findin(5+0*(1:6), 2:5) == 1:6
@test findin(5+0*(1:6), 2:7) == 1:6
@test findin(5+0*(1:6), 5:7) == 1:6
@test isempty(findin(5+0*(1:6), 6:7))
@test findin(5+0*(1:6), 5:5) == 1:6

@test intersect(1:5, 2:3) == 2:3
@test intersect(-3:5, 2:8) == 2:5
Expand Down

0 comments on commit 82b6419

Please sign in to comment.