blob: b0a5a87f87231301d3c8e72adb81c6baf4545713 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
-- from https://create.roblox.com/docs/luau/queues#implementing-queues,
-- amended per https://www.lua.org/pil/16.1.html
local Queue = {}
function Queue:new()
local obj = setmetatable({}, self)
self.__index = self
obj._first = 0
obj._last = -1
obj._queue = {}
return obj
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()
if self:IsEmpty() then
return nil
end
local first = self._first
local value = self._queue[first]
self._queue[first] = nil
self._first = first + 1
return value
end
return Queue
|