mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
114 lines
3.3 KiB
Swift
114 lines
3.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 init() {
|
|
self.init(_new: _Node(storage: _emptySingleton, count: 0))
|
|
}
|
|
|
|
@inlinable
|
|
public init(_ other: PersistentDictionary<Key, Value>) {
|
|
self = other
|
|
}
|
|
|
|
@inlinable
|
|
public init<S: Sequence>(
|
|
uniqueKeysWithValues keysAndValues: S
|
|
) where S.Element == (Key, Value) {
|
|
self.init()
|
|
for (key, value) in keysAndValues {
|
|
let hash = _Hash(key)
|
|
let unique = nil == _root.updateValue(value, forKey: key, .top, hash)
|
|
precondition(unique, "Duplicate key: '\(key)'")
|
|
}
|
|
_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)
|
|
}
|
|
}
|
|
}
|
|
}
|