File: queue.3

package info (click to toggle)
erlang-manpages 1%3A12.b.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,188 kB
  • ctags: 2
  • sloc: makefile: 68; perl: 30; sh: 15
file content (464 lines) | stat: -rw-r--r-- 8,665 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
.TH queue 3 "stdlib  1.15.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
queue \- Abstract Data Type for FIFO Queues
.SH DESCRIPTION
.LP
This module implements (double ended) FIFO queues in an efficient manner\&.
.LP
All functions fail with reason \fIbadarg\fR if arguments are of wrong type, for example queue arguments are not queues, indexes are not integers, list arguments are not lists\&. Improper lists cause internal crashes\&. An index out of range for a queue also causes a failure with reason \fIbadarg\fR\&.
.LP
Some functions, where noted, fail with reason \fIempty\fR for an empty queue\&.
.LP
All operations has an amortized O(1) running time, except \fIlen/1\fR, \fIjoin/2\fR, \fIsplit/2\fR and \fIfilter/2\fR that are O(n)\&. To minimize the size of a queue minimizing the amount of garbage built by queue operations, the queues do not contain explicit length information, and that is why \fIlen/1\fR is O(n)\&. If better performance for this particular operation is essential, it is easy for the caller to keep track of the length\&.
.LP
Queues are double ended\&. The mental picture of a queue is a line of people (items) waiting for their turn\&. The queue front is the end with the item that has waited the longest\&. The queue rear is the end an item enters when it starts to wait\&. If instead using the mental picture of a list, the front is called head and the rear is called tail\&.
.LP
Entering at the front and exiting at the rear are reverse operations on the queue\&.
.LP
The module has several sets of interface functions\&. The "Original API", the "Extended API" and the "Okasaki API"\&.
.LP
The "Original API" and the "Extended API" both use the mental picture of a waiting line of items\&. Both also have reverse operations suffixed "_r"\&.
.LP
The "Original API" item removal functions return compound terms with both the removed item and the resulting queue\&. The "Extended API" contain alternative functions that build less garbage as well as functions for just inspecting the queue ends\&. Also the "Okasaki API" functions build less garbage\&.
.LP
The "Okasaki API" is inspired by "Purely Functional Data structures" by Chris Okasaki\&. It regards queues as lists\&. The API is by many regarded as strange and avoidable\&. For example many reverse operations have lexically reversed names, some with more readable but perhaps less understandable aliases\&.

