Skip to content

Commit 77bb08e

Browse files
author
ematejska
authored
Merge pull request #13359 from apple/revert-13349-conditional-collections-4
Revert "[swift-4.1-branch][stdlib] Utilize conditional conformances for lazy collections"
2 parents 90011a8 + d54b6bc commit 77bb08e

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
@@ -658,6 +658,8 @@ public struct ${Self}<T> : ${SelfProtocols} {
658658
public func distance(from start: ${Index}, to end: ${Index})
659659
-> Int {
660660
% if Traversal == 'Forward':
661+
_precondition(start <= end,
662+
"Only BidirectionalCollections can have end come before start")
661663
% end
662664
// FIXME: swift-3-indexing-model: perform a range check properly.
663665
if start != endIndex {

stdlib/public/core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ set(SWIFTLIB_ESSENTIAL
5555
Equatable.swift
5656
ErrorType.swift
5757
Existential.swift
58-
Filter.swift
58+
Filter.swift.gyb
5959
FixedArray.swift.gyb
6060
FlatMap.swift
6161
Flatten.swift.gyb
@@ -83,7 +83,7 @@ set(SWIFTLIB_ESSENTIAL
8383
LazySequence.swift
8484
LifetimeManager.swift
8585
ManagedBuffer.swift
86-
Map.swift
86+
Map.swift.gyb
8787
MemoryLayout.swift
8888
UnicodeScalar.swift # ORDER DEPENDENCY: Must precede Mirrors.swift
8989
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)