Created
February 16, 2016 20:49
-
-
Save marcinkuptel/c7a8fdcd36ca8b88590a to your computer and use it in GitHub Desktop.
Function computing a hash value of a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let prime = 1231247 | |
let alphabetSize = 26 | |
let A: UInt32 = 96 | |
func stringHash(inout input: String, fromIndex: String.UnicodeScalarView.Index, length: Int) -> Int { | |
var hash = 0 | |
var index = fromIndex | |
for i in 0..<length { | |
let char = input.unicodeScalars[index] | |
hash += Int(char.value - A) * Int(pow(Float(alphabetSize), Float(length - 1 - i))) % prime | |
index = index.successor() | |
} | |
return hash | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment