1
1
mirror of https://github.com/github/semantic.git synced 2024-12-03 13:56:04 +03:00
semantic/prototype/Doubt/AnyHashable.swift

30 lines
678 B
Swift
Raw Normal View History

2015-10-01 21:25:17 +03:00
/// A Hashable instance for any type for which you can provide an equality function and a hash function.
2015-10-01 16:59:26 +03:00
public enum AnyHashable<A>: Hashable {
2015-10-01 17:32:44 +03:00
public init(_ value: A, equals: (A, A) -> Bool, hash: A -> Int) {
self = .External(.External(value, equals), hash)
}
case External(AnyEquatable<A>, A -> Int)
2015-10-01 16:59:26 +03:00
2015-10-01 17:28:24 +03:00
public var value: A {
switch self {
case let .External(a, _):
return a.value
2015-10-01 17:28:24 +03:00
}
}
2015-10-01 16:59:26 +03:00
public var hashValue: Int {
switch self {
case let .External(a, hash):
return hash(a.value)
2015-10-01 16:59:26 +03:00
}
}
}
public func == <A> (left: AnyHashable<A>, right: AnyHashable<A>) -> Bool {
switch (left, right) {
case let (.External(a, _), .External(b, _)):
return a == b
2015-10-01 16:59:26 +03:00
}
}