Mercurial > hg > xemacs-beta
annotate lisp/cl-seq.el @ 5292:e4305eb6fb8c
Merge some permissions corrections to trunk.
| author | Stephen J. Turnbull <stephen@xemacs.org> |
|---|---|
| date | Mon, 18 Oct 2010 23:21:23 +0900 |
| parents | 69f687b3ba9d |
| children | d1b17a33450b 308d34e9f07d |
| 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 | |
|
5261
69f687b3ba9d
Move #'replace to C, add bounds-checking to it and to #'fill.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5242
diff
changeset
|
145 ;; XEmacs; #'replace is in fns.c. |
| 428 | 146 |
| 147 (defun remove* (cl-item cl-seq &rest cl-keys) | |
| 148 "Remove all occurrences of ITEM in SEQ. | |
| 149 This is a non-destructive function; it makes a copy of SEQ if necessary | |
| 150 to avoid corrupting the original SEQ. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 on these keywords. |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
157 :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
|
158 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
|
159 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
|
160 given by :end. |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
161 :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
|
162 :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
|
163 instead of the beginning; in this case, this matters only if :count is given." |
| 428 | 164 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end |
| 165 (:start 0) :end) () | |
| 166 (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
| 167 cl-seq | |
| 168 (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000))) | |
| 169 (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end | |
| 170 cl-from-end))) | |
| 171 (if cl-i | |
| 172 (let ((cl-res (apply 'delete* cl-item (append cl-seq nil) | |
| 173 (append (if cl-from-end | |
| 2153 | 174 (list :end (1+ cl-i)) |
| 175 (list :start cl-i)) | |
| 428 | 176 cl-keys)))) |
|
5242
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
177 (typecase cl-seq |
|
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
178 (list cl-res) |
|
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
179 (string (concat cl-res)) |
|
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
180 (vector (vconcat cl-res)) |
|
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
181 (bit-vector (bvconcat cl-res)))) |
| 428 | 182 cl-seq)) |
| 183 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
| 184 (if (= cl-start 0) | |
| 185 (while (and cl-seq (> cl-end 0) | |
| 186 (cl-check-test cl-item (car cl-seq)) | |
| 187 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) | |
| 188 (> (setq cl-count (1- cl-count)) 0)))) | |
| 189 (if (and (> cl-count 0) (> cl-end 0)) | |
| 190 (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq) | |
| 191 (setq cl-end (1- cl-end)) (cdr cl-seq)))) | |
| 192 (while (and cl-p (> cl-end 0) | |
| 193 (not (cl-check-test cl-item (car cl-p)))) | |
| 194 (setq cl-p (cdr cl-p) cl-end (1- cl-end))) | |
| 195 (if (and cl-p (> cl-end 0)) | |
| 196 (nconc (ldiff cl-seq cl-p) | |
| 197 (if (= cl-count 1) (cdr cl-p) | |
| 198 (and (cdr cl-p) | |
| 199 (apply 'delete* cl-item | |
| 200 (copy-sequence (cdr cl-p)) | |
| 2153 | 201 :start 0 :end (1- cl-end) |
| 202 :count (1- cl-count) cl-keys)))) | |
| 428 | 203 cl-seq)) |
| 204 cl-seq))))) | |
| 205 | |
| 206 (defun remove-if (cl-pred cl-list &rest cl-keys) | |
| 207 "Remove all items satisfying PREDICATE in SEQ. | |
| 208 This is a non-destructive function; it makes a copy of SEQ if necessary | |
| 209 to avoid corrupting the original SEQ. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
210 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
|
211 See `remove*' for the meaning of the keywords." |
| 2153 | 212 (apply 'remove* nil cl-list :if cl-pred cl-keys)) |
| 428 | 213 |
| 214 (defun remove-if-not (cl-pred cl-list &rest cl-keys) | |
| 215 "Remove all items not satisfying PREDICATE in SEQ. | |
| 216 This is a non-destructive function; it makes a copy of SEQ if necessary | |
| 217 to avoid corrupting the original SEQ. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
218 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
|
219 See `remove*' for the meaning of the keywords." |
| 2153 | 220 (apply 'remove* nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 221 |
| 222 (defun delete* (cl-item cl-seq &rest cl-keys) | |
| 223 "Remove all occurrences of ITEM in SEQ. | |
| 224 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
|
225 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
|
226 See `remove*' for the meaning of the keywords." |
| 428 | 227 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end |
| 228 (:start 0) :end) () | |
| 229 (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
| 230 cl-seq | |
| 231 (if (listp cl-seq) | |
| 232 (if (and cl-from-end (< cl-count 4000000)) | |
| 233 (let (cl-i) | |
| 234 (while (and (>= (setq cl-count (1- cl-count)) 0) | |
| 235 (setq cl-i (cl-position cl-item cl-seq cl-start | |
| 236 cl-end cl-from-end))) | |
| 237 (if (= cl-i 0) (setq cl-seq (cdr cl-seq)) | |
| 238 (let ((cl-tail (nthcdr (1- cl-i) cl-seq))) | |
| 239 (setcdr cl-tail (cdr (cdr cl-tail))))) | |
| 240 (setq cl-end cl-i)) | |
| 241 cl-seq) | |
| 242 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
| 243 (if (= cl-start 0) | |
| 244 (progn | |
| 245 (while (and cl-seq | |
| 246 (> cl-end 0) | |
| 247 (cl-check-test cl-item (car cl-seq)) | |
| 248 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) | |
| 249 (> (setq cl-count (1- cl-count)) 0))) | |
| 250 (setq cl-end (1- cl-end))) | |
| 251 (setq cl-start (1- cl-start))) | |
| 252 (if (and (> cl-count 0) (> cl-end 0)) | |
| 253 (let ((cl-p (nthcdr cl-start cl-seq))) | |
| 254 (while (and (cdr cl-p) (> cl-end 0)) | |
| 255 (if (cl-check-test cl-item (car (cdr cl-p))) | |
| 256 (progn | |
| 257 (setcdr cl-p (cdr (cdr cl-p))) | |
| 258 (if (= (setq cl-count (1- cl-count)) 0) | |
| 259 (setq cl-end 1))) | |
| 260 (setq cl-p (cdr cl-p))) | |
| 261 (setq cl-end (1- cl-end))))) | |
| 262 cl-seq) | |
| 263 (apply 'remove* cl-item cl-seq cl-keys))))) | |
| 264 | |
| 265 (defun delete-if (cl-pred cl-list &rest cl-keys) | |
| 266 "Remove all items satisfying PREDICATE in SEQ. | |
| 267 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
|
268 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
|
269 See `remove*' for the meaning of the keywords." |
| 2153 | 270 (apply 'delete* nil cl-list :if cl-pred cl-keys)) |
| 428 | 271 |
| 272 (defun delete-if-not (cl-pred cl-list &rest cl-keys) | |
| 273 "Remove all items not satisfying PREDICATE in SEQ. | |
| 274 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
|
275 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
|
276 See `remove*' for the meaning of the keywords." |
| 2153 | 277 (apply 'delete* nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 278 |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
279 ;; XEmacs change: this is in subr.el in GNU Emacs |
| 428 | 280 (defun remove (cl-item cl-seq) |
| 281 "Remove all occurrences of ITEM in SEQ, testing with `equal' | |
| 282 This is a non-destructive function; it makes a copy of SEQ if necessary | |
| 283 to avoid corrupting the original SEQ. | |
| 284 Also see: `remove*', `delete', `delete*'" | |
| 285 (remove* cl-item cl-seq ':test 'equal)) | |
| 286 | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
287 ;; XEmacs change: this is in subr.el in GNU Emacs |
| 428 | 288 (defun remq (cl-elt cl-list) |
| 442 | 289 "Remove all occurrences of ELT in LIST, comparing with `eq'. |
| 428 | 290 This is a non-destructive function; it makes a copy of LIST to avoid |
| 291 corrupting the original LIST. | |
| 292 Also see: `delq', `delete', `delete*', `remove', `remove*'." | |
| 293 (if (memq cl-elt cl-list) | |
| 294 (delq cl-elt (copy-list cl-list)) | |
| 295 cl-list)) | |
| 296 | |
| 297 (defun remove-duplicates (cl-seq &rest cl-keys) | |
| 298 "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
|
299 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
|
300 See `remove*' for the meaning of the keywords." |
| 428 | 301 (cl-delete-duplicates cl-seq cl-keys t)) |
| 302 | |
| 303 (defun delete-duplicates (cl-seq &rest cl-keys) | |
| 304 "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
|
305 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
|
306 See `remove*' for the meaning of the keywords." |
| 428 | 307 (cl-delete-duplicates cl-seq cl-keys nil)) |
| 308 | |
| 309 (defun cl-delete-duplicates (cl-seq cl-keys cl-copy) | |
| 310 (if (listp cl-seq) | |
| 311 (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if) | |
| 312 () | |
| 313 (if cl-from-end | |
| 314 (let ((cl-p (nthcdr cl-start cl-seq)) cl-i) | |
| 315 (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
| 316 (while (> cl-end 1) | |
| 317 (setq cl-i 0) | |
| 318 (while (setq cl-i (cl-position (cl-check-key (car cl-p)) | |
| 319 (cdr cl-p) cl-i (1- cl-end))) | |
| 320 (if cl-copy (setq cl-seq (copy-sequence cl-seq) | |
| 321 cl-p (nthcdr cl-start cl-seq) cl-copy nil)) | |
| 322 (let ((cl-tail (nthcdr cl-i cl-p))) | |
| 323 (setcdr cl-tail (cdr (cdr cl-tail)))) | |
| 324 (setq cl-end (1- cl-end))) | |
| 325 (setq cl-p (cdr cl-p) cl-end (1- cl-end) | |
| 326 cl-start (1+ cl-start))) | |
| 327 cl-seq) | |
| 328 (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
| 329 (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1) | |
| 330 (cl-position (cl-check-key (car cl-seq)) | |
| 331 (cdr cl-seq) 0 (1- cl-end))) | |
| 332 (setq cl-seq (cdr cl-seq) cl-end (1- cl-end))) | |
| 333 (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq) | |
| 334 (setq cl-end (1- cl-end) cl-start 1) cl-seq))) | |
| 335 (while (and (cdr (cdr cl-p)) (> cl-end 1)) | |
| 336 (if (cl-position (cl-check-key (car (cdr cl-p))) | |
| 337 (cdr (cdr cl-p)) 0 (1- cl-end)) | |
| 338 (progn | |
| 339 (if cl-copy (setq cl-seq (copy-sequence cl-seq) | |
| 340 cl-p (nthcdr (1- cl-start) cl-seq) | |
| 341 cl-copy nil)) | |
| 342 (setcdr cl-p (cdr (cdr cl-p)))) | |
| 343 (setq cl-p (cdr cl-p))) | |
| 344 (setq cl-end (1- cl-end) cl-start (1+ cl-start))) | |
| 345 cl-seq))) | |
| 346 (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil))) | |
|
5242
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
347 (typecase cl-seq |
|
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
348 (string (concat cl-res)) |
|
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
349 (vector (vconcat cl-res)) |
|
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5227
diff
changeset
|
350 (bit-vector (bvconcat cl-res)))))) |
| 428 | 351 |
| 352 (defun substitute (cl-new cl-old cl-seq &rest cl-keys) | |
| 353 "Substitute NEW for OLD in SEQ. | |
| 354 This is a non-destructive function; it makes a copy of SEQ if necessary | |
| 355 to avoid corrupting the original SEQ. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
356 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
|
357 See `remove*' for the meaning of the keywords." |
| 428 | 358 (cl-parsing-keywords (:test :test-not :key :if :if-not :count |
| 359 (:start 0) :end :from-end) () | |
| 360 (if (or (eq cl-old cl-new) | |
| 361 (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0)) | |
| 362 cl-seq | |
| 363 (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end))) | |
| 364 (if (not cl-i) | |
| 365 cl-seq | |
| 366 (setq cl-seq (copy-sequence cl-seq)) | |
| 367 (or cl-from-end | |
| 368 (progn (cl-set-elt cl-seq cl-i cl-new) | |
| 369 (setq cl-i (1+ cl-i) cl-count (1- cl-count)))) | |
| 2153 | 370 (apply 'nsubstitute cl-new cl-old cl-seq :count cl-count |
| 371 :start cl-i cl-keys)))))) | |
| 428 | 372 |
| 373 (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys) | |
| 374 "Substitute NEW for all items satisfying PREDICATE in SEQ. | |
| 375 This is a non-destructive function; it makes a copy of SEQ if necessary | |
| 376 to avoid corrupting the original SEQ. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
377 See `remove*' for the meaning of the keywords." |
| 2153 | 378 (apply 'substitute cl-new nil cl-list :if cl-pred cl-keys)) |
| 428 | 379 |
| 380 (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys) | |
| 381 "Substitute NEW for all items not satisfying PREDICATE in SEQ. | |
| 382 This is a non-destructive function; it makes a copy of SEQ if necessary | |
| 383 to avoid corrupting the original SEQ. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
384 See `remove*' for the meaning of the keywords." |
| 2153 | 385 (apply 'substitute cl-new nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 386 |
| 387 (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys) | |
| 388 "Substitute NEW for OLD in SEQ. | |
| 389 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
|
390 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
|
391 See `remove*' for the meaning of the keywords." |
| 428 | 392 (cl-parsing-keywords (:test :test-not :key :if :if-not :count |
| 393 (:start 0) :end :from-end) () | |
| 394 (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0) | |
| 395 (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000))) | |
| 396 (let ((cl-p (nthcdr cl-start cl-seq))) | |
| 397 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
| 398 (while (and cl-p (> cl-end 0) (> cl-count 0)) | |
| 399 (if (cl-check-test cl-old (car cl-p)) | |
| 400 (progn | |
| 401 (setcar cl-p cl-new) | |
| 402 (setq cl-count (1- cl-count)))) | |
| 403 (setq cl-p (cdr cl-p) cl-end (1- cl-end)))) | |
| 404 (or cl-end (setq cl-end (length cl-seq))) | |
| 405 (if cl-from-end | |
| 406 (while (and (< cl-start cl-end) (> cl-count 0)) | |
| 407 (setq cl-end (1- cl-end)) | |
| 408 (if (cl-check-test cl-old (elt cl-seq cl-end)) | |
| 409 (progn | |
| 410 (cl-set-elt cl-seq cl-end cl-new) | |
| 411 (setq cl-count (1- cl-count))))) | |
| 412 (while (and (< cl-start cl-end) (> cl-count 0)) | |
| 413 (if (cl-check-test cl-old (aref cl-seq cl-start)) | |
| 414 (progn | |
| 415 (aset cl-seq cl-start cl-new) | |
| 416 (setq cl-count (1- cl-count)))) | |
| 417 (setq cl-start (1+ cl-start)))))) | |
| 418 cl-seq)) | |
| 419 | |
| 420 (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys) | |
| 421 "Substitute NEW for all items satisfying PREDICATE in SEQ. | |
| 422 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
|
423 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
|
424 See `remove*' for the meaning of the keywords." |
| 2153 | 425 (apply 'nsubstitute cl-new nil cl-list :if cl-pred cl-keys)) |
| 428 | 426 |
| 427 (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys) | |
| 428 "Substitute NEW for all items not satisfying PREDICATE in SEQ. | |
| 429 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
|
430 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
|
431 See `remove*' for the meaning of the keywords." |
| 2153 | 432 (apply 'nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 433 |
| 434 (defun find (cl-item cl-seq &rest cl-keys) | |
| 435 "Find the first occurrence of ITEM in LIST. | |
| 436 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
|
437 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
|
438 See `remove*' for the meaning of the keywords." |
| 428 | 439 (let ((cl-pos (apply 'position cl-item cl-seq cl-keys))) |
| 440 (and cl-pos (elt cl-seq cl-pos)))) | |
| 441 | |
| 442 (defun find-if (cl-pred cl-list &rest cl-keys) | |
| 443 "Find the first item satisfying PREDICATE in LIST. | |
| 444 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
|
445 Keywords supported: :key :start :end :from-end |
|
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 'find nil cl-list :if cl-pred cl-keys)) |
| 428 | 448 |
| 449 (defun find-if-not (cl-pred cl-list &rest cl-keys) | |
| 450 "Find the first item not satisfying PREDICATE in LIST. | |
| 451 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
|
452 Keywords supported: :key :start :end :from-end |
|
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 'find nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 455 |
| 456 (defun position (cl-item cl-seq &rest cl-keys) | |
| 457 "Find the first occurrence of ITEM in LIST. | |
| 458 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
|
459 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
|
460 See `remove*' for the meaning of the keywords." |
| 428 | 461 (cl-parsing-keywords (:test :test-not :key :if :if-not |
| 462 (:start 0) :end :from-end) () | |
| 463 (cl-position cl-item cl-seq cl-start cl-end cl-from-end))) | |
| 464 | |
| 465 (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end) | |
| 466 (if (listp cl-seq) | |
| 467 (let ((cl-p (nthcdr cl-start cl-seq))) | |
| 468 (or cl-end (setq cl-end 8000000)) | |
| 469 (let ((cl-res nil)) | |
| 470 (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end)) | |
| 471 (if (cl-check-test cl-item (car cl-p)) | |
| 472 (setq cl-res cl-start)) | |
| 473 (setq cl-p (cdr cl-p) cl-start (1+ cl-start))) | |
| 474 cl-res)) | |
| 475 (or cl-end (setq cl-end (length cl-seq))) | |
| 476 (if cl-from-end | |
| 477 (progn | |
| 478 (while (and (>= (setq cl-end (1- cl-end)) cl-start) | |
| 479 (not (cl-check-test cl-item (aref cl-seq cl-end))))) | |
| 480 (and (>= cl-end cl-start) cl-end)) | |
| 481 (while (and (< cl-start cl-end) | |
| 482 (not (cl-check-test cl-item (aref cl-seq cl-start)))) | |
| 483 (setq cl-start (1+ cl-start))) | |
| 484 (and (< cl-start cl-end) cl-start)))) | |
| 485 | |
| 486 (defun position-if (cl-pred cl-list &rest cl-keys) | |
| 487 "Find the first item satisfying PREDICATE in LIST. | |
| 488 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
|
489 Keywords supported: :key :start :end :from-end |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
490 See `remove*' for the meaning of the keywords." |
| 2153 | 491 (apply 'position nil cl-list :if cl-pred cl-keys)) |
| 428 | 492 |
| 493 (defun position-if-not (cl-pred cl-list &rest cl-keys) | |
| 494 "Find the first item not satisfying PREDICATE in LIST. | |
| 495 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
|
496 Keywords supported: :key :start :end :from-end |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
497 See `remove*' for the meaning of the keywords." |
| 2153 | 498 (apply 'position nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 499 |
| 500 (defun count (cl-item cl-seq &rest cl-keys) | |
| 501 "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
|
502 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
|
503 See `remove*' for the meaning of the keywords." |
| 428 | 504 (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) () |
| 505 (let ((cl-count 0) cl-x) | |
| 506 (or cl-end (setq cl-end (length cl-seq))) | |
| 507 (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq))) | |
| 508 (while (< cl-start cl-end) | |
| 2153 | 509 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start))) |
| 428 | 510 (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count))) |
| 511 (setq cl-start (1+ cl-start))) | |
| 512 cl-count))) | |
| 513 | |
| 514 (defun count-if (cl-pred cl-list &rest cl-keys) | |
| 515 "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
|
516 Keywords supported: :key :start :end |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
517 See `remove*' for the meaning of the keywords." |
| 2153 | 518 (apply 'count nil cl-list :if cl-pred cl-keys)) |
| 428 | 519 |
| 520 (defun count-if-not (cl-pred cl-list &rest cl-keys) | |
| 521 "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
|
522 Keywords supported: :key :start :end |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
523 See `remove*' for the meaning of the keywords." |
| 2153 | 524 (apply 'count nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 525 |
| 526 (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys) | |
| 527 "Compare SEQ1 with SEQ2, return index of first mismatching element. | |
| 528 Return nil if the sequences match. If one sequence is a prefix of the | |
| 2153 | 529 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
|
530 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
|
531 See `search' for the meaning of the keywords." |
| 428 | 532 (cl-parsing-keywords (:test :test-not :key :from-end |
| 533 (:start1 0) :end1 (:start2 0) :end2) () | |
| 534 (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
| 535 (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
| 536 (if cl-from-end | |
| 537 (progn | |
| 538 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
| 539 (cl-check-match (elt cl-seq1 (1- cl-end1)) | |
| 540 (elt cl-seq2 (1- cl-end2)))) | |
| 541 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2))) | |
| 542 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
| 543 (1- cl-end1))) | |
| 544 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1))) | |
| 545 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2)))) | |
| 546 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
| 547 (cl-check-match (if cl-p1 (car cl-p1) | |
| 548 (aref cl-seq1 cl-start1)) | |
| 549 (if cl-p2 (car cl-p2) | |
| 550 (aref cl-seq2 cl-start2)))) | |
| 551 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2) | |
| 552 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2))) | |
| 553 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
| 554 cl-start1))))) | |
| 555 | |
| 556 (defun search (cl-seq1 cl-seq2 &rest cl-keys) | |
| 557 "Search for SEQ1 as a subsequence of SEQ2. | |
| 558 Return the index of the leftmost element of the first match found; | |
| 559 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
|
560 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
|
561 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
|
562 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
|
563 of SEQ2." |
| 428 | 564 (cl-parsing-keywords (:test :test-not :key :from-end |
| 565 (:start1 0) :end1 (:start2 0) :end2) () | |
| 566 (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
| 567 (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
| 568 (if (>= cl-start1 cl-end1) | |
| 569 (if cl-from-end cl-end2 cl-start2) | |
| 570 (let* ((cl-len (- cl-end1 cl-start1)) | |
| 571 (cl-first (cl-check-key (elt cl-seq1 cl-start1))) | |
| 572 (cl-if nil) cl-pos) | |
| 573 (setq cl-end2 (- cl-end2 (1- cl-len))) | |
| 574 (while (and (< cl-start2 cl-end2) | |
| 575 (setq cl-pos (cl-position cl-first cl-seq2 | |
| 576 cl-start2 cl-end2 cl-from-end)) | |
| 577 (apply 'mismatch cl-seq1 cl-seq2 | |
| 2153 | 578 :start1 (1+ cl-start1) :end1 cl-end1 |
| 579 :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len) | |
| 580 :from-end nil cl-keys)) | |
| 428 | 581 (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos)))) |
| 582 (and (< cl-start2 cl-end2) cl-pos))))) | |
| 583 | |
| 584 (defun stable-sort (cl-seq cl-pred &rest cl-keys) | |
| 585 "Sort the argument SEQUENCE stably according to PREDICATE. | |
| 586 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
|
587 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
588 :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
|
589 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
|
590 `member*' for more information. |
| 428 | 591 |
|
5182
2e528066e2fc
Move #'sort*, #'fill, #'merge to C from cl-seq.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5084
diff
changeset
|
592 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
|
593 (apply 'sort* cl-seq cl-pred cl-keys)) |
| 428 | 594 |
| 595 ;;; See compiler macro in cl-macs.el | |
| 596 (defun member* (cl-item cl-list &rest cl-keys) | |
| 597 "Find the first occurrence of ITEM in LIST. | |
| 598 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
|
599 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
600 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
|
601 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
|
602 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
|
603 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
|
604 returns nil. |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
605 :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
|
606 \"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
|
607 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
|
608 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
|
609 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
|
610 LIST." |
| 428 | 611 (if cl-keys |
| 612 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
| 613 (while (and cl-list (not (cl-check-test cl-item (car cl-list)))) | |
| 614 (setq cl-list (cdr cl-list))) | |
| 615 cl-list) | |
|
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
2153
diff
changeset
|
616 (if (and (numberp cl-item) (not (fixnump cl-item))) |
| 428 | 617 (member cl-item cl-list) |
| 618 (memq cl-item cl-list)))) | |
| 619 | |
| 620 (defun member-if (cl-pred cl-list &rest cl-keys) | |
| 621 "Find the first item satisfying PREDICATE in LIST. | |
| 622 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
|
623 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
624 See `member*' for the meaning of :key." |
| 2153 | 625 (apply 'member* nil cl-list :if cl-pred cl-keys)) |
| 428 | 626 |
| 627 (defun member-if-not (cl-pred cl-list &rest cl-keys) | |
| 628 "Find the first item not satisfying PREDICATE in LIST. | |
| 629 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
|
630 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
631 See `member*' for the meaning of :key." |
| 2153 | 632 (apply 'member* nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 633 |
| 634 (defun cl-adjoin (cl-item cl-list &rest cl-keys) | |
| 635 (if (cl-parsing-keywords (:key) t | |
| 636 (apply 'member* (cl-check-key cl-item) cl-list cl-keys)) | |
| 637 cl-list | |
| 638 (cons cl-item cl-list))) | |
| 639 | |
| 640 ;;; See compiler macro in cl-macs.el | |
| 641 (defun assoc* (cl-item cl-alist &rest cl-keys) | |
| 642 "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
|
643 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
644 See `member*' for the meaning of :test, :test-not and :key." |
| 428 | 645 (if cl-keys |
| 646 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
| 647 (while (and cl-alist | |
| 648 (or (not (consp (car cl-alist))) | |
| 649 (not (cl-check-test cl-item (car (car cl-alist)))))) | |
| 650 (setq cl-alist (cdr cl-alist))) | |
| 651 (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
|
652 (if (and (numberp cl-item) (not (fixnump cl-item))) |
| 428 | 653 (assoc cl-item cl-alist) |
| 654 (assq cl-item cl-alist)))) | |
| 655 | |
| 656 (defun assoc-if (cl-pred cl-list &rest cl-keys) | |
| 657 "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
|
658 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
659 See `member*' for the meaning of :key." |
| 2153 | 660 (apply 'assoc* nil cl-list :if cl-pred cl-keys)) |
| 428 | 661 |
| 662 (defun assoc-if-not (cl-pred cl-list &rest cl-keys) | |
| 663 "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
|
664 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
665 See `member*' for the meaning of :key." |
| 2153 | 666 (apply 'assoc* nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 667 |
| 668 (defun rassoc* (cl-item cl-alist &rest cl-keys) | |
| 669 "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
|
670 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
671 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
|
672 (if (or cl-keys (and (numberp cl-item) (not (fixnump cl-item)))) |
| 428 | 673 (cl-parsing-keywords (:test :test-not :key :if :if-not) () |
| 674 (while (and cl-alist | |
| 675 (or (not (consp (car cl-alist))) | |
| 676 (not (cl-check-test cl-item (cdr (car cl-alist)))))) | |
| 677 (setq cl-alist (cdr cl-alist))) | |
| 678 (and cl-alist (car cl-alist))) | |
| 679 (rassq cl-item cl-alist))) | |
| 680 | |
| 681 (defun rassoc-if (cl-pred cl-list &rest cl-keys) | |
| 682 "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
|
683 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
684 See `member*' for the meaning of :key." |
| 2153 | 685 (apply 'rassoc* nil cl-list :if cl-pred cl-keys)) |
| 428 | 686 |
| 687 (defun rassoc-if-not (cl-pred cl-list &rest cl-keys) | |
| 688 "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
|
689 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
690 See `member*' for the meaning of :key." |
| 2153 | 691 (apply 'rassoc* nil cl-list :if-not cl-pred cl-keys)) |
| 428 | 692 |
| 693 (defun union (cl-list1 cl-list2 &rest cl-keys) | |
| 694 "Combine LIST1 and LIST2 using a set-union operation. | |
| 695 The result list contains all items that appear in either LIST1 or LIST2. | |
| 696 This is a non-destructive function; it makes a copy of the data if necessary | |
| 697 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
|
698 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
699 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
|
700 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
|
701 information. |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
702 :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
|
703 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
|
704 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
|
705 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
|
706 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
|
707 from the elements in LIST1 and LIST2." |
| 428 | 708 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
| 709 ((equal cl-list1 cl-list2) cl-list1) | |
| 710 (t | |
| 711 (or (>= (length cl-list1) (length cl-list2)) | |
| 712 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) | |
| 713 (while cl-list2 | |
| 714 (if (or cl-keys (numberp (car cl-list2))) | |
| 715 (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys)) | |
| 716 (or (memq (car cl-list2) cl-list1) | |
| 2153 | 717 (push (car cl-list2) cl-list1))) |
| 718 (pop cl-list2)) | |
| 428 | 719 cl-list1))) |
| 720 | |
| 721 (defun nunion (cl-list1 cl-list2 &rest cl-keys) | |
| 722 "Combine LIST1 and LIST2 using a set-union operation. | |
| 723 The result list contains all items that appear in either LIST1 or LIST2. | |
| 724 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
| 725 whenever possible. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
726 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
727 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 728 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
| 729 (t (apply 'union cl-list1 cl-list2 cl-keys)))) | |
| 730 | |
|
5067
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
731 ;; 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
|
732 (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
|
733 "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
|
734 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
|
735 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
|
736 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
|
737 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
|
738 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
|
739 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
|
740 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
|
741 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
|
742 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
|
743 |
|
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
744 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
|
745 extension." |
|
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
746 ;; 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
|
747 ;; 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
|
748 ;; 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
|
749 ;; `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
|
750 ;; 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
|
751 ;; 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
|
752 (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
|
753 ((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
|
754 (t |
|
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
755 (append |
|
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
756 cl-list1 |
|
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
757 (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
|
758 (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
|
759 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
|
760 (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
|
761 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
|
762 (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
|
763 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
|
764 |
| 428 | 765 (defun intersection (cl-list1 cl-list2 &rest cl-keys) |
| 766 "Combine LIST1 and LIST2 using a set-intersection operation. | |
| 767 The result list contains all items that appear in both LIST1 and LIST2. | |
| 768 This is a non-destructive function; it makes a copy of the data if necessary | |
| 769 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
|
770 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
771 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 772 (and cl-list1 cl-list2 |
| 773 (if (equal cl-list1 cl-list2) cl-list1 | |
| 774 (cl-parsing-keywords (:key) (:test :test-not) | |
| 775 (let ((cl-res nil)) | |
| 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 (if (or cl-keys (numberp (car cl-list2))) | |
| 780 (apply 'member* (cl-check-key (car cl-list2)) | |
| 781 cl-list1 cl-keys) | |
| 782 (memq (car cl-list2) cl-list1)) | |
| 2153 | 783 (push (car cl-list2) cl-res)) |
| 784 (pop cl-list2)) | |
| 428 | 785 cl-res))))) |
| 786 | |
| 787 (defun nintersection (cl-list1 cl-list2 &rest cl-keys) | |
| 788 "Combine LIST1 and LIST2 using a set-intersection operation. | |
| 789 The result list contains all items that appear in both LIST1 and LIST2. | |
| 790 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
| 791 whenever possible. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
792 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
793 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 794 (and cl-list1 cl-list2 (apply 'intersection 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-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
|
798 "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
|
799 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
|
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 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
|
802 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
|
803 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
|
804 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
|
805 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
|
806 |
|
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
807 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
|
808 extension." |
|
7d7ae8db0341
add functions `stable-union' and `stable-intersection' to do stable set operations
Ben Wing <ben@xemacs.org>
parents:
5066
diff
changeset
|
809 ;; 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
|
810 ;; 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
|
811 ;; 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
|
812 ;; `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
|
813 ;; 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
|
814 ;; 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
|
815 (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
|
816 (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
|
817 (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
|
818 (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
|
819 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
|
820 (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
|
821 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
|
822 (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
|
823 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
|
824 |
| 428 | 825 (defun set-difference (cl-list1 cl-list2 &rest cl-keys) |
| 826 "Combine LIST1 and LIST2 using a set-difference operation. | |
| 827 The result list contains all items that appear in LIST1 but not LIST2. | |
| 828 This is a non-destructive function; it makes a copy of the data if necessary | |
| 829 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
|
830 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
831 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 832 (if (or (null cl-list1) (null cl-list2)) cl-list1 |
| 833 (cl-parsing-keywords (:key) (:test :test-not) | |
| 834 (let ((cl-res nil)) | |
| 835 (while cl-list1 | |
| 836 (or (if (or cl-keys (numberp (car cl-list1))) | |
| 837 (apply 'member* (cl-check-key (car cl-list1)) | |
| 838 cl-list2 cl-keys) | |
| 839 (memq (car cl-list1) cl-list2)) | |
| 2153 | 840 (push (car cl-list1) cl-res)) |
| 841 (pop cl-list1)) | |
| 428 | 842 cl-res)))) |
| 843 | |
| 844 (defun nset-difference (cl-list1 cl-list2 &rest cl-keys) | |
| 845 "Combine LIST1 and LIST2 using a set-difference operation. | |
| 846 The result list contains all items that appear in LIST1 but not LIST2. | |
| 847 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
| 848 whenever possible. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
849 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
850 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 851 (if (or (null cl-list1) (null cl-list2)) cl-list1 |
| 852 (apply 'set-difference cl-list1 cl-list2 cl-keys))) | |
| 853 | |
| 854 (defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys) | |
| 855 "Combine LIST1 and LIST2 using a set-exclusive-or operation. | |
| 856 The result list contains all items that appear in exactly one of LIST1, LIST2. | |
| 857 This is a non-destructive function; it makes a copy of the data if necessary | |
| 858 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
|
859 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
860 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 861 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
| 862 ((equal cl-list1 cl-list2) nil) | |
| 863 (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys) | |
| 864 (apply 'set-difference cl-list2 cl-list1 cl-keys))))) | |
| 865 | |
| 866 (defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys) | |
| 867 "Combine LIST1 and LIST2 using a set-exclusive-or operation. | |
| 868 The result list contains all items that appear in exactly one of LIST1, LIST2. | |
| 869 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
| 870 whenever possible. | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
871 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
872 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 873 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
| 874 ((equal cl-list1 cl-list2) nil) | |
| 875 (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys) | |
| 876 (apply 'nset-difference cl-list2 cl-list1 cl-keys))))) | |
| 877 | |
| 878 (defun subsetp (cl-list1 cl-list2 &rest cl-keys) | |
| 879 "True if LIST1 is a subset of LIST2. | |
| 880 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
|
881 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
882 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 883 (cond ((null cl-list1) t) ((null cl-list2) nil) |
| 884 ((equal cl-list1 cl-list2) t) | |
| 885 (t (cl-parsing-keywords (:key) (:test :test-not) | |
| 886 (while (and cl-list1 | |
| 887 (apply 'member* (cl-check-key (car cl-list1)) | |
| 888 cl-list2 cl-keys)) | |
| 2153 | 889 (pop cl-list1)) |
| 428 | 890 (null cl-list1))))) |
| 891 | |
| 892 (defun subst-if (cl-new cl-pred cl-tree &rest cl-keys) | |
| 893 "Substitute NEW for elements matching PREDICATE in TREE (non-destructively). | |
| 894 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
|
895 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
896 See `member*' for the meaning of :key." |
| 2153 | 897 (apply 'sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys)) |
| 428 | 898 |
| 899 (defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys) | |
| 900 "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively). | |
| 901 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
|
902 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
903 See `member*' for the meaning of :key." |
| 2153 | 904 (apply 'sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys)) |
| 428 | 905 |
| 906 (defun nsubst (cl-new cl-old cl-tree &rest cl-keys) | |
| 907 "Substitute NEW for OLD everywhere in TREE (destructively). | |
| 908 Any element of TREE which is `eql' to OLD is changed to NEW (via a call | |
| 909 to `setcar'). | |
|
5066
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
910 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
911 See `member*' for the meaning of :test, :test-not and :key." |
| 428 | 912 (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys)) |
| 913 | |
| 914 (defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys) | |
| 915 "Substitute NEW for elements matching PREDICATE in TREE (destructively). | |
| 916 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
|
917 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
918 See `member*' for the meaning of :key." |
| 2153 | 919 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys)) |
| 428 | 920 |
| 921 (defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys) | |
| 922 "Substitute NEW for elements not matching PREDICATE in TREE (destructively). | |
| 923 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
|
924 Keywords supported: :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
925 See `member*' for the meaning of :key." |
| 2153 | 926 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys)) |
| 428 | 927 |
| 928 (defun sublis (cl-alist cl-tree &rest cl-keys) | |
| 929 "Perform substitutions indicated by ALIST in TREE (non-destructively). | |
| 930 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
|
931 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
932 See `member*' for the meaning of :test, :test-not and :key." |
| 428 | 933 (cl-parsing-keywords (:test :test-not :key :if :if-not) () |
| 934 (cl-sublis-rec cl-tree))) | |
| 935 | |
| 936 (defvar cl-alist) | |
| 937 (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if* | |
| 938 (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist)) | |
| 939 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
| 940 (setq cl-p (cdr cl-p))) | |
| 941 (if cl-p (cdr (car cl-p)) | |
| 942 (if (consp cl-tree) | |
| 943 (let ((cl-a (cl-sublis-rec (car cl-tree))) | |
| 944 (cl-d (cl-sublis-rec (cdr cl-tree)))) | |
| 945 (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree))) | |
| 946 cl-tree | |
| 947 (cons cl-a cl-d))) | |
| 948 cl-tree)))) | |
| 949 | |
| 950 (defun nsublis (cl-alist cl-tree &rest cl-keys) | |
| 951 "Perform substitutions indicated by ALIST in TREE (destructively). | |
| 952 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
|
953 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
954 See `member*' for the meaning of :test, :test-not and :key." |
| 428 | 955 (cl-parsing-keywords (:test :test-not :key :if :if-not) () |
| 956 (let ((cl-hold (list cl-tree))) | |
| 957 (cl-nsublis-rec cl-hold) | |
| 958 (car cl-hold)))) | |
| 959 | |
| 960 (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if* | |
| 961 (while (consp cl-tree) | |
| 962 (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist)) | |
| 963 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
| 964 (setq cl-p (cdr cl-p))) | |
| 965 (if cl-p (setcar cl-tree (cdr (car cl-p))) | |
| 966 (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree)))) | |
| 967 (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist) | |
| 968 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
| 969 (setq cl-p (cdr cl-p))) | |
| 970 (if cl-p | |
| 971 (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil)) | |
| 972 (setq cl-tree (cdr cl-tree)))))) | |
| 973 | |
| 974 (defun tree-equal (cl-x cl-y &rest cl-keys) | |
| 975 "Return t if trees X and Y have `eql' leaves. | |
| 976 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
|
977 Keywords supported: :test :test-not :key |
|
545ec923b4eb
add documentation on keywords to cl*.el
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
978 See `union' for the meaning of :test, :test-not and :key." |
| 428 | 979 (cl-parsing-keywords (:test :test-not :key) () |
| 980 (cl-tree-equal-rec cl-x cl-y))) | |
| 981 | |
| 982 (defun cl-tree-equal-rec (cl-x cl-y) | |
| 983 (while (and (consp cl-x) (consp cl-y) | |
| 984 (cl-tree-equal-rec (car cl-x) (car cl-y))) | |
| 985 (setq cl-x (cdr cl-x) cl-y (cdr cl-y))) | |
| 986 (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y))) | |
| 987 | |
| 988 | |
| 989 (run-hooks 'cl-seq-load-hook) | |
| 990 | |
| 2153 | 991 ;;; arch-tag: ec1cc072-9006-4225-b6ba-d6b07ed1710c |
| 428 | 992 ;;; cl-seq.el ends here |
