Hi everyone : )
type
ComponentPoint* = object
point: int
id: int
## I want to calculate hash by only one variable
proc hash(x: ComponentPoint): Hash =
result = x.id
var pp = ComponentPoint()
pp.id = 1000
pp.point = 1000
var pp2 = ComponentPoint()
pp2.id = 1000
pp2.point = 0
var hset= initHashSet[ComponentPoint](512)
hset.incl(pp)
# I suppose it must be true as the hashes are equal, but it's false
assert pp2 in sss == true
What's wrong with my code? ( Aside of primitive hash proc ) the thing is that is I set pp2.point to 1000 it gives the true. But Then I don't understand how things work under the hood. I thought that there is hash check and it must be valid.
hash and == must agree. So, you need:
proc `==`(a, b: ComponentPoint): bool =
a.id == b.id
Hello again !
A hash set is pretty much a hash table where the key and value types are the same.
So, checking for the existence of an element in a hash set will do the following :
This explains the results you had with that piece of code.