Skip to content

Commit 7265921

Browse files
authored
Define hash methods (#47)
* Define hash methods * Hash using endpoints
1 parent 2c48722 commit 7265921

6 files changed

+57
-4
lines changed

src/endpoint.jl

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ RightEndpoint(ep::T, included::Bool) where T = RightEndpoint{T}(ep, included)
2626
LeftEndpoint(i::AbstractInterval{T}) where T = LeftEndpoint{T}(first(i), first(inclusivity(i)))
2727
RightEndpoint(i::AbstractInterval{T}) where T = RightEndpoint{T}(last(i), last(inclusivity(i)))
2828

29+
function Base.hash(x::Endpoint{T, D}, h::UInt) where {T, D}
30+
# Note: we shouldn't need to hash `T` as this is covered by the endpoint field.
31+
h = hash(:Endpoint, h)
32+
h = hash(D, h)
33+
h = hash(x.endpoint, h)
34+
h = hash(x.included, h)
35+
return h
36+
end
2937

3038
"""
3139
==(a::Endpoint, b::Endpoint) -> Bool

src/interval.jl

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ end
101101

102102
Base.copy(x::Interval{T}) where T = Interval{T}(x.first, x.last, x.inclusivity)
103103

104+
function Base.hash(interval::AbstractInterval, h::UInt)
105+
h = hash(LeftEndpoint(interval), h)
106+
h = hash(RightEndpoint(interval), h)
107+
return h
108+
end
109+
104110
##### ACCESSORS #####
105111

106112
Base.first(interval::Interval) = interval.first

test/anchoredinterval.jl

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ using Intervals: canonicalize
3636
@test AnchoredInterval{25}('a') isa AnchoredInterval
3737
end
3838

39+
@testset "hash" begin
40+
# Need a complicated enough element type for this test to ever fail
41+
zdt = now(tz"Europe/London")
42+
a = HE(zdt)
43+
b = deepcopy(a)
44+
@test hash(a) == hash(b)
45+
end
46+
3947
@testset "conversion" begin
4048
he = HourEnding(dt)
4149
hb = HourBeginning(dt)

test/comparisons.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
215215
b = convert(B, Interval(1, 5, false, false))
216216

217217
@test a == b
218-
@test A != B || isequal(a, b)
219-
@test A != B || hash(a) == hash(b)
218+
@test isequal(a, b)
219+
@test hash(a) == hash(b)
220220

221221
@test !isless(a, b)
222222
@test !isless(a, b)
@@ -383,8 +383,8 @@ const INTERVAL_TYPES = [Interval, AnchoredInterval{Ending}, AnchoredInterval{Beg
383383
b = convert(B, Interval(1, 5, true, true))
384384

385385
@test a == b
386-
@test A != B || isequal(a, b)
387-
@test A != B || hash(a) == hash(b)
386+
@test isequal(a, b)
387+
@test hash(a) == hash(b)
388388

389389
@test !isless(a, b)
390390
@test !isless(b, a)

test/endpoint.jl

+23
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,27 @@ using Intervals: LeftEndpoint, RightEndpoint
206206
@test RightEndpoint(1, false) != LeftEndpoint(1.0, true)
207207
@test RightEndpoint(1, true) == LeftEndpoint(1.0, true)
208208
end
209+
210+
@testset "hash" begin
211+
# Need a complicated enough element type for this test to possibly fail. Using a
212+
# ZonedDateTime with a VariableTimeZone should do the trick.
213+
a = now(tz"Europe/London")
214+
b = deepcopy(a)
215+
@test hash(a) == hash(b) # Double check
216+
217+
@test hash(LeftEndpoint(a, false)) == hash(LeftEndpoint(b, false))
218+
@test hash(LeftEndpoint(a, true)) != hash(LeftEndpoint(b, false))
219+
@test hash(LeftEndpoint(a, false)) != hash(LeftEndpoint(b, true))
220+
@test hash(LeftEndpoint(a, true)) == hash(LeftEndpoint(b, true))
221+
222+
@test hash(RightEndpoint(a, false)) == hash(RightEndpoint(b, false))
223+
@test hash(RightEndpoint(a, true)) != hash(RightEndpoint(b, false))
224+
@test hash(RightEndpoint(a, false)) != hash(RightEndpoint(b, true))
225+
@test hash(RightEndpoint(a, true)) == hash(RightEndpoint(b, true))
226+
227+
@test hash(LeftEndpoint(a, false)) != hash(RightEndpoint(b, false))
228+
@test hash(LeftEndpoint(a, true)) != hash(RightEndpoint(b, false))
229+
@test hash(LeftEndpoint(a, false)) != hash(RightEndpoint(b, true))
230+
@test hash(LeftEndpoint(a, true)) != hash(RightEndpoint(b, true))
231+
end
209232
end

test/interval.jl

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
@test_throws MethodError Interval(1, 2, 3)
3838
end
3939

40+
@testset "hash" begin
41+
# Need a complicated enough element type for this test to ever fail
42+
zdt = now(tz"Europe/London")
43+
a = Interval(zdt, zdt)
44+
b = deepcopy(a)
45+
@test hash(a) == hash(b)
46+
end
47+
4048
@testset "conversion" begin
4149
@test_throws DomainError convert(Int, Interval(10, 10, Inclusivity(false, false)))
4250
@test_throws DomainError convert(Int, Interval(10, 10, Inclusivity(false, true)))

0 commit comments

Comments
 (0)