** Resize table 't' for the new given sizes. Both allocations (for ** the hash part and for the array part) can fail, which creates some ** subtleties. If the first allocation, for the hash part, fails, an ** error is raised and that is it. Otherwise, it copies the elements from ** the shrinking part of the array (if it is shrinking) into the new ** hash. Then it reallocates the array part. If th
| 507 | ** parts of the table. |
| 508 | */ |
| 509 | void luaH_resize (lua_State *L, Table *t, unsigned int newasize, |
| 510 | unsigned int nhsize) { |
| 511 | unsigned int i; |
| 512 | Table newt; /* to keep the new hash part */ |
| 513 | unsigned int oldasize = setlimittosize(t); |
| 514 | TValue *newarray; |
| 515 | /* create new hash part with appropriate size into 'newt' */ |
| 516 | setnodevector(L, &newt, nhsize); |
| 517 | if (newasize < oldasize) { /* will array shrink? */ |
| 518 | t->alimit = newasize; /* pretend array has new size... */ |
| 519 | exchangehashpart(t, &newt); /* and new hash */ |
| 520 | /* re-insert into the new hash the elements from vanishing slice */ |
| 521 | for (i = newasize; i < oldasize; i++) { |
| 522 | if (!isempty(&t->array[i])) |
| 523 | luaH_setint(L, t, i + 1, &t->array[i]); |
| 524 | } |
| 525 | t->alimit = oldasize; /* restore current size... */ |
| 526 | exchangehashpart(t, &newt); /* and hash (in case of errors) */ |
| 527 | } |
| 528 | /* allocate new array */ |
| 529 | newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); |
| 530 | if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ |
| 531 | freehash(L, &newt); /* release new hash part */ |
| 532 | luaM_error(L); /* raise error (with array unchanged) */ |
| 533 | } |
| 534 | /* allocation ok; initialize new part of the array */ |
| 535 | exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ |
| 536 | t->array = newarray; /* set new array part */ |
| 537 | t->alimit = newasize; |
| 538 | for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ |
| 539 | setempty(&t->array[i]); |
| 540 | /* re-insert elements from old hash part into new parts */ |
| 541 | reinsert(L, &newt, t); /* 'newt' now has the old hash */ |
| 542 | freehash(L, &newt); /* free old hash part */ |
| 543 | } |
| 544 | |
| 545 | |
| 546 | void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { |
no test coverage detected