Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift Hash Functions
import Foundation
struct Country {
let name: String
let capital: String
var visited: Bool
}
extension Country: Equatable {
static func == (lhs: Country, rhs: Country) -> Bool {
return lhs.name == rhs.name &&
lhs.capital == rhs.capital &&
lhs.visited == rhs.visited
}
}
extension Country: Comparable {
static func < (lhs: Country, rhs: Country) -> Bool {
return lhs.name < rhs.name ||
(lhs.name == rhs.name && lhs.capital < rhs.capital) ||
(lhs.name == rhs.name && lhs.capital == rhs.capital && rhs.visited)
}
}
// http://www.cse.yorku.ca/~oz/hash.html
extension String {
// hash(0) = 5381
// hash(i) = hash(i - 1) * 33 ^ str[i];
var djb2hash: Int {
let unicodeScalars = self.unicodeScalars.map { $0.value }
return unicodeScalars.reduce(5381) {
($0 << 5) &+ $0 &+ Int($1)
}
}
// hash(0) = 0
// hash(i) = hash(i - 1) * 65599 + str[i];
var sdbmhash: Int {
let unicodeScalars = self.unicodeScalars.map { $0.value }
return unicodeScalars.reduce(0) {
Int($1) &+ ($0 << 6) &+ ($0 << 16) - $0
}
}
}
// https://github.com/krzysztofzablocki/Sourcery
// MIT License
//
// Copyright (c) 2016 Krzysztof Zabłocki
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
fileprivate func combineHashes(_ hashes: [Int]) -> Int {
return hashes.reduce(0, combineHashValues)
}
fileprivate func combineHashValues(_ initial: Int, _ other: Int) -> Int {
#if arch(x86_64) || arch(arm64)
let magic: UInt = 0x9e3779b97f4a7c15
#elseif arch(i386) || arch(arm)
let magic: UInt = 0x9e3779b9
#endif
var lhs = UInt(bitPattern: initial)
let rhs = UInt(bitPattern: other)
lhs ^= rhs &+ magic &+ (lhs << 6) &+ (lhs >> 2)
return Int(bitPattern: lhs)
}
// Three different approachs to combining the hash values
// of the properties of a structure.
extension Country: Hashable {
// XOR the three values. Simple and works well in this case
// where we do not expect our name and capital to have
// matching values.
var simpleHashValue: Int {
return name.hashValue ^ capital.hashValue ^ visited.hashValue
}
// Avoid common collisions when name and capital can have
// matching values by applying a different hash method to
// one of the values
var djb2HashValue: Int {
return name.djb2hash ^ capital.hashValue ^ visited.hashValue
}
// Use the boost hash combine function to combine an array
// of hashValues.
var boostHashValue: Int {
return combineHashes([name.hashValue,capital.hashValue,visited.hashValue])
}
// Return the hash value using one of the above approaches
var hashValue: Int {
return boostHashValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.