ltablib.c 6.8 KB

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