Skip to content

Commit

Permalink
reformat to make readable
Browse files Browse the repository at this point in the history
  • Loading branch information
HHHartmann committed Feb 18, 2024
1 parent 70b1630 commit 47a1f63
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lua_modules/fifo/fifo.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- A generic fifo module. See docs/lua-modules/fifo.md for use examples.

local tr, ti = table.remove, table.insert
local tableRemove, tableInsert = table.remove, table.insert

-- Remove an element and pass it to k, together with a boolean indicating that
-- this is the last element in the queue; if that returns a value, leave that
Expand All @@ -17,29 +17,33 @@ local tr, ti = table.remove, table.insert
--
-- Returns 'true' if the queue contained at least one non-phantom entry,
-- 'false' otherwise.
local function dequeue(q,k)
if #q > 0
then
local new, again = k(q[1], #q == 1)
if new == nil
then tr(q,1)
if again then return dequeue(q, k) end -- note tail call
else q[1] = new
end
return true
else q._go = true ; return false
local function dequeue(q, k)
if #q > 0 then
local new, again = k(q[1], #q == 1)
if new == nil then
tableRemove(q, 1)
if again then
return dequeue(q, k)
end -- note tail call
else
q[1] = new
end
return true
else
q._go = true
return false
end
end

-- Queue a on queue q and dequeue with `k` if the fifo had previously emptied.
local function queue(q,a,k)
ti(q,a)
tableInsert(q,a)
if k ~= nil and q._go then q._go = false; dequeue(q, k) end
end

-- return a table containing just the FIFO constructor
return {
['new'] = function()
return { ['_go'] = true ; ['queue'] = queue ; ['dequeue'] = dequeue }
end
return { ['_go'] = true ; ['queue'] = queue ; ['dequeue'] = dequeue }
end
}

0 comments on commit 47a1f63

Please sign in to comment.