PageRenderTime 64ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ltablib.cs

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