Improve hash function in the same manner as was done for the standard library
This commit is contained in:
parent
ac4c3c6018
commit
0841abeaf2
|
@ -18,8 +18,14 @@ extension _Hash {
|
||||||
///
|
///
|
||||||
/// [ref]: http://goanna.cs.rmit.edu.au/~jz/fulltext/jasist-tch.pdf
|
/// [ref]: http://goanna.cs.rmit.edu.au/~jz/fulltext/jasist-tch.pdf
|
||||||
internal static func _combine(seed: Int = 0, _ values: AnyHashable...) -> Int {
|
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)
|
var x = UInt(bitPattern: seed)
|
||||||
let magic = 0x9e3779b9 as UInt // Based on the golden ratio.
|
|
||||||
for v in values {
|
for v in values {
|
||||||
x ^= UInt(bitPattern: v.hashValue) &+ magic &+ (x &<< 6) &+ (x &>> 2)
|
x ^= UInt(bitPattern: v.hashValue) &+ magic &+ (x &<< 6) &+ (x &>> 2)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue