675 lines
18 KiB
C++
675 lines
18 KiB
C++
#include "table.hpp"
|
|
#include "source.hpp"
|
|
|
|
void table_findtables_i(LuaStack &LS0, LuaSlot root, LuaSlot tabcount) {
|
|
lua_State *L = LS0.state();
|
|
LuaVar key, val, tab, count;
|
|
LuaStack LS(L, key, val, tab, count);
|
|
|
|
LS.newtable(tabcount);
|
|
int top = lua_gettop(L);
|
|
if (LS.istable(root)) {
|
|
lua_pushvalue(L, root.index());
|
|
}
|
|
while (lua_gettop(L) > top) {
|
|
lua_checkstack(L, 10);
|
|
lua_replace(L, tab.index());
|
|
LS.rawget(count, tabcount, tab);
|
|
if (LS.isnil(count)) {
|
|
LS.rawset(tabcount, tab, 1);
|
|
LS.set(key, LuaNil);
|
|
while (LS.next(tab, key, val)) {
|
|
if (LS.istable(key)) {
|
|
lua_pushvalue(L, key.index());
|
|
}
|
|
if (LS.istable(val)) {
|
|
lua_pushvalue(L, val.index());
|
|
}
|
|
}
|
|
LS.getmetatable(val, tab);
|
|
if (LS.istable(val)) {
|
|
lua_pushvalue(L, val.index());
|
|
}
|
|
} else {
|
|
LS.rawset(tabcount, tab, LS.ckint(count) + 1);
|
|
}
|
|
}
|
|
LS.result();
|
|
}
|
|
|
|
LuaDefine(table_findtables, "c") {
|
|
LuaArg root;
|
|
LuaRet tabcount;
|
|
LuaStack LS(L, root, tabcount);
|
|
table_findtables_i(LS, root, tabcount);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(table_getregistry, "f") {
|
|
LuaArg key;
|
|
LuaRet result;
|
|
LuaStack LS(L, key, result);
|
|
LS.rawget(result, LuaRegistry, key);
|
|
return LS.result();
|
|
}
|
|
|
|
bool table_equal(LuaStack &LS, LuaSlot t1, LuaSlot t2) {
|
|
lua_State *L = LS.state();
|
|
LS.checktable(t1);
|
|
LS.checktable(t2);
|
|
lua_pushnil(L);
|
|
int total1 = 0;
|
|
while (lua_next(L, t1.index()) != 0) {
|
|
lua_pushvalue(L, -2); // k v1 k
|
|
lua_rawget(L, t2.index()); // k v1 v2
|
|
if (!lua_rawequal(L, -1, -2)) {
|
|
return false;
|
|
}
|
|
lua_pop(L, 2);
|
|
total1 += 1;
|
|
}
|
|
int total2 = 0;
|
|
lua_pushnil(L);
|
|
while (lua_next(L, t2.index()) != 0) {
|
|
lua_pop(L, 1);
|
|
total2 += 1;
|
|
}
|
|
return total1 == total2;
|
|
}
|
|
|
|
LuaDefine(table_equal, "c") {
|
|
LuaArg t1, t2;
|
|
LuaRet eql;
|
|
LuaStack LS(L, t1, t2, eql);
|
|
LS.set(eql, table_equal(LS, t1, t2));
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(table_findremove, "c") {
|
|
luaL_checktype(L, -2, LUA_TTABLE);
|
|
int src = 1;
|
|
int dst = 1;
|
|
while (true) {
|
|
lua_pushinteger(L, src);
|
|
lua_rawget(L, -3);
|
|
if (lua_rawequal(L, -1, -2)) {
|
|
src++;
|
|
lua_pop(L, 1);
|
|
} else if (lua_isnil(L, -1)) {
|
|
lua_pop(L, 1);
|
|
int removed = src - dst;
|
|
while (src > dst) {
|
|
lua_pushinteger(L, dst);
|
|
lua_pushnil(L);
|
|
lua_rawset(L, -4);
|
|
dst++;
|
|
}
|
|
lua_pop(L, 2);
|
|
lua_pushinteger(L, removed);
|
|
return 1;
|
|
} else {
|
|
if (src > dst) {
|
|
lua_pushinteger(L, dst);
|
|
lua_insert(L, lua_gettop(L) - 1);
|
|
lua_rawset(L, -4);
|
|
} else {
|
|
lua_pop(L, 1);
|
|
}
|
|
src++;
|
|
dst++;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
LuaDefine(table_push, "c") {
|
|
luaL_checktype(L, -2, LUA_TTABLE);
|
|
int len = lua_rawlen(L, -2);
|
|
lua_pushinteger(L, len+1);
|
|
lua_pushvalue(L, -2);
|
|
lua_rawset(L, -4);
|
|
lua_pop(L, 2);
|
|
return 0;
|
|
}
|
|
|
|
LuaDefine(table_find, "c") {
|
|
luaL_checktype(L, -2, LUA_TTABLE);
|
|
for (int i = 1; ; i++) {
|
|
lua_pushinteger(L, i);
|
|
lua_rawget(L, -3);
|
|
if (lua_rawequal(L, -1, -2)) {
|
|
lua_pop(L, 3);
|
|
lua_pushinteger(L, i);
|
|
return 1;
|
|
} else if (lua_isnil(L, -1)) {
|
|
lua_pop(L, 3);
|
|
lua_pushnil(L);
|
|
return 1;
|
|
} else {
|
|
lua_pop(L, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
LuaDefine(table_empty, "c") {
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
lua_pushnil(L);
|
|
if (lua_next(L, -2) != 0) {
|
|
lua_pop(L, 3);
|
|
lua_pushboolean(L, 0);
|
|
return 1;
|
|
} else {
|
|
lua_pop(L, 1);
|
|
lua_pushboolean(L, 1);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
LuaDefine(table_count, "c") {
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
int total = lua_nkeys(L, -1);
|
|
lua_pushinteger(L, total);
|
|
return 1;
|
|
}
|
|
|
|
void table_clear(LuaStack &LS0, LuaSlot table) {
|
|
LS0.cleartable(table);
|
|
}
|
|
|
|
LuaDefine(table_clear, "c") {
|
|
LuaArg tab;
|
|
LuaStack LS(L, tab);
|
|
table_clear(LS, tab);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(table_getflagbits, "c") {
|
|
LuaArg tab;
|
|
LuaRet bits;
|
|
LuaStack LS(L, tab, bits);
|
|
uint16_t ubits = lua_getflagbits(L, tab.index());
|
|
LS.set(bits, ubits);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(table_setflagbits, "c") {
|
|
LuaArg tab, bits;
|
|
LuaStack LS(L, tab, bits);
|
|
uint16_t ubits = LS.ckinteger(bits);
|
|
lua_setflagbits(L, tab.index(), ubits);
|
|
return LS.result();
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
//
|
|
// Deque operators.
|
|
//
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
#define DEQUE_LEFT 1
|
|
#define DEQUE_FILL 2
|
|
#define DEQUE_MAX 3
|
|
#define DEQUE_BASE 4
|
|
|
|
void deque_checktype(lua_State *L, int slot, int type) {
|
|
if (lua_type(L, slot) != type) {
|
|
luaL_error(L, "object is not a valid deque");
|
|
}
|
|
}
|
|
|
|
void deque_get_info(lua_State *L, int deque, int *left, int *fill, int *max) {
|
|
luaL_checktype(L, deque, LUA_TTABLE);
|
|
if (left) {
|
|
lua_rawgeti(L, deque, DEQUE_LEFT);
|
|
deque_checktype(L, -1, LUA_TNUMBER);
|
|
*left = lua_tointeger(L, -1);
|
|
lua_pop(L, 1);
|
|
}
|
|
if (fill) {
|
|
lua_rawgeti(L, deque, DEQUE_FILL);
|
|
deque_checktype(L, -1, LUA_TNUMBER);
|
|
*fill = lua_tointeger(L, -1);
|
|
lua_pop(L, 1);
|
|
}
|
|
if (max) {
|
|
lua_rawgeti(L, deque, DEQUE_MAX);
|
|
deque_checktype(L, -1, LUA_TNUMBER);
|
|
*max = lua_tointeger(L, -1);
|
|
lua_pop(L, 1);
|
|
}
|
|
}
|
|
|
|
void deque_put_info(lua_State *L, int deque, int *left, int *fill, int *max) {
|
|
if (left) {
|
|
lua_pushinteger(L, *left);
|
|
lua_rawseti(L, deque, DEQUE_LEFT);
|
|
}
|
|
if (fill) {
|
|
lua_pushinteger(L, *fill);
|
|
lua_rawseti(L, deque, DEQUE_FILL);
|
|
}
|
|
if (max) {
|
|
lua_pushinteger(L, *max);
|
|
lua_rawseti(L, deque, DEQUE_MAX);
|
|
}
|
|
}
|
|
|
|
int deque_make_room(lua_State *L, int deque, int left, int fill, int max) {
|
|
if (fill == max) {
|
|
for (int i = 0; i < left; i++) {
|
|
lua_rawgeti(L, deque, DEQUE_BASE + i);
|
|
lua_rawseti(L, deque, DEQUE_BASE + i + max);
|
|
lua_pushinteger(L, 0);
|
|
lua_rawseti(L, deque, DEQUE_BASE + i);
|
|
}
|
|
for (int i = left; i < max; i++) {
|
|
lua_pushinteger(L, 0);
|
|
lua_rawseti(L, deque, DEQUE_BASE + i + max);
|
|
}
|
|
max *= 2;
|
|
lua_pushinteger(L, max);
|
|
lua_rawseti(L, deque, DEQUE_MAX);
|
|
}
|
|
return max;
|
|
}
|
|
|
|
LuaDefine(deque_create, "c") {
|
|
LuaRet rdeque;
|
|
LuaVar classobj;
|
|
LuaStack LS(L, rdeque, classobj);
|
|
const int imax = 4;
|
|
LS.createtable(rdeque, DEQUE_BASE + imax - 1, 0);
|
|
LS.rawseti(rdeque, DEQUE_LEFT, 0);
|
|
LS.rawseti(rdeque, DEQUE_FILL, 0);
|
|
LS.rawseti(rdeque, DEQUE_MAX, imax);
|
|
for (int i = 0; i < imax; i++) {
|
|
LS.rawseti(rdeque, DEQUE_BASE + i, 0);
|
|
}
|
|
LS.makeclass(classobj, "deque");
|
|
LS.setmetatable(rdeque, classobj);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_pushl, "c") {
|
|
LuaArg deque, elt;
|
|
LuaStack LS(L, deque, elt);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
max = deque_make_room(L, deque.index(), left, fill, max);
|
|
int target = (left - 1) & (max-1);
|
|
LS.rawseti(deque, DEQUE_BASE + target, elt);
|
|
fill += 1;
|
|
left = (left - 1) & (max - 1);
|
|
deque_put_info(L, deque.index(), &left, &fill, NULL);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_pushr, "c") {
|
|
LuaArg deque, elt;
|
|
LuaStack LS(L, deque, elt);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
max = deque_make_room(L, deque.index(), left, fill, max);
|
|
int target = (left + fill) & (max-1);
|
|
LS.rawseti(deque, DEQUE_BASE + target, elt);
|
|
fill += 1;
|
|
deque_put_info(L, deque.index(), NULL, &fill, NULL);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_popl, "c") {
|
|
LuaArg deque;
|
|
LuaRet result;
|
|
LuaStack LS(L, deque, result);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
if (fill == 0) {
|
|
LS.set(result, LuaNil);
|
|
return LS.result();
|
|
}
|
|
LS.rawgeti(result, deque, DEQUE_BASE + left);
|
|
LS.rawseti(deque, DEQUE_BASE + left, 0);
|
|
left = (left + 1) & (max - 1);
|
|
fill -= 1;
|
|
deque_put_info(L, deque.index(), &left, &fill, NULL);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_popr, "c") {
|
|
LuaArg deque;
|
|
LuaRet result;
|
|
LuaStack LS(L, deque, result);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
if (fill == 0) {
|
|
LS.set(result, LuaNil);
|
|
return LS.result();
|
|
}
|
|
int target = (left + fill - 1) & (max - 1);
|
|
LS.rawgeti(result, deque, DEQUE_BASE + target);
|
|
LS.rawseti(deque, DEQUE_BASE + target, 0);
|
|
fill -= 1;
|
|
deque_put_info(L, deque.index(), NULL, &fill, NULL);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_nthl, "c") {
|
|
LuaArg deque, nn;
|
|
LuaRet result;
|
|
LuaStack LS(L, deque, nn, result);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
int n = LS.ckint(nn);
|
|
if ((n < 1) || (n > fill)) {
|
|
LS.set(result, LuaNil);
|
|
return LS.result();
|
|
}
|
|
int target = (left + n - 1) & (max - 1);
|
|
LS.rawgeti(result, deque, DEQUE_BASE + target);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_nthr, "c") {
|
|
LuaArg deque, nn;
|
|
LuaRet result;
|
|
LuaStack LS(L, deque, nn, result);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
int n = LS.ckint(nn);
|
|
if ((n < 1) || (n > fill)) {
|
|
LS.set(result, LuaNil);
|
|
return LS.result();
|
|
}
|
|
int target = (left + fill - n) & (max - 1);
|
|
LS.rawgeti(result, deque, DEQUE_BASE + target);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_setl, "c") {
|
|
LuaArg deque, nn, val;
|
|
LuaStack LS(L, deque, nn, val);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
int n = LS.ckint(nn);
|
|
if ((n < 1) || (n > fill)) {
|
|
luaL_error(L, "invalid index");
|
|
return LS.result();
|
|
}
|
|
int target = (left + n - 1) & (max - 1);
|
|
LS.rawseti(deque, DEQUE_BASE + target, val);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_setr, "c") {
|
|
LuaArg deque, nn, val;
|
|
LuaStack LS(L, deque, nn, val);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
int n = LS.ckint(nn);
|
|
if ((n < 1) || (n > fill)) {
|
|
luaL_error(L, "invalid index");
|
|
return LS.result();
|
|
}
|
|
int target = (left + fill - n) & (max - 1);
|
|
LS.rawseti(deque, DEQUE_BASE + target, val);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_findl, "c") {
|
|
LuaArg deque, val;
|
|
LuaRet pos;
|
|
LuaVar check;
|
|
LuaStack LS(L, deque, val, pos, check);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
for (int i = 0; i < fill; i++) {
|
|
int index = (left + i) & (max - 1);
|
|
LS.rawgeti(check, deque, DEQUE_BASE + index);
|
|
if (LS.rawequal(check, val)) {
|
|
LS.set(pos, i + 1);
|
|
return LS.result();
|
|
}
|
|
}
|
|
LS.set(pos, LuaNil);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_findr, "c") {
|
|
LuaArg deque, val;
|
|
LuaRet pos;
|
|
LuaVar check;
|
|
LuaStack LS(L, deque, val, pos, check);
|
|
int left, fill, max;
|
|
deque_get_info(L, deque.index(), &left, &fill, &max);
|
|
int base = left + fill - 1;
|
|
for (int i = 0; i < fill; i++) {
|
|
int index = (base - i) & (max - 1);
|
|
LS.rawgeti(check, deque, DEQUE_BASE + index);
|
|
if (LS.rawequal(check, val)) {
|
|
LS.set(pos, i + 1);
|
|
return LS.result();
|
|
}
|
|
}
|
|
LS.set(pos, LuaNil);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(deque_size, "c") {
|
|
LuaArg deque;
|
|
LuaRet size;
|
|
LuaStack LS(L, deque, size);
|
|
LS.checktable(deque);
|
|
LS.rawgeti(size, deque, DEQUE_FILL);
|
|
LS.checknumber(size);
|
|
return LS.result();
|
|
}
|
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
//
|
|
// table_getpairs
|
|
//
|
|
// Given a table, return all the pairs in the table as a sequence.
|
|
// The resulting sequence contains a 1 in the first position,
|
|
// followed by the pairs, like so:
|
|
//
|
|
// sortedpairs({a=1,b=2,c=3}) => {1,"a",1,"b",2,"c",3}
|
|
//
|
|
// Note that this function is not directly exposed to lua.
|
|
// It's exposed only through the 'sortedpairs' iterator.
|
|
//
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
static void pushkey (lua_State *L, int tab, int i) {
|
|
lua_rawgeti(L, tab, i*2);
|
|
}
|
|
|
|
static void push2keys (lua_State *L, int tab, int i, int j) {
|
|
lua_rawgeti(L, tab, i*2);
|
|
lua_rawgeti(L, tab, j*2);
|
|
}
|
|
|
|
static void pop2keys (lua_State *L, int tab, int i, int j) {
|
|
lua_rawseti(L, tab, i*2);
|
|
lua_rawseti(L, tab, j*2);
|
|
}
|
|
|
|
static void swap2values (lua_State *L, int tab, int i, int j) {
|
|
lua_rawgeti(L, tab, i*2+1);
|
|
lua_rawgeti(L, tab, j*2+1);
|
|
lua_rawseti(L, tab, i*2+1);
|
|
lua_rawseti(L, tab, j*2+1);
|
|
}
|
|
|
|
static void auxsort (lua_State *L, int tab, int l, int u) {
|
|
while (l < u) { /* for tail recursion */
|
|
int i, j;
|
|
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
|
push2keys(L, tab, l, u);
|
|
if (lua_genlt(L, -1, -2)) {
|
|
pop2keys(L, tab, l, u);
|
|
swap2values(L, tab, l, u);
|
|
} else {
|
|
lua_pop(L, 2);
|
|
}
|
|
if (u - l == 1) break; /* only 2 elements */
|
|
i = (l + u) / 2;
|
|
push2keys(L, tab, i, l);
|
|
if (lua_genlt(L, -2, -1)) {
|
|
pop2keys(L, tab, i, l);
|
|
swap2values(L, tab, i, l);
|
|
} else {
|
|
lua_pop(L, 1); /* remove a[l] */
|
|
pushkey(L, tab, u);
|
|
if (lua_genlt(L, -1, -2)) {
|
|
pop2keys(L, tab, i, u);
|
|
swap2values(L, tab, i, u);
|
|
} else {
|
|
lua_pop(L, 2);
|
|
}
|
|
}
|
|
if (u - l == 2) break; /* only 3 elements */
|
|
/* put the pivot value on top of the stack and keep it there */
|
|
pushkey(L, tab, i);
|
|
/* move the pivot from i to u-1 */
|
|
lua_pushvalue(L, -1);
|
|
pushkey(L, tab, u-1);
|
|
pop2keys(L, tab, i, u-1);
|
|
swap2values(L, tab, i, u-1);
|
|
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
|
|
i = l;
|
|
j = u - 1;
|
|
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
|
/* repeat ++i until a[i] >= P */
|
|
while (pushkey(L, tab, ++i), lua_genlt(L, -1, -2)) {
|
|
if (i >= u) luaL_error(L, "invalid order function for sorting");
|
|
lua_pop(L, 1); /* remove a[i] */
|
|
}
|
|
/* repeat --j until a[j] <= P */
|
|
while (pushkey(L, tab, --j), lua_genlt(L, -3, -1)) {
|
|
if (j <= l) luaL_error(L, "invalid order function for sorting");
|
|
lua_pop(L, 1); /* remove a[j] */
|
|
}
|
|
if (j < i) {
|
|
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
|
|
break;
|
|
}
|
|
pop2keys(L, tab, i, j);
|
|
swap2values(L, tab, i, j);
|
|
}
|
|
push2keys(L, tab, u-1, i);
|
|
pop2keys(L, tab, u-1, i);
|
|
swap2values(L, tab, u-1, i);
|
|
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
|
|
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
|
|
if (i - l < u - i) {
|
|
j = l;
|
|
i = i - 1;
|
|
l = i + 2;
|
|
} else {
|
|
j = i + 1;
|
|
i = u;
|
|
u = j - 2;
|
|
}
|
|
auxsort(L, tab, j, i); /* call recursively the smaller one */
|
|
} /* repeat the routine for the larger one */
|
|
}
|
|
|
|
static int table_getpairs(lua_State *L, bool sort, bool *unsortable) {
|
|
lua_checkstack(L, 40);
|
|
LuaArg tab;
|
|
LuaVar key, value;
|
|
LuaRet pairs;
|
|
LuaStack LS(L, tab, key, value, pairs);
|
|
if (unsortable != nullptr) {
|
|
*unsortable = false;
|
|
}
|
|
LS.checktable(tab);
|
|
// Count the total number of pairs.
|
|
// TODO: add lua_npairs to make this faster.
|
|
lua_pushnil(L);
|
|
int total = 0;
|
|
while (lua_next(L, tab.index()) != 0) {
|
|
int ktype = lua_type(L, -2);
|
|
if (ktype == LUA_TNUMBER || ktype == LUA_TSTRING || ktype == LUA_TBOOLEAN) {
|
|
total += 1;
|
|
}
|
|
lua_pop(L, 1);
|
|
}
|
|
// Create the table, store the initial 1.
|
|
LS.createtable(pairs, total * 2 + 1, 0);
|
|
LS.rawseti(pairs, 1, 1);
|
|
// Transfer the pairs into the sequence.
|
|
lua_pushnil(L);
|
|
int offset = 2;
|
|
while (lua_next(L, tab.index()) != 0) {
|
|
int ktype = lua_type(L, -2);
|
|
if (ktype == LUA_TNUMBER || ktype == LUA_TSTRING || ktype == LUA_TBOOLEAN) {
|
|
lua_pushvalue(L, -2); // K V K
|
|
lua_rawseti(L, pairs.index(), offset++);
|
|
lua_rawseti(L, pairs.index(), offset++);
|
|
} else {
|
|
if (unsortable != nullptr) {
|
|
*unsortable = true;
|
|
}
|
|
lua_pop(L, 1);
|
|
}
|
|
}
|
|
if (sort) {
|
|
auxsort(L, pairs.index(), 1, total);
|
|
}
|
|
return LS.result();
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
//
|
|
// Given a sortedpairs vector, return the (key, value) pairs
|
|
// one by one. The first element of the vector is used as a
|
|
// counter to keep track of our position.
|
|
//
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
LuaDefine(table_nextsortedpair, "c") {
|
|
if (lua_gettop(L) < 2) {
|
|
luaL_error(L, "Not enough arguments to nextpair");
|
|
}
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
lua_rawgeti(L, 1, 1);
|
|
lua_Integer i = luaL_checkinteger(L, -1);
|
|
lua_pop(L, 1);
|
|
lua_pushinteger(L, i+1);
|
|
lua_rawseti(L, 1, 1);
|
|
lua_rawgeti(L, 1, i*2);
|
|
if (lua_isnil(L, -1)) {
|
|
return 1;
|
|
} else {
|
|
lua_rawgeti(L, 1, i*2+1);
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
LuaDefine(table_sortedpairs, "c") {
|
|
LuaArg tab;
|
|
LuaRet closure, rtab, key;
|
|
LuaStack LS(L, tab, closure, rtab, key);
|
|
lua_pushvalue(L, tab.index());
|
|
bool unsortable;
|
|
table_getpairs(L, true, &unsortable);
|
|
if (unsortable) {
|
|
luaL_error(L, "Cannot iterate over a table with unsortable keys");
|
|
}
|
|
lua_replace(L, rtab.index());
|
|
LS.set(closure, lfn_table_nextsortedpair);
|
|
LS.set(key, LuaNil);
|
|
return LS.result();
|
|
}
|
|
|
|
LuaDefine(table_genlt, "f") {
|
|
LuaArg o1,o2;
|
|
LuaRet lt;
|
|
LuaStack LS(L, o1, o2, lt);
|
|
int ltf = lua_genlt(L, o1.index(), o2.index());
|
|
LS.set(lt, ltf ? true:false);
|
|
return LS.result();
|
|
}
|