/script_binding/lua/lua/ltablib.c

http://ftk.googlecode.com/ · C · 287 lines · 230 code · 39 blank · 18 comment · 38 complexity · 25d290590652591f222de3bc384ce9a8 MD5 · raw file

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