From 5b5651957b3f5219cd7d5ddd6e987d759ea103ec Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 18 Oct 2022 21:57:46 -0700 Subject: [PATCH] [PersistentSet] Doc updates --- .../PersistentSet+Debugging.swift | 6 ++ .../PersistentSet+Equatable.swift | 10 ++++ ...sistentSet+ExpressibleByArrayLiteral.swift | 21 ++++--- .../PersistentSet/PersistentSet+Filter.swift | 10 ++++ .../PersistentSet+Hashable.swift | 4 ++ ...ersistentSet+SetAlgebra Initializers.swift | 28 +++++++++ .../PersistentSet+SetAlgebra basics.swift | 57 +++++++++++++++++++ ...rsistentSet+SetAlgebra fluent basics.swift | 17 ++++++ .../PersistentSet+SetAlgebra isEqual.swift | 49 ++++++++++++++++ ...sistentSet+SetAlgebra isStrictSubset.swift | 1 + 10 files changed, 194 insertions(+), 9 deletions(-) diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift index 59363858..9c108c37 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift @@ -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 } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift index c4c388c2..82614846 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift @@ -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) diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift index b5b45297..d2eb3a8b 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift @@ -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) } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Filter.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Filter.swift index 3c57db5c..888a4447 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+Filter.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Filter.swift @@ -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 diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Hashable.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Hashable.swift index c9e3c0d1..42cbf623 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+Hashable.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Hashable.swift @@ -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 diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra Initializers.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra Initializers.swift index 10ce9bce..26608494 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra Initializers.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra Initializers.swift @@ -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(_ 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( _ item: __owned PersistentDictionary.Keys diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift index 06512108..99ece768 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift @@ -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? { diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra fluent basics.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra fluent basics.swift index 1e89ed28..9764db72 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra fluent basics.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra fluent basics.swift @@ -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) diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isEqual.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isEqual.swift index a5c856ca..f9a6bf1f 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isEqual.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isEqual.swift @@ -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( to other: PersistentDictionary.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(to other: S) -> Bool where S.Element == Element diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift index e5f36c0d..9b3b211c 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift @@ -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 }