@@ -126,7 +126,7 @@ extension AdjacentPairsSequence: Sequence {
126
126
}
127
127
128
128
extension AdjacentPairsSequence : LazySequenceProtocol
129
- where Base: LazySequenceProtocol { }
129
+ where Base: LazySequenceProtocol { }
130
130
131
131
/// A collection of adjacent pairs of elements built from an underlying
132
132
/// collection.
@@ -143,7 +143,8 @@ public struct AdjacentPairsCollection<Base: Collection> {
143
143
@inlinable
144
144
internal init ( base: Base ) {
145
145
self . base = base
146
- self . secondBaseIndex = base. isEmpty
146
+ self . secondBaseIndex =
147
+ base. isEmpty
147
148
? base. endIndex
148
149
: base. index ( after: base. startIndex)
149
150
}
@@ -154,7 +155,7 @@ extension AdjacentPairsCollection {
154
155
public struct Index : Comparable {
155
156
@usableFromInline
156
157
internal var first : Base . Index
157
-
158
+
158
159
@usableFromInline
159
160
internal var second : Base . Index
160
161
@@ -168,7 +169,7 @@ extension AdjacentPairsCollection {
168
169
public static func == ( lhs: Index , rhs: Index ) -> Bool {
169
170
lhs. first == rhs. first
170
171
}
171
-
172
+
172
173
@inlinable
173
174
public static func < ( lhs: Index , rhs: Index ) -> Bool {
174
175
lhs. first < rhs. first
@@ -183,7 +184,7 @@ extension AdjacentPairsCollection: Collection {
183
184
first: secondBaseIndex == base. endIndex ? base. endIndex : base. startIndex,
184
185
second: secondBaseIndex)
185
186
}
186
-
187
+
187
188
@inlinable
188
189
public var endIndex : Index {
189
190
Index ( first: base. endIndex, second: base. endIndex)
@@ -207,20 +208,23 @@ extension AdjacentPairsCollection: Collection {
207
208
public func index( _ i: Index , offsetBy distance: Int ) -> Index {
208
209
guard distance != 0 else { return i }
209
210
210
- guard let result = distance > 0
211
- ? offsetForward ( i, by: distance, limitedBy: endIndex)
212
- : offsetBackward ( i, by: - distance, limitedBy: startIndex)
211
+ guard
212
+ let result = distance > 0
213
+ ? offsetForward ( i, by: distance, limitedBy: endIndex)
214
+ : offsetBackward ( i, by: - distance, limitedBy: startIndex)
213
215
else { fatalError ( " Index out of bounds " ) }
214
216
return result
215
217
}
216
218
217
219
@inlinable
218
220
public func index(
219
- _ i: Index , offsetBy distance: Int , limitedBy limit: Index
221
+ _ i: Index ,
222
+ offsetBy distance: Int ,
223
+ limitedBy limit: Index
220
224
) -> Index ? {
221
225
guard distance != 0 else { return i }
222
226
guard limit != i else { return nil }
223
-
227
+
224
228
if distance > 0 {
225
229
let limit = limit > i ? limit : endIndex
226
230
return offsetForward ( i, by: distance, limitedBy: limit)
@@ -229,38 +233,48 @@ extension AdjacentPairsCollection: Collection {
229
233
return offsetBackward ( i, by: - distance, limitedBy: limit)
230
234
}
231
235
}
232
-
236
+
233
237
@inlinable
234
238
internal func offsetForward(
235
- _ i: Index , by distance: Int , limitedBy limit: Index
239
+ _ i: Index ,
240
+ by distance: Int ,
241
+ limitedBy limit: Index
236
242
) -> Index ? {
237
243
assert ( distance > 0 )
238
244
assert ( limit > i)
239
-
240
- guard let newFirst = base. index ( i. second, offsetBy: distance - 1 , limitedBy: limit. first) ,
241
- newFirst != base. endIndex
245
+
246
+ let newFirst = base. index (
247
+ i. second,
248
+ offsetBy: distance - 1 ,
249
+ limitedBy: limit. first)
250
+ guard let newFirst, newFirst != base. endIndex
242
251
else { return nil }
252
+
243
253
let newSecond = base. index ( after: newFirst)
244
-
254
+
245
255
precondition ( newSecond <= base. endIndex, " Can't advance beyond endIndex " )
246
256
return newSecond == base. endIndex
247
257
? endIndex
248
258
: Index ( first: newFirst, second: newSecond)
249
259
}
250
-
260
+
251
261
@inlinable
252
262
internal func offsetBackward(
253
- _ i: Index , by distance: Int , limitedBy limit: Index
263
+ _ i: Index ,
264
+ by distance: Int ,
265
+ limitedBy limit: Index
254
266
) -> Index ? {
255
267
assert ( distance > 0 )
256
268
assert ( limit < i)
257
-
269
+
258
270
let offset = i == endIndex ? 0 : 1
259
- guard let newSecond = base. index (
271
+ let newSecond = base. index (
260
272
i. first,
261
273
offsetBy: - ( distance - offset) ,
262
274
limitedBy: limit. second)
275
+ guard let newSecond
263
276
else { return nil }
277
+
264
278
let newFirst = base. index ( newSecond, offsetBy: - 1 )
265
279
precondition ( newFirst >= base. startIndex, " Can't move before startIndex " )
266
280
return Index ( first: newFirst, second: newSecond)
@@ -282,12 +296,12 @@ extension AdjacentPairsCollection: Collection {
282
296
}
283
297
284
298
extension AdjacentPairsCollection : BidirectionalCollection
285
- where Base: BidirectionalCollection
286
- {
299
+ where Base: BidirectionalCollection {
287
300
@inlinable
288
301
public func index( before i: Index ) -> Index {
289
302
precondition ( i != startIndex, " Can't offset before startIndex " )
290
- let second = i == endIndex
303
+ let second =
304
+ i == endIndex
291
305
? base. index ( before: base. endIndex)
292
306
: i. first
293
307
let first = base. index ( before: second)
@@ -296,10 +310,10 @@ extension AdjacentPairsCollection: BidirectionalCollection
296
310
}
297
311
298
312
extension AdjacentPairsCollection : RandomAccessCollection
299
- where Base: RandomAccessCollection { }
313
+ where Base: RandomAccessCollection { }
300
314
301
315
extension AdjacentPairsCollection : LazySequenceProtocol , LazyCollectionProtocol
302
- where Base: LazySequenceProtocol { }
316
+ where Base: LazySequenceProtocol { }
303
317
304
318
extension AdjacentPairsCollection . Index : Hashable where Base. Index: Hashable {
305
319
@inlinable
0 commit comments