Permalink
Browse files

Merge pull request #2 from eric/remove-to_s-on-keys

Do not force keys to be strings
  • Loading branch information...
2 parents 409b997 + c21a4d0 commit fd8d59acce2789e2a046565de97e715c12ad3a96 @nahi committed Mar 19, 2012
Showing with 30 additions and 14 deletions.
  1. +4 −4 lib/avl_tree.rb
  2. +4 −4 lib/red_black_tree.rb
  3. +11 −3 test/test_avl_tree.rb
  4. +11 −3 test/test_red_black_tree.rb
View
@@ -355,17 +355,17 @@ def clear
end
def []=(key, value)
- @root = @root.insert(key.to_s, value)
+ @root = @root.insert(key, value)
end
alias insert []=
def key?(key)
- @root.retrieve(key.to_s) != Node::UNDEFINED
+ @root.retrieve(key) != Node::UNDEFINED
end
alias has_key? key?
def [](key)
- value = @root.retrieve(key.to_s)
+ value = @root.retrieve(key)
if value == Node::UNDEFINED
default_value
else
@@ -374,7 +374,7 @@ def [](key)
end
def delete(key)
- deleted, @root = @root.delete(key.to_s)
+ deleted, @root = @root.delete(key)
deleted.value
end
@@ -496,19 +496,19 @@ def clear
end
def []=(key, value)
- @root = @root.insert(key.to_s, value)
+ @root = @root.insert(key, value)
@root.set_root
@root.check_height if $DEBUG
end
alias insert []=
def key?(key)
- @root.retrieve(key.to_s) != Node::UNDEFINED
+ @root.retrieve(key) != Node::UNDEFINED
end
alias has_key? key?
def [](key)
- value = @root.retrieve(key.to_s)
+ value = @root.retrieve(key)
if value == Node::UNDEFINED
default_value
else
@@ -517,7 +517,7 @@ def [](key)
end
def delete(key)
- deleted, @root, rebalance = @root.delete(key.to_s)
+ deleted, @root, rebalance = @root.delete(key)
unless empty?
@root.set_root
@root.check_height if $DEBUG
View
@@ -372,9 +372,9 @@ def test_values
def test_to_s
h = AVLTree.new
- h[:abc] = 1
- assert_equal 1, h["abc"]
- assert_equal 1, h[:abc]
+ h[5] = 1
+ assert_equal 1, h[5]
+ assert_nil h["5"]
end
def test_key?
@@ -424,6 +424,14 @@ def test_clear
assert h.to_hash.empty?
end
+ def test_non_string_keys
+ h = AVLTree.new
+ h[1.3] = 'a'
+ h[4.3] = 'b'
+
+ assert_equal [1.3, 'a' ], h.first
+ end
+
if RUBY_VERSION >= '1.9.0'
# In contrast to RadixTree, AVLTree just uses String#<=> as-is
def test_encoding
@@ -522,9 +522,9 @@ def test_values
def test_to_s
h = RedBlackTree.new
- h[:abc] = 1
- assert_equal 1, h["abc"]
- assert_equal 1, h[:abc]
+ h[5] = 1
+ assert_equal 1, h[5]
+ assert_nil h["5"]
end
def test_key?
@@ -574,6 +574,14 @@ def test_clear
assert h.to_hash.empty?
end
+ def test_non_string_keys
+ h = RedBlackTree.new
+ h[1.3] = 'a'
+ h[4.3] = 'b'
+
+ assert_equal [1.3, 'a' ], h.first
+ end
+
if RUBY_VERSION >= '1.9.0'
# In contrast to RadixTree, RedBlackTree just uses String#<=> as-is
def test_encoding

0 comments on commit fd8d59a

Please sign in to comment.