-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.el
79 lines (61 loc) · 2.17 KB
/
queue.el
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
73
74
75
76
77
78
79
;;;; Advent of Code 2020
;;;; Queue implementation using lists
;;; Implementation:
;;;
;;; We have the head of the list, obviously, and a pointer to the tail
;;; - ie the last cons cell, as normally returned by last. These are
;;; kept in an outer cons cell with the car being the head and the cdr
;;; holding the tail. The exception is an empty queue, which will
;;; just be nil, so it will test as false.
;;;
(defun make-queue (&rest elts)
"Make a queue out of a variable number of arguments. If there is only one argument and it is a list, use that as arguments. List is reused."
(cond
((endp elts) nil)
((and (listp (car elts)) (endp (cdr elts)))
(cons (car elts) (last (car elts))))
(t (cons elts (last elts)))))
(defmacro queue-push (elem queue)
"Push an element onto the queue and return the queue. The queue is modified in place."
;; We actually need to push the element at the back
(let ((newlast (gensym)))
`(setf ,queue
(if ,queue
(let ((,newlast (cons ,elem nil)))
(rplacd (cdr ,queue) ,newlast)
(rplacd ,queue ,newlast)
,queue)
(make-queue ,elem)))))
(defun queue-first (queue)
"Return the element at the head of the queue"
(when (endp queue) (error "Trying to get first element of empty queue"))
(caar queue))
(defmacro queue-pop (queue)
"Pop an element from the queue. The updated queue is returned"
;; We pop from the front
`(setf ,queue
(cond
((null ,queue) (error "Pop from empty queue"))
;; Only one element in queue
((endp (cdar ,queue)) nil)
(t (rplaca ,queue (cdar ,queue)) ,queue))))
(defun queue-list (queue)
"Return a list of the elements in the queue"
(car queue))
;; (defun queue-test nil
;; (let ((q (make-queue 1 2 3)))
;; (and
;; (equal (queue-list q) '(1 2 3))
;; (queue-push 4 q)
;; (equal (queue-list q) '(1 2 3 4))
;; (queue-pop q)
;; (eql (queue-first q) 2)
;; (queue-pop q)
;; (equal (queue-list q) '(3 4))
;; (queue-pop q)
;; ;; Queue should now become empty
;; (progn (queue-pop q) (not q))
;; (queue-push 6 q)
;; (equal (queue-list q) '(6))
;; (queue-push 7 q)
;; (equal (queue-list q) '(6 7)))))