ltablib.c 9.8 KB

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