From d50d7c50651d5bcaf6f35349e2461351cf91224f Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 29 Feb 2024 15:09:49 -0500 Subject: Add Queue.lua from roblox.com documentation. --- indra/newview/scripts/lua/Queue.lua | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 indra/newview/scripts/lua/Queue.lua (limited to 'indra/newview/scripts') diff --git a/indra/newview/scripts/lua/Queue.lua b/indra/newview/scripts/lua/Queue.lua new file mode 100644 index 0000000000..fa8737334c --- /dev/null +++ b/indra/newview/scripts/lua/Queue.lua @@ -0,0 +1,40 @@ +-- from https://create.roblox.com/docs/luau/queues#implementing-queues + +local Queue = {} +Queue.__index = Queue +​ +function Queue.new() + local self = setmetatable({}, Queue) +​ + self._first = 0 + self._last = -1 + self._queue = {} +​ + return self +end +​ +-- Check if the queue is empty +function Queue:IsEmpty() + return self._first > self._last +end +​ +-- Add a value to the queue +function Queue:Enqueue(value) + local last = self._last + 1 + self._last = last + self._queue[last] = value +end +​ +-- Remove a value from the queue +function Queue:Dequeue() + local first = self._first + if self:IsEmpty() then + return nil + end + local value = self._queue[first] + self._queue[first] = nil + self._first = first + 1 + return value +end +​ +return Queue -- cgit v1.2.3