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