PGRData/Script/matrix/xcommon/XQueue.lua

52 lines
1 KiB
Lua
Raw Normal View History

2024-09-01 20:49:41 +00:00
---@class XQueue 队列
XQueue = XClass(nil, "XQueue")
function XQueue:Ctor()
self:Clear()
end
function XQueue:Clear()
self.__Container = {}
self.__StartIndex = 1
self.__EndIndex = 0
end
function XQueue:IsEmpty()
return self.__StartIndex > self.__EndIndex
end
function XQueue:Count()
return self.__EndIndex - self.__StartIndex + 1
end
function XQueue:Enqueue(element)
if not element then return end
local endIndex = self.__EndIndex + 1
self.__EndIndex = endIndex
self.__Container[endIndex] = element
end
function XQueue:EnqueueFront(element)
self.__Container[self.__StartIndex - 1] = element
self.__StartIndex = self.__StartIndex - 1
end
function XQueue:Dequeue()
if self:IsEmpty() then
self:Clear()
return
end
local startIndex = self.__StartIndex
local element = self.__Container[startIndex]
self.__StartIndex = startIndex + 1
self.__Container[startIndex] = nil
return element
end
function XQueue:Peek()
return self.__Container[self.__StartIndex]
end