Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SE-0206][stdlib] Implement Hasher API (underscored) #16009

Merged
merged 3 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ extension ${Self}: Hashable where Element: Hashable {
@inlinable // FIXME(sil-serialize-all)
public func _hash(into hasher: inout _Hasher) {
for element in self {
hasher.append(element)
hasher.combine(element)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ extension Bool : Equatable, Hashable {

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

@inlinable // FIXME(sil-serialize-all)
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(SWIFTLIB_ESSENTIAL
AnyHashable.swift
# END WORKAROUND
HashedCollectionsAnyHashableExtensions.swift
Hasher.swift
Hashing.swift
HeapBuffer.swift
ICU.swift
Expand Down Expand Up @@ -105,7 +106,7 @@ set(SWIFTLIB_ESSENTIAL
Reverse.swift
Runtime.swift.gyb
RuntimeFunctionCounters.swift
SipHash.swift.gyb
SipHash.swift
SentinelCollection.swift
Sequence.swift
SequenceAlgorithms.swift
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ extension OpaquePointer: Hashable {

@inlinable // FIXME(sil-serialize-all)
public func _hash(into hasher: inout _Hasher) {
hasher.append(Int(Builtin.ptrtoint_Word(_rawValue)))
hasher.combine(Int(Builtin.ptrtoint_Word(_rawValue)))
}
}

Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/core/ClosedRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ where Bound: Strideable, Bound.Stride: SignedInteger, Bound: Hashable {
public func _hash(into hasher: inout _Hasher) {
switch self {
case .inRange(let value):
hasher.append(0 as Int8)
hasher.append(value)
hasher.combine(0 as Int8)
hasher.combine(value)
case .pastEnd:
hasher.append(1 as Int8)
hasher.combine(1 as Int8)
}
}
}
Expand Down Expand Up @@ -396,8 +396,8 @@ extension ClosedRange: Hashable where Bound: Hashable {

@inlinable // FIXME(sil-serialize-all)
public func _hash(into hasher: inout _Hasher) {
hasher.append(lowerBound)
hasher.append(upperBound)
hasher.combine(lowerBound)
hasher.combine(upperBound)
}
}

Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1453,11 +1453,11 @@ extension Dictionary: Hashable where Value: Hashable {
var commutativeHash = 0
for (k, v) in self {
var elementHasher = _Hasher()
elementHasher.append(k)
elementHasher.append(v)
elementHasher.combine(k)
elementHasher.combine(v)
commutativeHash ^= elementHasher.finalize()
}
hasher.append(commutativeHash)
hasher.combine(commutativeHash)
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/DropWhile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ extension LazyDropWhileCollection.Index: Hashable where Base.Index: Hashable {

@inlinable // FIXME(sil-serialize-all)
public func _hash(into hasher: inout _Hasher) {
hasher.append(base)
hasher.combine(base)
}
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/Flatten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ extension FlattenCollection.Index : Hashable

@inlinable // FIXME(sil-serialize-all)
public func _hash(into hasher: inout _Hasher) {
hasher.append(_outer)
hasher.append(_inner)
hasher.combine(_outer)
hasher.combine(_inner)
}
}

Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1539,10 +1539,10 @@ extension ${Self} : Hashable {
v = 0
}
%if bits == 80:
hasher.append(v._representation.signAndExponent)
hasher.append(v.significandBitPattern)
hasher.combine(v._representation.signAndExponent)
hasher.combine(v.significandBitPattern)
%else:
hasher.append(v.bitPattern)
hasher.combine(v.bitPattern)
%end
}

Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"Interval.swift",
"Hashing.swift",
"SipHash.swift",
"Hasher.swift",
"ErrorType.swift",
"InputStream.swift",
"LifetimeManager.swift",
Expand Down
6 changes: 4 additions & 2 deletions stdlib/public/core/Hashable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,19 @@ public protocol Hashable : Equatable {
}

extension Hashable {
@inlinable
@inline(__always)
public func _hash(into hasher: inout _Hasher) {
hasher.append(self.hashValue)
hasher.combine(self.hashValue)
}
}

// Called by synthesized `hashValue` implementations.
@inlinable
@inline(__always)
public func _hashValue<H: Hashable>(for value: H) -> Int {
var hasher = _Hasher()
hasher.append(value)
hasher.combine(value)
return hasher.finalize()
}

Expand Down
Loading