9.3
Racket
In fact, all of the procedures in this module complete in
approximately constant time except fifo-length, whose
performance is proportional to the number of items in the FIFO queue (being
effectively the mutable list version of length).
A simple FIFO queue implemented using a mutable list wrapped in a vector whose
elements point to the head and tail of the list to achieve more or less constant
performance when adding or removing items from the queue.
https://github.com/parasaurolophus/fifo
Theory of operation, in pseudo-code:
(define fifo (make‑fifo)) | fifo is [’(),’()]
|
(fifo‑push! fifo ’a) | fifo is [e1, e1] where e1 is (mcons ’a ’())
|
(fifo‑push! fifo ’b) | fifo is [e1, e2] where e2 is (mcons ’b ’()) and e1’s mcdr has been updated to e2
|
(fifo‑push! fifo ’c) | fifo is [e1, e3] where e3 is (mcons ’c ’()) and e2’s mcdr has been updated to e3
|
(fifo‑pop! fifo) | returns ’a and fifo is [e2, e3]
|
(fifo‑pop! fifo) | returns ’b and fifo is [e3, e3]
|
(fifo‑pop! fifo) | returns ’c and fifo is [’(), ’()]
|
(fifo‑pop! fifo) | returns #<void> and fifo is unchanged
|
Return a new FIFO initialized
with the given elements.
(fifo-empty? fifo) → boolean?
|
| fifo : vector? |
Return #t if and only if fifo contains any elements, #f otherwise.
(fifo-length fifo) → number?
|
| fifo : vector? |
Return the number of elements in fifo.
(fifo-pop! fifo) → any
|
| fifo : vector? |
Remove and return the next element from fifo or void
(fifo-push! fifo element) → void?
|
| fifo : vector? |
| element : any/c |
Add element to the end of fifo.