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