Lines Matching full:l

17 #define aux_getn(L,n)	(luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))  argument
22 static int maxn (lua_State *L) { in maxn() argument
24 luaL_checktype(L, 1, LUA_TTABLE); in maxn()
25 lua_pushnil(L); /* first key */ in maxn()
26 while (lua_next(L, 1)) { in maxn()
27 lua_pop(L, 1); /* remove value */ in maxn()
28 if (lua_type(L, -1) == LUA_TNUMBER) { in maxn()
29 lua_Number v = lua_tonumber(L, -1); in maxn()
33 lua_pushnumber(L, max); in maxn()
39 static int tinsert (lua_State *L) { in tinsert() argument
40 int e = aux_getn(L, 1) + 1; /* first empty element */ in tinsert()
42 switch (lua_gettop(L)) { in tinsert()
49 pos = luaL_checkint(L, 2); /* 2nd argument is the position */ in tinsert()
50 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); in tinsert()
52 lua_rawgeti(L, 1, i-1); in tinsert()
53 lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ in tinsert()
58 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); in tinsert()
61 lua_rawseti(L, 1, pos); /* t[pos] = v */ in tinsert()
66 static int tremove (lua_State *L) { in tremove() argument
67 int size = aux_getn(L, 1); in tremove()
68 int pos = luaL_optint(L, 2, size); in tremove()
70 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); in tremove()
71 lua_rawgeti(L, 1, pos); /* result = t[pos] */ in tremove()
73 lua_rawgeti(L, 1, pos+1); in tremove()
74 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ in tremove()
76 lua_pushnil(L); in tremove()
77 lua_rawseti(L, 1, pos); /* t[pos] = nil */ in tremove()
82 static void addfield (lua_State *L, luaL_Buffer *b, int i) { in addfield() argument
83 lua_rawgeti(L, 1, i); in addfield()
84 if (!lua_isstring(L, -1)) in addfield()
85 luaL_error(L, "invalid value (%s) at index %d in table for " in addfield()
86 LUA_QL("concat"), luaL_typename(L, -1), i); in addfield()
91 static int tconcat (lua_State *L) { in tconcat() argument
95 const char *sep = luaL_optlstring(L, 2, "", &lsep); in tconcat()
96 luaL_checktype(L, 1, LUA_TTABLE); in tconcat()
97 i = luaL_optint(L, 3, 1); in tconcat()
98 last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); in tconcat()
99 luaL_buffinit(L, &b); in tconcat()
101 addfield(L, &b, i); in tconcat()
105 addfield(L, &b, i); in tconcat()
117 static int pack (lua_State *L) { in pack() argument
118 int n = lua_gettop(L); /* number of elements to pack */ in pack()
119 lua_createtable(L, n, 1); /* create result table */ in pack()
120 lua_pushinteger(L, n); in pack()
121 lua_setfield(L, -2, "n"); /* t.n = number of elements */ in pack()
124 lua_pushvalue(L, 1); in pack()
125 lua_rawseti(L, -2, 1); /* insert first element */ in pack()
126 lua_replace(L, 1); /* move table into index 1 */ in pack()
128 lua_rawseti(L, 1, i); in pack()
134 static int unpack (lua_State *L) { in unpack() argument
137 luaL_checktype(L, 1, LUA_TTABLE); in unpack()
138 i = luaL_optint(L, 2, 1); in unpack()
139 e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); in unpack()
142 if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) in unpack()
143 return luaL_error(L, "too many results to unpack"); in unpack()
144 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ in unpack()
146 lua_rawgeti(L, 1, i); in unpack()
163 static void set2 (lua_State *L, int i, int j) { in set2() argument
164 lua_rawseti(L, 1, i); in set2()
165 lua_rawseti(L, 1, j); in set2()
168 static int sort_comp (lua_State *L, int a, int b) { in sort_comp() argument
169 if (!lua_isnil(L, 2)) { /* function? */ in sort_comp()
171 lua_pushvalue(L, 2); in sort_comp()
172 lua_pushvalue(L, a-1); /* -1 to compensate function */ in sort_comp()
173 lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ in sort_comp()
174 lua_call(L, 2, 1); in sort_comp()
175 res = lua_toboolean(L, -1); in sort_comp()
176 lua_pop(L, 1); in sort_comp()
180 return lua_compare(L, a, b, LUA_OPLT); in sort_comp()
183 static void auxsort (lua_State *L, int l, int u) { in auxsort() argument
184 while (l < u) { /* for tail recursion */ in auxsort()
186 /* sort elements a[l], a[(l+u)/2] and a[u] */ in auxsort()
187 lua_rawgeti(L, 1, l); in auxsort()
188 lua_rawgeti(L, 1, u); in auxsort()
189 if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ in auxsort()
190 set2(L, l, u); /* swap a[l] - a[u] */ in auxsort()
192 lua_pop(L, 2); in auxsort()
193 if (u-l == 1) break; /* only 2 elements */ in auxsort()
194 i = (l+u)/2; in auxsort()
195 lua_rawgeti(L, 1, i); in auxsort()
196 lua_rawgeti(L, 1, l); in auxsort()
197 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ in auxsort()
198 set2(L, i, l); in auxsort()
200 lua_pop(L, 1); /* remove a[l] */ in auxsort()
201 lua_rawgeti(L, 1, u); in auxsort()
202 if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ in auxsort()
203 set2(L, i, u); in auxsort()
205 lua_pop(L, 2); in auxsort()
207 if (u-l == 2) break; /* only 3 elements */ in auxsort()
208 lua_rawgeti(L, 1, i); /* Pivot */ in auxsort()
209 lua_pushvalue(L, -1); in auxsort()
210 lua_rawgeti(L, 1, u-1); in auxsort()
211 set2(L, i, u-1); in auxsort()
212 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ in auxsort()
213 i = l; j = u-1; in auxsort()
214 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ in auxsort()
216 while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { in auxsort()
217 if (i>=u) luaL_error(L, "invalid order function for sorting"); in auxsort()
218 lua_pop(L, 1); /* remove a[i] */ in auxsort()
221 while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { in auxsort()
222 if (j<=l) luaL_error(L, "invalid order function for sorting"); in auxsort()
223 lua_pop(L, 1); /* remove a[j] */ in auxsort()
226 lua_pop(L, 3); /* pop pivot, a[i], a[j] */ in auxsort()
229 set2(L, i, j); in auxsort()
231 lua_rawgeti(L, 1, u-1); in auxsort()
232 lua_rawgeti(L, 1, i); in auxsort()
233 set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ in auxsort()
234 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ in auxsort()
235 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ in auxsort()
236 if (i-l < u-i) { in auxsort()
237 j=l; i=i-1; l=i+2; in auxsort()
242 auxsort(L, j, i); /* call recursively the smaller one */ in auxsort()
246 static int tsort (lua_State *L) { in tsort() argument
247 int n = aux_getn(L, 1); in tsort()
248 luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ in tsort()
249 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ in tsort()
250 luaL_checktype(L, 2, LUA_TFUNCTION); in tsort()
251 lua_settop(L, 2); /* make sure there is two arguments */ in tsort()
252 auxsort(L, 1, n); in tsort()
273 LUAMOD_API int luaopen_table (lua_State *L) { in luaopen_table() argument
274 luaL_newlib(L, tab_funcs); in luaopen_table()
277 lua_getfield(L, -1, "unpack"); in luaopen_table()
278 lua_setglobal(L, "unpack"); in luaopen_table()