Lines Matching +full:1 +full:l

18 #define aux_getn(L,n)	(luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))  argument
23 static int maxn (lua_State *L) { in maxn() argument
25 luaL_checktype(L, 1, LUA_TTABLE); in maxn()
26 lua_pushnil(L); /* first key */ in maxn()
27 while (lua_next(L, 1)) { in maxn()
28 lua_pop(L, 1); /* remove value */ in maxn()
29 if (lua_type(L, -1) == LUA_TNUMBER) { in maxn()
30 lua_Number v = lua_tonumber(L, -1); in maxn()
34 lua_pushnumber(L, max); in maxn()
35 return 1; in maxn()
40 static int tinsert (lua_State *L) { in tinsert() argument
41 int e = aux_getn(L, 1) + 1; /* first empty element */ in tinsert()
43 switch (lua_gettop(L)) { in tinsert()
50 pos = luaL_checkint(L, 2); /* 2nd argument is the position */ in tinsert()
51 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); in tinsert()
53 lua_rawgeti(L, 1, i-1); in tinsert()
54 lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ in tinsert()
59 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); in tinsert()
62 lua_rawseti(L, 1, pos); /* t[pos] = v */ in tinsert()
67 static int tremove (lua_State *L) { in tremove() argument
68 int size = aux_getn(L, 1); in tremove()
69 int pos = luaL_optint(L, 2, size); in tremove()
71 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); in tremove()
72 lua_rawgeti(L, 1, pos); /* result = t[pos] */ in tremove()
74 lua_rawgeti(L, 1, pos+1); in tremove()
75 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ in tremove()
77 lua_pushnil(L); in tremove()
78 lua_rawseti(L, 1, pos); /* t[pos] = nil */ in tremove()
79 return 1; in tremove()
83 static void addfield (lua_State *L, luaL_Buffer *b, int i) { in addfield() argument
84 lua_rawgeti(L, 1, i); in addfield()
85 if (!lua_isstring(L, -1)) in addfield()
86 luaL_error(L, "invalid value (%s) at index %d in table for " in addfield()
87 LUA_QL("concat"), luaL_typename(L, -1), i); in addfield()
92 static int tconcat (lua_State *L) { in tconcat() argument
96 const char *sep = luaL_optlstring(L, 2, "", &lsep); in tconcat()
97 luaL_checktype(L, 1, LUA_TTABLE); in tconcat()
98 i = luaL_optint(L, 3, 1); in tconcat()
99 last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); in tconcat()
100 luaL_buffinit(L, &b); in tconcat()
102 addfield(L, &b, i); in tconcat()
106 addfield(L, &b, i); in tconcat()
108 return 1; in tconcat()
118 static int pack (lua_State *L) { in pack() argument
119 int n = lua_gettop(L); /* number of elements to pack */ in pack()
120 lua_createtable(L, n, 1); /* create result table */ in pack()
121 lua_pushinteger(L, n); in pack()
122 lua_setfield(L, -2, "n"); /* t.n = number of elements */ in pack()
125 lua_pushvalue(L, 1); in pack()
126 lua_rawseti(L, -2, 1); /* insert first element */ in pack()
127 lua_replace(L, 1); /* move table into index 1 */ in pack()
129 lua_rawseti(L, 1, i); in pack()
131 return 1; /* return table */ in pack()
135 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()
142 n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */ in unpack()
143 if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) 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()
146 while (i++ < e) /* push arg[i + 1...e] */ 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
185 while (l < u) { /* for tail recursion */ in auxsort()
187 /* sort elements a[l], a[(l+u)/2] and a[u] */ in auxsort()
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()
194 if (u-l == 1) break; /* only 2 elements */ in auxsort()
195 i = (l+u)/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()
208 if (u-l == 2) break; /* only 3 elements */ 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()
213 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ in auxsort()
214 i = l; j = u-1; in auxsort()
215 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ 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()
235 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ in auxsort()
236 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ in auxsort()
237 if (i-l < u-i) { in auxsort()
238 j=l; i=i-1; l=i+2; in auxsort()
241 j=i+1; i=u; u=j-2; in auxsort()
243 auxsort(L, j, i); /* call recursively the smaller one */ in auxsort()
247 static int tsort (lua_State *L) { in tsort() argument
248 int n = aux_getn(L, 1); in tsort()
249 luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ in tsort()
250 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ in tsort()
251 luaL_checktype(L, 2, LUA_TFUNCTION); in tsort()
252 lua_settop(L, 2); /* make sure there is two arguments */ in tsort()
253 auxsort(L, 1, n); in tsort()
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()
281 return 1; in luaopen_table()