zoukankan      html  css  js  c++  java
  • lua的深拷贝和浅拷贝

    --- Deep copies a table into a new table.
    -- Tables used as keys are also deep copied, as are metatables
    -- @param orig The table to copy
    -- @return Returns a copy of the input table
    local function deep_copy(orig)
      local copy
      if type(orig) == "table" then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
          copy[deep_copy(orig_key)] = deep_copy(orig_value)
        end
        setmetatable(copy, deep_copy(getmetatable(orig)))
      else
        copy = orig
      end
      return copy
    end
    
    --- Copies a table into a new table.
    -- neither sub tables nor metatables will be copied.
    -- @param orig The table to copy
    -- @return Returns a copy of the input table
    local function shallow_copy(orig)
      local copy
      if type(orig) == "table" then
        copy = {}
        for orig_key, orig_value in pairs(orig) do
          copy[orig_key] = orig_value
        end
      else -- number, string, boolean, etc
        copy = orig
      end
      return copy
    end
    

    例子如下:

    local a = { aa = 1, bb = 2, cc = { dd = { ee = 3 } } }
    --local b = deep_copy(a)
    local b = shallow_copy(a)
    b.cc.dd.ee = 111
    b.bb = 111
    ngx.say(cjson.encode(a))
    ngx.say(cjson.encode(b))
    

    结果:{"aa":1,"bb":2,"cc":{"dd":{"ee":111}}}
    {"aa":1,"bb":111,"cc":{"dd":{"ee":111}}}

    • 浅拷贝修改拷贝的某个键对应的值并不影响原始的表的键对应值(只能作用于第一层,如果多层嵌套就会导致原始表被修改)
    local men = { mkey = 555 }
    local a = { aa = 1, bb = 2, cc = { dd = { ee = { ff = 3 } } } }
    setmetatable(a, { __index = men })
    local b = deep_copy(a)
    
    ngx.say(a.cc.dd.ee.ff)
    ngx.say(a.mkey)
    ngx.say(b.mkey)
    

    3
    555
    555

    • 这个深拷贝可以同时复制原始表的元表。如果不许要可以将setmetatable(copy, deep_copy(getmetatable(orig)))去掉。
  • 相关阅读:
    【splunk】数据输入-文件目录 导入失败
    【linux】tar压缩不包含路径
    【python】已安装模块提示ImportError: No module named
    【splunk】用正则表达式提取字段
    【python】xsspider零碎知识点
    【scrapy】资料
    【splunk】一些查询例子
    【docker】将容器中数据拷贝到主机
    【linux】ubuntu下crontab无效解决方法
    Flink – metrics V1.2
  • 原文地址:https://www.cnblogs.com/mentalidade/p/7098877.html
Copyright © 2011-2022 走看看