ltablib.c 7.6 KB

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