Hashtable keys
Factor handbook > The language > Collections > Hashtables

Prev:>hashtable ( assoc -- hashtable )
Next:Hashtable utilities


Hashtables rely on the hashcode word to rapidly locate values associated with keys. The objects used as keys in a hashtable must obey certain restrictions.

The hashcode of a key is a function of its slot values, and if the hashcode changes then the hashtable will be left in an inconsistent state. The easiest way to avoid this problem is to never mutate objects used as hashtable keys.

In certain advanced applications, this cannot be avoided and the best design involves mutating hashtable keys. In this case, a custom hashcode* method must be defined which only depends on immutable slots.

In addition, the equal? and hashcode* methods must be congruent, and if one is defined the other should be defined also. This is documented in detail in the documentation for these respective words.
hashcode ( obj -- code )

hashcode* ( depth obj -- code )

identity-hashcode ( obj -- code )