Skip to content

Instantly share code, notes, and snippets.

@AlexAvlonitis
Last active September 27, 2021 15:07
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 AlexAvlonitis/a15c39bcddb22e5d0b8a45a39c6faf1e to your computer and use it in GitHub Desktop.
Save AlexAvlonitis/a15c39bcddb22e5d0b8a45a39c6faf1e to your computer and use it in GitHub Desktop.
Simple hash table implemention in ruby
# https://github.com/alexavlonitis
# Simple HashMap/HashTable implementation in ruby
class HashMap
def initialize(table_size = 18)
@table = Array.new(table_size)
end
def set(key, value)
table_index = table_index(key)
if @table[table_index]
@table[table_index] << value
else
@table[table_index] = [value]
end
end
def get(key)
table_index = table_index(key)
@table[table_index]
end
private
def table_index(key)
key.hash % @table.size
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment