mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 14:14:36 +00:00
added tablex.copy
is an alias for tablex.shallow_copy
, same for overlay
, bunch of whitespace changes, some additional asserts
This commit is contained in:
parent
de5c803389
commit
213ba5df0f
32
tablex.lua
32
tablex.lua
@ -331,6 +331,7 @@ end
|
||||
-- Copy a table
|
||||
-- See shallow_overlay to shallow copy into an existing table to avoid garbage.
|
||||
function tablex.shallow_copy(t)
|
||||
assert:type(t, "table", "tablex.shallow_copy - t", 1)
|
||||
if type(t) == "table" then
|
||||
local into = {}
|
||||
for k, v in pairs(t) do
|
||||
@ -341,29 +342,39 @@ function tablex.shallow_copy(t)
|
||||
return t
|
||||
end
|
||||
|
||||
local function deep_copy(t, copied)
|
||||
--alias
|
||||
tablex.copy = tablex.shallow_copy
|
||||
|
||||
--implementation for deep copy
|
||||
--traces stuff that has already been copied, to handle circular references
|
||||
local function _deep_copy_impl(t, already_copied)
|
||||
local clone = t
|
||||
if type(t) == "table" then
|
||||
if copied[t] then
|
||||
clone = copied[t]
|
||||
if already_copied[t] then
|
||||
--something we've already encountered before
|
||||
clone = already_copied[t]
|
||||
elseif type(t.copy) == "function" then
|
||||
--something that provides its own copy function
|
||||
clone = t:copy()
|
||||
assert:type(clone, "table", "copy() didn't return a copy")
|
||||
assert:type(clone, "table", "member copy() function didn't return a copy")
|
||||
else
|
||||
--a plain table to clone
|
||||
clone = {}
|
||||
for k, v in pairs(t) do
|
||||
clone[k] = deep_copy(v, copied)
|
||||
clone[k] = _deep_copy_impl(v, already_copied)
|
||||
end
|
||||
setmetatable(clone, getmetatable(t))
|
||||
copied[t] = clone
|
||||
already_copied[t] = clone
|
||||
end
|
||||
end
|
||||
return clone
|
||||
end
|
||||
|
||||
-- Recursively copy values of a table.
|
||||
-- Retains the same keys as original table -- they're not cloned.
|
||||
function tablex.deep_copy(t)
|
||||
return deep_copy(t, {})
|
||||
assert:type(t, "table", "tablex.deep_copy - t", 1)
|
||||
return _deep_copy_impl(t, {})
|
||||
end
|
||||
|
||||
-- Overlay tables directly onto one another, merging them together.
|
||||
@ -383,6 +394,8 @@ function tablex.shallow_overlay(dest, ...)
|
||||
return dest
|
||||
end
|
||||
|
||||
tablex.overlay = tablex.shallow_overlay
|
||||
|
||||
-- Overlay tables directly onto one another, merging them together into something like a union.
|
||||
-- Also overlays nested tables, but doesn't clone them (so a nested table may be added to dest).
|
||||
-- Takes as many tables as required,
|
||||
@ -437,6 +450,7 @@ function tablex.shallow_equal(a, b)
|
||||
end
|
||||
end
|
||||
-- second loop to ensure a isn't missing any keys from b.
|
||||
-- we don't compare the values - if any are missing we're not equal
|
||||
for k, v in pairs(b) do
|
||||
if a[k] == nil then
|
||||
return false
|
||||
@ -462,8 +476,8 @@ function tablex.deep_equal(a, b)
|
||||
return false
|
||||
end
|
||||
end
|
||||
-- second loop to ensure a isn't missing any keys from b, so we can skip
|
||||
-- recursion.
|
||||
-- second loop to ensure a isn't missing any keys from b
|
||||
-- we don't compare the values - if any are missing we're not equal
|
||||
for k, v in pairs(b) do
|
||||
if a[k] == nil then
|
||||
return false
|
||||
|
Loading…
Reference in New Issue
Block a user