IntervalSets.jl icon indicating copy to clipboard operation
IntervalSets.jl copied to clipboard

`hash` definition is not correct

Open hyrodium opened this issue 2 years ago • 2 comments

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

hyrodium avatar Nov 09 '23 07:11 hyrodium

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.

daanhb avatar Nov 09 '23 08:11 daanhb

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.

daanhb avatar Feb 16 '24 21:02 daanhb