开发日记 -xlua 重定义数据结构
---- 堆(优先队列)--local function classHeap( predicate ) local self = {} ---- 成员变量 ---- local heap = {} local size = 0 ---- 成员函数 前置声明 ---- ---- 构造器 ---- -- local function _init() -- end ---- 成员函数 ---- function self.push( value ) size = size + 1 heap = value local i = size while i > 1 and predicate( heap, heap ) do heap, heap = heap, heap i = math.floor(i / 2) end end function self.pop() if size == 0 then return nil end local value = heap heap = heap heap = nil size = size - 1 local i = 1 while i * 2 <= size do local child = i * 2 if child < size and predicate( heap, heap ) then child = child + 1 end if predicate( heap, heap ) then heap, heap = heap, heap i = child else break end end return value end function self.peek() return heap 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 or nil
end
function self.last()
return _count > 0 and _list 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) == &#34;function&#34;)
for i = 1, _count do
if predicate(_list, ...) then
return _list
end
end
end
function self.findLast(predicate, ...)
assert(predicate ~= nil and type(predicate) == &#34;function&#34;)
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) == &#34;function&#34;)
for i = 1, _count do
if predicate(_list) then
return
end
end
end
function self.foreachReversed(predicate)
assert(predicate ~= nil and type(predicate) == &#34;function&#34;)
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 = 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 == 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) == &#34;function&#34;)
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) == &#34;function&#34;)
for i = 1, _count do
if predicate(self, ...) then
return self
end
end
end
function self.walk(predicate)
assert(predicate ~= nil and type(predicate) == &#34;function&#34;)
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 = value
self._count = self._count + 1
end
function self.dequeue()
if self._count == 0 then
return nil
end
local rtn = _list
_list = 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
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 = true
end
end
---- 成员函数 ----
function self.hasElement( e )
return _setTable == 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) == &#34;function&#34;)
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
页:
[1]