A lua library/`class' that implements a FIFO. Objects in the fifo can be of any type, including nil.
The library returns the constructor fifo.new:
new_fifo = require "fifo"
Create a new fifo by calling the constructor; it optionally takes the initial state.
myfifo = new_fifo("foo", "bar")
The behaviour when trying to :pop() or :remove() too many items from an empty list is configurable. Returns the fifo itself
By default an error will be thrown. You can set a custom behaviour by providing a function to :setempty(). The return values of your function will be returned by :pop() or :remove()
myfifo:setempty(function(myfifo) return nil end)
This method returns self, which makes it easy to use at construction time: e.g. to create a new fifo where :pop() returns nil when empty:
myfifo = new_fifo():setempty(function() return nil end)
Use the :push() method to append an object to the fifo
myfifo:push({"an object"})
Allows you to inspect a fifo without removing items from it. Returns the item at the given index (or nil) and whether it existed (as nil is a valid value). By default uses the next item from the fifo.
exists, myobject = myfifo:peek()
Returns the next item from the fifo, removing it.
myobject = myfifo:pop()
This can be used to insert an item into the middle of a fifo. The index is from the output of the fifo where 1 would be the next item popped from the fifo, and myfifo:length() + 1 would be equivalent to :push() The efficiency of this operation is proportional to the distance from either end of the fifo.
myobject = myfifo:insert(1, {"some object"})
This can be used to remove an item from the middle of a fifo. The index is from the output of the fifo where 1 would be the next item popped from the fifo, and myfifo:length() would be the input (i.e equivalent to :push()) The efficiency of this operation is proportional to the distance from either end of the fifo.
The object removed is returned.
myobject = myfifo:remove(2)
Returns the current number of items in the fifo. Available as :length() as the __len metamethod doesn't work for tables in lua versions 5.1 and earlier.