[added] tablex.swap

This commit is contained in:
Max Cahill 2021-05-04 20:52:47 +10:00
parent 87834e9267
commit 5cd33398a5

View File

@ -48,6 +48,12 @@ function tablex.unshift(t, v)
return t return t
end end
--swap two indices of a table
--(easier to read and generally less typing than the common idiom)
function tablex.swap(t, i, j)
t[i], t[j] = t[j], t[i]
end
--insert to the first position before the first larger element in the table --insert to the first position before the first larger element in the table
--if this is used on an already sorted table, the table will remain sorted and not need re-sorting --if this is used on an already sorted table, the table will remain sorted and not need re-sorting
--todo: make it do binary search rather than linear to improve performance --todo: make it do binary search rather than linear to improve performance