mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
- 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.
99 lines
2.2 KiB
Swift
99 lines
2.2 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// A type with manually controlled hash values, for easy testing of collision
|
|
/// scenarios.
|
|
struct Collider:
|
|
Hashable, CustomStringConvertible, CustomDebugStringConvertible
|
|
{
|
|
var identity: Int
|
|
var hash: Hash
|
|
|
|
init(_ identity: Int, _ hash: Hash) {
|
|
self.identity = identity
|
|
self.hash = hash
|
|
}
|
|
|
|
init(_ identity: Int) {
|
|
self.identity = identity
|
|
self.hash = Hash(identity)
|
|
}
|
|
|
|
init(_ identity: String) {
|
|
self.hash = Hash(identity)!
|
|
self.identity = hash.value
|
|
}
|
|
|
|
static func ==(left: Self, right: Self) -> Bool {
|
|
guard left.identity == right.identity else { return false }
|
|
precondition(left.hash == right.hash)
|
|
return true
|
|
}
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
hasher.combine(hash.value)
|
|
}
|
|
|
|
var description: String {
|
|
"\(identity)(#\(hash))"
|
|
}
|
|
|
|
var debugDescription: String {
|
|
description
|
|
}
|
|
}
|
|
|
|
/// A type with precisely controlled hash values. This can be used to set up
|
|
/// hash tables with fully deterministic contents.
|
|
struct RawCollider:
|
|
Hashable, CustomStringConvertible
|
|
{
|
|
var identity: Int
|
|
var hash: Hash
|
|
|
|
init(_ identity: Int, _ hash: Hash) {
|
|
self.identity = identity
|
|
self.hash = hash
|
|
}
|
|
|
|
init(_ identity: Int) {
|
|
self.identity = identity
|
|
self.hash = Hash(identity)
|
|
}
|
|
|
|
init(_ identity: String) {
|
|
self.hash = Hash(identity)!
|
|
self.identity = hash.value
|
|
}
|
|
|
|
static func ==(left: Self, right: Self) -> Bool {
|
|
guard left.identity == right.identity else { return false }
|
|
precondition(left.hash == right.hash)
|
|
return true
|
|
}
|
|
|
|
var hashValue: Int {
|
|
fatalError("Don't")
|
|
}
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
fatalError("Don't")
|
|
}
|
|
|
|
func _rawHashValue(seed: Int) -> Int {
|
|
hash.value
|
|
}
|
|
|
|
var description: String {
|
|
"\(identity)#\(hash)"
|
|
}
|
|
}
|