-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.lua
More file actions
executable file
·46 lines (38 loc) · 880 Bytes
/
Copy pathQueue.lua
File metadata and controls
executable file
·46 lines (38 loc) · 880 Bytes
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
local Queue = {}
Queue.__index = Queue
function Queue.new ()
return setmetatable({ head = 1, tail = 1 }, Queue)
end
function Queue:push (v)
if self:length() == 0 then
self.tail = 1
self.head = 1
end
self[self.tail] = v
self.tail = self.tail + 1
end
function Queue:pop ()
if self:length() == 0 then return nil end
local v = self[self.head]
self[self.head] = nil
self.head = self.head + 1
return v
end
function Queue:front ()
if self:length() == 0 then return nil end
return self[self.head]
end
function Queue:back ()
if self:length() == 0 then return nil end
return self[self.tail - 1]
end
function Queue:map (f)
if self:length() == 0 then return end
for i = self.head, self.tail - 1 do
f(self[i])
end
end
function Queue:length ()
return self.tail - self.head
end
return Queue