annotate lisp/pcl-cvs/dll.el @ 50:ee648375d8d6 r19-16b91

Import from CVS: tag r19-16b91
author cvs
date Mon, 13 Aug 2007 08:56:41 +0200
parents 376386a54a3c
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; $Id: dll.el,v 1.1.1.1 1996/12/18 03:32:27 steve Exp $
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;;; elib-dll.el -- Some primitives for Doubly linked lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; Copyright (C) 1991-1995 Free Software Foundation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Author: Per Cederqvist <ceder@lysator.liu.se>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Maintainer: elib-maintainers@lysator.liu.se
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; Created: 24 Sep 1991
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; Keywords: extensions, lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;;; This program is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;; the Free Software Foundation; either version 2 of the License, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;; (at your option) any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;;; This program is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;;; along with GNU Elib; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Boston, MA 02111-1307, USA
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;; Author: Per Cederqvist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; ceder@lysator.liu.se.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 (require 'elib-node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 (provide 'dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;; A doubly linked list consists of one cons cell which holds the tag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; 'DL-LIST in the car cell and a pointer to a dummy node in the cdr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; cell. The doubly linked list is implemented as a circular list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; with the dummy node first and last. The dummy node is recognized
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; by comparing it to the node which the cdr of the cons cell points
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; to.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; ================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;;; Internal functions for use in the doubly linked list package
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (defun dll-get-dummy-node (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;; Return the dummy node. INTERNAL USE ONLY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (cdr dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (defun dll-list-nodes (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;; Return a list of all nodes in DLL. INTERNAL USE ONLY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (let* ((result nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (node (elib-node-left dummy)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (while (not (eq node dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (setq result (cons node result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (setq node (elib-node-left node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (defun dll-set-from-node-list (dll list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;; Set the contents of DLL to the nodes in LIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;; INTERNAL USE ONLY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (dll-clear dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (let* ((dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (left dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (while list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (elib-node-set-left (car list) left)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (elib-node-set-right left (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (setq left (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (setq list (cdr list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (elib-node-set-right left dummy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (elib-node-set-left dummy left)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;;; ===================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ;;; The public functions which operate on doubly linked lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (defmacro dll-element (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 "Get the element of a NODE in a doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 Args: DLL NODE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (` (elib-node-data (, node))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (defun dll-create ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 "Create an empty doubly linked list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (let ((dummy-node (elib-node-create nil nil nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (elib-node-set-right dummy-node dummy-node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (elib-node-set-left dummy-node dummy-node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (cons 'DL-LIST dummy-node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (defun dll-p (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 "Return t if OBJECT is a doubly linked list, otherwise return nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (eq (car-safe object) 'DL-LIST))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (defun dll-enter-first (dll element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 "Add an element first on a doubly linked list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 Args: DLL ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (dll-enter-after
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 dll
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (dll-get-dummy-node dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (defun dll-enter-last (dll element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 "Add an element last on a doubly linked list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 Args: DLL ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (dll-enter-before
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 dll
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (dll-get-dummy-node dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (defun dll-enter-after (dll node element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 "In the doubly linked list DLL, insert a node containing ELEMENT after NODE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 Args: DLL NODE ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (let ((new-node (elib-node-create
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 node (elib-node-right node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 element)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (elib-node-set-left (elib-node-right node) new-node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (elib-node-set-right node new-node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (defun dll-enter-before (dll node element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 "In the doubly linked list DLL, insert a node containing ELEMENT before NODE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 Args: DLL NODE ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (let ((new-node (elib-node-create
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (elib-node-left node) node
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 element)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (elib-node-set-right (elib-node-left node) new-node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (elib-node-set-left node new-node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (defun dll-next (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 "Return the node after NODE, or nil if NODE is the last node.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 Args: DLL NODE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (if (eq (elib-node-right node) (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (elib-node-right node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (defun dll-previous (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 "Return the node before NODE, or nil if NODE is the first node.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 Args: DLL NODE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (if (eq (elib-node-left node) (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (elib-node-left node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (defun dll-delete (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 "Delete NODE from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 Args: DLL NODE. Return the element of node."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 ;; This is a no-op when applied to the dummy node. This will return
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ;; nil if applied to the dummy node since it always contains nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (elib-node-set-right (elib-node-left node) (elib-node-right node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (elib-node-set-left (elib-node-right node) (elib-node-left node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (dll-element dll node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (defun dll-delete-first (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 "Delete the first NODE from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 Return the element. Args: DLL. Returns nil if the DLL was empty."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ;; Relies on the fact that dll-delete does nothing and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 ;; returns nil if given the dummy node.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (dll-delete dll (elib-node-right (dll-get-dummy-node dll))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (defun dll-delete-last (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 "Delete the last NODE from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 Return the element. Args: DLL. Returns nil if the DLL was empty."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 ;; Relies on the fact that dll-delete does nothing and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 ;; returns nil if given the dummy node.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (dll-delete dll (elib-node-left (dll-get-dummy-node dll))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (defun dll-first (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 "Return the first element on the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 Return nil if the list is empty. The element is not removed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (if (eq (elib-node-right (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (elib-node-data (elib-node-right (dll-get-dummy-node dll)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (defun dll-last (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 "Return the last element on the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 Return nil if the list is empty. The element is not removed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (if (eq (elib-node-left (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (elib-node-data (elib-node-left (dll-get-dummy-node dll)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (defun dll-nth (dll n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 "Return the Nth node from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 Args: DLL N
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 N counts from zero. If DLL is not that long, nil is returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 If N is negative, return the -(N+1)th last element.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 Thus, (dll-nth dll 0) returns the first node,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 and (dll-nth dll -1) returns the last node."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 ;; Branch 0 ("follow left pointer") is used when n is negative.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 ;; Branch 1 ("follow right pointer") is used otherwise.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (let* ((dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (branch (if (< n 0) 0 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (node (elib-node-branch dummy branch)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (if (< n 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (setq n (- -1 n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (while (and (not (eq dummy node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (> n 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (setq node (elib-node-branch node branch))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (setq n (1- n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (if (eq dummy node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (defun dll-empty (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 "Return t if the doubly linked list DLL is empty, nil otherwise"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (eq (elib-node-left (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (dll-get-dummy-node dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (defun dll-length (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 "Returns the number of elements in the doubly linked list DLL."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (let* ((dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (node (elib-node-right dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (n 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (while (not (eq node dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (setq node (elib-node-right node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (setq n (1+ n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (defun dll-copy (dll &optional element-copy-fnc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 "Return a copy of the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 If optional second argument ELEMENT-COPY-FNC is non-nil it should be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 a function that takes one argument, an element, and returns a copy of it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 If ELEMENT-COPY-FNC is not given the elements are not copied."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (let ((result (dll-create))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (node (dll-nth dll 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (if element-copy-fnc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 ;; Copy the elements with the user-supplied function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (while node
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (dll-enter-last result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (funcall element-copy-fnc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (dll-element dll node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (setq node (dll-next dll node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 ;; Don't try to copy the elements - they might be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 ;; circular lists, or anything at all...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (while node
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (dll-enter-last result (dll-element dll node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (setq node (dll-next dll node))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (defun dll-all (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 "Return all elements on the double linked list DLL as an ordinary list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (let* ((result nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (node (elib-node-left dummy)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (while (not (eq node dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (setq result (cons (dll-element dll node) result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (setq node (elib-node-left node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (defun dll-clear (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 "Clear the doubly linked list DLL, i.e. make it completely empty."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (elib-node-set-left (dll-get-dummy-node dll) (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 (elib-node-set-right (dll-get-dummy-node dll) (dll-get-dummy-node dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (defun dll-map (map-function dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 "Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 The function is applied to the first element first."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (let* ((dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (node (elib-node-right dummy)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (while (not (eq node dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (funcall map-function (dll-element dll node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (setq node (elib-node-right node)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (defun dll-map-reverse (map-function dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 "Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 The function is applied to the last element first."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (let* ((dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (node (elib-node-left dummy)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (while (not (eq node dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 (funcall map-function (dll-element dll node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 (setq node (elib-node-left node)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (defun dll-create-from-list (list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 "Given an elisp LIST create a doubly linked list with the same elements."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 (let ((dll (dll-create)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (while list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (dll-enter-last dll (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 (setq list (cdr list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 (defun dll-sort (dll predicate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 "Sort the doubly linked list DLL, stably, comparing elements using PREDICATE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 Returns the sorted list. DLL is modified by side effects.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 PREDICATE is called with two elements of DLL, and should return T
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 if the first element is \"less\" than the second."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (dll-set-from-node-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 dll (sort (dll-list-nodes dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (function (lambda (x1 x2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (funcall predicate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (dll-element dll x1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (dll-element dll x2))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (defun dll-filter (dll predicate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 "Remove all elements in the doubly linked list DLL for which PREDICATE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 returns nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 (let* ((dummy (dll-get-dummy-node dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 (node (elib-node-right dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (while (not (eq node dummy))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 (setq next (elib-node-right node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 (if (funcall predicate (dll-element dll node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 (dll-delete dll node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 (setq node next))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 ;; dll.el ends here