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