diff --git a/codes/python/chapter_hashing/hash_map_open_addressing.py b/codes/python/chapter_hashing/hash_map_open_addressing.py index 7b3d2ba6e..88ffb84ce 100644 --- a/codes/python/chapter_hashing/hash_map_open_addressing.py +++ b/codes/python/chapter_hashing/hash_map_open_addressing.py @@ -35,19 +35,22 @@ class HashMapOpenAddressing: """搜索 key 对应的桶索引""" index = self.hash_func(key) first_tombstone = -1 - # 线性探测,当遇到空桶时跳出 - while self.buckets[index] is not None: + for _ in range(self.capacity): + bucket = self.buckets[index] + if bucket is None: + break + # 记录遇到的首个删除标记 + if bucket is self.TOMBSTONE: + if first_tombstone == -1: + first_tombstone = index # 若遇到 key ,返回对应的桶索引 - if self.buckets[index].key == key: + elif bucket.key == key: # 若之前遇到了删除标记,则将键值对移动至该索引处 if first_tombstone != -1: - self.buckets[first_tombstone] = self.buckets[index] + self.buckets[first_tombstone] = bucket self.buckets[index] = self.TOMBSTONE return first_tombstone # 返回移动后的桶索引 return index # 返回桶索引 - # 记录遇到的首个删除标记 - if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE: - first_tombstone = index # 计算桶索引,越过尾部则返回头部 index = (index + 1) % self.capacity # 若 key 不存在,则返回添加点的索引 diff --git a/en/codes/python/chapter_hashing/hash_map_open_addressing.py b/en/codes/python/chapter_hashing/hash_map_open_addressing.py index 50b8cf686..0227ccaba 100644 --- a/en/codes/python/chapter_hashing/hash_map_open_addressing.py +++ b/en/codes/python/chapter_hashing/hash_map_open_addressing.py @@ -35,19 +35,22 @@ class HashMapOpenAddressing: """Find the bucket index for key""" index = self.hash_func(key) first_tombstone = -1 - # Probe linearly until an empty bucket is reached - while self.buckets[index] is not None: + for _ in range(self.capacity): + bucket = self.buckets[index] + if bucket is None: + break + # Record the first tombstone encountered + if bucket is self.TOMBSTONE: + if first_tombstone == -1: + first_tombstone = index # If key is found, return its bucket index - if self.buckets[index].key == key: + elif bucket.key == key: # If a tombstone was encountered earlier, move the key-value pair to that index if first_tombstone != -1: - self.buckets[first_tombstone] = self.buckets[index] + self.buckets[first_tombstone] = bucket self.buckets[index] = self.TOMBSTONE return first_tombstone # Return the bucket index after the move return index # Return the bucket index - # Record the first tombstone encountered - if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE: - first_tombstone = index # Calculate the bucket index, wrapping to the beginning after the end index = (index + 1) % self.capacity # If key is absent, return the index for insertion diff --git a/ja/codes/python/chapter_hashing/hash_map_open_addressing.py b/ja/codes/python/chapter_hashing/hash_map_open_addressing.py index c2faa3344..658381068 100644 --- a/ja/codes/python/chapter_hashing/hash_map_open_addressing.py +++ b/ja/codes/python/chapter_hashing/hash_map_open_addressing.py @@ -35,19 +35,22 @@ class HashMapOpenAddressing: """key に対応するバケットのインデックスを探索する""" index = self.hash_func(key) first_tombstone = -1 - # 線形探索法で探索し、空のバケットに到達したら終了する - while self.buckets[index] is not None: + for _ in range(self.capacity): + bucket = self.buckets[index] + if bucket is None: + break + # 最初に見つかった削除マーカーを記録する + if bucket is self.TOMBSTONE: + if first_tombstone == -1: + first_tombstone = index # key が見つかった場合、対応するバケットのインデックスを返す - if self.buckets[index].key == key: + elif bucket.key == key: # それ以前に削除マーカーが見つかっていれば、そのインデックスにキーと値のペアを移す if first_tombstone != -1: - self.buckets[first_tombstone] = self.buckets[index] + self.buckets[first_tombstone] = bucket self.buckets[index] = self.TOMBSTONE return first_tombstone # 移動後のバケットのインデックスを返す return index # バケットのインデックスを返す - # 最初に見つかった削除マーカーを記録する - if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE: - first_tombstone = index # バケットのインデックスを計算し、末尾を越えたら先頭に戻る index = (index + 1) % self.capacity # key が存在しない場合、追加位置のインデックスを返す diff --git a/ko/codes/python/chapter_hashing/hash_map_open_addressing.py b/ko/codes/python/chapter_hashing/hash_map_open_addressing.py index 170bc9216..59b8140a6 100644 --- a/ko/codes/python/chapter_hashing/hash_map_open_addressing.py +++ b/ko/codes/python/chapter_hashing/hash_map_open_addressing.py @@ -35,19 +35,22 @@ class HashMapOpenAddressing: """key에 해당하는 버킷 인덱스 탐색""" index = self.hash_func(key) first_tombstone = -1 - # 선형 탐사로 탐색하고, 빈 버킷을 만나면 반복문을 종료한다 - while self.buckets[index] is not None: + for _ in range(self.capacity): + bucket = self.buckets[index] + if bucket is None: + break + # 처음 만난 삭제 표시를 기록한다 + if bucket is self.TOMBSTONE: + if first_tombstone == -1: + first_tombstone = index # key를 만나면 해당 버킷 인덱스를 반환한다 - if self.buckets[index].key == key: + elif bucket.key == key: # 이전에 삭제 표시를 만났으면 키-값 쌍을 그 인덱스로 옮긴다 if first_tombstone != -1: - self.buckets[first_tombstone] = self.buckets[index] + self.buckets[first_tombstone] = bucket self.buckets[index] = self.TOMBSTONE return first_tombstone # 옮긴 후의 버킷 인덱스를 반환한다 return index # 버킷 인덱스를 반환한다 - # 처음 만난 삭제 표시를 기록한다 - if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE: - first_tombstone = index # 버킷 인덱스를 계산하고, 끝을 넘어가면 처음으로 돌아온다 index = (index + 1) % self.capacity # key가 없으면 추가할 위치의 인덱스를 반환한다 diff --git a/ru/codes/python/chapter_hashing/hash_map_open_addressing.py b/ru/codes/python/chapter_hashing/hash_map_open_addressing.py index 1f9bf3da7..0876dcf46 100644 --- a/ru/codes/python/chapter_hashing/hash_map_open_addressing.py +++ b/ru/codes/python/chapter_hashing/hash_map_open_addressing.py @@ -35,19 +35,22 @@ class HashMapOpenAddressing: """Найти индекс корзины, соответствующий key""" index = self.hash_func(key) first_tombstone = -1 - # Выполнять линейное пробирование и завершить при встрече с пустой корзиной - while self.buckets[index] is not None: + for _ in range(self.capacity): + bucket = self.buckets[index] + if bucket is None: + break + # Записать первую встретившуюся метку удаления + if bucket is self.TOMBSTONE: + if first_tombstone == -1: + first_tombstone = index # Если встретился key, вернуть соответствующий индекс корзины - if self.buckets[index].key == key: + elif bucket.key == key: # Если ранее встретилась метка удаления, переместить пару ключ-значение на этот индекс if first_tombstone != -1: - self.buckets[first_tombstone] = self.buckets[index] + self.buckets[first_tombstone] = bucket self.buckets[index] = self.TOMBSTONE return first_tombstone # Вернуть индекс корзины после перемещения return index # Вернуть индекс корзины - # Записать первую встретившуюся метку удаления - if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE: - first_tombstone = index # Вычислить индекс корзины; при выходе за конец вернуться к началу index = (index + 1) % self.capacity # Если key не существует, вернуть индекс точки добавления diff --git a/zh-hant/codes/python/chapter_hashing/hash_map_open_addressing.py b/zh-hant/codes/python/chapter_hashing/hash_map_open_addressing.py index c1fa21316..d451a955e 100644 --- a/zh-hant/codes/python/chapter_hashing/hash_map_open_addressing.py +++ b/zh-hant/codes/python/chapter_hashing/hash_map_open_addressing.py @@ -35,19 +35,22 @@ class HashMapOpenAddressing: """搜尋 key 對應的桶索引""" index = self.hash_func(key) first_tombstone = -1 - # 線性探查,當遇到空桶時跳出 - while self.buckets[index] is not None: + for _ in range(self.capacity): + bucket = self.buckets[index] + if bucket is None: + break + # 記錄遇到的首個刪除標記 + if bucket is self.TOMBSTONE: + if first_tombstone == -1: + first_tombstone = index # 若遇到 key ,返回對應的桶索引 - if self.buckets[index].key == key: + elif bucket.key == key: # 若之前遇到了刪除標記,則將鍵值對移動至該索引處 if first_tombstone != -1: - self.buckets[first_tombstone] = self.buckets[index] + self.buckets[first_tombstone] = bucket self.buckets[index] = self.TOMBSTONE return first_tombstone # 返回移動後的桶索引 return index # 返回桶索引 - # 記錄遇到的首個刪除標記 - if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE: - first_tombstone = index # 計算桶索引,越過尾部則返回頭部 index = (index + 1) % self.capacity # 若 key 不存在,則返回新增點的索引