ltablib.c 7.5 KB

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