diff options
Diffstat (limited to 'lbuiltin.c')
-rw-r--r-- | lbuiltin.c | 35 |
1 files changed, 34 insertions, 1 deletions
@@ -387,6 +387,37 @@ static void luaB_foreachvar (void) { | |||
387 | } | 387 | } |
388 | 388 | ||
389 | 389 | ||
390 | static void luaB_tinsert (void) { | ||
391 | Hash *a = gethash(1); | ||
392 | lua_Object v = lua_getparam(3); | ||
393 | int n = (int)getnarg(a); | ||
394 | int pos; | ||
395 | if (v != LUA_NOOBJECT) | ||
396 | pos = luaL_check_int(2); | ||
397 | else { /* called with only 2 arguments */ | ||
398 | v = luaL_nonnullarg(2); | ||
399 | pos = n+1; | ||
400 | } | ||
401 | luaV_setn(a, n+1); /* increment field "n" */ | ||
402 | for ( ;n>=pos; n--) | ||
403 | tablemove(a, n, n+1); | ||
404 | luaH_setint(a, pos, luaA_Address(v)); | ||
405 | } | ||
406 | |||
407 | |||
408 | static void luaB_tremove (void) { | ||
409 | Hash *a = gethash(1); | ||
410 | int n = (int)getnarg(a); | ||
411 | int pos = luaL_opt_int(2, n); | ||
412 | TObject v = *luaH_getint(a, pos); | ||
413 | if (n <= 0) return; /* table is "empty" */ | ||
414 | luaV_setn(a, n-1); /* decrement field "n" */ | ||
415 | for ( ;pos<n; pos++) | ||
416 | tablemove(a, pos+1, pos); | ||
417 | luaA_pushobject(&v); | ||
418 | } | ||
419 | |||
420 | |||
390 | /* | 421 | /* |
391 | ** Quicksort algorithm from "Programming Pearls", pg. 112 | 422 | ** Quicksort algorithm from "Programming Pearls", pg. 112 |
392 | */ | 423 | */ |
@@ -666,7 +697,9 @@ static struct luaL_reg builtin_funcs[] = { | |||
666 | {"foreach", luaB_foreach}, | 697 | {"foreach", luaB_foreach}, |
667 | {"foreachi", luaB_foreachi}, | 698 | {"foreachi", luaB_foreachi}, |
668 | {"foreachvar", luaB_foreachvar}, | 699 | {"foreachvar", luaB_foreachvar}, |
669 | {"sort", luaB_sort} | 700 | {"sort", luaB_sort}, |
701 | {"tinsert", luaB_tinsert}, | ||
702 | {"tremove", luaB_tremove} | ||
670 | }; | 703 | }; |
671 | 704 | ||
672 | 705 | ||