diff options
| author | bors <bors@rust-lang.org> | 2015-01-04 21:36:36 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-01-04 21:36:36 +0000 |
| commit | 05abc65b993d7bc25e50678cf1d6b522f6e75804 (patch) | |
| tree | de6bebbd9c3d0a8fa9f04e4babb63a7677c8c2ad /src/libstd | |
| parent | 56795ad8c39a628960f242e88e045a0ea9b6a473 (diff) | |
| parent | 28cca28e623c1ac5a2a3e7dcdd31b3a90552c9eb (diff) | |
| download | rust-05abc65b993d7bc25e50678cf1d6b522f6e75804.tar.gz rust-05abc65b993d7bc25e50678cf1d6b522f6e75804.zip | |
Merge pull request #20464 from ranma42/improve-make-hash
Improve `make_hash` function Reviewed-by: Gankro, Gankro
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/table.rs | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 7c87094805d..ab91beb4f9b 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -1,4 +1,4 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -139,13 +139,11 @@ impl SafeHash { /// This function wraps up `hash_keyed` to be the only way outside this /// module to generate a SafeHash. pub fn make_hash<Sized? T: Hash<S>, S, H: Hasher<S>>(hasher: &H, t: &T) -> SafeHash { - match hasher.hash(t) { - // This constant is exceedingly likely to hash to the same - // bucket, but it won't be counted as empty! Just so we can maintain - // our precious uniform distribution of initial indexes. - EMPTY_BUCKET => SafeHash { hash: 0x8000_0000_0000_0000 }, - h => SafeHash { hash: h }, - } + // We need to avoid 0u64 in order to prevent collisions with + // EMPTY_HASH. We can maintain our precious uniform distribution + // of initial indexes by unconditionally setting the MSB, + // effectively reducing 64-bits hashes to 63 bits. + SafeHash { hash: 0x8000_0000_0000_0000 | hasher.hash(t) } } // `replace` casts a `*u64` to a `*SafeHash`. Since we statically |
