[PersistentDictionary] Apply leading underscore rule to common functions

This commit is contained in:
Karoy Lorentey committed 2022-09-07 21:06:19 -07:00
1 parent 1f7eb2f95d
commit 2597ff23cd
9 files changed
+140 -140

No files matched your search

@@ -28,7 +28,7 @@ extension PersistentDictionary: Collection {
/// Returns the index for the given key.
///
public func index(forKey key: Key) -> Self.Index? {
return rootNode.index(key, computeHash(key), 0, 0)
return rootNode.index(key, _computeHash(key), 0, 0)
}
///
@@ -28,7 +28,7 @@ extension PersistentDictionary: Sequence {
internal init(_root root: DictionaryNode) {
trieIteratorStackRemainder = []
trieIteratorStackRemainder.reserveCapacity(maxDepth)
trieIteratorStackRemainder.reserveCapacity(_maxDepth)
if root.hasNodes {
trieIteratorStackTop = root._trieSlice.makeIterator()
@@ -93,11 +93,11 @@ public struct PersistentDictionary<Key, Value> where Key: Hashable {
}
public func contains(_ key: Key) -> Bool {
rootNode.containsKey(key, computeHash(key), 0)
rootNode.containsKey(key, _computeHash(key), 0)
}
func get(_ key: Key) -> Value? {
rootNode.get(key, computeHash(key), 0)
rootNode.get(key, _computeHash(key), 0)
}
@discardableResult
@@ -105,7 +105,7 @@ public struct PersistentDictionary<Key, Value> where Key: Hashable {
let isUnique = isKnownUniquelyReferenced(&self.rootNode)
var effect = _DictionaryEffect<Value>()
let keyHash = computeHash(key)
let keyHash = _computeHash(key)
let newRootNode = rootNode.updateOrUpdating(
isUnique, key, value, keyHash, 0, &effect)
@@ -120,7 +120,7 @@ public struct PersistentDictionary<Key, Value> where Key: Hashable {
// fluid/immutable API
public func updatingValue(_ value: Value, forKey key: Key) -> Self {
var effect = _DictionaryEffect<Value>()
let keyHash = computeHash(key)
let keyHash = _computeHash(key)
let newRootNode = rootNode.updateOrUpdating(
false, key, value, keyHash, 0, &effect)
@@ -134,7 +134,7 @@ public struct PersistentDictionary<Key, Value> where Key: Hashable {
let isUnique = isKnownUniquelyReferenced(&self.rootNode)
var effect = _DictionaryEffect<Value>()
let keyHash = computeHash(key)
let keyHash = _computeHash(key)
let newRootNode = rootNode.removeOrRemoving(
isUnique, key, keyHash, 0, &effect)
@@ -149,7 +149,7 @@ public struct PersistentDictionary<Key, Value> where Key: Hashable {
// fluid/immutable API
public func removingValue(forKey key: Key) -> Self {
var effect = _DictionaryEffect<Value>()
let keyHash = computeHash(key)
let keyHash = _computeHash(key)
let newRootNode = rootNode.removeOrRemoving(false, key, keyHash, 0, &effect)
if effect.modified {
@@ -20,8 +20,8 @@ internal struct _BaseIterator<T: _Node> {
private var currentStackLevel: Int = -1
private var nodeCursorsAndLengths: [Int] = (
Array(repeating: 0, count: maxDepth * 2))
private var nodes: [T?] = Array(repeating: nil, count: maxDepth)
Array(repeating: 0, count: _maxDepth * 2))
private var nodes: [T?] = Array(repeating: nil, count: _maxDepth)
init(rootNode: T) {
if rootNode.hasNodes { pushNode(rootNode) }
@@ -17,8 +17,8 @@ internal struct _BaseReverseIterator<T: _Node> {
var currentValueNode: T? = nil
private var currentStackLevel: Int = -1
private var nodeIndex: [Int] = Array(repeating: 0, count: maxDepth + 1)
private var nodeStack: [T?] = Array(repeating: nil, count: maxDepth + 1)
private var nodeIndex: [Int] = Array(repeating: 0, count: _maxDepth + 1)
private var nodeStack: [T?] = Array(repeating: nil, count: _maxDepth + 1)
init(rootNode: T) {
pushNode(rootNode)
@@ -9,8 +9,8 @@
//
//===----------------------------------------------------------------------===//
fileprivate let initialDataCapacity: Capacity = 4
fileprivate let initialTrieCapacity: Capacity = 1
fileprivate let initialDataCapacity: _Capacity = 4
fileprivate let initialTrieCapacity: _Capacity = 1
final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
@@ -32,8 +32,8 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
header.hashCollision
}
let dataCapacity: Capacity
let trieCapacity: Capacity
let dataCapacity: _Capacity
let trieCapacity: _Capacity
let dataBaseAddress: UnsafeMutablePointer<DataBufferElement>
let trieBaseAddress: UnsafeMutablePointer<TrieBufferElement>
@@ -50,18 +50,18 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
@inline(__always)
var dataMap: Bitmap {
var dataMap: _Bitmap {
header.dataMap
}
@inline(__always)
var trieMap: Bitmap {
var trieMap: _Bitmap {
header.trieMap
}
@inlinable
static func _allocate(
dataCapacity: Capacity, trieCapacity: Capacity
dataCapacity: _Capacity, trieCapacity: _Capacity
) -> (
dataBaseAddress: UnsafeMutablePointer<DataBufferElement>,
trieBaseAddress: UnsafeMutablePointer<TrieBufferElement>
@@ -86,10 +86,10 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
func copy(
withDataCapacityFactor dataCapacityFactor: Capacity = 1,
withDataCapacityShrinkFactor dataCapacityShrinkFactor: Capacity = 1,
withTrieCapacityFactor trieCapacityFactor: Capacity = 1,
withTrieCapacityShrinkFactor trieCapacityShrinkFactor: Capacity = 1
withDataCapacityFactor dataCapacityFactor: _Capacity = 1,
withDataCapacityShrinkFactor dataCapacityShrinkFactor: _Capacity = 1,
withTrieCapacityFactor trieCapacityFactor: _Capacity = 1,
withTrieCapacityShrinkFactor trieCapacityShrinkFactor: _Capacity = 1
) -> Self {
let src = self
let dc = src.dataCapacity &* dataCapacityFactor / dataCapacityShrinkFactor
@@ -127,9 +127,9 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
if hashCollision {
let hash = computeHash(_dataSlice.first!.key)
let hash = _computeHash(_dataSlice.first!.key)
guard _dataSlice.allSatisfy({ computeHash($0.key) == hash }) else {
guard _dataSlice.allSatisfy({ _computeHash($0.key) == hash }) else {
return false
}
}
@@ -149,7 +149,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
UnsafeMutableBufferPointer(start: trieBaseAddress, count: header.trieCount)
}
init(dataCapacity: Capacity, trieCapacity: Capacity) {
init(dataCapacity: _Capacity, trieCapacity: _Capacity) {
let (dataBaseAddress, trieBaseAddress) = Self._allocate(
dataCapacity: dataCapacity,
trieCapacity: trieCapacity)
@@ -176,7 +176,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
assert(self.invariant)
}
convenience init(dataMap: Bitmap, firstKey: Key, firstValue: Value) {
convenience init(dataMap: _Bitmap, firstKey: Key, firstValue: Value) {
self.init()
self.header = Header(dataMap: dataMap, trieMap: 0)
@@ -188,7 +188,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
convenience init(
dataMap: Bitmap,
dataMap: _Bitmap,
firstKey: Key,
firstValue: Value,
secondKey: Key,
@@ -206,7 +206,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
convenience init(
trieMap: Bitmap,
trieMap: _Bitmap,
firstNode: BitmapIndexedDictionaryNode<Key, Value>
) {
self.init()
@@ -220,8 +220,8 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
convenience init(
dataMap: Bitmap,
trieMap: Bitmap,
dataMap: _Bitmap,
trieMap: _Bitmap,
firstKey: Key,
firstValue: Value,
firstNode: BitmapIndexedDictionaryNode<Key, Value>
@@ -238,11 +238,11 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
convenience init(collisions: [ReturnPayload]) {
self.init(dataCapacity: Capacity(collisions.count), trieCapacity: 0)
self.init(dataCapacity: _Capacity(collisions.count), trieCapacity: 0)
self.header = Header(
dataMap: Bitmap(collisions.count),
trieMap: Bitmap(collisions.count))
dataMap: _Bitmap(collisions.count),
trieMap: _Bitmap(collisions.count))
self.count = collisions.count
self.dataBaseAddress.initialize(from: collisions, count: collisions.count)
@@ -251,12 +251,12 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
func get(_ key: Key, _ keyHash: Int, _ shift: Int) -> Value? {
let mask = maskFrom(keyHash, shift)
let bitpos = bitposFrom(mask)
let mask = _maskFrom(keyHash, shift)
let bitpos = _bitposFrom(mask)
guard collisionFree else {
let content: [ReturnPayload] = Array(self)
let hash = computeHash(content.first!.key)
let hash = _computeHash(content.first!.key)
guard keyHash == hash else {
return nil
@@ -266,26 +266,26 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
guard (dataMap & bitpos) == 0 else {
let index = indexFrom(dataMap, mask, bitpos)
let index = _indexFrom(dataMap, mask, bitpos)
let payload = self.getPayload(index)
return key == payload.key ? payload.value : nil
}
guard (trieMap & bitpos) == 0 else {
let index = indexFrom(trieMap, mask, bitpos)
return self.getNode(index).get(key, keyHash, shift + bitPartitionSize)
let index = _indexFrom(trieMap, mask, bitpos)
return self.getNode(index).get(key, keyHash, shift + _bitPartitionSize)
}
return nil
}
func containsKey(_ key: Key, _ keyHash: Int, _ shift: Int) -> Bool {
let mask = maskFrom(keyHash, shift)
let bitpos = bitposFrom(mask)
let mask = _maskFrom(keyHash, shift)
let bitpos = _bitposFrom(mask)
guard collisionFree else {
let content: [ReturnPayload] = Array(self)
let hash = computeHash(content.first!.key)
let hash = _computeHash(content.first!.key)
guard keyHash == hash else {
return false
@@ -295,16 +295,16 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
guard (dataMap & bitpos) == 0 else {
let index = indexFrom(dataMap, mask, bitpos)
let index = _indexFrom(dataMap, mask, bitpos)
let payload = self.getPayload(index)
return key == payload.key
}
guard (trieMap & bitpos) == 0 else {
let index = indexFrom(trieMap, mask, bitpos)
let index = _indexFrom(trieMap, mask, bitpos)
return self
.getNode(index)
.containsKey(key, keyHash, shift + bitPartitionSize)
.containsKey(key, keyHash, shift + _bitPartitionSize)
}
return false
@@ -318,7 +318,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
) -> PersistentDictionaryIndex? {
guard collisionFree else {
let content: [ReturnPayload] = Array(self)
let hash = computeHash(content.first!.key)
let hash = _computeHash(content.first!.key)
assert(keyHash == hash)
return content
@@ -326,13 +326,13 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
.map { PersistentDictionaryIndex(value: $0) }
}
let mask = maskFrom(keyHash, shift)
let bitpos = bitposFrom(mask)
let mask = _maskFrom(keyHash, shift)
let bitpos = _bitposFrom(mask)
let skipped = self.counts.prefix(upTo: mask).reduce(0, +)
guard (dataMap & bitpos) == 0 else {
let index = indexFrom(dataMap, mask, bitpos)
let index = _indexFrom(dataMap, mask, bitpos)
let payload = self.getPayload(index)
guard key == payload.key else { return nil }
@@ -340,10 +340,10 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
guard (trieMap & bitpos) == 0 else {
let index = indexFrom(trieMap, mask, bitpos)
let index = _indexFrom(trieMap, mask, bitpos)
return self
.getNode(index)
.index(key, keyHash, shift + bitPartitionSize, skippedBefore + skipped)
.index(key, keyHash, shift + _bitPartitionSize, skippedBefore + skipped)
}
return nil
@@ -373,18 +373,18 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
let skipped = (mask == 0) ? 0 : cumulativeCounts[mask - 1]
let bitpos = bitposFrom(mask)
let bitpos = _bitposFrom(mask)
guard (dataMap & bitpos) == 0 else {
let index = indexFrom(dataMap, mask, bitpos)
let index = _indexFrom(dataMap, mask, bitpos)
return self.getPayload(index)
}
guard (trieMap & bitpos) == 0 else {
let index = indexFrom(trieMap, mask, bitpos)
let index = _indexFrom(trieMap, mask, bitpos)
return self
.getNode(index)
.get(position: position, shift + bitPartitionSize, stillToSkip - skipped)
.get(position: position, shift + _bitPartitionSize, stillToSkip - skipped)
}
fatalError("Should not reach here.")
@@ -404,18 +404,18 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
isStorageKnownUniquelyReferenced, key, value, keyHash, shift, &effect)
}
let mask = maskFrom(keyHash, shift)
let bitpos = bitposFrom(mask)
let mask = _maskFrom(keyHash, shift)
let bitpos = _bitposFrom(mask)
guard (dataMap & bitpos) == 0 else {
let index = indexFrom(dataMap, mask, bitpos)
let index = _indexFrom(dataMap, mask, bitpos)
let (key0, value0) = self.getPayload(index)
if key0 == key {
effect.setReplacedValue(previousValue: value0)
return copyAndSetValue(isStorageKnownUniquelyReferenced, bitpos, value)
} else {
let keyHash0 = computeHash(key0)
let keyHash0 = _computeHash(key0)
if keyHash0 == keyHash {
let subNodeNew = Self(
@@ -432,7 +432,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
let subNodeNew = mergeTwoKeyValPairs(
key0, value0, keyHash0,
key, value, keyHash,
shift + bitPartitionSize)
shift + _bitPartitionSize)
effect.setModified()
return copyAndMigrateFromInlineToNode(
@@ -442,7 +442,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
guard (trieMap & bitpos) == 0 else {
let index = indexFrom(trieMap, mask, bitpos)
let index = _indexFrom(trieMap, mask, bitpos)
let subNodeModifyInPlace = self.isTrieNodeKnownUniquelyReferenced(
index, isStorageKnownUniquelyReferenced)
@@ -451,7 +451,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
let subNodeNew = subNode.updateOrUpdating(
subNodeModifyInPlace,
key, value, keyHash,
shift + bitPartitionSize,
shift + _bitPartitionSize,
&effect)
guard effect.modified, subNode !== subNodeNew else {
if effect.previousValue == nil { count += 1 }
@@ -484,7 +484,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
assert(hashCollision)
let content: [ReturnPayload] = Array(self)
let hash = computeHash(content.first!.key)
let hash = _computeHash(content.first!.key)
guard keyHash == hash else {
effect.setModified()
@@ -520,11 +520,11 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
&effect)
}
let mask = maskFrom(keyHash, shift)
let bitpos = bitposFrom(mask)
let mask = _maskFrom(keyHash, shift)
let bitpos = _bitposFrom(mask)
guard (dataMap & bitpos) == 0 else {
let index = indexFrom(dataMap, mask, bitpos)
let index = _indexFrom(dataMap, mask, bitpos)
let (key0, value0) = self.getPayload(index)
guard key0 == key else {
assert(self.invariant)
@@ -544,7 +544,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
} else {
// create potential new root: will a) become new root, or b) inlined
// on another level
let newDataMap = bitposFrom(maskFrom(keyHash, 0))
let newDataMap = _bitposFrom(_maskFrom(keyHash, 0))
let (remainingKey, remainingValue) = getPayload(1 - index)
return Self(
dataMap: newDataMap,
@@ -564,14 +564,14 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
guard (trieMap & bitpos) == 0 else {
let index = indexFrom(trieMap, mask, bitpos)
let index = _indexFrom(trieMap, mask, bitpos)
let subNodeModifyInPlace = self.isTrieNodeKnownUniquelyReferenced(
index, isStorageKnownUniquelyReferenced)
let subNode = self.getNode(index)
let subNodeNew = subNode.removeOrRemoving(
subNodeModifyInPlace, key, keyHash, shift + bitPartitionSize, &effect)
subNodeModifyInPlace, key, keyHash, shift + _bitPartitionSize, &effect)
guard effect.modified, subNode !== subNodeNew else {
if effect.modified { count -= 1 }
assert(self.invariant)
@@ -620,7 +620,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
assert(hashCollision)
let content: [ReturnPayload] = Array(self)
let _ = computeHash(content.first!.key)
let _ = _computeHash(content.first!.key)
if let index = content.firstIndex(where: { key == $0.key }) {
effect.setModified(previousValue: content[index].value)
@@ -630,7 +630,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
if updatedContent.count == 1 {
// create potential new root: will a) become new root, or b) inlined
// on another level
let newDataMap = bitposFrom(maskFrom(keyHash, 0))
let newDataMap = _bitposFrom(_maskFrom(keyHash, 0))
let (remainingKey, remainingValue) = updatedContent.first!
return Self(
dataMap: newDataMap,
@@ -653,21 +653,21 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
) -> BitmapIndexedDictionaryNode<Key, Value> {
assert(keyHash0 != keyHash1)
let mask0 = maskFrom(keyHash0, shift)
let mask1 = maskFrom(keyHash1, shift)
let mask0 = _maskFrom(keyHash0, shift)
let mask1 = _maskFrom(keyHash1, shift)
if mask0 != mask1 {
// unique prefixes, payload fits on same level
if mask0 < mask1 {
return Self(
dataMap: bitposFrom(mask0) | bitposFrom(mask1),
dataMap: _bitposFrom(mask0) | _bitposFrom(mask1),
firstKey: key0,
firstValue: value0,
secondKey: key1,
secondValue: value1)
} else {
return Self(
dataMap: bitposFrom(mask1) | bitposFrom(mask0),
dataMap: _bitposFrom(mask1) | _bitposFrom(mask0),
firstKey: key1,
firstValue: value1,
secondKey: key0,
@@ -679,9 +679,9 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
let node = mergeTwoKeyValPairs(
key0, value0, keyHash0,
key1, value1, keyHash1,
shift + bitPartitionSize)
shift + _bitPartitionSize)
return Self(trieMap: bitposFrom(mask0), firstNode: node)
return Self(trieMap: _bitposFrom(mask0), firstNode: node)
}
}
@@ -693,14 +693,14 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
) -> BitmapIndexedDictionaryNode<Key, Value> {
assert(keyHash0 != nodeHash1)
let mask0 = maskFrom(keyHash0, shift)
let mask1 = maskFrom(nodeHash1, shift)
let mask0 = _maskFrom(keyHash0, shift)
let mask1 = _maskFrom(nodeHash1, shift)
if mask0 != mask1 {
// unique prefixes, payload and collision node fit on same level
return Self(
dataMap: bitposFrom(mask0),
trieMap: bitposFrom(mask1),
dataMap: _bitposFrom(mask0),
trieMap: _bitposFrom(mask1),
firstKey: key0,
firstValue: value0,
firstNode: node1)
@@ -712,9 +712,9 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
keyHash0,
node1,
nodeHash1,
shift + bitPartitionSize)
shift + _bitPartitionSize)
return Self(trieMap: bitposFrom(mask0), firstNode: node)
return Self(trieMap: _bitposFrom(mask0), firstNode: node)
}
}
@@ -744,7 +744,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
private final var counts: [Int] {
var counts = Array(repeating: 0, count: Bitmap.bitWidth)
var counts = Array(repeating: 0, count: _Bitmap.bitWidth)
zip(header.dataMap._nonzeroBits(), _dataSlice).forEach { (index, _) in
counts[index] = 1
@@ -758,10 +758,10 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
}
private final func count(upTo mask: Int) -> Int {
let bitpos = bitposFrom(mask)
let bitpos = _bitposFrom(mask)
let dataIndex = indexFrom(dataMap, mask, bitpos)
let trieIndex = indexFrom(trieMap, mask, bitpos)
let dataIndex = _indexFrom(dataMap, mask, bitpos)
let trieIndex = _indexFrom(trieMap, mask, bitpos)
let buffer = UnsafeMutableBufferPointer(
start: trieBaseAddress, count: header.trieCount)
@@ -772,17 +772,17 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
return count
}
func dataIndex(_ bitpos: Bitmap) -> Int {
func dataIndex(_ bitpos: _Bitmap) -> Int {
(dataMap & (bitpos &- 1)).nonzeroBitCount
}
func trieIndex(_ bitpos: Bitmap) -> Int {
func trieIndex(_ bitpos: _Bitmap) -> Int {
(trieMap & (bitpos &- 1)).nonzeroBitCount
}
func copyAndSetValue(
_ isStorageKnownUniquelyReferenced: Bool,
_ bitpos: Bitmap,
_ bitpos: _Bitmap,
_ newValue: Value
) -> BitmapIndexedDictionaryNode<Key, Value> {
let src: ReturnBitmapIndexedNode = self
@@ -805,7 +805,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
private func copyAndSetTrieNode(
_ isStorageKnownUniquelyReferenced: Bool,
_ bitpos: Bitmap,
_ bitpos: _Bitmap,
_ idx: Int,
_ newNode: TrieBufferElement,
updateCount: (inout Int) -> Void
@@ -831,7 +831,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
func copyAndInsertValue(
_ isStorageKnownUniquelyReferenced: Bool,
_ bitpos: Bitmap,
_ bitpos: _Bitmap,
_ key: Key,
_ value: Value
) -> BitmapIndexedDictionaryNode<Key, Value> {
@@ -846,8 +846,8 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
dst = src.copy(withDataCapacityFactor: hasRoomForData ? 1 : 2)
}
let dataIdx = indexFrom(dataMap, bitpos)
rangeInsert(
let dataIdx = _indexFrom(dataMap, bitpos)
_rangeInsert(
(key, value),
at: dataIdx,
into: dst.dataBaseAddress,
@@ -864,7 +864,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
func copyAndRemoveValue(
_ isStorageKnownUniquelyReferenced: Bool,
_ bitpos: Bitmap
_ bitpos: _Bitmap
) -> BitmapIndexedDictionaryNode<Key, Value> {
let src: ReturnBitmapIndexedNode = self
let dst: ReturnBitmapIndexedNode
@@ -875,8 +875,8 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
dst = src.copy()
}
let dataIdx = indexFrom(dataMap, bitpos)
rangeRemove(
let dataIdx = _indexFrom(dataMap, bitpos)
_rangeRemove(
at: dataIdx, from: dst.dataBaseAddress, count: dst.header.dataCount)
// update metadata: `dataMap ^ bitpos, nodeMap, collMap`
@@ -890,7 +890,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
func copyAndMigrateFromInlineToNode(
_ isStorageKnownUniquelyReferenced: Bool,
_ bitpos: Bitmap,
_ bitpos: _Bitmap,
_ node: TrieBufferElement
) -> BitmapIndexedDictionaryNode<Key, Value> {
let src: ReturnBitmapIndexedNode = self
@@ -915,12 +915,12 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
withTrieCapacityFactor: hasRoomForTrie ? 1 : 2)
}
let dataIdx = indexFrom(dataMap, bitpos)
rangeRemove(
let dataIdx = _indexFrom(dataMap, bitpos)
_rangeRemove(
at: dataIdx, from: dst.dataBaseAddress, count: dst.header.dataCount)
let trieIdx = indexFrom(trieMap, bitpos)
rangeInsert(
let trieIdx = _indexFrom(trieMap, bitpos)
_rangeInsert(
node, at: trieIdx, into: dst.trieBaseAddress, count: dst.header.trieCount)
// update metadata: `dataMap ^ bitpos, nodeMap | bitpos, collMap`
@@ -935,7 +935,7 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
func copyAndMigrateFromNodeToInline(
_ isStorageKnownUniquelyReferenced: Bool,
_ bitpos: Bitmap,
_ bitpos: _Bitmap,
_ tuple: (key: Key, value: Value)
) -> BitmapIndexedDictionaryNode<Key, Value> {
let src: ReturnBitmapIndexedNode = self
@@ -949,12 +949,12 @@ final class BitmapIndexedDictionaryNode<Key: Hashable, Value>: _DictionaryNode {
dst = src.copy(withDataCapacityFactor: hasRoomForData ? 1 : 2)
}
let nodeIdx = indexFrom(trieMap, bitpos)
rangeRemove(
let nodeIdx = _indexFrom(trieMap, bitpos)
_rangeRemove(
at: nodeIdx, from: dst.trieBaseAddress, count: dst.header.trieCount)
let dataIdx = indexFrom(dataMap, bitpos)
rangeInsert(
let dataIdx = _indexFrom(dataMap, bitpos)
_rangeInsert(
tuple, at: dataIdx, into: dst.dataBaseAddress, count: dst.header.dataCount)
// update metadata: `dataMap | bitpos, nodeMap ^ bitpos, collMap`
@@ -1018,8 +1018,8 @@ extension BitmapIndexedDictionaryNode: Sequence {
}
struct Header: Equatable {
var dataMap: Bitmap
var trieMap: Bitmap
var dataMap: _Bitmap
var trieMap: _Bitmap
var dataCount: Int { hashCollision ? Int(dataMap) : dataMap.nonzeroBitCount }
var trieCount: Int { hashCollision ? 0 : trieMap.nonzeroBitCount }
+17 -17
View File
@@ -9,44 +9,44 @@
//
//===----------------------------------------------------------------------===//
func computeHash<T: Hashable>(_ value: T) -> Int {
internal func _computeHash<T: Hashable>(_ value: T) -> Int {
value.hashValue
}
typealias Bitmap = UInt32
let bitPartitionSize: Int = 5
let bitPartitionMask: Int = (1 << bitPartitionSize) - 1
internal typealias _Bitmap = UInt32
// TODO: restore type to `UInt8` after reworking hash-collisions to grow in
// depth instead of width
typealias Capacity = UInt32
internal typealias _Capacity = UInt32
let hashCodeLength: Int = Int.bitWidth
internal let _bitPartitionSize: Int = 5
let maxDepth = Int((Double(hashCodeLength) / Double(bitPartitionSize)).rounded(.up))
internal let _bitPartitionMask: Int = (1 << _bitPartitionSize) - 1
func maskFrom(_ hash: Int, _ shift: Int) -> Int {
(hash >> shift) & bitPartitionMask
internal let _hashCodeLength: Int = Int.bitWidth
internal let _maxDepth = (_hashCodeLength + _bitPartitionSize - 1) / _bitPartitionSize
internal func _maskFrom(_ hash: Int, _ shift: Int) -> Int {
(hash >> shift) & _bitPartitionMask
}
func bitposFrom(_ mask: Int) -> Bitmap {
internal func _bitposFrom(_ mask: Int) -> _Bitmap {
1 << mask
}
func indexFrom(_ bitmap: Bitmap, _ bitpos: Bitmap) -> Int {
internal func _indexFrom(_ bitmap: _Bitmap, _ bitpos: _Bitmap) -> Int {
(bitmap & (bitpos &- 1)).nonzeroBitCount
}
func indexFrom(_ bitmap: Bitmap, _ mask: Int, _ bitpos: Bitmap) -> Int {
(bitmap == Bitmap.max) ? mask : indexFrom(bitmap, bitpos)
internal func _indexFrom(_ bitmap: _Bitmap, _ mask: Int, _ bitpos: _Bitmap) -> Int {
(bitmap == _Bitmap.max) ? mask : _indexFrom(bitmap, bitpos)
}
// NEW
@inlinable
@inline(__always)
func rangeInsert<T>(
internal func _rangeInsert<T>(
_ element: T,
at index: Int,
into baseAddress: UnsafeMutablePointer<T>,
@@ -63,7 +63,7 @@ func rangeInsert<T>(
// NEW
@inlinable
@inline(__always)
func rangeRemove<T>(
internal func _rangeRemove<T>(
at index: Int,
from baseAddress: UnsafeMutablePointer<T>,
count: Int
@@ -19,12 +19,12 @@ extension FixedWidthInteger {
}
}
internal struct _NonzeroBits<Bitmap: FixedWidthInteger>
internal struct _NonzeroBits<_Bitmap: FixedWidthInteger>
: Sequence, IteratorProtocol, CustomStringConvertible
{
var bitmap: Bitmap
var bitmap: _Bitmap
init(from bitmap: Bitmap) {
init(from bitmap: _Bitmap) {
self.bitmap = bitmap
}
@@ -15,34 +15,34 @@ import XCTest
final class BitmapSmokeTests: CollectionTestCase {
func test_BitPartitionSize_isValid() {
expectTrue(bitPartitionSize > 0)
expectTrue((2 << (bitPartitionSize - 1)) != 0)
expectTrue((2 << (bitPartitionSize - 1)) <= Bitmap.bitWidth)
expectTrue(_bitPartitionSize > 0)
expectTrue((2 << (_bitPartitionSize - 1)) != 0)
expectTrue((2 << (_bitPartitionSize - 1)) <= _Bitmap.bitWidth)
}
func test_Capacity_isValid() {
expectTrue(Int(2 << bitPartitionSize) <= Int(Capacity.max))
expectTrue(Int(2 << _bitPartitionSize) <= Int(_Capacity.max))
}
func test_Bitmap_nonzeroBits() {
let bitmap: Bitmap = 0b0100_0000_0000_1011
let bitmap: _Bitmap = 0b0100_0000_0000_1011
expectEqual(Array(bitmap._nonzeroBits()), [0, 1, 3, 14])
expectEqual(
Array(bitmap._zeroBits()),
(0 ..< Bitmap.bitWidth).filter { ![0, 1, 3, 14].contains($0) })
(0 ..< _Bitmap.bitWidth).filter { ![0, 1, 3, 14].contains($0) })
}
func test_Bitmap_nonzeroBitsToArray() {
let bitmap: Bitmap = 0b0100_0000_0000_1011
let bitmap: _Bitmap = 0b0100_0000_0000_1011
let counts = bitmap._nonzeroBits().reduce(
into: Array(repeating: 0, count: Bitmap.bitWidth)
into: Array(repeating: 0, count: _Bitmap.bitWidth)
) { counts, index in
counts[index] = 1
}
expectEqual(counts.count, Bitmap.bitWidth)
expectEqual(counts.count, _Bitmap.bitWidth)
expectEqual(counts.reduce(0, +), bitmap.nonzeroBitCount)
expectEqual(counts.reduce(0, +), 4)
expectEqual(counts[0], 1)
@@ -52,7 +52,7 @@ final class BitmapSmokeTests: CollectionTestCase {
}
func test_Bitmap_enumerateCompactedArray() {
let bitmap: Bitmap = 0b0100_0000_0000_1011
let bitmap: _Bitmap = 0b0100_0000_0000_1011
let elements: [String] = ["zero", "one", "three", "fourteen"]
var zipIterator = zip(bitmap._nonzeroBits(), elements).makeIterator()