Improve ipairs() implementation.

This commit is contained in:
James Roseborough
2010-08-13 21:03:05 +00:00
parent 51202c1445
commit f266c0079b
2 changed files with 8 additions and 22 deletions

View File

@@ -354,13 +354,12 @@ public class LuaTable extends LuaValue {
/**
* Get the next element after a particular key in the
* contiguous array part of a table
* @return key,value or nil
* @return key,value or none
*/
public Varargs inext(LuaValue key) {
int i = key.optint(0);
return i<0 || i>=array.length || array[i]==null?
NIL:
varargsOf(LuaInteger.valueOf(i+1),array[i]);
int k = key.checkint() + 1;
LuaValue v = rawget(k);
return v.isnil()? NONE: varargsOf(LuaInteger.valueOf(k),v);
}
/**
@@ -385,12 +384,10 @@ public class LuaTable extends LuaValue {
* @param func
*/
public LuaValue foreachi(LuaValue func) {
Varargs n;
LuaValue k = NIL;
LuaValue v;
while ( !(k = ((n = inext(k)).arg1())).isnil() )
if ( ! (v = func.call(k, n.arg(2))).isnil() )
return v;
LuaValue v,r;
for ( int k=0; !(v = rawget(++k)).isnil(); )
if ( ! (r = func.call(valueOf(k), v)).isnil() )
return r;
return NIL;
}

View File

@@ -170,17 +170,6 @@ public class WeakTable extends LuaTable {
}
}
/**
* Get the next element after a particular key in the
* contiguous array part of a table
* @return key,value or nil
*/
public Varargs inext(LuaValue key) {
int k = key.optint(0)+1;
LuaValue v = this.rawget(k);
return v.isnil()? NIL: varargsOf(valueOf(k),v);
}
// ----------------- sort support -----------------------------
public void sort(final LuaValue comparator) {
super.sort( new TwoArgFunction() {