Lines Matching +full:1 +full:l

27 #define TAB_R	1			/* read */
33 #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) argument
36 static int checkfield (lua_State *L, const char *key, int n) { in checkfield() argument
37 lua_pushstring(L, key); in checkfield()
38 return (lua_rawget(L, -n) != LUA_TNIL); in checkfield()
46 static void checktab (lua_State *L, int arg, int what) { in checktab() argument
47 if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ in checktab()
48 int n = 1; /* number of elements to pop */ in checktab()
49 if (lua_getmetatable(L, arg) && /* must have metatable */ in checktab()
50 (!(what & TAB_R) || checkfield(L, "__index", ++n)) && in checktab()
51 (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && in checktab()
52 (!(what & TAB_L) || checkfield(L, "__len", ++n))) { in checktab()
53 lua_pop(L, n); /* pop metatable and tested metamethods */ in checktab()
56 luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ in checktab()
61 static int tinsert (lua_State *L) { in tinsert() argument
63 lua_Integer e = aux_getn(L, 1, TAB_RW); in tinsert()
64 e = luaL_intop(+, e, 1); /* first empty element */ in tinsert()
65 switch (lua_gettop(L)) { in tinsert()
72 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ in tinsert()
73 /* check whether 'pos' is in [1, e] */ in tinsert()
74 luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2, in tinsert()
77 lua_geti(L, 1, i - 1); in tinsert()
78 lua_seti(L, 1, i); /* t[i] = t[i - 1] */ in tinsert()
83 return luaL_error(L, "wrong number of arguments to 'insert'"); in tinsert()
86 lua_seti(L, 1, pos); /* t[pos] = v */ in tinsert()
91 static int tremove (lua_State *L) { in tremove() argument
92 lua_Integer size = aux_getn(L, 1, TAB_RW); in tremove()
93 lua_Integer pos = luaL_optinteger(L, 2, size); in tremove()
95 /* check whether 'pos' is in [1, size + 1] */ in tremove()
96 luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2, in tremove()
98 lua_geti(L, 1, pos); /* result = t[pos] */ in tremove()
100 lua_geti(L, 1, pos + 1); in tremove()
101 lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ in tremove()
103 lua_pushnil(L); in tremove()
104 lua_seti(L, 1, pos); /* remove entry t[pos] */ in tremove()
105 return 1; in tremove()
110 ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
115 static int tmove (lua_State *L) { in tmove() argument
116 lua_Integer f = luaL_checkinteger(L, 2); in tmove()
117 lua_Integer e = luaL_checkinteger(L, 3); in tmove()
118 lua_Integer t = luaL_checkinteger(L, 4); in tmove()
119 int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ in tmove()
120 checktab(L, 1, TAB_R); in tmove()
121 checktab(L, tt, TAB_W); in tmove()
124 luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, in tmove()
126 n = e - f + 1; /* number of elements to move */ in tmove()
127 luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, in tmove()
129 if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { in tmove()
131 lua_geti(L, 1, f + i); in tmove()
132 lua_seti(L, tt, t + i); in tmove()
136 for (i = n - 1; i >= 0; i--) { in tmove()
137 lua_geti(L, 1, f + i); in tmove()
138 lua_seti(L, tt, t + i); in tmove()
142 lua_pushvalue(L, tt); /* return destination table */ in tmove()
143 return 1; in tmove()
147 static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { in addfield() argument
148 lua_geti(L, 1, i); in addfield()
149 if (l_unlikely(!lua_isstring(L, -1))) in addfield()
150 luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", in addfield()
151 luaL_typename(L, -1), (LUAI_UACINT)i); in addfield()
156 static int tconcat (lua_State *L) { in tconcat() argument
158 lua_Integer last = aux_getn(L, 1, TAB_R); in tconcat()
160 const char *sep = luaL_optlstring(L, 2, "", &lsep); in tconcat()
161 lua_Integer i = luaL_optinteger(L, 3, 1); in tconcat()
162 last = luaL_optinteger(L, 4, last); in tconcat()
163 luaL_buffinit(L, &b); in tconcat()
165 addfield(L, &b, i); in tconcat()
169 addfield(L, &b, i); in tconcat()
171 return 1; in tconcat()
181 static int tpack (lua_State *L) { in tpack() argument
183 int n = lua_gettop(L); /* number of elements to pack */ in tpack()
184 lua_createtable(L, n, 1); /* create result table */ in tpack()
185 lua_insert(L, 1); /* put it at index 1 */ in tpack()
186 for (i = n; i >= 1; i--) /* assign elements */ in tpack()
187 lua_seti(L, 1, i); in tpack()
188 lua_pushinteger(L, n); in tpack()
189 lua_setfield(L, 1, "n"); /* t.n = number of elements */ in tpack()
190 return 1; /* return table */ in tpack()
194 static int tunpack (lua_State *L) { in tunpack() argument
196 lua_Integer i = luaL_optinteger(L, 2, 1); in tunpack()
197 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); in tunpack()
199 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ in tunpack()
201 !lua_checkstack(L, (int)(++n)))) in tunpack()
202 return luaL_error(L, "too many results to unpack"); in tunpack()
203 for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ in tunpack()
204 lua_geti(L, 1, i); in tunpack()
206 lua_geti(L, 1, e); /* push last element */ in tunpack()
265 static void set2 (lua_State *L, IdxT i, IdxT j) { in set2() argument
266 lua_seti(L, 1, i); in set2()
267 lua_seti(L, 1, j); in set2()
275 static int sort_comp (lua_State *L, int a, int b) { in sort_comp() argument
276 if (lua_isnil(L, 2)) /* no function? */ in sort_comp()
277 return lua_compare(L, a, b, LUA_OPLT); /* a < b */ in sort_comp()
280 lua_pushvalue(L, 2); /* push function */ in sort_comp()
281 lua_pushvalue(L, a-1); /* -1 to compensate function */ in sort_comp()
282 lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ in sort_comp()
283 lua_call(L, 2, 1); /* call function */ in sort_comp()
284 res = lua_toboolean(L, -1); /* get result */ in sort_comp()
285 lua_pop(L, 1); /* pop result */ in sort_comp()
293 ** precondition: a[lo] <= P == a[up-1] <= a[up],
294 ** so it only needs to do the partition from lo + 1 to up - 2.
295 ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
298 static IdxT partition (lua_State *L, IdxT lo, IdxT up) { in partition() argument
300 IdxT j = up - 1; /* will be decremented before first use */ in partition()
304 while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { in partition()
305 if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ in partition()
306 luaL_error(L, "invalid order function for sorting"); in partition()
307 lua_pop(L, 1); /* remove a[i] */ in partition()
309 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ in partition()
311 while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { in partition()
313 luaL_error(L, "invalid order function for sorting"); in partition()
314 lua_pop(L, 1); /* remove a[j] */ in partition()
316 /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ in partition()
318 /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ in partition()
319 lua_pop(L, 1); /* pop a[j] */ in partition()
320 /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ in partition()
321 set2(L, up - 1, i); in partition()
325 set2(L, i, j); in partition()
345 static void auxsort (lua_State *L, IdxT lo, IdxT up, in auxsort() argument
351 lua_geti(L, 1, lo); in auxsort()
352 lua_geti(L, 1, up); in auxsort()
353 if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ in auxsort()
354 set2(L, lo, up); /* swap a[lo] - a[up] */ in auxsort()
356 lua_pop(L, 2); /* remove both values */ in auxsort()
357 if (up - lo == 1) /* only 2 elements? */ in auxsort()
363 lua_geti(L, 1, p); in auxsort()
364 lua_geti(L, 1, lo); in auxsort()
365 if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ in auxsort()
366 set2(L, p, lo); /* swap a[p] - a[lo] */ in auxsort()
368 lua_pop(L, 1); /* remove a[lo] */ in auxsort()
369 lua_geti(L, 1, up); in auxsort()
370 if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ in auxsort()
371 set2(L, p, up); /* swap a[up] - a[p] */ in auxsort()
373 lua_pop(L, 2); in auxsort()
377 lua_geti(L, 1, p); /* get middle element (Pivot) */ in auxsort()
378 lua_pushvalue(L, -1); /* push Pivot */ in auxsort()
379 lua_geti(L, 1, up - 1); /* push a[up - 1] */ in auxsort()
380 set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ in auxsort()
381 p = partition(L, lo, up); in auxsort()
382 /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ in auxsort()
384 auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ in auxsort()
386 lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */ in auxsort()
389 auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ in auxsort()
391 up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ in auxsort()
395 } /* tail call auxsort(L, lo, up, rnd) */ in auxsort()
399 static int sort (lua_State *L) { in sort() argument
400 lua_Integer n = aux_getn(L, 1, TAB_RW); in sort()
401 if (n > 1) { /* non-trivial interval? */ in sort()
402 luaL_argcheck(L, n < INT_MAX, 1, "array too big"); in sort()
403 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ in sort()
404 luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ in sort()
405 lua_settop(L, 2); /* make sure there are two arguments */ in sort()
406 auxsort(L, 1, (IdxT)n, 0); in sort()
426 LUAMOD_API int luaopen_table (lua_State *L) { in luaopen_table() argument
427 luaL_newlib(L, tab_funcs); in luaopen_table()
428 return 1; in luaopen_table()