From 3d10964905bec2d240d73f0e7b0f4e498630ea95 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Sat, 24 Sep 2022 16:57:25 -0700 Subject: [PATCH] [PersistentSet] Flesh out isSubset/iSuperset/isDisjoint/etc overloads (untested) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds an experimental _FastMembershipCheckable protocol to speed up these relationship predicates when possible — having separate overloads for each known collection with a fast containment check is not going to scale for long. This commit also introduces a set of isEqual(to:) methods that generalize ==. --- .../BitCollections/BitSet/BitSet+Extras.swift | 4 ++ .../OrderedSet/OrderedSet+Extras.swift | 14 +++++++ .../PersistentDictionary+Keys.swift | 9 +++++ .../PersistentSet+Equatable.swift | 35 +++++++++++++++- .../PersistentSet+SetAlgebra basics.swift | 9 +++++ .../PersistentSet+SetAlgebra isDisjoint.swift | 11 ++++- ...sistentSet+SetAlgebra isStrictSubset.swift | 40 ++++++++++++++++++- ...stentSet+SetAlgebra isStrictSuperset.swift | 36 ++++++++++++++++- .../PersistentSet+SetAlgebra isSubset.swift | 36 ++++++++++++++++- .../PersistentSet+SetAlgebra isSuperset.swift | 12 +++++- .../_FastMembershipCheckable.swift | 27 +++++++++++++ 11 files changed, 223 insertions(+), 10 deletions(-) create mode 100644 Sources/OrderedCollections/OrderedSet/OrderedSet+Extras.swift create mode 100644 Sources/_CollectionsUtilities/_FastMembershipCheckable.swift diff --git a/Sources/BitCollections/BitSet/BitSet+Extras.swift b/Sources/BitCollections/BitSet/BitSet+Extras.swift index 4ac8f8e0..3333cbd3 100644 --- a/Sources/BitCollections/BitSet/BitSet+Extras.swift +++ b/Sources/BitCollections/BitSet/BitSet+Extras.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + +extension BitSet: _FastMembershipCheckable {} + extension BitSet { /// Creates a new empty bit set with enough storage capacity to store values /// up to the given maximum value without reallocating storage. diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Extras.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Extras.swift new file mode 100644 index 00000000..4efd0309 --- /dev/null +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Extras.swift @@ -0,0 +1,14 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import _CollectionsUtilities + +extension OrderedSet: _FastMembershipCheckable {} diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift index 6386e402..36e7f10e 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + extension PersistentDictionary { /// A view of a dictionary’s keys. @frozen @@ -67,6 +69,13 @@ extension PersistentDictionary.Keys: Sequence { } } +extension PersistentDictionary.Keys: _FastMembershipCheckable { + @inlinable + public func contains(_ element: Element) -> Bool { + _base._root.containsKey(.top, element, _Hash(element)) + } +} + extension PersistentDictionary.Keys: BidirectionalCollection { public typealias Index = PersistentDictionary.Index diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift index 3e3e1d28..806d70c8 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Equatable.swift @@ -9,9 +9,40 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + extension PersistentSet: Equatable { - @inlinable + @inlinable @inline(__always) public static func == (left: Self, right: Self) -> Bool { - left._root.isEqual(to: right._root, by: { _, _ in true }) + left.isEqual(to: right) + } +} + +// FIXME: These are non-standard extensions generalizing ==. +extension PersistentSet { + @inlinable + public func isEqual(to other: Self) -> Bool { + _root.isEqual(to: other._root, by: { _, _ in true }) + } + + @inlinable + public func isEqual( + to other: PersistentDictionary.Keys + ) -> Bool { + _root.isEqual(to: other._base._root, by: { _, _ in true }) + } + + @inlinable + public func isEqual(to other: S) -> Bool + where S.Element == Element + { + guard other.underestimatedCount <= self.count else { return false } + // FIXME: Would making this a BitSet of seen positions be better? + var seen: PersistentSet = [] + for item in other { + guard self.contains(item) else { return false } + guard seen._insert(item) else { return false } + } + return seen.count == self.count } } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift index c8c815d3..840dacc6 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra basics.swift @@ -9,6 +9,15 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + +extension PersistentSet: _FastMembershipCheckable { + @inlinable + public func contains(_ item: Element) -> Bool { + _root.containsKey(.top, item, _Hash(item)) + } +} + extension PersistentSet: SetAlgebra { @discardableResult @inlinable diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isDisjoint.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isDisjoint.swift index 8d499bde..937e3a03 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isDisjoint.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isDisjoint.swift @@ -17,8 +17,15 @@ extension PersistentSet { @inlinable public func isDisjoint( - with other: PersistentDictionary + with other: PersistentDictionary.Keys ) -> Bool { - self._root.isDisjoint(.top, with: other._root) + self._root.isDisjoint(.top, with: other._base._root) + } + + @inlinable + public func isDisjoint(with other: S) -> Bool + where S.Element == Element + { + other.allSatisfy { !self.contains($0) } } } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift index dd44e88c..f4505653 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSubset.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + extension PersistentSet { @inlinable public func isStrictSubset(of other: Self) -> Bool { @@ -18,9 +20,45 @@ extension PersistentSet { @inlinable public func isStrictSubset( - of other: PersistentDictionary + of other: PersistentDictionary.Keys ) -> Bool { guard self.count < other.count else { return false } return isSubset(of: other) } + + @inlinable + public func isStrictSubset( + of other: S + ) -> Bool + where S.Element == Element { + guard self.isSubset(of: other) else { return false } + return !other.allSatisfy { self.contains($0) } + } + + + @inlinable + public func isStrictSubset(of other: S) -> Bool + where S.Element == Element + { + if other.underestimatedCount > self.count { + return isSubset(of: other) + } + // FIXME: Would making this a BitSet of seen positions be better? + var seen: PersistentSet? = [] + var isStrict = false + for item in other { + if self.contains(item), seen?._insert(item) == true { + if seen?.count == self.count { + if isStrict { return true } + // Stop collecting seen items -- we just need to decide + // strictness now. + seen = nil + } + } else { + isStrict = true + if seen == nil { return true } + } + } + return false + } } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSuperset.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSuperset.swift index 8bdc3d70..94b54d03 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSuperset.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isStrictSuperset.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + extension PersistentSet { @inlinable public func isStrictSuperset(of other: Self) -> Bool { @@ -18,9 +20,39 @@ extension PersistentSet { @inlinable public func isStrictSuperset( - of other: PersistentDictionary + of other: PersistentDictionary.Keys ) -> Bool { guard self.count > other.count else { return false } - return other._root.isSubset(.top, of: self._root) + return other._base._root.isSubset(.top, of: self._root) + } + + @inlinable + public func isStrictSuperset( + of other: S + ) -> Bool + where S.Element == Element + { + if self.count < other.underestimatedCount { return false } + if !other.allSatisfy({ self.contains($0) }) { return false } + return !self.allSatisfy { other.contains($0) } + } + + @inlinable + public func isStrictSuperset(of other: S) -> Bool + where S.Element == Element + { + guard self.count >= other.underestimatedCount else { + return false + } + // FIXME: Would making this a BitSet of seen positions be better? + var seen: PersistentSet = [] + for item in other { + guard self.contains(item) else { return false } + if seen._insert(item), seen.count == self.count { + return false + } + } + assert(seen.count < self.count) + return true } } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSubset.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSubset.swift index ab735729..608564d7 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSubset.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSubset.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + extension PersistentSet { @inlinable public func isSubset(of other: Self) -> Bool { @@ -17,8 +19,38 @@ extension PersistentSet { @inlinable public func isSubset( - of other: PersistentDictionary + of other: PersistentDictionary.Keys ) -> Bool { - self._root.isSubset(.top, of: other._root) + self._root.isSubset(.top, of: other._base._root) + } + + @inlinable + public func isSubset( + of other: S + ) -> Bool + where S.Element == Element { + self.allSatisfy { other.contains($0) } + } + + @inlinable + public func isSubset( + of other: S + ) -> Bool + where S.Element == Element { + self.allSatisfy { other.contains($0) } + } + + @inlinable + public func isSubset(of other: S) -> Bool + where S.Element == Element + { + // FIXME: Would making this a BitSet of seen positions be better? + var seen: PersistentSet = [] + for item in other { + if contains(item), seen._insert(item), seen.count == self.count { + return true + } + } + return false } } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSuperset.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSuperset.swift index 49fdef53..3641eed6 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSuperset.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra isSuperset.swift @@ -19,6 +19,16 @@ extension PersistentSet { public func isSuperset( of other: PersistentDictionary ) -> Bool { - return other._root.isSubset(.top, of: self._root) + other._root.isSubset(.top, of: self._root) + } + + @inlinable + public func isSuperset(of other: S) -> Bool + where S.Element == Element + { + guard self.count >= other.underestimatedCount else { + return false + } + return other.allSatisfy { self.contains($0) } } } diff --git a/Sources/_CollectionsUtilities/_FastMembershipCheckable.swift b/Sources/_CollectionsUtilities/_FastMembershipCheckable.swift new file mode 100644 index 00000000..4b9fa608 --- /dev/null +++ b/Sources/_CollectionsUtilities/_FastMembershipCheckable.swift @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +/// A utility protocol for marking container types (not necessarily +/// conforming to `Sequence` or `Collection`) that provide a +/// fast `contains` operation. +public protocol _FastMembershipCheckable { + // FIXME: Add as primary associated type on >=5.7 + associatedtype Element: Equatable + + /// Returns a Boolean value that indicates whether the given element exists in `self`. + /// + /// - Performance: O(log(*n*)) or better, where *n* is the size + /// of `self` (for some definition of "size"). + func contains(_ item: Element) -> Bool +} + +extension Set: _FastMembershipCheckable {} +extension Dictionary.Keys: _FastMembershipCheckable {}