From ed182eb425ce67a0112dffb3503a6ac38979af86 Mon Sep 17 00:00:00 2001 From: Christian Treffs Date: Thu, 9 Jul 2020 22:25:10 +0200 Subject: [PATCH] Make string hash functions return UInt64 based. - These are used to produce a stable hash, so we want all platforms to produce the same hash value, therefore we use UInt64 --- Sources/FirebladeECS/Hashing.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/FirebladeECS/Hashing.swift b/Sources/FirebladeECS/Hashing.swift index 9c2ed41..29b228d 100644 --- a/Sources/FirebladeECS/Hashing.swift +++ b/Sources/FirebladeECS/Hashing.swift @@ -91,10 +91,10 @@ public enum StringHashing { /// /// public static func singer_djb2(_ utf8String: String) -> UInt64 { - var hash = UInt64(5381) + var hash: UInt64 = 5381 var iter = utf8String.unicodeScalars.makeIterator() while let char = iter.next() { - hash = 127 * (hash & 0x00ffffffffffffff) &+ UInt64(char.value) + hash = 127 * (hash & 0xFFFFFFFFFFFFFF) &+ UInt64(char.value) } return hash } @@ -106,11 +106,11 @@ public enum StringHashing { /// The magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained. /// /// - public static func bernstein_djb2(_ string: String) -> UInt { - var hash: UInt = 5381 + public static func bernstein_djb2(_ string: String) -> UInt64 { + var hash: UInt64 = 5381 var iter = string.unicodeScalars.makeIterator() while let char = iter.next() { - hash = (hash << 5) &+ hash &+ UInt(char.value) + hash = (hash << 5) &+ hash &+ UInt64(char.value) } return hash } @@ -122,11 +122,11 @@ public enum StringHashing { /// It also happens to be a good general hashing function with good distribution. /// /// - public static func sdbm(_ string: String) -> UInt { - var hash: UInt = 0 + public static func sdbm(_ string: String) -> UInt64 { + var hash: UInt64 = 0 var iter = string.unicodeScalars.makeIterator() while let char = iter.next() { - hash = (UInt(char.value) &+ (hash << 6) &+ (hash << 16)) + hash = (UInt64(char.value) &+ (hash << 6) &+ (hash << 16)) } return hash }