lua-users home
lua-l archive

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


On 07/08/2016 09:07 AM, Dirk Laurie wrote:
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.

Why not use a binary search in your example code?

Also, while I think your example is legit, I don't think it's such a common use case that it deserves its own operator. The current implementation of # could easily just be moved to a function of the table library.
--
Thomas