@@ -89,9 +89,8 @@ public struct CompactedCollection<Base: Collection, Element>: Collection
89
89
90
90
@inlinable
91
91
public func index( after i: Index ) -> Index {
92
- guard i != endIndex else {
93
- fatalError ( " Index out of bounds " )
94
- }
92
+ precondition ( i < endIndex, " Index out of bounds " )
93
+
95
94
let baseIdx = base. index ( after: i. base)
96
95
guard let idx = base [ baseIdx... ] . firstIndex ( where: { $0 != nil } )
97
96
else { return endIndex }
@@ -104,13 +103,12 @@ extension CompactedCollection: BidirectionalCollection
104
103
105
104
@inlinable
106
105
public func index( before i: Index ) -> Index {
107
- guard i != startIndex else {
108
- fatalError ( " Index out of bounds " )
109
- }
106
+ precondition ( i > startIndex, " Index out of bounds " )
110
107
111
- let baseIdx = base. index ( before: i. base)
112
- guard let idx = base [ ... baseIdx] . lastIndex ( where: { $0 != nil } )
113
- else { return Index ( base: base. startIndex) }
108
+ guard let idx =
109
+ base [ startIndex. base..< i. base]
110
+ . lastIndex ( where: { $0 != nil } )
111
+ else { fatalError ( " Index out of bounds " ) }
114
112
return Index ( base: idx)
115
113
}
116
114
}
@@ -150,46 +148,42 @@ extension CompactedCollection: LazyCollectionProtocol
150
148
// Hashable and Equatable conformance are based on each non-nil
151
149
// element on base collection.
152
150
extension CompactedSequence : Equatable
153
- where Base: Equatable , Base . Element: Equatable {
151
+ where Base. Element: Equatable {
154
152
155
153
@inlinable
156
154
public static func == ( lhs: CompactedSequence ,
157
155
rhs: CompactedSequence ) -> Bool {
158
- lhs. compactMap ( { $0 } )
159
- . elementsEqual ( rhs. compactMap ( { $0 } ) )
156
+ lhs. elementsEqual ( rhs)
160
157
}
161
158
}
162
159
163
160
extension CompactedSequence : Hashable
164
- where Base : Hashable , Base . Element: Hashable {
161
+ where Element: Hashable {
165
162
@inlinable
166
163
public func hash( into hasher: inout Hasher ) {
167
- for element in base {
168
- guard let unwrapped = element else { continue }
169
- hasher. combine ( unwrapped)
164
+ for element in self {
165
+ hasher. combine ( element)
170
166
}
171
167
}
172
168
}
173
169
174
170
extension CompactedCollection : Equatable
175
- where Base: Equatable , Base . Element: Equatable {
171
+ where Base. Element: Equatable {
176
172
177
173
@inlinable
178
174
public static func == ( lhs: CompactedCollection ,
179
175
rhs: CompactedCollection ) -> Bool {
180
- lhs. compactMap ( { $0 } )
181
- . elementsEqual ( rhs. compactMap ( { $0 } ) )
176
+ lhs. elementsEqual ( rhs)
182
177
}
183
178
}
184
179
185
180
extension CompactedCollection : Hashable
186
- where Base : Hashable , Base . Element: Hashable {
181
+ where Element: Hashable {
187
182
188
183
@inlinable
189
184
public func hash( into hasher: inout Hasher ) {
190
- for element in base {
191
- guard let unwrapped = element else { continue }
192
- hasher. combine ( unwrapped)
185
+ for element in self {
186
+ hasher. combine ( element)
193
187
}
194
188
}
195
189
}
0 commit comments