annotate lisp/pcl-cvs/dll-debug.el @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents 376386a54a3c
children
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 ;;; dll-debug -- A slow implementation of dll for debugging.
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
2 ;;; $Id: dll-debug.el,v 1.1.1.1 1996/12/18 22:42:58 steve Exp $
0
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: before 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 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;; This is a plug-in replacement for dll.el. It is dreadfully
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;; slow, but it facilitates debugging. Don't trust the comments in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;; this file too much.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 (provide 'dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
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 the list in the cdr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; cell. The doubly linked list is implemented as a normal list. You
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; should use dll.el and not this package in debugged code. This
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; package is not written for speed...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;; ================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; Internal functions for use in the doubly linked list package
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (defun dll-get-dummy-node (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; Return the dummy node. INTERNAL USE ONLY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (defun dll-list-nodes (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;; Return a list of all nodes in DLL. INTERNAL USE ONLY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (cdr dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (defun dll-set-from-node-list (dll list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; Set the contents of DLL to the nodes in LIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;; INTERNAL USE ONLY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (setcdr dll list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (defun dll-get-node-before (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;; Return the node in DLL that points to NODE. Use
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 ;; (dll-get-node-before some-list nil) to get the last node.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;; INTERNAL USE ONLY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (while (and dll (not (eq (cdr dll) node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (setq dll (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (if (not dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (error "Node not on list"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (defmacro dll-insert-after (node element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (let ((node-v (make-symbol "node"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (element-v (make-symbol "element")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (` (let (((, node-v) (, node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ((, element-v) (, element)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (setcdr (, node-v) (cons (, element-v) (cdr (, node-v))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;;; ===================================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;;; The public functions which operate on doubly linked lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (defmacro dll-element (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 "Get the element of a NODE in a doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 Args: 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 (` (car (, node))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (defun dll-create ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 "Create an empty doubly linked list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (cons 'DL-LIST nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (defun dll-p (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 "Return t if OBJECT is a doubly linked list, otherwise return nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (eq (car-safe object) 'DL-LIST))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
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-enter-first (dll element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 "Add an element first on a doubly linked list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 Args: DLL ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (setcdr dll (cons element (cdr dll))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (defun dll-enter-last (dll element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 "Add an element last on a doubly linked list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 Args: DLL ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (dll-insert-after (dll-get-node-before dll nil) element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (defun dll-enter-after (dll node element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 "In the doubly linked list DLL, insert a node containing ELEMENT after NODE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 Args: DLL NODE ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (dll-get-node-before dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (dll-insert-after node element))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (defun dll-enter-before (dll node element)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 "In the doubly linked list DLL, insert a node containing ELEMENT before NODE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 Args: DLL NODE ELEMENT."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (dll-insert-after (dll-get-node-before 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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (defun dll-next (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 "Return the node after NODE, or nil if NODE is the last node.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 Args: DLL 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 (dll-get-node-before dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (cdr node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (defun dll-previous (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 "Return the node before NODE, or nil if NODE is the first node.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 Args: DLL NODE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (let ((prev (dll-get-node-before dll node)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (if (eq dll prev)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 prev)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (defun dll-delete (dll node)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 "Delete NODE from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 Args: DLL NODE. Return the element of node."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (setcdr (dll-get-node-before dll node) (cdr node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (car node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (defun dll-delete-first (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 "Delete the first NODE from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 Return the element. Args: DLL. Returns nil if the DLL was empty."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (car (cdr dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (setcdr dll (cdr (cdr dll)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (defun dll-delete-last (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 "Delete the last NODE from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 Return the element. Args: DLL. Returns nil if the DLL was empty."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (let* ((last (dll-get-node-before dll nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (semilast (dll-get-node-before dll last)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (if (eq last dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (setcdr semilast nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (car last))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (defun dll-first (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 "Return the first element on the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 Return nil if the list is empty. The element is not removed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (car (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187
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-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 "Return the last element on the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 Return nil if the list is empty. The element is not removed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (let ((last (dll-get-node-before dll nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (if (eq last dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (car last))))
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (defun dll-nth (dll n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 "Return the Nth node from the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 Args: DLL N
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 N counts from zero. If DLL is not that long, nil is returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 If N is negative, return the -(N+1)th last element.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 Thus, (dll-nth dll 0) returns the first node,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 and (dll-nth dll -1) returns the last node."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 ;; Branch 0 ("follow left pointer") is used when n is negative.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 ;; Branch 1 ("follow right pointer") is used otherwise.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (if (>= n 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (nthcdr n (cdr dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (progn (setcdr dll (nreverse (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (nthcdr (- n) dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (setcdr dll (nreverse (cdr dll))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (defun dll-empty (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 "Return t if the doubly linked list DLL is empty, nil otherwise"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (not (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (defun dll-length (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 "Returns the number of elements in the doubly linked list DLL."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (length (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (defun dll-copy (dll &optional element-copy-fnc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 "Return a copy of the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 If optional second argument ELEMENT-COPY-FNC is non-nil it should be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 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
240 If ELEMENT-COPY-FNC is not given the elements are not copied."
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 element-copy-fnc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (cons 'DL-LIST (mapcar element-copy-fnc (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (copy-sequence dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (defun dll-all (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 "Return all elements on the double linked list DLL as an ordinary list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 (cdr dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (defun dll-clear (dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 "Clear the doubly linked list DLL, i.e. make it completely empty."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (setcdr dll nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (defun dll-map (map-function dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 "Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 The function is applied to the first element first."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (mapcar map-function (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (defun dll-map-reverse (map-function dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 "Apply MAP-FUNCTION to all elements in the doubly linked list DLL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 The function is applied to the last element first."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (setcdr dll (nreverse (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (mapcar map-function (cdr dll))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (setcdr dll (nreverse (cdr dll)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (defun dll-create-from-list (list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 "Given an elisp LIST create a doubly linked list with the same elements."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (cons 'DL-LIST list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (defun dll-sort (dll predicate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 "Sort the doubly linked list DLL, stably, comparing elements using PREDICATE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 Returns the sorted list. DLL is modified by side effects.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 PREDICATE is called with two elements of DLL, and should return T
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 if the first element is \"less\" than the second."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (setcdr dll (sort (cdr dll) predicate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (defun dll-filter (dll predicate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 "Remove all elements in the doubly linked list DLL for which PREDICATE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 return nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (let* ((prev dll)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (node (cdr dll)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (while node
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 ((funcall predicate (car node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (setq prev node))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (setcdr prev (cdr node))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (setq node (cdr node)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 ;; dll-debug.el ends here