在Lua中使用List 和 栈图

转自jianguhan

在《Programming in Lua》一书中提到了一个双端队列的实现方法,我把它拿来改进了一下用在了我正在制作的一个RPG游戏里,用起觉得 来还不错,加了一个GetSize()来取得List的大小,这个在游戏制作中用处还是很大的^_^ 

源代码如下: 
CList = class() 

function CList:ctor() 
        self.m_list = { first = 0, last = -1 } 
end 

function CList:PushFront(value) 
        local first = self.m_list.first - 1 
        self.m_list.first = first 
        self.m_list[first] = value 
end 

function CList:PushBack(value) 
        local last = self.m_list.last + 1 
        self.m_list.last = last 
        self.m_list[last] = value 
end 

function CList:PopFront() 
        local first = self.m_list.first 
        if first > self.m_list.last then return nil end 
        local value = self.m_list[first] 
        self.m_list[first] = nil 
        self.m_list.first = first + 1 
        return value 
end 

function CList:PopBack() 
        local last = self.m_list.last 
        if self.m_list.first > last then return nil end 
        local value = self.m_list[last] 
        self.m_list[last] = nil 
        self.m_list.last = last - 1 
        return value 
end 

function CList:GetSize() 
        if self.m_list.first > self.m_list.last then 
                return 0 
        else 
                return math.abs(self.m_list.last - self.m_list.first) + 1 
        end 
end 

在最前面的那句CList = class() 这里使用了云风写的一个class函数创建一个类, 
这个函数可以在云风的博客上找到,为了方便起见我就在这里先抄一下了: 

local _class={} 

function class(super) 
        local class_type={} 
        class_type.ctor=false 
        class_type.super=super 
        class_type.new=function(...) 
                        local obj={} 
                        do 
                                local create 
                                create = function(c,...) 
                                        if c.super then 
                                                create(c.super,...) 
                                        end 
                                        if c.ctor then 
                                                c.ctor(obj,...) 
                                        end 
                                end 

                                create(class_type,...) 
                        end 
                        setmetatable(obj,{ __index=_class[class_type] }) 
                        return obj 
                end 
        local vtbl={} 
        _class[class_type]=vtbl 

        setmetatable(class_type,{__newindex= 
                function(t,k,v) 
                        vtbl[k]=v 
                end 
        }) 

        if super then 
                setmetatable(vtbl,{__index= 
                        function(t,k) 
                                local ret=_class[super][k] 
                                vtbl[k]=ret 
                                return ret 
                        end 
                }) 
        end 

        return class_type 
end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
 热更新框架设计系列课程总体介绍:        本系列课程由《热更新框架设计之Xlua基础》、《热更新框架设计之热更流程与热补丁技术》、《热更新框架设计之游戏客户端框架》三套课程组成。 三套课程是一个不可分割有机的整体,笔者带领大家由浅入深逐级深入 ,在领悟热更精髓的基础之上,通过高端架构设计,设计出“低耦合”、“低侵入”、“高复用”性的游戏(VR/AR)客户端热更框架。                           温馨提示:            本套课程需要具备一定的框架理解与驾驭能力,为了更好的理解本作,强烈推荐广大学员首先学完必要的前导课程:“UI客户端框架设计”、“AssetBundle 框架设计”、“lua基础与级篇”。《热更新框架设计之Xlua基础》课程介绍:       本课程为系列课程的xlua基础部分,本课程主要就xlua的优势、特点、环境搭建、以及lua文件加载、C#调用lua的各种方式、lua调用C#的各种技巧展开讨论与讲解,学后基本掌握xlua常用的技术基础,为后续课程打下坚实的基础。 热更新系列(技术含量:高级):A:《lua热更新技术级篇》https://edu.csdn.net/course/detail/27087B:《热更新框架设计之热更流程与热补丁技术》https://edu.csdn.net/course/detail/27118C:《热更新框架设计之客户端热更框架(上)》https://edu.csdn.net/course/detail/27132D:《热更新框架设计之客户端热更框架()》https://edu.csdn.net/course/detail/27135E:《热更新框架设计之客户端热更框架(下)》https://edu.csdn.net/course/detail/27136

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值