Lines Matching refs:L
19 #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) argument
24 static int maxn (lua_State *L) { in maxn() argument
26 luaL_checktype(L, 1, LUA_TTABLE); in maxn()
27 lua_pushnil(L); /* first key */ in maxn()
28 while (lua_next(L, 1)) { in maxn()
29 lua_pop(L, 1); /* remove value */ in maxn()
30 if (lua_type(L, -1) == LUA_TNUMBER) { in maxn()
31 lua_Number v = lua_tonumber(L, -1); in maxn()
35 lua_pushnumber(L, max); in maxn()
41 static int tinsert (lua_State *L) { in tinsert() argument
42 int e = aux_getn(L, 1) + 1; /* first empty element */ in tinsert()
44 switch (lua_gettop(L)) { in tinsert()
51 pos = luaL_checkint(L, 2); /* 2nd argument is the position */ in tinsert()
52 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); in tinsert()
54 lua_rawgeti(L, 1, i-1); in tinsert()
55 lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ in tinsert()
60 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); in tinsert()
63 lua_rawseti(L, 1, pos); /* t[pos] = v */ in tinsert()
68 static int tremove (lua_State *L) { in tremove() argument
69 int size = aux_getn(L, 1); in tremove()
70 int pos = luaL_optint(L, 2, size); in tremove()
72 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); in tremove()
73 lua_rawgeti(L, 1, pos); /* result = t[pos] */ in tremove()
75 lua_rawgeti(L, 1, pos+1); in tremove()
76 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ in tremove()
78 lua_pushnil(L); in tremove()
79 lua_rawseti(L, 1, pos); /* t[pos] = nil */ in tremove()
84 static void addfield (lua_State *L, luaL_Buffer *b, int i) { in addfield() argument
85 lua_rawgeti(L, 1, i); in addfield()
86 if (!lua_isstring(L, -1)) in addfield()
87 luaL_error(L, "invalid value (%s) at index %d in table for " in addfield()
88 LUA_QL("concat"), luaL_typename(L, -1), i); in addfield()
93 static int tconcat (lua_State *L) { in tconcat() argument
97 const char *sep = luaL_optlstring(L, 2, "", &lsep); in tconcat()
98 luaL_checktype(L, 1, LUA_TTABLE); in tconcat()
99 i = luaL_optint(L, 3, 1); in tconcat()
100 last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); in tconcat()
101 luaL_buffinit(L, &b); in tconcat()
103 addfield(L, &b, i); in tconcat()
107 addfield(L, &b, i); in tconcat()
119 static int pack (lua_State *L) { in pack() argument
120 int n = lua_gettop(L); /* number of elements to pack */ in pack()
121 lua_createtable(L, n, 1); /* create result table */ in pack()
122 lua_pushinteger(L, n); in pack()
123 lua_setfield(L, -2, "n"); /* t.n = number of elements */ in pack()
126 lua_pushvalue(L, 1); in pack()
127 lua_rawseti(L, -2, 1); /* insert first element */ in pack()
128 lua_replace(L, 1); /* move table into index 1 */ in pack()
130 lua_rawseti(L, 1, i); in pack()
136 static int unpack (lua_State *L) { in unpack() argument
138 luaL_checktype(L, 1, LUA_TTABLE); in unpack()
139 i = luaL_optint(L, 2, 1); in unpack()
140 e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); in unpack()
143 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ in unpack()
144 return luaL_error(L, "too many results to unpack"); in unpack()
145 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ in unpack()
147 lua_rawgeti(L, 1, i); in unpack()
164 static void set2 (lua_State *L, int i, int j) { in set2() argument
165 lua_rawseti(L, 1, i); in set2()
166 lua_rawseti(L, 1, j); in set2()
169 static int sort_comp (lua_State *L, int a, int b) { in sort_comp() argument
170 if (!lua_isnil(L, 2)) { /* function? */ in sort_comp()
172 lua_pushvalue(L, 2); in sort_comp()
173 lua_pushvalue(L, a-1); /* -1 to compensate function */ in sort_comp()
174 lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ in sort_comp()
175 lua_call(L, 2, 1); in sort_comp()
176 res = lua_toboolean(L, -1); in sort_comp()
177 lua_pop(L, 1); in sort_comp()
181 return lua_compare(L, a, b, LUA_OPLT); in sort_comp()
184 static void auxsort (lua_State *L, int l, int u) { in auxsort() argument
188 lua_rawgeti(L, 1, l); in auxsort()
189 lua_rawgeti(L, 1, u); in auxsort()
190 if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ in auxsort()
191 set2(L, l, u); /* swap a[l] - a[u] */ in auxsort()
193 lua_pop(L, 2); in auxsort()
196 lua_rawgeti(L, 1, i); in auxsort()
197 lua_rawgeti(L, 1, l); in auxsort()
198 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ in auxsort()
199 set2(L, i, l); in auxsort()
201 lua_pop(L, 1); /* remove a[l] */ in auxsort()
202 lua_rawgeti(L, 1, u); in auxsort()
203 if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ in auxsort()
204 set2(L, i, u); in auxsort()
206 lua_pop(L, 2); in auxsort()
209 lua_rawgeti(L, 1, i); /* Pivot */ in auxsort()
210 lua_pushvalue(L, -1); in auxsort()
211 lua_rawgeti(L, 1, u-1); in auxsort()
212 set2(L, i, u-1); in auxsort()
217 while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { in auxsort()
218 if (i>=u) luaL_error(L, "invalid order function for sorting"); in auxsort()
219 lua_pop(L, 1); /* remove a[i] */ in auxsort()
222 while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { in auxsort()
223 if (j<=l) luaL_error(L, "invalid order function for sorting"); in auxsort()
224 lua_pop(L, 1); /* remove a[j] */ in auxsort()
227 lua_pop(L, 3); /* pop pivot, a[i], a[j] */ in auxsort()
230 set2(L, i, j); in auxsort()
232 lua_rawgeti(L, 1, u-1); in auxsort()
233 lua_rawgeti(L, 1, i); in auxsort()
234 set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ in auxsort()
243 auxsort(L, j, i); /* call recursively the smaller one */ in auxsort()
247 static int sort (lua_State *L) { in sort() argument
248 int n = aux_getn(L, 1); in sort()
249 luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ in sort()
250 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ in sort()
251 luaL_checktype(L, 2, LUA_TFUNCTION); in sort()
252 lua_settop(L, 2); /* make sure there is two arguments */ in sort()
253 auxsort(L, 1, n); in sort()
274 LUAMOD_API int luaopen_table (lua_State *L) { in luaopen_table() argument
275 luaL_newlib(L, tab_funcs); in luaopen_table()
278 lua_getfield(L, -1, "unpack"); in luaopen_table()
279 lua_setglobal(L, "unpack"); in luaopen_table()