-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPair.swift
52 lines (43 loc) · 1.1 KB
/
Pair.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Pair.swift
//
// Advent of Code Tools
//
public struct Pair<T, U> {
public let first: T
public let second: U
public init(_ first: T, _ second: U) {
self.first = first
self.second = second
}
public init(_ value: (T, U)) {
self.first = value.0
self.second = value.1
}
public var tuple: (T, U) { (first, second) }
}
extension Pair: Equatable where T: Equatable, U: Equatable {}
extension Pair: Hashable where T: Hashable, U: Hashable {}
extension Pair: Comparable where T: Comparable, U: Comparable {
public static func < (lhs: Pair, rhs: Pair) -> Bool {
if lhs.first != rhs.first {
return lhs.first < rhs.first
}
return lhs.second < rhs.second
}
}
extension Pair: CustomStringConvertible {
public var description: String {
"\(first),\(second)"
}
}
extension Pair where T == U {
public var array: [T] {
[first, second]
}
}
extension Dictionary {
init(pairs: [Pair<Key, Value>]) {
self.init(uniqueKeysWithValues: zip(pairs.map(\.first), pairs.map(\.second)))
}
}