Files
swift-collections/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Merge.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

82 lines
2.3 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 PersistentDictionary {
@inlinable
public mutating func merge(
_ keysAndValues: Self,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows {
_invalidateIndices()
try _root.merge(.top, keysAndValues._root, combine)
}
@inlinable
public mutating func merge<S: Sequence>(
_ keysAndValues: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == (Key, Value) {
for (key, value) in keysAndValues {
try self.updateValue(forKey: key) { target in
if let old = target {
target = try combine(old, value)
} else {
target = value
}
}
}
}
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
@inlinable
public mutating func merge<S: Sequence>(
_ keysAndValues: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == Element {
try merge(
keysAndValues.lazy.map { ($0.key, $0.value) },
uniquingKeysWith: combine)
}
@inlinable
public func merging(
_ other: Self,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> Self {
var copy = self
try copy.merge(other, uniquingKeysWith: combine)
return copy
}
@inlinable
public func merging<S: Sequence>(
_ other: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> Self
where S.Element == (Key, Value) {
var copy = self
try copy.merge(other, uniquingKeysWith: combine)
return copy
}
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
@inlinable
public func merging<S: Sequence>(
_ other: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> Self
where S.Element == Element {
var copy = self
try copy.merge(other, uniquingKeysWith: combine)
return copy
}
}