/
queue.lisp
72 lines (57 loc) · 2.04 KB
/
queue.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;;; Simple FIFO for Common Lisp
;;;;
;;;; Copyright (c) Jeffrey Massung
;;;;
;;;; This file is provided to you under the Apache License,
;;;; Version 2.0 (the "License"); you may not use this file
;;;; except in compliance with the License. You may obtain
;;;; a copy of the License at
;;;;
;;;; http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing,
;;;; software distributed under the License is distributed on an
;;;; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
;;;; KIND, either express or implied. See the License for the
;;;; specific language governing permissions and limitations
;;;; under the License.
;;;;
(defpackage :queue
(:use :cl)
(:export
#:make-queue
;; push/pop elements on/off the queue
#:queue-push
#:queue-pop
#:queue-list))
(in-package :queue)
;;; ----------------------------------------------------
(defstruct (queue (:constructor %make-queue (xs &aux (ys (last xs)))))
(head xs :read-only t)
(tail ys :read-only t))
;;; ----------------------------------------------------
(defun make-queue (&key initial-contents)
"Create a new queue."
(%make-queue (cons nil initial-contents)))
;;; ----------------------------------------------------
(defmethod print-object ((q queue) stream)
"Output a deque to a stream."
(print-unreadable-object (q stream :type t)
(format stream "~:[EMPTY~;~:*~a~]" (rest (queue-head q)))))
;;; ----------------------------------------------------
(defun queue-push (x q)
"Push a value onto the tail of the queue."
(with-slots (tail)
q
(car (setf tail (cdr (rplacd tail (list x)))))))
;;; ----------------------------------------------------
(defun queue-pop (q)
"Pop a value off a queue, return the value and success flag."
(with-slots (head)
q
(when (rest head)
(values (car (setf head (cdr head))) t))))
;;; ----------------------------------------------------
(defun queue-list (q)
"Return the list of elements in the queue; does not alter the queue."
(rest (queue-head q)))