diff options
| -rw-r--r-- | ltablib.c | 26 |
1 files changed, 25 insertions, 1 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.48 2009/11/24 12:05:44 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.49 2009/11/26 17:35:13 roberto Exp roberto $ |
| 3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -167,12 +167,35 @@ static int tconcat (lua_State *L) { | |||
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | 169 | ||
| 170 | /* | ||
| 171 | ** {====================================================== | ||
| 172 | ** Pack | ||
| 173 | ** ======================================================= | ||
| 174 | */ | ||
| 175 | |||
| 176 | static int pack (lua_State *L) { | ||
| 177 | int top = lua_gettop(L); | ||
| 178 | lua_createtable(L, top, 1); /* create result table */ | ||
| 179 | /* use function environment as a temporary place to keep new table */ | ||
| 180 | lua_replace(L, LUA_ENVIRONINDEX); | ||
| 181 | lua_pushinteger(L, top); /* number of elements */ | ||
| 182 | lua_setfield(L, LUA_ENVIRONINDEX, "n"); /* t.n = number of elements */ | ||
| 183 | for (; top >= 1; top--) /* assign elements */ | ||
| 184 | lua_rawseti(L, LUA_ENVIRONINDEX, top); | ||
| 185 | lua_pushvalue(L, LUA_ENVIRONINDEX); /* return new table */ | ||
| 186 | return 1; | ||
| 187 | } | ||
| 188 | |||
| 189 | /* }====================================================== */ | ||
| 190 | |||
| 191 | |||
| 170 | 192 | ||
| 171 | /* | 193 | /* |
| 172 | ** {====================================================== | 194 | ** {====================================================== |
| 173 | ** Quicksort | 195 | ** Quicksort |
| 174 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; | 196 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; |
| 175 | ** Addison-Wesley, 1993.) | 197 | ** Addison-Wesley, 1993.) |
| 198 | ** ======================================================= | ||
| 176 | */ | 199 | */ |
| 177 | 200 | ||
| 178 | 201 | ||
| @@ -279,6 +302,7 @@ static const luaL_Reg tab_funcs[] = { | |||
| 279 | {"getn", getn}, | 302 | {"getn", getn}, |
| 280 | {"maxn", maxn}, | 303 | {"maxn", maxn}, |
| 281 | {"insert", tinsert}, | 304 | {"insert", tinsert}, |
| 305 | {"pack", pack}, | ||
| 282 | {"remove", tremove}, | 306 | {"remove", tremove}, |
| 283 | {"setn", setn}, | 307 | {"setn", setn}, |
| 284 | {"sort", sort}, | 308 | {"sort", sort}, |
