Files
swift-collections/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Values.swift
T
Karoy Lorentey 424c8ece98 HashTreeCollections: Qualify internal type names with “Hash”
This makes them unique throughout the package, allowing this code to be built as a single, huge module.
2023-03-21 16:37:22 -07:00

170 lines
4.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
import _CollectionsUtilities
extension TreeDictionary {
/// A view of a dictionary’s values.
@frozen
public struct Values {
@usableFromInline
internal typealias _HashNode = HashTreeCollections._HashNode<Key, Value>
@usableFromInline
internal typealias _UnsafeHandle = _HashNode.UnsafeHandle
@usableFromInline
internal var _base: TreeDictionary
@inlinable
internal init(_base: TreeDictionary) {
self._base = _base
}
}
/// A collection containing just the values of the dictionary.
@inlinable
public var values: Values {
// Note: this property is kept read only for now until we decide whether
// it's worth providing setters without a `MutableCollection` conformance.
get {
Values(_base: self)
}
}
}
#if swift(>=5.5)
extension TreeDictionary.Values: Sendable
where Key: Sendable, Value: Sendable {}
#endif
extension TreeDictionary.Values: CustomStringConvertible {
// A textual representation of this instance.
public var description: String {
_arrayDescription(for: self)
}
}
extension TreeDictionary.Values: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
_arrayDescription(
for: self,
debug: true,
typeName: "\(TreeDictionary._debugTypeName()).Values")
}
}
extension TreeDictionary.Values: Sequence {
public typealias Element = Value
@frozen
public struct Iterator: IteratorProtocol {
public typealias Element = Value
@usableFromInline
internal var _base: TreeDictionary.Iterator
@inlinable
internal init(_base: TreeDictionary.Iterator) {
self._base = _base
}
@inlinable
public mutating func next() -> Element? {
_base.next()?.value
}
}
@inlinable
public func makeIterator() -> Iterator {
Iterator(_base: _base.makeIterator())
}
}
#if swift(>=5.5)
extension TreeDictionary.Values.Iterator: Sendable
where Key: Sendable, Value: Sendable {}
#endif
// Note: This cannot be a MutableCollection because its subscript setter
// needs to invalidate indices.
extension TreeDictionary.Values: 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 {
// The subscript is kept read only for now until we decide whether it's
// worth providing setters without a `MutableCollection` conformance.
// (With the current index implementation, mutating values must invalidate
// indices.)
get {
_base[index].value
}
}
@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.Values: BidirectionalCollection {
// Note: Let's not do this. `BidirectionalCollection` would imply that
// the ordering of elements would be meaningful, which isn't true for
// `TreeDictionary.Values`.
@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