From 9cb2a6d2e4686b0046b74374f4b89fddd6f97f83 Mon Sep 17 00:00:00 2001 From: Caroline113 <1412585094@qq.com> Date: Sun, 16 Jun 2024 23:35:38 +0800 Subject: [PATCH] Change _find_slot_for_insert() --- "docs/07_\345\223\210\345\270\214\350\241\250/hashtable.py" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/docs/07_\345\223\210\345\270\214\350\241\250/hashtable.py" "b/docs/07_\345\223\210\345\270\214\350\241\250/hashtable.py" index f1df5e2..f66fdf8 100644 --- "a/docs/07_\345\223\210\345\270\214\350\241\250/hashtable.py" +++ "b/docs/07_\345\223\210\345\270\214\350\241\250/hashtable.py" @@ -87,8 +87,13 @@ def _find_key(self, key): def _find_slot_for_insert(self, key): index = self._hash(key) _len = len(self._table) + attempts = 0 while not self._slot_can_insert(index): # 直到找到一个可以用的槽 + if attempts > _len: # Rehash if we've attempted more than the table size + self._rehash() # 改变_len + return self._find_slot_for_insert(key) index = (index * 5 + 1) % _len + attempts += 1 return index def _slot_can_insert(self, index):