ltablib.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. ** $Id: ltablib.c,v 1.16 2002/11/14 15:41:38 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. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lualib.h"
  11. static int checkint (lua_State *L) {
  12. int n = (int)lua_tonumber(L, -1);
  13. if (n == 0 && !lua_isnumber(L, -1)) n = -1;
  14. lua_pop(L, 1);
  15. return n;
  16. }
  17. static void aux_setn (lua_State *L, int t, int n) {
  18. lua_pushliteral(L, "n");
  19. lua_rawget(L, t);
  20. if (checkint(L) >= 0) {
  21. lua_pushliteral(L, "n"); /* use it */
  22. lua_pushnumber(L, n);
  23. lua_rawset(L, t);
  24. }
  25. else { /* use N */
  26. lua_pushvalue(L, t);
  27. lua_pushnumber(L, n);
  28. lua_rawset(L, lua_upvalueindex(1)); /* N[t] = n */
  29. }
  30. }
  31. static int aux_getn (lua_State *L, int t) {
  32. int n;
  33. luaL_checktype(L, t, LUA_TTABLE);
  34. lua_pushliteral(L, "n"); /* try t.n */
  35. lua_rawget(L, t);
  36. if ((n = checkint(L)) >= 0) return n;
  37. lua_pushvalue(L, t); /* try N[t] */
  38. lua_rawget(L, lua_upvalueindex(1));
  39. if ((n = checkint(L)) >= 0) return n;
  40. else { /* must count elements */
  41. n = 0;
  42. for (;;) {
  43. lua_rawgeti(L, t, ++n);
  44. if (lua_isnil(L, -1)) break;
  45. lua_pop(L, 1);
  46. }
  47. lua_pop(L, 1);
  48. aux_setn(L, t, n - 1);
  49. return n - 1;
  50. }
  51. }
  52. static int luaB_foreachi (lua_State *L) {
  53. int i;
  54. int n = aux_getn(L, 1);
  55. luaL_checktype(L, 2, LUA_TFUNCTION);
  56. for (i=1; i<=n; i++) {
  57. lua_pushvalue(L, 2); /* function */
  58. lua_pushnumber(L, i); /* 1st argument */
  59. lua_rawgeti(L, 1, i); /* 2nd argument */
  60. lua_call(L, 2, 1);
  61. if (!lua_isnil(L, -1))
  62. return 1;
  63. lua_pop(L, 1); /* remove nil result */
  64. }
  65. return 0;
  66. }
  67. static int luaB_foreach (lua_State *L) {
  68. luaL_checktype(L, 1, LUA_TTABLE);
  69. luaL_checktype(L, 2, LUA_TFUNCTION);
  70. lua_pushnil(L); /* first key */
  71. for (;;) {
  72. if (lua_next(L, 1) == 0)
  73. return 0;
  74. lua_pushvalue(L, 2); /* function */
  75. lua_pushvalue(L, -3); /* key */
  76. lua_pushvalue(L, -3); /* value */
  77. lua_call(L, 2, 1);
  78. if (!lua_isnil(L, -1))
  79. return 1;
  80. lua_pop(L, 2); /* remove value and result */
  81. }
  82. }
  83. static int luaB_getn (lua_State *L) {
  84. lua_pushnumber(L, aux_getn(L, 1));
  85. return 1;
  86. }
  87. static int luaB_setn (lua_State *L) {
  88. luaL_checktype(L, 1, LUA_TTABLE);
  89. aux_setn(L, 1, luaL_checkint(L, 2));
  90. return 0;
  91. }
  92. static int luaB_tinsert (lua_State *L) {
  93. int v = lua_gettop(L); /* number of arguments */
  94. int n = aux_getn(L, 1) + 1;
  95. int pos; /* where to insert new element */
  96. if (v == 2) /* called with only 2 arguments */
  97. pos = n; /* insert new element at the end */
  98. else {
  99. pos = luaL_checkint(L, 2); /* 2nd argument is the position */
  100. if (pos > n) n = pos; /* `grow' array if necessary */
  101. v = 3; /* function may be called with more than 3 args */
  102. }
  103. aux_setn(L, 1, n); /* new size */
  104. while (--n >= pos) { /* move up elements */
  105. lua_rawgeti(L, 1, n);
  106. lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
  107. }
  108. lua_pushvalue(L, v);
  109. lua_rawseti(L, 1, pos); /* t[pos] = v */
  110. return 0;
  111. }
  112. static int luaB_tremove (lua_State *L) {
  113. int n = aux_getn(L, 1);
  114. int pos = luaL_optint(L, 2, n);
  115. if (n <= 0) return 0; /* table is `empty' */
  116. aux_setn(L, 1, n-1); /* t.n = n-1 */
  117. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  118. for ( ;pos<n; pos++) {
  119. lua_rawgeti(L, 1, pos+1);
  120. lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
  121. }
  122. lua_pushnil(L);
  123. lua_rawseti(L, 1, n); /* t[n] = nil */
  124. return 1;
  125. }
  126. static int str_concat (lua_State *L) {
  127. luaL_Buffer b;
  128. size_t lsep;
  129. const char *sep = luaL_optlstring(L, 2, "", &lsep);
  130. int i = luaL_optint(L, 3, 1);
  131. int n = luaL_optint(L, 4, 0);
  132. luaL_checktype(L, 1, LUA_TTABLE);
  133. if (n == 0) n = aux_getn(L, 1);
  134. luaL_buffinit(L, &b);
  135. for (; i <= n; i++) {
  136. lua_rawgeti(L, 1, i);
  137. luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings");
  138. luaL_addvalue(&b);
  139. if (i != n)
  140. luaL_addlstring(&b, sep, lsep);
  141. }
  142. luaL_pushresult(&b);
  143. return 1;
  144. }
  145. /*
  146. ** {======================================================
  147. ** Quicksort
  148. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  149. ** Addison-Wesley, 1993.)
  150. */
  151. static void set2 (lua_State *L, int i, int j) {
  152. lua_rawseti(L, 1, i);
  153. lua_rawseti(L, 1, j);
  154. }
  155. static int sort_comp (lua_State *L, int a, int b) {
  156. if (!lua_isnil(L, 2)) { /* function? */
  157. int res;
  158. lua_pushvalue(L, 2);
  159. lua_pushvalue(L, a-1); /* -1 to compensate function */
  160. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  161. lua_call(L, 2, 1);
  162. res = lua_toboolean(L, -1);
  163. lua_pop(L, 1);
  164. return res;
  165. }
  166. else /* a < b? */
  167. return lua_lessthan(L, a, b);
  168. }
  169. static void auxsort (lua_State *L, int l, int u) {
  170. while (l < u) { /* for tail recursion */
  171. int i, j;
  172. /* sort elements a[l], a[(l+u)/2] and a[u] */
  173. lua_rawgeti(L, 1, l);
  174. lua_rawgeti(L, 1, u);
  175. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  176. set2(L, l, u); /* swap a[l] - a[u] */
  177. else
  178. lua_pop(L, 2);
  179. if (u-l == 1) break; /* only 2 elements */
  180. i = (l+u)/2;
  181. lua_rawgeti(L, 1, i);
  182. lua_rawgeti(L, 1, l);
  183. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  184. set2(L, i, l);
  185. else {
  186. lua_pop(L, 1); /* remove a[l] */
  187. lua_rawgeti(L, 1, u);
  188. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  189. set2(L, i, u);
  190. else
  191. lua_pop(L, 2);
  192. }
  193. if (u-l == 2) break; /* only 3 elements */
  194. lua_rawgeti(L, 1, i); /* Pivot */
  195. lua_pushvalue(L, -1);
  196. lua_rawgeti(L, 1, u-1);
  197. set2(L, i, u-1);
  198. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  199. i = l; j = u-1;
  200. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  201. /* repeat ++i until a[i] >= P */
  202. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  203. if (i>u) luaL_error(L, "invalid order function for sorting");
  204. lua_pop(L, 1); /* remove a[i] */
  205. }
  206. /* repeat --j until a[j] <= P */
  207. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  208. if (j<l) luaL_error(L, "invalid order function for sorting");
  209. lua_pop(L, 1); /* remove a[j] */
  210. }
  211. if (j<i) {
  212. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  213. break;
  214. }
  215. set2(L, i, j);
  216. }
  217. lua_rawgeti(L, 1, u-1);
  218. lua_rawgeti(L, 1, i);
  219. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  220. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  221. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  222. if (i-l < u-i) {
  223. j=l; i=i-1; l=i+2;
  224. }
  225. else {
  226. j=i+1; i=u; u=j-2;
  227. }
  228. auxsort(L, j, i); /* call recursively the smaller one */
  229. } /* repeat the routine for the larger one */
  230. }
  231. static int luaB_sort (lua_State *L) {
  232. int n = aux_getn(L, 1);
  233. luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
  234. if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
  235. luaL_checktype(L, 2, LUA_TFUNCTION);
  236. lua_settop(L, 2); /* make sure there is two arguments */
  237. auxsort(L, 1, n);
  238. return 0;
  239. }
  240. /* }====================================================== */
  241. static const luaL_reg tab_funcs[] = {
  242. {"concat", str_concat},
  243. {"foreach", luaB_foreach},
  244. {"foreachi", luaB_foreachi},
  245. {"getn", luaB_getn},
  246. {"setn", luaB_setn},
  247. {"sort", luaB_sort},
  248. {"insert", luaB_tinsert},
  249. {"remove", luaB_tremove},
  250. {NULL, NULL}
  251. };
  252. LUALIB_API int lua_tablibopen (lua_State *L) {
  253. lua_newtable(L); /* create N (table to store num. elements in tables) */
  254. lua_pushvalue(L, -1); /* `N' will be its own metatable */
  255. lua_setmetatable(L, -2);
  256. lua_pushliteral(L, "__mode");
  257. lua_pushliteral(L, "k");
  258. lua_rawset(L, -3); /* metatable(N).__mode = "k" */
  259. luaL_openlib(L, LUA_TABLIBNAME, tab_funcs, 1);
  260. return 0;
  261. }