Files

73 lines
2.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.4) && UnstableHashedContainers
@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 unsafe _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.
///
/// This operation is intended to be used just before resizing the table.
@inlinable
@unsafe
package mutating func _punchHole(at bucket: _Bucket) -> Element {
assert(unsafe _table.isOccupied(bucket))
unsafe _table.createHole(at: bucket)
let result = unsafe _memberPtr(at: bucket).move()
let members = unsafe _members.unsafelyUnwrapped
unsafe _table.finalizeHole(
at: bucket,
mover: {
unsafe (members + $1.offset).initialize(to: (members + $0.offset).move())
})
return result
}
@inlinable
@unsafe
package mutating func _remove(at bucket: _Bucket) -> Element {
assert(unsafe _table.isOccupied(bucket))
unsafe _table.createHole(at: bucket)
let result = unsafe _memberPtr(at: bucket).move()
let seed = self._seed
let members = unsafe _members.unsafelyUnwrapped
unsafe _table.resolveHole(
at: bucket,
hashGenerator: {
unsafe members[$0.offset]._rawHashValue(seed: seed)
},
mover: {
unsafe (members + $1.offset).initialize(to: (members + $0.offset).move())
})
return result
}
@inlinable
public mutating func removeAll() {
if isEmpty { return }
unsafe _deinitializeMembers()
unsafe _table.clear()
}
}
#endif