Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 06aa3f7de368
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 80efaf4d89b9
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 10, 2018

  1. Copy the full SHA
    c0741fa View commit details
  2. Fix Hash#rehash method for duplicate keys

    Duplicate keys where just ignored before and inserted again.
    Fix #4958.
    ChrisBr committed Jan 10, 2018
    Copy the full SHA
    2a7d600 View commit details
  3. Merge pull request #4961 from ChrisBr/bug/rehash

    Fix Hash#rehash method for duplicate keys
    headius authored Jan 10, 2018
    Copy the full SHA
    80efaf4 View commit details
Showing with 26 additions and 2 deletions.
  1. +11 −2 core/src/main/java/org/jruby/RubyHash.java
  2. +15 −0 spec/ruby/core/hash/rehash_spec.rb
13 changes: 11 additions & 2 deletions core/src/main/java/org/jruby/RubyHash.java
Original file line number Diff line number Diff line change
@@ -933,8 +933,17 @@ public RubyHash rehash() {
RubyHashEntry next = entry.next;
entry.hash = hashValue(entry.key); // update the hash value
int i = bucketIndex(entry.hash, newTable.length);
entry.next = newTable[i];
newTable[i] = entry;

if (newTable[i] != null && internalKeyExist(newTable[i], entry.hash, entry.key)) {
RubyHashEntry tmpNext = entry.nextAdded;
RubyHashEntry tmpPrev = entry.prevAdded;
tmpPrev.nextAdded = tmpNext;
tmpPrev.prevAdded = tmpPrev;
size--;
} else {
entry.next = newTable[i];
newTable[i] = entry;
}
entry = next;
}
}
15 changes: 15 additions & 0 deletions spec/ruby/core/hash/rehash_spec.rb
Original file line number Diff line number Diff line change
@@ -35,6 +35,21 @@
h[k2].should == v2
end

it "removes duplicate keys" do
a = [1,2]
b = [1]

h = {}
h[a] = true
h[b] = true
b << 2
h.length.should == 2
h.keys == [a, b]
h.rehash
h.length.should == 1
h.keys == [a]
end

it "raises a #{frozen_error_class} if called on a frozen instance" do
lambda { HashSpecs.frozen_hash.rehash }.should raise_error(frozen_error_class)
lambda { HashSpecs.empty_frozen_hash.rehash }.should raise_error(frozen_error_class)