Created
April 6, 2016 09:13
-
-
Save haldun/96c632225c2b1cd7beb03132165c967b to your computer and use it in GitHub Desktop.
Generic hash value computation for swift objects
This file contains 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
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