Skip to content

Commit 7ed7354

Browse files
authored
Merge pull request #16009 from lorentey/brand-new-combine-harvester2
[SE-0206][stdlib] Implement Hasher API (underscored)
2 parents 78ca835 + dffbbd1 commit 7ed7354

30 files changed

+705
-525
lines changed

stdlib/public/core/Arrays.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ extension ${Self}: Hashable where Element: Hashable {
22712271
@inlinable // FIXME(sil-serialize-all)
22722272
public func _hash(into hasher: inout _Hasher) {
22732273
for element in self {
2274-
hasher.append(element)
2274+
hasher.combine(element)
22752275
}
22762276
}
22772277
}

stdlib/public/core/Bool.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ extension Bool : Equatable, Hashable {
159159

160160
@inlinable // FIXME(sil-serialize-all)
161161
public func _hash(into hasher: inout _Hasher) {
162-
hasher.append((self ? 1 : 0) as UInt8)
162+
hasher.combine((self ? 1 : 0) as UInt8)
163163
}
164164

165165
@inlinable // FIXME(sil-serialize-all)

stdlib/public/core/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ set(SWIFTLIB_ESSENTIAL
6666
AnyHashable.swift
6767
# END WORKAROUND
6868
HashedCollectionsAnyHashableExtensions.swift
69+
Hasher.swift
6970
Hashing.swift
7071
HeapBuffer.swift
7172
ICU.swift
@@ -105,7 +106,7 @@ set(SWIFTLIB_ESSENTIAL
105106
Reverse.swift
106107
Runtime.swift.gyb
107108
RuntimeFunctionCounters.swift
108-
SipHash.swift.gyb
109+
SipHash.swift
109110
SentinelCollection.swift
110111
Sequence.swift
111112
SequenceAlgorithms.swift

stdlib/public/core/CTypes.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ extension OpaquePointer: Hashable {
186186

187187
@inlinable // FIXME(sil-serialize-all)
188188
public func _hash(into hasher: inout _Hasher) {
189-
hasher.append(Int(Builtin.ptrtoint_Word(_rawValue)))
189+
hasher.combine(Int(Builtin.ptrtoint_Word(_rawValue)))
190190
}
191191
}
192192

stdlib/public/core/ClosedRange.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ where Bound: Strideable, Bound.Stride: SignedInteger, Bound: Hashable {
177177
public func _hash(into hasher: inout _Hasher) {
178178
switch self {
179179
case .inRange(let value):
180-
hasher.append(0 as Int8)
181-
hasher.append(value)
180+
hasher.combine(0 as Int8)
181+
hasher.combine(value)
182182
case .pastEnd:
183-
hasher.append(1 as Int8)
183+
hasher.combine(1 as Int8)
184184
}
185185
}
186186
}
@@ -396,8 +396,8 @@ extension ClosedRange: Hashable where Bound: Hashable {
396396

397397
@inlinable // FIXME(sil-serialize-all)
398398
public func _hash(into hasher: inout _Hasher) {
399-
hasher.append(lowerBound)
400-
hasher.append(upperBound)
399+
hasher.combine(lowerBound)
400+
hasher.combine(upperBound)
401401
}
402402
}
403403

stdlib/public/core/Dictionary.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -1453,11 +1453,11 @@ extension Dictionary: Hashable where Value: Hashable {
14531453
var commutativeHash = 0
14541454
for (k, v) in self {
14551455
var elementHasher = _Hasher()
1456-
elementHasher.append(k)
1457-
elementHasher.append(v)
1456+
elementHasher.combine(k)
1457+
elementHasher.combine(v)
14581458
commutativeHash ^= elementHasher.finalize()
14591459
}
1460-
hasher.append(commutativeHash)
1460+
hasher.combine(commutativeHash)
14611461
}
14621462
}
14631463

@@ -2436,8 +2436,8 @@ extension _NativeDictionaryBuffer where Key: Hashable
24362436
@inlinable // FIXME(sil-serialize-all)
24372437
@inline(__always) // For performance reasons.
24382438
internal func _bucket(_ k: Key) -> Int {
2439-
var hasher = _Hasher(seed: _storage.seed)
2440-
hasher.append(k)
2439+
var hasher = _Hasher(_seed: _storage.seed)
2440+
hasher.combine(k)
24412441
return hasher.finalize() & _bucketMask
24422442
}
24432443

stdlib/public/core/DropWhile.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ extension LazyDropWhileCollection.Index: Hashable where Base.Index: Hashable {
190190

191191
@inlinable // FIXME(sil-serialize-all)
192192
public func _hash(into hasher: inout _Hasher) {
193-
hasher.append(base)
193+
hasher.combine(base)
194194
}
195195
}
196196

stdlib/public/core/Flatten.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ extension FlattenCollection.Index : Hashable
236236

237237
@inlinable // FIXME(sil-serialize-all)
238238
public func _hash(into hasher: inout _Hasher) {
239-
hasher.append(_outer)
240-
hasher.append(_inner)
239+
hasher.combine(_outer)
240+
hasher.combine(_inner)
241241
}
242242
}
243243

stdlib/public/core/FloatingPointTypes.swift.gyb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1539,10 +1539,10 @@ extension ${Self} : Hashable {
15391539
v = 0
15401540
}
15411541
%if bits == 80:
1542-
hasher.append(v._representation.signAndExponent)
1543-
hasher.append(v.significandBitPattern)
1542+
hasher.combine(v._representation.signAndExponent)
1543+
hasher.combine(v.significandBitPattern)
15441544
%else:
1545-
hasher.append(v.bitPattern)
1545+
hasher.combine(v.bitPattern)
15461546
%end
15471547
}
15481548

stdlib/public/core/GroupInfo.json

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
"Interval.swift",
166166
"Hashing.swift",
167167
"SipHash.swift",
168+
"Hasher.swift",
168169
"ErrorType.swift",
169170
"InputStream.swift",
170171
"LifetimeManager.swift",

stdlib/public/core/Hashable.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,19 @@ public protocol Hashable : Equatable {
114114
}
115115

116116
extension Hashable {
117+
@inlinable
117118
@inline(__always)
118119
public func _hash(into hasher: inout _Hasher) {
119-
hasher.append(self.hashValue)
120+
hasher.combine(self.hashValue)
120121
}
121122
}
122123

123124
// Called by synthesized `hashValue` implementations.
125+
@inlinable
124126
@inline(__always)
125127
public func _hashValue<H: Hashable>(for value: H) -> Int {
126128
var hasher = _Hasher()
127-
hasher.append(value)
129+
hasher.combine(value)
128130
return hasher.finalize()
129131
}
130132

0 commit comments

Comments
 (0)