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