ltablib.c 7.2 KB

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