Last active
November 3, 2017 19:12
-
-
Save ddddxxx/b1f6c3b5e39a6f3f908b1b2a44f58d18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func hash_combine(seed: inout Int, value: Int) { | |
if MemoryLayout<Int>.size == 64 { | |
// 64 bit hash_combine | |
let mul: UInt64 = 0x9ddfea08eb382d69 | |
let s = UInt64(bitPattern: Int64(seed)) | |
let v = UInt64(bitPattern: Int64(value)) | |
var a = (v ^ s) &* mul | |
a ^= (a >> 47) | |
var b = (s ^ a) &* mul | |
b ^= (b >> 47) | |
let result = b &* mul | |
seed = Int(Int64(bitPattern: result)) | |
} else if MemoryLayout<Int>.size == 32 { | |
// 32 bit hash_combine | |
var s = UInt32(bitPattern: Int32(seed)) | |
let v = UInt32(bitPattern: Int32(value)) | |
s ^= v &+ 0x9e3779b9 &+ (s << 6) &+ (s >> 2) | |
seed = Int(Int32(bitPattern: s)) | |
} else { | |
fatalError() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment