-
Notifications
You must be signed in to change notification settings - Fork 5
/
queues.lisp
210 lines (164 loc) · 5.21 KB
/
queues.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;; queues.lisp
;; --------------------------------------------------------------------------------------
;; Fast FIFO Queues using circular lists
;;
;; Copyright (C) 2008 by SpectroDynamics, LLC. All rights reserved.
;;
;; DM/SD 08/08
;; --------------------------------------------------------------------------------------
;; -------------------------------------------------------------------
(in-package :queue)
;; -------------------------------------------------------------------
;; FIFO queues with in-place modification
;;
;; A queue is a reference to either nothing or some cell of a cyclic list.
;; By convention, that cell is to be viewed as the last cell in the queue.
;; The first cell in the queue is then found in constant time: it is the next
;; cell in the cyclic list. The queue's length is also recorded, so as to make
;; (length) a constant-time operation.
(defstruct queue
(length 0)
(tail nil))
(defun create ()
(make-queue))
(defun clear (q)
(setf (queue-length q) 0
(queue-tail q) nil))
;; -----------------------------------------------------
(defun contents (q)
;; destructively read the queue
;; returning its contents as a list
;; the queue structure is then reset to empty
(um:when-let (lst (shiftf (queue-tail q) nil))
(setf (queue-length q) 0)
(shiftf (cdr lst) nil)))
(defun set-contents! (q lst)
;; forcibly make lst the contents of the queue
(setf (queue-tail q) (last lst)
(queue-length q) (cl:length lst))
(when lst
(setf (cdr (queue-tail q)) lst))
lst)
(defun set-contents (q lst)
;; safely make lst the contents of the queue
(set-contents! q (and lst (copy-list lst)))
lst)
(defsetf contents (q) (lst)
`(set-contents ,q ,lst))
;; -----------------------------------------------------
(defun do-as-list (q fn)
(um:when-let (tl (queue-tail q))
(let ((hd (shiftf (cdr tl) nil)))
(unwind-protect
(funcall fn hd)
(setf (cdr tl) hd))
)))
(defmacro with-queue-as-list ((q lst-name) &body body)
`(do-as-list ,q (lambda (,lst-name) ,@body)))
#+:LISPWORKS
(editor:setup-indent "with-queue-as-list" 1)
;; -----------------------------------------------------
(defun add (x q)
(let ((cell (cons x nil)))
(um:if-let (tail (queue-tail q))
(setf (cdr cell) (cdr tail)
(cdr tail) cell)
;; else
(setf (cdr cell) cell))
(setf (queue-tail q) cell)
(incf (queue-length q))
x))
(defun push (x q)
(add x q))
(defun peek (q &optional empty-value)
(if (queue-tail q)
(values (cadr (queue-tail q)) t)
(values empty-value nil)))
(defun top (q &optional empty-value)
(peek q empty-value))
(defun tail (q)
(queue-tail q))
(defun take (q &optional (empty-error-p t) empty-value)
(um:if-let (tail (queue-tail q))
(let ((head (cdr tail)))
(if (eq head tail)
(setf (queue-tail q) nil)
(setf (cdr tail) (cdr head)))
(decf (queue-length q))
(values (car head) t))
;; else
(if empty-error-p
(error "Queue empty")
(values empty-value nil)) ))
(defun pop (q &optional (empty-error-p t) empty-value)
(take q empty-error-p empty-value))
(defun copy (q)
(let ((qcopy (copy-queue q)))
(with-queue-as-list (q qlst)
(setf (contents qcopy) qlst)
qcopy)))
(defun not-empty (q)
(queue-tail q))
(defun is-empty (q)
(null (queue-tail q)))
(defun length (q)
(queue-length q))
(defun iter (f q)
(with-queue-as-list (q qlst)
(dolist (qitem qlst)
(funcall f qitem))))
(defun map (f q)
(with-queue-as-list (q qlst)
(mapcar f qlst)))
(defun fold (f accu q)
(with-queue-as-list (q qlst)
(cl:reduce f qlst :initial-value accu)))
(defun transfer (q1 q2)
(set-contents! q2 (nconc (contents q1)
(contents q2))))
(defun delete (item q)
(set-contents! q (cl:delete item (contents q))))
(defun delete-if (pred q)
(set-contents! q (cl:delete-if pred (contents q))))
(defun find (item q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:find item lst args)))
(defun find-if (fn q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:find-if fn lst args)))
(defun every (fn q)
(with-queue-as-list (q lst)
(cl:every fn lst)))
(defun some (fn q)
(with-queue-as-list (q lst)
(cl:some fn lst)))
(defun list-of (q)
(with-queue-as-list (q lst)
(copy-list lst)))
(defun count (item q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:count item lst args)))
(defun count-if (pred q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:count-if pred lst args)))
(defun reduce (fn q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:reduce fn lst args)))
(defun nth (n q)
(with-queue-as-list (q lst)
(cl:nth n lst)))
(defun position (item q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:position item lst args)))
(defun position-if (pred q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:position-if pred lst args)))
(defun member (item q &rest args)
(with-queue-as-list (q lst)
(apply 'cl:member item lst args)))
(um:defmacro! do-queue ((item q &optional ans) &body body)
`(with-queue-as-list (,q ,g!lst)
(dolist (,item ,g!lst ,ans)
,@body)))
#+:LISPWORKS
(editor:setup-indent "do-queue" 1)