ltablib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. ** $Id: ltablib.c,v 1.82 2015/09/09 15:42:30 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. ** Operations that an object must define to mimic a table
  16. ** (some functions only need some of them)
  17. */
  18. #define TAB_R 1
  19. #define TAB_W 2
  20. #define TAB_L 4
  21. #define TAB_RW (TAB_R | TAB_W)
  22. #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
  23. static int checkfield (lua_State *L, const char *key, int n) {
  24. lua_pushstring(L, key);
  25. return (lua_rawget(L, -n) != LUA_TNIL);
  26. }
  27. /*
  28. ** Check that 'arg' either is a table or can behave like one (that is,
  29. ** has a metatable with the required metamethods)
  30. */
  31. static void checktab (lua_State *L, int arg, int what) {
  32. if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
  33. int n = 1; /* number of elements to pop */
  34. if (lua_getmetatable(L, arg) && /* must have metatable */
  35. (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
  36. (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
  37. (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
  38. lua_pop(L, n); /* pop metatable and tested metamethods */
  39. }
  40. else
  41. luaL_argerror(L, arg, "table expected"); /* force an error */
  42. }
  43. }
  44. #if defined(LUA_COMPAT_MAXN)
  45. static int maxn (lua_State *L) {
  46. lua_Number max = 0;
  47. luaL_checktype(L, 1, LUA_TTABLE);
  48. lua_pushnil(L); /* first key */
  49. while (lua_next(L, 1)) {
  50. lua_pop(L, 1); /* remove value */
  51. if (lua_type(L, -1) == LUA_TNUMBER) {
  52. lua_Number v = lua_tonumber(L, -1);
  53. if (v > max) max = v;
  54. }
  55. }
  56. lua_pushnumber(L, max);
  57. return 1;
  58. }
  59. #endif
  60. static int tinsert (lua_State *L) {
  61. lua_Integer e = aux_getn(L, 1, TAB_RW) + 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. lua_geti(L, 1, i - 1);
  74. lua_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. lua_seti(L, 1, pos); /* t[pos] = v */
  83. return 0;
  84. }
  85. static int tremove (lua_State *L) {
  86. lua_Integer size = aux_getn(L, 1, TAB_RW);
  87. lua_Integer pos = luaL_optinteger(L, 2, size);
  88. if (pos != size) /* validate 'pos' if given */
  89. luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
  90. lua_geti(L, 1, pos); /* result = t[pos] */
  91. for ( ; pos < size; pos++) {
  92. lua_geti(L, 1, pos + 1);
  93. lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
  94. }
  95. lua_pushnil(L);
  96. lua_seti(L, 1, pos); /* t[pos] = nil */
  97. return 1;
  98. }
  99. /*
  100. ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
  101. ** possible, copy in increasing order, which is better for rehashing.
  102. ** "possible" means destination after original range, or smaller
  103. ** than origin, or copying to another table.
  104. */
  105. static int tmove (lua_State *L) {
  106. lua_Integer f = luaL_checkinteger(L, 2);
  107. lua_Integer e = luaL_checkinteger(L, 3);
  108. lua_Integer t = luaL_checkinteger(L, 4);
  109. int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
  110. checktab(L, 1, TAB_R);
  111. checktab(L, tt, TAB_W);
  112. if (e >= f) { /* otherwise, nothing to move */
  113. lua_Integer n, i;
  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 > e || t <= f || tt != 1) {
  120. for (i = 0; i < n; i++) {
  121. lua_geti(L, 1, f + i);
  122. lua_seti(L, tt, t + i);
  123. }
  124. }
  125. else {
  126. for (i = n - 1; i >= 0; i--) {
  127. lua_geti(L, 1, f + i);
  128. lua_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, lua_Integer i) {
  136. lua_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. luaL_Buffer b;
  144. size_t lsep;
  145. lua_Integer i;
  146. lua_Integer last = aux_getn(L, 1, TAB_R);
  147. const char *sep = luaL_optlstring(L, 2, "", &lsep);
  148. i = luaL_optinteger(L, 3, 1);
  149. last = luaL_opt(L, luaL_checkinteger, 4, last);
  150. luaL_buffinit(L, &b);
  151. for (; i < last; i++) {
  152. addfield(L, &b, i);
  153. luaL_addlstring(&b, sep, lsep);
  154. }
  155. if (i == last) /* add last value (if interval was not empty) */
  156. addfield(L, &b, i);
  157. luaL_pushresult(&b);
  158. return 1;
  159. }
  160. /*
  161. ** {======================================================
  162. ** Pack/unpack
  163. ** =======================================================
  164. */
  165. static int pack (lua_State *L) {
  166. int i;
  167. int n = lua_gettop(L); /* number of elements to pack */
  168. lua_createtable(L, n, 1); /* create result table */
  169. lua_insert(L, 1); /* put it at index 1 */
  170. for (i = n; i >= 1; i--) /* assign elements */
  171. lua_seti(L, 1, i);
  172. lua_pushinteger(L, n);
  173. lua_setfield(L, 1, "n"); /* t.n = number of elements */
  174. return 1; /* return table */
  175. }
  176. static int unpack (lua_State *L) {
  177. lua_Integer i, e;
  178. lua_Unsigned n;
  179. i = luaL_optinteger(L, 2, 1);
  180. e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
  181. if (i > e) return 0; /* empty range */
  182. n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
  183. if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
  184. return luaL_error(L, "too many results to unpack");
  185. for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
  186. lua_geti(L, 1, i);
  187. }
  188. lua_geti(L, 1, e); /* push last element */
  189. return (int)n;
  190. }
  191. /* }====================================================== */
  192. /*
  193. ** {======================================================
  194. ** Quicksort
  195. ** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
  196. ** Addison-Wesley, 1993.)
  197. ** =======================================================
  198. */
  199. static void set2 (lua_State *L, int i, int j) {
  200. lua_seti(L, 1, i);
  201. lua_seti(L, 1, j);
  202. }
  203. static int sort_comp (lua_State *L, int a, int b) {
  204. if (!lua_isnil(L, 2)) { /* function? */
  205. int res;
  206. lua_pushvalue(L, 2);
  207. lua_pushvalue(L, a-1); /* -1 to compensate function */
  208. lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
  209. lua_call(L, 2, 1);
  210. res = lua_toboolean(L, -1);
  211. lua_pop(L, 1);
  212. return res;
  213. }
  214. else /* a < b? */
  215. return lua_compare(L, a, b, LUA_OPLT);
  216. }
  217. static void auxsort (lua_State *L, int l, int u) {
  218. while (l < u) { /* for tail recursion */
  219. int i, j;
  220. /* sort elements a[l], a[(l+u)/2] and a[u] */
  221. lua_geti(L, 1, l);
  222. lua_geti(L, 1, u);
  223. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  224. set2(L, l, u); /* swap a[l] - a[u] */
  225. else
  226. lua_pop(L, 2);
  227. if (u-l == 1) break; /* only 2 elements */
  228. i = (l+u)/2;
  229. lua_geti(L, 1, i);
  230. lua_geti(L, 1, l);
  231. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  232. set2(L, i, l);
  233. else {
  234. lua_pop(L, 1); /* remove a[l] */
  235. lua_geti(L, 1, u);
  236. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  237. set2(L, i, u);
  238. else
  239. lua_pop(L, 2);
  240. }
  241. if (u-l == 2) break; /* only 3 elements */
  242. lua_geti(L, 1, i); /* Pivot */
  243. lua_pushvalue(L, -1);
  244. lua_geti(L, 1, u-1);
  245. set2(L, i, u-1);
  246. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  247. i = l; j = u-1;
  248. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  249. /* repeat ++i until a[i] >= P */
  250. while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
  251. if (i>=u) luaL_error(L, "invalid order function for sorting");
  252. lua_pop(L, 1); /* remove a[i] */
  253. }
  254. /* repeat --j until a[j] <= P */
  255. while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
  256. if (j<=l) luaL_error(L, "invalid order function for sorting");
  257. lua_pop(L, 1); /* remove a[j] */
  258. }
  259. if (j<i) {
  260. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  261. break;
  262. }
  263. set2(L, i, j);
  264. }
  265. lua_geti(L, 1, u-1);
  266. lua_geti(L, 1, i);
  267. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  268. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  269. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  270. if (i-l < u-i) {
  271. j=l; i=i-1; l=i+2;
  272. }
  273. else {
  274. j=i+1; i=u; u=j-2;
  275. }
  276. auxsort(L, j, i); /* call recursively the smaller one */
  277. } /* repeat the routine for the larger one */
  278. }
  279. static int sort (lua_State *L) {
  280. int n = (int)aux_getn(L, 1, TAB_RW);
  281. luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */
  282. if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
  283. luaL_checktype(L, 2, LUA_TFUNCTION);
  284. lua_settop(L, 2); /* make sure there are two arguments */
  285. auxsort(L, 1, n);
  286. return 0;
  287. }
  288. /* }====================================================== */
  289. static const luaL_Reg tab_funcs[] = {
  290. {"concat", tconcat},
  291. #if defined(LUA_COMPAT_MAXN)
  292. {"maxn", maxn},
  293. #endif
  294. {"insert", tinsert},
  295. {"pack", pack},
  296. {"unpack", unpack},
  297. {"remove", tremove},
  298. {"move", tmove},
  299. {"sort", sort},
  300. {NULL, NULL}
  301. };
  302. LUAMOD_API int luaopen_table (lua_State *L) {
  303. luaL_newlib(L, tab_funcs);
  304. #if defined(LUA_COMPAT_UNPACK)
  305. /* _G.unpack = table.unpack */
  306. lua_getfield(L, -1, "unpack");
  307. lua_setglobal(L, "unpack");
  308. #endif
  309. return 1;
  310. }