Skip to content

Instantly share code, notes, and snippets.

@ddddxxx
Last active November 3, 2017 19:12
Show Gist options
  • Save ddddxxx/b1f6c3b5e39a6f3f908b1b2a44f58d18 to your computer and use it in GitHub Desktop.
Save ddddxxx/b1f6c3b5e39a6f3f908b1b2a44f58d18 to your computer and use it in GitHub Desktop.
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