| 345 | |
| 346 | |
| 347 | void luaH_resize (lua_State *L, Table *t, unsigned int nasize, |
| 348 | unsigned int nhsize) { |
| 349 | unsigned int i; |
| 350 | int j; |
| 351 | AuxsetnodeT asn; |
| 352 | unsigned int oldasize = t->sizearray; |
| 353 | int oldhsize = allocsizenode(t); |
| 354 | Node *nold = t->node; /* save old hash ... */ |
| 355 | if (nasize > oldasize) /* array part must grow? */ |
| 356 | setarrayvector(L, t, nasize); |
| 357 | /* create new hash part with appropriate size */ |
| 358 | asn.t = t; asn.nhsize = nhsize; |
| 359 | if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */ |
| 360 | setarrayvector(L, t, oldasize); /* array back to its original size */ |
| 361 | luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */ |
| 362 | } |
| 363 | if (nasize < oldasize) { /* array part must shrink? */ |
| 364 | t->sizearray = nasize; |
| 365 | /* re-insert elements from vanishing slice */ |
| 366 | for (i=nasize; i<oldasize; i++) { |
| 367 | if (!ttisnil(&t->array[i])) |
| 368 | luaH_setint(L, t, i + 1, &t->array[i]); |
| 369 | } |
| 370 | /* shrink array */ |
| 371 | luaM_reallocvector(L, t->array, oldasize, nasize, TValue); |
| 372 | } |
| 373 | /* re-insert elements from hash part */ |
| 374 | for (j = oldhsize - 1; j >= 0; j--) { |
| 375 | Node *old = nold + j; |
| 376 | if (!ttisnil(gval(old))) { |
| 377 | /* doesn't need barrier/invalidate cache, as entry was |
| 378 | already present in the table */ |
| 379 | setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); |
| 380 | } |
| 381 | } |
| 382 | if (oldhsize > 0) /* not the dummy node? */ |
| 383 | luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */ |
| 384 | } |
| 385 | |
| 386 | |
| 387 | void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { |
no test coverage detected