PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Lua/lua-5.0.3/src/lib/ltablib.c

https://bitbucket.org/kctcynic/devsetup
C | 250 lines | 197 code | 35 blank | 18 comment | 38 complexity | 333622a7e232df800edb2597d8f209ee MD5 | raw file
  1. /*
  2. ** $Id: ltablib.c,v 1.21 2003/04/03 13:35:34 roberto Exp $
  3. ** Library for Table Manipulation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #define ltablib_c
  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 luaB_foreachi (lua_State *L) {
  13. int i;
  14. int n = aux_getn(L, 1);
  15. luaL_checktype(L, 2, LUA_TFUNCTION);
  16. for (i=1; i<=n; i++) {
  17. lua_pushvalue(L, 2); /* function */
  18. lua_pushnumber(L, (lua_Number)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 luaB_foreach (lua_State *L) {
  28. luaL_checktype(L, 1, LUA_TTABLE);
  29. luaL_checktype(L, 2, LUA_TFUNCTION);
  30. lua_pushnil(L); /* first key */
  31. for (;;) {
  32. if (lua_next(L, 1) == 0)
  33. return 0;
  34. lua_pushvalue(L, 2); /* function */
  35. lua_pushvalue(L, -3); /* key */
  36. lua_pushvalue(L, -3); /* value */
  37. lua_call(L, 2, 1);
  38. if (!lua_isnil(L, -1))
  39. return 1;
  40. lua_pop(L, 2); /* remove value and result */
  41. }
  42. }
  43. static int luaB_getn (lua_State *L) {
  44. lua_pushnumber(L, (lua_Number)aux_getn(L, 1));
  45. return 1;
  46. }
  47. static int luaB_setn (lua_State *L) {
  48. luaL_checktype(L, 1, LUA_TTABLE);
  49. luaL_setn(L, 1, luaL_checkint(L, 2));
  50. return 0;
  51. }
  52. static int luaB_tinsert (lua_State *L) {
  53. int v = lua_gettop(L); /* number of arguments */
  54. int n = aux_getn(L, 1) + 1;
  55. int pos; /* where to insert new element */
  56. if (v == 2) /* called with only 2 arguments */
  57. pos = n; /* insert new element at the end */
  58. else {
  59. pos = luaL_checkint(L, 2); /* 2nd argument is the position */
  60. if (pos > n) n = pos; /* `grow' array if necessary */
  61. v = 3; /* function may be called with more than 3 args */
  62. }
  63. luaL_setn(L, 1, n); /* new size */
  64. while (--n >= pos) { /* move up elements */
  65. lua_rawgeti(L, 1, n);
  66. lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
  67. }
  68. lua_pushvalue(L, v);
  69. lua_rawseti(L, 1, pos); /* t[pos] = v */
  70. return 0;
  71. }
  72. static int luaB_tremove (lua_State *L) {
  73. int n = aux_getn(L, 1);
  74. int pos = luaL_optint(L, 2, n);
  75. if (n <= 0) return 0; /* table is `empty' */
  76. luaL_setn(L, 1, n-1); /* t.n = n-1 */
  77. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  78. for ( ;pos<n; pos++) {
  79. lua_rawgeti(L, 1, pos+1);
  80. lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
  81. }
  82. lua_pushnil(L);
  83. lua_rawseti(L, 1, n); /* t[n] = nil */
  84. return 1;
  85. }
  86. static int str_concat (lua_State *L) {
  87. luaL_Buffer b;
  88. size_t lsep;
  89. const char *sep = luaL_optlstring(L, 2, "", &lsep);
  90. int i = luaL_optint(L, 3, 1);
  91. int n = luaL_optint(L, 4, 0);
  92. luaL_checktype(L, 1, LUA_TTABLE);
  93. if (n == 0) n = luaL_getn(L, 1);
  94. luaL_buffinit(L, &b);
  95. for (; i <= n; i++) {
  96. lua_rawgeti(L, 1, i);
  97. luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings");
  98. luaL_addvalue(&b);
  99. if (i != n)
  100. luaL_addlstring(&b, sep, lsep);
  101. }
  102. luaL_pushresult(&b);
  103. return 1;
  104. }
  105. /*
  106. ** {======================================================
  107. ** Quicksort
  108. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  109. ** Addison-Wesley, 1993.)
  110. */
  111. static void set2 (lua_State *L, int i, int j) {
  112. lua_rawseti(L, 1, i);
  113. lua_rawseti(L, 1, j);
  114. }
  115. static int sort_comp (lua_State *L, int a, int b) {
  116. if (!lua_isnil(L, 2)) { /* function? */
  117. int res;
  118. lua_pushvalue(L, 2);
  119. lua_pushvalue(L, a-1); /* -1 to compensate function */
  120. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  121. lua_call(L, 2, 1);
  122. res = lua_toboolean(L, -1);
  123. lua_pop(L, 1);
  124. return res;
  125. }
  126. else /* a < b? */
  127. return lua_lessthan(L, a, b);
  128. }
  129. static void auxsort (lua_State *L, int l, int u) {
  130. while (l < u) { /* for tail recursion */
  131. int i, j;
  132. /* sort elements a[l], a[(l+u)/2] and a[u] */
  133. lua_rawgeti(L, 1, l);
  134. lua_rawgeti(L, 1, u);
  135. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  136. set2(L, l, u); /* swap a[l] - a[u] */
  137. else
  138. lua_pop(L, 2);
  139. if (u-l == 1) break; /* only 2 elements */
  140. i = (l+u)/2;
  141. lua_rawgeti(L, 1, i);
  142. lua_rawgeti(L, 1, l);
  143. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  144. set2(L, i, l);
  145. else {
  146. lua_pop(L, 1); /* remove a[l] */
  147. lua_rawgeti(L, 1, u);
  148. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  149. set2(L, i, u);
  150. else
  151. lua_pop(L, 2);
  152. }
  153. if (u-l == 2) break; /* only 3 elements */
  154. lua_rawgeti(L, 1, i); /* Pivot */
  155. lua_pushvalue(L, -1);
  156. lua_rawgeti(L, 1, u-1);
  157. set2(L, i, u-1);
  158. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  159. i = l; j = u-1;
  160. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  161. /* repeat ++i until a[i] >= P */
  162. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  163. if (i>u) luaL_error(L, "invalid order function for sorting");
  164. lua_pop(L, 1); /* remove a[i] */
  165. }
  166. /* repeat --j until a[j] <= P */
  167. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  168. if (j<l) luaL_error(L, "invalid order function for sorting");
  169. lua_pop(L, 1); /* remove a[j] */
  170. }
  171. if (j<i) {
  172. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  173. break;
  174. }
  175. set2(L, i, j);
  176. }
  177. lua_rawgeti(L, 1, u-1);
  178. lua_rawgeti(L, 1, i);
  179. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  180. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  181. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  182. if (i-l < u-i) {
  183. j=l; i=i-1; l=i+2;
  184. }
  185. else {
  186. j=i+1; i=u; u=j-2;
  187. }
  188. auxsort(L, j, i); /* call recursively the smaller one */
  189. } /* repeat the routine for the larger one */
  190. }
  191. static int luaB_sort (lua_State *L) {
  192. int n = aux_getn(L, 1);
  193. luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
  194. if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
  195. luaL_checktype(L, 2, LUA_TFUNCTION);
  196. lua_settop(L, 2); /* make sure there is two arguments */
  197. auxsort(L, 1, n);
  198. return 0;
  199. }
  200. /* }====================================================== */
  201. static const luaL_reg tab_funcs[] = {
  202. {"concat", str_concat},
  203. {"foreach", luaB_foreach},
  204. {"foreachi", luaB_foreachi},
  205. {"getn", luaB_getn},
  206. {"setn", luaB_setn},
  207. {"sort", luaB_sort},
  208. {"insert", luaB_tinsert},
  209. {"remove", luaB_tremove},
  210. {NULL, NULL}
  211. };
  212. LUALIB_API int luaopen_table (lua_State *L) {
  213. luaL_openlib(L, LUA_TABLIBNAME, tab_funcs, 0);
  214. return 1;
  215. }