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