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