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

[WIP] Avoid infinite loop when hash-table has no empty members #405

Closed
wants to merge 1 commit into from

Conversation

Affonso-Gui
Copy link
Member

Problem:
Infinite loops when hash-table has no empty members

Code to reproduce:

(defvar ht (make-hash-table))
(dotimes (i (send ht :size))
  (send ht :enter i "foo")
  (send ht :delete i))
(send ht :enter 0 "foo")
;; infinite loop

Why it happens:

  • On the first loop the hash-table gets full with del symbols
  • The :find method loops until it finds an empty member, which in this case does not exist

Solution:
After all elements are iterated in the :find method, return the first available slot (del symbol). If there are no available slots, raise an error reporting that the hash-table is full.

Thanks to @ikeshou for finding the bug!

@Affonso-Gui
Copy link
Member Author

Affonso-Gui commented Dec 11, 2019

Debating with @knorth55 about this problem, we realized that another possible solution is to have two different :find methods, one for setting and one for getting.

The getter :find needs to loop through the list until an empty element is found because we have open addressing. However, for the setter :find it suffices to return the first available slot (empty or del) as soon as it is found.

EDIT: we do need to stop looping over the same elements repeatedly anyways

@Affonso-Gui
Copy link
Member Author

Talking to @ikeshou, having to loop once before detecting that the hash is full with deleted elements is not a good approach, and the correct would be to rehash the table based on the non-empty elements (filled + deleted) instead.

Will try to implement that and rebase later (changing to WIP for now)

@Affonso-Gui Affonso-Gui changed the title Avoid infinite loop when hash-table has no empty members [WIP] Avoid infinite loop when hash-table has no empty members Dec 16, 2019
@k-okada k-okada added the bug label Feb 14, 2020
Affonso-Gui added a commit to Affonso-Gui/EusLisp that referenced this pull request May 5, 2022
@Affonso-Gui
Copy link
Member Author

Redirecting to #492

@Affonso-Gui Affonso-Gui closed this May 5, 2022
Affonso-Gui added a commit to Affonso-Gui/EusLisp that referenced this pull request May 5, 2022
Affonso-Gui added a commit to Affonso-Gui/EusLisp that referenced this pull request Mar 27, 2023
@Affonso-Gui Affonso-Gui deleted the fix-hashing branch April 18, 2024 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants