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