new functions "tinsert" and "tremove"

This commit is contained in:
Roberto Ierusalimschy 1998-12-30 11:16:50 -02:00
parent 9a45543841
commit fe5c41fb8a
3 changed files with 49 additions and 13 deletions

View File

@ -387,6 +387,37 @@ static void luaB_foreachvar (void) {
}
static void luaB_tinsert (void) {
Hash *a = gethash(1);
lua_Object v = lua_getparam(3);
int n = (int)getnarg(a);
int pos;
if (v != LUA_NOOBJECT)
pos = luaL_check_int(2);
else { /* called with only 2 arguments */
v = luaL_nonnullarg(2);
pos = n+1;
}
luaV_setn(a, n+1); /* increment field "n" */
for ( ;n>=pos; n--)
tablemove(a, n, n+1);
luaH_setint(a, pos, luaA_Address(v));
}
static void luaB_tremove (void) {
Hash *a = gethash(1);
int n = (int)getnarg(a);
int pos = luaL_opt_int(2, n);
TObject v = *luaH_getint(a, pos);
if (n <= 0) return; /* table is "empty" */
luaV_setn(a, n-1); /* decrement field "n" */
for ( ;pos<n; pos++)
tablemove(a, pos+1, pos);
luaA_pushobject(&v);
}
/*
** Quicksort algorithm from "Programming Pearls", pg. 112
*/
@ -666,7 +697,9 @@ static struct luaL_reg builtin_funcs[] = {
{"foreach", luaB_foreach},
{"foreachi", luaB_foreachi},
{"foreachvar", luaB_foreachvar},
{"sort", luaB_sort}
{"sort", luaB_sort},
{"tinsert", luaB_tinsert},
{"tremove", luaB_tremove}
};

24
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 1.33 1998/12/24 14:57:23 roberto Exp $
** $Id: lvm.c,v 1.34 1998/12/27 20:25:20 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -93,6 +93,16 @@ int luaV_tostring (TObject *obj) {
}
void luaV_setn (Hash *t, int val) {
TObject index, value;
ttype(&index) = LUA_T_STRING;
tsvalue(&index) = luaS_new("n");
ttype(&value) = LUA_T_NUMBER;
nvalue(&value) = val;
*(luaH_set(t, &index)) = value;
}
void luaV_closure (int nelems)
{
if (nelems > 0) {
@ -275,8 +285,7 @@ void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal,
}
void luaV_pack (StkId firstel, int nvararg, TObject *tab)
{
void luaV_pack (StkId firstel, int nvararg, TObject *tab) {
TObject *firstelem = L->stack.stack+firstel;
int i;
Hash *htab;
@ -285,14 +294,7 @@ void luaV_pack (StkId firstel, int nvararg, TObject *tab)
ttype(tab) = LUA_T_ARRAY;
for (i=0; i<nvararg; i++)
luaH_setint(htab, i+1, firstelem+i);
/* store counter in field "n" */ {
TObject index, extra;
ttype(&index) = LUA_T_STRING;
tsvalue(&index) = luaS_new("n");
ttype(&extra) = LUA_T_NUMBER;
nvalue(&extra) = nvararg;
*(luaH_set(htab, &index)) = extra;
}
luaV_setn(htab, nvararg); /* store counter in field "n" */
}

3
lvm.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.h,v 1.4 1997/12/15 16:17:20 roberto Exp roberto $
** $Id: lvm.h,v 1.5 1998/07/12 16:16:43 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -20,6 +20,7 @@
void luaV_pack (StkId firstel, int nvararg, TObject *tab);
int luaV_tonumber (TObject *obj);
int luaV_tostring (TObject *obj);
void luaV_setn (Hash *t, int val);
void luaV_gettable (void);
void luaV_settable (TObject *t, int mode);
void luaV_getglobal (TaggedString *ts);