-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue-m.el
142 lines (106 loc) · 3.9 KB
/
queue-m.el
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
;;;; $Id: queue-m.el,v 1.5 2007/09/03 21:01:20 skk-cvs Exp $
;;;; This file implements a simple FIFO queue using macros.
;; Copyright (C) 1991-1995 Free Software Foundation
;; Author: Inge Wallin <inge@lysator.liu.se>
;; Maintainer: elib-maintainers@lysator.liu.se
;; Created: before 12 May 1991
;; Last Modified: $Date: 2007/09/03 21:01:20 $
;; Keywords: extensions, lisp
;;;;
;;;; This file is part of the GNU Emacs lisp library, Elib.
;;;;
;;;; GNU Elib is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
;;;;
;;;; GNU Elib is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with GNU Elib; see the file COPYING. If not, write to
;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;;;; Boston, MA 02110-1301, USA
;;;;
;;;; Author: Inge Wallin
;;;;
;;; Commentary:
;;; The queue is implemented as a two cons cell list, the first
;;; containing the tag 'QUEUE. The car of the the second cons
;;; cell points at the first element of the queue and the cdr points
;;; at the last. All entries and removals are done using destructive
;;; functions.
;;;
;;; This file implements the short functions as macros for speed in
;;; compiled code.
;;;
;;; Code:
;; Provide the function version and remove the macro version
(provide 'queue-m)
(setq features (delq 'queue-f features))
;;; ================================================================
(defmacro queue-create ()
"Create an empty fifo queue."
`(cons 'QUEUE (cons nil nil)))
(defmacro queue-p (queue)
"Return t if QUEUE is a queue, otherwise return nil."
`(eq (car-safe ,queue) 'QUEUE))
(defun queue-enqueue (queue element)
"Enter an element into a queue.
Args: QUEUE ELEMENT"
(let ((elementcell (cons element nil)))
(if (null (car (cdr queue)))
;; QUEUE is empty
(setcar (cdr queue)
(setcdr (cdr queue)
elementcell))
(setcdr (cdr (cdr queue))
elementcell)
(setcdr (cdr queue)
elementcell))))
(defun queue-dequeue (queue)
"Remove the first element of QUEUE and return it.
If QUEUE is empty, return nil and do nothing."
(if (car (cdr queue))
(prog1
(car (car (cdr queue)))
(setcar (cdr queue)
(cdr (car (cdr queue))))
(if (null (car (cdr queue)))
(setcdr (cdr queue) nil)))))
(defmacro queue-empty (queue)
"Return t if QUEUE is empty, otherwise return nil."
`(null (car (cdr ,queue))))
(defmacro queue-first (queue)
"Return the first element of QUEUE or nil if it is empty.
The element is not removed."
`(car-safe (car (cdr ,queue))))
(defmacro queue-nth (queue n)
"Return the nth element of a queue, but don't remove it.
Args: QUEUE N
If the length of the queue is less than N, return nil.
The oldest element (the first one) has number 0."
`(nth ,n (car (cdr ,queue))))
(defmacro queue-last (queue)
"Return the last element of QUEUE or nil if it is empty."
`(car-safe (cdr (cdr ,queue))))
(defmacro queue-all (queue)
"Return a list of all elements of QUEUE or nil if it is empty.
The oldest element in the queue is the first in the list."
`(car (cdr ,queue)))
(defun queue-copy (queue)
"Return a copy of QUEUE. All entries in QUEUE are also copied."
(let* ((first (copy-sequence (car (cdr queue))))
(last first))
(while (cdr last)
(setq last (cdr last)))
(cons 'QUEUE (cons first last))))
(defmacro queue-length (queue)
"Return the number of elements in QUEUE."
`(length (car (cdr ,queue))))
(defmacro queue-clear (queue)
"Remove all elements from QUEUE."
`(setcdr ,queue (cons nil nil)))
;;; queue-m.el ends here