Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created February 3, 2010 17:53
Show Gist options
  • Save max-mapper/293825 to your computer and use it in GitHub Desktop.
Save max-mapper/293825 to your computer and use it in GitHub Desktop.
simple hash table in pure ruby
class GammaDonkey
def initialize
@data = Array.new
@keys = Array.new
end
def hashed(value)
magic = 2166136261
value.each_byte {|byte| magic = ( magic * 16777619 ) ^ byte}
magic
end
def sort(value)
value.to_s[0..2].to_i
end
def [](k)
sortval = sort(hashed(k))
@data[sortval][@keys[sortval].index(k)]
end
def []=(k,v)
sortval = sort(hashed(k))
keybucket = @keys[sortval] || Array.new
keybucket << k
@keys[sortval] = keybucket
databucket = @data[sortval] || Array.new
databucket[@keys[sortval].index(k)] = v
@data[sortval] = databucket
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment