Skip to content

Instantly share code, notes, and snippets.

@tamanyan
Last active December 21, 2015 19:09
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 tamanyan/6352131 to your computer and use it in GitHub Desktop.
Save tamanyan/6352131 to your computer and use it in GitHub Desktop.
consistent hashing hash selection algorithm
def getHash(hashes, val)
start = 0
finish = hashes.size - 1
val = val % hashes[finish]
if hashes[start] >= val
return hashes[0]
end
mid = start
while finish - start > 1
mid = (start + finish) / 2
if hashes[mid] >= val
finish = mid
else
start = mid
end
end
return hashes[finish]
end
hashes = [123, 1243, 3455, 53423, 343234] # sorted hash array
puts getHash(hashes, 126)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment