summaryrefslogtreecommitdiff
path: root/lib/stdlib/doc/src/queue.xml
blob: 5ada1c2c57d217feba549f27385c257ef6f3e22c (plain)
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
<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1996</year><year>2009</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.
    
      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.
    
    </legalnotice>

    <title>queue</title>
    <prepared>Joe</prepared>
    <responsible>Bjarne D&auml;cker</responsible>
    <docno>1</docno>
    <approved>Bjarne D&auml;cker</approved>
    <checked></checked>
    <date>97-01-15</date>
    <rev>B</rev>
    <file>queue.sgml</file>
  </header>
  <module>queue</module>
  <modulesummary>Abstract Data Type for FIFO Queues</modulesummary>
  <description>
    <p>This module implements (double ended) FIFO queues
      in an efficient manner.</p>
    <p>All functions fail with reason <c>badarg</c> 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 <c>badarg</c>.</p>
    <p>Some functions, where noted, fail with reason <c>empty</c>
      for an empty queue.</p>
    <p>The data representing a queue as used by this module
      should be regarded as opaque by other modules. Any code
      assuming knowledge of the format is running on thin ice.</p>
    <p>All operations has an amortized O(1) running time, except
      <c>len/1</c>, <c>join/2</c>, <c>split/2</c>, <c>filter/2</c>
      and <c>member/2</c> that have 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 <c>len/1</c> is O(n). If better performance for this
      particular operation is essential, it is easy for
      the caller to keep track of the length.</p>
    <p>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.</p>
    <p>Entering at the front and exiting at the rear
      are reverse operations on the queue.</p>
    <p>The module has several sets of interface functions. The
      "Original API", the "Extended API" and the "Okasaki API".</p>
    <p>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".</p>
    <p>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.</p>
    <p>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.</p>
  </description>



  <section>
    <title>Original API</title>
  </section>

  <funcs>
    <func>
      <name>new() -> Q</name>
      <fsummary>Create an empty queue</fsummary>
      <type>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns an empty queue.</p>
      </desc>
    </func>
    <func>
      <name>is_queue(Term) -> true | false</name>
      <fsummary>Test if a term is a queue</fsummary>
      <type>
        <v>Term = term()</v>
      </type>
      <desc>
        <p>Tests if <c>Q</c> is a queue and returns <c>true</c> if so and
          <c>false</c> otherwise.</p>
      </desc>
    </func>
    <func>
      <name>is_empty(Q) -> true | false</name>
      <fsummary>Test if a queue is empty</fsummary>
      <type>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Tests if <c>Q</c> is empty and returns <c>true</c> if so and
          <c>false</c> otherwise.</p>
      </desc>
    </func>
    <func>
      <name>len(Q) -> N</name>
      <fsummary>Get the length of a queue</fsummary>
      <type>
        <v>Q = queue()</v>
        <v>N = integer()</v>
      </type>
      <desc>
        <p>Calculates and returns the length of queue <c>Q</c>.</p>
      </desc>
    </func>

    <func>
      <name>in(Item, Q1) -> Q2</name>
      <fsummary>Insert an item at the rear of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Inserts <c>Item</c> at the rear of queue <c>Q1</c>.
          Returns the resulting queue <c>Q2</c>.</p>
      </desc>
    </func>
    <func>
      <name>in_r(Item, Q1) -> Q2</name>
      <fsummary>Insert an item at the front of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Inserts <c>Item</c> at the front of queue <c>Q1</c>.
          Returns the resulting queue <c>Q2</c>.</p>
      </desc>
    </func>
    <func>
      <name>out(Q1) -> Result</name>
      <fsummary>Remove the front item from a queue</fsummary>
      <type>
        <v>Result = {{value, Item}, Q2} | {empty, Q1}</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Removes the item at the front of queue <c>Q1</c>. Returns the
          tuple <c>{{value, Item}, Q2}</c>, where <c>Item</c> is the
          item removed and <c>Q2</c> is the resulting queue. If <c>Q1</c> is
          empty, the tuple <c>{empty, Q1}</c> is returned.</p>
      </desc>
    </func>
    <func>
      <name>out_r(Q1) -> Result</name>
      <fsummary>Remove the rear item from a queue</fsummary>
      <type>
        <v>Result = {{value, Item}, Q2} | {empty, Q1}</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Removes the item at the rear of the queue <c>Q1</c>. Returns the
          tuple <c>{{value, Item}, Q2}</c>, where <c>Item</c> is the 
          item removed and <c>Q2</c> is the new queue. If <c>Q1</c> is
          empty, the tuple <c>{empty, Q1}</c> is returned.  </p>
      </desc>
    </func>

    <func>
      <name>from_list(L) -> queue()</name>
      <fsummary>Convert a list to a queue</fsummary>
      <type>
        <v>L = list()</v>
      </type>
      <desc>
        <p>Returns a queue containing the items in <c>L</c> in the
          same order; the head item of the list will become the front
          item of the queue.</p>
      </desc>
    </func>
    <func>
      <name>to_list(Q) -> list()</name>
      <fsummary>Convert a queue to a list</fsummary>
      <type>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>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.</p>
      </desc>
    </func>

    <func>
      <name>reverse(Q1) -> Q2</name>
      <fsummary>Reverse a queue</fsummary>
      <type>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Returns a queue <c>Q2</c> that contains the items of
          <c>Q1</c> in the reverse order.</p>
      </desc>
    </func>
    <func>
      <name>split(N, Q1) -> {Q2,Q3}</name>
      <fsummary>Split a queue in two</fsummary>
      <type>
        <v>N = integer()</v>
        <v>Q1 = Q2 = Q3 = queue()</v>
      </type>
      <desc>
        <p>Splits <c>Q1</c> in two. The <c>N</c> front items
          are put in <c>Q2</c> and the rest in <c>Q3</c></p>
      </desc>
    </func>
    <func>
      <name>join(Q1, Q2) -> Q3</name>
      <fsummary>Join two queues</fsummary>
      <type>
        <v>Q1 = Q2 = Q3 = queue()</v>
      </type>
      <desc>
        <p>Returns a queue <c>Q3</c> that is the result of joining
          <c>Q1</c> and <c>Q2</c> with <c>Q1</c> in front of
          <c>Q2</c>.</p>
      </desc>
    </func>
    <func>
      <name>filter(Fun, Q1) -> Q2</name>
      <fsummary>Filter a queue</fsummary>
      <type>
        <v>Fun = fun(Item) -> bool() | list()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Returns a queue <c>Q2</c> that is the result of calling
          <c>Fun(Item)</c> on all items in <c>Q1</c>,
          in order from front to rear.</p>
        <p>If <c>Fun(Item)</c> returns <c>true</c>, <c>Item</c>
          is copied to the result queue. If it returns <c>false</c>,
          <c>Item</c> is not copied. If it returns a list
          the list elements are inserted instead of <c>Item</c> in the
          result queue.</p>
        <p>So, <c>Fun(Item)</c> returning <c>[Item]</c> is thereby
          semantically equivalent to returning <c>true</c>, just
          as returning <c>[]</c> is semantically equivalent to
          returning <c>false</c>. But returning a list builds
          more garbage than returning an atom.</p>
      </desc>
    </func>
    <func>
      <name>member(Item, Q) -> bool()</name>
      <fsummary>Test if an item is in a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns <c>true</c> if <c>Item</c> matches some element
          in <c>Q</c>, otherwise <c>false</c>.</p>
      </desc>
    </func>
  </funcs>



  <section>
    <title>Extended API</title>
  </section>

  <funcs>
    <func>
      <name>get(Q) -> Item</name>
      <fsummary>Return the front item of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns <c>Item</c> at the front of queue <c>Q</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>get_r(Q) -> Item</name>
      <fsummary>Return the rear item of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns <c>Item</c> at the rear of queue <c>Q</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>drop(Q1) -> Q2</name>
      <fsummary>Remove the front item from a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Returns a queue <c>Q2</c> that is the result of removing
          the front item from <c>Q1</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q1</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>drop_r(Q1) -> Q2</name>
      <fsummary>Remove the rear item from a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Returns a queue <c>Q2</c> that is the result of removing
          the rear item from <c>Q1</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q1</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>peek(Q) -> {value,Item} | empty</name>
      <fsummary>Return the front item of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns the tuple <c>{value, Item}</c> where <c>Item</c> is the
          front item of <c>Q</c>, or <c>empty</c> if <c>Q1</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>peek_r(Q) -> {value,Item} | empty</name>
      <fsummary>Return the rear item of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns the tuple <c>{value, Item}</c> where <c>Item</c> is the
          rear item of <c>Q</c>, or <c>empty</c> if <c>Q1</c> is empty.</p>
      </desc>
    </func>
  </funcs>


  <section>
    <title>Okasaki API</title>
  </section>

  <funcs>
    <func>
      <name>cons(Item, Q1) -> Q2</name>
      <fsummary>Insert an item at the head of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Inserts <c>Item</c> at the head of queue <c>Q1</c>. Returns
          the new queue <c>Q2</c>.</p>
      </desc>
    </func>
    <func>
      <name>head(Q) -> Item</name>
      <fsummary>Return the item at the head of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns <c>Item</c> from the head of queue <c>Q</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>tail(Q1) -> Q2</name>
      <fsummary>Remove the head item from a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Returns a queue <c>Q2</c> that is the result of removing
          the head item from <c>Q1</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q1</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>snoc(Q1, Item) -> Q2</name>
      <fsummary>Insert an item at the tail of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Inserts <c>Item</c> as the tail item of queue <c>Q1</c>. Returns
          the new queue <c>Q2</c>.</p>
      </desc>
    </func>
    <func>
      <name>daeh(Q) -> Item</name>
      <name>last(Q) -> Item</name>
      <fsummary>Return the tail item of a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q = queue()</v>
      </type>
      <desc>
        <p>Returns the tail item of queue <c>Q</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q</c> is empty.</p>
      </desc>
    </func>
    <func>
      <name>liat(Q1) -> Q2</name>
      <name>init(Q1) -> Q2</name>
      <name>lait(Q1) -> Q2</name>
      <fsummary>Remove the tail item from a queue</fsummary>
      <type>
        <v>Item = term()</v>
        <v>Q1 = Q2 = queue()</v>
      </type>
      <desc>
        <p>Returns a queue <c>Q2</c> that is the result of removing
          the tail item from <c>Q1</c>.</p>
        <p>Fails with reason <c>empty</c> if <c>Q1</c> is empty.</p>
        <p>The name <c>lait/1</c> is a misspelling - do not use it anymore.</p>
      </desc>
    </func>
  </funcs>

</erlref>