ltablib.c 7.5 KB

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