summaryrefslogtreecommitdiff
path: root/indra
diff options
context:
space:
mode:
authornat-goodspeed <nat@lindenlab.com>2024-03-01 08:54:19 -0500
committerGitHub <noreply@github.com>2024-03-01 08:54:19 -0500
commit80c157661b694b0e38716b34dd8015cbf646186e (patch)
treeb92852f9c9b790fbae1b199c2dd835852ccb79cd /indra
parentba1f0060b42390bd4f63de7fe32b0d0e9360f633 (diff)
parent6aee62f2bbdd06cf34c4042317cb52ec3c09570c (diff)
Merge pull request #910 from secondlife/lua-queue
Add Queue.lua from roblox.com documentation.
Diffstat (limited to 'indra')
-rw-r--r--indra/newview/scripts/lua/Queue.lua40
1 files changed, 40 insertions, 0 deletions
diff --git a/indra/newview/scripts/lua/Queue.lua b/indra/newview/scripts/lua/Queue.lua
new file mode 100644
index 0000000000..e178ad9969
--- /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