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

124 lines
3.5 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 init() {
self.init(_new: ._emptyNode())
}
@inlinable
public init(_ other: PersistentDictionary<Key, Value>) {
self = other
}
// FIXME: This is a non-standard addition
@inlinable
public init(
keys: PersistentSet<Key>,
_ valueTransform: (Key) throws -> Value
) rethrows {
let root = try keys._root.mapValues { try valueTransform($0.key) }
self.init(_new: root)
}
@inlinable
public init<S: Sequence>(
uniqueKeysWithValues keysAndValues: S
) where S.Element == (Key, Value) {
self.init()
for item in keysAndValues {
let hash = _Hash(item.0)
let r = _root.insert(.top, item, hash)
precondition(r.inserted, "Duplicate key: '\(item.0)'")
}
_invariantCheck()
}
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
@inlinable
public init<S: Sequence>(
uniqueKeysWithValues keysAndValues: S
) where S.Element == Element {
if S.self == Self.self {
self = keysAndValues as! Self
return
}
self.init(
uniqueKeysWithValues: keysAndValues.lazy.map { ($0.key, $0.value ) })
}
@inlinable
public init<Keys: Sequence, Values: Sequence>(
uniqueKeys keys: Keys,
values: Values
) where Keys.Element == Key, Values.Element == Value {
self.init(uniqueKeysWithValues: zip(keys, values))
}
public init<S: Sequence>(
_ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == (Key, Value) {
self.init()
try self.merge(keysAndValues, uniquingKeysWith: combine)
}
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
public init<S: Sequence>(
_ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == Element {
try self.init(
keysAndValues.lazy.map { ($0.key, $0.value) },
uniquingKeysWith: combine)
}
}
extension PersistentDictionary {
@inlinable @inline(__always)
public init<S: Sequence>(
grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows
where Value: RangeReplaceableCollection, Value.Element == S.Element
{
try self.init(_grouping: values, by: keyForValue)
}
@inlinable @inline(__always)
public init<S: Sequence>(
grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows
where Value == [S.Element]
{
// Note: this extra overload is necessary to make type inference work
// for the `Value` type -- we want it to default to `[S.Element`].
// (https://github.com/apple/swift-collections/issues/139)
try self.init(_grouping: values, by: keyForValue)
}
@inlinable
internal init<S: Sequence>(
_grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows
where Value: RangeReplaceableCollection, Value.Element == S.Element
{
self.init()
for value in values {
let key = try keyForValue(value)
self.updateValue(forKey: key, default: Value()) { array in
array.append(value)
}
}
}
}