(util deque) - Deque

Library (util deque)

This library provides deque (double-ended queue) data structure and its operations.

You can create a simple deque, which is not thread-safe, or an MT deque, a thread-safe deque. Basic deque operations work on both type of deques. When a mtdeque is passed to the procedures listed in this section, each operation is done in atomic way, unless otherwise noted.

There are also a set of procedures for mtdeques that can be used for thread synchronisation; for example, you can let the consumer thread block if an mtdeque is empty, and/or the producer thread block if the number of items in the mtdeque reaches a specified limit. Using these procedures allows the program to use an mtdeque as a channel.

NOTE: (util queue) is implemented using this library.

Class <deque>

A class of simple deque.

Class <mtdeque>

A class of mtdeque. Inherits <deque>.

Function make-deque

Creates and return an empty simple deque.

Function make-mtdeque :key max-length

Creates and return an empty mtdeque.

The keyword argument max-length specifies the maximum entry count of the deque. Negative number indicates unlimited number of entry. If the given number is zero then the deque cannot hold any item.

Function deque? obj

Returns #t if obj is a deque (either a simple deque or an mtdeque).

Function deque? obj

Returns #t if obj is an mtdeque.

Function deque-empty? deque

Returns #t if deque is an empty deque.

Function deque-length deque

Returns the number of the items in the deque.

Function mtdeque-max-length mtdeque

Returns the maximum number of items mtdeque can hold. #f indicates unlimited.

Function mtdeque-room mtdeque

Returns the number of elements mtdeque can accept at this moment before it hits its maximum length. If the deque has unlimited capacity then the procedure returns +inf.0.

Function copy-deque deque

Returns a copy of deque.

Function deque-push! deque obj more-objs ...

Adds obj to the end of deque. You may give more than one object, and each of them are pushed in order.

If deque is an mtdeque, all the objects are pushed atomically; no other objects from other threads can be inserted between the objects given to a single deque-push! call. Besides, if the value of the result of mtdeque-max-length is positive, and adding objs makes the number of element in deque exceeds it, an error is raised and deque won't be modified. (If the maximum length is zero, this procedure always fail. Use deque-push/wait! below.)

Function deque-unshift! deque obj more-objs ...

Adds obj to in front of deque. You may give more than one object, and each of them are pushed in order.

Like deque-push!, when deque is an mtdeque, all objects are added atomically, and the value of max length is checked. See deque-push! above for more detail.

The name unshift is taken from Perl.

Function deque-push-unique! deque eq-proc obj more-objs ...
Function deque-unshift-unique! deque eq-proc obj more-objs ...

Like deque-push! and deque-unshift!, respectively, except that these don't modify deque if it already contains objs (elements are compared by two-argument procedure eq-proc).

When deque is an mtdeque, all objects are added atomically, and the max length is checked. See deque-push! above for the detail.

Function deque-pop! deque :optional fallback
Function deque-shift! deque :optional fallback

Take one object from the end or the front of deque, respectively, and return it.

If deque is empty, fallback is returned if give, otherwise an error is raised.

If deque is mtdeque and its max length is zero, then the deque is always empty. Use deque-pop/wait! or deque-shift/wait! to use such a deque as a synchronisation device.

The name shift is take from Perl.

Function deque-pop-all! deque
Function deque-shift-all! deque

Returns the whole content of deque by a list, with emptying deque. If deque is empty, returns an empty list.

The the returning list of deque-pop-all! is constructed from the end of queue and deque-shift-all!'s one is constructed from the front of queue.

See also deque->list below.

Function deque-front deque :optional fallback
Function deque-rear deque :optional fallback

Peek the head or the tail of deque and return the object, respectively.

If deque is empty, fallback is returned if give, otherwise an error is raised.

Function list->deque list :optional class :rest initargs

Returns a new deque which content is the elements in list, in the given order.

By default the created deque is a simple deque, but you can create mtdeque or instance of other subclass <deque> by giving the class to the optional class arguments. The optional initargs arguments are passed to the constructor of class.

Function deque->list deque

Returns a list whose content is the items in deque in order. Unlike deque-shift-all!, the content of deque remains intact. The returning list is a copy of the content. So modifying the list won't affect deque.

Function find-in-deque pred deque

Returns the first item in deque that satisfies a predicate pred.

Function any-in-deque pred deque

Apply pred on each item in deque until it evaluates true, and returns that true value. If no item satisfies pred, #f is returned.

Function every-in-deque pred deque

Apply pred on each item in deque. If pred returns #f, stops iteration and returns #f immediately. Otherwise, returns the result of pred on the last item of deque. If the deque is empty, #t is returned.

Function remove-from-deque! pred deque

Removes all items in deque that satisfies pred. Returns #t if any item is removed. Otherwise #f.

Function deque-unshift/wait! mtdeque obj :optional timeout timeout-val
Function deque-push/wait! mtdeque obj :optional timeout timeout-val
Function deque-shift/wait! mtdeque :optional timeout timeout-val
Function deque-pop/wait! mtdeque :optional timeout timeout-val

These synchronising variants work on an mtdeque and make the caller thread block when the mtdeque has reached its maximum length (for deque-unshift/wait! and deque-push/wait!), or the mtdeque is empty (for deque-shift/wait! and deque-pop/wait!). The blocked caller thread is unblocked either the blocking condition is resolved, or the timeout condition is met.

The optional timeout argument specifies the timeout condition. If it is #f, those procedure wait indefinitely. If it is a real number, they wait at least the given number of seconds.

In case the call is blocked then timed out, the value of timeout-val is returned, which default value is #t.

When deque-unshift/wait! and deque-push/wait! succeeds without hitting timeout, they return #t.