diff --git a/codes/dart/chapter_hashing/hash_map_open_addressing.dart b/codes/dart/chapter_hashing/hash_map_open_addressing.dart index 618031234..c0045c068 100644 --- a/codes/dart/chapter_hashing/hash_map_open_addressing.dart +++ b/codes/dart/chapter_hashing/hash_map_open_addressing.dart @@ -35,22 +35,21 @@ class HashMapOpenAddressing { int findBucket(int key) { int index = hashFunc(key); int firstTombstone = -1; - // 线性探测,当遇到空桶时跳出 - while (_buckets[index] != null) { - // 若遇到 key ,返回对应的桶索引 - if (_buckets[index]!.key == key) { + for (int i = 0; i < _capacity; i++) { + final bucket = _buckets[index]; + if (bucket == null) break; + // 记录遇到的首个删除标记 + if (identical(bucket, _TOMBSTONE)) { + if (firstTombstone == -1) firstTombstone = index; + } else if (bucket.key == key) { // 若之前遇到了删除标记,则将键值对移动至该索引处 if (firstTombstone != -1) { - _buckets[firstTombstone] = _buckets[index]; + _buckets[firstTombstone] = bucket; _buckets[index] = _TOMBSTONE; return firstTombstone; // 返回移动后的桶索引 } return index; // 返回桶索引 } - // 记录遇到的首个删除标记 - if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) { - firstTombstone = index; - } // 计算桶索引,越过尾部则返回头部 index = (index + 1) % _capacity; } diff --git a/en/codes/dart/chapter_hashing/hash_map_open_addressing.dart b/en/codes/dart/chapter_hashing/hash_map_open_addressing.dart index d66ab50d7..b39d63273 100644 --- a/en/codes/dart/chapter_hashing/hash_map_open_addressing.dart +++ b/en/codes/dart/chapter_hashing/hash_map_open_addressing.dart @@ -35,22 +35,21 @@ class HashMapOpenAddressing { int findBucket(int key) { int index = hashFunc(key); int firstTombstone = -1; - // Probe linearly until an empty bucket is reached - while (_buckets[index] != null) { - // If key is found, return its bucket index - if (_buckets[index]!.key == key) { + for (int i = 0; i < _capacity; i++) { + final bucket = _buckets[index]; + if (bucket == null) break; + // Record the first tombstone encountered + if (identical(bucket, _TOMBSTONE)) { + if (firstTombstone == -1) firstTombstone = index; + } else if (bucket.key == key) { // If a tombstone was encountered earlier, move the key-value pair to that index if (firstTombstone != -1) { - _buckets[firstTombstone] = _buckets[index]; + _buckets[firstTombstone] = bucket; _buckets[index] = _TOMBSTONE; return firstTombstone; // Return the bucket index after the move } return index; // Return the bucket index } - // Record the first tombstone encountered - if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) { - firstTombstone = index; - } // Calculate the bucket index, wrapping to the beginning after the end index = (index + 1) % _capacity; } diff --git a/ja/codes/dart/chapter_hashing/hash_map_open_addressing.dart b/ja/codes/dart/chapter_hashing/hash_map_open_addressing.dart index 515cb305e..c40aa8966 100644 --- a/ja/codes/dart/chapter_hashing/hash_map_open_addressing.dart +++ b/ja/codes/dart/chapter_hashing/hash_map_open_addressing.dart @@ -35,22 +35,21 @@ class HashMapOpenAddressing { int findBucket(int key) { int index = hashFunc(key); int firstTombstone = -1; - // 線形探索法で探索し、空のバケットに到達したら終了する - while (_buckets[index] != null) { - // key が見つかった場合、対応するバケットのインデックスを返す - if (_buckets[index]!.key == key) { + for (int i = 0; i < _capacity; i++) { + final bucket = _buckets[index]; + if (bucket == null) break; + // 最初に見つかった削除マーカーを記録する + if (identical(bucket, _TOMBSTONE)) { + if (firstTombstone == -1) firstTombstone = index; + } else if (bucket.key == key) { // それ以前に削除マーカーが見つかっていれば、そのインデックスにキーと値のペアを移す if (firstTombstone != -1) { - _buckets[firstTombstone] = _buckets[index]; + _buckets[firstTombstone] = bucket; _buckets[index] = _TOMBSTONE; return firstTombstone; // 移動後のバケットのインデックスを返す } return index; // バケットのインデックスを返す } - // 最初に見つかった削除マーカーを記録する - if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) { - firstTombstone = index; - } // バケットのインデックスを計算し、末尾を越えたら先頭に戻る index = (index + 1) % _capacity; } diff --git a/ko/codes/dart/chapter_hashing/hash_map_open_addressing.dart b/ko/codes/dart/chapter_hashing/hash_map_open_addressing.dart index 9756cb98d..1fb34b387 100644 --- a/ko/codes/dart/chapter_hashing/hash_map_open_addressing.dart +++ b/ko/codes/dart/chapter_hashing/hash_map_open_addressing.dart @@ -35,22 +35,21 @@ class HashMapOpenAddressing { int findBucket(int key) { int index = hashFunc(key); int firstTombstone = -1; - // 선형 탐사로 탐색하고, 빈 버킷을 만나면 반복문을 종료한다 - while (_buckets[index] != null) { - // key를 만나면 해당 버킷 인덱스를 반환한다 - if (_buckets[index]!.key == key) { + for (int i = 0; i < _capacity; i++) { + final bucket = _buckets[index]; + if (bucket == null) break; + // 처음 만난 삭제 표시를 기록한다 + if (identical(bucket, _TOMBSTONE)) { + if (firstTombstone == -1) firstTombstone = index; + } else if (bucket.key == key) { // 이전에 삭제 표시를 만났으면 키-값 쌍을 그 인덱스로 옮긴다 if (firstTombstone != -1) { - _buckets[firstTombstone] = _buckets[index]; + _buckets[firstTombstone] = bucket; _buckets[index] = _TOMBSTONE; return firstTombstone; // 옮긴 후의 버킷 인덱스를 반환한다 } return index; // 버킷 인덱스를 반환한다 } - // 처음 만난 삭제 표시를 기록한다 - if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) { - firstTombstone = index; - } // 버킷 인덱스를 계산하고, 끝을 넘어가면 처음으로 돌아온다 index = (index + 1) % _capacity; } diff --git a/ru/codes/dart/chapter_hashing/hash_map_open_addressing.dart b/ru/codes/dart/chapter_hashing/hash_map_open_addressing.dart index e0e97a131..63b84bb81 100644 --- a/ru/codes/dart/chapter_hashing/hash_map_open_addressing.dart +++ b/ru/codes/dart/chapter_hashing/hash_map_open_addressing.dart @@ -35,22 +35,21 @@ class HashMapOpenAddressing { int findBucket(int key) { int index = hashFunc(key); int firstTombstone = -1; - // Выполнять линейное пробирование и завершить при встрече с пустой корзиной - while (_buckets[index] != null) { - // Если встретился key, вернуть соответствующий индекс корзины - if (_buckets[index]!.key == key) { + for (int i = 0; i < _capacity; i++) { + final bucket = _buckets[index]; + if (bucket == null) break; + // Записать первую встретившуюся метку удаления + if (identical(bucket, _TOMBSTONE)) { + if (firstTombstone == -1) firstTombstone = index; + } else if (bucket.key == key) { // Если ранее встретилась метка удаления, переместить пару ключ-значение на этот индекс if (firstTombstone != -1) { - _buckets[firstTombstone] = _buckets[index]; + _buckets[firstTombstone] = bucket; _buckets[index] = _TOMBSTONE; return firstTombstone; // Вернуть индекс корзины после перемещения } return index; // Вернуть индекс корзины } - // Записать первую встретившуюся метку удаления - if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) { - firstTombstone = index; - } // Вычислить индекс корзины; при выходе за конец вернуться к началу index = (index + 1) % _capacity; } diff --git a/zh-hant/codes/dart/chapter_hashing/hash_map_open_addressing.dart b/zh-hant/codes/dart/chapter_hashing/hash_map_open_addressing.dart index 24e2ed151..d8640d8fa 100644 --- a/zh-hant/codes/dart/chapter_hashing/hash_map_open_addressing.dart +++ b/zh-hant/codes/dart/chapter_hashing/hash_map_open_addressing.dart @@ -35,22 +35,21 @@ class HashMapOpenAddressing { int findBucket(int key) { int index = hashFunc(key); int firstTombstone = -1; - // 線性探查,當遇到空桶時跳出 - while (_buckets[index] != null) { - // 若遇到 key ,返回對應的桶索引 - if (_buckets[index]!.key == key) { + for (int i = 0; i < _capacity; i++) { + final bucket = _buckets[index]; + if (bucket == null) break; + // 記錄遇到的首個刪除標記 + if (identical(bucket, _TOMBSTONE)) { + if (firstTombstone == -1) firstTombstone = index; + } else if (bucket.key == key) { // 若之前遇到了刪除標記,則將鍵值對移動至該索引處 if (firstTombstone != -1) { - _buckets[firstTombstone] = _buckets[index]; + _buckets[firstTombstone] = bucket; _buckets[index] = _TOMBSTONE; return firstTombstone; // 返回移動後的桶索引 } return index; // 返回桶索引 } - // 記錄遇到的首個刪除標記 - if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) { - firstTombstone = index; - } // 計算桶索引,越過尾部則返回頭部 index = (index + 1) % _capacity; }