I have a type whose instance is equivalent to instances that are in some distance away:
struct VectorWithEpsRadius{T}
value::T
eps::Float64
end
function ==(a::VectorWithEpsRadius, b::VectorWithEpsRadius)
return norm(a.value - b.value) <= a.eps
end
I would like to use this inside a Set. However the behavior is not what I expect:
f(x) = VectorWithEpsRadius{Int64}(x, 10)
Base.hash(m::VectorWithEpsRadius{T}, h::UInt) where T = Base.hash(m.value, h)
println(f(10) == f(11)) # true
println(hash(f(10)) == hash(f(11))) # false
and
julia> s = Set{ChaosTools.VectorWithEpsRadius{Int64}}()
Set{ChaosTools.VectorWithEpsRadius{Int64}}()
julia> push!(s, f(21))
Set{ChaosTools.VectorWithEpsRadius{Int64}} with 1 element:
ChaosTools.VectorWithEpsRadius{Int64}(21, 10.0)
julia> push!(s, f(22))
Set{ChaosTools.VectorWithEpsRadius{Int64}} with 2 elements:
ChaosTools.VectorWithEpsRadius{Int64}(22, 10.0)
ChaosTools.VectorWithEpsRadius{Int64}(21, 10.0)
I want the set to contain only the first element and not the second. I want those two elements to be considered equal.
How to solve this?
Thanks