diff options
author | chai <chaifix@163.com> | 2021-10-20 13:50:50 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-20 13:50:50 +0800 |
commit | afdcbfa9c4259fb003fd072ae011836230e7e39b (patch) | |
tree | 28687805fa6cd08ea998adffeac7b241af42cfe8 /Resources/DefaultContent/Libraries/containers/queue.lua | |
parent | c795fb754bfd5c84c1bfd7dc793c6519f01109ea (diff) |
+containers
Diffstat (limited to 'Resources/DefaultContent/Libraries/containers/queue.lua')
-rw-r--r-- | Resources/DefaultContent/Libraries/containers/queue.lua | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/Resources/DefaultContent/Libraries/containers/queue.lua b/Resources/DefaultContent/Libraries/containers/queue.lua new file mode 100644 index 0000000..126daa8 --- /dev/null +++ b/Resources/DefaultContent/Libraries/containers/queue.lua @@ -0,0 +1,111 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by Dee. +--- DateTime: 2019/3/7 11:27 +--- FIFO +--- + +queue = queue or {} + +---@return Queue +function queue.create() + ---数据容器 + local data = {} + ---数据长度 + local lenght = 0 + ---队首索引 + local first = 1 + + ---获取队首值 + local peek = function() + return data[first] + end + + ---压入数据 + local enqueue = function(v) + assert(v ~= nil, "nil value") + first = lenght == 0 and 1 or first + lenght = lenght + 1 + table.insert(data, first+lenght-1, v) + end + + ---弹出数据 + local dequeue = function() + assert(lenght > 0, "nill queue") + + local ret = peek() + data[first] = nil + first = first+1 + lenght = lenght - 1 + first = lenght == 0 and 1 or first + + if math.fmod(first, 4) == 0 then + local tmp = {} + table.move(data, first, first + lenght, 1, tmp) + + first = 1 + data = nil + data = tmp + end + + return ret + end + + local clear = function() + data = {} + first = 1 + lenght = 0 + end + + local __tostring = function() + local tmp = {} + for i=1,lenght do + tmp[i] = data[i + first - 1] + end + return table.concat(tmp, ",") + end + + local __len = function() + return lenght + end + + local __index = function(i_t, key) + error(">> Dee: Limited access") + end + + local __newindex = function(i_t, key, v) + error(">> Dee: Limited access") + end + + local __ipairs = function(i_t) + local idx = 0 + local function iter(i_t) + idx = idx + 1 + if idx <= lenght then + return idx, data[first + idx - 1] + end + end + + return iter + end + + local __pairs = function(i_t) + error(">> Dee: Limited access") + end + + local mt = {__tostring = __tostring, __index = __index, __newindex = __newindex, __ipairs = __ipairs, __pairs = __pairs, __len = __len} + + ---@class Queue + local t = { + enqueue = enqueue, + dequeue = dequeue, + peek = peek, + clear = clear + } + + setmetatable(t, mt) + + return t +end + +return queue |