您的位置:首页 > 编程语言 > Lua

[Lua] 优化列表多元素删除

2018-10-25 23:46 375 查看

[Lua] 优化列表多元素删除

在 Lua 的内置库中,table 提供了 table.remove(t,[num]) 来删除列表中的元素,因为在该函数的执行过程涉及到了内存的移动,所以在删除多个元素的时候,如果每次都调用该函数,就会造成多次内存移动。

针对这一点,对 table 进行一些删除操作上的性能优化,代码基本上是从 C# 源码中抄过来的:-D,介绍在这里,下面直接附上代码,里面注释比较清楚,有什么问题,希望能够留言指出。

local TableUtil = {}

---从列表 t 中删除从 pos 开始的 num 个元素。
---@t 删除元素的列表
---@pos 开始删除索引
---@num 要删除元素的数量,如果没有指定,则删除 pos 之后的所有元素
function TableUtil.ClearArray(t,pos,num)
assert(t ~= nil,"The array:t is nil")
assert(pos > 0 and pos<=#t,"The pos is out range")

--如果没有指定 num 值,则默认 pos 之后全部删除
num = num or #t

--需要删除的最后一个元素的索引
local index = 0
if num >= 0 then
index = pos + num - 1
else
--为了保持从尾往头删,调换头尾索引
pos,index = pos + num + 1,pos
end

--对头尾索引进行限制,避免超界
index = index > #t and #t or index
index = index < 1 and 1 or index
pos = pos < 1 and 1 or pos

local maxn = #t - index + pos - 1

table.move(t,index+1,#t,pos)

local temp = #t
while temp > maxn  do
table.remove(t,temp)
temp = temp - 1
end
end

---从列表 t 中删除,所有满足 match 条件的元素
---@t 列表
---@match 筛选列表
function TableUtil.RemoveAll(t,match)
assert(match ~= nil,"Then match argument cannot be nil")

--找到第一个需要删除的元素的索引 num
local num = 1
while(num < #t and not match(t[num])) do
num = num + 1
end

--如果需要删除的索引超过列表最大索引,则表明没有满足删除条件的元素
if num > #t then
return 0
end

--将 num 之后不需要删除的元素,移动到需要删除的元素之前,这样可以避免拷贝
local i = num + 1
while(i <= #t) do

while(i <= #t and match(t[i])) do
i = i + 1
end

if i <= #t then
t[num] = t[i]
num = num + 1
i = i + 1
end
end
local result = #t - num + 1

--清楚需要删除的元素
ClearArray(t,num,result)
return result
end

return TableUtil
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: