diff options
| author | chai <chaifix@163.com> | 2020-09-10 20:55:10 +0800 |
|---|---|---|
| committer | chai <chaifix@163.com> | 2020-09-10 20:55:10 +0800 |
| commit | d9c2d065698641837bc75ded236b23d5aaa36fd4 (patch) | |
| tree | 9acb1fe3f61121bc5963b9b76f22265d8cd00f0e /src/lua51/ltable.c | |
| parent | 229a3937a3b99a175b551e28d09b9a45d37c44f7 (diff) | |
*table string comment
Diffstat (limited to 'src/lua51/ltable.c')
| -rw-r--r-- | src/lua51/ltable.c | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/lua51/ltable.c b/src/lua51/ltable.c index 73464a0..38f4218 100644 --- a/src/lua51/ltable.c +++ b/src/lua51/ltable.c @@ -92,7 +92,7 @@ static Node *hashnum (const Table *t, lua_Number n) { } - +//c 计算hash不同key类型的hash值 /* ** returns the `main' position of an element in a table (that is, the index ** of its hash value) @@ -404,7 +404,7 @@ static Node *getfreepos (Table *t) { } -//c 表新建key +//c 表新建key,规则如下↓ /* ** inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main @@ -416,6 +416,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { Node *mp = mainposition(t, key); if (!ttisnil(gval(mp)) || mp == dummynode) {//c mainposition上已经有数据 Node *othern; + //c 取一个空桶,后面可能会用到 Node *n = getfreepos(t); /* get a free place */ //c 如果没有空位,扩展hash table大小为2倍 if (n == NULL) { /* cannot find a free place? */ @@ -425,8 +426,9 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { } lua_assert(n != dummynode); //c 先看一下现在mainposition上的这个node,它的mainposition是不是这个值 - //c 不是的话给新的key让路 - othern = mainposition(t, key2tval(mp)); + //c 不是的话说明这个node是临时放在这里的,给新的key让路,把这个node移到一个空位上 + //c othern是原先mp位置的这个key应该对应的位置 + othern = mainposition(t, key2tval(mp)); //c key2tval(mp)那大mp的key地址,用来计算hash if (othern != mp) { /* is colliding node out of its main position? */ // 把mp空出来,mp里的值移到n(freeposition) /* yes; move colliding node into free position */ @@ -434,8 +436,9 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ + gnext(mp) = NULL; /* now `mp' is free */ - setnilvalue(gval(mp)); + setnilvalue(gval(mp)); // 清空这个位置,留个要加的这个key用 //mp是要赋值的位置,即新key的元素的位置 } else { /* colliding node is in its own main position */ @@ -443,7 +446,8 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { //c 将free position插入到mp后第一个 gnext(n) = gnext(mp); /* chain new position */ gnext(mp) = n; - //c 修改一下mp指针,指向freeposition,留个后续使用 + + //c 修改一下mp指针,指向freeposition,留给后续使用。一个hack,这样就可以和mainposition位置上是空的兼容 mp = n; } } |
