|
|
||
|
We appreciate tested code solutions for problems and enhancements; wejust don't incorporate third-party code verbatim. So, sure, please, let's see your code.
Maybe it's just me, but since all the commits are done by the author, I assumed that literally everyone else is considered a "third-party".
Anyway, here's my implementation. I'm also attaching a test
script, not sure if this mailing list allows attachments though.
//--------
//--------
On Sat, Sep 10, 2022 at 8:16 PM Alexander Chernoskutov wrote:
Right know there's no good way to remove `count` elements from a table.
By using table.move(), it is possible to create implementation with O(#t) time complexity:
function table.remove_count (list, pos, count)
pos = pos or #list
count = count or 1
return
(
function (...)
table.move(list, pos + count, #list + count, pos)
return ...
end
)
(
table.unpack(list, pos, pos + count - 1)
)
end
-- With regards, Alex Ch
function init_t()
return {1,2,3,4,5,6,7,8,9,10}
end
function assert_t_eq(t1, t2)
if #t1 ~= #t2 then
print('assertion failed: ', table.unpack(t1), "(" .. #t1 .. ")", "~=", table.unpack(t2), "(" .. #t2 .. ")")
error('assertion failed')
end
for i = 1, #t1 do
if t1[i] ~= t2[i] then
print('assertion failed: ', table.unpack(t1), "(" .. #t1 .. ")", "~=", table.unpack(t2), "(" .. #t2 .. ")")
error('assertion failed')
end
end
end
local t, tail
t = init_t()
tail = {table.remove(t)}
assert_t_eq(t, {1,2,3,4,5,6,7,8,9})
assert_t_eq(tail, {10})
t = init_t()
tail = {table.remove(t, 1)}
assert_t_eq(t, {2,3,4,5,6,7,8,9,10})
assert_t_eq(tail, {1})
t = init_t()
tail = {table.remove(t, 1, 3)}
assert_t_eq(t, {4,5,6,7,8,9,10})
assert_t_eq(tail, {1,2,3})
t = init_t()
tail = {table.remove(t, #t, -3)}
assert_t_eq(t, {1,2,3,4,5,6,7})
assert_t_eq(tail, {8,9,10})
t = init_t()
tail = {table.remove(t, 4, 3)}
assert_t_eq(t, {1,2,3,7,8,9,10})
assert_t_eq(tail, {4,5,6})
t = init_t()
tail = {table.remove(t, 2, 12)}
assert_t_eq(t, {1})
assert_t_eq(tail, {2,3,4,5,6,7,8,9,10})
t = init_t()
tail = {table.remove(t, 2, 0)}
assert_t_eq(t, init_t())
assert_t_eq(tail, {})
print("test table.remove()", "Ok")