Skip to content

Commit fd4e866

Browse files
author
Max Moiseev
committed
Implement a custom distance(from:to:) for LazyFilterCollection
1 parent 25a28bc commit fd4e866

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

stdlib/public/core/Filter.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,33 @@ extension LazyFilterCollection : LazyCollectionProtocol, Collection {
265265
}
266266
}
267267

268+
@_inlineable // FIXME(sil-serialize-all)
269+
public func distance(from start: Index, to end: Index) -> Int {
270+
// The following line makes sure that distance(from:to:) is invoked on the
271+
// _base at least once, to trigger a _precondition in forward only
272+
// collections.
273+
_ = _base.distance(from: start, to: end)
274+
var _start: Index
275+
let _end: Index
276+
let step: Int
277+
if start > end {
278+
_start = end
279+
_end = start
280+
step = -1
281+
}
282+
else {
283+
_start = start
284+
_end = end
285+
step = 1
286+
}
287+
var count = 0
288+
while _start != _end {
289+
count += step
290+
formIndex(after: &_start)
291+
}
292+
return count
293+
}
294+
268295
@_inlineable // FIXME(sil-serialize-all)
269296
public func index(_ i: Index, offsetBy n: Int) -> Index {
270297
var i = i

0 commit comments

Comments
 (0)