.SH ORIGINAL API
.SH EXPORTS
.LP
.B
new() -> Q
.br
.RS
.TP
Types
Q = queue()
.br
.RE
.RS
.LP
Returns an empty queue\&.
.RE
.LP
.B
is_queue(Term) -> true | false
.br
.RS
.TP
Types
Term = term()
.br
.RE
.RS
.LP
Tests if \fIQ\fR is a queue and returns \fItrue\fR if so and \fIfalse\fR otherwise\&.
.RE
.LP
.B
is_empty(Q) -> true | false
.br
.RS
.TP
Types
Q = queue()
.br
.RE
.RS
.LP
Tests if \fIQ\fR is empty and returns \fItrue\fR if so and \fIfalse\fR otherwise\&.
.RE
.LP
.B
len(Q) -> N
.br
.RS
.TP
Types
Q = queue()
.br
N = integer()
.br
.RE
.RS
.LP
Calculates and returns the length of queue \fIQ\fR\&.
.RE
.LP
.B
in(Item, Q1) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Inserts \fIItem\fR at the rear of queue \fIQ1\fR\&. Returns the resulting queue \fIQ2\fR\&.
.RE
.LP
.B
in_r(Item, Q1) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Inserts \fIItem\fR at the front of queue \fIQ1\fR\&. Returns the resulting queue \fIQ2\fR\&.
.RE
.LP
.B
out(Q1) -> Result
.br
.RS
.TP
Types
Result = {{value, Item}, Q2} | {empty, Q1}
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Removes the item at the front of queue \fIQ1\fR\&. Returns the tuple \fI{{value, Item}, Q2}\fR, where \fIItem\fR is the item removed and \fIQ2\fR is the resulting queue\&. If \fIQ1\fR is empty, the tuple \fI{empty, Q1}\fR is returned\&.
.RE
.LP
.B
out_r(Q1) -> Result
.br
.RS
.TP
Types
Result = {{value, Item}, Q2} | {empty, Q1}
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Removes the item at the rear of the queue \fIQ1\fR\&. Returns the tuple \fI{{value, Item}, Q2}\fR, where \fIItem\fR is the item removed and \fIQ2\fR is the new queue\&. If \fIQ1\fR is empty, the tuple \fI{empty, Q1}\fR is returned\&. 
.RE
.LP
.B
from_list(L) -> queue()
.br
.RS
.TP
Types
L = list()
.br
.RE
.RS
.LP
Returns a queue containing the items in \fIL\fR in the same order; the head item of the list will become the front item of the queue\&.
.RE
.LP
.B
to_list(Q) -> list()
.br
.RS
.TP
Types
Q = queue()
.br
.RE
.RS
.LP
Returns a list of the items in the queue in the same order; the front item of the queue will become the head of the list\&.
.RE
.LP
.B
reverse(Q1) -> Q2
.br
.RS
.TP
Types
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Returns a queue \fIQ2\fR that contains the items of \fIQ1\fR in the reverse order\&.
.RE
.LP
.B
split(N, Q1) -> {Q2,Q3}
.br
.RS
.TP
Types
N = integer()
.br
Q1 = Q2 = Q3 = queue()
.br
.RE
.RS
.LP
Splits \fIQ1\fR in two\&. The \fIN\fR front items are put in \fIQ2\fR and the rest in \fIQ3\fR
.RE
.LP
.B
join(Q1, Q2) -> Q3
.br
.RS
.TP
Types
Q1 = Q2 = Q3 = queue()
.br
.RE
.RS
.LP
Returns a queue \fIQ3\fR that is the result of joining \fIQ1\fR and \fIQ2\fR with \fIQ1\fR in front of \fIQ2\fR\&.
.RE
.LP
.B
filter(Fun, Q1) -> Q2
.br
.RS
.TP
Types
Fun = fun(Item) -> bool() | list()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Returns a queue \fIQ2\fR that is the result of calling \fIFun(Item)\fR on all items in \fIQ1\fR, in order from front to rear\&.
.LP
If \fIFun(Item)\fR returns \fItrue\fR, \fIItem\fR is copied to the result queue\&. If it returns \fIfalse\fR, \fIItem\fR is not copied\&. If it returns a list the list elements are inserted instead of \fIItem\fR in the result queue\&.
.LP
So, \fIFun(Item)\fR returning \fI[Item]\fR is thereby semantically equivalent to returning \fItrue\fR, just as returning \fI[]\fR is semantically equivalent to returning \fIfalse\fR\&. But returning a list builds more garbage than returning an atom\&.
.RE
.SH EXTENDED API
.SH EXPORTS
.LP
.B
get(Q) -> Item
.br
.RS
.TP
Types
Item = term()
.br
Q = queue()
.br
.RE
.RS
.LP
Returns \fIItem\fR at the front of queue \fIQ\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ\fR is empty\&.
.RE
.LP
.B
get_r(Q) -> Item
.br
.RS
.TP
Types
Item = term()
.br
Q = queue()
.br
.RE
.RS
.LP
Returns \fIItem\fR at the rear of queue \fIQ\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ\fR is empty\&.
.RE
.LP
.B
drop(Q1) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Returns a queue \fIQ2\fR that is the result of removing the front item from \fIQ1\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ1\fR is empty\&.
.RE
.LP
.B
drop_r(Q1) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Returns a queue \fIQ2\fR that is the result of removing the rear item from \fIQ1\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ1\fR is empty\&.
.RE
.LP
.B
peek(Q) -> {value,Item} | empty
.br
.RS
.TP
Types
Item = term()
.br
Q = queue()
.br
.RE
.RS
.LP
Returns the tuple \fI{value, Item}\fR where \fIItem\fR is the front item of \fIQ\fR, or \fIempty\fR if \fIQ1\fR is empty\&.
.RE
.LP
.B
peek_r(Q) -> {value,Item} | empty
.br
.RS
.TP
Types
Item = term()
.br
Q = queue()
.br
.RE
.RS
.LP
Returns the tuple \fI{value, Item}\fR where \fIItem\fR is the rear item of \fIQ\fR, or \fIempty\fR if \fIQ1\fR is empty\&.
.RE
.SH OKASAKI API
.SH EXPORTS
.LP
.B
cons(Item, Q1) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Inserts \fIItem\fR at the head of queue \fIQ1\fR\&. Returns the new queue \fIQ2\fR\&.
.RE
.LP
.B
head(Q) -> Item
.br
.RS
.TP
Types
Item = term()
.br
Q = queue()
.br
.RE
.RS
.LP
Returns \fIItem\fR from the head of queue \fIQ\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ\fR is empty\&.
.RE
.LP
.B
tail(Q1) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Returns a queue \fIQ2\fR that is the result of removing the head item from \fIQ1\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ1\fR is empty\&.
.RE
.LP
.B
snoc(Q1, Item) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Inserts \fIItem\fR as the tail item of queue \fIQ1\fR\&. Returns the new queue \fIQ2\fR\&.
.RE
.LP
.B
daeh(Q) -> Item
.br
.B
last(Q) -> Item
.br
.RS
.TP
Types
Item = term()
.br
Q = queue()
.br
.RE
.RS
.LP
Returns the tail item of queue \fIQ\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ\fR is empty\&.
.RE
.LP
.B
liat(Q1) -> Q2
.br
.B
init(Q1) -> Q2
.br
.B
lait(Q1) -> Q2
.br
.RS
.TP
Types
Item = term()
.br
Q1 = Q2 = queue()
.br
.RE
.RS
.LP
Returns a queue \fIQ2\fR that is the result of removing the tail item from \fIQ1\fR\&.
.LP
Fails with reason \fIempty\fR if \fIQ1\fR is empty\&.
.LP
The name \fIlait/1\fR is a misspelling - do not use it anymore\&.
.RE