• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            劍孤寒的小站

            人生的價(jià)值不是用時(shí)間,而是用深度去衡量的!
            posts - 75, comments - 290, trackbacks - 0, articles - 0

            在Lua中使用List

            Posted on 2008-10-26 11:06 劍孤寒 閱讀(6808) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Lua
            《在Lua中使用List 》
            轉(zhuǎn)載請(qǐng)注明出處:http://groups.google.com/group/jianguhan


            在《Programming in Lua》一書中提到了一個(gè)雙端隊(duì)列的實(shí)現(xiàn)方法,我把它拿來改進(jìn)了一下用在了我正在制作的一個(gè)RPG游戲里,用起覺得 來還不錯(cuò),加了一個(gè)GetSize()來取得List的大小,這個(gè)在游戲制作中用處還是很大的^_^

            源代碼如下:
            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() 這里使用了云風(fēng)寫的一個(gè)class函數(shù)創(chuàng)建一個(gè)類,
            這個(gè)函數(shù)可以在云風(fēng)的博客上找到,為了方便起見我就在這里先抄一下了:

            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

            久久久久噜噜噜亚洲熟女综合| 久久无码高潮喷水| 精品久久久久久综合日本| av国内精品久久久久影院| AA级片免费看视频久久| 欧美午夜A∨大片久久| 久久国语露脸国产精品电影| 国产麻豆精品久久一二三| 热久久国产精品| 国色天香久久久久久久小说| 久久亚洲AV成人无码国产| 亚洲国产精品热久久| 狠狠色综合网站久久久久久久高清 | 国产精品一区二区久久精品涩爱| 漂亮人妻被黑人久久精品| 99久久亚洲综合精品成人| 久久亚洲熟女cc98cm| 99热热久久这里只有精品68| 亚洲精品无码久久久影院相关影片| 国产精品久久毛片完整版| 久久福利资源国产精品999| 99久久国产亚洲高清观看2024| 7777久久久国产精品消防器材| 精品欧美一区二区三区久久久 | 久久黄色视频| 久久久综合九色合综国产| 久久人人爽人人爽人人片AV麻烦| 国产叼嘿久久精品久久| 99久久99这里只有免费费精品| 久久人人爽人人爽人人片AV东京热 | 精品伊人久久久| 日本精品久久久久影院日本| 久久99精品国产| aaa级精品久久久国产片| 亚洲中文字幕久久精品无码喷水| 久久综合色老色| 99久久香蕉国产线看观香 | 国色天香久久久久久久小说| 综合久久一区二区三区| 亚洲欧美国产精品专区久久| 亚洲国产成人久久综合区|