@@ -289,25 +289,27 @@ extension FlattenCollection: Collection {
289
289
290
290
@inlinable // lazy-performance
291
291
public func distance( from start: Index , to end: Index ) -> Int {
292
- // The following check makes sure that distance(from:to:) is invoked on the
293
- // _base at least once, to trigger a _precondition in forward only
292
+ // The following check ensures that distance(from:to:) is invoked on
293
+ // the _base at least once, to trigger a _precondition in forward only
294
294
// collections.
295
295
if start > end {
296
296
_ = _base. distance ( from: _base. endIndex, to: _base. startIndex)
297
297
}
298
298
299
- // This handles the case where both indices belong to the same collection.
299
+ // This handles indices belonging to the same collection.
300
300
if start. _outer == end. _outer {
301
- return start. _outer < _base. endIndex ? _base [ start. _outer] . distance ( from: start. _inner!, to: end. _inner!) : Int . zero
301
+ guard let i = start. _inner, let j = end. _inner else { return 0 }
302
+ return _base [ start. _outer] . distance ( from: i, to: j)
302
303
}
303
304
304
- // The following path combines the prefix, the middle, and the suffix distance .
305
- let range = Range ( uncheckedBounds : start <= end ? ( start, end) : ( end, start) )
305
+ // The following combines the distance of three sections .
306
+ let range = start <= end ? start ... end : end ... start
306
307
var outer = range. lowerBound. _outer
307
308
var count = 0 as Int // 0...Int.max
308
309
309
310
if let inner = range. lowerBound. _inner {
310
- count += _base [ outer] [ inner... ] . count
311
+ let collection = _base [ outer]
312
+ count += collection. distance ( from: inner, to: collection. endIndex)
311
313
_base. formIndex ( after: & outer)
312
314
}
313
315
@@ -317,7 +319,8 @@ extension FlattenCollection: Collection {
317
319
}
318
320
319
321
if let inner = range. upperBound. _inner {
320
- count += _base [ outer] [ ..< inner] . count
322
+ let collection = _base [ outer]
323
+ count += collection. distance ( from: collection. startIndex, to: inner)
321
324
}
322
325
323
326
return start <= end ? count : - count
0 commit comments