ltablib.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. ** $Id: ltablib.c,v 1.44 2008/04/07 18:43:00 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_objlen(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 foreachcont (lua_State *L) {
  29. for (;;) {
  30. if (!lua_isnil(L, -1))
  31. return 1;
  32. lua_pop(L, 2); /* remove value and result */
  33. if (lua_next(L, 1) == 0) /* no more elements? */
  34. return 0;
  35. lua_pushvalue(L, 2); /* function */
  36. lua_pushvalue(L, -3); /* key */
  37. lua_pushvalue(L, -3); /* value */
  38. lua_callcont(L, 2, 1, &foreachcont);
  39. }
  40. }
  41. static int foreach (lua_State *L) {
  42. luaL_checktype(L, 1, LUA_TTABLE);
  43. luaL_checktype(L, 2, LUA_TFUNCTION);
  44. lua_pushnil(L); /* first key */
  45. lua_pushnil(L); /* first value */
  46. lua_pushnil(L); /* first "return" */
  47. return foreachcont(L);
  48. }
  49. static int maxn (lua_State *L) {
  50. lua_Number max = 0;
  51. luaL_checktype(L, 1, LUA_TTABLE);
  52. lua_pushnil(L); /* first key */
  53. while (lua_next(L, 1)) {
  54. lua_pop(L, 1); /* remove value */
  55. if (lua_type(L, -1) == LUA_TNUMBER) {
  56. lua_Number v = lua_tonumber(L, -1);
  57. if (v > max) max = v;
  58. }
  59. }
  60. lua_pushnumber(L, max);
  61. return 1;
  62. }
  63. static int getn (lua_State *L) {
  64. lua_pushinteger(L, aux_getn(L, 1));
  65. return 1;
  66. }
  67. static int setn (lua_State *L) {
  68. return luaL_error(L, LUA_QL("setn") " is obsolete");
  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. lua_rawseti(L, 1, pos); /* t[pos] = v */
  93. return 0;
  94. }
  95. static int tremove (lua_State *L) {
  96. int e = aux_getn(L, 1);
  97. int pos = luaL_optint(L, 2, e);
  98. if (!(1 <= pos && pos <= e)) /* position is outside bounds? */
  99. return 0; /* nothing to remove */
  100. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  101. for ( ;pos<e; pos++) {
  102. lua_rawgeti(L, 1, pos+1);
  103. lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
  104. }
  105. lua_pushnil(L);
  106. lua_rawseti(L, 1, e); /* t[e] = nil */
  107. return 1;
  108. }
  109. static void addfield (lua_State *L, luaL_Buffer *b, int i) {
  110. lua_rawgeti(L, 1, i);
  111. if (!lua_isstring(L, -1))
  112. luaL_error(L, "invalid value (%s) at index %d in table for "
  113. LUA_QL("concat"), luaL_typename(L, -1), i);
  114. luaL_addvalue(b);
  115. }
  116. static int tconcat (lua_State *L) {
  117. luaL_Buffer b;
  118. size_t lsep;
  119. int i, last;
  120. const char *sep = luaL_optlstring(L, 2, "", &lsep);
  121. luaL_checktype(L, 1, LUA_TTABLE);
  122. i = luaL_optint(L, 3, 1);
  123. last = luaL_opt(L, luaL_checkint, 4, (int)lua_objlen(L, 1));
  124. luaL_buffinit(L, &b);
  125. for (; i < last; i++) {
  126. addfield(L, &b, i);
  127. luaL_addlstring(&b, sep, lsep);
  128. }
  129. if (i == last) /* add last value (if interval was not empty) */
  130. addfield(L, &b, i);
  131. luaL_pushresult(&b);
  132. return 1;
  133. }
  134. /*
  135. ** {======================================================
  136. ** Quicksort
  137. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  138. ** Addison-Wesley, 1993.)
  139. */
  140. static void set2 (lua_State *L, int i, int j) {
  141. lua_rawseti(L, 1, i);
  142. lua_rawseti(L, 1, j);
  143. }
  144. static int sort_comp (lua_State *L, int a, int b) {
  145. if (!lua_isnil(L, 2)) { /* function? */
  146. int res;
  147. lua_pushvalue(L, 2);
  148. lua_pushvalue(L, a-1); /* -1 to compensate function */
  149. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  150. lua_call(L, 2, 1);
  151. res = lua_toboolean(L, -1);
  152. lua_pop(L, 1);
  153. return res;
  154. }
  155. else /* a < b? */
  156. return lua_lessthan(L, a, b);
  157. }
  158. static void auxsort (lua_State *L, int l, int u) {
  159. while (l < u) { /* for tail recursion */
  160. int i, j;
  161. /* sort elements a[l], a[(l+u)/2] and a[u] */
  162. lua_rawgeti(L, 1, l);
  163. lua_rawgeti(L, 1, u);
  164. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  165. set2(L, l, u); /* swap a[l] - a[u] */
  166. else
  167. lua_pop(L, 2);
  168. if (u-l == 1) break; /* only 2 elements */
  169. i = (l+u)/2;
  170. lua_rawgeti(L, 1, i);
  171. lua_rawgeti(L, 1, l);
  172. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  173. set2(L, i, l);
  174. else {
  175. lua_pop(L, 1); /* remove a[l] */
  176. lua_rawgeti(L, 1, u);
  177. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  178. set2(L, i, u);
  179. else
  180. lua_pop(L, 2);
  181. }
  182. if (u-l == 2) break; /* only 3 elements */
  183. lua_rawgeti(L, 1, i); /* Pivot */
  184. lua_pushvalue(L, -1);
  185. lua_rawgeti(L, 1, u-1);
  186. set2(L, i, u-1);
  187. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  188. i = l; j = u-1;
  189. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  190. /* repeat ++i until a[i] >= P */
  191. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  192. if (i>=u) luaL_error(L, "invalid order function for sorting");
  193. lua_pop(L, 1); /* remove a[i] */
  194. }
  195. /* repeat --j until a[j] <= P */
  196. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  197. if (j<=l) luaL_error(L, "invalid order function for sorting");
  198. lua_pop(L, 1); /* remove a[j] */
  199. }
  200. if (j<i) {
  201. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  202. break;
  203. }
  204. set2(L, i, j);
  205. }
  206. lua_rawgeti(L, 1, u-1);
  207. lua_rawgeti(L, 1, i);
  208. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  209. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  210. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  211. if (i-l < u-i) {
  212. j=l; i=i-1; l=i+2;
  213. }
  214. else {
  215. j=i+1; i=u; u=j-2;
  216. }
  217. auxsort(L, j, i); /* call recursively the smaller one */
  218. } /* repeat the routine for the larger one */
  219. }
  220. static int sort (lua_State *L) {
  221. int n = aux_getn(L, 1);
  222. luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
  223. if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
  224. luaL_checktype(L, 2, LUA_TFUNCTION);
  225. lua_settop(L, 2); /* make sure there is two arguments */
  226. auxsort(L, 1, n);
  227. return 0;
  228. }
  229. /* }====================================================== */
  230. static const luaL_Reg tab_funcs[] = {
  231. {"concat", tconcat},
  232. {"foreach", foreach},
  233. {"foreachi", foreachi},
  234. {"getn", getn},
  235. {"maxn", maxn},
  236. {"insert", tinsert},
  237. {"remove", tremove},
  238. {"setn", setn},
  239. {"sort", sort},
  240. {NULL, NULL}
  241. };
  242. LUALIB_API int luaopen_table (lua_State *L) {
  243. luaL_register(L, LUA_TABLIBNAME, tab_funcs);
  244. return 1;
  245. }