ltablib.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. ** $Id: ltablib.c,v 1.56 2010/07/02 11:38:13 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) \
  13. (luaL_checktype(L, n, LUA_TTABLE), (int)lua_rawlen(L, n))
  14. static int foreachi (lua_State *L) {
  15. int n = aux_getn(L, 1);
  16. int i;
  17. if (lua_getctx(L, &i) == LUA_YIELD) goto poscall;
  18. luaL_checktype(L, 2, LUA_TFUNCTION);
  19. for (i = 1; i <= n; i++) {
  20. lua_pushvalue(L, 2); /* function */
  21. lua_pushinteger(L, i); /* 1st argument */
  22. lua_rawgeti(L, 1, i); /* 2nd argument */
  23. lua_callk(L, 2, 1, i, foreachi);
  24. poscall:
  25. if (!lua_isnil(L, -1))
  26. return 1;
  27. lua_pop(L, 1); /* remove nil result */
  28. }
  29. return 0;
  30. }
  31. static int foreachcont (lua_State *L) {
  32. for (;;) {
  33. if (!lua_isnil(L, -1))
  34. return 1;
  35. lua_pop(L, 2); /* remove value and result */
  36. if (lua_next(L, 1) == 0) /* no more elements? */
  37. return 0;
  38. lua_pushvalue(L, 2); /* function */
  39. lua_pushvalue(L, -3); /* key */
  40. lua_pushvalue(L, -3); /* value */
  41. lua_callk(L, 2, 1, 0, foreachcont);
  42. }
  43. }
  44. static int foreach (lua_State *L) {
  45. luaL_checktype(L, 1, LUA_TTABLE);
  46. luaL_checktype(L, 2, LUA_TFUNCTION);
  47. lua_pushnil(L); /* first key */
  48. lua_pushnil(L); /* first value */
  49. lua_pushnil(L); /* first "return" */
  50. return foreachcont(L);
  51. }
  52. #if defined(LUA_COMPAT_MAXN)
  53. static int maxn (lua_State *L) {
  54. lua_Number max = 0;
  55. luaL_checktype(L, 1, LUA_TTABLE);
  56. lua_pushnil(L); /* first key */
  57. while (lua_next(L, 1)) {
  58. lua_pop(L, 1); /* remove value */
  59. if (lua_type(L, -1) == LUA_TNUMBER) {
  60. lua_Number v = lua_tonumber(L, -1);
  61. if (v > max) max = v;
  62. }
  63. }
  64. lua_pushnumber(L, max);
  65. return 1;
  66. }
  67. #else
  68. static int maxn (lua_State *L) {
  69. return luaL_error(L, "function 'maxn' is deprecated");
  70. }
  71. #endif
  72. static int getn (lua_State *L) {
  73. lua_pushinteger(L, aux_getn(L, 1));
  74. return 1;
  75. }
  76. static int tinsert (lua_State *L) {
  77. int e = aux_getn(L, 1) + 1; /* first empty element */
  78. int pos; /* where to insert new element */
  79. switch (lua_gettop(L)) {
  80. case 2: { /* called with only 2 arguments */
  81. pos = e; /* insert new element at the end */
  82. break;
  83. }
  84. case 3: {
  85. int i;
  86. pos = luaL_checkint(L, 2); /* 2nd argument is the position */
  87. if (pos > e) e = pos; /* `grow' array if necessary */
  88. for (i = e; i > pos; i--) { /* move up elements */
  89. lua_rawgeti(L, 1, i-1);
  90. lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
  91. }
  92. break;
  93. }
  94. default: {
  95. return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
  96. }
  97. }
  98. lua_rawseti(L, 1, pos); /* t[pos] = v */
  99. return 0;
  100. }
  101. static int tremove (lua_State *L) {
  102. int e = aux_getn(L, 1);
  103. int pos = luaL_optint(L, 2, e);
  104. if (!(1 <= pos && pos <= e)) /* position is outside bounds? */
  105. return 0; /* nothing to remove */
  106. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  107. for ( ;pos<e; pos++) {
  108. lua_rawgeti(L, 1, pos+1);
  109. lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
  110. }
  111. lua_pushnil(L);
  112. lua_rawseti(L, 1, e); /* t[e] = nil */
  113. return 1;
  114. }
  115. static void addfield (lua_State *L, luaL_Buffer *b, int i) {
  116. lua_rawgeti(L, 1, i);
  117. if (!lua_isstring(L, -1))
  118. luaL_error(L, "invalid value (%s) at index %d in table for "
  119. LUA_QL("concat"), luaL_typename(L, -1), i);
  120. luaL_addvalue(b);
  121. }
  122. static int tconcat (lua_State *L) {
  123. luaL_Buffer b;
  124. size_t lsep;
  125. int i, last;
  126. const char *sep = luaL_optlstring(L, 2, "", &lsep);
  127. luaL_checktype(L, 1, LUA_TTABLE);
  128. i = luaL_optint(L, 3, 1);
  129. last = luaL_opt(L, luaL_checkint, 4, (int)lua_rawlen(L, 1));
  130. luaL_buffinit(L, &b);
  131. for (; i < last; i++) {
  132. addfield(L, &b, i);
  133. luaL_addlstring(&b, sep, lsep);
  134. }
  135. if (i == last) /* add last value (if interval was not empty) */
  136. addfield(L, &b, i);
  137. luaL_pushresult(&b);
  138. return 1;
  139. }
  140. /*
  141. ** {======================================================
  142. ** Pack/unpack
  143. ** =======================================================
  144. */
  145. static int pack (lua_State *L) {
  146. int top = lua_gettop(L);
  147. lua_createtable(L, top, 1); /* create result table */
  148. lua_pushinteger(L, top); /* number of elements */
  149. lua_setfield(L, -2, "n"); /* t.n = number of elements */
  150. if (top > 0) { /* at least one element? */
  151. lua_pushvalue(L, 1);
  152. lua_rawseti(L, -2, 1); /* insert first element */
  153. lua_replace(L, 1); /* move table into its position (index 1) */
  154. for (; top >= 2; top--) /* assign other elements */
  155. lua_rawseti(L, 1, top);
  156. }
  157. return 1;
  158. }
  159. static int unpack (lua_State *L) {
  160. int i, e, n;
  161. luaL_checktype(L, 1, LUA_TTABLE);
  162. i = luaL_optint(L, 2, 1);
  163. e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1));
  164. if (i > e) return 0; /* empty range */
  165. n = e - i + 1; /* number of elements */
  166. if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
  167. return luaL_error(L, "too many results to unpack");
  168. lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
  169. while (i++ < e) /* push arg[i + 1...e] */
  170. lua_rawgeti(L, 1, i);
  171. return n;
  172. }
  173. /* }====================================================== */
  174. /*
  175. ** {======================================================
  176. ** Quicksort
  177. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  178. ** Addison-Wesley, 1993.)
  179. ** =======================================================
  180. */
  181. static void set2 (lua_State *L, int i, int j) {
  182. lua_rawseti(L, 1, i);
  183. lua_rawseti(L, 1, j);
  184. }
  185. static int sort_comp (lua_State *L, int a, int b) {
  186. if (!lua_isnil(L, 2)) { /* function? */
  187. int res;
  188. lua_pushvalue(L, 2);
  189. lua_pushvalue(L, a-1); /* -1 to compensate function */
  190. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  191. lua_call(L, 2, 1);
  192. res = lua_toboolean(L, -1);
  193. lua_pop(L, 1);
  194. return res;
  195. }
  196. else /* a < b? */
  197. return lua_compare(L, a, b, LUA_OPLT);
  198. }
  199. static void auxsort (lua_State *L, int l, int u) {
  200. while (l < u) { /* for tail recursion */
  201. int i, j;
  202. /* sort elements a[l], a[(l+u)/2] and a[u] */
  203. lua_rawgeti(L, 1, l);
  204. lua_rawgeti(L, 1, u);
  205. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  206. set2(L, l, u); /* swap a[l] - a[u] */
  207. else
  208. lua_pop(L, 2);
  209. if (u-l == 1) break; /* only 2 elements */
  210. i = (l+u)/2;
  211. lua_rawgeti(L, 1, i);
  212. lua_rawgeti(L, 1, l);
  213. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  214. set2(L, i, l);
  215. else {
  216. lua_pop(L, 1); /* remove a[l] */
  217. lua_rawgeti(L, 1, u);
  218. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  219. set2(L, i, u);
  220. else
  221. lua_pop(L, 2);
  222. }
  223. if (u-l == 2) break; /* only 3 elements */
  224. lua_rawgeti(L, 1, i); /* Pivot */
  225. lua_pushvalue(L, -1);
  226. lua_rawgeti(L, 1, u-1);
  227. set2(L, i, u-1);
  228. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  229. i = l; j = u-1;
  230. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  231. /* repeat ++i until a[i] >= P */
  232. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  233. if (i>=u) luaL_error(L, "invalid order function for sorting");
  234. lua_pop(L, 1); /* remove a[i] */
  235. }
  236. /* repeat --j until a[j] <= P */
  237. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  238. if (j<=l) luaL_error(L, "invalid order function for sorting");
  239. lua_pop(L, 1); /* remove a[j] */
  240. }
  241. if (j<i) {
  242. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  243. break;
  244. }
  245. set2(L, i, j);
  246. }
  247. lua_rawgeti(L, 1, u-1);
  248. lua_rawgeti(L, 1, i);
  249. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  250. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  251. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  252. if (i-l < u-i) {
  253. j=l; i=i-1; l=i+2;
  254. }
  255. else {
  256. j=i+1; i=u; u=j-2;
  257. }
  258. auxsort(L, j, i); /* call recursively the smaller one */
  259. } /* repeat the routine for the larger one */
  260. }
  261. static int sort (lua_State *L) {
  262. int n = aux_getn(L, 1);
  263. luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
  264. if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
  265. luaL_checktype(L, 2, LUA_TFUNCTION);
  266. lua_settop(L, 2); /* make sure there is two arguments */
  267. auxsort(L, 1, n);
  268. return 0;
  269. }
  270. /* }====================================================== */
  271. static const luaL_Reg tab_funcs[] = {
  272. {"concat", tconcat},
  273. {"foreach", foreach},
  274. {"foreachi", foreachi},
  275. {"getn", getn},
  276. {"maxn", maxn},
  277. {"insert", tinsert},
  278. {"pack", pack},
  279. {"unpack", unpack},
  280. {"remove", tremove},
  281. {"sort", sort},
  282. {NULL, NULL}
  283. };
  284. LUAMOD_API int luaopen_table (lua_State *L) {
  285. luaL_newlib(L, tab_funcs);
  286. #if defined(LUA_COMPAT_UNPACK)
  287. /* _G.unpack = table.unpack */
  288. lua_getfield(L, -1, "unpack");
  289. lua_setglobal(L, "unpack");
  290. #endif
  291. return 1;
  292. }