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