diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-30 11:16:50 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-30 11:16:50 -0200 |
| commit | fe5c41fb8a0e1d4f130437b22667bf699851b17a (patch) | |
| tree | 61671399e70d42e12ace5fa06e3c66d77532693f | |
| parent | 9a455438417b45be12b763ca56005b32bf0bb017 (diff) | |
| download | lua-fe5c41fb8a0e1d4f130437b22667bf699851b17a.tar.gz lua-fe5c41fb8a0e1d4f130437b22667bf699851b17a.tar.bz2 lua-fe5c41fb8a0e1d4f130437b22667bf699851b17a.zip | |
new functions "tinsert" and "tremove"
| -rw-r--r-- | lbuiltin.c | 35 | ||||
| -rw-r--r-- | lvm.c | 24 | ||||
| -rw-r--r-- | lvm.h | 3 |
3 files changed, 49 insertions, 13 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 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.33 1998/12/24 14:57:23 roberto Exp $ | 2 | ** $Id: lvm.c,v 1.34 1998/12/27 20:25:20 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -93,6 +93,16 @@ int luaV_tostring (TObject *obj) { | |||
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | 95 | ||
| 96 | void luaV_setn (Hash *t, int val) { | ||
| 97 | TObject index, value; | ||
| 98 | ttype(&index) = LUA_T_STRING; | ||
| 99 | tsvalue(&index) = luaS_new("n"); | ||
| 100 | ttype(&value) = LUA_T_NUMBER; | ||
| 101 | nvalue(&value) = val; | ||
| 102 | *(luaH_set(t, &index)) = value; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 96 | void luaV_closure (int nelems) | 106 | void luaV_closure (int nelems) |
| 97 | { | 107 | { |
| 98 | if (nelems > 0) { | 108 | if (nelems > 0) { |
| @@ -275,8 +285,7 @@ void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal, | |||
| 275 | } | 285 | } |
| 276 | 286 | ||
| 277 | 287 | ||
| 278 | void luaV_pack (StkId firstel, int nvararg, TObject *tab) | 288 | void luaV_pack (StkId firstel, int nvararg, TObject *tab) { |
| 279 | { | ||
| 280 | TObject *firstelem = L->stack.stack+firstel; | 289 | TObject *firstelem = L->stack.stack+firstel; |
| 281 | int i; | 290 | int i; |
| 282 | Hash *htab; | 291 | Hash *htab; |
| @@ -285,14 +294,7 @@ void luaV_pack (StkId firstel, int nvararg, TObject *tab) | |||
| 285 | ttype(tab) = LUA_T_ARRAY; | 294 | ttype(tab) = LUA_T_ARRAY; |
| 286 | for (i=0; i<nvararg; i++) | 295 | for (i=0; i<nvararg; i++) |
| 287 | luaH_setint(htab, i+1, firstelem+i); | 296 | luaH_setint(htab, i+1, firstelem+i); |
| 288 | /* store counter in field "n" */ { | 297 | luaV_setn(htab, nvararg); /* store counter in field "n" */ |
| 289 | TObject index, extra; | ||
| 290 | ttype(&index) = LUA_T_STRING; | ||
| 291 | tsvalue(&index) = luaS_new("n"); | ||
| 292 | ttype(&extra) = LUA_T_NUMBER; | ||
| 293 | nvalue(&extra) = nvararg; | ||
| 294 | *(luaH_set(htab, &index)) = extra; | ||
| 295 | } | ||
| 296 | } | 298 | } |
| 297 | 299 | ||
| 298 | 300 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.h,v 1.4 1997/12/15 16:17:20 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.5 1998/07/12 16:16:43 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -20,6 +20,7 @@ | |||
| 20 | void luaV_pack (StkId firstel, int nvararg, TObject *tab); | 20 | void luaV_pack (StkId firstel, int nvararg, TObject *tab); |
| 21 | int luaV_tonumber (TObject *obj); | 21 | int luaV_tonumber (TObject *obj); |
| 22 | int luaV_tostring (TObject *obj); | 22 | int luaV_tostring (TObject *obj); |
| 23 | void luaV_setn (Hash *t, int val); | ||
| 23 | void luaV_gettable (void); | 24 | void luaV_gettable (void); |
| 24 | void luaV_settable (TObject *t, int mode); | 25 | void luaV_settable (TObject *t, int mode); |
| 25 | void luaV_getglobal (TaggedString *ts); | 26 | void luaV_getglobal (TaggedString *ts); |
