/
find.jl
49 lines (40 loc) · 879 Bytes
/
find.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
index(a, x)
Locate the leftmost value exactly equal to `x` in `a`.
"""
function index(a, x)
i = bisect_left(a, x)
return i != length(a) + 1 && a[i] == x ? i : nothing
end
"""
find_lt(a, x)
Find rightmost value less than `x` in `a`.
"""
function find_lt(a, x)
i = bisect_left(a, x)
return i > 1 ? a[i-1] : nothing
end
"""
find_le(a, x)
Find rightmost value less than or equal to `x` in `a`.
"""
function find_le(a, x)
i = bisect_right(a, x)
return i > 1 ? a[i-1] : nothing
end
"""
find_gt(a, x)
Find leftmost value greater than `x` in `a`.
"""
function find_gt(a, x)
i = bisect_right(a, x)
return i != length(a) + 1 ? a[i] : nothing
end
"""
find_ge(a, x)
Find leftmost item greater than or equal to `x` in `a`.
"""
function find_ge(a, x)
i = bisect_left(a, x)
return i != length(a) + 1 ? a[i] : nothing
end