1fifo.lua(3) fifo.lua(3)
2
3
4
6 A lua library/`class' that implements a FIFO. Objects in the fifo can
7 be of any type, including nil.
8
10 The library returns the constructor fifo.new:
11
12 new_fifo = require "fifo"
13
14 myfifo:fifo = new_fifo(...)
15 Create a new fifo by calling the constructor; it optionally takes the
16 initial state.
17
18 myfifo = new_fifo("foo", "bar")
19
20 myfifo = myfifo:setempty(f:function)
21 The behaviour when trying to :pop() or :remove() too many items from an
22 empty list is configurable. Returns the fifo itself
23
24 By default an error will be thrown. You can set a custom behaviour by
25 providing a function to :setempty(). The return values of your func‐
26 tion will be returned by :pop() or :remove()
27
28 myfifo:setempty(function(myfifo) return nil end)
29
30 This method returns self, which makes it easy to use at construction
31 time: e.g. to create a new fifo where :pop() returns nil when empty:
32
33 myfifo = new_fifo():setempty(function() return nil end)
34
35 fifo:push(object:*)
36 Use the :push() method to append an object to the fifo
37
38 myfifo:push({"an object"})
39
40 object:*, exists:bool = fifo:peek(n:number|none)
41 Allows you to inspect a fifo without removing items from it. Returns
42 the item at the given index (or nil) and whether it existed (as nil is
43 a valid value). By default uses the next item from the fifo.
44
45 exists, myobject = myfifo:peek()
46
47 object:* = fifo:pop()
48 Returns the next item from the fifo, removing it.
49
50 myobject = myfifo:pop()
51
52 fifo:insert(index:number, object:*)
53 This can be used to insert an item into the middle of a fifo. The in‐
54 dex is from the output of the fifo where 1 would be the next item
55 popped from the fifo, and myfifo:length() + 1 would be equivalent to
56 :push() The efficiency of this operation is proportional to the dis‐
57 tance from either end of the fifo.
58
59 myobject = myfifo:insert(1, {"some object"})
60
61 object:* = fifo:remove(index:number)
62 This can be used to remove an item from the middle of a fifo. The in‐
63 dex is from the output of the fifo where 1 would be the next item
64 popped from the fifo, and myfifo:length() would be the input (i.e
65 equivalent to :push()) The efficiency of this operation is proportional
66 to the distance from either end of the fifo.
67
68 The object removed is returned.
69
70 myobject = myfifo:remove(2)
71
72 length:number = fifo:length() and length:number = #fifo operator
73 Returns the current number of items in the fifo. Available as
74 :length() as the __len metamethod doesn’t work for tables in lua ver‐
75 sions 5.1 and earlier.
76
78 daurnimator <quae@daurnimator.com>.
79
80
81
82 fifo.lua(3)