Skip to content

Commit

Permalink
Showing 2 changed files with 27 additions and 5 deletions.
15 changes: 10 additions & 5 deletions core/src/main/java/org/jruby/RubyHash.java
Original file line number Diff line number Diff line change
@@ -485,6 +485,11 @@ protected static int hashValue(final int h) {
return MRI_HASH ? MRIHashValue(h) : JavaSoftHashValue(h);
}

protected final int hashValue(final IRubyObject key) {
final int h = isComparedByIdentity() ? System.identityHashCode(key) : key.hashCode();
return MRI_HASH ? MRIHashValue(h) : JavaSoftHashValue(h);
}

private static int bucketIndex(final int h, final int length) {
return MRI_HASH ? MRIBucketIndex(h, length) : JavaSoftBucketIndex(h, length);
}
@@ -518,7 +523,7 @@ protected void internalPut(final IRubyObject key, final IRubyObject value, final
}

protected void internalPutSmall(final IRubyObject key, final IRubyObject value, final boolean checkForExisting) {
final int hash = hashValue(key.hashCode());
final int hash = hashValue(key);
final int i = bucketIndex(hash, table.length);

// if (table[i] != null) collisions++;
@@ -547,7 +552,7 @@ protected IRubyObject internalGet(IRubyObject key) { // specialized for value
protected RubyHashEntry internalGetEntry(IRubyObject key) {
if (size == 0) return NO_ENTRY;

final int hash = hashValue(key.hashCode());
final int hash = hashValue(key);
for (RubyHashEntry entry = table[bucketIndex(hash, table.length)]; entry != null; entry = entry.next) {
if (internalKeyExist(entry, hash, key)) {
return entry;
@@ -567,12 +572,12 @@ private boolean internalKeyExist(RubyHashEntry entry, int hash, IRubyObject key)
protected RubyHashEntry internalDelete(final IRubyObject key) {
if (size == 0) return NO_ENTRY;

return internalDelete(hashValue(key.hashCode()), MATCH_KEY, key);
return internalDelete(hashValue(key), MATCH_KEY, key);
}

protected RubyHashEntry internalDeleteEntry(final RubyHashEntry entry) {
// n.b. we need to recompute the hash in case the key object was modified
return internalDelete(hashValue(entry.key.hashCode()), MATCH_ENTRY, entry);
return internalDelete(hashValue(entry.key), MATCH_ENTRY, entry);
}

private final RubyHashEntry internalDelete(final int hash, final EntryMatchType matchType, final Object obj) {
@@ -923,7 +928,7 @@ public RubyHash rehash() {
oldTable[j] = null;
while (entry != null) {
RubyHashEntry next = entry.next;
entry.hash = hashValue(entry.key.hashCode()); // update the hash value
entry.hash = hashValue(entry.key); // update the hash value
int i = bucketIndex(entry.hash, newTable.length);
entry.next = newTable[i];
newTable[i] = entry;
17 changes: 17 additions & 0 deletions test/jruby/test_hash.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test/unit'

class TestHash < Test::Unit::TestCase

def test_clone_copies_default_initializer
hash = Hash.new { |h, k| h[k] = 0 }
clone = hash.clone
@@ -88,7 +89,23 @@ def test_yield_arguments
passed = []; ret = hash.collect { |k,v| passed << k; passed << v; passed.size < 3 }
assert_equal [:a, 1, :b, 2, :c, 3], passed
assert_equal [true, false, false], ret
end

def test_compare_by_identity
hash = {}.compare_by_identity
arr = [0]
hash[arr] = 42
arr[0] = 1
assert_equal 42, hash[arr]

hash = {}.compare_by_identity
arr1 = []; arr2 = []
hash[arr2] = 2
assert_equal nil, hash[arr1]
assert_equal 2, hash[arr2]
hash[arr1] = 1
assert_equal 1, hash[arr1]
assert_equal 2, hash[arr2]
end

end

0 comments on commit 0285d99

Please sign in to comment.