[swift-users] Collection underestimatedCount() does _?

Will Stanton willstanton1 at yahoo.com
Sat Mar 19 00:53:44 CDT 2016


I came across `underestimatedCount()` and wonder what it does. I’m trying to become more familiar with Swift collections so (perhaps) I can write better-performing code!

/// Returns a value less than or equal to the number of elements in
/// `self`, *nondestructively*.
/// - Complexity: O(N).
public func underestimateCount() -> Int
/// Returns the number of elements.
/// - Complexity: O(1) if `Index` conforms to `RandomAccessIndexType`;
///   O(N) otherwise.
public var count: Self.Index.Distance { get }

I might have missed something since a lot of the results are for tests <https://github.com/apple/swift/search?utf8=✓&q=underestimatedCount&type=Code>
But why would `underestimatedCount` ever be used instead of `count`? Don’t most collections have O(1) `count` anyway?

Will Stanton

More information about the swift-users mailing list