1queue(3)                   Erlang Module Definition                   queue(3)
2
3
4

NAME

6       queue - Abstract data type for FIFO queues.
7

DESCRIPTION

9       This module provides (double-ended) FIFO queues in an efficient manner.
10
11       All  functions  fail with reason badarg if arguments are of wrong type,
12       for example, queue arguments are not queues, indexes are not  integers,
13       and  list  arguments  are  not  lists.  Improper  lists  cause internal
14       crashes. An index out of range for a queue also causes a  failure  with
15       reason badarg.
16
17       Some functions, where noted, fail with reason empty for an empty queue.
18
19       The  data representing a queue as used by this module is to be regarded
20       as opaque by other modules. Any code assuming knowledge of  the  format
21       is running on thin ice.
22
23       All  operations  have  an  amortized  O(1)  running time, except all/2,
24       any/2, delete/2, delete_r/2, delete_with/2, delete_with_r/2,  filter/2,
25       filtermap/2,  fold/3,  join/2, len/1, member/2, split/2 that have O(n).
26       To minimize the size of a queue minimizing the amount of garbage  built
27       by queue operations, the queues do not contain explicit length informa‐
28       tion, and that is why len/1 is O(n). If  better  performance  for  this
29       particular  operation  is  essential, it is easy for the caller to keep
30       track of the length.
31
32       Queues are double-ended. The mental picture of a queue  is  a  line  of
33       people  (items) waiting for their turn. The queue front is the end with
34       the item that has waited the longest. The queue rear is the end an item
35       enters when it starts to wait. If instead using the mental picture of a
36       list, the front is called head and the rear is called tail.
37
38       Entering at the front and exiting at the rear are reverse operations on
39       the queue.
40
41       This  module has three sets of interface functions: the "Original API",
42       the "Extended API", and the "Okasaki API".
43
44       The "Original API" and the "Extended API" both use the  mental  picture
45       of a waiting line of items. Both have reverse operations suffixed "_r".
46
47       The  "Original  API"  item removal functions return compound terms with
48       both the removed item and the resulting queue. The "Extended API"  con‐
49       tains  alternative  functions that build less garbage and functions for
50       just inspecting the queue ends. Also the "Okasaki API" functions  build
51       less garbage.
52
53       The "Okasaki API" is inspired by "Purely Functional Data Structures" by
54       Chris Okasaki. It regards queues as lists. This API is by many regarded
55       as  strange  and  avoidable.  For example, many reverse operations have
56       lexically reversed names, some with more readable but perhaps less  un‐
57       derstandable aliases.
58

DATA TYPES

60       queue(Item)
61
62              As returned by new/0.
63
64       queue() = queue(term())
65

ORIGINAL API

EXPORTS

