@@ -289,25 +289,27 @@ extension FlattenCollection: Collection {
289289
290290 @inlinable // lazy-performance
291291 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
294294 // collections.
295295 if start > end {
296296 _ = _base. distance ( from: _base. endIndex, to: _base. startIndex)
297297 }
298298
299- // This handles the case where both indices belong to the same collection.
299+ // This handles indices belonging to the same collection.
300300 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)
302303 }
303304
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
306307 var outer = range. lowerBound. _outer
307308 var count = 0 as Int // 0...Int.max
308309
309310 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)
311313 _base. formIndex ( after: & outer)
312314 }
313315
@@ -317,7 +319,8 @@ extension FlattenCollection: Collection {
317319 }
318320
319321 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)
321324 }
322325
323326 return start <= end ? count : - count
0 commit comments