annotate lisp/cl-seq.el @ 5442:8d3d05ffbb43

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