Files
swift-collections/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Values.swift
T
Karoy Lorentey 09c09f0f73 Fix CustomStringConvertible/CustomDebugStringConvertible conformances
- For CustomStringConvertible, `description` should print elements using `String(reflecting:)` rather than `String(describing:)`, to avoid element output from interfering with syntactically relevant punctuation within structured list displays.
- To make this work without making displays overly verbose, make `debugDescription` mostly the same as `description`, except for escaping raw string displays when and if needed.
- Avoid printing type names in `debugDescription` — it is generally not helpful, as the type is already easily accessible elsewhere. Including it makes for overly verbose displays when the type serves as an Element of a collection.
- Avoid having `debugDescription` return the empty string — it can be confusing when such a description appears in list displays.

Resolves #301.
2023-07-06 17:25:11 -07:00

165 lines
4.1 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 - 2023 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 _CollectionsUtilities
#endif
extension TreeDictionary {
/// A view of a dictionary’s values.
@frozen
public struct Values {
@usableFromInline
internal typealias _Node = TreeDictionary._Node
@usableFromInline
internal typealias _UnsafeHandle = _Node.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)
}
}
}
extension TreeDictionary.Values: Sendable
where Key: Sendable, Value: Sendable {}
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 {
description
}
}
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())
}
}
extension TreeDictionary.Values.Iterator: Sendable
where Key: Sendable, Value: Sendable {}
// 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