This site is a static rendering of the Trac instance that was used by R7RS-WG1 for its work on R7RS-small (PDF), which was ratified in 2013. For more information, see Home. For a version of this page that may be more recent, see QueuesCowan in WG2's repo for R7RS-large.

Queues­Cowan

cowan
2014-11-28 13:17:19
17history
source

Abstract

Queues (more precisely, mostly-output-restricted deques) are mutable ordered collections that can contain any Scheme object. Each queue is based on an ordinary Scheme list that contains the elements of the queue by maintaining pointers to the first and last pairs of the list. It's cheap to add or remove elements from the front of the list or to add elements to the back, but not to remove elements from the back. Queues are disjoint from other types of Scheme objects.

Rationale

Because the representation of queues as lists is exposed by the implementation, it's not necessary to provide a comprehensive API for queues, since SRFI 1 and other list APIs can serve the same purpose, using the queue->list, list->queue, and list-queue! procedures to convert between queues and lists fairly cheaply. Consequently, the API provided here over and above the bare necessities of queueing and dequeueing elements is closely analogous to the R7RS-small API for lists. It also subsumes the Chicken and SLIB APIs.

Specification

Constructors

(make-queue k [ fill ])

Returns a newly allocated queue containing k elements. If fill is provided, each element is equal to it; otherwise, the elements have unspecified values.

(queue element ...)

Returns a newly allocated queue containing the elements. This operation is O(n).

(queue-copy queue)

Returns a newly allocated queue containing the elements of queue. This operation is O(n).

(queue-unfold stop? mapper successor seed)

If the result of applying the predicate stop? to seed is #f, return a newly allocated empty queue. Otherwise, apply the procedure mapper to seed. Mapper returns a value which is added to the end of the queue. Then get a new seed by applying the procedure successor to seed, and repeat this algorithm.

(queue-unfold-right stop? mapper successor seed)

If the result of applying the predicate stop? to seed is #f, return a newly allocated empty queue. Otherwise, apply the procedure mapper to seed. Mapper returns a value which is added to the beginning of the queue. Then get a new seed by applying the procedure successor to seed, and repeat this algorithm.

Predicates

(queue? obj)

Returns #t if obj is a queue, and #f otherwise. This operation is O(1).

(queue-empty? queue)

Returns #t if obj is a queue with no elements, and #f otherwise. This operation is O(1).

Accessors

(queue-front queue element)

Returns the first element of the queue. This operation is O(1).

(queue-back queue element)

Returns the last element of the queue. This operation is O(1).

Mutators

(queue-add-front! queue element)

Adds element to the beginning of queue. Returns an unspecified value. This operation is O(1).

(queue-add-back! queue element)

Adds element to the end of queue. Returns an unspecified value. This operation is O(1).

(queue-remove-front! queue)

Removes the first element of queue and returns it. It is an error if queue is empty. This operation is O(1).

(queue-remove-back! queue)

Removes the last element of queue and returns it. It is an error if queue is empty. This operation is O(n), because the list does not have backward links.

(queue-clear! queue)

Removes all elements of queue. This operation is O(1).

The whole queue

(queue-length queue)

Returns the number of elements in queue. This operation is O(n).

(queue-append queue ...)

Returns a queue which contains all the elements in all the queues in the order in which they appear in the call. The result does not share storage with any of the arguments. This operation is O(n) in the total number of elements.

(queue-concatenate list-of-queues ...)

Returns a queue which contains all the elements in all the queues which are members of list-of-queues in the order in which they appear. The result does not share storage with any of the arguments. This operation is O(n) in the total number of elements. It is not part of the R7RS-small list API, but is included here to make appending a large number of queues possible in Schemes that limit the number of arguments to apply.

Mapping

(queue-map proc queue)

Applies proc to each element of queue in unspecified order and returns a newly allocated queue containing the results. This operation is O(n).

(queue-map! proc queue)

Applies proc to each element of queue in order and modifies queue to contain the results. This operation is O(n). It is not part of the R7RS-small list API, but is included here to make transformation of a queue by mutation more efficient.

(queue-for-each proc queue)

Applies proc to each element of queue in order, discarding the returned values. Returns an unspecified value. This operation is O(n).

Conversion and list operations

(queue->list queue)

Returns the list that contains the members of queue in order. The result shares storage with queue. This operation is O(1).

(list->queue list)

Returns a newly allocated queue containing the elements of list in order. The result shares storage with list. This operation is O(n).

To apply a non-destructive list procedure to a queue and return a new queue, use (list->queue (proc (queue->list queue))).

(list->queue! queue list)

Replaces the list associated with queue with list, effectively discarding all the elements of queue in favor of those in list. The result is unspecified. Returns an unspecified value. This operation is O(n).

To apply a destructive list procedure to a queue, use (list->queue! (proc (queue->list queue))).

(queue->first-lastqueue)

Returns two values, the first and last pairs of the list that contains the members of queue in order. If queue is empty, returns two empty lists. The results share storage with the queue. This operation is O(1).

(first-last->queue front back)

Returns a newly allocated queue containing the elements of the list whose first pair is first and whose last pair is last. Alternatively, both first and last can be the empty list. The result shares storage with the list. This operation is O(1).

(first-last->queue! queue front back)

Replaces the list associated with queue with the list whose first pair is first and whose last pair is last. Alternatively, both first and last can be the empty list. The result is unspecified. This operation is O(1).

Queues as hooks

(queue-invoke queue arg ...)

Apply each element of queue in turn to the args. It is an error if any element is not a procedure. This allows queues to be used as hooks, which various parts of a program can register interest in by adding a procedure to the queue. This operation is O(n).

Implementation note

R5RS implementations do not have make-list, list-copy, or map!, which therefore are packaged with the sample implementation. All are available in SRFI 1, but it's trivial to provide local, simplified implementations of them.