Skip to content

Commit 572bb43

Browse files
authored
Merge pull request #13358 from apple/revert-13199-conditional-collections
2 parents 12e7caf + 62a6b74 commit 572bb43

File tree

14 files changed

+249
-242
lines changed

14 files changed

+249
-242
lines changed

benchmark/single-source/LazyFilter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public func run_LazilyFilteredArrays(_ N: Int) {
4747
CheckResults(res == 123)
4848
}
4949

50-
fileprivate var multiplesOfThree: LazyFilterCollection<Array<Int>>?
50+
fileprivate var multiplesOfThree: LazyFilterBidirectionalCollection<Array<Int>>?
5151

5252
fileprivate func setup_LazilyFilteredArrayContains() {
5353
multiplesOfThree = Array(1..<5_000).lazy.filter { $0 % 3 == 0 }

stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift.gyb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,11 @@ extension TestSuite {
15841584
.forEach(in: distanceFromToTests) {
15851585
test in
15861586
let c = toCollection(0..<20)
1587+
let backwards = (test.startOffset > test.endOffset)
1588+
if backwards && !collectionIsBidirectional {
1589+
expectCrashLater()
1590+
}
1591+
15871592
let d = c.distance(
15881593
from: c.nthIndex(test.startOffset), to: c.nthIndex(test.endOffset))
15891594
expectEqual(

stdlib/private/StdlibCollectionUnittest/MinimalCollections.swift.gyb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ public struct ${Self}<T> : ${SelfProtocols} {
657657
public func distance(from start: ${Index}, to end: ${Index})
658658
-> Int {
659659
% if Traversal == 'Forward':
660+
_precondition(start <= end,
661+
"Only BidirectionalCollections can have end come before start")
660662
% end
661663
// FIXME: swift-3-indexing-model: perform a range check properly.
662664
if start != endIndex {

stdlib/public/core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ set(SWIFTLIB_ESSENTIAL
5454
Equatable.swift
5555
ErrorType.swift
5656
Existential.swift
57-
Filter.swift
57+
Filter.swift.gyb
5858
FixedArray.swift.gyb
5959
FlatMap.swift
6060
Flatten.swift.gyb
@@ -82,7 +82,7 @@ set(SWIFTLIB_ESSENTIAL
8282
LazySequence.swift
8383
LifetimeManager.swift
8484
ManagedBuffer.swift
85-
Map.swift
85+
Map.swift.gyb
8686
MemoryLayout.swift
8787
UnicodeScalar.swift # ORDER DEPENDENCY: Must precede Mirrors.swift
8888
Mirrors.swift.gyb

stdlib/public/core/Collection.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,16 @@ public protocol Collection: Sequence where SubSequence: Collection {
701701

702702
/// Returns the distance between two indices.
703703
///
704+
/// Unless the collection conforms to the `BidirectionalCollection` protocol,
705+
/// `start` must be less than or equal to `end`.
706+
///
704707
/// - Parameters:
705708
/// - start: A valid index of the collection.
706709
/// - end: Another valid index of the collection. If `end` is equal to
707710
/// `start`, the result is zero.
708-
/// - Returns: The distance between `start` and `end`.
711+
/// - Returns: The distance between `start` and `end`. The result can be
712+
/// negative only if the collection conforms to the
713+
/// `BidirectionalCollection` protocol.
709714
///
710715
/// - Complexity: O(1) if the collection conforms to
711716
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the
@@ -957,34 +962,30 @@ extension Collection {
957962

958963
/// Returns the distance between two indices.
959964
///
965+
/// Unless the collection conforms to the `BidirectionalCollection` protocol,
966+
/// `start` must be less than or equal to `end`.
967+
///
960968
/// - Parameters:
961969
/// - start: A valid index of the collection.
962970
/// - end: Another valid index of the collection. If `end` is equal to
963971
/// `start`, the result is zero.
964-
/// - Returns: The distance between `start` and `end`.
972+
/// - Returns: The distance between `start` and `end`. The result can be
973+
/// negative only if the collection conforms to the
974+
/// `BidirectionalCollection` protocol.
965975
///
966976
/// - Complexity: O(1) if the collection conforms to
967977
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the
968978
/// resulting distance.
969979
@_inlineable
970980
public func distance(from start: Index, to end: Index) -> Int {
971-
var _start: Index
972-
let _end: Index
973-
let step: Int
974-
if start > end {
975-
_start = end
976-
_end = start
977-
step = -1
978-
}
979-
else {
980-
_start = start
981-
_end = end
982-
step = 1
983-
}
981+
_precondition(start <= end,
982+
"Only BidirectionalCollections can have end come before start")
983+
984+
var start = start
984985
var count = 0
985-
while _start != _end {
986-
count += step
987-
formIndex(after: &_start)
986+
while start != end {
987+
count = count + 1
988+
formIndex(after: &start)
988989
}
989990
return count
990991
}

0 commit comments

Comments
 (0)