[BasicContainers] Add removal operations to dictionaries

This commit is contained in:
Karoy Lorentey committed 2026-02-23 13:59:36 -08:00
1 parent 8624f936ea
commit 2654a0711e
6 files changed
+235 -8

No files matched your search

@@ -0,0 +1,68 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2026 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS
@available(SwiftStdlib 5.0, *)
extension RigidDictionary where Key: ~Copyable, Value: ~Copyable {
@inlinable
public mutating func removeValue(forKey key: borrowing Key) -> Value? {
let r = _keys._find(key)
guard let bucket = r.bucket else { return nil }
return _removeValue(at: bucket)
}
@inlinable
package mutating func _removeValue(at bucket: _Bucket) -> Value {
assert(self._keys._table.isOccupied(bucket))
self._keys._table.createHole(at: bucket)
self._keyPtr(at: bucket).deinitialize(count: 1)
let oldValue = self._valuePtr(at: bucket).move()
let seed = self._keys._seed
let keys = self._keys._members.unsafelyUnwrapped
let values = self._values.unsafelyUnwrapped
self._keys._table.resolveHole(
at: bucket,
hashGenerator: {
keys[$0.offset]._rawHashValue(seed: seed)
},
mover: {
(keys + $1.offset).initialize(to: (keys + $0.offset).move())
(values + $1.offset).initialize(to: (values + $0.offset).move())
})
return oldValue
}
/// Remove the member currently at the specified occupied bucket,
/// and mark it as unoccupied, without restoring the hash table's
/// invariants. Lookup operations may fail after this.
///
/// This operation is intended to be used just before resizing the table.
@inlinable
package mutating func _punchHole(at bucket: _Bucket) -> Value {
assert(self._keys._table.isOccupied(bucket))
self._keys._table.createHole(at: bucket)
self._keyPtr(at: bucket).deinitialize(count: 1)
let oldValue = self._valuePtr(at: bucket).move()
let keys = self._keys._members.unsafelyUnwrapped
let values = self._values.unsafelyUnwrapped
self._keys._table.finalizeHole(
at: bucket,
mover: {
(keys + $1.offset).initialize(to: (keys + $0.offset).move())
(values + $1.offset).initialize(to: (values + $0.offset).move())
})
return oldValue
}
}
#endif
@@ -13,6 +13,13 @@
@available(SwiftStdlib 5.0, *)
extension RigidSet where Element: ~Copyable {
@inlinable
public mutating func remove(_ member: borrowing Element) -> Element? {
let r = _find(member)
guard let bucket = r.bucket else { return nil }
return _remove(at: bucket)
}
/// Remove the member currently at the specified occupied bucket,
/// and mark it as unoccupied, without restoring the hash table's
/// invariants. Lookup operations may fail after this.
@@ -49,13 +56,6 @@ extension RigidSet where Element: ~Copyable {
})
return result
}
@inlinable
public mutating func remove(_ member: borrowing Element) -> Element? {
let r = _find(member)
guard let bucket = r.bucket else { return nil }
return _remove(at: bucket)
}
}
#endif
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2026 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS
@available(SwiftStdlib 5.0, *)
extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable {
@inlinable
public mutating func removeValue(forKey key: borrowing Key) -> Value? {
let r = _storage._keys._find(key)
guard let bucket = r.bucket else { return nil }
return _removeValue(at: bucket)
}
@inlinable
package mutating func _removeValue(at bucket: _Bucket) -> Value {
guard self.count <= _HTable.minimumCapacity(forScale: self._scale) else {
return _storage._removeValue(at: bucket)
}
// Shrink storage.
let result = _storage._punchHole(at: bucket)
_resize(minimumCapacity: self.count)
return result
}
}
#endif
@@ -15,7 +15,7 @@
extension UniqueSet where Element: ~Copyable {
@inlinable
package mutating func _remove(at bucket: _Bucket) -> Element {
guard self.count == _HTable.minimumCapacity(forScale: self._scale) else {
guard self.count <= _HTable.minimumCapacity(forScale: self._scale) else {
return _storage._remove(at: bucket)
}
// Shrink storage.