From 9fc1a1220a490e60d2ea60e9effc2f68a58c0423 Mon Sep 17 00:00:00 2001 From: nahome Date: Wed, 13 Jun 2018 23:08:33 -0700 Subject: [PATCH 1/3] hashing function --- .../04-hashes-part-1/hash_item.rb | 4 ++- .../04-hashes-part-1/hashclass.rb | 25 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/01-data-structures/04-hashes-part-1/hash_item.rb b/01-data-structures/04-hashes-part-1/hash_item.rb index 4a420212..5953907a 100644 --- a/01-data-structures/04-hashes-part-1/hash_item.rb +++ b/01-data-structures/04-hashes-part-1/hash_item.rb @@ -3,5 +3,7 @@ class HashItem attr_accessor :value def initialize(key, value) + @key = key + @value = value end -end \ No newline at end of file +end diff --git a/01-data-structures/04-hashes-part-1/hashclass.rb b/01-data-structures/04-hashes-part-1/hashclass.rb index e538428a..68232e03 100644 --- a/01-data-structures/04-hashes-part-1/hashclass.rb +++ b/01-data-structures/04-hashes-part-1/hashclass.rb @@ -1,27 +1,50 @@ class HashClass + i = index(key, size()) + def initialize(size) @items = Array.new(size) end def []=(key, value) + + if @items[i] == nil + @items[i] = HashItem.new(key,value) + elsif @items[i].key != key + self.resize + self[key] = value + elsif @items[i].key == key && @items[i] != value + self.resize + @items[i].value = value + end end def [](key) + @items[i].value end def resize + @size = @size * 2 + resized_hash = Array.new(@size) + @items.each do |item| + if item != nil + resized_hash[index(item.key, @size)] = item + end + end + @items = resized_hash end # Returns a unique, deterministically reproducible index into an array # We are hashing based on strings, let's use the ascii value of each string as # a starting point. def index(key, size) + key.sum % size end # Simple method to return the number of items in the hash def size + @items.length end -end \ No newline at end of file +end From aa656a5002c373be3974f17b814d94562cfdb86b Mon Sep 17 00:00:00 2001 From: nahome Date: Fri, 15 Jun 2018 22:51:19 -0700 Subject: [PATCH 2/3] revised hashes --- .../04-hashes-part-1/hashclass.rb | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/01-data-structures/04-hashes-part-1/hashclass.rb b/01-data-structures/04-hashes-part-1/hashclass.rb index 68232e03..f4968c4f 100644 --- a/01-data-structures/04-hashes-part-1/hashclass.rb +++ b/01-data-structures/04-hashes-part-1/hashclass.rb @@ -1,31 +1,37 @@ class HashClass - i = index(key, size()) + def initialize(size) @items = Array.new(size) end def []=(key, value) - - if @items[i] == nil + i = index(key, size()) + new_item = @items[i] + if new_item == nil @items[i] = HashItem.new(key,value) - elsif @items[i].key != key - self.resize + elsif new_item.key != key + while @items[index(key, size())].key != nil && @items[index(key, size())].key != key + resize() + j = index(key, size()) + break if @items[j] == nil + end self[key] = value - elsif @items[i].key == key && @items[i] != value - self.resize - @items[i].value = value + elsif new_item.key == key && new_item.value != value + resize() + new_item.value = value end end def [](key) + i = index(key, size()) @items[i].value end def resize - @size = @size * 2 + @size = @items.length * 2 resized_hash = Array.new(@size) @items.each do |item| if item != nil From c9311b323688049e1f02f160ccef43155a4527cf Mon Sep 17 00:00:00 2001 From: nahome Date: Fri, 15 Jun 2018 22:56:00 -0700 Subject: [PATCH 3/3] hashes revision --- 01-data-structures/04-hashes-part-1/hashclass.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/01-data-structures/04-hashes-part-1/hashclass.rb b/01-data-structures/04-hashes-part-1/hashclass.rb index f4968c4f..11e79caa 100644 --- a/01-data-structures/04-hashes-part-1/hashclass.rb +++ b/01-data-structures/04-hashes-part-1/hashclass.rb @@ -1,6 +1,6 @@ -class HashClass - +require_relative 'hash_item' +class HashClass def initialize(size) @items = Array.new(size)