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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
extension TreeDictionary {
/// A view of a persistent dictionary’s keys, as a standalone collection.
@frozen
public struct Keys {
@usableFromInline
internal typealias _Node = _HashNode<Key, Value>
@usableFromInline
internal var _base: TreeDictionary
@inlinable
internal init(_base: TreeDictionary) {
self._base = _base
}
}
/// A collection containing just the keys of the dictionary.
///
/// - Complexity: O(1)
@inlinable
public var keys: Keys {
Keys(_base: self)
}
}
extension TreeDictionary.Keys: Sendable
where Key: Sendable, Value: Sendable {}
extension TreeDictionary.Keys: _UniqueCollection {}
extension TreeDictionary.Keys: CustomStringConvertible {
// A textual representation of this instance.
public var description: String {
_arrayDescription(for: self)
}
}
extension TreeDictionary.Keys: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
description
}
}
extension TreeDictionary.Keys: Sequence {
/// The element type of the collection.
public typealias Element = Key
/// The type that allows iteration over the elements of the keys view
/// of a persistent dictionary.
@frozen
public struct Iterator: IteratorProtocol {
public typealias Element = Key
@usableFromInline
internal var _base: TreeDictionary.Iterator
@inlinable
internal init(_base: TreeDictionary.Iterator) {
self._base = _base
}
@inlinable
public mutating func next() -> Element? {
_base.next()?.key
}
}
@inlinable
public func makeIterator() -> Iterator {
Iterator(_base: _base.makeIterator())
}
@inlinable
public func _customContainsEquatableElement(
_ element: Element
) -> Bool? {
_base._root.containsKey(.top, element, _Hash(element))
}
/// Returns a Boolean value that indicates whether the given key exists
/// in the dictionary.
///
/// - Parameter element: A key to look for in the dictionary/
///
/// - Returns: `true` if `element` exists in the set; otherwise, `false`.
///
/// - Complexity: This operation is expected to perform O(1) hashing and
/// comparison operations on average, provided that `Element` implements
/// high-quality hashing.
@inlinable
public func contains(_ element: Element) -> Bool {
_base._root.containsKey(.top, element, _Hash(element))
}
}
extension TreeDictionary.Keys.Iterator: Sendable
where Key: Sendable, Value: Sendable {}
extension TreeDictionary.Keys: Collection {
public typealias Index = TreeDictionary.Index
@inlinable
public var isEmpty: Bool { _base.isEmpty }
@inlinable
public var count: Int { _base.count }
@inlinable
public var startIndex: Index { _base.startIndex }
@inlinable
public var endIndex: Index { _base.endIndex }
@inlinable
public subscript(index: Index) -> Element {
_base[index].key
}
@inlinable
public func formIndex(after i: inout Index) {
_base.formIndex(after: &i)
}
@inlinable
public func index(after i: Index) -> Index {
_base.index(after: i)
}
@inlinable
public func index(_ i: Index, offsetBy distance: Int) -> Index {
_base.index(i, offsetBy: distance)
}
@inlinable
public func index(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
) -> Index? {
_base.index(i, offsetBy: distance, limitedBy: limit)
}
@inlinable
public func distance(from start: Index, to end: Index) -> Int {
_base.distance(from: start, to: end)
}
}
#if false
extension TreeDictionary.Keys: BidirectionalCollection {
// Note: Let's not do this. `BidirectionalCollection` would imply that
// the ordering of elements would be meaningful, which isn't true for
// `TreeDictionary.Keys`.
@inlinable
public func formIndex(before i: inout Index) {
_base.formIndex(before: &i)
}
@inlinable
public func index(before i: Index) -> Index {
_base.index(before: i)
}
}
#endif
extension TreeDictionary.Keys {
/// Returns a new keys view with the elements that are common to both this
/// view and the provided other one.
///
/// var a: TreeDictionary = ["a": 1, "b": 2, "c": 3]
/// let b: TreeDictionary = ["b": 4, "d": 5, "e": 6]
/// let c = a.keys.intersection(b.keys)
/// // `c` is `["b"]`
///
/// The result will only contain instances that were originally in `self`.
/// (This matters if equal members can be distinguished by comparing their
/// identities, or by some other means.)
///
/// - Parameter other: The keys view of a persistent dictionary with the same
/// `Key` type.
///
/// - Complexity: Expected complexity is O(`self.count`) in
/// the worst case, if `Element` properly implements hashing.
/// However, the implementation is careful to make the best use of
/// hash tree structure to minimize work when possible, e.g. by linking
/// parts of the input trees directly into the result.
public func intersection<Value2>(
_ other: TreeDictionary<Key, Value2>.Keys
) -> Self {
guard let r = _base._root.intersection(.top, other._base._root) else {
return self
}
let d = TreeDictionary(_new: r)
d._invariantCheck()
return d.keys
}
/// Returns a new keys view with the elements that are common to both this
/// view and the provided persistent set.
///
/// var a: TreeDictionary = ["a": 1, "b": 2, "c": 3]
/// let b: TreeSet = ["b", "d", "e"]
/// let c = a.keys.intersection(b)
/// // `c` is `["b"]`
///
/// The result will only contain instances that were originally in `self`.
/// (This matters if equal members can be distinguished by comparing their
/// identities, or by some other means.)
///
/// - Parameter other: A persistent set whose `Element` type is `Key`.
///
/// - Complexity: Expected complexity is O(`self.count`) in
/// the worst case, if `Element` properly implements hashing.
/// However, the implementation is careful to make the best use of
/// hash tree structure to minimize work when possible, e.g. by linking
/// parts of the input trees directly into the result.
public func intersection(_ other: TreeSet<Key>) -> Self {
guard let r = _base._root.intersection(.top, other._root) else {
return self
}
let d = TreeDictionary(_new: r)
d._invariantCheck()
return d.keys
}
/// Returns a new keys view containing the elements of `self` that do not
/// occur in the provided other one.
///
/// var a: TreeDictionary = ["a": 1, "b": 2, "c": 3]
/// let b: TreeDictionary = ["b": 4, "d": 5, "e": 6]
/// let c = a.keys.subtracting(b.keys)
/// // `c` is some permutation of `["a", "c"]`
///
/// - Parameter other: The keys view of a persistent dictionary with the same
/// `Key` type.
///
/// - Complexity: Expected complexity is O(`self.count`) in
/// the worst case, if `Element` properly implements hashing.
/// However, the implementation is careful to make the best use of
/// hash tree structure to minimize work when possible, e.g. by linking
/// parts of the input trees directly into the result.
public func subtracting<Value2>(
_ other: TreeDictionary<Key, Value2>.Keys
) -> Self {
guard let r = _base._root.subtracting(.top, other._base._root) else {
return self
}
let d = TreeDictionary(_new: r)
d._invariantCheck()
return d.keys
}
/// Returns a new keys view containing the elements of `self` that do not
/// occur in the provided persistent set.
///
/// var a: TreeDictionary = ["a": 1, "b": 2, "c": 3]
/// let b: TreeSet = ["b", "d", "e"]
/// let c = a.keys.subtracting(b)
/// // `c` is some permutation of `["a", "c"]`
///
/// - Parameter other: A persistent set whose `Element` type is `Key`.
///
/// - Complexity: Expected complexity is O(`self.count`) in
/// the worst case, if `Element` properly implements hashing.
/// However, the implementation is careful to make the best use of
/// hash tree structure to minimize work when possible, e.g. by linking
/// parts of the input trees directly into the result.
public func subtracting(_ other: TreeSet<Key>) -> Self {
guard let r = _base._root.subtracting(.top, other._root) else {
return self
}
let d = TreeDictionary(_new: r)
d._invariantCheck()
return d.keys
}
}
extension TreeDictionary.Keys: Equatable {
/// Returns a Boolean value indicating whether two values are equal.
///
/// Equality is the inverse of inequality. For any values `a` and `b`,
/// `a == b` implies that `a != b` is `false`.
///
/// - Parameter lhs: A value to compare.
/// - Parameter rhs: Another value to compare.
///
/// - Complexity: Generally O(`count`), as long as`Element` properly
/// implements hashing. That said, the implementation is careful to take
/// every available shortcut to reduce complexity, e.g. by skipping
/// comparing elements in shared subtrees.
@inlinable
public static func == (left: Self, right: Self) -> Bool {
left._base._root.isEqualSet(to: right._base._root, by: { _, _ in true })
}
}
extension TreeDictionary.Keys: Hashable {
/// Hashes the essential components of this value by feeding them into the
/// given hasher.
///
/// Complexity: O(`count`)
@inlinable
public func hash(into hasher: inout Hasher) {
let copy = hasher
let seed = copy.finalize()
var hash = 0
for member in self {
hash ^= member._rawHashValue(seed: seed)
}
hasher.combine(hash)
}
}
|