fix: replace utils.difference by utils.shallowDiff

This commit is contained in:
Guillaume ARM 2024-05-20 20:51:20 +02:00
parent e9ebf63c55
commit 930b104522
2 changed files with 23 additions and 7 deletions

View File

@ -385,8 +385,8 @@ local function replantProcedure()
error('cannot fetch the remote plan')
end
local seedsToRemove = utils.difference(localPlan, remotePlan)
local seedsToPlant = utils.difference(remotePlan, localPlan)
local seedsToRemove = utils.shallowDiff(localPlan, remotePlan)
local seedsToPlant = utils.shallowDiff(remotePlan, localPlan)
if utils.sizeof(seedsToRemove) > 0 then
print('nb seeds to remove: ' .. utils.sizeof(seedsToRemove))

View File

@ -12,15 +12,31 @@ utils.sizeof = function(t)
return size
end
utils.difference = function(a, b)
local aa = {}
for k,v in pairs(a) do aa[v]=true end
for k,v in pairs(b) do aa[v]=nil end
utils.shallowDiff = function(a, b)
local counters = {}
for k,v in pairs(a) do
local currentCounter = counters[v] or 0
counters[v] = currentCounter + 1
end
for k,v in pairs(b) do
if counters[v] then
counters[v] = counters[v] - 1
end
end
local ret = {}
local n = 0
for k,v in pairs(a) do
if aa[v] then n=n+1 ret[n]=v end
if counters[v] and counters[v] > 0 then
counters[v] = counters[v] - 1
n = n + 1
ret[n] = v
end
end
return ret
end