refactor: implement turnOrder:remove by filtering/rebuilding queues instead of O(n) PriorityQueue:remove
This commit is contained in:
parent
2d29d35f96
commit
752fe00910
@ -119,8 +119,18 @@ function turnOrder:remove(id)
|
||||
return
|
||||
end
|
||||
|
||||
self.actedQueue:remove(id)
|
||||
self.pendingQueue:remove(id)
|
||||
local function filterQueue(q, targetId)
|
||||
local newQ = PriorityQueue.new(initiativeComparator)
|
||||
for _, val in ipairs(q.data) do
|
||||
if val ~= targetId then
|
||||
newQ:insert(val)
|
||||
end
|
||||
end
|
||||
return newQ
|
||||
end
|
||||
|
||||
self.actedQueue = filterQueue(self.actedQueue, id)
|
||||
self.pendingQueue = filterQueue(self.pendingQueue, id)
|
||||
end
|
||||
|
||||
return { new = new }
|
||||
|
||||
@ -115,29 +115,4 @@ function PriorityQueue:copy()
|
||||
}, PriorityQueue)
|
||||
end
|
||||
|
||||
--- Удалить элемент из очереди.
|
||||
--- Осторожно: O(n), так как нужно найти индекс элемента.
|
||||
---@param x any
|
||||
function PriorityQueue:remove(x)
|
||||
local index = nil
|
||||
for i, v in ipairs(self.data) do
|
||||
if v == x then
|
||||
index = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not index then return end
|
||||
|
||||
local n = #self.data
|
||||
if index == n then
|
||||
self.data[n] = nil
|
||||
else
|
||||
self.data[index] = self.data[n]
|
||||
self.data[n] = nil
|
||||
self:_sift_up(index)
|
||||
self:_sift_down(index)
|
||||
end
|
||||
end
|
||||
|
||||
return PriorityQueue
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user