mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
Merging is best done in-place, as merging a non-empty dictionary will always mutate the dictionary, so we don’t have to tiptoe around uniqueness issues. (The union operation on PersistentSet is vaguely similar, but not quite — unlike with merge, union is able directly link shared subtrees into the result. However, a similar optimization may be worthwhile for symmetricDifference.)
82 lines
2.4 KiB
Swift
82 lines
2.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 PersistentDictionary {
|
|
@inlinable
|
|
public mutating func merge(
|
|
_ keysAndValues: Self,
|
|
uniquingKeysWith combine: (Value, Value) throws -> Value
|
|
) rethrows {
|
|
_invalidateIndices()
|
|
try _root.merge(.top, .emptyPrefix, 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
|
|
}
|
|
}
|