lua-users home
lua-l archive

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


On Friday 31 December 2010 04:21:19 Dirk Laurie wrote:

> Thus, table.remove won't remove something that table.insert
> has inserted.
> 
> Suggestion:
>         "if necessary" should mean that inserting something into
>         an empty position k does the same as assigning it to t[k].
> 
> Dirk

While this is being sorted out, how much performance is lost by by keeping 
track in the table, e.g.

STACK = {}

STACK.new=function()
	local mystack = {cur = 0}
	function mystack.push(stk, data)
		stk.cur = stk.cur + 1
		stk[stk.cur] = data
	end
	function mystack.pop(stk)
		stk.cur = stk.cur - 1
		if stk.cur < 0 then return nil end
		return stk[stk.cur + 1]
	end
	return mystack
end

stack1 = STACK.new()

stack1:push(1)
stack1:push(2)
stack1:push(3)

print(stack1:pop())
print(stack1:pop())
print(stack1:pop())