Skip to content

Instantly share code, notes, and snippets.

@marcinkuptel
Created February 16, 2016 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcinkuptel/c7a8fdcd36ca8b88590a to your computer and use it in GitHub Desktop.
Save marcinkuptel/c7a8fdcd36ca8b88590a to your computer and use it in GitHub Desktop.
Function computing a hash value of a string
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