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