Created
February 3, 2010 17:53
-
-
Save max-mapper/293825 to your computer and use it in GitHub Desktop.
simple hash table in pure ruby
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
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