@@ -16,7 +16,7 @@ function searchsortednearest(vec::AbstractVector, x)
1616 return idx
1717end
1818# Base only specializes searching ranges by Numbers; so optimize for Intervals
19- function Base. searchsorted (a:: Range , I:: ClosedInterval )
19+ function Base. searchsorted (a:: AbstractRange , I:: ClosedInterval )
2020 searchsortedfirst (a, I. left): searchsortedlast (a, I. right)
2121end
2222
@@ -27,8 +27,9 @@ sufficient since it can be turned off by `--check-bounds=yes`.
2727"""
2828module Extrapolated
2929using .. ClosedInterval
30+ using Compat: AbstractRange
3031
31- function searchsortednearest (vec:: Range , x)
32+ function searchsortednearest (vec:: AbstractRange , x)
3233 idx = searchsortedfirst (vec, x) # Returns the first idx | vec[idx] >= x
3334 if (getindex (vec, idx) - x) > (x - getindex (vec, idx- 1 ))
3435 idx -= 1 # The previous element is closer
@@ -37,25 +38,25 @@ function searchsortednearest(vec::Range, x)
3738end
3839
3940"""
40- searchsorted(a::Range , I::ClosedInterval)
41+ searchsorted(a::AbstractRange , I::ClosedInterval)
4142
4243Return the indices of the range that fall within an interval without checking
4344bounds, possibly extrapolating outside the range if needed.
4445"""
45- function searchsorted (a:: Range , I:: ClosedInterval )
46+ function searchsorted (a:: AbstractRange , I:: ClosedInterval )
4647 searchsortedfirst (a, I. left): searchsortedlast (a, I. right)
4748end
4849
4950# When running with `--check-bounds=yes` (like on Travis), the bounds-check isn't elided
50- @inline function getindex (v:: Range {T} , i:: Integer ) where T
51+ @inline function getindex (v:: AbstractRange {T} , i:: Integer ) where T
5152 convert (T, first (v) + (i- 1 )* step (v))
5253end
53- @inline function getindex (r:: Range , s:: Range {<:Integer} )
54+ @inline function getindex (r:: AbstractRange , s:: AbstractRange {<:Integer} )
5455 f = first (r)
5556 st = oftype (f, f + (first (s)- 1 )* step (r))
5657 range (st, step (r)* step (s), length (s))
5758end
58- getindex (r:: Range , I:: Array ) = [getindex (r, i) for i in I]
59+ getindex (r:: AbstractRange , I:: Array ) = [getindex (r, i) for i in I]
5960@inline getindex (r:: StepRangeLen , i:: Integer ) = Base. unsafe_getindex (r, i)
6061@inline function getindex (r:: StepRangeLen , s:: AbstractUnitRange )
6162 soffset = 1 + (r. offset - first (s))
@@ -68,29 +69,29 @@ getindex(r::Range, I::Array) = [getindex(r, i) for i in I]
6869 end
6970end
7071
71- function searchsortedlast (a:: Range , x)
72+ function searchsortedlast (a:: AbstractRange , x)
7273 step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
7374 n = round (Integer,(x- first (a))/ step (a))+ 1
7475 isless (x, getindex (a, n)) ? n- 1 : n
7576end
76- function searchsortedfirst (a:: Range , x)
77+ function searchsortedfirst (a:: AbstractRange , x)
7778 step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
7879 n = round (Integer,(x- first (a))/ step (a))+ 1
7980 isless (getindex (a, n), x) ? n+ 1 : n
8081end
81- function searchsortedlast (a:: Range {<:Integer} , x)
82+ function searchsortedlast (a:: AbstractRange {<:Integer} , x)
8283 step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
8384 fld (floor (Integer,x)- first (a),step (a))+ 1
8485end
85- function searchsortedfirst (a:: Range {<:Integer} , x)
86+ function searchsortedfirst (a:: AbstractRange {<:Integer} , x)
8687 step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
8788 - fld (floor (Integer,- x)+ first (a),step (a))+ 1
8889end
89- function searchsortedfirst (a:: Range {<:Integer} , x:: Unsigned )
90+ function searchsortedfirst (a:: AbstractRange {<:Integer} , x:: Unsigned )
9091 step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
9192 - fld (first (a)- signed (x),step (a))+ 1
9293end
93- function searchsortedlast (a:: Range {<:Integer} , x:: Unsigned )
94+ function searchsortedlast (a:: AbstractRange {<:Integer} , x:: Unsigned )
9495 step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
9596 fld (signed (x)- first (a),step (a))+ 1
9697end
0 commit comments