1 (in-package #:memcache)
4 ;;; queue implementation from http://aima.cs.berkeley.edu/lisp/utilities/queue.lisp
12 (defun make-empty-queue ()
15 (defun empty-queue? (q)
16 "Are there no elements in the queue?"
17 (= (length (q-elements q)) 0))
19 (defun queue-front (q)
20 "Return the element at the front of the queue."
21 (elt (q-elements q) 0))
23 (defun remove-front (q)
24 "Remove the element from the front of the queue and return it."
25 (if (listp (q-elements q))
30 (defun enqueue-at-end (q items)
31 "Add a list of items to the end of the queue."
32 ;; To make this more efficient, keep a pointer to the last cons in the queue
33 (let ((items (list items)))
34 (cond ((null items) nil)
35 ((or (null (q-last q)) (null (q-elements q)))
36 (setf (q-last q) (last items)
37 (q-elements q) (nconc (q-elements q) items)))
38 (t (setf (cdr (q-last q)) items
39 (q-last q) (last items))))))
45 #+allegro (make-instance 'mp:queue)
46 #-allegro (make-empty-queue))
48 (defmacro enqueue (queue what)
50 #+allegro `(mp:enqueue ,queue ,what)
51 #-allegro `(enqueue-at-end ,queue ,what))
53 (defmacro dequeue (queue)
55 #+allegro `(mp:dequeue ,queue)
56 #-allegro `(remove-front ,queue))
58 (defmacro queue-empty-p (queue)
60 #+allegro `(mp:queue-empty-p ,queue)
61 #-allegro `(empty-queue? ,queue))