Swift 里 Set (三)Inspecting a Set
isEmpty
/// A Boolean value that indicates whether the set is empty.
@inlinable
public var isEmpty: Bool {
return count == 0
}
count
/// The number of elements in the set.
///
/// - Complexity: O(1).
@inlinable
public var count: Int {
return _variant.count
}
最后会返回__RawSetStorage
里的count
/// The current number of occupied entries in this set.
@usableFromInline
@nonobjc
internal final var _count: Int
在修改Set
的时候,会不断调整这个值。
internal mutating func _delete(at bucket: Bucket) {
...
_storage._count -= 1
...
}
internal func _unsafeInsertNew(_ element: __owned Element) {
...
_storage._count &+= 1
}
capacity
/// The total number of elements that the set can contain without
/// allocating new storage.
@inlinable
public var capacity: Int {
return _variant.capacity
}
注意不是实际占用内存的大小,因为要预留部分空闲空间,防止性能下降。
storage._capacity = _HashTable.capacity(forScale: scale)
下起雨,也要勇敢前行