From 5dfd51238c683fe67162db69db2e774546f4dcec Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 19 Oct 2022 15:10:07 -0700 Subject: [PATCH] [OrderedSet] Review, test, fix, and document set relation predicates --- .../OrderedSet/OrderedSet+Equatable.swift | 5 + ...rderedSet+Partial SetAlgebra isEqual.swift | 80 ++++++ ...redSet+Partial SetAlgebra+Predicates.swift | 86 ++++++- .../OrderedSet.UnorderedView Tests.swift | 3 + .../OrderedSet/OrderedSetTests.swift | 237 +++++++++--------- 5 files changed, 293 insertions(+), 118 deletions(-) create mode 100644 Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqual.swift diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Equatable.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Equatable.swift index 1190cd6e..2c6dee53 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Equatable.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Equatable.swift @@ -15,6 +15,11 @@ extension OrderedSet: Equatable { /// Two ordered sets are considered equal if they contain the same /// elements in the same order. /// + /// - Note: This operator implements different behavior than the + /// `isEqual(to:)` method -- the latter implements an unordered + /// comparison, to match the behavior of members like `isSubset(of:)`, + /// `isStrictSuperset(of:)` etc. + /// /// - Complexity: O(`min(left.count, right.count)`) @inlinable public static func ==(left: Self, right: Self) -> Bool { diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqual.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqual.swift new file mode 100644 index 00000000..8f63fe72 --- /dev/null +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqual.swift @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// 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 { + /// Returns a Boolean value indicating whether two set values contain the + /// same elements, but not necessarily in the same order. + /// + /// - Note: This member implements different behavior than the `==(_:_:)` + /// operator -- the latter implements an ordered comparison, matching + /// the stricter concept of equality expected of an ordered collection + /// type. + /// + /// - Complexity: O(`min(left.count, right.count)`), as long as`Element` + /// properly implements hashing. + public func isEqual(to other: Self) -> Bool { + self.unordered == other.unordered + } + + /// Returns a Boolean value indicating whether two set values contain the + /// same elements, but not necessarily in the same order. + /// + /// - Complexity: O(`min(left.count, right.count)`), as long as`Element` + /// properly implements hashing. + public func isEqual(to other: UnorderedView) -> Bool { + self.unordered == other + } + + /// Returns a Boolean value indicating whether an ordered set contains the + /// same values as a given sequence, but not necessarily in the same + /// order. + /// + /// Duplicate items in `other` do not prevent it from comparing equal to + /// `self`. + /// + /// - Complexity: O(*n*), where *n* is the number of items in + /// `other`, as long as`Element` properly implements hashing. + public func isEqual(to other: S) -> Bool + where S.Element == Element { + if S.self == Self.self { + return isEqual(to: other as! Self) + } + + if self.isEmpty { + return other.allSatisfy { _ in false } + } + + if other is _UniqueCollection { + // We don't need to create a temporary set. + guard other.underestimatedCount <= self.count else { return false } + var seen = 0 + for item in other { + guard self.contains(item) else { return false } + seen &+= 1 + } + precondition( + seen <= self.count, + // Otherwise other.underestimatedCount != other.count + "Invalid Collection '\(S.self)' (bad underestimatedCount)") + return seen == self.count + } + + return _UnsafeBitset.withTemporaryBitset(capacity: count) { seen in + for item in other { + guard let index = _find(item).index else { return false } + _ = seen.insert(index) // Ignore dupes + } + return seen.count == count + } + } +} diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra+Predicates.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra+Predicates.swift index e975a0b4..c5f67ba7 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra+Predicates.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra+Predicates.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + // `OrderedSet` does not directly conform to `SetAlgebra` because its definition // of equality conflicts with `SetAlgebra` requirements. However, it still // implements most `SetAlgebra` requirements (except `insert`, which is replaced @@ -115,6 +117,22 @@ extension OrderedSet { of other: S ) -> Bool where S.Element == Element { guard !isEmpty else { return true } + + if S.self == Self.self { + return isSubset(of: other as! Self) + } + + var it = self.makeIterator() + let first = it.next()! + 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 } + } + return true + } + return _UnsafeBitset.withTemporaryBitset(capacity: count) { seen in // Mark elements in `self` that we've seen in `other`. for item in other { @@ -175,6 +193,22 @@ extension OrderedSet { isSuperset(of: other._base) } + /// Returns a Boolean value that indicates whether this set is a superset of + /// the given set. + /// + /// Set *A* is a superset of another set *B* if every member of *B* is also a + /// member of *A*, ignoring the order they appear in the two sets. + /// + /// let a: OrderedSet = [1, 2, 3, 4] + /// let b: Set = [4, 2, 1] + /// a.isSuperset(of: b) // true + /// + /// - Parameter other: Another set. + /// + /// - Returns: `true` if the set is a superset of `other`; otherwise, `false`. + /// + /// - Complexity: Expected to be O(`other.count`) on average, if `Element` + /// implements high-quality hashing. @inlinable public func isSuperset(of other: Set) -> Bool { guard self.count >= other.count else { return false } @@ -191,7 +225,8 @@ extension OrderedSet { /// let b: Array = [4, 2, 1] /// a.isSuperset(of: b) // true /// - /// - Parameter other: A finite sequence of elements. + /// - Parameter other: A finite sequence of elements, some of whose members + /// may appear more than once. (Duplicate items are ignored.) /// /// - Returns: `true` if the set is a superset of `other`; otherwise, `false`. /// @@ -208,6 +243,9 @@ extension OrderedSet { internal func _isSuperset( of other: S ) -> Bool where S.Element == Element { + if S.self == Self.self { + return self.isSuperset(of: other as! Self) + } for item in other { guard self.contains(item) else { return false } } @@ -311,7 +349,27 @@ extension OrderedSet { public func isStrictSubset( of other: S ) -> Bool where S.Element == Element { - _UnsafeBitset.withTemporaryBitset(capacity: count) { seen in + if S.self == Self.self { + return self.isStrictSubset(of: other as! Self) + } + if S.self == Set.self { + return self.isStrictSubset(of: other as! Set) + } + + var it = self.makeIterator() + guard let first = it.next() else { + 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 } + } + return !other.allSatisfy { self.contains($0) } + } + + return _UnsafeBitset.withTemporaryBitset(capacity: count) { seen in // Mark elements in `self` that we've seen in `other`. var isKnownStrict = false for item in other { @@ -414,7 +472,8 @@ extension OrderedSet { /// let b: Array = [4, 2, 1] /// a.isStrictSuperset(of: b) // true /// - /// - Parameter other: A finite sequence of elements. + /// - Parameter other: A finite sequence of elements, some of whose members + /// may appear more than once. (Duplicate items are ignored.) /// /// - Returns: `true` if `self` is a strict superset of `other`; otherwise, /// `false`. @@ -426,7 +485,26 @@ extension OrderedSet { public func isStrictSuperset( of other: S ) -> Bool where S.Element == Element { - _UnsafeBitset.withTemporaryBitset(capacity: count) { seen in + if S.self == Self.self { + return self.isStrictSuperset(of: other as! Self) + } + if S.self == Set.self { + return self.isStrictSuperset(of: other as! Set) + } + + var it = self.makeIterator() + guard let first = it.next() else { return false } + if let match = other._customContainsEquatableElement(first) { + // Fast path: the sequence has fast containment checks. + guard other.allSatisfy({ self.contains($0) }) else { return false } + guard match else { return true } + while let item = it.next() { + guard other.contains(item) else { return true } + } + return false + } + + return _UnsafeBitset.withTemporaryBitset(capacity: count) { seen in // Mark elements in `self` that we've seen in `other`. for item in other { guard let index = _find(item).index else { diff --git a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSet.UnorderedView Tests.swift b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSet.UnorderedView Tests.swift index b9021ca6..4d166253 100644 --- a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSet.UnorderedView Tests.swift +++ b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSet.UnorderedView Tests.swift @@ -13,6 +13,9 @@ import XCTest @_spi(Testing) import OrderedCollections import _CollectionsTestSupport +// Note: This cannot really work unless `UnorderedView` becomes a Collection. +// extension OrderedSet.UnorderedView: SetAPIChecker {} + class OrderedSetUnorderedViewTests: CollectionTestCase { func test_unordered_insert() { withEvery("count", in: 0 ..< 20) { count in diff --git a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift index a0f09547..fbec54fa 100644 --- a/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift +++ b/Tests/OrderedCollectionsTests/OrderedSet/OrderedSetTests.swift @@ -13,7 +13,7 @@ import XCTest @_spi(Testing) import OrderedCollections import _CollectionsTestSupport -extension OrderedSet: SetAPIChecker {} +extension OrderedSet: SetAPIExtras {} class OrderedSetTests: CollectionTestCase { func test_init_uncheckedUniqueElements_concrete() { @@ -1198,167 +1198,176 @@ class OrderedSetTests: CollectionTestCase { } } - func test_isSubset_Self() { + func test_isEqual() { + withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in + SampleRanges(unit: unit).withEveryPair { r1, r2 in + let expected = Set(r1) == Set(r2) + let a = OrderedSet(r1) + let b = OrderedSet(r2) + let c = Set(r2) + let d = Array(r2) + + func checkSequence( + _ set: OrderedSet, + _ other: S + ) -> Bool where S.Element == Int { + set.isEqual(to: other) + } + + expectEqual(a.isEqual(to: b), expected) + expectEqual(a.isEqual(to: b.unordered), expected) + expectEqual(a.isEqual(to: c), expected) + + expectEqual(checkSequence(a, b), expected) + expectEqual(checkSequence(a, c), expected) + expectEqual(a.isEqual(to: d), expected) + expectEqual(a.isEqual(to: d + d), expected) + expectEqual(a.isEqual(to: r2), expected) + } + } + } + + func test_isSubset() { withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in SampleRanges(unit: unit).withEveryPair { r1, r2 in let expected = Set(r1).isSubset(of: r2) let a = OrderedSet(r1) let b = OrderedSet(r2) + let c = Set(r2) + let d = Array(r2) + + func checkSequence( + _ set: OrderedSet, + _ other: S + ) -> Bool where S.Element == Int { + set.isSubset(of: other) + } + expectEqual(a.isSubset(of: b), expected) + expectEqual(a.isSubset(of: b.unordered), expected) + expectEqual(a.isSubset(of: c), expected) + + expectEqual(checkSequence(a, b), expected) + expectEqual(checkSequence(a, c), expected) + expectEqual(a.isSubset(of: d), expected) + expectEqual(a.isSubset(of: d + d), expected) + expectEqual(a.isSubset(of: r2), expected) } } } - func test_isSubset_Set() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isSubset(of: r2) - let a = OrderedSet(r1) - let b = Set(r2) - expectEqual(a.isSubset(of: b), expected) - } - } - } - - func test_isSubset_generic() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isSubset(of: r2) - let a = OrderedSet(r1) - let b = r2 - expectEqual(a.isSubset(of: b), expected) - } - } - } - - func test_isSuperset_Self() { + func test_isSuperset() { withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in SampleRanges(unit: unit).withEveryPair { r1, r2 in let expected = Set(r1).isSuperset(of: r2) let a = OrderedSet(r1) let b = OrderedSet(r2) + let c = Set(r2) + let d = Array(r2) + + func checkSequence( + _ set: OrderedSet, + _ other: S + ) -> Bool where S.Element == Int { + set.isSuperset(of: other) + } + expectEqual(a.isSuperset(of: b), expected) + expectEqual(a.isSuperset(of: b.unordered), expected) + expectEqual(a.isSuperset(of: c), expected) + + expectEqual(checkSequence(a, b), expected) + expectEqual(checkSequence(a, c), expected) + expectEqual(a.isSuperset(of: d), expected) + expectEqual(a.isSuperset(of: d + d), expected) + expectEqual(a.isSuperset(of: r2), expected) } } } - func test_isSuperset_Set() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isSuperset(of: r2) - let a = OrderedSet(r1) - let b = Set(r2) - expectEqual(a.isSuperset(of: b), expected) - } - } - } - - func test_isSuperset_generic() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isSuperset(of: r2) - let a = OrderedSet(r1) - let b = r2 - expectEqual(a.isSuperset(of: b), expected) - } - } - } - - func test_isStrictSubset_Self() { + func test_isStrictSubset() { withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in SampleRanges(unit: unit).withEveryPair { r1, r2 in let expected = Set(r1).isStrictSubset(of: r2) let a = OrderedSet(r1) let b = OrderedSet(r2) + let c = Set(r2) + let d = Array(r2) + + func checkSequence( + _ set: OrderedSet, + _ other: S + ) -> Bool where S.Element == Int { + set.isStrictSubset(of: other) + } + expectEqual(a.isStrictSubset(of: b), expected) + expectEqual(a.isStrictSubset(of: b.unordered), expected) + expectEqual(a.isStrictSubset(of: c), expected) + + expectEqual(checkSequence(a, b), expected) + expectEqual(checkSequence(a, c), expected) + expectEqual(a.isStrictSubset(of: d), expected) + expectEqual(a.isStrictSubset(of: d + d), expected) + expectEqual(a.isStrictSubset(of: r2), expected) } } } - func test_isStrictSubset_Set() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isStrictSubset(of: r2) - let a = OrderedSet(r1) - let b = Set(r2) - expectEqual(a.isStrictSubset(of: b), expected) - } - } - } - - func test_isStrictSubset_generic() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isStrictSubset(of: r2) - let a = OrderedSet(r1) - let b = r2 - expectEqual(a.isStrictSubset(of: b), expected) - } - } - } - - func test_isStrictSuperset_Self() { + func test_isStrictSuperset() { withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in SampleRanges(unit: unit).withEveryPair { r1, r2 in let expected = Set(r1).isStrictSuperset(of: r2) let a = OrderedSet(r1) let b = OrderedSet(r2) + let c = Set(r2) + let d = Array(r2) + + func checkSequence( + _ set: OrderedSet, + _ other: S + ) -> Bool where S.Element == Int { + set.isStrictSuperset(of: other) + } + expectEqual(a.isStrictSuperset(of: b), expected) + expectEqual(a.isStrictSuperset(of: b.unordered), expected) + expectEqual(a.isStrictSuperset(of: c), expected) + + expectEqual(checkSequence(a, b), expected) + expectEqual(checkSequence(a, c), expected) + expectEqual(a.isStrictSuperset(of: d), expected) + expectEqual(a.isStrictSuperset(of: d + d), expected) + expectEqual(a.isStrictSuperset(of: r2), expected) } } } - func test_isStrictSuperset_Set() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isStrictSuperset(of: r2) - let a = OrderedSet(r1) - let b = Set(r2) - expectEqual(a.isStrictSuperset(of: b), expected) - } - } - } - - func test_isStrictSuperset_generic() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isStrictSuperset(of: r2) - let a = OrderedSet(r1) - let b = r2 - expectEqual(a.isStrictSuperset(of: b), expected) - } - } - } - - func test_isDisjoint_Self() { + func test_isDisjoint() { withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in SampleRanges(unit: unit).withEveryPair { r1, r2 in let expected = Set(r1).isDisjoint(with: r2) let a = OrderedSet(r1) let b = OrderedSet(r2) - expectEqual(a.isDisjoint(with: b), expected) - } - } - } + let c = Set(r2) + let d = Array(r2) - func test_isDisjoint_Set() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isDisjoint(with: r2) - let a = OrderedSet(r1) - let b = Set(r2) - expectEqual(a.isDisjoint(with: b), expected) - } - } - } + func checkSequence( + _ set: OrderedSet, + _ other: S + ) -> Bool where S.Element == Int { + set.isDisjoint(with: other) + } - func test_isDisjoint_generic() { - withEvery("unit", in: [1, 3, 7, 10, 20, 50]) { unit in - SampleRanges(unit: unit).withEveryPair { r1, r2 in - let expected = Set(r1).isDisjoint(with: r2) - let a = OrderedSet(r1) - let b = r2 expectEqual(a.isDisjoint(with: b), expected) + expectEqual(a.isDisjoint(with: b.unordered), expected) + expectEqual(a.isDisjoint(with: c), expected) + + expectEqual(checkSequence(a, b), expected) + expectEqual(checkSequence(a, c), expected) + expectEqual(a.isDisjoint(with: d), expected) + expectEqual(a.isDisjoint(with: d + d), expected) + expectEqual(a.isDisjoint(with: r2), expected) } } }