lua-users home
lua-l archive

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


Quoth Gaspard Bucher <gaspard@teti.ch>, on 2010-12-31 16:07:29 +0100:
> 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);

Okay.  Good to know.  (I realize in retrospect that my response was a
bit neurotic, but the opposite very insistent misunderstanding has
occurred too many times in recent history.)

I would use "no keys other than integer keys N where 1 <= N <= #t".
You can combine this with getting the total number of keys, since
they're both full table iterations.  The rule you're using above can
lose pairs (assuming pack_as_array ignores everything outside the
list-like part) in the case of tables that have both contiguous
integer keys from 1 and other keys.

You might also consider providing metamethod-based ways of overriding
the serialization, depending on the use of your library, if you aren't
already.

> 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;

I think that's the best you can do there.

> G.

   ---> Drake Wilson