ltablib.c 7.7 KB

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