IntervalSets.jl
IntervalSets.jl copied to clipboard
`hash` definition is not correct
x-ref: https://github.com/JuliaMath/IntervalSets.jl/pull/28#issuecomment-1513607361
julia> using IntervalSets
julia> hash(1..2)
0x3932a21170706f90
julia> hash(OpenInterval(1,2)) # should be different
0x3932a21170706f90
julia> isequal(2..1, 3..1)
true
julia> unique([2..1, 3..1]) # should be 1-element
2-element Vector{ClosedInterval{Int64}}:
2 .. 1
3 .. 1
Good catch. There is a different definition of hash in DomainSets (which we should remove I suppose) which takes into account openness and so is correct for the first example, but it still gets the second one wrong.
How about:
hash(I::TypedEndpointsInterval, h::UInt) =
isempty(I) ? hash(_interval_hash, h) : hash(isleftopen(I), hash(isrightopen(I), hash(leftendpoint(I), hash(rightendpoint(I), hash(_interval_hash, h)))))
It seems a unique variable _interval_hash is already defined, so for empty intervals we can return that one. For other intervals the openness of the endpoints is included, compared to the current implementation.