Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Array.HasValue(tbl, val)
- for i = 1, #tbl do
- if tbl[i] == val then
- return true
- end
- end
- return false
- end
- function Array.Compare(tOrig, tNew)
- local tAdded, uAddedLen = {}, 0
- local tLost uLostLen = {}, 0
- local uOrigLen = #tOrig
- local uNewLen = #tNew
- if (uOrigLen <= uNewLen) then
- -- Do added and lost comparisons in one loop for the length of the original table
- for i1 = 1, uOrigLen do
- local val = tOrig[i1]
- for i2 = 1, uNewLen do
- if (val == tNew[i2]) then
- goto NoLoss11
- end
- end
- uLostLen = uLostLen + 1
- tLost[uLostLen] = val
- ::NoLoss11::
- val = tNew[i1]
- for i2 = 1, uOrigLen do
- if (val == tOrig[i2]) then
- goto NoAdded11
- end
- end
- uAddedLen = uAddedLen + 1
- tAdded[uAddedLen] = val
- ::NoAdded11::
- end
- -- Only do the second loop if there's still more to check
- if (uAddedLen ~= uOrigLen) then
- -- Check the rest of the second table now for any additional adds
- for i1 = uOrigLen + 1, uNewLen do
- local val = tNew[i1]
- for i2 = 1, uOrigLen do
- if (val == tOrig[i2]) then
- goto NoAdded12
- end
- end
- uAddedLen = uAddedLen + 1
- tAdded[uAddedLen] = val
- ::NoAdded12::
- end
- end
- else
- -- Since the new table has less elements, the second loop will be a loss check instead of an add check
- for i1 = 1, uNewLen do
- local val = tNew[i1]
- for i2 = 1, uOrigLen do
- if (val == tOrig[i2]) then
- goto NoAdded21
- end
- end
- uAddedLen = uAddedLen + 1
- tAdded[uAddedLen] = val
- ::NoAdded21::
- val = tOrig[i1]
- for i2 = 1, uNewLen do
- if (val == tNew[i2]) then
- goto NoLoss21
- end
- end
- uLostLen = uLostLen + 1
- tLost[uLostLen] = val
- ::NoLoss21::
- end
- if (uLostLen ~= uNewLen) then
- for i1 = uNewLen + 1, uOrigLen do
- local val = tOrig[i1]
- for i2 = 1, uNewLen do
- if (val == tNew[i2]) then
- goto NoLoss22
- end
- end
- uLostLen = uLostLen + 1
- tLost[uLostLen] = val
- ::NoLoss22::
- end
- end
- end
- return tAdded, uAddedLen, tLost, uLostLen
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement