1 ;;;; $Id: queue-m.el,v 1.1 1999/02/13 20:29:15 daiki Exp $
2 ;;;; This file implements a simple FIFO queue using macros.
4 ;; Copyright (C) 1991-1995 Free Software Foundation
6 ;; Author: Inge Wallin <inge@lysator.liu.se>
7 ;; Maintainer: elib-maintainers@lysator.liu.se
8 ;; Created: before 12 May 1991
9 ;; Keywords: extensions, lisp
12 ;;;; This file is part of the GNU Emacs lisp library, Elib.
14 ;;;; GNU Elib is free software; you can redistribute it and/or modify
15 ;;;; it under the terms of the GNU General Public License as published by
16 ;;;; the Free Software Foundation; either version 2, or (at your option)
17 ;;;; any later version.
19 ;;;; GNU Elib is distributed in the hope that it will be useful,
20 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;;;; GNU General Public License for more details.
24 ;;;; You should have received a copy of the GNU General Public License
25 ;;;; along with GNU Elib; see the file COPYING. If not, write to
26 ;;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;;;; Boston, MA 02111-1307, USA
29 ;;;; Author: Inge Wallin
34 ;;; The queue is implemented as a two cons cell list, the first
35 ;;; containing the tag 'QUEUE. The car of the the second cons
36 ;;; cell points at the first element of the queue and the cdr points
37 ;;; at the last. All entries and removals are done using destructive
40 ;;; This file implements the short functions as macros for speed in
47 ;; Provide the function version and remove the macro version
49 (setq features (delq 'queue-f features))
52 ;;; ================================================================
55 (defmacro queue-create ()
56 "Create an empty fifo queue."
57 (` (cons 'QUEUE (cons nil nil))))
60 (defmacro queue-p (queue)
61 "Return t if QUEUE is a queue, otherwise return nil."
62 (` (eq (car-safe (, queue)) 'QUEUE)))
65 (defun queue-enqueue (queue element)
66 "Enter an element into a queue.
68 (let ((elementcell (cons element nil)))
69 (if (null (car (cdr queue)))
74 (setcdr (cdr (cdr queue))
80 (defun queue-dequeue (queue)
81 "Remove the first element of QUEUE and return it.
82 If QUEUE is empty, return nil and do nothing."
83 (if (not (null (car (cdr queue))))
85 (car (car (cdr queue)))
87 (cdr (car (cdr queue))))
88 (if (null (car (cdr queue)))
89 (setcdr (cdr queue) nil)))))
92 (defmacro queue-empty (queue)
93 "Return t if QUEUE is empty, otherwise return nil."
94 (` (null (car (cdr (, queue))))))
97 (defmacro queue-first (queue)
98 "Return the first element of QUEUE or nil if it is empty.
99 The element is not removed."
100 (` (car-safe (car (cdr (, queue))))))
103 (defmacro queue-nth (queue n)
104 "Return the nth element of a queue, but don't remove it.
106 If the length of the queue is less than N, return nil.
108 The oldest element (the first one) has number 0."
109 (` (nth (, n) (car (cdr (, queue))))))
112 (defmacro queue-last (queue)
113 "Return the last element of QUEUE or nil if it is empty."
114 (` (car-safe (cdr (cdr (, queue))))))
117 (defmacro queue-all (queue)
118 "Return a list of all elements of QUEUE or nil if it is empty.
119 The oldest element in the queue is the first in the list."
120 (` (car (cdr (, queue)))))
123 (defun queue-copy (queue)
124 "Return a copy of QUEUE. All entries in QUEUE are also copied."
125 (let* ((first (copy-sequence (car (cdr queue))))
128 (setq last (cdr last)))
129 (cons 'QUEUE (cons first last))))
132 (defmacro queue-length (queue)
133 "Return the number of elements in QUEUE."
134 (` (length (car (cdr (, queue))))))
137 (defmacro queue-clear (queue)
138 "Remove all elements from QUEUE."
139 (` (setcdr (, queue) (cons nil nil))))
141 ;;; queue-m.el ends here