-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNonEmpty+SetAlgebra.swift
More file actions
197 lines (162 loc) · 4.92 KB
/
NonEmpty+SetAlgebra.swift
File metadata and controls
197 lines (162 loc) · 4.92 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
public typealias NonEmptySet<Element> = NonEmpty<Set<Element>> where Element: Hashable
// NB: `NonEmpty` does not conditionally conform to `SetAlgebra` because it contains destructive methods.
extension NonEmpty where Collection: SetAlgebra, Collection.Element: Hashable {
public init(_ head: Collection.Element, _ tail: Collection.Element...) {
var tail = Collection(tail)
tail.insert(head)
self.init(rawValue: tail)!
}
public init?<S>(_ elements: S) where S: Sequence, Collection.Element == S.Element {
self.init(rawValue: Collection(elements))
}
@inlinable
public func contains(_ member: Collection.Element) -> Bool {
self.rawValue.contains(member)
}
@_disfavoredOverload
@inlinable
public func union(_ other: NonEmpty) -> NonEmpty {
var copy = self
copy.formUnion(other)
return copy
}
@inlinable
public func union(_ other: Collection) -> NonEmpty {
var copy = self
copy.formUnion(other)
return copy
}
@_disfavoredOverload
@inlinable
public func intersection(_ other: NonEmpty) -> Collection {
self.rawValue.intersection(other.rawValue)
}
@inlinable
public func intersection(_ other: Collection) -> Collection {
self.rawValue.intersection(other)
}
@_disfavoredOverload
@inlinable
public func symmetricDifference(_ other: NonEmpty) -> Collection {
self.rawValue.symmetricDifference(other.rawValue)
}
@inlinable
public func symmetricDifference(_ other: Collection) -> Collection {
self.rawValue.symmetricDifference(other)
}
@discardableResult
public mutating func insert(_ newMember: Collection.Element) -> (
inserted: Bool, memberAfterInsert: Collection.Element
) {
self.rawValue.insert(newMember)
}
@discardableResult
public mutating func update(with newMember: Collection.Element) -> Collection.Element? {
self.rawValue.update(with: newMember)
}
@_disfavoredOverload
public mutating func formUnion(_ other: NonEmpty) {
self.rawValue.formUnion(other.rawValue)
}
public mutating func formUnion(_ other: Collection) {
self.rawValue.formUnion(other)
}
@_disfavoredOverload
@inlinable
public func subtracting(_ other: NonEmpty) -> Collection {
self.rawValue.subtracting(other.rawValue)
}
@inlinable
public func subtracting(_ other: Collection) -> Collection {
self.rawValue.subtracting(other)
}
@_disfavoredOverload
@inlinable
public func isDisjoint(with other: NonEmpty) -> Bool {
self.rawValue.isDisjoint(with: other.rawValue)
}
@inlinable
public func isDisjoint(with other: Collection) -> Bool {
self.rawValue.isDisjoint(with: other)
}
@_disfavoredOverload
@inlinable
public func isSubset(of other: NonEmpty) -> Bool {
self.rawValue.isSubset(of: other.rawValue)
}
@inlinable
public func isSubset(of other: Collection) -> Bool {
self.rawValue.isSubset(of: other)
}
@_disfavoredOverload
@inlinable
public func isSuperset(of other: NonEmpty) -> Bool {
self.rawValue.isSuperset(of: other.rawValue)
}
@inlinable
public func isSuperset(of other: Collection) -> Bool {
self.rawValue.isSuperset(of: other)
}
@_disfavoredOverload
@inlinable
public func isStrictSubset(of other: NonEmpty) -> Bool {
self.rawValue.isStrictSubset(of: other.rawValue)
}
@inlinable
public func isStrictSubset(of other: Collection) -> Bool {
self.rawValue.isStrictSubset(of: other)
}
@_disfavoredOverload
@inlinable
public func isStrictSuperset(of other: NonEmpty) -> Bool {
self.rawValue.isStrictSuperset(of: other.rawValue)
}
@inlinable
public func isStrictSuperset(of other: Collection) -> Bool {
self.rawValue.isStrictSuperset(of: other)
}
}
extension SetAlgebra where Self: Collection, Element: Hashable {
@inlinable
public func union(_ other: NonEmpty<Self>) -> NonEmpty<Self> {
var copy = other
copy.formUnion(self)
return copy
}
@inlinable
public func intersection(_ other: NonEmpty<Self>) -> Self {
self.intersection(other.rawValue)
}
@inlinable
public func symmetricDifference(_ other: NonEmpty<Self>) -> Self {
self.symmetricDifference(other.rawValue)
}
@inlinable
public mutating func formUnion(_ other: NonEmpty<Self>) {
self.formUnion(other.rawValue)
}
@inlinable
public func subtracting(_ other: NonEmpty<Self>) -> Self {
self.subtracting(other.rawValue)
}
@inlinable
public func isDisjoint(with other: NonEmpty<Self>) -> Bool {
self.isDisjoint(with: other.rawValue)
}
@inlinable
public func isSubset(of other: NonEmpty<Self>) -> Bool {
self.isSubset(of: other.rawValue)
}
@inlinable
public func isSuperset(of other: NonEmpty<Self>) -> Bool {
self.isSuperset(of: other.rawValue)
}
@inlinable
public func isStrictSubset(of other: NonEmpty<Self>) -> Bool {
self.isStrictSubset(of: other.rawValue)
}
@inlinable
public func isStrictSuperset(of other: NonEmpty<Self>) -> Bool {
self.isStrictSuperset(of: other.rawValue)
}
}