Last active
September 27, 2021 15:07
-
-
Save AlexAvlonitis/a15c39bcddb22e5d0b8a45a39c6faf1e to your computer and use it in GitHub Desktop.
Simple hash table implemention in 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
# 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