Minetest-WorldEditAdditions/worldeditadditions/utils/queue.lua

75 lines
2.0 KiB
Lua
Raw Normal View History

-------------------------------------------------------------------------------
2021-08-04 22:11:47 +02:00
--- A Queue implementation
-- Taken & adapted from https://www.lua.org/pil/11.4.html
-- @submodule worldeditadditions.utils.queue
2021-08-04 22:11:47 +02:00
-- @class
local Queue = {}
Queue.__index = Queue
2021-08-04 22:11:47 +02: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 22:11:47 +02: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-14 00:42:08 +02:00
return new_last
end
2021-08-04 22:11:47 +02: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 22:11:47 +02: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 22:11:47 +02: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-14 00:42:08 +02:00
function Queue:remove_index(index)
self.items[index] = nil
end
2021-08-04 22:11:47 +02: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-14 00:42:08 +02:00
local first = self.first
-- Find the next non-nil item
local value
while value == nil do
if first >= self.last then return nil end
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 22:11:47 +02:00
return Queue