lua-users home
lua-l archive

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


2016-07-07 23:36 GMT+02:00 William Ahern <william@25thandclement.com>:
>
> How did "largest numeric index" work when deleting the last index,
> like when popping a stack data structure.

Lua 3.2 to 5.1 offered two array models, both of which allowed
holes.

1. If the table has 'n', that is its length as far for sort,, tinsert and
tremove. The latter two respectively increment and decrement n.
2. If it does not have 'n', the actual largest numeric index is
laboriously calculated by traversing the entire table each time
you call a table function.

> Did you also assign nil to erase it? If so, isn't that an inconsistency

Yes, it is. That's why you should use tremove (table.remove)
instead of assigning nil, since it updates t.n.

> But for a non-sequence, #t might evaluate to a lesser index
> after executing t[#t + 1] = true. What use is the guarantee that
> t[#t + 1] is empty if you lose track of that value after the fact?

Suppose that t represents the rooms in an hotel. Guests arrive
and leave. You evaluate #t. It tells you where to find an empty
room. You put a guest in that room. Next guest comes. Again
#t finds an empty room. Why is it bad if that room happens to
have a lower number? But it would be bad if the guarantee
that t[#t+1] is empty did not hold.