Skip to content

Commit 2e1f550

Browse files
committed
Oops, fix RawCollider conflict
1 parent 0006013 commit 2e1f550

5 files changed

Lines changed: 96 additions & 132 deletions

File tree

Tests/HashTreeCollectionsTests/Colliders.swift

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Collections open source project
4+
//
5+
// Copyright (c) 2022 - 2026 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
/// A type with manually controlled hash values, for easy testing of collision
15+
/// scenarios.
16+
package struct Collider:
17+
Hashable, CustomStringConvertible, CustomDebugStringConvertible
18+
{
19+
package var identity: Int
20+
package var hash: Hash
21+
22+
package init(_ identity: Int, _ hash: Hash) {
23+
self.identity = identity
24+
self.hash = hash
25+
}
26+
27+
package init(_ identity: Int) {
28+
self.identity = identity
29+
self.hash = Hash(identity)
30+
}
31+
32+
package init(_ identity: String) {
33+
self.hash = Hash(identity)!
34+
self.identity = hash.value
35+
}
36+
37+
package static func ==(left: Self, right: Self) -> Bool {
38+
guard left.identity == right.identity else { return false }
39+
precondition(left.hash == right.hash)
40+
return true
41+
}
42+
43+
package func hash(into hasher: inout Hasher) {
44+
hasher.combine(hash.value)
45+
}
46+
47+
package var description: String {
48+
"\(identity)(#\(hash))"
49+
}
50+
51+
package var debugDescription: String {
52+
description
53+
}
54+
}

Tests/HashTreeCollectionsTests/Hash.swift renamed to Tests/_CollectionsTestSupport/Utilities/Hash.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,39 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
/// An abstract representation of a hash value.
15-
struct Hash {
16-
var value: Int
15+
package struct Hash {
16+
package var value: Int
1717

18-
init(_ value: Int) {
18+
package init(_ value: Int) {
1919
self.value = value
2020
}
2121

22-
init(_ value: UInt) {
22+
package init(_ value: UInt) {
2323
self.value = Int(bitPattern: value)
2424
}
2525
}
2626

2727

2828
extension Hash {
29-
static var bucketBitWidth: Int { 5 }
30-
static var bucketCount: Int { 1 << bucketBitWidth }
31-
static var bitWidth: Int { UInt.bitWidth }
29+
package static var bucketBitWidth: Int { 5 }
30+
package static var bucketCount: Int { 1 << bucketBitWidth }
31+
package static var bitWidth: Int { UInt.bitWidth }
3232
}
3333

3434
extension Hash: Equatable {
35-
static func ==(left: Self, right: Self) -> Bool {
35+
package static func ==(left: Self, right: Self) -> Bool {
3636
left.value == right.value
3737
}
3838
}
3939

4040
extension Hash: Hashable {
41-
func hash(into hasher: inout Hasher) {
41+
package func hash(into hasher: inout Hasher) {
4242
hasher.combine(value)
4343
}
4444
}
4545

4646
extension Hash: CustomStringConvertible {
47-
var description: String {
47+
package var description: String {
4848
// Print hash values in radix 32 & reversed, so that the path in the hash
4949
// tree is readily visible.
5050
let p = String(
@@ -62,21 +62,21 @@ extension Hash: CustomStringConvertible {
6262
}
6363

6464
extension Hash: LosslessStringConvertible {
65-
init?(_ description: String) {
65+
package init?(_ description: String) {
6666
let s = String(description.reversed())
6767
guard let hash = UInt(s, radix: 32) else { return nil }
6868
self.init(Int(bitPattern: hash))
6969
}
7070
}
7171

7272
extension Hash: ExpressibleByIntegerLiteral {
73-
init(integerLiteral value: UInt) {
73+
package init(integerLiteral value: UInt) {
7474
self.init(value)
7575
}
7676
}
7777

7878
extension Hash: ExpressibleByStringLiteral {
79-
init(stringLiteral value: String) {
79+
package init(stringLiteral value: String) {
8080
self.init(value)!
8181
}
8282
}

Tests/BasicContainersTests/RawCollider.swift renamed to Tests/_CollectionsTestSupport/Utilities/RawCollider.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,31 @@ package struct RawCollider:
1717
Hashable, CustomStringConvertible
1818
{
1919
package var identity: Int
20-
package var rawHashValue: Int
20+
package var hash: Hash
21+
22+
package init(_ identity: Int, _ hash: Hash) {
23+
self.identity = identity
24+
self.hash = hash
25+
}
2126

2227
package init(_ identity: Int, _ rawHashValue: Int) {
2328
self.identity = identity
24-
self.rawHashValue = rawHashValue
29+
self.hash = Hash(rawHashValue)
2530
}
2631

2732
package init(_ identity: Int) {
2833
self.identity = identity
29-
self.rawHashValue = identity
34+
self.hash = Hash(identity)
35+
}
36+
37+
package init(_ identity: String) {
38+
self.hash = Hash(identity)!
39+
self.identity = hash.value
3040
}
3141

3242
package static func ==(left: Self, right: Self) -> Bool {
3343
guard left.identity == right.identity else { return false }
34-
precondition(left.rawHashValue == right.rawHashValue)
44+
precondition(left.hash == right.hash)
3545
return true
3646
}
3747

@@ -44,10 +54,10 @@ package struct RawCollider:
4454
}
4555

4656
package func _rawHashValue(seed: Int) -> Int {
47-
rawHashValue
57+
hash.value
4858
}
4959

5060
package var description: String {
51-
"\(identity)#\(rawHashValue)"
61+
"\(identity)#\(hash)"
5262
}
5363
}

0 commit comments

Comments
 (0)