Comment #0 by verylonglogin.reg — 2015-11-26T12:27:41Z
Currently the only documented requirement for a hash function is to return equal hashes for equal objects [1]. But it's common that hash function also should meet the following requirement for performance:
All hash bits must equally represent the object.
This requirement allows hash tables and other algorithms to cut any `k` bits. It really is a common situation: currently Druntime's associative arrays only use lowest bits of hash (by getting a modulo with hash table size which is a power of 2 [2]) for bucket index calculation and nothing prevents it from using highest bits instead because of e.g. some refactoring.
As a result even if an object is fully represented by an integer varying in a small range this integer can't be used as a hash as it has only few changing bits which may be cut by a hash table and this MUST be documented.
[1] http://dlang.org/hash-map.html
[2] https://github.com/D-Programming-Language/druntime/blob/893cc056bb717bea58c77bf960c4a006a1af079b/src/rt/aaA.d#L115
Comment #1 by verylonglogin.reg — 2015-11-26T12:48:44Z
See also enhancement Issue 15381 for requirement for algorithms to cut only highest bits from hashes.
Comment #2 by robert.schadek — 2024-12-15T15:23:02Z