lua-users home
lua-l archive

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


Hello everyone!

***Sorry in advance if this idea has been discussed recently. I did found the same exact suggestion back from 2011, but the discussion didn't seem to reach any conclusion.***

I would like to propose a third argument to `table.remove (list [, pos [, count]])`

`count` - number of elements to remove starting from position `pos` (defaults to 1). All the removed values are returned. [Also, we can probably allow `count` to be negative.]

Right know there's no good way to remove `count` elements from a table. One can only write something like this

for i = 1,count do table.remove(t, pos) end

My problem with this approach is grossly unjustified inefficiency. table.remove itself is O(#t) and the cycle amounts to O(count * #t).

(Worst case scenario is `pos` == 1, `count` == #t which leads to O(#t ^ 2) )

If we add count argument to the library function, the complexity is going to be limited to O(#t) regardless of `count`.

This, of course, should not break any existing code since the argument is optional and defaults to the current behaviour.

If necessary, I can provide my implementation. (I'm not doing it right away because the official FAQ discourages submitting any code.)

--
With regards, Alex Ch