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