68       all(Pred, Q :: queue(Item)) -> boolean()
69
70              Types:
71
72                 Pred = fun((Item) -> boolean())
73
74              Returns true if Pred(Item) returns true for all items Item in Q,
75              otherwise false.
76
77       any(Pred, Q :: queue(Item)) -> boolean()
78
79              Types:
80
81                 Pred = fun((Item) -> boolean())
82
83              Returns true if Pred(Item) returns true for at  least  one  item
84              Item in Q, otherwise false.
85
86       delete(Item, Q1) -> Q2
87
88              Types:
89
90                 Item = T
91                 Q1 = Q2 = queue(T)
92                 T = term()
93
94              Returns  a  copy  of  Q1  where  the first item matching Item is
95              deleted, if there is such an item.
96
97       delete_r(Item, Q1) -> Q2
98
99              Types:
100
101                 Item = T
102                 Q1 = Q2 = queue(T)
103                 T = term()
104
105              Returns a copy of Q1  where  the  last  item  matching  Item  is
106              deleted, if there is such an item.
107
108       delete_with(Pred, Q1) -> Q2
109
110              Types:
111
112                 Pred = fun((Item) -> boolean())
113                 Q1 = Q2 = queue(Item)
114                 Item = term()
115
116              Returns a copy of Q1 where the first item for which Pred returns
117              true is deleted, if there is such an item.
118
119       delete_with_r(Pred, Q1) -> Q2
120
121              Types:
122
123                 Pred = fun((Item) -> boolean())
124                 Q1 = Q2 = queue(Item)
125                 Item = term()
126
127              Returns a copy of Q1 where the last item for which Pred  returns
128              true is deleted, if there is such an item.
129
130       filter(Fun, Q1 :: queue(Item)) -> Q2 :: queue(Item)
131
132              Types:
133
134                 Fun = fun((Item) -> boolean() | [Item])
135
136              Returns  a  queue  Q2 that is the result of calling Fun(Item) on
137              all items in Q1.
138
139              If Fun(Item) returns true, Item is copied to the  result  queue.
140              If  it  returns false, Item is not copied. If it returns a list,
141              the list elements are inserted instead of  Item  in  the  result
142              queue.
143
144              So,  Fun(Item)  returning [Item] is thereby semantically equiva‐
145              lent to returning true, just as  returning  []  is  semantically
146              equivalent  to returning false. But returning a list builds more
147              garbage than returning an atom.
148
149       filtermap(Fun, Q1) -> Q2
150
151              Types:
152
153                 Fun = fun((Item) -> boolean() | {true, Value})
154                 Q1 = queue(Item)
155                 Q2 = queue(Item | Value)
156                 Item = Value = term()
157
158              Returns a queue Q2 that is the result of  calling  Fun(Item)  on
159              all items in Q1.
160
161              If  Fun(Item)  returns true, Item is copied to the result queue.
162              If it returns false, Item is not copied. If  it  returns  {true,
163              NewItem},  the  queue  element at this position is replaced with
164              NewItem in the result queue.
165
166       fold(Fun, Acc0, Q :: queue(Item)) -> Acc1
167
168              Types:
169
170                 Fun = fun((Item, AccIn) -> AccOut)
171                 Acc0 = Acc1 = AccIn = AccOut = term()
172
173              Calls Fun(Item, AccIn) on successive items Item of Queue, start‐
174              ing  with  AccIn == Acc0. The queue is traversed in queue order,
175              that is, from front to rear. Fun/2 must return a  new  accumula‐
176              tor,  which is passed to the next call. The function returns the
177              final value of the accumulator. Acc0 is returned if the queue is
178              empty.
179
180              Example:
181
182              > queue:fold(fun(X, Sum) -> X + Sum end, 0, queue:from_list([1,2,3,4,5])).
183              15
184              > queue:fold(fun(X, Prod) -> X * Prod end, 1, queue:from_list([1,2,3,4,5])).
185              120
186
187       from_list(L :: [Item]) -> queue(Item)
188
189              Returns a queue containing the items in L in the same order; the
190              head item of the list becomes the front item of the queue.
191
192       in(Item, Q1 :: queue(Item)) -> Q2 :: queue(Item)
193
194              Inserts Item at the rear of  queue  Q1.  Returns  the  resulting
195              queue Q2.
196
197       in_r(Item, Q1 :: queue(Item)) -> Q2 :: queue(Item)
198
199              Inserts  Item  at  the  front of queue Q1. Returns the resulting
200              queue Q2.
201
202       is_empty(Q :: queue()) -> boolean()
203
204              Tests if Q is empty and returns true if so, otherwise false.
205
206       is_queue(Term :: term()) -> boolean()
207
208              Tests if Term is a queue  and  returns  true  if  so,  otherwise
209              false.
210
211       join(Q1 :: queue(Item), Q2 :: queue(Item)) -> Q3 :: queue(Item)
212
213              Returns  a queue Q3 that is the result of joining Q1 and Q2 with
214              Q1 in front of Q2.
215
216       len(Q :: queue()) -> integer() >= 0
217
218              Calculates and returns the length of queue Q.
219
220       member(Item, Q :: queue(Item)) -> boolean()
221
222              Returns true if Item matches some element in Q, otherwise false.
223
224       new() -> queue()
225
226              Returns an empty queue.
227
228       out(Q1 :: queue(Item)) ->
229              {{value, Item}, Q2 :: queue(Item)} |
230              {empty, Q1 :: queue(Item)}
231
232              Removes the item  at  the  front  of  queue  Q1.  Returns  tuple
233              {{value,  Item},  Q2},  where Item is the item removed and Q2 is
234              the resulting queue. If Q1 is empty, tuple {empty,  Q1}  is  re‐
235              turned.
236
237       out_r(Q1 :: queue(Item)) ->
238                {{value, Item}, Q2 :: queue(Item)} |
239                {empty, Q1 :: queue(Item)}
240
241              Removes the item at the rear of queue Q1. Returns tuple {{value,
242              Item}, Q2}, where Item is the item removed and  Q2  is  the  new
243              queue. If Q1 is empty, tuple {empty, Q1} is returned.
244
245       reverse(Q1 :: queue(Item)) -> Q2 :: queue(Item)
246
247              Returns a queue Q2 containing the items of Q1 in the reverse or‐
248              der.
249
250       split(N :: integer() >= 0, Q1 :: queue(Item)) ->
251                {Q2 :: queue(Item), Q3 :: queue(Item)}
252
253              Splits Q1 in two. The N front items are put in Q2 and  the  rest
254              in Q3.
255
256       to_list(Q :: queue(Item)) -> [Item]
257
258              Returns  a list of the items in the queue in the same order; the
259              front item of the queue becomes the head of the list.
260

