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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
613
023b83f4e54b [xemacs-hg @ 2001-06-10 10:42:16 by ben]
ben
parents: 442
diff changeset
1 ;;; cl-seq.el --- Common Lisp extensions for XEmacs Lisp (part three)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 ;; Maintainer: XEmacs Development Team
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ;; Version: 2.02
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 ;; Keywords: extensions, dumped
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ;; XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 ;; under the terms of the GNU General Public License as published by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 ;; the Free Software Foundation; either version 2, or (at your option)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 ;; any later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 ;; XEmacs is distributed in the hope that it will be useful, but
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 ;; General Public License for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 ;; 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
28 ;;; Synched up with: FSF 21.3.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 ;; This file is dumped with XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 ;; These are extensions to Emacs Lisp that provide a degree of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 ;; Common Lisp compatibility, beyond what is already built-in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 ;; in Emacs Lisp.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 ;; This package was written by Dave Gillespie; it is a complete
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 ;; Bug reports, comments, and suggestions are welcome!
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 ;; This file contains the Common Lisp sequence and list functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 ;; which take keyword arguments.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 ;; See cl.el for Change Log.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 ;;; Code:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 ;;; Keyword parsing. This is special-cased here so that we can compile
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 ;;; this file independent from cl-macs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 (defmacro cl-parsing-keywords (kwords other-keys &rest body)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
57 "Helper macro for functions with keyword arguments.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
58 This is a temporary solution, until keyword arguments are natively supported.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
59 Declare your function ending with (... &rest cl-keys), then wrap the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
60 function body in a call to `cl-parsing-keywords'.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
61
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
62 KWORDS is a list of keyword definitions. Each definition should be
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
63 either a keyword or a list (KEYWORD DEFAULT-VALUE). In the former case,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
64 the default value is nil. The keywords are available in BODY as the name
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
65 of the keyword, minus its initial colon and prepended with `cl-'.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
66
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
67 OTHER-KEYS specifies other keywords that are accepted but ignored. It
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
68 is either the value 't' (ignore all other keys, equivalent to the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
69 &allow-other-keys argument declaration in Common Lisp) or a list in the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
70 same format as KWORDS. If keywords are given that are not in KWORDS
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
71 and not allowed by OTHER-KEYS, an error will normally be signalled; but
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
72 the caller can override this by specifying a non-nil value for the
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
73 keyword :allow-other-keys (which defaults to t)."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 (cons
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 'let*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 (cons (mapcar
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 (lambda (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 (let* ((var (if (consp x) (car x) x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 (mem (list 'car (list 'cdr (list 'memq (list 'quote var)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 'cl-keys)))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
82 (if (eq var :test-not)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 (setq mem (list 'and mem (list 'setq 'cl-test mem) t)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
84 (if (eq var :if-not)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 (setq mem (list 'and mem (list 'setq 'cl-if mem) t)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 (list (intern
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 (format "cl-%s" (substring (symbol-name var) 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 (if (consp x) (list 'or mem (car (cdr x))) mem)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 kwords)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 (append
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 (and (not (eq other-keys t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 (list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 (list 'let '((cl-keys-temp cl-keys))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 (list 'while 'cl-keys-temp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 (list 'or (list 'memq '(car cl-keys-temp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 (list 'quote
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 (mapcar
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 (lambda (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 (if (consp x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 (car x) x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 (append kwords
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 other-keys))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 '(car (cdr (memq (quote :allow-other-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 (car cl-keys-temp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 '(setq cl-keys-temp (cdr (cdr cl-keys-temp)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 body))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 (put 'cl-parsing-keywords 'lisp-indent-function 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 (put 'cl-parsing-keywords 'edebug-form-spec '(sexp sexp &rest form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 (defmacro cl-check-key (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 (list 'if 'cl-key (list 'funcall 'cl-key x) x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 (defmacro cl-check-test-nokey (item x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 (list 'cond
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 (list 'cl-test
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 (list 'eq (list 'not (list 'funcall 'cl-test item x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 'cl-test-not))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 (list 'cl-if
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 (list 't (list 'if (list 'numberp item)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 (list 'equal item x) (list 'eq item x)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 (defmacro cl-check-test (item x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 (list 'cl-check-test-nokey item (list 'cl-check-key x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 (defmacro cl-check-match (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 (setq x (list 'cl-check-key x) y (list 'cl-check-key y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 (list 'if 'cl-test
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 (list 'if (list 'numberp x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 (list 'equal x y) (list 'eq x y))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 (put 'cl-check-key 'edebug-form-spec 'edebug-forms)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 (put 'cl-check-test 'edebug-form-spec 'edebug-forms)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 (put 'cl-check-test-nokey 'edebug-form-spec 'edebug-forms)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 (put 'cl-check-match 'edebug-form-spec 'edebug-forms)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 (defvar cl-test) (defvar cl-test-not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 (defvar cl-if) (defvar cl-if-not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 (defvar cl-key)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 (setq cl-seq (subseq cl-seq cl-start cl-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 (if cl-from-end (setq cl-seq (nreverse cl-seq)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
163 (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
164 (cl-seq (cl-check-key (pop cl-seq)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 (t (funcall cl-func)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 (if cl-from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 (while cl-seq
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
168 (setq cl-accum (funcall cl-func (cl-check-key (pop cl-seq))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 cl-accum)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 (while cl-seq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 (setq cl-accum (funcall cl-func cl-accum
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
172 (cl-check-key (pop cl-seq))))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 cl-accum)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 (defun replace (cl-seq1 cl-seq2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 "Replace the elements of SEQ1 with the elements of SEQ2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 (or (= cl-start1 cl-start2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 (let* ((cl-len (length cl-seq1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 (cl-n (min (- (or cl-end1 cl-len) cl-start1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 (- (or cl-end2 cl-len) cl-start2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 (while (>= (setq cl-n (1- cl-n)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 (cl-set-elt cl-seq1 (+ cl-start1 cl-n)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 (elt cl-seq2 (+ cl-start2 cl-n))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 (if (listp cl-seq1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 (let ((cl-p1 (nthcdr cl-start1 cl-seq1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 (if (listp cl-seq2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 (let ((cl-p2 (nthcdr cl-start2 cl-seq2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 (cl-n (min cl-n1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 (if cl-end2 (- cl-end2 cl-start2) 4000000))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 (setcar cl-p1 (car cl-p2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 (setq cl-end2 (min (or cl-end2 (length cl-seq2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 (+ cl-start2 cl-n1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 (while (and cl-p1 (< cl-start2 cl-end2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 (setcar cl-p1 (aref cl-seq2 cl-start2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 (setq cl-end1 (min (or cl-end1 (length cl-seq1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 (+ cl-start1 (- (or cl-end2 (length cl-seq2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 cl-start2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 (if (listp cl-seq2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 (let ((cl-p2 (nthcdr cl-start2 cl-seq2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 (while (< cl-start1 cl-end1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 (aset cl-seq1 cl-start1 (car cl-p2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 (while (< cl-start1 cl-end1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 cl-seq1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 (defun remove* (cl-item cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 "Remove all occurrences of ITEM in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 This is a non-destructive function; it makes a copy of SEQ if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 (:start 0) :end) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 (if (<= (or cl-count (setq cl-count 8000000)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 cl-seq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 cl-from-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 (if cl-i
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 (let ((cl-res (apply 'delete* cl-item (append cl-seq nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 (append (if cl-from-end
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
245 (list :end (1+ cl-i))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
246 (list :start cl-i))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 cl-keys))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 (if (listp cl-seq) cl-res
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 (setq cl-end (- (or cl-end 8000000) cl-start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 (if (= cl-start 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 (while (and cl-seq (> cl-end 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 (cl-check-test cl-item (car cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 (> (setq cl-count (1- cl-count)) 0))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 (if (and (> cl-count 0) (> cl-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 (setq cl-end (1- cl-end)) (cdr cl-seq))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 (while (and cl-p (> cl-end 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 (not (cl-check-test cl-item (car cl-p))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 (setq cl-p (cdr cl-p) cl-end (1- cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 (if (and cl-p (> cl-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 (nconc (ldiff cl-seq cl-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 (if (= cl-count 1) (cdr cl-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 (and (cdr cl-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 (apply 'delete* cl-item
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 (copy-sequence (cdr cl-p))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
269 :start 0 :end (1- cl-end)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
270 :count (1- cl-count) cl-keys))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 cl-seq)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 (defun remove-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 "Remove all items satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 This is a non-destructive function; it makes a copy of SEQ if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
280 (apply 'remove* nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 (defun remove-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 "Remove all items not satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 This is a non-destructive function; it makes a copy of SEQ if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
288 (apply 'remove* nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 (defun delete* (cl-item cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 "Remove all occurrences of ITEM in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 (:start 0) :end) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 (if (<= (or cl-count (setq cl-count 8000000)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 cl-seq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 (if (listp cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 (if (and cl-from-end (< cl-count 4000000))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 (let (cl-i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 (while (and (>= (setq cl-count (1- cl-count)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 (setq cl-i (cl-position cl-item cl-seq cl-start
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 cl-end cl-from-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 (if (= cl-i 0) (setq cl-seq (cdr cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 (let ((cl-tail (nthcdr (1- cl-i) cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 (setcdr cl-tail (cdr (cdr cl-tail)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 (setq cl-end cl-i))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 (setq cl-end (- (or cl-end 8000000) cl-start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 (if (= cl-start 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 (while (and cl-seq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 (> cl-end 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 (cl-check-test cl-item (car cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 (> (setq cl-count (1- cl-count)) 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 (setq cl-end (1- cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 (setq cl-start (1- cl-start)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 (if (and (> cl-count 0) (> cl-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 (let ((cl-p (nthcdr cl-start cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 (while (and (cdr cl-p) (> cl-end 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 (if (cl-check-test cl-item (car (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 (setcdr cl-p (cdr (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 (if (= (setq cl-count (1- cl-count)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 (setq cl-end 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 (setq cl-p (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 (setq cl-end (1- cl-end)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 (apply 'remove* cl-item cl-seq cl-keys)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 (defun delete-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 "Remove all items satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
338 (apply 'delete* nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 (defun delete-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 "Remove all items not satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
345 (apply 'delete* nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 (defun remove (cl-item cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 "Remove all occurrences of ITEM in SEQ, testing with `equal'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 This is a non-destructive function; it makes a copy of SEQ if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 to avoid corrupting the original SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 Also see: `remove*', `delete', `delete*'"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 (remove* cl-item cl-seq ':test 'equal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 (defun remq (cl-elt cl-list)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 428
diff changeset
357 "Remove all occurrences of ELT in LIST, comparing with `eq'.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 This is a non-destructive function; it makes a copy of LIST to avoid
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 corrupting the original LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 Also see: `delq', `delete', `delete*', `remove', `remove*'."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 (if (memq cl-elt cl-list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 (delq cl-elt (copy-list cl-list))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 cl-list))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 (defun remove-duplicates (cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 (cl-delete-duplicates cl-seq cl-keys t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 (defun delete-duplicates (cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 (cl-delete-duplicates cl-seq cl-keys nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 (defun cl-delete-duplicates (cl-seq cl-keys cl-copy)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 (if (listp cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 (if cl-from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 (let ((cl-p (nthcdr cl-start cl-seq)) cl-i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 (while (> cl-end 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 (setq cl-i 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 (while (setq cl-i (cl-position (cl-check-key (car cl-p))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 (cdr cl-p) cl-i (1- cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 cl-p (nthcdr cl-start cl-seq) cl-copy nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 (let ((cl-tail (nthcdr cl-i cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 (setcdr cl-tail (cdr (cdr cl-tail))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 (setq cl-end (1- cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 (setq cl-p (cdr cl-p) cl-end (1- cl-end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 cl-start (1+ cl-start)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 (cl-position (cl-check-key (car cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 (cdr cl-seq) 0 (1- cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 (setq cl-seq (cdr cl-seq) cl-end (1- cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 (setq cl-end (1- cl-end) cl-start 1) cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 (while (and (cdr (cdr cl-p)) (> cl-end 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 (if (cl-position (cl-check-key (car (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 (cdr (cdr cl-p)) 0 (1- cl-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 cl-p (nthcdr (1- cl-start) cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 cl-copy nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 (setcdr cl-p (cdr (cdr cl-p))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 (setq cl-p (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 (setq cl-end (1- cl-end) cl-start (1+ cl-start)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 (defun substitute (cl-new cl-old cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 "Substitute NEW for OLD in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 This is a non-destructive function; it makes a copy of SEQ if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 (:start 0) :end :from-end) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 (if (or (eq cl-old cl-new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 cl-seq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 (if (not cl-i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 cl-seq
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 (setq cl-seq (copy-sequence cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 (or cl-from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 (progn (cl-set-elt cl-seq cl-i cl-new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 (setq cl-i (1+ cl-i) cl-count (1- cl-count))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
435 (apply 'nsubstitute cl-new cl-old cl-seq :count cl-count
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
436 :start cl-i cl-keys))))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 "Substitute NEW for all items satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 This is a non-destructive function; it makes a copy of SEQ if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
443 (apply 'substitute cl-new nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 This is a non-destructive function; it makes a copy of SEQ if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
450 (apply 'substitute cl-new nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 "Substitute NEW for OLD in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 (:start 0) :end :from-end) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 (let ((cl-p (nthcdr cl-start cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 (setq cl-end (- (or cl-end 8000000) cl-start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 (while (and cl-p (> cl-end 0) (> cl-count 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 (if (cl-check-test cl-old (car cl-p))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 (setcar cl-p cl-new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 (setq cl-count (1- cl-count))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 (setq cl-p (cdr cl-p) cl-end (1- cl-end))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 (or cl-end (setq cl-end (length cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 (if cl-from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 (while (and (< cl-start cl-end) (> cl-count 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 (setq cl-end (1- cl-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 (if (cl-check-test cl-old (elt cl-seq cl-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 (cl-set-elt cl-seq cl-end cl-new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 (setq cl-count (1- cl-count)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 (while (and (< cl-start cl-end) (> cl-count 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 (if (cl-check-test cl-old (aref cl-seq cl-start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 (aset cl-seq cl-start cl-new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 (setq cl-count (1- cl-count))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 (setq cl-start (1+ cl-start))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 cl-seq))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 "Substitute NEW for all items satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
490 (apply 'nsubstitute cl-new nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
497 (apply 'nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 (defun find (cl-item cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 "Find the first occurrence of ITEM in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 (let ((cl-pos (apply 'position cl-item cl-seq cl-keys)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 (and cl-pos (elt cl-seq cl-pos))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 (defun find-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 "Find the first item satisfying PREDICATE in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
512 (apply 'find nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 (defun find-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 "Find the first item not satisfying PREDICATE in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
519 (apply 'find nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 (defun position (cl-item cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 "Find the first occurrence of ITEM in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 (cl-parsing-keywords (:test :test-not :key :if :if-not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 (:start 0) :end :from-end) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 (cl-position cl-item cl-seq cl-start cl-end cl-from-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 (if (listp cl-seq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 (let ((cl-p (nthcdr cl-start cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 (or cl-end (setq cl-end 8000000))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 (let ((cl-res nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 (if (cl-check-test cl-item (car cl-p))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 (setq cl-res cl-start))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 (setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 cl-res))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 (or cl-end (setq cl-end (length cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 (if cl-from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 (while (and (>= (setq cl-end (1- cl-end)) cl-start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 (not (cl-check-test cl-item (aref cl-seq cl-end)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 (and (>= cl-end cl-start) cl-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 (while (and (< cl-start cl-end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 (not (cl-check-test cl-item (aref cl-seq cl-start))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 (setq cl-start (1+ cl-start)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 (and (< cl-start cl-end) cl-start))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 (defun position-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 "Find the first item satisfying PREDICATE in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
556 (apply 'position nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 (defun position-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 "Find the first item not satisfying PREDICATE in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
563 (apply 'position nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 (defun count (cl-item cl-seq &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 (let ((cl-count 0) cl-x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 (or cl-end (setq cl-end (length cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 (while (< cl-start cl-end)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
574 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 (setq cl-start (1+ cl-start)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 cl-count)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 (defun count-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
583 (apply 'count nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 (defun count-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
589 (apply 'count nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 "Compare SEQ1 with SEQ2, return index of first mismatching element.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 Return nil if the sequences match. If one sequence is a prefix of the
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 (cl-parsing-keywords (:test :test-not :key :from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 (:start1 0) :end1 (:start2 0) :end2) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 (or cl-end1 (setq cl-end1 (length cl-seq1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 (or cl-end2 (setq cl-end2 (length cl-seq2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 (if cl-from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 (cl-check-match (elt cl-seq1 (1- cl-end1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 (elt cl-seq2 (1- cl-end2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 (1- cl-end1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 (cl-check-match (if cl-p1 (car cl-p1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 (aref cl-seq1 cl-start1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 (if cl-p2 (car cl-p2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 (aref cl-seq2 cl-start2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 cl-start1)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 (defun search (cl-seq1 cl-seq2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 "Search for SEQ1 as a subsequence of SEQ2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 Return the index of the leftmost element of the first match found;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 (cl-parsing-keywords (:test :test-not :key :from-end
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 (:start1 0) :end1 (:start2 0) :end2) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 (or cl-end1 (setq cl-end1 (length cl-seq1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 (or cl-end2 (setq cl-end2 (length cl-seq2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 (if (>= cl-start1 cl-end1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 (if cl-from-end cl-end2 cl-start2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 (let* ((cl-len (- cl-end1 cl-start1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 (cl-first (cl-check-key (elt cl-seq1 cl-start1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 (cl-if nil) cl-pos)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 (setq cl-end2 (- cl-end2 (1- cl-len)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 (while (and (< cl-start2 cl-end2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 (setq cl-pos (cl-position cl-first cl-seq2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 cl-start2 cl-end2 cl-from-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 (apply 'mismatch cl-seq1 cl-seq2
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
643 :start1 (1+ cl-start1) :end1 cl-end1
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
644 :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
645 :from-end nil cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647 (and (< cl-start2 cl-end2) cl-pos)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649 (defun stable-sort (cl-seq cl-pred &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 "Sort the argument SEQUENCE stably according to PREDICATE.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
660 ;;; See compiler macro in cl-macs.el
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
661 (defun member* (cl-item cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 "Find the first occurrence of ITEM in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 (if cl-keys
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678 (while (and cl-list (not (cl-check-test cl-item (car cl-list))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 (setq cl-list (cdr cl-list)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 (member cl-item cl-list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 (memq cl-item cl-list))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 (defun member-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 "Find the first item satisfying PREDICATE in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
690 (apply 'member* nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 (defun member-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693 "Find the first item not satisfying PREDICATE in LIST.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
697 (apply 'member* nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 (defun cl-adjoin (cl-item cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700 (if (cl-parsing-keywords (:key) t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 (apply 'member* (cl-check-key cl-item) cl-list cl-keys))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 cl-list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
703 (cons cl-item cl-list)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705 ;;; See compiler macro in cl-macs.el
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706 (defun assoc* (cl-item cl-alist &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 (if cl-keys
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712 (while (and cl-alist
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 (or (not (consp (car cl-alist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714 (not (cl-check-test cl-item (car (car cl-alist))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715 (setq cl-alist (cdr cl-alist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718 (assoc cl-item cl-alist)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 (assq cl-item cl-alist))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
721 (defun assoc-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
725 (apply 'assoc* nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 (defun assoc-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
731 (apply 'assoc* nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
732
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 (defun rassoc* (cl-item cl-alist &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
739 (while (and cl-alist
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 (or (not (consp (car cl-alist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 (not (cl-check-test cl-item (cdr (car cl-alist))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742 (setq cl-alist (cdr cl-alist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 (and cl-alist (car cl-alist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 (rassq cl-item cl-alist)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746 (defun rassoc-if (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
750 (apply 'rassoc* nil cl-list :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 (defun rassoc-if-not (cl-pred cl-list &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
756 (apply 'rassoc* nil cl-list :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758 (defun union (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 "Combine LIST1 and LIST2 using a set-union operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 The result list contains all items that appear in either LIST1 or LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 This is a non-destructive function; it makes a copy of the data if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 ((equal cl-list1 cl-list2) cl-list1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776 (or (>= (length cl-list1) (length cl-list2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 (while cl-list2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 (if (or cl-keys (numberp (car cl-list2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 (or (memq (car cl-list2) cl-list1)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
782 (push (car cl-list2) cl-list1)))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
783 (pop cl-list2))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784 cl-list1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 (defun nunion (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787 "Combine LIST1 and LIST2 using a set-union operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788 The result list contains all items that appear in either LIST1 or LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 This is a destructive function; it reuses the storage of LIST1 and LIST2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
793 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794 (t (apply 'union cl-list1 cl-list2 cl-keys))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 (defun intersection (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831 "Combine LIST1 and LIST2 using a set-intersection operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 The result list contains all items that appear in both LIST1 and LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 This is a non-destructive function; it makes a copy of the data if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837 (and cl-list1 cl-list2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 (if (equal cl-list1 cl-list2) cl-list1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839 (cl-parsing-keywords (:key) (:test :test-not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 (let ((cl-res nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841 (or (>= (length cl-list1) (length cl-list2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 (while cl-list2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
844 (if (if (or cl-keys (numberp (car cl-list2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 (apply 'member* (cl-check-key (car cl-list2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846 cl-list1 cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847 (memq (car cl-list2) cl-list1))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
848 (push (car cl-list2) cl-res))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
849 (pop cl-list2))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 cl-res)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
852 (defun nintersection (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 "Combine LIST1 and LIST2 using a set-intersection operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854 The result list contains all items that appear in both LIST1 and LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855 This is a destructive function; it reuses the storage of LIST1 and LIST2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859 (and cl-list1 cl-list2 (apply 'intersection cl-list1 cl-list2 cl-keys)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890 (defun set-difference (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891 "Combine LIST1 and LIST2 using a set-difference operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
892 The result list contains all items that appear in LIST1 but not LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 This is a non-destructive function; it makes a copy of the data if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897 (if (or (null cl-list1) (null cl-list2)) cl-list1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 (cl-parsing-keywords (:key) (:test :test-not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 (let ((cl-res nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900 (while cl-list1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 (or (if (or cl-keys (numberp (car cl-list1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
902 (apply 'member* (cl-check-key (car cl-list1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
903 cl-list2 cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
904 (memq (car cl-list1) cl-list2))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
905 (push (car cl-list1) cl-res))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
906 (pop cl-list1))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
907 cl-res))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
908
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
909 (defun nset-difference (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
910 "Combine LIST1 and LIST2 using a set-difference operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
911 The result list contains all items that appear in LIST1 but not LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
912 This is a destructive function; it reuses the storage of LIST1 and LIST2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
916 (if (or (null cl-list1) (null cl-list2)) cl-list1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
917 (apply 'set-difference cl-list1 cl-list2 cl-keys)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
918
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
919 (defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
920 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
921 The result list contains all items that appear in exactly one of LIST1, LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
922 This is a non-destructive function; it makes a copy of the data if necessary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
926 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
927 ((equal cl-list1 cl-list2) nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
928 (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
929 (apply 'set-difference cl-list2 cl-list1 cl-keys)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
930
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
931 (defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
932 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
933 The result list contains all items that appear in exactly one of LIST1, LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
934 This is a destructive function; it reuses the storage of LIST1 and LIST2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
938 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
939 ((equal cl-list1 cl-list2) nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
940 (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
941 (apply 'nset-difference cl-list2 cl-list1 cl-keys)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
942
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
943 (defun subsetp (cl-list1 cl-list2 &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
944 "True if LIST1 is a subset of LIST2.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
948 (cond ((null cl-list1) t) ((null cl-list2) nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
949 ((equal cl-list1 cl-list2) t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
950 (t (cl-parsing-keywords (:key) (:test :test-not)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
951 (while (and cl-list1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
952 (apply 'member* (cl-check-key (car cl-list1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
953 cl-list2 cl-keys))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
954 (pop cl-list1))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
955 (null cl-list1)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
956
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
957 (defun subst-if (cl-new cl-pred cl-tree &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
958 "Substitute NEW for elements matching PREDICATE in TREE (non-destructively).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
962 (apply 'sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
963
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
964 (defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
965 "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
969 (apply 'sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
970
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
971 (defun nsubst (cl-new cl-old cl-tree &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
972 "Substitute NEW for OLD everywhere in TREE (destructively).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
973 Any element of TREE which is `eql' to OLD is changed to NEW (via a call
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
977 (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
978
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
979 (defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
980 "Substitute NEW for elements matching PREDICATE in TREE (destructively).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
984 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
985
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
986 (defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
987 "Substitute NEW for elements not matching PREDICATE in TREE (destructively).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
991 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
992
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
993 (defun sublis (cl-alist cl-tree &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
994 "Perform substitutions indicated by ALIST in TREE (non-destructively).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
998 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
999 (cl-sublis-rec cl-tree)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1000
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1001 (defvar cl-alist)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1002 (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1003 (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1004 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1005 (setq cl-p (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1006 (if cl-p (cdr (car cl-p))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1007 (if (consp cl-tree)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1008 (let ((cl-a (cl-sublis-rec (car cl-tree)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1009 (cl-d (cl-sublis-rec (cdr cl-tree))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1010 (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1011 cl-tree
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1012 (cons cl-a cl-d)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1013 cl-tree))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1014
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1015 (defun nsublis (cl-alist cl-tree &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1016 "Perform substitutions indicated by ALIST in TREE (destructively).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1020 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1021 (let ((cl-hold (list cl-tree)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1022 (cl-nsublis-rec cl-hold)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1023 (car cl-hold))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1024
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1025 (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1026 (while (consp cl-tree)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1027 (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1028 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1029 (setq cl-p (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1030 (if cl-p (setcar cl-tree (cdr (car cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1031 (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1032 (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1033 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1034 (setq cl-p (cdr cl-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1035 (if cl-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1036 (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1037 (setq cl-tree (cdr cl-tree))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1038
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1039 (defun tree-equal (cl-x cl-y &rest cl-keys)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1040 "Return t if trees X and Y have `eql' leaves.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1044 (cl-parsing-keywords (:test :test-not :key) ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1045 (cl-tree-equal-rec cl-x cl-y)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1046
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1047 (defun cl-tree-equal-rec (cl-x cl-y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1048 (while (and (consp cl-x) (consp cl-y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1049 (cl-tree-equal-rec (car cl-x) (car cl-y)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1050 (setq cl-x (cdr cl-x) cl-y (cdr cl-y)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1051 (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1052
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1053
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1054 (run-hooks 'cl-seq-load-hook)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1055
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 613
diff changeset
1056 ;;; arch-tag: ec1cc072-9006-4225-b6ba-d6b07ed1710c
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1057 ;;; cl-seq.el ends here