6.19 Hash functions

For an object to be used as a key in a hashmap (see Maps) or a member of a hashset (see Sets), it must have a method int hash() that returns a nonnegative integer. For correctness, this method must return the same value for two objects of the same type that are equal according to the == operator. For efficiency, it should behave like a pseudorandom function, so that the hash values of distinct objects are unrelated. Also note that it is not necessary for hash functions to be consistent across different runs of the program; a.hash() == a.hash() should always be true, but write(a.hash()) may produce a different output each time the program is run.

In Asymptote, the hash method is defined for three of the builtin types: int, real, and string. Note that things like 3.hash() and 'hello'.hash() are not valid syntax; the correct version of these would be (3).hash() and ('hello').hash(), with parentheses around the literal. If you want to hash a real, there are two important things to note: first, even if two reals are extremely close, their hash values will almost certainly not be; second, the same number will almost certainly have different hash values when hashed as an int versus a real.