5 ;;; (c) 2008 Mark Wooding
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2 of the License, or
13 ;;; (at your option) any later version.
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software Foundation,
22 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 (:export #:make-queue #:queue-emptyp #:enqueue #:pushqueue #:dequeue))
30 "Make a new queue object."
31 ;; A queue is just a cons cell. The cdr is the head of the list of items
32 ;; in the queue, and the car points to the last entry in the list. If the
33 ;; queue is empty, then the car points to the queue itself for the sake of
35 (let ((q (cons nil nil)))
38 (defun queue-emptyp (q)
39 "Answer whether the queue Q is empty."
43 "Enqueue the object X into the queue Q."
44 (let ((c (cons x nil)))
48 (defun pushqueue (x q)
49 "Push the object X onto the front of the queue Q."
50 (let* ((first (cdr q))
53 (unless first (setf (car q) new))))
56 "Remove and return the object at the head of the queue Q."
58 (error "Queue is empty.")
61 (unless (setf (cdr q) (cdr c))
65 (defun queue-check (q)
66 "Check consistency of the queue Q."
69 (assert (eq (car q) q))
71 (collection nil (cons (car item) collection))
72 (item (cdr q) (cdr item)))
73 ((endp item) (nreverse collection))
75 (assert (not (eq item tail)))
76 (assert (eq item tail))))))
80 "Randomized test of the queue functions."
81 (let ((q (make-queue))
85 (0 (setf want (nconc want (list i)))
90 (assert (queue-emptyp q))
95 (assert (equal want (queue-check q))))))
97 ;;;----- That's all, folks --------------------------------------------------