[PersistentSet] Doc updates

This commit is contained in:
Karoy Lorentey committed 2022-10-18 21:57:46 -07:00
1 parent b9d8637900
commit 5b5651957b
10 files changed
+194 -9

No files matched your search

@@ -12,6 +12,12 @@
import _CollectionsUtilities
extension PersistentSet {
/// True if consistency checking is enabled in the implementation of this
/// type, false otherwise.
///
/// Documented performance promises are null and void when this property
/// returns true -- for example, operations that are documented to take
/// O(1) time might take O(*n*) time, or worse.
public static var _isConsistencyCheckingEnabled: Bool {
_isCollectionsInternalCheckingEnabled
}
@@ -12,6 +12,16 @@
import _CollectionsUtilities
extension PersistentSet: Equatable {
/// Returns a Boolean value indicating whether two values are equal.
///
/// Two persistent sets are considered equal if they contain the same
/// elements, but not necessarily in the same order.
///
/// - Note: This simply forwards to the ``isEqual(to:)-3m3in`` method.
/// That method has additional overloads that can be used to compare
/// persistent sets with additional types.
///
/// - Complexity: O(`min(left.count, right.count)`)
@inlinable @inline(__always)
public static func == (left: Self, right: Self) -> Bool {
left.isEqual(to: right)
@@ -12,19 +12,22 @@
extension PersistentSet: ExpressibleByArrayLiteral {
/// Creates a new set from the contents of an array literal.
///
/// Duplicate elements in the literal are allowed, but the resulting ordered
/// set will only contain the first occurrence of each.
/// Duplicate elements in the literal are allowed, but the resulting
/// persistent set will only contain the first occurrence of each.
///
/// Do not call this initializer directly. It is used by the compiler when
/// you use an array literal. Instead, create a new ordered set using an array
/// literal as its value by enclosing a comma-separated list of values in
/// square brackets. You can use an array literal anywhere an ordered set is
/// expected by the type context.
/// you use an array literal. Instead, create a new persistent set using an
/// array literal as its value by enclosing a comma-separated list of values
/// in square brackets. You can use an array literal anywhere a peristent set
/// is expected by the type context.
///
/// - Parameter elements: A variadic list of elements of the new ordered set.
/// Like the standard `Set`, persistent sets do not preserve the order of
/// elements inside the array literal.
///
/// - Complexity: O(`elements.count`) if `Element` implements
/// high-quality hashing.
/// - Parameter elements: A variadic list of elements of the new set.
///
/// - Complexity: O(`elements.count`) if `Element` properly implements
/// hashing.
public init(arrayLiteral elements: Element...) {
self.init(elements)
}
@@ -10,6 +10,16 @@
//===----------------------------------------------------------------------===//
extension PersistentSet {
/// Returns a new persistent set containing the values pairs of the ordered
/// set that satisfy the given predicate.
///
/// - Parameter isIncluded: A closure that takes a value as its
/// argument and returns a Boolean value indicating whether the value
/// should be included in the returned set.
///
/// - Returns: A set of the values that `isIncluded` allows.
///
/// - Complexity: O(`count`)
@inlinable
public func filter(
_ isIncluded: (Element) throws -> Bool
@@ -10,6 +10,10 @@
//===----------------------------------------------------------------------===//
extension PersistentSet: Hashable {
/// Hashes the essential components of this value by feeding them into the
/// given hasher.
///
/// Complexity: O(`count`)
@inlinable
public func hash(into hasher: inout Hasher) {
let copy = hasher
@@ -10,11 +10,27 @@
//===----------------------------------------------------------------------===//
extension PersistentSet {
/// Creates an empty set.
///
/// This initializer is equivalent to initializing with an empty array
/// literal.
///
/// - Complexity: O(1)
@inlinable
public init() {
self.init(_new: ._empty())
}
/// Creates a new set from a finite sequence of items.
///
/// - Parameter items: The elements to use as members of the new set.
/// The sequence is allowed to contain duplicate elements, but only
/// the first duplicate instance is preserved in the result.
///
/// - Complexity: This operation is expected to perform O(*n*)
/// hashing and equality comparisons on average (where *n*
/// is the number of elements in the sequence), provided that
/// `Element` properly implements hashing.
@inlinable
public init<S: Sequence>(_ items: __owned S) where S.Element == Element {
if S.self == Self.self {
@@ -27,11 +43,23 @@ extension PersistentSet {
}
}
/// Creates a new set from a an existing set. This is functionally the same as
/// copying the value of `items` into a new variable.
///
/// - Parameter items: The elements to use as members of the new set.
///
/// - Complexity: O(1)
@inlinable
public init(_ items: __owned Self) {
self = items
}
/// Creates a new persistent set from the keys view of an existing persistent
/// dictionary.
///
/// - Parameter items: The elements to use as members of the new set.
///
/// - Complexity: O(*items.count*)
@inlinable
public init<Value>(
_ item: __owned PersistentDictionary<Element, Value>.Keys
@@ -12,11 +12,39 @@
import _CollectionsUtilities
extension PersistentSet: SetAlgebra {
/// Returns a Boolean value that indicates whether the given element exists
/// in the set.
///
/// - Parameter element: An element to look for in the set.
///
/// - Returns: `true` if `member` exists in the set; otherwise, `false`.
///
/// - Complexity: This operation is expected to perform O(1) hashing and
/// comparison operations on average, provided that `Element` implements
/// high-quality hashing.
@inlinable
public func contains(_ item: Element) -> Bool {
_root.containsKey(.top, item, _Hash(item))
}
/// Insert a new member to this set, if the set doesn't already contain it.
///
/// - Parameter newMember: The element to insert into the set.
///
/// - Returns: `(true, newMember)` if `newMember` was not contained in the
/// set. If an element equal to `newMember` was already contained in the
/// set, the method returns `(false, oldMember)`, where `oldMember` is the
/// element that was equal to `newMember`. In some cases, `oldMember` may
/// be distinguishable from `newMember` by identity comparison or some
/// other means.
///
/// - Complexity: If `newMember` is not already a member of `self`, and if
/// `self` isn't uniquely held, then this operation is expected to copy
/// at most O(log(`count`)) existing members, as long as `Element` properly implements hashing.
///
/// In addition to this, this operation is expected to perform O(1)
/// hashing/comparison operations on the `Element` type (with the same
/// caveat.)
@discardableResult
@inlinable
public mutating func insert(
@@ -41,6 +69,18 @@ extension PersistentSet: SetAlgebra {
return r.inserted
}
/// Removes the given element from the set.
///
/// - Parameter member: The element of the set to remove.
///
/// - Returns: The element equal to `member` if `member` is contained in the
/// set; otherwise, `nil`. In some cases, the returned element may be
/// distinguishable from `newMember` by identity comparison or some other
/// means.
///
/// - Complexity: The operation is expected to perform amortized
/// O(1) hashing/comparison operations on the `Element` type, if it
/// properly implements hashing.
@discardableResult
@inlinable
public mutating func remove(_ member: Element) -> Element? {
@@ -49,6 +89,23 @@ extension PersistentSet: SetAlgebra {
return _root.remove(.top, member, hash)?.key
}
/// Inserts the given element into the set unconditionally.
///
/// If an element equal to `newMember` is already contained in the set,
/// `newMember` replaces the existing element.
///
/// If `newMember` was not already a member, it gets inserted.
///
/// - Parameter newMember: An element to insert into the set.
///
/// - Returns: The original member equal to `newMember` if the set already
/// contained such a member; otherwise, `nil`. In some cases, the returned
/// element may be distinguishable from `newMember` by identity comparison
/// or some other means.
///
/// - Complexity: This operation is expected to perform O(1)
/// hashing/comparison operations on the element type, provided that
/// `Element` properly implements hashing.
@discardableResult
@inlinable
public mutating func update(with newMember: __owned Element) -> Element? {
@@ -10,6 +10,23 @@
//===----------------------------------------------------------------------===//
extension PersistentSet {
/// Return a new set that contains `newMember` in addition to all existing
/// elements in this set.
///
/// If `self` already contains `newMember`, then this simply returns self.
///
/// - Parameter newMember: The element to insert into the set.
///
/// - Returns: A new set containing `newMember` and all existing members of
/// `self`.
///
/// - Complexity: The operation is expected to copy at most O(log(`count`))
/// existing members if `newMember` is not already a member of `self`,
/// as long as `Element` properly implements hashing.
///
/// In addition to this, this operation is expected to perform O(1)
/// hashing/comparison operations on the `Element` type (with the same
/// caveat.)
@inlinable
public func inserting(_ newMember: __owned Element) -> Self {
let hash = _Hash(newMember)
@@ -11,11 +11,37 @@
// FIXME: These are non-standard extensions generalizing ==.
extension PersistentSet {
/// Returns a Boolean value indicating whether persistent sets are equal. Two
/// persistent sets are considered equal if they contain the same elements,
/// but not necessarily in the same order.
///
/// - Parameter other: Another set.
///
/// - Returns: `true` if the set is equal to `other`; otherwise, `false`.
///
/// - Complexity: Generally O(`count`), as long as`Element` properly
/// implements hashing. That said, the implementation is careful to take
/// every available shortcut to reduce complexity, e.g. by skipping
/// comparing elements in shared subtrees.
@inlinable
public func isEqual(to other: Self) -> Bool {
_root.isEqual(to: other._root, by: { _, _ in true })
}
/// Returns a Boolean value indicating whether a persistent set compares equal
/// to the given keys view of a persistent dictionary. The two input
/// collections are considered equal if they contain the same elements,
/// but not necessarily in the same order.
///
/// - Parameter other: The keys view of a persistent dictionary.
///
/// - Returns: `true` if the set contains exactly the same members as `other`;
/// otherwise, `false`.
///
/// - Complexity: Generally O(`count`), as long as`Element` properly
/// implements hashing. That said, the implementation is careful to take
/// every available shortcut to reduce complexity, e.g. by skipping
/// comparing elements in shared subtrees.
@inlinable
public func isEqual<Value>(
to other: PersistentDictionary<Element, Value>.Keys
@@ -23,6 +49,29 @@ extension PersistentSet {
_root.isEqual(to: other._base._root, by: { _, _ in true })
}
/// Returns a Boolean value indicating whether this persistent set contains
/// the same elements as the given `other` sequence, but not necessarily
/// in the same order.
///
/// Duplicate items in `other` do not prevent it from comparing equal to
/// `self`.
///
/// let this: PersistentSet = [0, 1, 5, 6]
/// let that = [5, 5, 0, 1, 1, 6, 5, 0, 1, 6, 6, 5]
///
/// this.isEqual(to: that) // true
///
/// - Parameter other: The keys view of a persistent dictionary.
///
/// - Returns: `true` if the set contains exactly the same members as `other`;
/// otherwise, `false`. This function does not consider the order of
/// elements and it ignores duplicate items in `other`.
///
/// - Complexity: Generally O(*n*), where *n* is the number of items in
/// `other`, as long as`Element` properly implements hashing.
/// That said, the implementation is careful to take
/// every available shortcut to reduce complexity, e.g. by skipping
/// comparing elements in shared subtrees.
@inlinable
public func isEqual<S: Sequence>(to other: S) -> Bool
where S.Element == Element
@@ -100,6 +100,7 @@ extension PersistentSet {
return other.contains(where: { _ in true })
}
if let match = other._customContainsEquatableElement(first) {
// Fast path: the sequence has fast containment checks.
guard match else { return false }
while let item = it.next() {
guard other.contains(item) else { return false }