From 879e05964dbc6d9b91f25d54594825491085f0e9 Mon Sep 17 00:00:00 2001 From: freak4pc Date: Tue, 26 Mar 2019 16:20:14 +0200 Subject: [PATCH] Use Hashable's `hash(into:)` when available --- Sources/Differentiator/Diff.swift | 18 ++++++++++++------ Sources/Differentiator/ItemPath.swift | 9 +++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Sources/Differentiator/Diff.swift b/Sources/Differentiator/Diff.swift index b1ff3948..b9938171 100644 --- a/Sources/Differentiator/Diff.swift +++ b/Sources/Differentiator/Diff.swift @@ -126,21 +126,22 @@ public enum Diff { //================================================================================ // swift dictionary optimizations { - private struct OptimizedIdentity : Hashable { - + private struct OptimizedIdentity: Hashable { + #if swift(>=4.2) + #else let hashValue: Int + #endif let identity: UnsafePointer init(_ identity: UnsafePointer) { self.identity = identity + #if swift(>=4.2) + #else self.hashValue = identity.pointee.hashValue + #endif } static func == (lhs: OptimizedIdentity, rhs: OptimizedIdentity) -> Bool { - if lhs.hashValue != rhs.hashValue { - return false - } - if lhs.identity.distance(to: rhs.identity) == 0 { return true } @@ -148,6 +149,11 @@ public enum Diff { return lhs.identity.pointee == rhs.identity.pointee } + #if swift(>=4.2) + public func hash(into hasher: inout Hasher) { + hasher.combine(self.identity.pointee) + } + #endif } private static func calculateAssociatedData( diff --git a/Sources/Differentiator/ItemPath.swift b/Sources/Differentiator/ItemPath.swift index 9551e112..3ae7b610 100644 --- a/Sources/Differentiator/ItemPath.swift +++ b/Sources/Differentiator/ItemPath.swift @@ -27,9 +27,14 @@ public func == (lhs: ItemPath, rhs: ItemPath) -> Bool { } extension ItemPath: Hashable { - + #if swift(>=4.2) + public func hash(into hasher: inout Hasher) { + hasher.combine(sectionIndex.byteSwapped) + hasher.combine(itemIndex) + } + #else public var hashValue: Int { return sectionIndex.byteSwapped.hashValue ^ itemIndex.hashValue } - + #endif }