ltablib.c 8.2 KB

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