Added tablex.swap_and_pop

This commit is contained in:
Max Cahill 2021-06-09 19:49:04 +10:00
parent 0bc9ccb17b
commit 9b95643e6e

View File

@ -54,6 +54,13 @@ function tablex.swap(t, i, j)
t[i], t[j] = t[j], t[i]
end
--swap the element at i to the back of the table, and remove it
--avoids linear cost of removal at the expense of messing with the order of the table
function tablex.swap_and_pop(t, i)
tablex.swap(t, i, #t)
return tablex.pop(t)
end
--default comparison; hoisted for clarity
--(shared with sort.lua and suggests the sorted functions below should maybe be refactored there)
local function default_less(a, b)