Bound linear probing in the Dart hash table (#2026)

Co-authored-by: James V <118329763+Dio77799@users.noreply.github.com>
This commit is contained in:
James V
2026-11-23 11:26:33 -06:00
committed by GitHub
parent 2a1a78f67a
commit 53ca4f76a1
6 changed files with 48 additions and 54 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}