Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

287 lignes
7.5 KiB

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