1
;;; $Id: dll.el,v 1.8 1995/12/11 00:11:13 ceder Exp $
2
;;; elib-dll.el -- Some primitives for Doubly linked lists.
4
;; Copyright (C) 1991-1995 Free Software Foundation
6
;; Author: Per Cederqvist <ceder@lysator.liu.se>
7
;; Maintainer: elib-maintainers@lysator.liu.se
8
;; Created: 24 Sep 1991
9
;; Keywords: extensions, lisp
11
;;; This program is free software; you can redistribute it and/or modify
12
;;; it under the terms of the GNU General Public License as published by
13
;;; the Free Software Foundation; either version 2 of the License, or
14
;;; (at your option) any later version.
16
;;; This program is distributed in the hope that it will be useful,
17
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
;;; GNU General Public License for more details.
21
;;; You should have received a copy of the GNU General Public License
22
;;; along with GNU Elib; see the file COPYING. If not, write to
23
;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24
;;; Boston, MA 02111-1307, USA
26
;;; Author: Per Cederqvist
27
;;; ceder@lysator.liu.se.
34
;;; A doubly linked list consists of one cons cell which holds the tag
35
;;; 'DL-LIST in the car cell and a pointer to a dummy node in the cdr
36
;;; cell. The doubly linked list is implemented as a circular list
37
;;; with the dummy node first and last. The dummy node is recognized
38
;;; by comparing it to the node which the cdr of the cons cell points
44
;;; ================================================================
45
;;; Internal functions for use in the doubly linked list package
47
(defun dll-get-dummy-node (dll)
49
;; Return the dummy node. INTERNAL USE ONLY.
52
(defun dll-list-nodes (dll)
54
;; Return a list of all nodes in DLL. INTERNAL USE ONLY.
57
(dummy (dll-get-dummy-node dll))
58
(node (elib-node-left dummy)))
60
(while (not (eq node dummy))
61
(setq result (cons node result))
62
(setq node (elib-node-left node)))
66
(defun dll-set-from-node-list (dll list)
68
;; Set the contents of DLL to the nodes in LIST.
72
(let* ((dummy (dll-get-dummy-node dll))
75
(elib-node-set-left (car list) left)
76
(elib-node-set-right left (car list))
77
(setq left (car list))
78
(setq list (cdr list)))
80
(elib-node-set-right left dummy)
81
(elib-node-set-left dummy left)))
84
;;; ===================================================================
85
;;; The public functions which operate on doubly linked lists.
87
(defmacro dll-element (dll node)
89
"Get the element of a NODE in a doubly linked list DLL.
92
(` (elib-node-data (, node))))
96
"Create an empty doubly linked list."
97
(let ((dummy-node (elib-node-create nil nil nil)))
98
(elib-node-set-right dummy-node dummy-node)
99
(elib-node-set-left dummy-node dummy-node)
100
(cons 'DL-LIST dummy-node)))
102
(defun dll-p (object)
103
"Return t if OBJECT is a doubly linked list, otherwise return nil."
104
(eq (car-safe object) 'DL-LIST))
106
(defun dll-enter-first (dll element)
107
"Add an element first on a doubly linked list.
111
(dll-get-dummy-node dll)
115
(defun dll-enter-last (dll element)
116
"Add an element last on a doubly linked list.
120
(dll-get-dummy-node dll)
124
(defun dll-enter-after (dll node element)
125
"In the doubly linked list DLL, insert a node containing ELEMENT after NODE.
126
Args: DLL NODE ELEMENT."
128
(let ((new-node (elib-node-create
129
node (elib-node-right node)
131
(elib-node-set-left (elib-node-right node) new-node)
132
(elib-node-set-right node new-node)))
135
(defun dll-enter-before (dll node element)
136
"In the doubly linked list DLL, insert a node containing ELEMENT before NODE.
137
Args: DLL NODE ELEMENT."
139
(let ((new-node (elib-node-create
140
(elib-node-left node) node
142
(elib-node-set-right (elib-node-left node) new-node)
143
(elib-node-set-left node new-node)))
147
(defun dll-next (dll node)
148
"Return the node after NODE, or nil if NODE is the last node.
151
(if (eq (elib-node-right node) (dll-get-dummy-node dll))
153
(elib-node-right node)))
156
(defun dll-previous (dll node)
157
"Return the node before NODE, or nil if NODE is the first node.
160
(if (eq (elib-node-left node) (dll-get-dummy-node dll))
162
(elib-node-left node)))
165
(defun dll-delete (dll node)
167
"Delete NODE from the doubly linked list DLL.
168
Args: DLL NODE. Return the element of node."
170
;; This is a no-op when applied to the dummy node. This will return
171
;; nil if applied to the dummy node since it always contains nil.
173
(elib-node-set-right (elib-node-left node) (elib-node-right node))
174
(elib-node-set-left (elib-node-right node) (elib-node-left node))
175
(dll-element dll node))
179
(defun dll-delete-first (dll)
181
"Delete the first NODE from the doubly linked list DLL.
182
Return the element. Args: DLL. Returns nil if the DLL was empty."
184
;; Relies on the fact that dll-delete does nothing and
185
;; returns nil if given the dummy node.
187
(dll-delete dll (elib-node-right (dll-get-dummy-node dll))))
190
(defun dll-delete-last (dll)
192
"Delete the last NODE from the doubly linked list DLL.
193
Return the element. Args: DLL. Returns nil if the DLL was empty."
195
;; Relies on the fact that dll-delete does nothing and
196
;; returns nil if given the dummy node.
198
(dll-delete dll (elib-node-left (dll-get-dummy-node dll))))
201
(defun dll-first (dll)
203
"Return the first element on the doubly linked list DLL.
204
Return nil if the list is empty. The element is not removed."
206
(if (eq (elib-node-right (dll-get-dummy-node dll))
207
(dll-get-dummy-node dll))
209
(elib-node-data (elib-node-right (dll-get-dummy-node dll)))))
214
(defun dll-last (dll)
216
"Return the last element on the doubly linked list DLL.
217
Return nil if the list is empty. The element is not removed."
219
(if (eq (elib-node-left (dll-get-dummy-node dll))
220
(dll-get-dummy-node dll))
222
(elib-node-data (elib-node-left (dll-get-dummy-node dll)))))
226
(defun dll-nth (dll n)
228
"Return the Nth node from the doubly linked list DLL.
230
N counts from zero. If DLL is not that long, nil is returned.
231
If N is negative, return the -(N+1)th last element.
232
Thus, (dll-nth dll 0) returns the first node,
233
and (dll-nth dll -1) returns the last node."
235
;; Branch 0 ("follow left pointer") is used when n is negative.
236
;; Branch 1 ("follow right pointer") is used otherwise.
238
(let* ((dummy (dll-get-dummy-node dll))
239
(branch (if (< n 0) 0 1))
240
(node (elib-node-branch dummy branch)))
245
(while (and (not (eq dummy node))
247
(setq node (elib-node-branch node branch))
255
(defun dll-empty (dll)
257
"Return t if the doubly linked list DLL is empty, nil otherwise"
259
(eq (elib-node-left (dll-get-dummy-node dll))
260
(dll-get-dummy-node dll)))
262
(defun dll-length (dll)
264
"Returns the number of elements in the doubly linked list DLL."
266
(let* ((dummy (dll-get-dummy-node dll))
267
(node (elib-node-right dummy))
270
(while (not (eq node dummy))
271
(setq node (elib-node-right node))
278
(defun dll-copy (dll &optional element-copy-fnc)
280
"Return a copy of the doubly linked list DLL.
281
If optional second argument ELEMENT-COPY-FNC is non-nil it should be
282
a function that takes one argument, an element, and returns a copy of it.
283
If ELEMENT-COPY-FNC is not given the elements are not copied."
285
(let ((result (dll-create))
286
(node (dll-nth dll 0)))
289
;; Copy the elements with the user-supplied function.
291
(dll-enter-last result
292
(funcall element-copy-fnc
293
(dll-element dll node)))
294
(setq node (dll-next dll node)))
296
;; Don't try to copy the elements - they might be
297
;; circular lists, or anything at all...
299
(dll-enter-last result (dll-element dll node))
300
(setq node (dll-next dll node))))
308
"Return all elements on the double linked list DLL as an ordinary list."
311
(dummy (dll-get-dummy-node dll))
312
(node (elib-node-left dummy)))
314
(while (not (eq node dummy))
315
(setq result (cons (dll-element dll node) result))
316
(setq node (elib-node-left node)))
321
(defun dll-clear (dll)
323
"Clear the doubly linked list DLL, i.e. make it completely empty."
325
(elib-node-set-left (dll-get-dummy-node dll) (dll-get-dummy-node dll))
326
(elib-node-set-right (dll-get-dummy-node dll) (dll-get-dummy-node dll)))
329
(defun dll-map (map-function dll)
331
"Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
332
The function is applied to the first element first."
334
(let* ((dummy (dll-get-dummy-node dll))
335
(node (elib-node-right dummy)))
337
(while (not (eq node dummy))
338
(funcall map-function (dll-element dll node))
339
(setq node (elib-node-right node)))))
342
(defun dll-map-reverse (map-function dll)
344
"Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
345
The function is applied to the last element first."
347
(let* ((dummy (dll-get-dummy-node dll))
348
(node (elib-node-left dummy)))
350
(while (not (eq node dummy))
351
(funcall map-function (dll-element dll node))
352
(setq node (elib-node-left node)))))
355
(defun dll-create-from-list (list)
357
"Given an elisp LIST create a doubly linked list with the same elements."
359
(let ((dll (dll-create)))
361
(dll-enter-last dll (car list))
362
(setq list (cdr list)))
367
(defun dll-sort (dll predicate)
369
"Sort the doubly linked list DLL, stably, comparing elements using PREDICATE.
370
Returns the sorted list. DLL is modified by side effects.
371
PREDICATE is called with two elements of DLL, and should return T
372
if the first element is \"less\" than the second."
374
(dll-set-from-node-list
375
dll (sort (dll-list-nodes dll)
376
(function (lambda (x1 x2)
379
(dll-element dll x2))))))
383
(defun dll-filter (dll predicate)
385
"Remove all elements in the doubly linked list DLL for which PREDICATE
388
(let* ((dummy (dll-get-dummy-node dll))
389
(node (elib-node-right dummy))
392
(while (not (eq node dummy))
393
(setq next (elib-node-right node))
394
(if (funcall predicate (dll-element dll node))
396
(dll-delete dll node))