mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
This will simplify merge operations as we no longer have to spend effort on preventing repeated rehashings of items within collision nodes. Also, review and simplify node allocation and lookup code.
153 lines
3.7 KiB
Swift
153 lines
3.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// The storage header in a hash tree node. This includes data about the
|
|
/// current size and capacity of the node's storage region, as well as
|
|
/// information about the currently occupied hash table buckets.
|
|
@usableFromInline
|
|
@frozen
|
|
internal struct _StorageHeader {
|
|
@usableFromInline
|
|
internal var itemMap: _Bitmap
|
|
|
|
@usableFromInline
|
|
internal var childMap: _Bitmap
|
|
|
|
@usableFromInline
|
|
internal var _byteCapacity: UInt32
|
|
|
|
@usableFromInline
|
|
internal var _bytesFree: UInt32
|
|
|
|
@inlinable
|
|
internal init(byteCapacity: Int) {
|
|
assert(byteCapacity >= 0 && byteCapacity <= UInt32.max)
|
|
self.itemMap = .empty
|
|
self.childMap = .empty
|
|
self._byteCapacity = UInt32(truncatingIfNeeded: byteCapacity)
|
|
self._bytesFree = self._byteCapacity
|
|
}
|
|
}
|
|
|
|
extension _StorageHeader {
|
|
@inlinable @inline(__always)
|
|
internal var byteCapacity: Int {
|
|
get { Int(truncatingIfNeeded: _byteCapacity) }
|
|
}
|
|
|
|
@inlinable
|
|
internal var bytesFree: Int {
|
|
@inline(__always)
|
|
get { Int(truncatingIfNeeded: _bytesFree) }
|
|
set {
|
|
assert(newValue >= 0 && newValue <= UInt32.max)
|
|
_bytesFree = UInt32(truncatingIfNeeded: newValue)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension _StorageHeader {
|
|
@inlinable @inline(__always)
|
|
internal var isEmpty: Bool {
|
|
return itemMap.isEmpty && childMap.isEmpty
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
internal var isCollisionNode: Bool {
|
|
!itemMap.intersection(childMap).isEmpty
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
internal var hasChildren: Bool {
|
|
itemMap != childMap && !childMap.isEmpty
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
internal var hasItems: Bool {
|
|
!itemMap.isEmpty
|
|
}
|
|
|
|
@inlinable
|
|
internal var childCount: Int {
|
|
itemMap == childMap ? 0 : childMap.count
|
|
}
|
|
|
|
@inlinable
|
|
internal var itemCount: Int {
|
|
(itemMap == childMap
|
|
? Int(truncatingIfNeeded: itemMap._value)
|
|
: itemMap.count)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
internal var childrenEndSlot: _Slot {
|
|
_Slot(childCount)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
internal var itemsEndSlot: _Slot {
|
|
_Slot(itemCount)
|
|
}
|
|
|
|
@inlinable
|
|
internal var collisionCount: Int {
|
|
get {
|
|
assert(isCollisionNode)
|
|
return Int(truncatingIfNeeded: itemMap._value)
|
|
}
|
|
set {
|
|
assert(isCollisionNode || childMap.isEmpty)
|
|
assert(newValue > 0 && newValue < _Bitmap.Value.max)
|
|
itemMap._value = _Bitmap.Value(truncatingIfNeeded: newValue)
|
|
childMap = itemMap
|
|
}
|
|
}
|
|
}
|
|
|
|
extension _StorageHeader {
|
|
@inlinable
|
|
internal mutating func clear() {
|
|
itemMap = .empty
|
|
childMap = .empty
|
|
bytesFree = byteCapacity
|
|
}
|
|
}
|
|
|
|
extension _StorageHeader {
|
|
@inlinable
|
|
internal func bitmapsForInsertingItem(
|
|
at slot: _Slot,
|
|
_ bucket: _Bucket
|
|
) -> (itemMap: _Bitmap, childMap: _Bitmap) {
|
|
assert(!isCollisionNode)
|
|
assert(!bucket.isInvalid)
|
|
assert(!itemMap.contains(bucket) && !childMap.contains(bucket))
|
|
var itemMap = self.itemMap
|
|
itemMap.insert(bucket)
|
|
assert(itemMap.slot(of: bucket) == slot)
|
|
return (itemMap, childMap)
|
|
}
|
|
|
|
@inlinable
|
|
internal func bitmapsForSpawningChild(
|
|
at slot: _Slot,
|
|
_ bucket: _Bucket
|
|
) -> (itemMap: _Bitmap, childMap: _Bitmap) {
|
|
assert(!isCollisionNode)
|
|
assert(itemMap.contains(bucket))
|
|
var itemMap = self.itemMap
|
|
var childMap = self.childMap
|
|
itemMap.remove(bucket)
|
|
childMap.insert(bucket)
|
|
return (itemMap, childMap)
|
|
}
|
|
}
|