Skip to content

Commit 2c90d5f

Browse files
committed
Remove customization points from Sequence and Collection
1 parent f85d8ae commit 2c90d5f

17 files changed

+64
-541
lines changed

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ public class SequenceLog {
7777
// Sequence
7878
public static var makeIterator = TypeIndexed(0)
7979
public static var underestimatedCount = TypeIndexed(0)
80-
public static var map = TypeIndexed(0)
81-
public static var filter = TypeIndexed(0)
82-
public static var forEach = TypeIndexed(0)
8380
public static var dropFirst = TypeIndexed(0)
8481
public static var dropLast = TypeIndexed(0)
8582
public static var dropWhile = TypeIndexed(0)
@@ -101,20 +98,15 @@ public class SequenceLog {
10198
public static var successor = TypeIndexed(0)
10299
public static var formSuccessor = TypeIndexed(0)
103100
public static var indices = TypeIndexed(0)
104-
public static var prefixUpTo = TypeIndexed(0)
105-
public static var prefixThrough = TypeIndexed(0)
106-
public static var suffixFrom = TypeIndexed(0)
107101
public static var isEmpty = TypeIndexed(0)
108102
public static var count = TypeIndexed(0)
109103
public static var _customIndexOfEquatableElement = TypeIndexed(0)
110-
public static var first = TypeIndexed(0)
111104
public static var advance = TypeIndexed(0)
112105
public static var advanceLimit = TypeIndexed(0)
113106
public static var distance = TypeIndexed(0)
114107
// BidirectionalCollection
115108
public static var predecessor = TypeIndexed(0)
116109
public static var formPredecessor = TypeIndexed(0)
117-
public static var last = TypeIndexed(0)
118110
// MutableCollection
119111
public static var subscriptIndexSet = TypeIndexed(0)
120112
public static var subscriptRangeSet = TypeIndexed(0)
@@ -222,25 +214,6 @@ extension LoggingSequence: Sequence {
222214
return base.underestimatedCount
223215
}
224216

225-
public func map<T>(
226-
_ transform: (Element) throws -> T
227-
) rethrows -> [T] {
228-
SequenceLog.map[selfType] += 1
229-
return try base.map(transform)
230-
}
231-
232-
public func filter(
233-
_ isIncluded: (Element) throws -> Bool
234-
) rethrows -> [Element] {
235-
SequenceLog.filter[selfType] += 1
236-
return try base.filter(isIncluded)
237-
}
238-
239-
public func forEach(_ body: (Element) throws -> Void) rethrows {
240-
SequenceLog.forEach[selfType] += 1
241-
try base.forEach(body)
242-
}
243-
244217
public func dropFirst(_ n: Int) -> SubSequence {
245218
SequenceLog.dropFirst[selfType] += 1
246219
return base.dropFirst(n)
@@ -336,17 +309,13 @@ extension LoggingCollection: Collection {
336309
}
337310

338311
public subscript(position: Index) -> Element {
339-
get {
340-
CollectionLog.subscriptIndex[selfType] += 1
341-
return base[position]
342-
}
312+
CollectionLog.subscriptIndex[selfType] += 1
313+
return base[position]
343314
}
344315

345316
public subscript(bounds: Range<Index>) -> SubSequence {
346-
get {
347-
CollectionLog.subscriptRange[selfType] += 1
348-
return base[bounds]
349-
}
317+
CollectionLog.subscriptRange[selfType] += 1
318+
return base[bounds]
350319
}
351320

352321
public func _failEarlyRangeCheck(_ index: Index, bounds: Range<Index>) {
@@ -374,21 +343,6 @@ extension LoggingCollection: Collection {
374343
return base.indices
375344
}
376345

377-
public func prefix(upTo end: Index) -> SubSequence {
378-
CollectionLog.prefixUpTo[selfType] += 1
379-
return base.prefix(upTo: end)
380-
}
381-
382-
public func prefix(through position: Index) -> SubSequence {
383-
CollectionLog.prefixThrough[selfType] += 1
384-
return base.prefix(through: position)
385-
}
386-
387-
public func suffix(from start: Index) -> SubSequence {
388-
CollectionLog.suffixFrom[selfType] += 1
389-
return base.suffix(from: start)
390-
}
391-
392346
public var isEmpty: Bool {
393347
CollectionLog.isEmpty[selfType] += 1
394348
return base.isEmpty
@@ -406,11 +360,6 @@ extension LoggingCollection: Collection {
406360
return base._customIndexOfEquatableElement(element)
407361
}
408362

409-
public var first: Element? {
410-
CollectionLog.first[selfType] += 1
411-
return base.first
412-
}
413-
414363
public func index(_ i: Index, offsetBy n: Int) -> Index {
415364
CollectionLog.advance[selfType] += 1
416365
return base.index(i, offsetBy: n)
@@ -443,11 +392,6 @@ extension LoggingBidirectionalCollection: BidirectionalCollection {
443392
BidirectionalCollectionLog.formPredecessor[selfType] += 1
444393
base.formIndex(before: &i)
445394
}
446-
447-
public var last: Element? {
448-
BidirectionalCollectionLog.last[selfType] += 1
449-
return base.last
450-
}
451395
}
452396

453397
public typealias LoggingRandomAccessCollection<Base: RandomAccessCollection>

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,6 @@ where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
185185
/// // c == MyFancyCollection([2, 4, 6, 8, 10])
186186
override var indices: Indices { get }
187187

188-
// TODO: swift-3-indexing-model: tests.
189-
/// The last element of the collection.
190-
///
191-
/// If the collection is empty, the value of this property is `nil`.
192-
///
193-
/// let numbers = [10, 20, 30, 40, 50]
194-
/// if let lastNumber = numbers.last {
195-
/// print(lastNumber)
196-
/// }
197-
/// // Prints "50"
198-
///
199-
/// - Complexity: O(1)
200-
var last: Element? { get }
201-
202188
/// Accesses a contiguous subrange of the collection's elements.
203189
///
204190
/// The accessed slice uses the same indices for the same elements as the

stdlib/public/core/Collection.swift

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -477,108 +477,6 @@ public protocol Collection: Sequence where SubSequence: Collection {
477477
/// // c == MyFancyCollection([2, 4, 6, 8, 10])
478478
var indices: Indices { get }
479479

480-
/// Returns a subsequence from the start of the collection up to, but not
481-
/// including, the specified position.
482-
///
483-
/// The resulting subsequence *does not include* the element at the position
484-
/// `end`. The following example searches for the index of the number `40`
485-
/// in an array of integers, and then prints the prefix of the array up to,
486-
/// but not including, that index:
487-
///
488-
/// let numbers = [10, 20, 30, 40, 50, 60]
489-
/// if let i = numbers.firstIndex(of: 40) {
490-
/// print(numbers.prefix(upTo: i))
491-
/// }
492-
/// // Prints "[10, 20, 30]"
493-
///
494-
/// Passing the collection's starting index as the `end` parameter results in
495-
/// an empty subsequence.
496-
///
497-
/// print(numbers.prefix(upTo: numbers.startIndex))
498-
/// // Prints "[]"
499-
///
500-
/// Using the `prefix(upTo:)` method is equivalent to using a partial
501-
/// half-open range as the collection's subscript. The subscript notation is
502-
/// preferred over `prefix(upTo:)`.
503-
///
504-
/// if let i = numbers.firstIndex(of: 40) {
505-
/// print(numbers[..<i])
506-
/// }
507-
/// // Prints "[10, 20, 30]"
508-
///
509-
/// - Parameter end: The "past the end" index of the resulting subsequence.
510-
/// `end` must be a valid index of the collection.
511-
/// - Returns: A subsequence up to, but not including, the `end` position.
512-
///
513-
/// - Complexity: O(1)
514-
__consuming func prefix(upTo end: Index) -> SubSequence
515-
516-
/// Returns a subsequence from the specified position to the end of the
517-
/// collection.
518-
///
519-
/// The following example searches for the index of the number `40` in an
520-
/// array of integers, and then prints the suffix of the array starting at
521-
/// that index:
522-
///
523-
/// let numbers = [10, 20, 30, 40, 50, 60]
524-
/// if let i = numbers.firstIndex(of: 40) {
525-
/// print(numbers.suffix(from: i))
526-
/// }
527-
/// // Prints "[40, 50, 60]"
528-
///
529-
/// Passing the collection's `endIndex` as the `start` parameter results in
530-
/// an empty subsequence.
531-
///
532-
/// print(numbers.suffix(from: numbers.endIndex))
533-
/// // Prints "[]"
534-
///
535-
/// Using the `suffix(from:)` method is equivalent to using a partial range
536-
/// from the index as the collection's subscript. The subscript notation is
537-
/// preferred over `suffix(from:)`.
538-
///
539-
/// if let i = numbers.firstIndex(of: 40) {
540-
/// print(numbers[i...])
541-
/// }
542-
/// // Prints "[40, 50, 60]"
543-
///
544-
/// - Parameter start: The index at which to start the resulting subsequence.
545-
/// `start` must be a valid index of the collection.
546-
/// - Returns: A subsequence starting at the `start` position.
547-
///
548-
/// - Complexity: O(1)
549-
__consuming func suffix(from start: Index) -> SubSequence
550-
551-
/// Returns a subsequence from the start of the collection through the
552-
/// specified position.
553-
///
554-
/// The resulting subsequence *includes* the element at the position `end`.
555-
/// The following example searches for the index of the number `40` in an
556-
/// array of integers, and then prints the prefix of the array up to, and
557-
/// including, that index:
558-
///
559-
/// let numbers = [10, 20, 30, 40, 50, 60]
560-
/// if let i = numbers.firstIndex(of: 40) {
561-
/// print(numbers.prefix(through: i))
562-
/// }
563-
/// // Prints "[10, 20, 30, 40]"
564-
///
565-
/// Using the `prefix(through:)` method is equivalent to using a partial
566-
/// closed range as the collection's subscript. The subscript notation is
567-
/// preferred over `prefix(through:)`.
568-
///
569-
/// if let i = numbers.firstIndex(of: 40) {
570-
/// print(numbers[...i])
571-
/// }
572-
/// // Prints "[10, 20, 30, 40]"
573-
///
574-
/// - Parameter end: The index of the last element to include in the
575-
/// resulting subsequence. `end` must be a valid index of the collection
576-
/// that is not equal to the `endIndex` property.
577-
/// - Returns: A subsequence up to, and including, the `end` position.
578-
///
579-
/// - Complexity: O(1)
580-
__consuming func prefix(through position: Index) -> SubSequence
581-
582480
/// A Boolean value indicating whether the collection is empty.
583481
///
584482
/// When you need to check whether your collection is empty, use the
@@ -632,22 +530,6 @@ public protocol Collection: Sequence where SubSequence: Collection {
632530
/// - Complexity: Hopefully less than O(`count`).
633531
func _customLastIndexOfEquatableElement(_ element: Element) -> Index??
634532

635-
// FIXME(move-only types): `first` might not be implementable by collections
636-
// with move-only elements, since they would need to be able to somehow form
637-
// a temporary `Optional<Element>` value from a nonoptional Element without
638-
// modifying the collection.
639-
640-
/// The first element of the collection.
641-
///
642-
/// If the collection is empty, the value of this property is `nil`.
643-
///
644-
/// let numbers = [10, 20, 30, 40, 50]
645-
/// if let firstNumber = numbers.first {
646-
/// print(firstNumber)
647-
/// }
648-
/// // Prints "10"
649-
var first: Element? { get }
650-
651533
/// Returns an index that is the specified distance from the given index.
652534
///
653535
/// The following example obtains an index advanced four positions from a
@@ -1346,8 +1228,7 @@ extension Collection {
13461228
@inlinable
13471229
public __consuming func dropFirst(_ k: Int) -> SubSequence {
13481230
_precondition(k >= 0, "Can't drop a negative number of elements from a collection")
1349-
let start = index(startIndex,
1350-
offsetBy: k, limitedBy: endIndex) ?? endIndex
1231+
let start = index(startIndex, offsetBy: k, limitedBy: endIndex) ?? endIndex
13511232
return self[start..<endIndex]
13521233
}
13531234

stdlib/public/core/Dictionary.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -728,19 +728,6 @@ extension Dictionary: Collection {
728728
public var isEmpty: Bool {
729729
return count == 0
730730
}
731-
732-
/// The first element of the dictionary.
733-
///
734-
/// The first element of the dictionary is not necessarily the first element
735-
/// added to the dictionary. Don't expect any particular ordering of
736-
/// dictionary elements.
737-
///
738-
/// If the dictionary is empty, the value of this property is `nil`.
739-
@inlinable
740-
public var first: Element? {
741-
var it = makeIterator()
742-
return it.next()
743-
}
744731
}
745732

746733
extension Dictionary {

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ internal class _AnyRandomAccessCollectionBox<Element>
352352
func _customLastIndexOfEquatableElement(element: Element) -> Index??
353353
*/
354354

355-
@inlinable // FIXME(sil-serialize-all)
356-
internal var _first: Element? { _abstract() }
357-
358355
@inlinable
359356
internal init(
360357
_startIndex: _AnyIndexBox,
@@ -385,8 +382,6 @@ internal class _AnyRandomAccessCollectionBox<Element>
385382
internal func _index(before i: _AnyIndexBox) -> _AnyIndexBox { _abstract() }
386383
@inlinable
387384
internal func _formIndex(before i: _AnyIndexBox) { _abstract() }
388-
@inlinable
389-
internal var _last: Element? { _abstract() }
390385
% end
391386
}
392387

@@ -617,11 +612,6 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Element>
617612
return numericCast(_base.count)
618613
}
619614

620-
@inlinable
621-
internal override var _first: Element? {
622-
return _base.first
623-
}
624-
625615
% if Kind in ['BidirectionalCollection', 'RandomAccessCollection']:
626616
@inlinable
627617
internal override func _index(before position: _AnyIndexBox) -> _AnyIndexBox {
@@ -636,10 +626,6 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Element>
636626
fatalError("Index type mismatch!")
637627
}
638628

639-
@inlinable
640-
internal override var _last: Element? {
641-
return _base.last
642-
}
643629
% end
644630

645631
% end
@@ -1118,11 +1104,6 @@ extension ${Self}: ${SelfProtocol} {
11181104
return _box._count
11191105
}
11201106

1121-
@inlinable
1122-
public var first: Element? {
1123-
return _box._first
1124-
}
1125-
11261107
% if Traversal == 'Bidirectional' or Traversal == 'RandomAccess':
11271108
@inlinable
11281109
public func index(before i: Index) -> Index {
@@ -1138,11 +1119,6 @@ extension ${Self}: ${SelfProtocol} {
11381119
i = index(before: i)
11391120
}
11401121
}
1141-
1142-
@inlinable
1143-
public var last: Element? {
1144-
return _box._last
1145-
}
11461122
% end
11471123
}
11481124

stdlib/public/core/LazyCollection.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ extension LazyCollection : Collection {
201201
return _base._customLastIndexOfEquatableElement(element)
202202
}
203203

204-
/// Returns the first element of `self`, or `nil` if `self` is empty.
205-
@inlinable
206-
public var first: Element? {
207-
return _base.first
208-
}
209-
210204
// TODO: swift-3-indexing-model - add docs
211205
@inlinable
212206
public func index(_ i: Index, offsetBy n: Int) -> Index {
@@ -235,11 +229,6 @@ extension LazyCollection : BidirectionalCollection
235229
public func index(before i: Index) -> Index {
236230
return _base.index(before: i)
237231
}
238-
239-
@inlinable
240-
public var last: Element? {
241-
return _base.last
242-
}
243232
}
244233

245234
extension LazyCollection : RandomAccessCollection

0 commit comments

Comments
 (0)