diff options
author | Colin Okay <okay@toyful.space> | 2022-02-27 10:58:36 -0600 |
---|---|---|
committer | Colin Okay <okay@toyful.space> | 2022-02-27 10:58:36 -0600 |
commit | d4a82f6c7e4d4bd1978f4071aab494271db73e54 (patch) | |
tree | d6a022ad00666e03b4705bfc55542441657f92f7 /src/util.lisp | |
parent | 91fae8571748ff55d0cfcc4c51be673fe56f7209 (diff) |
deletion endpoint, newest endpoint, queue implementation
Diffstat (limited to 'src/util.lisp')
-rw-r--r-- | src/util.lisp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/util.lisp b/src/util.lisp index 2fc079b..ad312eb 100644 --- a/src/util.lisp +++ b/src/util.lisp @@ -18,3 +18,43 @@ :key #'symbol-name))) ,@body)))) + +(defclass queue-buffer () + ((front :initform (list)) + (back :initform (list)) + (size :initform 0) + (capacity :initarg capacity))) + +(defun make-qb (capacity) + (make-instance 'queue-buffer :capacity capacity)) + +(defun qb-empty-p (q) + (zerop (slot-value q 'size))) + +(defun qb-full-p (q) + (= (slot-value q 'size) (slot-value q 'capacity))) + +(defun enqueue-qb (q x) + (when (qb-full-p q) (dequeue-qb q)) + (with-slots (size back) q + (push x back) + (incf size))) + +(defun dequeue-qb (q &optional default) + (with-slots (front back size) q + (cond + ((plusp size) + (when (null front) + (setf front (nreverse back) + back nil)) + (decf size) + (pop front)) + + (t default)))) + +(defun qb-look (q) + "get a list of the queue, but don't remove items from it" + (with-slots (front back) q + (nconc (copy-seq front) + (reverse back)))) + |