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