-
Notifications
You must be signed in to change notification settings - Fork 23
/
fifo.lua
31 lines (31 loc) · 908 Bytes
/
fifo.lua
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
local fifomax = 50
function find_or_create_fifo(name)
local fifo = box.select(0, 0, name)
if fifo == nil then
fifo = {}
for i = 1, fifomax do fifo[i] = 0 end
fifo = box.insert(0, name, 3, 3, unpack(fifo))
end
return fifo
end
function fifo_push(name, val)
local fifo = find_or_create_fifo(name)
local top = box.unpack('i', fifo[1])
local bottom = box.unpack('i', fifo[2])
if top == fifomax+2 then -- % size
top = 3
elseif top ~= bottom then -- was not empty
top = top + 1
end
if bottom == fifomax + 2 then -- % size
bottom = 3
elseif bottom == top then
bottom = bottom + 1
end
return box.update(0, name, '=p=p=p', 1, top, 2, bottom, top, val)
end
function fifo_top(name)
local fifo = find_or_create_fifo(name)
local top = box.unpack('i', fifo[1])
return box.unpack('i', fifo[top])
end