EXTENDED API

EXPORTS

263       drop(Q1 :: queue(Item)) -> Q2 :: queue(Item)
264
265              Returns a queue Q2 that is the result of removing the front item
266              from Q1.
267
268              Fails with reason empty if Q1 is empty.
269
270       drop_r(Q1 :: queue(Item)) -> Q2 :: queue(Item)
271
272              Returns  a queue Q2 that is the result of removing the rear item
273              from Q1.
274
275              Fails with reason empty if Q1 is empty.
276
277       get(Q :: queue(Item)) -> Item
278
279              Returns Item at the front of queue Q.
280
281              Fails with reason empty if Q is empty.
282
283       get_r(Q :: queue(Item)) -> Item
284
285              Returns Item at the rear of queue Q.
286
287              Fails with reason empty if Q is empty.
288
289       peek(Q :: queue(Item)) -> empty | {value, Item}
290
291              Returns tuple {value, Item}, where Item is the front item of  Q,
292              or empty if Q is empty.
293
294       peek_r(Q :: queue(Item)) -> empty | {value, Item}
295
296              Returns  tuple  {value, Item}, where Item is the rear item of Q,
297              or empty if Q is empty.
298

OKASAKI API

EXPORTS

301       cons(Item, Q1 :: queue(Item)) -> Q2 :: queue(Item)
302
303              Inserts Item at the head of queue Q1. Returns the new queue Q2.
304
305       daeh(Q :: queue(Item)) -> Item
306
307              Returns the tail item of queue Q.
308
309              Fails with reason empty if Q is empty.
310
311       head(Q :: queue(Item)) -> Item
312
313              Returns Item from the head of queue Q.
314
315              Fails with reason empty if Q is empty.
316
317       init(Q1 :: queue(Item)) -> Q2 :: queue(Item)
318
319              Returns a queue Q2 that is the result of removing the tail  item
320              from Q1.
321
322              Fails with reason empty if Q1 is empty.
323
324       lait(Q1 :: queue(Item)) -> Q2 :: queue(Item)
325
326              Returns  a queue Q2 that is the result of removing the tail item
327              from Q1.
328
329              Fails with reason empty if Q1 is empty.
330
331              The name lait/1 is a misspelling - do not use it anymore.
332
333       last(Q :: queue(Item)) -> Item
334
335              Returns the tail item of queue Q.
336
337              Fails with reason empty if Q is empty.
338
339       liat(Q1 :: queue(Item)) -> Q2 :: queue(Item)
340
341              Returns a queue Q2 that is the result of removing the tail  item
342              from Q1.
343
344              Fails with reason empty if Q1 is empty.
345
346       snoc(Q1 :: queue(Item), Item) -> Q2 :: queue(Item)
347
348              Inserts Item as the tail item of queue Q1. Returns the new queue
349              Q2.
350
351       tail(Q1 :: queue(Item)) -> Q2 :: queue(Item)
352
353              Returns a queue Q2 that is the result of removing the head  item
354              from Q1.
355
356              Fails with reason empty if Q1 is empty.
357
358
359
360Ericsson AB                      stdlib 3.17.2                        queue(3)
Impressum