Swift Collection underestimateCount用法 [英] Swift Collection underestimateCount usage
问题描述
我想知道,集合 underestimateCount
的用例是什么? 文档说,它与标准Collection 计数。
I wonder, what is the use case for Collection underestimateCount
? Documentation says that it has the same complexity as standard Collection count.
/// Returns a value less than or equal to the number of elements in
/// `self`, *nondestructively*.
///
/// - Complexity: O(N).
public func underestimateCount() -> Int
但是它没有说明何时应使用它以及出于什么原因。
But it doesn't describe when it should be used and for what reason.
推荐答案
underestimatedCount
实际上是序列的要求
协议,并且具有 a默认实现,仅返回 0
:
underestimatedCount
is actually a requirement of the Sequence
protocol, and has a default implementation that just returns 0
:
public var underestimatedCount: Int {
return 0
}
但是,对于提供自己实现的 underestimatedCount
,这对于需要下限确定序列长度的逻辑很有用,而无需迭代遍历(记住序列
不能保证无损迭代。
However, for sequences that provide their own implementation of underestimatedCount
, this can be useful for logic that needs a lower bound of how long the sequence is, without having to iterate through it (remember that Sequence
gives no guarantee of non-destructive iteration).
例如, map(_ :)
序列上的方法(在此处查看其实现)使用 underestimateCount
来保留所得数组的初始容量:
For example, the map(_:)
method on Sequence
(see its implementation here) uses underestimateCount
in order to reserve an initial capacity for the resultant array:
public func map<T>(
_ transform: (Iterator.Element) throws -> T
) rethrows -> [T] {
let initialCapacity = underestimatedCount
var result = ContiguousArray<T>()
result.reserveCapacity(initialCapacity)
// ...
这允许 map(_:)
以最大程度地减少重复添加到 result
的成本,因为(可能)已经为其分配了初始内存块(尽管在任何情况下都应注意 ContiguousArray
具有指数增长策略,可分摊附加成本。
This allows map(_:)
to minimise the cost of repeatedly appending to the result
, as an initial block of memory has (possibly) already been allocated for it (although its worth noting in any case that ContiguousArray
has an exponential growth strategy that amortises the cost of appending).
但是,对于 Collection
, underestimateCount
的默认实现实际上只是返回集合的 count
:
However, in the case of a Collection
, the default implementation of underestimateCount
actually just returns the collection's count
:
public var underestimatedCount: Int {
// TODO: swift-3-indexing-model - review the following
return numericCast(count)
}
这将是集合的O(1)操作符合 RandomAccessCollection
,否则为O(n)。
Which will be an O(1) operation for collections that conform to RandomAccessCollection
, O(n) otherwise.
因此,由于采用了默认设置,因此使用<与使用序列
Collection 的 underestimatedCount
绝对不常见>,因为 Collection
保证无损迭代,并且在大多数情况下 underestimatedCount
只会返回 count
。
Therefore, because of this default implementation, using a Collection
's underestimatedCount
directly is definitely less common than using a Sequence
's, as Collection
guarantees non-destructive iteration, and in most cases underestimatedCount
will just return the count
.
当然,自定义集合类型可以提供自己的 underestimatedCount
–以一种比其 count
实现可能更有效的方式,给出它们包含的元素的下限,这可能很有用。
Although, of course, custom collection types could provide their own implementation of underestimatedCount
– giving a lower bound of how many elements they contain, in a possibly more efficient way than their count
implementation, which could potentially be useful.
这篇关于Swift Collection underestimateCount用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!