Pileup

Pileup provides a portable, performant, and thread-safe binary heap for Common Lisp, licensed under MIT-style license.

It depends on Alexandria, and outside SBCL additionally on Bordeaux-Threads.

Pileup is maintained in Git:

     git clone git://github.com/nikodemus/pileup.git

will get you a local copy.

      http://github.com/nikodemus/pileup

is the GitHub project page.

Table of Contents

1 Making Heaps

— Structure: heap

Class precedence list: heap, structure-object, t

A thread-safe binary heap.

Heap operations which need the heap to remain consistent heap lock it. Users can also group multiple heap operations into atomic units using with-locked-heap.

Thread-safety is implemented using a single lock per heap. While Pileup heaps are fine for threaded use, a more specialized solution is recommended when the heap is highly contested between multiple threads.

Important: Pileup heaps are not asynch-unwind safe: asynchronous interrupts causing non-local exits may leave the heap in an inconsistent state or lose data. Do not use interrupt-thread or asychronous timeouts with Pileup.

All slot names in heap are internal to the pileup package, so it is safe to subclass using eg. defstruct :include, as long as only the exported operations are used to accessor or modify heap state.

— Function: make-heap predicate &key name size key

Constructs a heap.

The predicate determines the ordering of the heap. It must be a function of two arguments, returning true if the first argument should be closer to top of the heap than the second. If a predicate signals an error and causes a non-local exit from a heap operation, it may leave the heap in an inconsistent state and cause a subsequent heap operation to signal an error.

If key is not nil, it must be a function of one argument, and is used to extract values for use by predicate for comparison.

The name can be used to optionally specify a name for the heap: it affects only printing of the heap.

The size is the size of the storage initially reserved for the heap. Specifying size is not necessary: the heap will grow as necessary, but a reasonable estimate can improve performance by eliminating unnecessary copying by allocating sufficient storage immediately.

2 Heap Operations

— Macro: with-locked-heap (heap) &body body

Executes body with heap locked. Heap operations which implicitly lock the heap are: heap-insert, heap-pop, heap-delete, and map-heap. Allows grouping multiple heap operations into atomic units.

— Function: heap-insert elt heap

Insert elt to heap. Returns elt.

Locks the heap during its operation unless the current thread is already holding the heap lock via with-locked-heap.

— Function: heap-pop heap

Removes and returns the element at the top of the heap and a secondary value of t. Should the heap be empty, both the primary and the secondary values are nil.

Locks the heap during its operation unless the current thread is already holding the heap lock via with-locked-heap.

— Function: heap-top heap

Returns the element at the top of the heap without removing it, and a secondary value of t. Should the heap be empty, both the primary and the secondary values are nil.

— Function: heap-delete elt heap &key count

Removes elements of the heap eql to elt. Returns t if one or more elements were found and removed, nil otherwise.

If count is nil (the default), removes all elements eql to elt, otherwise at most the indicated number.

Locks the heap during its operation unless the current thread is already holding the heap lock via with-locked-heap.

— Function: map-heap function heap &key ordered

Calls function for each element in heap. Returns the heap.

If ordered is true (the default), processes the elements in heap order from top down.

If ordered is false, uses unordered traversal. Unordered traversal is faster and also works on heaps that have been corrupted by eg. the heap predicate performing a non-local exit from a heap operation.

Attempts to insert or delete elements to the heap from function will cause an error to be signalled.

Locks the heap during its operation unless the current thread is already holding the heap lock via with-locked-heap.

3 Heap Properties

— Function: heap-count heap

Returns the number of objects in the heap.

— Function: heap-empty-p heap

Returns true if the heap is empty, that is iff heap-count is zero.

— Function: heap-name heap

Returns the name of the heap. Heap name affects only printed representation of the heap. Can be changed using setf unlike other heap properties.

— Function: heap-key heap

Returns the heap key, a function one argument used to extract values for use by the heap predicate. Heap key may also be nil, meaning heap elements are used directly by the heap predicate.

— Function: heap-predicate heap

Returns the heap predicate, a function of two arguments, returning true if the first argument should be closer to te top of the heap than the second.

— Function: heap-size heap

Returns the reserved size of the heap. Note, this is not the same as the number of elements in the heap: see heap-count for comparison.

— Constant: heap-size-limit

Exclusive upper limit for heap size, based on array-dimension-limit. When an insertion is attempted and the heap cannot grow any further, an error is signaled.