This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SpencerBeige's profile picture

help hash

Started by SpencerBeige, 24 February 2015 - 02:58 PM
SpencerBeige #1
Posted 24 February 2015 - 04:00 PM
can anyone turn:

    uint32_t hash, i;
    for(hash = i = 0; i < len; ++i)
    {
	    hash += key[i];
	    hash += (hash << 10);
	    hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
into lua? its the 'jenkin's hash, which im going to use in a program, but i just…cant'

the reason i cant is im not good in this language. i'd give the first person to do it, a virtual cookie
GopherAtl #2
Posted 24 February 2015 - 04:09 PM
Guessing that key is a string? if so…


local function jenkins(key)
  local hash, i=0,0
  for i = 1,  #key do
    hash = hash + string.byte(key,i)
    hash = (hash  + hash*1024)%0x100000000
    hash = bit.bxor(hash,math.floor(hash/64))
  end
  hash =(hash+hash*8)%0x100000000
  hash=bit.bxor(hash,math.floor(hash/2048))
  hash = (hash + hash*32767)%0x100000000
  return hash
end

:edit: rapid-fire series of edits as I was having a derpy moment there
Edited on 24 February 2015 - 03:15 PM
SpencerBeige #3
Posted 24 February 2015 - 04:10 PM
thanks!
SpencerBeige #4
Posted 24 February 2015 - 04:33 PM
[content removed]
Edited on 24 February 2015 - 03:35 PM