Skip to content

Commit ea49459

Browse files
author
Thomas Di Meco
committed
[stdlib] Updated underestimatedCount doc related to complexity
There were inconsistencies in the documentation about the complexity of the underestimatedCount property.
1 parent 65f1b82 commit ea49459

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

stdlib/public/core/LazyCollection.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ extension LazyCollection : Sequence {
8484
return _base.makeIterator()
8585
}
8686

87-
/// Returns a value less than or equal to the number of elements in
88-
/// `self`, **nondestructively**.
87+
/// A value less than or equal to the number of elements in the collection.
8988
///
90-
/// - Complexity: O(*n*)
89+
/// - Complexity: O(1) if the collection conforms to
90+
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
91+
/// of the collection.
9192
@_inlineable
9293
public var underestimatedCount: Int { return _base.underestimatedCount }
9394

stdlib/public/core/Map.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ extension LazyMapSequence: LazySequenceProtocol {
8080
return Iterator(_base: _base.makeIterator(), _transform: _transform)
8181
}
8282

83-
/// Returns a value less than or equal to the number of elements in
84-
/// `self`, **nondestructively**.
83+
/// A value less than or equal to the number of elements in the sequence,
84+
/// calculated nondestructively.
8585
///
86-
/// - Complexity: O(*n*)
86+
/// The default implementation returns 0. If you provide your own
87+
/// implementation, make sure to compute the value nondestructively.
88+
///
89+
/// - Complexity: O(1), except if the sequence also conforms to `Collection`.
90+
/// In this case, see the documentation of `Collection.underestimatedCount`.
8791
@_inlineable
8892
public var underestimatedCount: Int {
8993
return _base.underestimatedCount
@@ -122,6 +126,11 @@ extension LazyMapCollection: Sequence {
122126
return Iterator(_base: _base.makeIterator(), _transform: _transform)
123127
}
124128

129+
/// A value less than or equal to the number of elements in the collection.
130+
///
131+
/// - Complexity: O(1) if the collection conforms to
132+
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
133+
/// of the collection.
125134
@_inlineable
126135
public var underestimatedCount: Int {
127136
return _base.underestimatedCount
@@ -204,6 +213,11 @@ extension LazyMapCollection: LazyCollectionProtocol {
204213
extension LazyMapCollection : BidirectionalCollection
205214
where Base : BidirectionalCollection {
206215

216+
/// A value less than or equal to the number of elements in the collection.
217+
///
218+
/// - Complexity: O(1) if the collection conforms to
219+
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
220+
/// of the collection.
207221
@_inlineable
208222
public func index(before i: Index) -> Index { return _base.index(before: i) }
209223

stdlib/public/core/Sequence.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,14 @@ public protocol Sequence {
338338
/// Returns an iterator over the elements of this sequence.
339339
func makeIterator() -> Iterator
340340

341-
/// A value less than or equal to the number of elements in
342-
/// the sequence, calculated nondestructively.
341+
/// A value less than or equal to the number of elements in the sequence,
342+
/// calculated nondestructively.
343343
///
344-
/// - Complexity: O(1)
344+
/// The default implementation returns 0. If you provide your own
345+
/// implementation, make sure to compute the value nondestructively.
346+
///
347+
/// - Complexity: O(1), except if the sequence also conforms to `Collection`.
348+
/// In this case, see the documentation of `Collection.underestimatedCount`.
345349
var underestimatedCount: Int { get }
346350

347351
/// Returns an array containing the results of mapping the given closure
@@ -885,10 +889,14 @@ extension Sequence {
885889
return Array(result)
886890
}
887891

888-
/// Returns a value less than or equal to the number of elements in
889-
/// the sequence, nondestructively.
892+
/// A value less than or equal to the number of elements in the sequence,
893+
/// calculated nondestructively.
894+
///
895+
/// The default implementation returns 0. If you provide your own
896+
/// implementation, make sure to compute the value nondestructively.
890897
///
891-
/// - Complexity: O(*n*)
898+
/// - Complexity: O(1), except if the sequence also conforms to `Collection`.
899+
/// In this case, see the documentation of `Collection.underestimatedCount`.
892900
@_inlineable
893901
public var underestimatedCount: Int {
894902
return 0

0 commit comments

Comments
 (0)