-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.lua
More file actions
33 lines (29 loc) · 728 Bytes
/
queue.lua
File metadata and controls
33 lines (29 loc) · 728 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
function new ()
return {first = 0, last = -1}
end
function pushleft (list, value)
local first = list.first - 1
list.first = first
list[first] = value
end
function pushright (list, value)
local last = list.last + 1
list.last = last
list[last] = value
end
function popleft (list)
local first = list.first
if first > list.last then return nil end
local value = list[first]
list[first] = nil -- to allow garbage collection
list.first = first + 1
return value
end
function popright (list)
local last = list.last
if list.first > last then return nil end
local value = list[last]
list[last] = nil -- to allow garbage collection
list.last = last - 1
return value
end