mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +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.
75 lines
2.1 KiB
Swift
75 lines
2.1 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import _CollectionsUtilities
|
|
|
|
extension _Node {
|
|
@inlinable
|
|
internal func mapValues<T>(
|
|
_ transform: (Value) throws -> T
|
|
) rethrows -> _Node<Key, T> {
|
|
let c = self.count
|
|
return try read { source in
|
|
var result: _Node<Key, T>
|
|
if isCollisionNode {
|
|
result = _Node<Key, T>.allocateCollision(
|
|
count: c, source.collisionHash,
|
|
initializingWith: { _ in }
|
|
).node
|
|
} else {
|
|
result = _Node<Key, T>.allocate(
|
|
itemMap: source.itemMap,
|
|
childMap: source.childMap,
|
|
count: c,
|
|
initializingWith: { _, _ in }
|
|
).node
|
|
}
|
|
try result.update { target in
|
|
let sourceItems = source.reverseItems
|
|
let targetItems = target.reverseItems
|
|
assert(sourceItems.count == targetItems.count)
|
|
|
|
let sourceChildren = source.children
|
|
let targetChildren = target.children
|
|
assert(sourceChildren.count == targetChildren.count)
|
|
|
|
var i = 0
|
|
var j = 0
|
|
|
|
var success = false
|
|
|
|
defer {
|
|
if !success {
|
|
targetItems.prefix(i).deinitialize()
|
|
targetChildren.prefix(j).deinitialize()
|
|
target.clear()
|
|
}
|
|
}
|
|
|
|
while i < targetItems.count {
|
|
let key = sourceItems[i].key
|
|
let value = try transform(sourceItems[i].value)
|
|
targetItems.initializeElement(at: i, to: (key, value))
|
|
i += 1
|
|
}
|
|
while j < targetChildren.count {
|
|
let child = try sourceChildren[j].mapValues(transform)
|
|
targetChildren.initializeElement(at: j, to: child)
|
|
j += 1
|
|
}
|
|
success = true
|
|
}
|
|
result._invariantCheck()
|
|
return result
|
|
}
|
|
}
|
|
}
|