Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lua-5.2.4/src/ltablib.c
Go to the documentation of this file.
1/*
2** $Id: ltablib.c,v 1.65.1.2 2014/05/07 16:32:55 roberto Exp $
3** Library for Table Manipulation
4** See Copyright Notice in lua.h
5*/
6
7
8#include <limits.h>
9#include <stddef.h>
10
11#define ltablib_c
12#define LUA_LIB
13
14#include "lua.h"
15
16#include "lauxlib.h"
17#include "lualib.h"
18
19
20#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
21
22
23
24#if defined(LUA_COMPAT_MAXN)
25static int maxn (lua_State *L) {
26 lua_Number max = 0;
28 lua_pushnil(L); /* first key */
29 while (lua_next(L, 1)) {
30 lua_pop(L, 1); /* remove value */
31 if (lua_type(L, -1) == LUA_TNUMBER) {
32 lua_Number v = lua_tonumber(L, -1);
33 if (v > max) max = v;
34 }
35 }
36 lua_pushnumber(L, max);
37 return 1;
38}
39#endif
40
41
42static int tinsert (lua_State *L) {
43 int e = aux_getn(L, 1) + 1; /* first empty element */
44 int pos; /* where to insert new element */
45 switch (lua_gettop(L)) {
46 case 2: { /* called with only 2 arguments */
47 pos = e; /* insert new element at the end */
48 break;
49 }
50 case 3: {
51 int i;
52 pos = luaL_checkint(L, 2); /* 2nd argument is the position */
53 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
54 for (i = e; i > pos; i--) { /* move up elements */
55 lua_rawgeti(L, 1, i-1);
56 lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
57 }
58 break;
59 }
60 default: {
61 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
62 }
63 }
64 lua_rawseti(L, 1, pos); /* t[pos] = v */
65 return 0;
66}
67
68
69static int tremove (lua_State *L) {
70 int size = aux_getn(L, 1);
71 int pos = luaL_optint(L, 2, size);
72 if (pos != size) /* validate 'pos' if given */
73 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
74 lua_rawgeti(L, 1, pos); /* result = t[pos] */
75 for ( ; pos < size; pos++) {
76 lua_rawgeti(L, 1, pos+1);
77 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
78 }
79 lua_pushnil(L);
80 lua_rawseti(L, 1, pos); /* t[pos] = nil */
81 return 1;
82}
83
84
85static void addfield (lua_State *L, luaL_Buffer *b, int i) {
86 lua_rawgeti(L, 1, i);
87 if (!lua_isstring(L, -1))
88 luaL_error(L, "invalid value (%s) at index %d in table for "
89 LUA_QL("concat"), luaL_typename(L, -1), i);
91}
92
93
94static int tconcat (lua_State *L) {
96 size_t lsep;
97 int i, last;
98 const char *sep = luaL_optlstring(L, 2, "", &lsep);
100 i = luaL_optint(L, 3, 1);
101 last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));
102 luaL_buffinit(L, &b);
103 for (; i < last; i++) {
104 addfield(L, &b, i);
105 luaL_addlstring(&b, sep, lsep);
106 }
107 if (i == last) /* add last value (if interval was not empty) */
108 addfield(L, &b, i);
109 luaL_pushresult(&b);
110 return 1;
111}
112
113
114/*
115** {======================================================
116** Pack/unpack
117** =======================================================
118*/
119
120static int pack (lua_State *L) {
121 int n = lua_gettop(L); /* number of elements to pack */
122 lua_createtable(L, n, 1); /* create result table */
123 lua_pushinteger(L, n);
124 lua_setfield(L, -2, "n"); /* t.n = number of elements */
125 if (n > 0) { /* at least one element? */
126 int i;
127 lua_pushvalue(L, 1);
128 lua_rawseti(L, -2, 1); /* insert first element */
129 lua_replace(L, 1); /* move table into index 1 */
130 for (i = n; i >= 2; i--) /* assign other elements */
131 lua_rawseti(L, 1, i);
132 }
133 return 1; /* return table */
134}
135
136
137static int unpack (lua_State *L) {
138 int i, e;
139 unsigned int n;
141 i = luaL_optint(L, 2, 1);
142 e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
143 if (i > e) return 0; /* empty range */
144 n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */
145 if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n))
146 return luaL_error(L, "too many results to unpack");
147 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
148 while (i++ < e) /* push arg[i + 1...e] */
149 lua_rawgeti(L, 1, i);
150 return n;
151}
152
153/* }====================================================== */
154
155
156
157/*
158** {======================================================
159** Quicksort
160** (based on `Algorithms in MODULA-3', Robert Sedgewick;
161** Addison-Wesley, 1993.)
162** =======================================================
163*/
164
165
166static void set2 (lua_State *L, int i, int j) {
167 lua_rawseti(L, 1, i);
168 lua_rawseti(L, 1, j);
169}
170
171static int sort_comp (lua_State *L, int a, int b) {
172 if (!lua_isnil(L, 2)) { /* function? */
173 int res;
174 lua_pushvalue(L, 2);
175 lua_pushvalue(L, a-1); /* -1 to compensate function */
176 lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
177 lua_call(L, 2, 1);
178 res = lua_toboolean(L, -1);
179 lua_pop(L, 1);
180 return res;
181 }
182 else /* a < b? */
183 return lua_compare(L, a, b, LUA_OPLT);
184}
185
186static void auxsort (lua_State *L, int l, int u) {
187 while (l < u) { /* for tail recursion */
188 int i, j;
189 /* sort elements a[l], a[(l+u)/2] and a[u] */
190 lua_rawgeti(L, 1, l);
191 lua_rawgeti(L, 1, u);
192 if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
193 set2(L, l, u); /* swap a[l] - a[u] */
194 else
195 lua_pop(L, 2);
196 if (u-l == 1) break; /* only 2 elements */
197 i = (l+u)/2;
198 lua_rawgeti(L, 1, i);
199 lua_rawgeti(L, 1, l);
200 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
201 set2(L, i, l);
202 else {
203 lua_pop(L, 1); /* remove a[l] */
204 lua_rawgeti(L, 1, u);
205 if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
206 set2(L, i, u);
207 else
208 lua_pop(L, 2);
209 }
210 if (u-l == 2) break; /* only 3 elements */
211 lua_rawgeti(L, 1, i); /* Pivot */
212 lua_pushvalue(L, -1);
213 lua_rawgeti(L, 1, u-1);
214 set2(L, i, u-1);
215 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
216 i = l; j = u-1;
217 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
218 /* repeat ++i until a[i] >= P */
219 while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
220 if (i>=u) luaL_error(L, "invalid order function for sorting");
221 lua_pop(L, 1); /* remove a[i] */
222 }
223 /* repeat --j until a[j] <= P */
224 while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
225 if (j<=l) luaL_error(L, "invalid order function for sorting");
226 lua_pop(L, 1); /* remove a[j] */
227 }
228 if (j<i) {
229 lua_pop(L, 3); /* pop pivot, a[i], a[j] */
230 break;
231 }
232 set2(L, i, j);
233 }
234 lua_rawgeti(L, 1, u-1);
235 lua_rawgeti(L, 1, i);
236 set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
237 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
238 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
239 if (i-l < u-i) {
240 j=l; i=i-1; l=i+2;
241 }
242 else {
243 j=i+1; i=u; u=j-2;
244 }
245 auxsort(L, j, i); /* call recursively the smaller one */
246 } /* repeat the routine for the larger one */
247}
248
249static int sort (lua_State *L) {
250 int n = aux_getn(L, 1);
251 luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
252 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
254 lua_settop(L, 2); /* make sure there is two arguments */
255 auxsort(L, 1, n);
256 return 0;
257}
258
259/* }====================================================== */
260
261
262static const luaL_Reg tab_funcs[] = {
263 {"concat", tconcat},
264#if defined(LUA_COMPAT_MAXN)
265 {"maxn", maxn},
266#endif
267 {"insert", tinsert},
268 {"pack", pack},
269 {"unpack", unpack},
270 {"remove", tremove},
271 {"sort", sort},
272 {NULL, NULL}
273};
274
275
278#if defined(LUA_COMPAT_UNPACK)
279 /* _G.unpack = table.unpack */
280 lua_getfield(L, -1, "unpack");
281 lua_setglobal(L, "unpack");
282#endif
283 return 1;
284}
285
#define NULL
Definition gmacros.h:924
LUA_API void lua_pushnil(lua_State *L)
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API int lua_type(lua_State *L, int idx)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
LUA_API int lua_isstring(lua_State *L, int idx)
LUA_API int lua_next(lua_State *L, int idx)
LUA_API int lua_checkstack(lua_State *L, int size)
LUA_API void lua_settop(lua_State *L, int idx)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
LUA_API int lua_gettop(lua_State *L)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
LUALIB_API const char * luaL_optlstring(lua_State *L, int narg, const char *def, size_t *len)
#define luaL_typename(L, i)
#define luaL_optint(L, n, d)
#define luaL_opt(L, f, n, d)
#define luaL_checkint(L, n)
#define luaL_argcheck(L, cond, numarg, extramsg)
static int maxn(lua_State *L)
#define LUA_TTABLE
#define LUA_TNUMBER
#define lua_isnil(L, n)
#define lua_setglobal(L, s)
#define lua_pop(L, n)
#define LUA_TFUNCTION
LUA_NUMBER lua_Number
#define lua_isnoneornil(L, n)
#define LUA_QL(x)
LUA_API int lua_compare(lua_State *L, int index1, int index2, int op)
LUALIB_API int luaL_len(lua_State *L, int idx)
#define luaL_newlib(L, l)
static void set2(lua_State *L, int i, int j)
static int tinsert(lua_State *L)
static void auxsort(lua_State *L, int l, int u)
static int pack(lua_State *L)
static void addfield(lua_State *L, luaL_Buffer *b, int i)
static int unpack(lua_State *L)
LUAMOD_API int luaopen_table(lua_State *L)
static int tremove(lua_State *L)
static int tconcat(lua_State *L)
#define aux_getn(L, n)
static const luaL_Reg tab_funcs[]
static int sort(lua_State *L)
static int sort_comp(lua_State *L, int a, int b)
#define LUA_OPLT
#define lua_tonumber(L, i)
#define lua_call(L, n, r)
#define LUAMOD_API
#define lua_replace(L, idx)