[PersistentSet] Flesh out isSubset/iSuperset/isDisjoint/etc overloads (untested)

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 ==.
This commit is contained in:
Karoy Lorentey committed 2022-09-24 17:02:16 -07:00
1 parent 00ae83df1e
commit 3d10964905
11 files changed
+223 -10

No files matched your search

@@ -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.
@@ -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 {}
@@ -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
@@ -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<Value>(
to other: PersistentDictionary<Element, Value>.Keys
) -> Bool {
_root.isEqual(to: other._base._root, by: { _, _ in true })
}
@inlinable
public func isEqual<S: Sequence>(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
}
}
@@ -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
@@ -17,8 +17,15 @@ extension PersistentSet {
@inlinable
public func isDisjoint<Value>(
with other: PersistentDictionary<Element, Value>
with other: PersistentDictionary<Element, Value>.Keys
) -> Bool {
self._root.isDisjoint(.top, with: other._root)
self._root.isDisjoint(.top, with: other._base._root)
}
@inlinable
public func isDisjoint<S: Sequence>(with other: S) -> Bool
where S.Element == Element
{
other.allSatisfy { !self.contains($0) }
}
}
@@ -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<Value>(
of other: PersistentDictionary<Element, Value>
of other: PersistentDictionary<Element, Value>.Keys
) -> Bool {
guard self.count < other.count else { return false }
return isSubset(of: other)
}
@inlinable
public func isStrictSubset<S: Sequence & _FastMembershipCheckable>(
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<S: Sequence>(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
}
}
@@ -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<Value>(
of other: PersistentDictionary<Element, Value>
of other: PersistentDictionary<Element, Value>.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<S: Sequence & _FastMembershipCheckable>(
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<S: Sequence>(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
}
}
@@ -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<Value>(
of other: PersistentDictionary<Element, Value>
of other: PersistentDictionary<Element, Value>.Keys
) -> Bool {
self._root.isSubset(.top, of: other._root)
self._root.isSubset(.top, of: other._base._root)
}
@inlinable
public func isSubset<S: _FastMembershipCheckable>(
of other: S
) -> Bool
where S.Element == Element {
self.allSatisfy { other.contains($0) }
}
@inlinable
public func isSubset<S: Sequence & _FastMembershipCheckable>(
of other: S
) -> Bool
where S.Element == Element {
self.allSatisfy { other.contains($0) }
}
@inlinable
public func isSubset<S: Sequence>(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
}
}
@@ -19,6 +19,16 @@ extension PersistentSet {
public func isSuperset<Value>(
of other: PersistentDictionary<Element, Value>
) -> Bool {
return other._root.isSubset(.top, of: self._root)
other._root.isSubset(.top, of: self._root)
}
@inlinable
public func isSuperset<S: Sequence>(of other: S) -> Bool
where S.Element == Element
{
guard self.count >= other.underestimatedCount else {
return false
}
return other.allSatisfy { self.contains($0) }
}
}
@@ -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 {}