Minetest-WorldEditAdditions/worldeditadditions_core/utils/queue.lua

74 lines
2 KiB
Lua
Raw Permalink Normal View History

-------------------------------------------------------------------------------
2021-08-04 20:11:47 +00:00
--- A Queue implementation
-- Taken & adapted from https://www.lua.org/pil/11.4.html
2023-07-02 18:03:46 +00:00
-- @class worldeditadditions_core.Queue
local Queue = {}
Queue.__index = Queue
2021-08-04 20:11:47 +00:00
--- Creates a new queue instance.
-- @returns Queue
function Queue.new()
local result = { first = 0, last = -1, items = {} }
setmetatable(result, Queue)
return result
end
2021-08-04 20:11:47 +00:00
--- Adds a new value to the end of the queue.
-- @param value any The new value to add to the end of the queue.
-- @returns number The index of the value that was added to the queue.
function Queue:enqueue(value)
local new_last = self.last + 1
self.last = new_last
self.items[new_last] = value
2021-07-13 22:42:08 +00:00
return new_last
end
2021-08-04 20:11:47 +00:00
--- Determines whether a given value is present in this queue or not.
-- @param value any The value to check.
-- @returns bool Whether the given value exists in the queue or not.
function Queue:contains(value)
for i=self.first,self.last do
if self.items[i] == value then
return true
end
end
return false
end
2021-08-04 20:11:47 +00:00
--- Returns whether the queue is empty or not.
-- @returns bool Whether the queue is empty or not.
function Queue:is_empty()
return self.first > self.last
end
2021-08-04 20:11:47 +00:00
--- Removes the item with the given index from the queue.
-- Item indexes do not change as the items in a queue are added and removed.
-- @param number The index of the item to remove from the queue.
-- @returns nil
2021-07-13 22:42:08 +00:00
function Queue:remove_index(index)
self.items[index] = nil
end
2021-08-04 20:11:47 +00:00
--- Dequeues an item from the front of the queue.
-- @returns any|nil Returns the item at the front of the queue, or nil if no items are currently enqueued.
function Queue:dequeue()
if Queue.is_empty(self) then
error("Error: The self is empty!")
end
2021-07-13 22:42:08 +00:00
local first = self.first
-- Find the next non-nil item
local value
while value == nil do
2021-08-07 01:30:38 +00:00
if first > self.last then return nil end
2021-07-13 22:42:08 +00:00
value = self.items[first]
self.items[first] = nil -- Help the garbage collector out
first = first + 1
end
self.first = first
return value
end
2021-08-04 20:11:47 +00:00
return Queue