[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A very basic thing I don't get
- From: Kristofer Karlsson <kristofer.karlsson@...>
- Date: Sat, 1 Oct 2011 21:01:53 +0200
Also, I saw a confusing implementation of the length operator in kahlua:
public final int len() {
int high = keys.length;
int low = 0;
while (low < high) {
int middle = (high + low + 1) >> 1;
Double key = LuaState.toDouble(middle);
Object value = rawget(key);
if (value == null) {
high = middle - 1;
} else {
low = middle;
}
}
return low;
}
It's obviously a binary search to find the highest key. But does it
even work? If there are nils in the table, it begins to act randomly.
It may, among others, return the key before the lowest nil - or the
highest key in the array.
Sorry about that. That is a bug in an old version of Kahlua.
The latest version starts with a binary search and falls back to linear probing if it fails. Sort of like regular Lua.
This is the latest version for Kahlua 1: http://code.google.com/p/kahlua/source/browse/trunk/src/se/krka/kahlua/vm/LuaTableImpl.java
For Kahlua 2, see this the method len in https://github.com/krka/kahlua2/blob/master/core/src/se/krka/kahlua/vm/KahluaUtil.java