From 7ecd04a6526eb3f9825270b449ff99e75f4ee7b3 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 22 Apr 2008 05:30:00 +0000 Subject: [PATCH] Improve Java table length computation relative to C version --- src/core/org/luaj/vm/LTable.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/core/org/luaj/vm/LTable.java b/src/core/org/luaj/vm/LTable.java index 69910e68..192e8dd4 100644 --- a/src/core/org/luaj/vm/LTable.java +++ b/src/core/org/luaj/vm/LTable.java @@ -252,17 +252,16 @@ public class LTable extends LValue { * such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). */ public int luaLength() { - int j = array.length; - int k = hashKeys.length; - - // degenerate case - if ( j + k <= 0 ) - return 0; // find `i' and `j' such that i is present and j is not int i = 0; - for ( ++j; containsKey(j) && j < MAX_KEY; j*=2 ) - i = j; + int j = array.length; + if (j<=0 || containsKey(j)) { + if ( hashKeys.length == 0 ) + return j; + for ( ++j; containsKey(j) && j < MAX_KEY; j*=2 ) + i = j; + } // binary search while ( j - i > 1) {