diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-07 13:50:27 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-07 13:50:27 -0200 |
commit | a4472490bc4b2c5802b98d0b27b77a4353eff867 (patch) | |
tree | 31821bee2ffea76803d585ac9e1d232b489f1bf7 /ltablib.c | |
parent | 86312e1a7d128be944a29bea7421eff58efb83be (diff) | |
download | lua-a4472490bc4b2c5802b98d0b27b77a4353eff867.tar.gz lua-a4472490bc4b2c5802b98d0b27b77a4353eff867.tar.bz2 lua-a4472490bc4b2c5802b98d0b27b77a4353eff867.zip |
new 'table.pack' function
Diffstat (limited to 'ltablib.c')
-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}, |