Skip to content

Instantly share code, notes, and snippets.

@haldun
Created April 6, 2016 09:13
Show Gist options
  • Save haldun/96c632225c2b1cd7beb03132165c967b to your computer and use it in GitHub Desktop.
Save haldun/96c632225c2b1cd7beb03132165c967b to your computer and use it in GitHub Desktop.
Generic hash value computation for swift objects
func hashValue<T: Hashable>(objects: T?...) -> Int {
var result = 31
for object in objects {
result = (result &* 17) &+ object.hashValue
}
return result
}
func hashValue<T: Hashable>(objects: T...) -> Int {
var result = 31
for object in objects {
result = (result &* 17) &+ object.hashValue
}
return result
}
extension Optional where Wrapped: Hashable {
var hashValue: Int {
switch self {
case .None: return 0
case .Some(let object): return object.hashValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment