mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
[Capsule] Use non-materializing data/trie slice views in iterator
This commit is contained in:
1 parent
8bc5e20b29
commit
45b9d8db26
2 files changed
+8
-20
No files matched your search
@@ -202,18 +202,8 @@ public struct DictionaryKeyValueTupleIterator<Key: Hashable, Value>: IteratorPro
|
||||
trieIteratorStackRemainder = []
|
||||
trieIteratorStackRemainder.reserveCapacity(maxDepth)
|
||||
|
||||
if rootNode.hasNodes { trieIteratorStackTop = makeTrieIterator(rootNode) }
|
||||
if rootNode.hasPayload { payloadIterator = makePayloadIterator(rootNode) }
|
||||
}
|
||||
|
||||
// TODO consider moving to `BitmapIndexedDictionaryNode<Key, Value>`
|
||||
private func makePayloadIterator(_ node: BitmapIndexedDictionaryNode<Key, Value>) -> UnsafeBufferPointer<(key: Key, value: Value)>.Iterator {
|
||||
UnsafeBufferPointer(start: node.dataBaseAddress, count: node.payloadArity).makeIterator()
|
||||
}
|
||||
|
||||
// TODO consider moving to `BitmapIndexedDictionaryNode<Key, Value>`
|
||||
private func makeTrieIterator(_ node: BitmapIndexedDictionaryNode<Key, Value>) -> UnsafeBufferPointer<BitmapIndexedDictionaryNode<Key, Value>>.Iterator {
|
||||
UnsafeBufferPointer(start: node.trieBaseAddress, count: node.nodeArity).makeIterator()
|
||||
if rootNode.hasNodes { trieIteratorStackTop = rootNode._trieSlice.makeIterator() }
|
||||
if rootNode.hasPayload { payloadIterator = rootNode._dataSlice.makeIterator() }
|
||||
}
|
||||
|
||||
public mutating func next() -> (key: Key, value: Value)? {
|
||||
@@ -225,10 +215,10 @@ public struct DictionaryKeyValueTupleIterator<Key: Hashable, Value>: IteratorPro
|
||||
if let nextNode = trieIteratorStackTop!.next() {
|
||||
if nextNode.hasNodes {
|
||||
trieIteratorStackRemainder.append(trieIteratorStackTop!)
|
||||
trieIteratorStackTop = makeTrieIterator(nextNode)
|
||||
trieIteratorStackTop = nextNode._trieSlice.makeIterator()
|
||||
}
|
||||
if nextNode.hasPayload {
|
||||
payloadIterator = makePayloadIterator(nextNode)
|
||||
payloadIterator = nextNode._dataSlice.makeIterator()
|
||||
return payloadIterator?.next()
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -120,14 +120,12 @@ final class BitmapIndexedDictionaryNode<Key, Value>: DictionaryNode where Key: H
|
||||
(header.dataMap & header.trieMap) == 0 || (header.dataMap == header.trieMap)
|
||||
}
|
||||
|
||||
// TODO: should not materialize as `Array` for performance reasons
|
||||
var _dataSlice: [DataBufferElement] {
|
||||
UnsafeMutableBufferPointer(start: dataBaseAddress, count: header.dataCount).map { $0 }
|
||||
var _dataSlice: UnsafeBufferPointer<DataBufferElement> {
|
||||
UnsafeBufferPointer(start: dataBaseAddress, count: header.dataCount)
|
||||
}
|
||||
|
||||
// TODO: should not materialize as `Array` for performance reasons
|
||||
var _trieSlice: [TrieBufferElement] {
|
||||
UnsafeMutableBufferPointer(start: trieBaseAddress, count: header.trieCount).map { $0 }
|
||||
var _trieSlice: UnsafeMutableBufferPointer<TrieBufferElement> {
|
||||
UnsafeMutableBufferPointer(start: trieBaseAddress, count: header.trieCount)
|
||||
}
|
||||
|
||||
init(dataCapacity: Capacity, trieCapacity: Capacity) {
|
||||
|
||||
Reference in new issue
Block a user