commit fe5c41fb8a0e1d4f130437b22667bf699851b17a
parent 9a455438417b45be12b763ca56005b32bf0bb017
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 30 Dec 1998 11:16:29 -0200
new functions "tinsert" and "tremove"
Diffstat:
M | lbuiltin.c | | | 35 | ++++++++++++++++++++++++++++++++++- |
M | lvm.c | | | 24 | +++++++++++++----------- |
M | lvm.h | | | 3 | ++- |
3 files changed, 49 insertions(+), 13 deletions(-)
diff --git a/lbuiltin.c b/lbuiltin.c
@@ -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}
};
diff --git a/lvm.c b/lvm.c
@@ -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" */
}
diff --git a/lvm.h b/lvm.h
@@ -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);