ltablib.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. ** $Id: ltablib.c,v 1.11 2002/08/06 17:06:56 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. int n = aux_getn(L, 1);
  128. size_t lsep;
  129. const char *sep = luaL_opt_lstr(L, 2, "", &lsep);
  130. int i;
  131. luaL_buffinit(L, &b);
  132. for (i=1; i<=n; i++) {
  133. lua_rawgeti(L, 1, i);
  134. luaL_arg_check(L, lua_isstring(L, -1), 1,
  135. "table contains non-strings");
  136. luaL_addvalue(&b);
  137. if (i != n)
  138. luaL_addlstring(&b, sep, lsep);
  139. }
  140. luaL_pushresult(&b);
  141. return 1;
  142. }
  143. /*
  144. ** {======================================================
  145. ** Quicksort
  146. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  147. ** Addison-Wesley, 1993.)
  148. */
  149. static void set2 (lua_State *L, int i, int j) {
  150. lua_rawseti(L, 1, i);
  151. lua_rawseti(L, 1, j);
  152. }
  153. static int sort_comp (lua_State *L, int a, int b) {
  154. if (!lua_isnil(L, 2)) { /* function? */
  155. int res;
  156. lua_pushvalue(L, 2);
  157. lua_pushvalue(L, a-1); /* -1 to compensate function */
  158. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  159. lua_call(L, 2, 1);
  160. res = lua_toboolean(L, -1);
  161. lua_pop(L, 1);
  162. return res;
  163. }
  164. else /* a < b? */
  165. return lua_lessthan(L, a, b);
  166. }
  167. static void auxsort (lua_State *L, int l, int u) {
  168. while (l < u) { /* for tail recursion */
  169. int i, j;
  170. /* sort elements a[l], a[(l+u)/2] and a[u] */
  171. lua_rawgeti(L, 1, l);
  172. lua_rawgeti(L, 1, u);
  173. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  174. set2(L, l, u); /* swap a[l] - a[u] */
  175. else
  176. lua_pop(L, 2);
  177. if (u-l == 1) break; /* only 2 elements */
  178. i = (l+u)/2;
  179. lua_rawgeti(L, 1, i);
  180. lua_rawgeti(L, 1, l);
  181. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  182. set2(L, i, l);
  183. else {
  184. lua_pop(L, 1); /* remove a[l] */
  185. lua_rawgeti(L, 1, u);
  186. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  187. set2(L, i, u);
  188. else
  189. lua_pop(L, 2);
  190. }
  191. if (u-l == 2) break; /* only 3 elements */
  192. lua_rawgeti(L, 1, i); /* Pivot */
  193. lua_pushvalue(L, -1);
  194. lua_rawgeti(L, 1, u-1);
  195. set2(L, i, u-1);
  196. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  197. i = l; j = u-1;
  198. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  199. /* repeat ++i until a[i] >= P */
  200. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  201. if (i>u) luaL_error(L, "invalid order function for sorting");
  202. lua_pop(L, 1); /* remove a[i] */
  203. }
  204. /* repeat --j until a[j] <= P */
  205. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  206. if (j<l) luaL_error(L, "invalid order function for sorting");
  207. lua_pop(L, 1); /* remove a[j] */
  208. }
  209. if (j<i) {
  210. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  211. break;
  212. }
  213. set2(L, i, j);
  214. }
  215. lua_rawgeti(L, 1, u-1);
  216. lua_rawgeti(L, 1, i);
  217. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  218. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  219. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  220. if (i-l < u-i) {
  221. j=l; i=i-1; l=i+2;
  222. }
  223. else {
  224. j=i+1; i=u; u=j-2;
  225. }
  226. auxsort(L, j, i); /* call recursively the smaller one */
  227. } /* repeat the routine for the larger one */
  228. }
  229. static int luaB_sort (lua_State *L) {
  230. int n = aux_getn(L, 1);
  231. luaL_check_stack(L, 40, ""); /* assume array is smaller than 2^40 */
  232. if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
  233. luaL_check_type(L, 2, LUA_TFUNCTION);
  234. lua_settop(L, 2); /* make sure there is two arguments */
  235. auxsort(L, 1, n);
  236. return 0;
  237. }
  238. /* }====================================================== */
  239. static const luaL_reg tab_funcs[] = {
  240. {"concat", str_concat},
  241. {"foreach", luaB_foreach},
  242. {"foreachi", luaB_foreachi},
  243. {"getn", luaB_getn},
  244. {"setn", luaB_setn},
  245. {"sort", luaB_sort},
  246. {"insert", luaB_tinsert},
  247. {"remove", luaB_tremove},
  248. {NULL, NULL}
  249. };
  250. LUALIB_API int lua_tablibopen (lua_State *L) {
  251. lua_newtable(L); /* create N (table to store num. elements in tables) */
  252. lua_setmode(L, -1, "k"); /* make it a weak table */
  253. luaL_opennamedlib(L, LUA_TABLIBNAME, tab_funcs, 1);
  254. return 0;
  255. }