Files
swift-collections/Sources/PersistentCollections/Node/_Node+Structural compactMapValues.swift
T
Karoy Lorentey 2e276ba42b [PersistentCollections] Clean up _Node.Builder & removals
This sanitizes the semantics of _Node.Builder, removing
the (unneccessary) need to pass around hash paths.

It also slightly refactors removal operations, but stops short of
implementing node shrinking.

# Conflicts:
#	Sources/PersistentCollections/Node/_Node+Builder.swift
#	Sources/PersistentCollections/Node/_Node+Initializers.swift
#	Sources/PersistentCollections/Node/_Node+Structural intersection.swift
#	Sources/PersistentCollections/Node/_Node+Structural subtracting.swift
#	Sources/PersistentCollections/Node/_Node+Subtree Modify.swift
#	Sources/PersistentCollections/Node/_Node+Subtree Removals.swift
#	Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Initializers.swift
#	Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra Initializers.swift
#	Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra intersection.swift
#	Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra subtracting.swift

(cherry picked from commit c73a8caad1)
2022-10-19 17:58:46 -07:00

47 lines
1.4 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
//
//===----------------------------------------------------------------------===//
extension _Node {
@inlinable
internal func compactMapValues<T>(
_ level: _Level,
_ transform: (Value) throws -> T?
) rethrows -> _Node<Key, T>.Builder {
return try self.read {
var result: _Node<Key, T>.Builder = .empty(level)
if isCollisionNode {
let items = $0.reverseItems
for i in items.indices {
if let v = try transform(items[i].value) {
result.addNewCollision(level, (items[i].key, v), $0.collisionHash)
}
}
return result
}
for (bucket, slot) in $0.itemMap {
let p = $0.itemPtr(at: slot)
if let v = try transform(p.pointee.value) {
result.addNewItem(level, (p.pointee.key, v), at: bucket)
}
}
for (bucket, slot) in $0.childMap {
let branch = try $0[child: slot]
.compactMapValues(level.descend(), transform)
result.addNewChildBranch(level, branch, at: bucket)
}
return result
}
}
}