找回密码
 立即注册
查看: 173|回复: 0

开发日记 -xlua 重定义数据结构

[复制链接]
发表于 2023-3-8 16:43 | 显示全部楼层 |阅读模式
---- 堆(优先队列)--local function classHeap( predicate )    local self = {}    ---- 成员变量 ----    local heap = {}    local size = 0    ---- 成员函数 前置声明 ----    ---- 构造器 ----    -- local function _init()    -- end    ---- 成员函数 ----        function self.push( value )        size = size + 1        heap[size] = value        local i = size        while i > 1 and predicate( heap, heap[math.floor(i / 2)] ) do            heap, heap[math.floor(i / 2)] = heap[math.floor(i / 2)], heap            i = math.floor(i / 2)        end    end    function self.pop()        if size == 0 then            return nil        end        local value = heap[1]        heap[1] = heap[size]        heap[size] = nil        size = size - 1        local i = 1        while i * 2 <= size do            local child = i * 2            if child < size and predicate( heap[child + 1], heap[child] ) then                child = child + 1            end            if predicate( heap[child], heap ) then                heap, heap[child] = heap[child], heap                i = child            else                break            end        end        return value    end    function self.peek()        return heap[1]    end    function self.isEmpty()        return size == 0    end    function self.clear()        heap = {}        size = 0    end    return selfendreturn classHeap

--
-- 链表
--

local function classLinkedList()
    ---@class LinkedList
    local self = {}

    local tableInsert = table.insert
    local tableRemove = table.remove

    ---- 成员变量 ----
    local _list = {}
    local _count = 0

    ---- 成员函数 前置声明 ----

    ---- 构造器 ----

    -- local function _init()
    -- end

    ---- 成员函数 ----
    function self.first()
        return _count > 0 and _list[1] or nil
    end

    function self.last()
        return _count > 0 and _list[_count] or nil
    end

    function self.addFirst(value)
        tableInsert(_list, 1, value)
        _count = _count + 1
    end

    function self.addLast(value)
        tableInsert(_list, value)
        _count = _count + 1
    end

    function self.removeFirst()
        if _count > 0 then
            tableRemove(_list, 1)
            _count = _count - 1
        end
    end

    function self.removeLast()
        if _count > 0 then
            tableRemove(_list)
            _count = _count - 1
        end
    end

    function self.remove(value)
        if _count == 0 then
            return
        end

        local pos = 0
        for i = 1, _count do
            if _list == value then
                pos = i
                break
            end
        end

        if pos > 0 then
            tableRemove(_list, pos)
            _count = _count - 1
        end
    end

    function self.contains(value)
        for i = 1, _count do
            if _list == value then
                return true
            end
        end
        return false
    end

    function self.find(predicate, ...)
        assert(predicate ~= nil and type(predicate) == "function")

        for i = 1, _count do
            if predicate(_list, ...) then
                return _list
            end
        end
    end

    function self.findLast(predicate, ...)
        assert(predicate ~= nil and type(predicate) == "function")

        for i = _count, 1, -1 do
            if predicate(_list, ...) then
                return _list
            end
        end
    end

    function self.foreach(predicate)
        assert(predicate ~= nil and type(predicate) == "function")

        for i = 1, _count do
            if predicate(_list) then
                return
            end
        end
    end

    function self.foreachReversed(predicate)
        assert(predicate ~= nil and type(predicate) == "function")

        for i = _count, 1, -1 do
            if predicate(_list) then
                return
            end
        end
    end

    function self.count()
        return _count
    end

    function self.clear()
        while _count > 0 do
            self.removeLast()
        end
    end
    ---- END

    -- _init()

    return self
end

return classLinkedList

--
-- 列表
--

