mirror of
https://github.com/github/semantic.git
synced 2024-12-01 17:59:10 +03:00
42 lines
820 B
Swift
42 lines
820 B
Swift
|
public enum Hash: Hashable {
|
|||
|
case Sequence([Hash])
|
|||
|
case String(Swift.String)
|
|||
|
case Int(Swift.Int)
|
|||
|
|
|||
|
|
|||
|
public var hashValue: Swift.Int {
|
|||
|
switch self {
|
|||
|
case let .Sequence(s):
|
|||
|
// Bob Jenkins’ one-at-a-time hash: https://en.wikipedia.org/wiki/Jenkins_hash_function
|
|||
|
var hash = 0
|
|||
|
for each in s {
|
|||
|
hash += each.hashValue
|
|||
|
hash += hash << 10
|
|||
|
hash ^= hash >> 6
|
|||
|
}
|
|||
|
hash += hash << 3
|
|||
|
hash ^= hash >> 11
|
|||
|
hash += hash << 15
|
|||
|
return hash
|
|||
|
case let .String(s):
|
|||
|
return s.hashValue
|
|||
|
case let .Int(i):
|
|||
|
return i.hashValue
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public func == (left: Hash, right: Hash) -> Bool {
|
|||
|
switch (left, right) {
|
|||
|
case let (.Sequence(a), .Sequence(b)):
|
|||
|
return a == b
|
|||
|
case let (.String(a), .String(b)):
|
|||
|
return a == b
|
|||
|
case let (.Int(a), .Int(b)):
|
|||
|
return a == b
|
|||
|
default:
|
|||
|
return false
|
|||
|
}
|
|||
|
}
|
|||
|
|