Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hash#rehash to reset @first #14606

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions spec/std/hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1393,16 +1393,25 @@ describe "Hash" do
hash.@indices_size_pow2.should eq(12)
end

it "rehashes" do
a = [1]
h = {a => 0}
(10..100).each do |i|
h[[i]] = i
end
a << 2
h[a]?.should be_nil
h.rehash
h[a].should eq(0)
describe "#rehash" do
it "rehashes" do
a = [1]
h = {a => 0}
(10..100).each do |i|
h[[i]] = i
end
a << 2
h[a]?.should be_nil
h.rehash
h[a].should eq(0)
end

it "resets @first (#14602)" do
h = {"a" => 1, "b" => 2}
h.delete("a")
h.rehash
h["b"].should eq 2
ysbaddaden marked this conversation as resolved.
Show resolved Hide resolved
end
end

describe "some edge cases while changing the implementation to open addressing" do
Expand Down
3 changes: 3 additions & 0 deletions src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@ class Hash(K, V)
new_entry_index += 1
end

# Reset offset to first non-deleted entry
@first = 0

# We have to mark entries starting from the final new index
# as deleted so the GC can collect them.
entries_to_clear = entries_size - new_entry_index
Expand Down