Commit 4dc3879

mo khan <mo@mokhan.ca>
2013-10-13 03:00:09
add a primitive hash table.
1 parent 9f8f967
Changed files (3)
lib
data_structures
spec
lib/data_structures/hash_table.rb
@@ -0,0 +1,16 @@
+class HashTable
+  MAX_SIZE = 64
+
+  def initialize(items = [], max = MAX_SIZE)
+    @items = items
+    @max = max
+  end
+
+  def []=(key, value)
+    @items[key.hash % @max] = value
+  end
+
+  def [](key)
+    @items[key.hash % @max]
+  end
+end
spec/data_structures/hash_table_spec.rb
@@ -0,0 +1,14 @@
+require "spec_helper"
+
+describe HashTable do
+  let(:sut) { HashTable.new }
+
+  it "returns a default value" do
+    sut[:unkonwn].should be_nil
+  end
+
+  it "can return the value associated to a key" do
+    sut[:name] = 'mo'
+    sut[:name].should == 'mo'
+  end
+end
spec/spec_helper.rb
@@ -10,3 +10,4 @@ require_relative '../lib/data_structures/linked_list_stack'
 require_relative '../lib/data_structures/dynamic_array_stack'
 require_relative '../lib/data_structures/binary_tree'
 require_relative '../lib/data_structures/avl_tree'
+require_relative '../lib/data_structures/hash_table'