-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.lua
More file actions
68 lines (52 loc) · 1.36 KB
/
Copy pathqueue.lua
File metadata and controls
68 lines (52 loc) · 1.36 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local HERE = (...):match("(.-)[^%.]+$")
local Base = require(HERE .. ".base")
---@class Queue
local queue = {}
queue.__index = queue
queue.__extVersion = "0.0.5"
-------------------------------------------------
---@return Queue
function queue.new(t)
return setmetatable(t or {}, queue)
end
--- Adds an element to the rear of the queue.
function queue:enqueue(value)
table.insert(self, value)
end
queue.add = queue.enqueue
--- Removes and returns the element from the front of the queue.
function queue:dequeue()
return table.remove(self, 1)
end
queue.remove = queue.dequeue
--- Returns the element at the front of the queue without removing it.
function queue:peek()
return self[1]
end
queue.front = queue.peek
--- Clears the queue while keeping same memory address.<br>
--- Use queue.new unless you need the same memory address.
function queue:clear()
for k in ipairs(self) do
self[k] = nil
end
end
--- Checks if the queue is empty and returns a boolean value.
function queue:isEmpty()
return #self == 0
end
function queue:contains(value)
for i, v in ipairs(self) do
if v == value then return true, i end
end
return false
end
function queue:iterate()
return ipairs(self)
end
---Create copy of the passed Queue recursively.
queue.clone = Base.clone
---Create a simple clone.
queue.shallowClone = Base.shallowClone
queue.toString = table.concat
return queue