小工具知识网

当前位置: 首页 > 行业知识 > Lua迭代器详解(附加红点功能实例)

行业知识

Lua迭代器详解(附加红点功能实例)

2026-12-05 07:48:43 来源:互联网转载

Lua迭代器详解与用法

  • 1. 什么是迭代器
  • 2. 为什么需要理解迭代器的原理
  • 3. 迭代器的实现
    • 0. 闭包
    • 1. 有状态迭代器
    • 2. 无状态迭代器
    • 4. 红点树系统基础

      1. 什么是迭代器

      迭代器是一种能让我们遍历一个集合中的所有元素的代码结构。比如常用ipairs()和pairs()。

      ()

      2. 为什么需要理解迭代器的原理

      对于常见的table,无论key,value都可以通过ipairs()和pairs(),甚至访问table下标[*],next()遍历到table中所有的元素,而对于诸如链表,树,图等结构时,只依靠Lua原生的迭代器并不能方便的“以一条链或者路径的方式进行输出”,这时,我们需要根据所需自定义迭代器。

            对于一些还是在校学生等接触Lua时间不长的人来说,可能没法立刻体会到链表,树,图在Lua中的使用场景(除了练习数据结构)。

      ()

            其实,在结合Lua进行游戏开发的商业项目中,这些数据结构会得到广泛的使用,比如:

      1. LRU动态管理内存资源,通常LRU会由链表去解决地址冲突问题;
      2. 游戏的红点树,红点一般都是用内向外(一般游戏主界面可以理解为最外一层)逐级传递,通过树来构建整个游戏中所有界面的红点依赖,就可以通过设置某一个节点从而使得这条路径上所有的节点红点都能被响应(设置显隐)。

      3. 迭代器的实现

      0. 闭包

      闭包(closure)是一个函数以及其引用的非局部变量的组合。闭包允许函数携带其执行环境,使得函数可以访问并操作其定义时的变量,即使这些变量超出了其定义的作用域。

      function createCounter()    local count = 0    local function counter()        count = count + 1        return count    end    return counterendlocal myCounter = createCounter()print(myCounter()) -- 输出 1print(myCounter()) -- 输出 2print(myCounter()) -- 输出 3

      1. 有状态迭代器

      有状态迭代器需要在外部环境中维护状态信息,通常利用Lua的闭包实现

      -- statefulRange 返回一个闭包函数,闭包中维护了 current 状态,这个状态在每次迭代时更新。function statefulRange(start, stop)    local current = start - 1    return function()        current = current + 1        if current }local RedPointTable = {}function RedPoint.GetAll() -- 获取红点所有配置表    return RedPointTableend-- 获取一个节点的配置项function RedPoint.GetConfig(NodeName)    return RedPointTable[NodeName]endfunction RedPoint.AddNode(NodeName,ParentNodeName,bEnd) -- 这种方式插入节点有个问题是要按照树的顺序从上到下,如果先插子节点,而父节点不存在,导致父节点的Child子节点索引表错误    return function(cfg)        if ParentNodeName ~= "none" then            local parentNode = RedPoint.GetConfig(ParentNodeName)            if not parentNode.Child then                parentNode.Child = {}            end            parentNode.Child[NodeName] = true -- 以hash的方式存储,方便做一些子节点快速查找操作,如果不需要也可以直接按顺序插入        end        cfg.bEndNode = bEnd -- bEnd = false 添加非叶子节点 bEnd = true 添加非叶子节点        cfg.parent = ParentNodeName        RedPointTable[NodeName] = cfg    endendfunction RedPoint.AddNodeEX(NodeName,ParentNodeName,bEnd) -- 这种方式插入可以不管父子节点的顺序,但是需要在使用前先对该配置表RedPointTable进行一次预处理,来建立每个节点的子节点表    return function (cfg)        cfg.bCatalog = bEnd        cfg.parent = ParentNodeName        RedPointTable[NodeName] = cfg    endend------------------ 策划配置部分 Begin --------------------- 路径 1RedPoint.AddNode("root","none",false){    NodeValue = 1}RedPoint.AddNode("child1","root",false){    NodeValue = 2}RedPoint.AddNode("grandchild1","child1",false){    NodeValue = 5}RedPoint.AddNode("grandchild2","grandchild1",true){    NodeValue = 7}--- 路径 2RedPoint.AddNode("child2","root",true){    NodeValue = 3}--- 路径 3RedPoint.AddNode("child3","root",true){    NodeValue = 4}--- 路径 4RedPoint.AddNode("grandchild3","child1",true){    NodeValue = 6}------------------ 策划配置部分 End ------------------------------------ 功能函数测试 --------------------- 实际开发中会有一个单独的红点功能来管理下面这些接口-- 迭代器函数local InnerNextNode = function (CurNodeName, CurNodeParentName)    if not CurNodeParentName then        return CurNodeName,RedPoint.GetConfig(CurNodeName)    else        local Config = RedPoint.GetConfig(CurNodeParentName)        local parent = Config.parent        if parent ~= "none" then            return parent, RedPoint.GetConfig(parent)        else            return nil, nil        end    endend-- 自定义for无状态迭代器local NextNode = function(CurNodeName)    return InnerNextNode,CurNodeName,nilend-- 由当前节点向父节点依次遍历,直到遍历到没有父节点为止function RedPoint.GetEachNode(CurNodeName)    for NodeName,NodeConfig in NextNode(CurNodeName) do        print("NodeName:",NodeName," NodeConfig:",NodeConfig.NodeValue)    endend-- 获取一个节点的所有直接子节点function RedPoint.GetNodeChild(CurNodeName)    local CurNodeCfg = RedPointTable[CurNodeName]    if CurNodeCfg and CurNodeCfg.Child then        local Child = CurNodeCfg.Child        for NodeName,bChild in pairs(Child) do            if bChild then                local CurChildConfig = RedPoint.GetConfig(NodeName)                if CurChildConfig then                    print("CurNodeName's Child NodeName is:",NodeName,CurChildConfig.NodeValue)                else                    error("Cur Child Node is Invalid:",NodeName)                end            end        end    endend-- 遍历以CurNodeName节点为根节点时下面所有节点function RedPoint.GetEachNodeAll(CurNodeName)    local CurNodeConfig = RedPoint.GetConfig(CurNodeName)    if CurNodeConfig then        print("Cur Node====",CurNodeConfig.NodeValue)        local CurChildNode = CurNodeConfig.Child        if CurChildNode then            for NodeName,NodeConfig in pairs(CurChildNode) do                RedPoint.GetEachNodeAll(NodeName)            end        end    endenddo    RedPoint.GetEachNode("grandchild2")    -- NodeName:    grandchild2  NodeConfig:    7    -- NodeName:    grandchild1  NodeConfig:    5    -- NodeName:    child1   NodeConfig:    2    -- NodeName:    root     NodeConfig:    1    RedPoint.GetNodeChild("root")    -- CurNodeName's Child NodeName is: child2  3    -- CurNodeName's Child NodeName is: child3  4    -- CurNodeName's Child NodeName is: child1  2    RedPoint.GetEachNodeAll("root")    -- Cur Node==== 1    -- Cur Node==== 3    -- Cur Node==== 4    -- Cur Node==== 2    -- Cur Node==== 5    -- Cur Node==== 7    -- Cur Node==== 6endreturn RedPoint

上一篇:Java多例Bean的应用场景-easyExcel导入

下一篇:服务器宕机ip冲突怎么解决?