* Makefile.am (EXTRA_DIST): Add liece.xbm and liece.xpm.
[elisp/liece.git] / lisp / queue-m.el
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.
3
4 ;; Copyright (C) 1991-1995 Free Software Foundation
5
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
10
11 ;;;;
12 ;;;; This file is part of the GNU Emacs lisp library, Elib.
13 ;;;;
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.
18 ;;;;
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.
23 ;;;;
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
28 ;;;;
29 ;;;; Author: Inge Wallin
30 ;;;; 
31
32 ;;; Commentary:
33
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
38 ;;; functions.
39 ;;;
40 ;;; This file implements the short functions as macros for speed in 
41 ;;; compiled code.
42 ;;;
43
44
45 ;;; Code:
46
47 ;; Provide the function version and remove the macro version
48 (provide 'queue-m)
49 (setq features (delq 'queue-f features))
50
51
52 ;;; ================================================================
53
54
55 (defmacro queue-create ()
56   "Create an empty fifo queue."
57   (` (cons 'QUEUE (cons nil nil))))
58
59
60 (defmacro queue-p (queue)
61   "Return t if QUEUE is a queue, otherwise return nil."
62   (` (eq (car-safe (, queue)) 'QUEUE)))
63
64
65 (defun queue-enqueue (queue element)
66   "Enter an element into a queue.
67 Args: QUEUE ELEMENT"
68   (let ((elementcell (cons element nil)))
69     (if (null (car (cdr queue)))
70         ;; QUEUE is empty
71         (setcar (cdr queue)
72                 (setcdr (cdr queue) 
73                         elementcell))
74       (setcdr (cdr (cdr queue))
75               elementcell)
76       (setcdr (cdr queue)
77               elementcell))))
78
79
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))))
84       (prog1
85           (car (car (cdr queue)))
86         (setcar (cdr queue)
87                 (cdr (car (cdr queue))))
88         (if (null (car (cdr queue)))
89             (setcdr (cdr queue) nil)))))
90
91
92 (defmacro queue-empty (queue)
93   "Return t if QUEUE is empty, otherwise return nil."
94   (` (null (car (cdr (, queue))))))
95
96
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))))))
101
102
103 (defmacro queue-nth (queue n)
104   "Return the nth element of a queue, but don't remove it.
105 Args: QUEUE N
106 If the length of the queue is less than N, return nil.
107
108 The oldest element (the first one) has number 0."
109   (` (nth (, n) (car (cdr (, queue))))))
110
111
112 (defmacro queue-last (queue)
113   "Return the last element of QUEUE or nil if it is empty."
114   (` (car-safe (cdr (cdr (, queue))))))
115
116
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)))))
121
122
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))))
126          (last first))
127     (while (cdr last)
128       (setq last (cdr last)))
129     (cons 'QUEUE (cons first last))))
130
131
132 (defmacro queue-length (queue)
133   "Return the number of elements in QUEUE."
134   (` (length (car (cdr (, queue))))))
135
136
137 (defmacro queue-clear (queue)
138   "Remove all elements from QUEUE."
139   (` (setcdr (, queue) (cons nil nil))))
140
141 ;;; queue-m.el ends here