** Try to find a border in table 't'. (A 'border' is an integer index ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent, ** or 'maxinteger' if t[maxinteger] is present.) ** If there is an array part, try to find a border there. First try ** to find it in the vicinity of the previous result (hint), to handle ** cases like 't[#t + 1] = val' or 't[#t] = nil', that move the bor
| 1299 | ** border may be in the hash part. |
| 1300 | */ |
| 1301 | lua_Unsigned luaH_getn (lua_State *L, Table *t) { |
| 1302 | unsigned asize = t->asize; |
| 1303 | if (asize > 0) { /* is there an array part? */ |
| 1304 | const unsigned maxvicinity = 4; |
| 1305 | unsigned limit = *lenhint(t); /* start with the hint */ |
| 1306 | if (limit == 0) |
| 1307 | limit = 1; /* make limit a valid index in the array */ |
| 1308 | if (arraykeyisempty(t, limit)) { /* t[limit] empty? */ |
| 1309 | /* there must be a border before 'limit' */ |
| 1310 | unsigned i; |
| 1311 | /* look for a border in the vicinity of the hint */ |
| 1312 | for (i = 0; i < maxvicinity && limit > 1; i++) { |
| 1313 | limit--; |
| 1314 | if (!arraykeyisempty(t, limit)) |
| 1315 | return newhint(t, limit); /* 'limit' is a border */ |
| 1316 | } |
| 1317 | /* t[limit] still empty; search for a border in [0, limit) */ |
| 1318 | return newhint(t, binsearch(t, 0, limit)); |
| 1319 | } |
| 1320 | else { /* 'limit' is present in table; look for a border after it */ |
| 1321 | unsigned i; |
| 1322 | /* look for a border in the vicinity of the hint */ |
| 1323 | for (i = 0; i < maxvicinity && limit < asize; i++) { |
| 1324 | limit++; |
| 1325 | if (arraykeyisempty(t, limit)) |
| 1326 | return newhint(t, limit - 1); /* 'limit - 1' is a border */ |
| 1327 | } |
| 1328 | if (arraykeyisempty(t, asize)) { /* last element empty? */ |
| 1329 | /* t[limit] not empty; search for a border in [limit, asize) */ |
| 1330 | return newhint(t, binsearch(t, limit, asize)); |
| 1331 | } |
| 1332 | } |
| 1333 | /* last element non empty; set a hint to speed up finding that again */ |
| 1334 | /* (keys in the hash part cannot be hints) */ |
| 1335 | *lenhint(t) = asize; |
| 1336 | } |
| 1337 | /* no array part or t[asize] is not empty; check the hash part */ |
| 1338 | lua_assert(asize == 0 || !arraykeyisempty(t, asize)); |
| 1339 | if (isdummy(t) || hashkeyisempty(t, asize + 1)) |
| 1340 | return asize; /* 'asize + 1' is empty */ |
| 1341 | else /* 'asize + 1' is also non empty */ |
| 1342 | return hash_search(L, t, asize); |
| 1343 | } |
| 1344 | |
| 1345 | |
| 1346 |
no test coverage detected