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