ltablib.c 8.8 KB

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