Initial commit
[memstore.git] / memcache / compat.lisp
1 (in-package #:memcache)
2
3 ;;;
4 ;;; queue implementation from http://aima.cs.berkeley.edu/lisp/utilities/queue.lisp
5 ;;;
6
7 (defstruct q
8   (key #'identity)
9   (last nil)
10   (elements nil))
11
12 (defun make-empty-queue ()
13   (make-q))
14
15 (defun empty-queue? (q)
16   "Are there no elements in the queue?"
17   (= (length (q-elements q)) 0))
18
19 (defun queue-front (q)
20   "Return the element at the front of the queue."
21   (elt (q-elements q) 0))
22
23 (defun remove-front (q)
24   "Remove the element from the front of the queue and return it."
25   (if (listp (q-elements q))
26       (pop (q-elements q))
27     nil))
28
29
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))))))
40
41 ;; the wrappers
42
43 (defun make-queue ()
44   ""
45   #+allegro (make-instance 'mp:queue)
46   #-allegro (make-empty-queue))
47
48 (defmacro enqueue (queue what)
49   ""
50   #+allegro `(mp:enqueue ,queue ,what)
51   #-allegro `(enqueue-at-end ,queue ,what))
52
53 (defmacro dequeue (queue)
54   ""
55   #+allegro `(mp:dequeue ,queue)
56   #-allegro `(remove-front ,queue))
57
58 (defmacro queue-empty-p (queue)
59   ""
60   #+allegro `(mp:queue-empty-p ,queue)
61   #-allegro `(empty-queue? ,queue))