lua-users home
lua-l archive

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


That's perfectly possible, but I need to manually track the height anyway, to avoid counting the array entries for every operation. And then I have the added overhead of implementing a new nil "type", translate back and forth. And then in the backing Lua code NIL (say it's a named empty table, or the value 0) is no longer false in a conditional context... I'm not sure it gains me much.

On Thu, May 7, 2015 at 11:43 AM, Luciano Ramalho <luciano@ramalho.org> wrote:
On Thu, May 7, 2015 at 2:57 PM, Brigham Toskin <brighamtoskin@gmail.com> wrote:
>>  As long as you can avoid nil, then table.insert() and table.remove()
>> implement a LIFO stack (since both default to the last position).  That
>> is:
>>
>>         push = table.insert
>>         pop  = table.remove
>>
>>   How much more compilicated is your ADT?
>
>
> This is actually exactly what I was doing, originally. But I have to check
> for underflow for most operations. And nil is a valid value. [...]

Can't you replace nil with another sentinel value when you're about to
push it into the stack?

Cheers,

Luciano



--
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
|     http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg




--
Brigham Toskin