Skip to content

Commit a93bdce

Browse files
authored
support units in findall (#84)
1 parent 7d6042f commit a93bdce

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ julia = "0.7, 1"
1515
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1616
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1717
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18+
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
1819

1920
[targets]
20-
test = ["Test", "Random", "OffsetArrays"]
21+
test = ["Test", "Random", "OffsetArrays", "Unitful"]

src/findall.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function Base.findall(interval_d::Base.Fix2{typeof(in),Interval{L,R,T}}, x::Abst
4040
interval = interval_d.x
4141
il, ir = firstindex(x), lastindex(x)
4242
δx = step(x)
43-
a,b = if δx < 0
43+
a,b = if δx < zero(δx)
4444
rev = findall(in(interval), reverse(x))
4545
isempty(rev) && return rev
4646

@@ -50,8 +50,8 @@ function Base.findall(interval_d::Base.Fix2{typeof(in),Interval{L,R,T}}, x::Abst
5050
a,b
5151
else
5252
lx, rx = first(x), last(x)
53-
l = max(leftendpoint(interval), lx-1)
54-
r = min(rightendpoint(interval), rx+1)
53+
l = max(leftendpoint(interval), lx - oneunit(δx))
54+
r = min(rightendpoint(interval), rx + oneunit(δx))
5555

5656
(l > rx || r < lx) && return 1:0
5757

test/findall.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using OffsetArrays
2+
using Unitful
23

34
# Helper function to test that findall(in(interval), x) works. By
45
# default, a reference is generated using the general algorithm,
@@ -124,4 +125,30 @@ end
124125
assert_in_interval(reverse(x), interval)
125126
end
126127
end
128+
129+
@testset "Units, dates" begin
130+
for (x, interval) in [
131+
[
132+
([-2u"m", 3u"m"], -1u"m"..1u"m"),
133+
([-2u"m", 0u"m", 1u"m"], -1u"m"..1u"m"),
134+
(-2u"m":10u"m":50u"m", -1u"m"..1u"m"),
135+
(-2u"m":1u"m":1u"m", -1u"m"..1u"m"),
136+
(-2u"km":1u"cm":1u"km", -1u"m"..1u"m"),
137+
(-2u"km":1u"cm":1u"km", -1u"m"..1u"km"),
138+
(-2u"km":0.1u"m":1u"km", -1u"m"..1u"km"),
139+
(-2u"m":0.1u"m":1u"m", -1.05u"m"..1u"km"),
140+
(-2u"m":0.1u"m":1u"m", -4u"m"..1u"km"),
141+
(-2u"m":0.1u"m":1u"m", -4.05u"m"..1u"km"),
142+
];
143+
VERSION >= v"1.4" ? [
144+
(Date(2021, 1, 1):Day(1):Date(2021, 3, 1), Date(2020, 1, 11)..Date(2020, 2, 22)),
145+
(Date(2021, 1, 1):Day(1):Date(2021, 3, 1), Date(2021, 1, 11)..Date(2021, 2, 22)),
146+
(DateTime(2021, 1, 1):Millisecond(10000):DateTime(2021, 3, 1), DateTime(2020, 1, 11)..DateTime(2020, 2, 22)),
147+
(DateTime(2021, 1, 1):Millisecond(10000):DateTime(2021, 3, 1), DateTime(2021, 1, 11)..DateTime(2021, 2, 22)),
148+
] : [];
149+
]
150+
assert_in_interval(x, interval)
151+
assert_in_interval(reverse(x), interval)
152+
end
153+
end
127154
end

0 commit comments

Comments
 (0)