mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-26 15:44:35 +00:00
[added] tablex.insert_sorted (does what you'd expect, though no binary search yet)
This commit is contained in:
parent
7922cd3912
commit
d445c170e1
19
tablex.lua
19
tablex.lua
@ -45,6 +45,25 @@ function tablex.unshift(t, v)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--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
|
||||||
|
--todo: make it do binary search rather than linear to improve performance
|
||||||
|
--return the table for possible chaining
|
||||||
|
function tablex.insert_sorted(t, v, less)
|
||||||
|
local inserted = false
|
||||||
|
for i = 1, #t do
|
||||||
|
if less(v, t[i]) then
|
||||||
|
table.insert(t, i, v)
|
||||||
|
inserted = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not inserted then
|
||||||
|
table.insert(t, v)
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
--find the index in a sequential table that a resides at
|
--find the index in a sequential table that a resides at
|
||||||
--or nil if nothing was found
|
--or nil if nothing was found
|
||||||
function tablex.index_of(t, a)
|
function tablex.index_of(t, a)
|
||||||
|
Loading…
Reference in New Issue
Block a user