Improve hash function in the same manner as was done for the standard library

This commit is contained in:
Xiaodi Wu 2018-02-09 22:53:50 -06:00
parent ac4c3c6018
commit 0841abeaf2
1 changed files with 7 additions and 1 deletions

View File

@ -18,8 +18,14 @@ extension _Hash {
///
/// [ref]: http://goanna.cs.rmit.edu.au/~jz/fulltext/jasist-tch.pdf
internal static func _combine(seed: Int = 0, _ values: AnyHashable...) -> Int {
// Use a magic number based on the golden ratio
// (0x1.9e3779b97f4a7c15f39cc0605cedc8341082276bf3a27251f86c6a11d0c18e95p0).
#if arch(i386) || arch(arm)
let magic = 0x9e3779b9 as UInt
#else
let magic = 0x9e3779b97f4a7c15 as UInt
#endif
var x = UInt(bitPattern: seed)
let magic = 0x9e3779b9 as UInt // Based on the golden ratio.
for v in values {
x ^= UInt(bitPattern: v.hashValue) &+ magic &+ (x &<< 6) &+ (x &>> 2)
}