local function classList(  )
    ---@class List
    local self = {}

    local tableInsert = table.insert
    local tableRemove = table.remove
    local tableSort = table.sort

    ---- 成员变量 ----
    local _count = 0

    ---- 成员函数 前置声明 ----

    ---- 构造器 ----

    -- local function _init()
    -- end

    ---- 成员函数 ----
    function self.add(value)
        _count = _count + 1
        self[_count] = value
    end

    function self.insert(index, value)
        tableInsert(self, index, value)
        _count = _count + 1
    end

    function self.remove(value)
        local index = self.indexOf(value)
        if index ~= -1 then
            self.removeAt(index)
        end
    end

    function self.removeAt(index)
        tableRemove(self, index)
        _count = _count - 1
    end

    function self.clear()
        while _count > 0 do
            self.removeAt(1)
        end
    end

    function self.contains(value)
        for index = 1, _count do
            if self[index] == value then
                return true
            end
        end
        return false
    end

    function self.count()
        return _count
    end

    function self.sort(comparison)
        assert(comparison ~= nil and type(comparison) == "function")
        tableSort(self, comparison)
    end

    function self.indexOf(value)
        for i = 1, _count do
            if self == value then
                return i
            end
        end
        return -1
    end

    function self.lastIndexOf(value)
        for i = _count, 1, -1 do
            if self == value then
                return i
            end
        end
        return -1
    end

    function self.find(predicate, ...)
        assert(predicate ~= nil and type(predicate) == "function")
        for i = 1, _count do
            if predicate(self, ...) then
                return self
            end
        end
    end

    function self.walk(predicate)
        assert(predicate ~= nil and type(predicate) == "function")
        for i = 1, _count do
            if predicate(self) then
                return true
            end
        end
    end

    function self.isEmpty()
        return _count == 0
    end
    ---- END

    -- _init()

    return self
end

return classList

--
-- 队列
--

local function classQueue(  )
    ---@class Queue
    local self = {
        _count = 0
    }

    ---- 成员变量 ----
    local _list = {}
    local _first = 1
    local _last = 0

    ---- 成员函数 前置声明 ----

    ---- 构造器 ----

    -- local function _init()
    -- end

    ---- 成员函数 ----
    function self.enqueue(value)
        _last = _last + 1
        _list[_last] = value
        self._count = self._count + 1
    end

    function self.dequeue()
        if self._count == 0 then
            return nil
        end
        local rtn = _list[_first]
        _list[_first] = nil
        _first = _first + 1
        self._count = self._count - 1
        return rtn
    end

    function self.peek()
        if self._count == 0 then
            return nil
        end
        return _list[_first]
    end

    function self.contains(value)
        for _, v in ipairs(_list) do
            if v == value then
                return true
            end
        end
        return false
    end

    function self.count()
        return self._count
    end

    function self.isEmpty()
        return self._count == 0
    end

    function self.clear()
        while not self.isEmpty() do
            self.dequeue()
        end
    end

    ---- END

    -- _init()

    return self
end

return classQueue

-- 集合
--

local function classSet( array )
        local self = {}

        ---- 成员变量 ----

        local _setTable = {}

        ---- 成员函数 前置声明 ----

        ---- 构造器 ----

        local function _init()
                for _, v in ipairs( array ) do
                        _setTable[v] = true
                end
        end

        ---- 成员函数 ----

        function self.hasElement( e )
                return _setTable[e] == true
        end

        function self.allElement()
                return array
        end


        ---- END

        _init()
        return self
end

return classSet
--
-- 栈
--
local function classStack()
    ---@class Stack
    local self = {}

    local tableInsert = table.insert
    local tableRemove = table.remove

    ---- 成员变量 ----
    local list = {}

    ---- 成员函数 前置声明 ----

    ---- 构造器 ----

    -- local function _init()
    -- end

    ---- 成员函数 ----
    function self.pop()
        if #list == 0 then
            return nil
        end
        local rtn = list[#list]
        tableRemove(list)
        return rtn
    end

    function self.push(value)
        tableInsert(list, value)
    end

    function self.peek()
        if #list == 0 then
            return nil
        end
        return list[#list]
    end

    function self.peekFor(predicate)
        assert(predicate ~= nil and type(predicate) == "function")

        for i = #list, 1, -1 do
            if predicate(list) then
                return list
            end
        end
    end

    function self.contains(value)
        for _, v in ipairs(list) do
            if v == value then
                return true
            end
        end
        return false
    end

    function self.clear()
        while #list > 0 do
            self.pop()
        end
    end

    function self.count()
        return #list
    end
    ---- END

    -- _init()

    return self
end

return classStack
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-4 07:37 , Processed in 0.156210 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表