lua-users home
lua-l archive

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


2016-07-08 4:01 GMT+02:00 William Ahern <william@25thandclement.com>:

> I can't think of a scenario where relying on the #t+1
> guarantee alone is either more concise or more performant.

Suppose you have coroutines 'producer' and 'consumer' accessing
the same table. 'consumer' has some capricious way of choosing
an item and removing it. 'producer' has no way of knowing what
'consumer' has been doing and now needs to put a new item in.

    t[#t+1] = newitem

seems to me to be unbeatably concise, and needs only O(log n)
time, but with the present Lua documentation is unsound code.
You can make it sound by sacrificing both conciseness and
performance:

   local k=#t+1; while t[k] do k=k+1 end; t[k]=newitem

which may take O(n) time.