Common Lisp Package: PILEUP

Pileup provides a thread-safe binary heap implementation.

README:

FUNCTION

Public

HEAP-COUNT (HEAP)

Returns the number of objects in the heap.

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.

HEAP-EMPTY-P (HEAP)

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

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.

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.

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.

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.

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.

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.

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.

MAKE-HEAP (PREDICATE &KEY ((NAME %NAME) NIL) ((SIZE %SIZE) 12) ((KEY %KEY) NIL) &AUX (%VECTOR (MAKE-HEAP-VECTOR %SIZE)) (%PREDICATE PREDICATE) (FAST-PRED (LOCALLY (DECLARE (MUFFLE-CONDITIONS COMPILER-NOTE)) (IF %KEY (LAMBDA (X Y) (DECLARE (FUNCTION %KEY %PREDICATE) (OPTIMIZE (SPEED 3) (DEBUG 0) (SAFETY 0))) (LET ((XX (FUNCALL %KEY X)) (YY (FUNCALL %KEY Y))) (FUNCALL %PREDICATE XX YY))) %PREDICATE))))

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.

MAP-HEAP (FUNCTION HEAP &KEY (ORDERED T))

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.

Undocumented

SETFHEAP-NAME (NAME HEAP)

Private

HEAP-%COUNT (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-%KEY (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-%NAME (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-%PREDICATE (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-%SIZE (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-%VECTOR (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-FAST-PRED (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-LOCK (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

HEAP-STATE (INSTANCE)

@arg[extid]{A @class{extid}} @return[sytemid]{puri:uri or nil} Returns the System ID part of this External ID.

Undocumented

%HEAP-DELETE (INDEX HEAP)

CHECK-HEAP-CLEAN (HEAP WHAT &OPTIONAL ALLOW-TRAVERSE)

SETFHEAP-%COUNT (NEW-VALUE INSTANCE)

SETFHEAP-%NAME (NEW-VALUE INSTANCE)

SETFHEAP-%SIZE (NEW-VALUE INSTANCE)

SETFHEAP-%VECTOR (NEW-VALUE INSTANCE)

SETFHEAP-STATE (NEW-VALUE INSTANCE)

MAKE-HEAP-USING-FAST-PRED (%PREDICATE FAST-PRED &KEY ((NAME %NAME) NIL) ((SIZE %SIZE) 12) &AUX (%VECTOR (MAKE-HEAP-VECTOR %SIZE)))

MAKE-HEAP-VECTOR (SIZE)

TWO-ARG-<

TWO-ARG-<=

TWO-ARG->

TWO-ARG->=

MACRO

Public

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.

VARIABLE

Private

Undocumented

*TWO-ARG-PREDICATES*

CLASS

Public

HEAP

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.

CONSTANT

Public

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.

Private

Undocumented

+EMPTY+

MAX-HEAP-SIZE