ltablib.c 6.6 KB

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