lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Dec 31, 2010, at 7:07 AM, Gaspard Bucher wrote:

> I understand your point. I am not saying a table in Lua is this or that and I agree that it I have to use an arbitrary rule. I am currently using "no value in array" means hash:
> 
> if (lua_objlen(L, index) > 0)
>   pack_as_array(L, index);
> else
>   pack_as_hash(L, index);
> 
> I am also wondering if there is a faster method to get the hash size (needed to prepare the map):
> 
> for(lua_pushnil(L); lua_next(L, index) != 0; lua_pop(L, 1)) ++sz;

The overall time complexity will remain the same(*) -- remember that you will need to fetch and store each value -- so you could solve both problems by writing an analysis loop to run through the table once looking at the keys. A table is an array if all of its keys are positive integers and the maximum such integer is equal to the count of the number of keys.

Mark

(*) That said, it is often wise to be aware of the per element cost.