Skip to content

Commit 5fa71e0

Browse files
committed
update map
1 parent b2a3031 commit 5fa71e0

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

map.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,18 @@ again:
287287
// 这里的 bucketCnt 是全局常量
288288
// 其实叫 bucketElemCnt 更合适
289289
if b.tophash[i] != top {
290+
// 在 b.tophash[i] != top 的情况下
291+
// 理论上有可能会是一个空槽位
292+
// 一般情况下 map 的槽位分布是这样的,e 表示 empty:
293+
// [h1][h2][h3][h4][h5][e][e][e]
294+
// 但在执行过 delete 操作时,可能会变成这样:
295+
// [h1][h2][e][e][h5][e][e][e]
296+
// 所以如果再插入的话,会尽量往前面的位置插
297+
// [h1][h2][e][e][h5][e][e][e]
298+
// ^
299+
// ^
300+
// 这个位置
301+
// 所有在循环的时候还要顺便把前面的空位置先记下来
290302
if b.tophash[i] == empty && inserti == nil {
291303
// 如果这个槽位没有被占,说明可以往这里塞 key 和 value
292304
inserti = &b.tophash[i] // tophash 的插入位置

0 commit comments

Comments
 (0)