Search code examples
c++unordered-set

Will I guaranteedly not get collisions with different hashes values in `unordered_set` if I specify the min buckets size in constructor?


So I constructed my unordered_set passing 512 as min buckets, i.e. the n parameter. My hash function will always return a value in the range [0,511].

My question is, may I still get collision between two values which the hashes are not the same? Just to make it clearer, I tolerate any collision regarding values with the same hash, but I may not get collision with different hashes values.


Solution

  • Any sensible implementation would implement bucket(k) as hasher(k) % bucket_count(), in which case you won't get collisions from values with different hashes if the hash range is no larger than bucket_count().

    However, there's no guarantee of this; only that equal hash values map to the same bucket. A bad implementation could (for example) ignore one of the buckets and still meet the container requirements; in which case, you would get collisions.

    If your program's correctness relies on different hash values ending up in different buckets, then you'll have to either check the particular implementation you're using (perhaps writing a test for this behaviour), or implement your own container satisfying your requirements.