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