11-- A generic fifo module. See docs/lua-modules/fifo.md for use examples.
22
3- local tr , ti = table.remove , table.insert
3+ local tableRemove , tableInsert = table.remove , table.insert
44
55-- Remove an element and pass it to k, together with a boolean indicating that
66-- this is the last element in the queue; if that returns a value, leave that
@@ -17,29 +17,33 @@ local tr, ti = table.remove, table.insert
1717--
1818-- Returns 'true' if the queue contained at least one non-phantom entry,
1919-- 'false' otherwise.
20- local function dequeue (q ,k )
21- if # q > 0
22- then
23- local new , again = k (q [1 ], # q == 1 )
24- if new == nil
25- then tr (q ,1 )
26- if again then return dequeue (q , k ) end -- note tail call
27- else q [1 ] = new
28- end
29- return true
30- else q ._go = true ; return false
20+ local function dequeue (q , k )
21+ if # q > 0 then
22+ local new , again = k (q [1 ], # q == 1 )
23+ if new == nil then
24+ tableRemove (q , 1 )
25+ else
26+ q [1 ] = new
27+ end
28+ if again then
29+ return dequeue (q , k )
30+ end -- note tail call
31+ return true
32+ else
33+ q ._go = true
34+ return false
3135 end
3236end
3337
3438-- Queue a on queue q and dequeue with `k` if the fifo had previously emptied.
3539local function queue (q ,a ,k )
36- ti (q ,a )
40+ tableInsert (q ,a )
3741 if k ~= nil and q ._go then q ._go = false ; dequeue (q , k ) end
3842end
3943
4044-- return a table containing just the FIFO constructor
4145return {
4246 [' new' ] = function ()
43- return { [' _go' ] = true ; [' queue' ] = queue ; [' dequeue' ] = dequeue }
44- end
47+ return { [' _go' ] = true ; [' queue' ] = queue ; [' dequeue' ] = dequeue }
48+ end
4549}
0 commit comments