@@ -16,7 +16,7 @@ function searchsortednearest(vec::AbstractVector, x)
16
16
return idx
17
17
end
18
18
# 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 )
20
20
searchsortedfirst (a, I. left): searchsortedlast (a, I. right)
21
21
end
22
22
@@ -27,8 +27,9 @@ sufficient since it can be turned off by `--check-bounds=yes`.
27
27
"""
28
28
module Extrapolated
29
29
using .. ClosedInterval
30
+ using Compat: AbstractRange
30
31
31
- function searchsortednearest (vec:: Range , x)
32
+ function searchsortednearest (vec:: AbstractRange , x)
32
33
idx = searchsortedfirst (vec, x) # Returns the first idx | vec[idx] >= x
33
34
if (getindex (vec, idx) - x) > (x - getindex (vec, idx- 1 ))
34
35
idx -= 1 # The previous element is closer
@@ -37,25 +38,25 @@ function searchsortednearest(vec::Range, x)
37
38
end
38
39
39
40
"""
40
- searchsorted(a::Range , I::ClosedInterval)
41
+ searchsorted(a::AbstractRange , I::ClosedInterval)
41
42
42
43
Return the indices of the range that fall within an interval without checking
43
44
bounds, possibly extrapolating outside the range if needed.
44
45
"""
45
- function searchsorted (a:: Range , I:: ClosedInterval )
46
+ function searchsorted (a:: AbstractRange , I:: ClosedInterval )
46
47
searchsortedfirst (a, I. left): searchsortedlast (a, I. right)
47
48
end
48
49
49
50
# 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
51
52
convert (T, first (v) + (i- 1 )* step (v))
52
53
end
53
- @inline function getindex (r:: Range , s:: Range {<:Integer} )
54
+ @inline function getindex (r:: AbstractRange , s:: AbstractRange {<:Integer} )
54
55
f = first (r)
55
56
st = oftype (f, f + (first (s)- 1 )* step (r))
56
57
range (st, step (r)* step (s), length (s))
57
58
end
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]
59
60
@inline getindex (r:: StepRangeLen , i:: Integer ) = Base. unsafe_getindex (r, i)
60
61
@inline function getindex (r:: StepRangeLen , s:: AbstractUnitRange )
61
62
soffset = 1 + (r. offset - first (s))
@@ -68,29 +69,29 @@ getindex(r::Range, I::Array) = [getindex(r, i) for i in I]
68
69
end
69
70
end
70
71
71
- function searchsortedlast (a:: Range , x)
72
+ function searchsortedlast (a:: AbstractRange , x)
72
73
step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
73
74
n = round (Integer,(x- first (a))/ step (a))+ 1
74
75
isless (x, getindex (a, n)) ? n- 1 : n
75
76
end
76
- function searchsortedfirst (a:: Range , x)
77
+ function searchsortedfirst (a:: AbstractRange , x)
77
78
step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
78
79
n = round (Integer,(x- first (a))/ step (a))+ 1
79
80
isless (getindex (a, n), x) ? n+ 1 : n
80
81
end
81
- function searchsortedlast (a:: Range {<:Integer} , x)
82
+ function searchsortedlast (a:: AbstractRange {<:Integer} , x)
82
83
step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
83
84
fld (floor (Integer,x)- first (a),step (a))+ 1
84
85
end
85
- function searchsortedfirst (a:: Range {<:Integer} , x)
86
+ function searchsortedfirst (a:: AbstractRange {<:Integer} , x)
86
87
step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
87
88
- fld (floor (Integer,- x)+ first (a),step (a))+ 1
88
89
end
89
- function searchsortedfirst (a:: Range {<:Integer} , x:: Unsigned )
90
+ function searchsortedfirst (a:: AbstractRange {<:Integer} , x:: Unsigned )
90
91
step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
91
92
- fld (first (a)- signed (x),step (a))+ 1
92
93
end
93
- function searchsortedlast (a:: Range {<:Integer} , x:: Unsigned )
94
+ function searchsortedlast (a:: AbstractRange {<:Integer} , x:: Unsigned )
94
95
step (a) == 0 && throw (ArgumentError (" ranges with a zero step are unsupported" ))
95
96
fld (signed (x)- first (a),step (a))+ 1
96
97
end
0 commit comments