Mercurial > hg > xemacs-beta
comparison lisp/cl/cl-seq.el @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | ac2d302a0011 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 ;;; cl-seq.el --- Common Lisp extensions for GNU Emacs Lisp (part three) | |
2 | |
3 ;; Copyright (C) 1993 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Dave Gillespie <daveg@synaptics.com> | |
6 ;; Version: 2.02 | |
7 ;; Keywords: extensions | |
8 | |
9 ;; This file is part of XEmacs. | |
10 | |
11 ;; XEmacs is free software; you can redistribute it and/or modify it | |
12 ;; under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; XEmacs is distributed in the hope that it will be useful, but | |
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 ;; General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
23 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | |
25 ;;; Synched up with: FSF 19.30. | |
26 | |
27 ;;; Commentary: | |
28 | |
29 ;; These are extensions to Emacs Lisp that provide a degree of | |
30 ;; Common Lisp compatibility, beyond what is already built-in | |
31 ;; in Emacs Lisp. | |
32 ;; | |
33 ;; This package was written by Dave Gillespie; it is a complete | |
34 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
35 ;; | |
36 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. | |
37 ;; | |
38 ;; Bug reports, comments, and suggestions are welcome! | |
39 | |
40 ;; This file contains the Common Lisp sequence and list functions | |
41 ;; which take keyword arguments. | |
42 | |
43 ;; See cl.el for Change Log. | |
44 | |
45 | |
46 ;;; Code: | |
47 | |
48 (or (memq 'cl-19 features) | |
49 (error "Tried to load `cl-seq' before `cl'!")) | |
50 | |
51 | |
52 ;;; We define these here so that this file can compile without having | |
53 ;;; loaded the cl.el file already. | |
54 | |
55 (defmacro cl-push (x place) (list 'setq place (list 'cons x place))) | |
56 (defmacro cl-pop (place) | |
57 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place))))) | |
58 | |
59 | |
60 ;;; Keyword parsing. This is special-cased here so that we can compile | |
61 ;;; this file independent from cl-macs. | |
62 | |
63 (defmacro cl-parsing-keywords (kwords other-keys &rest body) | |
64 (cons | |
65 'let* | |
66 (cons (mapcar | |
67 (function | |
68 (lambda (x) | |
69 (let* ((var (if (consp x) (car x) x)) | |
70 (mem (list 'car (list 'cdr (list 'memq (list 'quote var) | |
71 'cl-keys))))) | |
72 (if (eq var ':test-not) | |
73 (setq mem (list 'and mem (list 'setq 'cl-test mem) t))) | |
74 (if (eq var ':if-not) | |
75 (setq mem (list 'and mem (list 'setq 'cl-if mem) t))) | |
76 (list (intern | |
77 (format "cl-%s" (substring (symbol-name var) 1))) | |
78 (if (consp x) (list 'or mem (car (cdr x))) mem))))) | |
79 kwords) | |
80 (append | |
81 (and (not (eq other-keys t)) | |
82 (list | |
83 (list 'let '((cl-keys-temp cl-keys)) | |
84 (list 'while 'cl-keys-temp | |
85 (list 'or (list 'memq '(car cl-keys-temp) | |
86 (list 'quote | |
87 (mapcar | |
88 (function | |
89 (lambda (x) | |
90 (if (consp x) | |
91 (car x) x))) | |
92 (append kwords | |
93 other-keys)))) | |
94 '(car (cdr (memq (quote :allow-other-keys) | |
95 cl-keys))) | |
96 '(error "Bad keyword argument %s" | |
97 (car cl-keys-temp))) | |
98 '(setq cl-keys-temp (cdr (cdr cl-keys-temp))))))) | |
99 body)))) | |
100 (put 'cl-parsing-keywords 'lisp-indent-function 2) | |
101 (put 'cl-parsing-keywords 'edebug-form-spec '(sexp sexp &rest form)) | |
102 | |
103 (defmacro cl-check-key (x) | |
104 (list 'if 'cl-key (list 'funcall 'cl-key x) x)) | |
105 | |
106 (defmacro cl-check-test-nokey (item x) | |
107 (list 'cond | |
108 (list 'cl-test | |
109 (list 'eq (list 'not (list 'funcall 'cl-test item x)) | |
110 'cl-test-not)) | |
111 (list 'cl-if | |
112 (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not)) | |
113 (list 't (list 'if (list 'numberp item) | |
114 (list 'equal item x) (list 'eq item x))))) | |
115 | |
116 (defmacro cl-check-test (item x) | |
117 (list 'cl-check-test-nokey item (list 'cl-check-key x))) | |
118 | |
119 (defmacro cl-check-match (x y) | |
120 (setq x (list 'cl-check-key x) y (list 'cl-check-key y)) | |
121 (list 'if 'cl-test | |
122 (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not) | |
123 (list 'if (list 'numberp x) | |
124 (list 'equal x y) (list 'eq x y)))) | |
125 | |
126 (put 'cl-check-key 'edebug-form-spec 'edebug-forms) | |
127 (put 'cl-check-test 'edebug-form-spec 'edebug-forms) | |
128 (put 'cl-check-test-nokey 'edebug-form-spec 'edebug-forms) | |
129 (put 'cl-check-match 'edebug-form-spec 'edebug-forms) | |
130 | |
131 (defvar cl-test) (defvar cl-test-not) | |
132 (defvar cl-if) (defvar cl-if-not) | |
133 (defvar cl-key) | |
134 | |
135 | |
136 (defun reduce (cl-func cl-seq &rest cl-keys) | |
137 "Reduce two-argument FUNCTION across SEQUENCE. | |
138 Keywords supported: :start :end :from-end :initial-value :key" | |
139 (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) () | |
140 (or (listp cl-seq) (setq cl-seq (append cl-seq nil))) | |
141 (setq cl-seq (subseq cl-seq cl-start cl-end)) | |
142 (if cl-from-end (setq cl-seq (nreverse cl-seq))) | |
143 (let ((cl-accum (cond ((memq ':initial-value cl-keys) cl-initial-value) | |
144 (cl-seq (cl-check-key (cl-pop cl-seq))) | |
145 (t (funcall cl-func))))) | |
146 (if cl-from-end | |
147 (while cl-seq | |
148 (setq cl-accum (funcall cl-func (cl-check-key (cl-pop cl-seq)) | |
149 cl-accum))) | |
150 (while cl-seq | |
151 (setq cl-accum (funcall cl-func cl-accum | |
152 (cl-check-key (cl-pop cl-seq)))))) | |
153 cl-accum))) | |
154 | |
155 (defun fill (seq item &rest cl-keys) | |
156 "Fill the elements of SEQ with ITEM. | |
157 Keywords supported: :start :end" | |
158 (cl-parsing-keywords ((:start 0) :end) () | |
159 (if (listp seq) | |
160 (let ((p (nthcdr cl-start seq)) | |
161 (n (if cl-end (- cl-end cl-start) 8000000))) | |
162 (while (and p (>= (setq n (1- n)) 0)) | |
163 (setcar p item) | |
164 (setq p (cdr p)))) | |
165 (or cl-end (setq cl-end (length seq))) | |
166 (if (and (= cl-start 0) (= cl-end (length seq))) | |
167 (fillarray seq item) | |
168 (while (< cl-start cl-end) | |
169 (aset seq cl-start item) | |
170 (setq cl-start (1+ cl-start))))) | |
171 seq)) | |
172 | |
173 (defun replace (cl-seq1 cl-seq2 &rest cl-keys) | |
174 "Replace the elements of SEQ1 with the elements of SEQ2. | |
175 SEQ1 is destructively modified, then returned. | |
176 Keywords supported: :start1 :end1 :start2 :end2" | |
177 (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) () | |
178 (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1)) | |
179 (or (= cl-start1 cl-start2) | |
180 (let* ((cl-len (length cl-seq1)) | |
181 (cl-n (min (- (or cl-end1 cl-len) cl-start1) | |
182 (- (or cl-end2 cl-len) cl-start2)))) | |
183 (while (>= (setq cl-n (1- cl-n)) 0) | |
184 (cl-set-elt cl-seq1 (+ cl-start1 cl-n) | |
185 (elt cl-seq2 (+ cl-start2 cl-n)))))) | |
186 (if (listp cl-seq1) | |
187 (let ((cl-p1 (nthcdr cl-start1 cl-seq1)) | |
188 (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000))) | |
189 (if (listp cl-seq2) | |
190 (let ((cl-p2 (nthcdr cl-start2 cl-seq2)) | |
191 (cl-n (min cl-n1 | |
192 (if cl-end2 (- cl-end2 cl-start2) 4000000)))) | |
193 (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0)) | |
194 (setcar cl-p1 (car cl-p2)) | |
195 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))) | |
196 (setq cl-end2 (min (or cl-end2 (length cl-seq2)) | |
197 (+ cl-start2 cl-n1))) | |
198 (while (and cl-p1 (< cl-start2 cl-end2)) | |
199 (setcar cl-p1 (aref cl-seq2 cl-start2)) | |
200 (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2))))) | |
201 (setq cl-end1 (min (or cl-end1 (length cl-seq1)) | |
202 (+ cl-start1 (- (or cl-end2 (length cl-seq2)) | |
203 cl-start2)))) | |
204 (if (listp cl-seq2) | |
205 (let ((cl-p2 (nthcdr cl-start2 cl-seq2))) | |
206 (while (< cl-start1 cl-end1) | |
207 (aset cl-seq1 cl-start1 (car cl-p2)) | |
208 (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1)))) | |
209 (while (< cl-start1 cl-end1) | |
210 (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2)) | |
211 (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1)))))) | |
212 cl-seq1)) | |
213 | |
214 (defun remove* (cl-item cl-seq &rest cl-keys) | |
215 "Remove all occurrences of ITEM in SEQ. | |
216 This is a non-destructive function; it makes a copy of SEQ if necessary | |
217 to avoid corrupting the original SEQ. | |
218 Keywords supported: :test :test-not :key :count :start :end :from-end" | |
219 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end | |
220 (:start 0) :end) () | |
221 (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
222 cl-seq | |
223 (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000))) | |
224 (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end | |
225 cl-from-end))) | |
226 (if cl-i | |
227 (let ((cl-res (apply 'delete* cl-item (append cl-seq nil) | |
228 (append (if cl-from-end | |
229 (list ':end (1+ cl-i)) | |
230 (list ':start cl-i)) | |
231 cl-keys)))) | |
232 (if (listp cl-seq) cl-res | |
233 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))) | |
234 cl-seq)) | |
235 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
236 (if (= cl-start 0) | |
237 (while (and cl-seq (> cl-end 0) | |
238 (cl-check-test cl-item (car cl-seq)) | |
239 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) | |
240 (> (setq cl-count (1- cl-count)) 0)))) | |
241 (if (and (> cl-count 0) (> cl-end 0)) | |
242 (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq) | |
243 (setq cl-end (1- cl-end)) (cdr cl-seq)))) | |
244 (while (and cl-p (> cl-end 0) | |
245 (not (cl-check-test cl-item (car cl-p)))) | |
246 (setq cl-p (cdr cl-p) cl-end (1- cl-end))) | |
247 (if (and cl-p (> cl-end 0)) | |
248 (nconc (ldiff cl-seq cl-p) | |
249 (if (= cl-count 1) (cdr cl-p) | |
250 (and (cdr cl-p) | |
251 (apply 'delete* cl-item | |
252 (copy-sequence (cdr cl-p)) | |
253 ':start 0 ':end (1- cl-end) | |
254 ':count (1- cl-count) cl-keys)))) | |
255 cl-seq)) | |
256 cl-seq))))) | |
257 | |
258 (defun remove-if (cl-pred cl-list &rest cl-keys) | |
259 "Remove all items satisfying PREDICATE in SEQ. | |
260 This is a non-destructive function; it makes a copy of SEQ if necessary | |
261 to avoid corrupting the original SEQ. | |
262 Keywords supported: :key :count :start :end :from-end" | |
263 (apply 'remove* nil cl-list ':if cl-pred cl-keys)) | |
264 | |
265 (defun remove-if-not (cl-pred cl-list &rest cl-keys) | |
266 "Remove all items not satisfying PREDICATE in SEQ. | |
267 This is a non-destructive function; it makes a copy of SEQ if necessary | |
268 to avoid corrupting the original SEQ. | |
269 Keywords supported: :key :count :start :end :from-end" | |
270 (apply 'remove* nil cl-list ':if-not cl-pred cl-keys)) | |
271 | |
272 (defun delete* (cl-item cl-seq &rest cl-keys) | |
273 "Remove all occurrences of ITEM in SEQ. | |
274 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
275 Keywords supported: :test :test-not :key :count :start :end :from-end" | |
276 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end | |
277 (:start 0) :end) () | |
278 (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
279 cl-seq | |
280 (if (listp cl-seq) | |
281 (if (and cl-from-end (< cl-count 4000000)) | |
282 (let (cl-i) | |
283 (while (and (>= (setq cl-count (1- cl-count)) 0) | |
284 (setq cl-i (cl-position cl-item cl-seq cl-start | |
285 cl-end cl-from-end))) | |
286 (if (= cl-i 0) (setq cl-seq (cdr cl-seq)) | |
287 (let ((cl-tail (nthcdr (1- cl-i) cl-seq))) | |
288 (setcdr cl-tail (cdr (cdr cl-tail))))) | |
289 (setq cl-end cl-i)) | |
290 cl-seq) | |
291 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
292 (if (= cl-start 0) | |
293 (progn | |
294 (while (and cl-seq | |
295 (> cl-end 0) | |
296 (cl-check-test cl-item (car cl-seq)) | |
297 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) | |
298 (> (setq cl-count (1- cl-count)) 0))) | |
299 (setq cl-end (1- cl-end))) | |
300 (setq cl-start (1- cl-start))) | |
301 (if (and (> cl-count 0) (> cl-end 0)) | |
302 (let ((cl-p (nthcdr cl-start cl-seq))) | |
303 (while (and (cdr cl-p) (> cl-end 0)) | |
304 (if (cl-check-test cl-item (car (cdr cl-p))) | |
305 (progn | |
306 (setcdr cl-p (cdr (cdr cl-p))) | |
307 (if (= (setq cl-count (1- cl-count)) 0) | |
308 (setq cl-end 1))) | |
309 (setq cl-p (cdr cl-p))) | |
310 (setq cl-end (1- cl-end))))) | |
311 cl-seq) | |
312 (apply 'remove* cl-item cl-seq cl-keys))))) | |
313 | |
314 (defun delete-if (cl-pred cl-list &rest cl-keys) | |
315 "Remove all items satisfying PREDICATE in SEQ. | |
316 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
317 Keywords supported: :key :count :start :end :from-end" | |
318 (apply 'delete* nil cl-list ':if cl-pred cl-keys)) | |
319 | |
320 (defun delete-if-not (cl-pred cl-list &rest cl-keys) | |
321 "Remove all items not satisfying PREDICATE in SEQ. | |
322 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
323 Keywords supported: :key :count :start :end :from-end" | |
324 (apply 'delete* nil cl-list ':if-not cl-pred cl-keys)) | |
325 | |
326 (or (and (fboundp 'delete) (subrp (symbol-function 'delete))) | |
327 (defalias 'delete (function (lambda (x y) (delete* x y ':test 'equal))))) | |
328 (defun remove (x y) (remove* x y ':test 'equal)) | |
329 (defun remq (x y) (if (memq x y) (delq x (copy-list y)) y)) | |
330 | |
331 (defun remove-duplicates (cl-seq &rest cl-keys) | |
332 "Return a copy of SEQ with all duplicate elements removed. | |
333 Keywords supported: :test :test-not :key :start :end :from-end" | |
334 (cl-delete-duplicates cl-seq cl-keys t)) | |
335 | |
336 (defun delete-duplicates (cl-seq &rest cl-keys) | |
337 "Remove all duplicate elements from SEQ (destructively). | |
338 Keywords supported: :test :test-not :key :start :end :from-end" | |
339 (cl-delete-duplicates cl-seq cl-keys nil)) | |
340 | |
341 (defun cl-delete-duplicates (cl-seq cl-keys cl-copy) | |
342 (if (listp cl-seq) | |
343 (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if) | |
344 () | |
345 (if cl-from-end | |
346 (let ((cl-p (nthcdr cl-start cl-seq)) cl-i) | |
347 (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
348 (while (> cl-end 1) | |
349 (setq cl-i 0) | |
350 (while (setq cl-i (cl-position (cl-check-key (car cl-p)) | |
351 (cdr cl-p) cl-i (1- cl-end))) | |
352 (if cl-copy (setq cl-seq (copy-sequence cl-seq) | |
353 cl-p (nthcdr cl-start cl-seq) cl-copy nil)) | |
354 (let ((cl-tail (nthcdr cl-i cl-p))) | |
355 (setcdr cl-tail (cdr (cdr cl-tail)))) | |
356 (setq cl-end (1- cl-end))) | |
357 (setq cl-p (cdr cl-p) cl-end (1- cl-end) | |
358 cl-start (1+ cl-start))) | |
359 cl-seq) | |
360 (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
361 (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1) | |
362 (cl-position (cl-check-key (car cl-seq)) | |
363 (cdr cl-seq) 0 (1- cl-end))) | |
364 (setq cl-seq (cdr cl-seq) cl-end (1- cl-end))) | |
365 (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq) | |
366 (setq cl-end (1- cl-end) cl-start 1) cl-seq))) | |
367 (while (and (cdr (cdr cl-p)) (> cl-end 1)) | |
368 (if (cl-position (cl-check-key (car (cdr cl-p))) | |
369 (cdr (cdr cl-p)) 0 (1- cl-end)) | |
370 (progn | |
371 (if cl-copy (setq cl-seq (copy-sequence cl-seq) | |
372 cl-p (nthcdr (1- cl-start) cl-seq) | |
373 cl-copy nil)) | |
374 (setcdr cl-p (cdr (cdr cl-p)))) | |
375 (setq cl-p (cdr cl-p))) | |
376 (setq cl-end (1- cl-end) cl-start (1+ cl-start))) | |
377 cl-seq))) | |
378 (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil))) | |
379 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))) | |
380 | |
381 (defun substitute (cl-new cl-old cl-seq &rest cl-keys) | |
382 "Substitute NEW for OLD in SEQ. | |
383 This is a non-destructive function; it makes a copy of SEQ if necessary | |
384 to avoid corrupting the original SEQ. | |
385 Keywords supported: :test :test-not :key :count :start :end :from-end" | |
386 (cl-parsing-keywords (:test :test-not :key :if :if-not :count | |
387 (:start 0) :end :from-end) () | |
388 (if (or (eq cl-old cl-new) | |
389 (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0)) | |
390 cl-seq | |
391 (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end))) | |
392 (if (not cl-i) | |
393 cl-seq | |
394 (setq cl-seq (copy-sequence cl-seq)) | |
395 (or cl-from-end | |
396 (progn (cl-set-elt cl-seq cl-i cl-new) | |
397 (setq cl-i (1+ cl-i) cl-count (1- cl-count)))) | |
398 (apply 'nsubstitute cl-new cl-old cl-seq ':count cl-count | |
399 ':start cl-i cl-keys)))))) | |
400 | |
401 (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys) | |
402 "Substitute NEW for all items satisfying PREDICATE in SEQ. | |
403 This is a non-destructive function; it makes a copy of SEQ if necessary | |
404 to avoid corrupting the original SEQ. | |
405 Keywords supported: :key :count :start :end :from-end" | |
406 (apply 'substitute cl-new nil cl-list ':if cl-pred cl-keys)) | |
407 | |
408 (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys) | |
409 "Substitute NEW for all items not satisfying PREDICATE in SEQ. | |
410 This is a non-destructive function; it makes a copy of SEQ if necessary | |
411 to avoid corrupting the original SEQ. | |
412 Keywords supported: :key :count :start :end :from-end" | |
413 (apply 'substitute cl-new nil cl-list ':if-not cl-pred cl-keys)) | |
414 | |
415 (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys) | |
416 "Substitute NEW for OLD in SEQ. | |
417 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
418 Keywords supported: :test :test-not :key :count :start :end :from-end" | |
419 (cl-parsing-keywords (:test :test-not :key :if :if-not :count | |
420 (:start 0) :end :from-end) () | |
421 (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0) | |
422 (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000))) | |
423 (let ((cl-p (nthcdr cl-start cl-seq))) | |
424 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
425 (while (and cl-p (> cl-end 0) (> cl-count 0)) | |
426 (if (cl-check-test cl-old (car cl-p)) | |
427 (progn | |
428 (setcar cl-p cl-new) | |
429 (setq cl-count (1- cl-count)))) | |
430 (setq cl-p (cdr cl-p) cl-end (1- cl-end)))) | |
431 (or cl-end (setq cl-end (length cl-seq))) | |
432 (if cl-from-end | |
433 (while (and (< cl-start cl-end) (> cl-count 0)) | |
434 (setq cl-end (1- cl-end)) | |
435 (if (cl-check-test cl-old (elt cl-seq cl-end)) | |
436 (progn | |
437 (cl-set-elt cl-seq cl-end cl-new) | |
438 (setq cl-count (1- cl-count))))) | |
439 (while (and (< cl-start cl-end) (> cl-count 0)) | |
440 (if (cl-check-test cl-old (aref cl-seq cl-start)) | |
441 (progn | |
442 (aset cl-seq cl-start cl-new) | |
443 (setq cl-count (1- cl-count)))) | |
444 (setq cl-start (1+ cl-start)))))) | |
445 cl-seq)) | |
446 | |
447 (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys) | |
448 "Substitute NEW for all items satisfying PREDICATE in SEQ. | |
449 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
450 Keywords supported: :key :count :start :end :from-end" | |
451 (apply 'nsubstitute cl-new nil cl-list ':if cl-pred cl-keys)) | |
452 | |
453 (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys) | |
454 "Substitute NEW for all items not satisfying PREDICATE in SEQ. | |
455 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
456 Keywords supported: :key :count :start :end :from-end" | |
457 (apply 'nsubstitute cl-new nil cl-list ':if-not cl-pred cl-keys)) | |
458 | |
459 (defun find (cl-item cl-seq &rest cl-keys) | |
460 "Find the first occurrence of ITEM in LIST. | |
461 Return the matching ITEM, or nil if not found. | |
462 Keywords supported: :test :test-not :key :start :end :from-end" | |
463 (let ((cl-pos (apply 'position cl-item cl-seq cl-keys))) | |
464 (and cl-pos (elt cl-seq cl-pos)))) | |
465 | |
466 (defun find-if (cl-pred cl-list &rest cl-keys) | |
467 "Find the first item satisfying PREDICATE in LIST. | |
468 Return the matching ITEM, or nil if not found. | |
469 Keywords supported: :key :start :end :from-end" | |
470 (apply 'find nil cl-list ':if cl-pred cl-keys)) | |
471 | |
472 (defun find-if-not (cl-pred cl-list &rest cl-keys) | |
473 "Find the first item not satisfying PREDICATE in LIST. | |
474 Return the matching ITEM, or nil if not found. | |
475 Keywords supported: :key :start :end :from-end" | |
476 (apply 'find nil cl-list ':if-not cl-pred cl-keys)) | |
477 | |
478 (defun position (cl-item cl-seq &rest cl-keys) | |
479 "Find the first occurrence of ITEM in LIST. | |
480 Return the index of the matching item, or nil if not found. | |
481 Keywords supported: :test :test-not :key :start :end :from-end" | |
482 (cl-parsing-keywords (:test :test-not :key :if :if-not | |
483 (:start 0) :end :from-end) () | |
484 (cl-position cl-item cl-seq cl-start cl-end cl-from-end))) | |
485 | |
486 (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end) | |
487 (if (listp cl-seq) | |
488 (let ((cl-p (nthcdr cl-start cl-seq))) | |
489 (or cl-end (setq cl-end 8000000)) | |
490 (let ((cl-res nil)) | |
491 (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end)) | |
492 (if (cl-check-test cl-item (car cl-p)) | |
493 (setq cl-res cl-start)) | |
494 (setq cl-p (cdr cl-p) cl-start (1+ cl-start))) | |
495 cl-res)) | |
496 (or cl-end (setq cl-end (length cl-seq))) | |
497 (if cl-from-end | |
498 (progn | |
499 (while (and (>= (setq cl-end (1- cl-end)) cl-start) | |
500 (not (cl-check-test cl-item (aref cl-seq cl-end))))) | |
501 (and (>= cl-end cl-start) cl-end)) | |
502 (while (and (< cl-start cl-end) | |
503 (not (cl-check-test cl-item (aref cl-seq cl-start)))) | |
504 (setq cl-start (1+ cl-start))) | |
505 (and (< cl-start cl-end) cl-start)))) | |
506 | |
507 (defun position-if (cl-pred cl-list &rest cl-keys) | |
508 "Find the first item satisfying PREDICATE in LIST. | |
509 Return the index of the matching item, or nil if not found. | |
510 Keywords supported: :key :start :end :from-end" | |
511 (apply 'position nil cl-list ':if cl-pred cl-keys)) | |
512 | |
513 (defun position-if-not (cl-pred cl-list &rest cl-keys) | |
514 "Find the first item not satisfying PREDICATE in LIST. | |
515 Return the index of the matching item, or nil if not found. | |
516 Keywords supported: :key :start :end :from-end" | |
517 (apply 'position nil cl-list ':if-not cl-pred cl-keys)) | |
518 | |
519 (defun count (cl-item cl-seq &rest cl-keys) | |
520 "Count the number of occurrences of ITEM in LIST. | |
521 Keywords supported: :test :test-not :key :start :end" | |
522 (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) () | |
523 (let ((cl-count 0) cl-x) | |
524 (or cl-end (setq cl-end (length cl-seq))) | |
525 (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq))) | |
526 (while (< cl-start cl-end) | |
527 (setq cl-x (if (consp cl-seq) (cl-pop cl-seq) (aref cl-seq cl-start))) | |
528 (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count))) | |
529 (setq cl-start (1+ cl-start))) | |
530 cl-count))) | |
531 | |
532 (defun count-if (cl-pred cl-list &rest cl-keys) | |
533 "Count the number of items satisfying PREDICATE in LIST. | |
534 Keywords supported: :key :start :end" | |
535 (apply 'count nil cl-list ':if cl-pred cl-keys)) | |
536 | |
537 (defun count-if-not (cl-pred cl-list &rest cl-keys) | |
538 "Count the number of items not satisfying PREDICATE in LIST. | |
539 Keywords supported: :key :start :end" | |
540 (apply 'count nil cl-list ':if-not cl-pred cl-keys)) | |
541 | |
542 (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys) | |
543 "Compare SEQ1 with SEQ2, return index of first mismatching element. | |
544 Return nil if the sequences match. If one sequence is a prefix of the | |
545 other, the return value indicates the end of the shorted sequence. | |
546 Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end" | |
547 (cl-parsing-keywords (:test :test-not :key :from-end | |
548 (:start1 0) :end1 (:start2 0) :end2) () | |
549 (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
550 (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
551 (if cl-from-end | |
552 (progn | |
553 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
554 (cl-check-match (elt cl-seq1 (1- cl-end1)) | |
555 (elt cl-seq2 (1- cl-end2)))) | |
556 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2))) | |
557 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
558 (1- cl-end1))) | |
559 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1))) | |
560 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2)))) | |
561 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
562 (cl-check-match (if cl-p1 (car cl-p1) | |
563 (aref cl-seq1 cl-start1)) | |
564 (if cl-p2 (car cl-p2) | |
565 (aref cl-seq2 cl-start2)))) | |
566 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2) | |
567 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2))) | |
568 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
569 cl-start1))))) | |
570 | |
571 (defun search (cl-seq1 cl-seq2 &rest cl-keys) | |
572 "Search for SEQ1 as a subsequence of SEQ2. | |
573 Return the index of the leftmost element of the first match found; | |
574 return nil if there are no matches. | |
575 Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end" | |
576 (cl-parsing-keywords (:test :test-not :key :from-end | |
577 (:start1 0) :end1 (:start2 0) :end2) () | |
578 (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
579 (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
580 (if (>= cl-start1 cl-end1) | |
581 (if cl-from-end cl-end2 cl-start2) | |
582 (let* ((cl-len (- cl-end1 cl-start1)) | |
583 (cl-first (cl-check-key (elt cl-seq1 cl-start1))) | |
584 (cl-if nil) cl-pos) | |
585 (setq cl-end2 (- cl-end2 (1- cl-len))) | |
586 (while (and (< cl-start2 cl-end2) | |
587 (setq cl-pos (cl-position cl-first cl-seq2 | |
588 cl-start2 cl-end2 cl-from-end)) | |
589 (apply 'mismatch cl-seq1 cl-seq2 | |
590 ':start1 (1+ cl-start1) ':end1 cl-end1 | |
591 ':start2 (1+ cl-pos) ':end2 (+ cl-pos cl-len) | |
592 ':from-end nil cl-keys)) | |
593 (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos)))) | |
594 (and (< cl-start2 cl-end2) cl-pos))))) | |
595 | |
596 (defun sort* (cl-seq cl-pred &rest cl-keys) | |
597 "Sort the argument SEQUENCE according to PREDICATE. | |
598 This is a destructive function; it reuses the storage of SEQUENCE if possible. | |
599 Keywords supported: :key" | |
600 (if (nlistp cl-seq) | |
601 (replace cl-seq (apply 'sort* (append cl-seq nil) cl-pred cl-keys)) | |
602 (cl-parsing-keywords (:key) () | |
603 (if (memq cl-key '(nil identity)) | |
604 (sort cl-seq cl-pred) | |
605 (sort cl-seq (function (lambda (cl-x cl-y) | |
606 (funcall cl-pred (funcall cl-key cl-x) | |
607 (funcall cl-key cl-y))))))))) | |
608 | |
609 (defun stable-sort (cl-seq cl-pred &rest cl-keys) | |
610 "Sort the argument SEQUENCE stably according to PREDICATE. | |
611 This is a destructive function; it reuses the storage of SEQUENCE if possible. | |
612 Keywords supported: :key" | |
613 (apply 'sort* cl-seq cl-pred cl-keys)) | |
614 | |
615 (defun merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys) | |
616 "Destructively merge the two sequences to produce a new sequence. | |
617 TYPE is the sequence type to return, SEQ1 and SEQ2 are the two | |
618 argument sequences, and PRED is a `less-than' predicate on the elements. | |
619 Keywords supported: :key" | |
620 (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil))) | |
621 (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil))) | |
622 (cl-parsing-keywords (:key) () | |
623 (let ((cl-res nil)) | |
624 (while (and cl-seq1 cl-seq2) | |
625 (if (funcall cl-pred (cl-check-key (car cl-seq2)) | |
626 (cl-check-key (car cl-seq1))) | |
627 (cl-push (cl-pop cl-seq2) cl-res) | |
628 (cl-push (cl-pop cl-seq1) cl-res))) | |
629 (coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type)))) | |
630 | |
631 ;;; See compiler macro in cl-macs.el | |
632 (defun member* (cl-item cl-list &rest cl-keys) | |
633 "Find the first occurrence of ITEM in LIST. | |
634 Return the sublist of LIST whose car is ITEM. | |
635 Keywords supported: :test :test-not :key" | |
636 (if cl-keys | |
637 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
638 (while (and cl-list (not (cl-check-test cl-item (car cl-list)))) | |
639 (setq cl-list (cdr cl-list))) | |
640 cl-list) | |
641 (if (and (numberp cl-item) (not (integerp cl-item))) | |
642 (member cl-item cl-list) | |
643 (memq cl-item cl-list)))) | |
644 | |
645 (defun member-if (cl-pred cl-list &rest cl-keys) | |
646 "Find the first item satisfying PREDICATE in LIST. | |
647 Return the sublist of LIST whose car matches. | |
648 Keywords supported: :key" | |
649 (apply 'member* nil cl-list ':if cl-pred cl-keys)) | |
650 | |
651 (defun member-if-not (cl-pred cl-list &rest cl-keys) | |
652 "Find the first item not satisfying PREDICATE in LIST. | |
653 Return the sublist of LIST whose car matches. | |
654 Keywords supported: :key" | |
655 (apply 'member* nil cl-list ':if-not cl-pred cl-keys)) | |
656 | |
657 (defun cl-adjoin (cl-item cl-list &rest cl-keys) | |
658 (if (cl-parsing-keywords (:key) t | |
659 (apply 'member* (cl-check-key cl-item) cl-list cl-keys)) | |
660 cl-list | |
661 (cons cl-item cl-list))) | |
662 | |
663 ;;; See compiler macro in cl-macs.el | |
664 (defun assoc* (cl-item cl-alist &rest cl-keys) | |
665 "Find the first item whose car matches ITEM in LIST. | |
666 Keywords supported: :test :test-not :key" | |
667 (if cl-keys | |
668 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
669 (while (and cl-alist | |
670 (or (not (consp (car cl-alist))) | |
671 (not (cl-check-test cl-item (car (car cl-alist)))))) | |
672 (setq cl-alist (cdr cl-alist))) | |
673 (and cl-alist (car cl-alist))) | |
674 (if (and (numberp cl-item) (not (integerp cl-item))) | |
675 (assoc cl-item cl-alist) | |
676 (assq cl-item cl-alist)))) | |
677 | |
678 (defun assoc-if (cl-pred cl-list &rest cl-keys) | |
679 "Find the first item whose car satisfies PREDICATE in LIST. | |
680 Keywords supported: :key" | |
681 (apply 'assoc* nil cl-list ':if cl-pred cl-keys)) | |
682 | |
683 (defun assoc-if-not (cl-pred cl-list &rest cl-keys) | |
684 "Find the first item whose car does not satisfy PREDICATE in LIST. | |
685 Keywords supported: :key" | |
686 (apply 'assoc* nil cl-list ':if-not cl-pred cl-keys)) | |
687 | |
688 (defun rassoc* (cl-item cl-alist &rest cl-keys) | |
689 "Find the first item whose cdr matches ITEM in LIST. | |
690 Keywords supported: :test :test-not :key" | |
691 (if (or cl-keys (numberp cl-item)) | |
692 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
693 (while (and cl-alist | |
694 (or (not (consp (car cl-alist))) | |
695 (not (cl-check-test cl-item (cdr (car cl-alist)))))) | |
696 (setq cl-alist (cdr cl-alist))) | |
697 (and cl-alist (car cl-alist))) | |
698 (rassq cl-item cl-alist))) | |
699 | |
700 (defun rassoc-if (cl-pred cl-list &rest cl-keys) | |
701 "Find the first item whose cdr satisfies PREDICATE in LIST. | |
702 Keywords supported: :key" | |
703 (apply 'rassoc* nil cl-list ':if cl-pred cl-keys)) | |
704 | |
705 (defun rassoc-if-not (cl-pred cl-list &rest cl-keys) | |
706 "Find the first item whose cdr does not satisfy PREDICATE in LIST. | |
707 Keywords supported: :key" | |
708 (apply 'rassoc* nil cl-list ':if-not cl-pred cl-keys)) | |
709 | |
710 (defun union (cl-list1 cl-list2 &rest cl-keys) | |
711 "Combine LIST1 and LIST2 using a set-union operation. | |
712 The result list contains all items that appear in either LIST1 or LIST2. | |
713 This is a non-destructive function; it makes a copy of the data if necessary | |
714 to avoid corrupting the original LIST1 and LIST2. | |
715 Keywords supported: :test :test-not :key" | |
716 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) | |
717 ((equal cl-list1 cl-list2) cl-list1) | |
718 (t | |
719 (or (>= (length cl-list1) (length cl-list2)) | |
720 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) | |
721 (while cl-list2 | |
722 (if (or cl-keys (numberp (car cl-list2))) | |
723 (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys)) | |
724 (or (memq (car cl-list2) cl-list1) | |
725 (cl-push (car cl-list2) cl-list1))) | |
726 (cl-pop cl-list2)) | |
727 cl-list1))) | |
728 | |
729 (defun nunion (cl-list1 cl-list2 &rest cl-keys) | |
730 "Combine LIST1 and LIST2 using a set-union operation. | |
731 The result list contains all items that appear in either LIST1 or LIST2. | |
732 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
733 whenever possible. | |
734 Keywords supported: :test :test-not :key" | |
735 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) | |
736 (t (apply 'union cl-list1 cl-list2 cl-keys)))) | |
737 | |
738 (defun intersection (cl-list1 cl-list2 &rest cl-keys) | |
739 "Combine LIST1 and LIST2 using a set-intersection operation. | |
740 The result list contains all items that appear in both LIST1 and LIST2. | |
741 This is a non-destructive function; it makes a copy of the data if necessary | |
742 to avoid corrupting the original LIST1 and LIST2. | |
743 Keywords supported: :test :test-not :key" | |
744 (and cl-list1 cl-list2 | |
745 (if (equal cl-list1 cl-list2) cl-list1 | |
746 (cl-parsing-keywords (:key) (:test :test-not) | |
747 (let ((cl-res nil)) | |
748 (or (>= (length cl-list1) (length cl-list2)) | |
749 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) | |
750 (while cl-list2 | |
751 (if (if (or cl-keys (numberp (car cl-list2))) | |
752 (apply 'member* (cl-check-key (car cl-list2)) | |
753 cl-list1 cl-keys) | |
754 (memq (car cl-list2) cl-list1)) | |
755 (cl-push (car cl-list2) cl-res)) | |
756 (cl-pop cl-list2)) | |
757 cl-res))))) | |
758 | |
759 (defun nintersection (cl-list1 cl-list2 &rest cl-keys) | |
760 "Combine LIST1 and LIST2 using a set-intersection operation. | |
761 The result list contains all items that appear in both LIST1 and LIST2. | |
762 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
763 whenever possible. | |
764 Keywords supported: :test :test-not :key" | |
765 (and cl-list1 cl-list2 (apply 'intersection cl-list1 cl-list2 cl-keys))) | |
766 | |
767 (defun set-difference (cl-list1 cl-list2 &rest cl-keys) | |
768 "Combine LIST1 and LIST2 using a set-difference operation. | |
769 The result list contains all items that appear in LIST1 but not LIST2. | |
770 This is a non-destructive function; it makes a copy of the data if necessary | |
771 to avoid corrupting the original LIST1 and LIST2. | |
772 Keywords supported: :test :test-not :key" | |
773 (if (or (null cl-list1) (null cl-list2)) cl-list1 | |
774 (cl-parsing-keywords (:key) (:test :test-not) | |
775 (let ((cl-res nil)) | |
776 (while cl-list1 | |
777 (or (if (or cl-keys (numberp (car cl-list1))) | |
778 (apply 'member* (cl-check-key (car cl-list1)) | |
779 cl-list2 cl-keys) | |
780 (memq (car cl-list1) cl-list2)) | |
781 (cl-push (car cl-list1) cl-res)) | |
782 (cl-pop cl-list1)) | |
783 cl-res)))) | |
784 | |
785 (defun nset-difference (cl-list1 cl-list2 &rest cl-keys) | |
786 "Combine LIST1 and LIST2 using a set-difference operation. | |
787 The result list contains all items that appear in LIST1 but not LIST2. | |
788 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
789 whenever possible. | |
790 Keywords supported: :test :test-not :key" | |
791 (if (or (null cl-list1) (null cl-list2)) cl-list1 | |
792 (apply 'set-difference cl-list1 cl-list2 cl-keys))) | |
793 | |
794 (defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys) | |
795 "Combine LIST1 and LIST2 using a set-exclusive-or operation. | |
796 The result list contains all items that appear in exactly one of LIST1, LIST2. | |
797 This is a non-destructive function; it makes a copy of the data if necessary | |
798 to avoid corrupting the original LIST1 and LIST2. | |
799 Keywords supported: :test :test-not :key" | |
800 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) | |
801 ((equal cl-list1 cl-list2) nil) | |
802 (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys) | |
803 (apply 'set-difference cl-list2 cl-list1 cl-keys))))) | |
804 | |
805 (defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys) | |
806 "Combine LIST1 and LIST2 using a set-exclusive-or operation. | |
807 The result list contains all items that appear in exactly one of LIST1, LIST2. | |
808 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
809 whenever possible. | |
810 Keywords supported: :test :test-not :key" | |
811 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) | |
812 ((equal cl-list1 cl-list2) nil) | |
813 (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys) | |
814 (apply 'nset-difference cl-list2 cl-list1 cl-keys))))) | |
815 | |
816 (defun subsetp (cl-list1 cl-list2 &rest cl-keys) | |
817 "True if LIST1 is a subset of LIST2. | |
818 I.e., if every element of LIST1 also appears in LIST2. | |
819 Keywords supported: :test :test-not :key" | |
820 (cond ((null cl-list1) t) ((null cl-list2) nil) | |
821 ((equal cl-list1 cl-list2) t) | |
822 (t (cl-parsing-keywords (:key) (:test :test-not) | |
823 (while (and cl-list1 | |
824 (apply 'member* (cl-check-key (car cl-list1)) | |
825 cl-list2 cl-keys)) | |
826 (cl-pop cl-list1)) | |
827 (null cl-list1))))) | |
828 | |
829 (defun subst-if (cl-new cl-pred cl-tree &rest cl-keys) | |
830 "Substitute NEW for elements matching PREDICATE in TREE (non-destructively). | |
831 Return a copy of TREE with all matching elements replaced by NEW. | |
832 Keywords supported: :key" | |
833 (apply 'sublis (list (cons nil cl-new)) cl-tree ':if cl-pred cl-keys)) | |
834 | |
835 (defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys) | |
836 "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively). | |
837 Return a copy of TREE with all non-matching elements replaced by NEW. | |
838 Keywords supported: :key" | |
839 (apply 'sublis (list (cons nil cl-new)) cl-tree ':if-not cl-pred cl-keys)) | |
840 | |
841 (defun nsubst (cl-new cl-old cl-tree &rest cl-keys) | |
842 "Substitute NEW for OLD everywhere in TREE (destructively). | |
843 Any element of TREE which is `eql' to OLD is changed to NEW (via a call | |
844 to `setcar'). | |
845 Keywords supported: :test :test-not :key" | |
846 (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys)) | |
847 | |
848 (defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys) | |
849 "Substitute NEW for elements matching PREDICATE in TREE (destructively). | |
850 Any element of TREE which matches is changed to NEW (via a call to `setcar'). | |
851 Keywords supported: :key" | |
852 (apply 'nsublis (list (cons nil cl-new)) cl-tree ':if cl-pred cl-keys)) | |
853 | |
854 (defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys) | |
855 "Substitute NEW for elements not matching PREDICATE in TREE (destructively). | |
856 Any element of TREE which matches is changed to NEW (via a call to `setcar'). | |
857 Keywords supported: :key" | |
858 (apply 'nsublis (list (cons nil cl-new)) cl-tree ':if-not cl-pred cl-keys)) | |
859 | |
860 (defun sublis (cl-alist cl-tree &rest cl-keys) | |
861 "Perform substitutions indicated by ALIST in TREE (non-destructively). | |
862 Return a copy of TREE with all matching elements replaced. | |
863 Keywords supported: :test :test-not :key" | |
864 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
865 (cl-sublis-rec cl-tree))) | |
866 | |
867 (defvar cl-alist) | |
868 (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if* | |
869 (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist)) | |
870 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
871 (setq cl-p (cdr cl-p))) | |
872 (if cl-p (cdr (car cl-p)) | |
873 (if (consp cl-tree) | |
874 (let ((cl-a (cl-sublis-rec (car cl-tree))) | |
875 (cl-d (cl-sublis-rec (cdr cl-tree)))) | |
876 (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree))) | |
877 cl-tree | |
878 (cons cl-a cl-d))) | |
879 cl-tree)))) | |
880 | |
881 (defun nsublis (cl-alist cl-tree &rest cl-keys) | |
882 "Perform substitutions indicated by ALIST in TREE (destructively). | |
883 Any matching element of TREE is changed via a call to `setcar'. | |
884 Keywords supported: :test :test-not :key" | |
885 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
886 (let ((cl-hold (list cl-tree))) | |
887 (cl-nsublis-rec cl-hold) | |
888 (car cl-hold)))) | |
889 | |
890 (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if* | |
891 (while (consp cl-tree) | |
892 (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist)) | |
893 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
894 (setq cl-p (cdr cl-p))) | |
895 (if cl-p (setcar cl-tree (cdr (car cl-p))) | |
896 (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree)))) | |
897 (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist) | |
898 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
899 (setq cl-p (cdr cl-p))) | |
900 (if cl-p | |
901 (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil)) | |
902 (setq cl-tree (cdr cl-tree)))))) | |
903 | |
904 (defun tree-equal (cl-x cl-y &rest cl-keys) | |
905 "T if trees X and Y have `eql' leaves. | |
906 Atoms are compared by `eql'; cons cells are compared recursively. | |
907 Keywords supported: :test :test-not :key" | |
908 (cl-parsing-keywords (:test :test-not :key) () | |
909 (cl-tree-equal-rec cl-x cl-y))) | |
910 | |
911 (defun cl-tree-equal-rec (cl-x cl-y) | |
912 (while (and (consp cl-x) (consp cl-y) | |
913 (cl-tree-equal-rec (car cl-x) (car cl-y))) | |
914 (setq cl-x (cdr cl-x) cl-y (cdr cl-y))) | |
915 (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y))) | |
916 | |
917 | |
918 (run-hooks 'cl-seq-load-hook) | |
919 | |
920 ;;; cl-seq.el ends here |