diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Removals.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Removals.swift new file mode 100644 index 00000000..6cc8ff39 --- /dev/null +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Removals.swift @@ -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 diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift b/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift index e5675910..6c0a337a 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift @@ -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 diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift new file mode 100644 index 00000000..5a5c6cf5 --- /dev/null +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Removals.swift @@ -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 diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+Removals.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+Removals.swift index 931e7574..6f8c2831 100644 --- a/Sources/BasicContainers/UniqueSet/UniqueSet+Removals.swift +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+Removals.swift @@ -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. diff --git a/Tests/BasicContainersTests/RigidDictionaryTests.swift b/Tests/BasicContainersTests/RigidDictionaryTests.swift index 4a37b792..b3d9e1b2 100644 --- a/Tests/BasicContainersTests/RigidDictionaryTests.swift +++ b/Tests/BasicContainersTests/RigidDictionaryTests.swift @@ -278,6 +278,67 @@ class RigidDictionaryTests: CollectionTestCase { } } } + + func test_removeValueForKey_one() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in + withEvery("count", in: [1, capacity / 3, capacity / 2, 2 * capacity / 3, capacity - 1, capacity] as Set) { count in + guard count > 0 else { return } + withSome("key", in: 0 ..< count) { key in + withLifetimeTracking { tracker in + var d = RigidDictionary(capacity: capacity) + for i in 0 ..< count { + let key = tracker.instance(for: i) + let value = tracker.instance(for: "\(i)") + d.insertValue(value, forKey: key) + } + + let oldValue = d.removeValue(forKey: tracker.instance(for: key)) + expectNotNil(oldValue) { + expectEqual($0.payload, "\(key)") + } + expectEqual(d.count, count - 1) + expectEqual(d.capacity, capacity) + + for i in 0 ..< count { + let value = d.withValue(forKey: tracker.instance(for: i)) { $0 } + if i == key { + expectNil(value) + } else { + expectEqual(value?.payload, "\(i)") + } + } + } + } + } + } + } + + func test_removeValueForKey_all() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in + withLifetimeTracking { tracker in + var d = RigidDictionary(capacity: capacity) + for i in 0 ..< capacity { + let key = tracker.instance(for: i) + let value = tracker.instance(for: "\(i)") + d.insertValue(value, forKey: key) + } + + withEvery("key", in: 0 ..< capacity) { key in + let oldValue = d.removeValue(forKey: tracker.instance(for: key)) + expectNotNil(oldValue) { + expectEqual($0.payload, "\(key)") + } + expectEqual(d.count, capacity - key - 1) + expectEqual(d.capacity, capacity) + } + expectEqual(d.count, 0) + } + } + } } #endif diff --git a/Tests/BasicContainersTests/UniqueDictionaryTests.swift b/Tests/BasicContainersTests/UniqueDictionaryTests.swift index d6318382..3474bc20 100644 --- a/Tests/BasicContainersTests/UniqueDictionaryTests.swift +++ b/Tests/BasicContainersTests/UniqueDictionaryTests.swift @@ -57,4 +57,66 @@ class UniqueDictionaryTests: CollectionTestCase { } } } + + func test_removeValueForKey_one() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("minCap", in: [1, 2, 10, 100, 200]) { minCap in + withEvery("count", in: [1, minCap / 3, minCap / 2, 2 * minCap / 3, minCap - 1, minCap] as Set) { count in + guard count > 0 else { return } + withSome("key", in: 0 ..< count) { key in + withLifetimeTracking { tracker in + var d = UniqueDictionary(minimumCapacity: minCap) + for i in 0 ..< count { + let key = tracker.instance(for: i) + let value = tracker.instance(for: "\(i)") + d.insertValue(value, forKey: key) + } + + let oldValue = d.removeValue(forKey: tracker.instance(for: key)) + expectNotNil(oldValue) { + expectEqual($0.payload, "\(key)") + } + expectEqual(d.count, count - 1) + + for i in 0 ..< count { + let value = d.withValue(forKey: tracker.instance(for: i)) { $0 } + if i == key { + expectNil(value) + } else { + expectEqual(value?.payload, "\(i)") + } + } + } + } + } + } + } + + func test_removeValueForKey_all() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("minCap", in: [1, 2, 10, 100, 200]) { minCap in + withLifetimeTracking { tracker in + var d = UniqueDictionary(minimumCapacity: minCap) + let capacity = d.capacity + for i in 0 ..< d.capacity { + let key = tracker.instance(for: i) + let value = tracker.instance(for: "\(i)") + d.insertValue(value, forKey: key) + } + expectEqual(d.capacity, capacity) + + withEvery("key", in: 0 ..< d.capacity) { key in + let oldValue = d.removeValue(forKey: tracker.instance(for: key)) + expectNotNil(oldValue) { + expectEqual($0.payload, "\(key)") + } + expectEqual(d.count, capacity - key - 1) + } + expectEqual(d.count, 0) + } + } + } + }