lua-users home
lua-l archive

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

Hash: SHA1

Luiz Henrique de Figueiredo wrote:
> How do you propose to do that? I mean, how can the Lua core detect that a
> table is not an array without traversing it completely?

Beats me. But it would sure be nice!

Traversing the table completely would be expensive, but probably not so
expensive as to be worthless --- this is only a debug option, after all.
You'd keep a flag on the table indicating whether it as an array or not,
and only update the flag on writes.

In fact, it would most likely be possible to optimise further. If you
kept track of the maximum and minimum key values in the hash part, that
would mean you'd only have to traverse the hash part when writing to
keys in this range. Writes outside this range with either be obvious
(writing to a key beyond maximum+1? Not an array any more) or simple
(writing to a key less than minimum? Check the array part). There are
also lots of special cases, particularly when writing to a table that's
known to be an array; for example, when assigning a value to a new key,
you only need to check the key immediately smaller than the current one
to determine arrayness.

Of course, this probably isn't worth the extra complexity in the core,
even #ifdef'd. But it would still be very useful to have...

- --
┌─── ───── ─────
│ "They laughed at Newton. They laughed at Einstein. Of course, they
│ also laughed at Bozo the Clown." --- Carl Sagan
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla -