1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 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
//
//===----------------------------------------------------------------------===//
/// A test protocol for validating that dictionary-like types implement users'
/// baseline expectations.
///
/// To ensure maximum utility, this protocol doesn't refine `Collection`,
/// although it does share some of the same requirements.
public protocol DictionaryAPIChecker {
associatedtype Key
associatedtype Value
associatedtype Index
typealias Element = (key: Key, value: Value)
associatedtype Keys: Collection
where Keys.Element == Key, Keys.Index == Index
var keys: Keys { get }
// `Values` ought to be a `MutableCollection` when possible, with `values`
// providing a setter. Unfortunately, tree-based dictionaries need to
// invalidate indices when mutating keys.
associatedtype Values: Collection
where Values.Element == Value, Values.Index == Index
var values: Values { get }
var isEmpty: Bool { get }
var count: Int { get }
func index(forKey key: Key) -> Index?
subscript(key: Key) -> Value? { get set }
subscript(
key: Key,
default defaultValue: @autoclosure () -> Value
) -> Value { get set }
mutating func updateValue(_ value: Value, forKey key: Key) -> Value?
mutating func removeValue(forKey key: Key) -> Value?
mutating func remove(at index: Index) -> Element
init()
init<S: Sequence>(
_ keysAndValues: S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == (Key, Value)
mutating func merge<S: Sequence>(
_ keysAndValues: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == (Key, Value)
__consuming func merging<S: Sequence>(
_ other: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> Self where S.Element == (Key, Value)
func filter(
_ isIncluded: (Element) throws -> Bool
) rethrows -> Self
#if false
// We can't express these as protocol requirements:
func mapValues<T>(
_ transform: (Value) throws -> T
) rethrows -> Self<Key, T>
func compactMapValues<T>(
_ transform: (Value) throws -> T?
) rethrows -> Self<Key, T>
init<S: Sequence>(
grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows where Value: RangeReplaceableCollection, Value.Element == S.Element
public init<S: Sequence>(
grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows where Value == [S.Element]
#endif
}
extension Dictionary: DictionaryAPIChecker {}
/// Additional entry points provided by this package that aren't provided
/// by `Dictionary` (yet?).
public protocol DictionaryAPIExtras: DictionaryAPIChecker {
// Extras (not in the Standard Library)
init<S: Sequence>(
uniqueKeysWithValues keysAndValues: S
) where S.Element == (Key, Value)
init<S: Sequence>(
uniqueKeysWithValues keysAndValues: S
) where S.Element == Element
// init<Keys: Sequence, Values: Sequence>(
// uniqueKeys keys: Keys,
// values: Values
// ) where Keys.Element == Key, Values.Element == Value
init<S: Sequence>(
_ keysAndValues: S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == Element
mutating func merge<S: Sequence>(
_ keysAndValues: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == Element
__consuming func merging<S: Sequence>(
_ other: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> Self where S.Element == Element
mutating func updateValue<R>(
forKey key: Key,
default defaultValue: @autoclosure () -> Value,
with body: (inout Value) throws -> R
) rethrows -> R
#if false
// Potential additions implemented by TreeDictionary:
func contains(_ key: Key) -> Bool
mutating func updateValue<R>(
forKey key: Key,
with body: (inout Value?) throws -> R
) rethrows -> R
#endif
}
|