mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
Final touch up, polishes, and optimizations.
Key Changes: - Add duplicate handling functionality to _BTree.Builder - Remove transient allocation in SortedDictionary.values _modify - Add missing SortedDictionary.Values.subscript _modify - Add SortedSet(sortedElements:) - Add optimized SetAlgebra implementations - Remove remaining high-level offset APIs - Various optimizations - Expand internal documentation Minor Changes: - Add ability to create dummy node - _BTree.Builder.append(key: Key) for sets. - Fix bug where findAnyIndex(forKey:) didn't calculate the offset. - Remove old, unused files - findAnyCursor -> takeCursor (to emphasize consuming nature) - Add takeCursor(at: Index) - Optimize init(grouping:by:) to use modifyValue to reduce CoW Optimizations: - Use dummy node for transient _modify state rather than allocating new buffer
This commit is contained in:
30 files changed
+691
-282
No files matched your search
@@ -10,8 +10,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension SortedSet: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
// TODO: see if there is way to reduce boilerplate here
|
||||
|
||||
@inlinable
|
||||
public var description: String {
|
||||
var result = "["
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension SortedSet: Equatable {
|
||||
// TODO: look into more efficient ways to implement this
|
||||
|
||||
/// Returns a Boolean value indicating whether two values are equal.
|
||||
///
|
||||
/// Equality is the inverse of inequality. For any values `a` and `b`,
|
||||
|
||||
@@ -24,5 +24,30 @@ extension SortedSet {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: potentially add unconditional
|
||||
/// Creates a dictionary from a sequence of **sorted** elements.
|
||||
///
|
||||
/// This is a more efficient alternative to ``init(_:)`` which offers
|
||||
/// better asymptotic performance, and also reduces memory usage when constructing a
|
||||
/// sorted set on a pre-sorted sequence.
|
||||
///
|
||||
/// - Parameter elements: A sequence of elements in non-decreasing comparison order for the
|
||||
/// new set.
|
||||
/// - Complexity: O(`n`) where `n` is the number of elements in the
|
||||
/// sequence.
|
||||
@inlinable
|
||||
public init<S: Sequence>(
|
||||
sortedElements elements: S
|
||||
) where S.Element == Element {
|
||||
var builder = _Tree.Builder()
|
||||
|
||||
var previousElement: Element? = nil
|
||||
for element in elements {
|
||||
precondition(previousElement != nil && previousElement! < element,
|
||||
"Sequence out of order.")
|
||||
builder.append(element)
|
||||
previousElement = element
|
||||
}
|
||||
|
||||
self.init(_rootedAt: builder.finish())
|
||||
}
|
||||
}
|
||||
@@ -85,64 +85,201 @@ extension SortedSet: SetAlgebra {
|
||||
/// - Returns: A new sorted set with the unique elements of this set and `other`.
|
||||
/// - Note: if this set and `other` contain elements that are equal but
|
||||
/// distinguishable (e.g. via `===`), the element from the second set is inserted.
|
||||
/// - Complexity: O(max(`self.count`, `other.count`))
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public func union(_ other: __owned Self) -> Self {
|
||||
var builder = _Tree.Builder()
|
||||
var builder = _Tree.Builder(deduplicating: true)
|
||||
|
||||
var it1 = self.makeIterator()
|
||||
var it2 = other.makeIterator()
|
||||
|
||||
var e1 = it1.next()
|
||||
var e2 = it2.next()
|
||||
|
||||
// While both sequences have a value, consume the smallest element.
|
||||
while let el1 = e1, let el2 = e2 {
|
||||
if el1 < el2 {
|
||||
builder.append(el1)
|
||||
e1 = it1.next()
|
||||
} else {
|
||||
builder.append(el2)
|
||||
e2 = it2.next()
|
||||
}
|
||||
}
|
||||
|
||||
while let el1 = e1 {
|
||||
builder.append(el1)
|
||||
e1 = it1.next()
|
||||
}
|
||||
|
||||
while let el2 = e2 {
|
||||
builder.append(el2)
|
||||
e2 = it2.next()
|
||||
}
|
||||
|
||||
return SortedSet(_rootedAt: builder.finish())
|
||||
}
|
||||
|
||||
/// Adds the elements of the given set to the set.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Note: if this set and `other` contain elements that are equal but
|
||||
/// distinguishable (e.g. via `===`), the element from the second set is inserted.
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public mutating func formUnion(_ other: __owned Self) {
|
||||
self = union(other)
|
||||
}
|
||||
|
||||
/// Returns a new set with the elements that are common to both this set and
|
||||
/// the given set.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Returns: A new set.
|
||||
/// - Note: if this set and `other` contain elements that are equal but
|
||||
/// distinguishable (e.g. via `===`), which of these elements is present
|
||||
/// in the result is unspecified.
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public func intersection(_ other: Self) -> Self {
|
||||
// We'll run this such that 'self' is the smallest array
|
||||
// TODO: might want to consider uniqueness to minimize CoW copies.
|
||||
var builder = _Tree.Builder(deduplicating: true)
|
||||
|
||||
var it1 = self.makeIterator()
|
||||
var it2 = other.makeIterator()
|
||||
|
||||
while let e1 = it1.next(), let e2 = it2.next() {
|
||||
if e1 <= e2 {
|
||||
builder.append((e1, ()))
|
||||
// while
|
||||
var e1 = it1.next()
|
||||
var e2 = it2.next()
|
||||
|
||||
// While both sequences have a value, consume the smallest element.
|
||||
while let el1 = e1, let el2 = e2 {
|
||||
if el1 < el2 {
|
||||
e1 = it1.next()
|
||||
} else if el1 == el2 {
|
||||
builder.append(el2)
|
||||
e1 = it1.next()
|
||||
e2 = it2.next()
|
||||
} else {
|
||||
builder.append((e2, ()))
|
||||
// el1 > el1
|
||||
e2 = it2.next()
|
||||
}
|
||||
}
|
||||
|
||||
return SortedSet(_rootedAt: builder.finish())
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formUnion(_ other: __owned Self) {
|
||||
for element in other {
|
||||
self.insert(element)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func intersection(_ other: Self) -> Self {
|
||||
var newSet = SortedSet<Element>()
|
||||
for element in self {
|
||||
if other.contains(element) {
|
||||
newSet.insert(element)
|
||||
}
|
||||
}
|
||||
return newSet
|
||||
}
|
||||
|
||||
/// Removes the elements of this set that aren't also in the given set.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Note: if this set and `other` contain elements that are equal but
|
||||
/// distinguishable (e.g. via `===`), which of these elements is present
|
||||
/// in the result is unspecified.
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public mutating func formIntersection(_ other: Self) {
|
||||
for element in self {
|
||||
if !other.contains(element) {
|
||||
self.remove(element)
|
||||
}
|
||||
}
|
||||
self = intersection(other)
|
||||
}
|
||||
|
||||
/// Returns a new set with the elements that are either in this set or in the
|
||||
/// given set, but not in both.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Returns: A new set.
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public func symmetricDifference(_ other: Self) -> Self {
|
||||
var newSet = self.union(other)
|
||||
newSet.subtract(self.intersection(other))
|
||||
return newSet
|
||||
var builder = _Tree.Builder(deduplicating: true)
|
||||
|
||||
var it1 = self.makeIterator()
|
||||
var it2 = other.makeIterator()
|
||||
|
||||
var e1 = it1.next()
|
||||
var e2 = it2.next()
|
||||
|
||||
// While both sequences have a value, consume the smallest element.
|
||||
while let el1 = e1, let el2 = e2 {
|
||||
if el1 < el2 {
|
||||
builder.append(el1)
|
||||
e1 = it1.next()
|
||||
} else if el2 < el1 {
|
||||
builder.append(el2)
|
||||
e2 = it2.next()
|
||||
} else {
|
||||
// e1 == e2
|
||||
e1 = it1.next()
|
||||
e2 = it2.next()
|
||||
}
|
||||
}
|
||||
|
||||
while let el1 = e1 {
|
||||
builder.append(el1)
|
||||
e1 = it1.next()
|
||||
}
|
||||
|
||||
while let el2 = e2 {
|
||||
builder.append(el2)
|
||||
e2 = it2.next()
|
||||
}
|
||||
|
||||
return SortedSet(_rootedAt: builder.finish())
|
||||
}
|
||||
|
||||
/// Removes the elements of the set that are also in the given set and adds
|
||||
/// the members of the given set that are not already in the set.
|
||||
///
|
||||
/// - Parameter other: A set of the same type.
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public mutating func formSymmetricDifference(_ other: Self) {
|
||||
self.formUnion(other)
|
||||
self.subtract(self.intersection(other))
|
||||
self = self.symmetricDifference(other)
|
||||
}
|
||||
|
||||
/// Returns a new set containing the elements of this set that do not occur
|
||||
/// in the given set.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Returns: A new set.
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public func subtracting(_ other: Self) -> Self {
|
||||
var builder = _Tree.Builder(deduplicating: true)
|
||||
|
||||
var it1 = self.makeIterator()
|
||||
var it2 = other.makeIterator()
|
||||
|
||||
var e1 = it1.next()
|
||||
var e2 = it2.next()
|
||||
|
||||
// While both sequences have a value, consume the smallest element.
|
||||
while let el1 = e1, let el2 = e2 {
|
||||
if el1 < el2 {
|
||||
builder.append(el1)
|
||||
e1 = it1.next()
|
||||
} else if el2 < el1 {
|
||||
e2 = it2.next()
|
||||
} else {
|
||||
// e1 == e2
|
||||
e1 = it1.next()
|
||||
e2 = it2.next()
|
||||
}
|
||||
}
|
||||
|
||||
while let el1 = e1 {
|
||||
builder.append(el1)
|
||||
e1 = it1.next()
|
||||
}
|
||||
|
||||
return SortedSet(_rootedAt: builder.finish())
|
||||
}
|
||||
|
||||
/// Removes the elements of the given set from this set.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Complexity: O(`self.count` + `other.count`)
|
||||
@inlinable
|
||||
public mutating func subtract(_ other: SortedSet<Element>) {
|
||||
self = self.subtracting(other)
|
||||
}
|
||||
|
||||
// MARK: Comparing Sets
|
||||
@@ -156,6 +293,11 @@ extension SortedSet: SetAlgebra {
|
||||
@inlinable
|
||||
public func isSubset(of other: SortedSet<Element>) -> Bool {
|
||||
// TODO: could be worthwhile to evaluate recursive approach
|
||||
// TODO: in some cases, it could be faster to search from the root each time
|
||||
// Searching from the root is faster when:
|
||||
// self.count < other.count / (log(other.count) - 1)
|
||||
// This means when `other` is significantly larger than `self`, it may be
|
||||
// faster to search from the root each time.
|
||||
if self.count > other.count { return false }
|
||||
|
||||
var superIterator = other.makeIterator()
|
||||
@@ -178,4 +320,51 @@ extension SortedSet: SetAlgebra {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/// Returns a Boolean value that indicates whether this set is a strict
|
||||
/// subset of the given set.
|
||||
///
|
||||
/// Set *A* is a strict subset of another set *B* if every member of *A* is
|
||||
/// also a member of *B* and *B* contains at least one element that is not a
|
||||
/// member of *A*.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Returns: `true` if the set is a strict subset of `other`; otherwise,
|
||||
/// `false`.
|
||||
/// - Complexity: O(`self.count` + `other.count`).
|
||||
@inlinable
|
||||
public func isStrictSubset(of other: SortedSet<Element>) -> Bool {
|
||||
if self.count >= other.count { return false }
|
||||
return self.isSubset(of: other)
|
||||
}
|
||||
|
||||
/// Returns a Boolean value that indicates whether the set has no members in
|
||||
/// common with the given set.
|
||||
///
|
||||
/// - Parameter other: A set of the same type as the current set.
|
||||
/// - Returns: `true` if the set has no elements in common with `other`;
|
||||
/// otherwise, `false`.
|
||||
/// - Complexity: O(`self.count` + `other.count`).
|
||||
@inlinable
|
||||
public func isDisjoint(with other: SortedSet<Element>) -> Bool {
|
||||
var it1 = self.makeIterator()
|
||||
var it2 = other.makeIterator()
|
||||
|
||||
var e1 = it1.next()
|
||||
var e2 = it2.next()
|
||||
|
||||
// While both sequences have a value, consume the smallest element.
|
||||
while let el1 = e1, let el2 = e2 {
|
||||
if el1 < el2 {
|
||||
e1 = it1.next()
|
||||
} else if el1 == el2 {
|
||||
return false
|
||||
} else {
|
||||
// el1 > el2
|
||||
e2 = it2.next()
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,6 @@ extension SortedSet {
|
||||
|
||||
// MARK: Equatable
|
||||
extension SortedSet.Index: Equatable {
|
||||
// TODO: Potentially validate if lhs & rhs aren't pointing to deallocated dictionaries.
|
||||
@inlinable
|
||||
public static func ==(lhs: SortedSet.Index, rhs: SortedSet.Index) -> Bool {
|
||||
lhs._index.ensureValid(with: rhs._index)
|
||||
|
||||
@@ -35,23 +35,3 @@ public struct SortedSet<Element: Comparable> {
|
||||
self._root = tree
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Testing for Membership
|
||||
|
||||
// MARK: Adding Elements
|
||||
extension SortedSet {
|
||||
// TODO: document
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
@discardableResult
|
||||
public mutating func remove(atOffset offset: Int) -> Element {
|
||||
return self._root.remove(atOffset: offset).key
|
||||
}
|
||||
|
||||
// TODO: document
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public subscript(offset absoluteOffset: Int) -> Element {
|
||||
return self._root.index(atOffset: absoluteOffset).element.key
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user