1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 19:00:06 +08:00
SpaceVim/bundle/plenary.nvim/lua/plenary/async/structs.lua

117 lines
2.2 KiB
Lua
Raw Normal View History

2022-05-16 22:20:10 +08:00
local M = {}
Deque = {}
Deque.__index = Deque
---@class Deque
---A double ended queue
---
---@return Deque
function Deque.new()
-- the indexes are created with an offset so that the indices are consequtive
-- otherwise, when both pushleft and pushright are used, the indices will have a 1 length hole in the middle
return setmetatable({ first = 0, last = -1 }, Deque)
end
---push to the left of the deque
---@param value any
function Deque:pushleft(value)
local first = self.first - 1
self.first = first
self[first] = value
end
---push to the right of the deque
---@param value any
function Deque:pushright(value)
local last = self.last + 1
self.last = last
self[last] = value
end
---pop from the left of the deque
---@return any
function Deque:popleft()
local first = self.first
if first > self.last then
return nil
end
local value = self[first]
self[first] = nil -- to allow garbage collection
self.first = first + 1
return value
end
---pops from the right of the deque
---@return any
function Deque:popright()
local last = self.last
if self.first > last then
return nil
end
local value = self[last]
self[last] = nil -- to allow garbage collection
self.last = last - 1
return value
end
---checks if the deque is empty
---@return boolean
function Deque:is_empty()
return self:len() == 0
end
---returns the number of elements of the deque
---@return number
function Deque:len()
return self.last - self.first + 1
end
---returns and iterator of the indices and values starting from the left
---@return function
function Deque:ipairs_left()
local i = self.first
return function()
local res = self[i]
local idx = i
if res then
i = i + 1
return idx, res
end
end
end
---returns and iterator of the indices and values starting from the right
---@return function
function Deque:ipairs_right()
local i = self.last
return function()
local res = self[i]
local idx = i
if res then
i = i - 1 -- advance the iterator before we return
return idx, res
end
end
end
---removes all values from the deque
---@return nil
function Deque:clear()
for i, _ in self:ipairs_left() do
self[i] = nil
end
self.first = 0
self.last = -1
end
M.Deque = Deque
return M