dep: Major overhaul.
[lisp] / queue.lisp
CommitLineData
2626af66
MW
1;;; -*-lisp-*-
2;;;
3;;; A simple queue
4;;;
5;;; (c) 2008 Mark Wooding
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
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.
14;;;
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.
19;;;
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.
23
24(defpackage #:queue
25 (:use #:common-lisp)
dd33a773 26 (:export #:make-queue #:queue-emptyp #:enqueue #:pushqueue #:dequeue))
2626af66
MW
27(in-package #:queue)
28
29(defun make-queue ()
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
34 ;; uniformity.
35 (let ((q (cons nil nil)))
36 (setf (car q) q)))
37
38(defun queue-emptyp (q)
39 "Answer whether the queue Q is empty."
40 (null (cdr q)))
41
42(defun enqueue (x q)
43 "Enqueue the object X into the queue Q."
44 (let ((c (cons x nil)))
45 (setf (cdr (car q)) c
46 (car q) c)))
47
dd33a773
MW
48(defun pushqueue (x q)
49 "Push the object X onto the front of the queue Q."
50 (let* ((first (cdr q))
51 (new (cons x first)))
52 (setf (cdr q) new)
53 (unless first (setf (car q) new))))
54
2626af66
MW
55(defun dequeue (q)
56 "Remove and return the object at the head of the queue Q."
57 (if (queue-emptyp q)
58 (error "Queue is empty.")
59 (let ((c (cdr q)))
60 (prog1 (car c)
61 (unless (setf (cdr q) (cdr c))
62 (setf (car q) q))))))
63
64#+ test
65(defun queue-check (q)
66 "Check consistency of the queue Q."
67 (assert (car q))
68 (if(null (cdr q))
69 (assert (eq (car q) q))
70 (do ((tail (car q))
71 (collection nil (cons (car item) collection))
72 (item (cdr q) (cdr item)))
73 ((endp item) (nreverse collection))
74 (if (cdr item)
75 (assert (not (eq item tail)))
76 (assert (eq item tail))))))
77
78#+ test
79(defun test-queue ()
80 "Randomized test of the queue functions."
81 (let ((q (make-queue))
82 (want nil))
83 (dotimes (i 10000)
dd33a773 84 (case (random 3)
2626af66
MW
85 (0 (setf want (nconc want (list i)))
86 (enqueue i q))
dd33a773
MW
87 (1 (push i want)
88 (pushqueue i q))
89 (2 (if (null want)
2626af66
MW
90 (assert (queue-emptyp q))
91 (progn
92 (let ((j (dequeue q))
93 (k (pop want)))
94 (assert (= j k)))))))
95 (assert (equal want (queue-check q))))))
96
97;;;----- That's all, folks --------------------------------------------------