lua-users home
lua-l archive

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

On 8/18/2016 5:48 PM, ThePhD wrote:
My only problem is: how do I know how big the array part is?

See, this is why naming the internal implementation detail "array part" is a mistake. Forget there ever was such a thing.

What you have is called a "sequence". It is a table that contains integer keys from 1 to n with no holes. It might have other keys too, and generally that doesn't prevent it being a sequence.

The implementation detail that some or all of those integers are physically in "the array part" is not what made it a sequence. It is still a sequence if none of the integers are in the array part.

I could use
`#t` or fight with its equivalent in the Lua 5.1/2/3 code, which is
either lua_objlen or lua_rawlength (I can't remember which).

If your code is sufficiently disciplined to never poke holes in your sequences, then `#t` is the right way to learn the length of the sequence in the table `t`. In C, the equivalent is lua_objlen() in 5.1, and lua_len() in 5.2 and 5.3.

Right now I
have a fixed size at compile-time... I could just keep iterating integer
keys from 1 to infinity until I hit a snag, but that seems less than
useful (and doesn't allow me to pre-allocate the size to save performance).

Since you already need to iterate, then iterating until nil saves you the additional O(log n) cost from `#t`.

Ross Berteig                     
Cheshire Engineering Corp. 
+1 626 303 1602