annotate lisp/emacs-lisp/cl-seq.el @ 33018:d9abb0441663

*** empty log message ***
author Dave Love <fx@gnu.org>
date Sat, 28 Oct 2000 21:30:03 +0000
parents add63b27c709
children 073685934f00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16057
a74507d555ba Turn on byte-compile-dynamic.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
1 ;;; cl-seq.el --- Common Lisp features, part 3 -*-byte-compile-dynamic: t;-*-
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; Version: 2.02
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; Keywords: extensions
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
12244
ac7375e60931 Update GPL to version 2.
Karl Heuer <kwzh@gnu.org>
parents: 7942
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; any later version.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 13337
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 13337
diff changeset
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 13337
diff changeset
24 ;; Boston, MA 02111-1307, USA.
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25
7942
bc5dccc5375f Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 4355
diff changeset
26 ;;; Commentary:
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;; These are extensions to Emacs Lisp that provide a degree of
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; Common Lisp compatibility, beyond what is already built-in
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 ;; in Emacs Lisp.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;; This package was written by Dave Gillespie; it is a complete
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 ;;
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 ;;
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;; Bug reports, comments, and suggestions are welcome!
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 ;; This file contains the Common Lisp sequence and list functions
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;; which take keyword arguments.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;; See cl.el for Change Log.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44
7942
bc5dccc5375f Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 4355
diff changeset
45 ;;; Code:
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 (or (memq 'cl-19 features)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 (error "Tried to load `cl-seq' before `cl'!"))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;;; We define these here so that this file can compile without having
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;;; loaded the cl.el file already.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 (defmacro cl-push (x place) (list 'setq place (list 'cons x place)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 (defmacro cl-pop (place)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 ;;; Keyword parsing. This is special-cased here so that we can compile
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 ;;; this file independent from cl-macs.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 (defmacro cl-parsing-keywords (kwords other-keys &rest body)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 (cons
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 'let*
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 (cons (mapcar
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 (function
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 (lambda (x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 (let* ((var (if (consp x) (car x) x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 (mem (list 'car (list 'cdr (list 'memq (list 'quote var)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 'cl-keys)))))
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
71 (if (eq var :test-not)
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 (setq mem (list 'and mem (list 'setq 'cl-test mem) t)))
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
73 (if (eq var :if-not)
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 (setq mem (list 'and mem (list 'setq 'cl-if mem) t)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 (list (intern
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 (format "cl-%s" (substring (symbol-name var) 1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 (if (consp x) (list 'or mem (car (cdr x))) mem)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 kwords)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 (append
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 (and (not (eq other-keys t))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 (list
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 (list 'let '((cl-keys-temp cl-keys))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 (list 'while 'cl-keys-temp
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 (list 'or (list 'memq '(car cl-keys-temp)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 (list 'quote
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 (mapcar
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 (function
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 (lambda (x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 (if (consp x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 (car x) x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 (append kwords
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 other-keys))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 '(car (cdr (memq (quote :allow-other-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 cl-keys)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 '(error "Bad keyword argument %s"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 (car cl-keys-temp)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 '(setq cl-keys-temp (cdr (cdr cl-keys-temp)))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 body))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 (put 'cl-parsing-keywords 'lisp-indent-function 2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 (put 'cl-parsing-keywords 'edebug-form-spec '(sexp sexp &rest form))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 (defmacro cl-check-key (x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 (list 'if 'cl-key (list 'funcall 'cl-key x) x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 (defmacro cl-check-test-nokey (item x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 (list 'cond
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 (list 'cl-test
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 (list 'eq (list 'not (list 'funcall 'cl-test item x))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 'cl-test-not))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 (list 'cl-if
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 (list 't (list 'if (list 'numberp item)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 (list 'equal item x) (list 'eq item x)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 (defmacro cl-check-test (item x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (list 'cl-check-test-nokey item (list 'cl-check-key x)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 (defmacro cl-check-match (x y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 (setq x (list 'cl-check-key x) y (list 'cl-check-key y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 (list 'if 'cl-test
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 (list 'if (list 'numberp x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (list 'equal x y) (list 'eq x y))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (put 'cl-check-key 'edebug-form-spec 'edebug-forms)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 (put 'cl-check-test 'edebug-form-spec 'edebug-forms)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 (put 'cl-check-test-nokey 'edebug-form-spec 'edebug-forms)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 (put 'cl-check-match 'edebug-form-spec 'edebug-forms)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 (defvar cl-test) (defvar cl-test-not)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 (defvar cl-if) (defvar cl-if-not)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 (defvar cl-key)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 (defun reduce (cl-func cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 "Reduce two-argument FUNCTION across SEQUENCE.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 Keywords supported: :start :end :from-end :initial-value :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 (setq cl-seq (subseq cl-seq cl-start cl-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 (if cl-from-end (setq cl-seq (nreverse cl-seq)))
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
142 (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (cl-seq (cl-check-key (cl-pop cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 (t (funcall cl-func)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (if cl-from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 (while cl-seq
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (setq cl-accum (funcall cl-func (cl-check-key (cl-pop cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 cl-accum)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (while cl-seq
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 (setq cl-accum (funcall cl-func cl-accum
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (cl-check-key (cl-pop cl-seq))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 cl-accum)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 (defun fill (seq item &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 "Fill the elements of SEQ with ITEM.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 Keywords supported: :start :end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (cl-parsing-keywords ((:start 0) :end) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (if (listp seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 (let ((p (nthcdr cl-start seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (n (if cl-end (- cl-end cl-start) 8000000)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 (while (and p (>= (setq n (1- n)) 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 (setcar p item)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 (setq p (cdr p))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 (or cl-end (setq cl-end (length seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 (if (and (= cl-start 0) (= cl-end (length seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 (fillarray seq item)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (while (< cl-start cl-end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (aset seq cl-start item)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 (setq cl-start (1+ cl-start)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 (defun replace (cl-seq1 cl-seq2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 "Replace the elements of SEQ1 with the elements of SEQ2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 SEQ1 is destructively modified, then returned.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 Keywords supported: :start1 :end1 :start2 :end2"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 (or (= cl-start1 cl-start2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (let* ((cl-len (length cl-seq1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 (cl-n (min (- (or cl-end1 cl-len) cl-start1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 (- (or cl-end2 cl-len) cl-start2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 (while (>= (setq cl-n (1- cl-n)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 (cl-set-elt cl-seq1 (+ cl-start1 cl-n)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 (elt cl-seq2 (+ cl-start2 cl-n))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 (if (listp cl-seq1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 (let ((cl-p1 (nthcdr cl-start1 cl-seq1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (if (listp cl-seq2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (let ((cl-p2 (nthcdr cl-start2 cl-seq2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 (cl-n (min cl-n1
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (if cl-end2 (- cl-end2 cl-start2) 4000000))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 (setcar cl-p1 (car cl-p2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (setq cl-end2 (min (or cl-end2 (length cl-seq2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 (+ cl-start2 cl-n1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 (while (and cl-p1 (< cl-start2 cl-end2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (setcar cl-p1 (aref cl-seq2 cl-start2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (setq cl-end1 (min (or cl-end1 (length cl-seq1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (+ cl-start1 (- (or cl-end2 (length cl-seq2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 cl-start2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 (if (listp cl-seq2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 (let ((cl-p2 (nthcdr cl-start2 cl-seq2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 (while (< cl-start1 cl-end1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 (aset cl-seq1 cl-start1 (car cl-p2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 (while (< cl-start1 cl-end1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 cl-seq1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 (defun remove* (cl-item cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 "Remove all occurrences of ITEM in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 This is a non-destructive function; it makes a copy of SEQ if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 to avoid corrupting the original SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 Keywords supported: :test :test-not :key :count :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 (:start 0) :end) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 (if (<= (or cl-count (setq cl-count 8000000)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 cl-seq
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 cl-from-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (if cl-i
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (let ((cl-res (apply 'delete* cl-item (append cl-seq nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 (append (if cl-from-end
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
228 (list :end (1+ cl-i))
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
229 (list :start cl-i))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 cl-keys))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (if (listp cl-seq) cl-res
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 (setq cl-end (- (or cl-end 8000000) cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (if (= cl-start 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 (while (and cl-seq (> cl-end 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (cl-check-test cl-item (car cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (> (setq cl-count (1- cl-count)) 0))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 (if (and (> cl-count 0) (> cl-end 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 (setq cl-end (1- cl-end)) (cdr cl-seq))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 (while (and cl-p (> cl-end 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (not (cl-check-test cl-item (car cl-p))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 (setq cl-p (cdr cl-p) cl-end (1- cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 (if (and cl-p (> cl-end 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 (nconc (ldiff cl-seq cl-p)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 (if (= cl-count 1) (cdr cl-p)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 (and (cdr cl-p)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (apply 'delete* cl-item
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (copy-sequence (cdr cl-p))
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
252 :start 0 :end (1- cl-end)
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
253 :count (1- cl-count) cl-keys))))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 cl-seq)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 (defun remove-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 "Remove all items satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 This is a non-destructive function; it makes a copy of SEQ if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 to avoid corrupting the original SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
262 (apply 'remove* nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 (defun remove-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 "Remove all items not satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 This is a non-destructive function; it makes a copy of SEQ if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 to avoid corrupting the original SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
269 (apply 'remove* nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 (defun delete* (cl-item cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 "Remove all occurrences of ITEM in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 This is a destructive function; it reuses the storage of SEQ whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 Keywords supported: :test :test-not :key :count :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 (:start 0) :end) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 (if (<= (or cl-count (setq cl-count 8000000)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 cl-seq
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 (if (listp cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 (if (and cl-from-end (< cl-count 4000000))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 (let (cl-i)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (while (and (>= (setq cl-count (1- cl-count)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 (setq cl-i (cl-position cl-item cl-seq cl-start
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 cl-end cl-from-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (if (= cl-i 0) (setq cl-seq (cdr cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 (let ((cl-tail (nthcdr (1- cl-i) cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 (setcdr cl-tail (cdr (cdr cl-tail)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 (setq cl-end cl-i))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 (setq cl-end (- (or cl-end 8000000) cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 (if (= cl-start 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 (while (and cl-seq
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 (> cl-end 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 (cl-check-test cl-item (car cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 (> (setq cl-count (1- cl-count)) 0)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 (setq cl-end (1- cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 (setq cl-start (1- cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 (if (and (> cl-count 0) (> cl-end 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 (let ((cl-p (nthcdr cl-start cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 (while (and (cdr cl-p) (> cl-end 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 (if (cl-check-test cl-item (car (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 (setcdr cl-p (cdr (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (if (= (setq cl-count (1- cl-count)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 (setq cl-end 1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (setq cl-p (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 (setq cl-end (1- cl-end)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (apply 'remove* cl-item cl-seq cl-keys)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 (defun delete-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 "Remove all items satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 This is a destructive function; it reuses the storage of SEQ whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
317 (apply 'delete* nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 (defun delete-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 "Remove all items not satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 This is a destructive function; it reuses the storage of SEQ whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
323 (apply 'delete* nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
325 (defun remove (x y) (remove* x y :test 'equal))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (defun remq (x y) (if (memq x y) (delq x (copy-list y)) y))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 (defun remove-duplicates (cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 "Return a copy of SEQ with all duplicate elements removed.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 Keywords supported: :test :test-not :key :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 (cl-delete-duplicates cl-seq cl-keys t))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 (defun delete-duplicates (cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 "Remove all duplicate elements from SEQ (destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 Keywords supported: :test :test-not :key :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (cl-delete-duplicates cl-seq cl-keys nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (defun cl-delete-duplicates (cl-seq cl-keys cl-copy)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (if (listp cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (if cl-from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (let ((cl-p (nthcdr cl-start cl-seq)) cl-i)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (while (> cl-end 1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (setq cl-i 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (while (setq cl-i (cl-position (cl-check-key (car cl-p))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (cdr cl-p) cl-i (1- cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 cl-p (nthcdr cl-start cl-seq) cl-copy nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (let ((cl-tail (nthcdr cl-i cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 (setcdr cl-tail (cdr (cdr cl-tail))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (setq cl-end (1- cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 (setq cl-p (cdr cl-p) cl-end (1- cl-end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 cl-start (1+ cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 (cl-position (cl-check-key (car cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 (cdr cl-seq) 0 (1- cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (setq cl-seq (cdr cl-seq) cl-end (1- cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 (setq cl-end (1- cl-end) cl-start 1) cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 (while (and (cdr (cdr cl-p)) (> cl-end 1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (if (cl-position (cl-check-key (car (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 (cdr (cdr cl-p)) 0 (1- cl-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 cl-p (nthcdr (1- cl-start) cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 cl-copy nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 (setcdr cl-p (cdr (cdr cl-p))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 (setq cl-p (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 (setq cl-end (1- cl-end) cl-start (1+ cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 (defun substitute (cl-new cl-old cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 "Substitute NEW for OLD in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 This is a non-destructive function; it makes a copy of SEQ if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 to avoid corrupting the original SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 Keywords supported: :test :test-not :key :count :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 (:start 0) :end :from-end) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 (if (or (eq cl-old cl-new)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 cl-seq
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 (if (not cl-i)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 cl-seq
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 (setq cl-seq (copy-sequence cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 (or cl-from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (progn (cl-set-elt cl-seq cl-i cl-new)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 (setq cl-i (1+ cl-i) cl-count (1- cl-count))))
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
395 (apply 'nsubstitute cl-new cl-old cl-seq :count cl-count
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
396 :start cl-i cl-keys))))))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 "Substitute NEW for all items satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 This is a non-destructive function; it makes a copy of SEQ if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 to avoid corrupting the original SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
403 (apply 'substitute cl-new nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 This is a non-destructive function; it makes a copy of SEQ if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 to avoid corrupting the original SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
410 (apply 'substitute cl-new nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 "Substitute NEW for OLD in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 This is a destructive function; it reuses the storage of SEQ whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 Keywords supported: :test :test-not :key :count :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 (:start 0) :end :from-end) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 (let ((cl-p (nthcdr cl-start cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 (setq cl-end (- (or cl-end 8000000) cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 (while (and cl-p (> cl-end 0) (> cl-count 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 (if (cl-check-test cl-old (car cl-p))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 (setcar cl-p cl-new)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 (setq cl-count (1- cl-count))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 (setq cl-p (cdr cl-p) cl-end (1- cl-end))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 (or cl-end (setq cl-end (length cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 (if cl-from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 (while (and (< cl-start cl-end) (> cl-count 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 (setq cl-end (1- cl-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 (if (cl-check-test cl-old (elt cl-seq cl-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 (cl-set-elt cl-seq cl-end cl-new)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 (setq cl-count (1- cl-count)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 (while (and (< cl-start cl-end) (> cl-count 0))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 (if (cl-check-test cl-old (aref cl-seq cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 (aset cl-seq cl-start cl-new)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 (setq cl-count (1- cl-count))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441 (setq cl-start (1+ cl-start))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 cl-seq))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 "Substitute NEW for all items satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 This is a destructive function; it reuses the storage of SEQ whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
448 (apply 'nsubstitute cl-new nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 This is a destructive function; it reuses the storage of SEQ whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453 Keywords supported: :key :count :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
454 (apply 'nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 (defun find (cl-item cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 "Find the first occurrence of ITEM in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458 Return the matching ITEM, or nil if not found.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 Keywords supported: :test :test-not :key :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 (let ((cl-pos (apply 'position cl-item cl-seq cl-keys)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 (and cl-pos (elt cl-seq cl-pos))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 (defun find-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 "Find the first item satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465 Return the matching ITEM, or nil if not found.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 Keywords supported: :key :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
467 (apply 'find nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
468
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 (defun find-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 "Find the first item not satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 Return the matching ITEM, or nil if not found.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
472 Keywords supported: :key :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
473 (apply 'find nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
474
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
475 (defun position (cl-item cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
476 "Find the first occurrence of ITEM in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
477 Return the index of the matching item, or nil if not found.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
478 Keywords supported: :test :test-not :key :start :end :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
479 (cl-parsing-keywords (:test :test-not :key :if :if-not
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
480 (:start 0) :end :from-end) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
481 (cl-position cl-item cl-seq cl-start cl-end cl-from-end)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
482
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
483 (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
484 (if (listp cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
485 (let ((cl-p (nthcdr cl-start cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
486 (or cl-end (setq cl-end 8000000))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
487 (let ((cl-res nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
488 (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
489 (if (cl-check-test cl-item (car cl-p))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
490 (setq cl-res cl-start))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
491 (setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
492 cl-res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
493 (or cl-end (setq cl-end (length cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
494 (if cl-from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
495 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
496 (while (and (>= (setq cl-end (1- cl-end)) cl-start)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
497 (not (cl-check-test cl-item (aref cl-seq cl-end)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
498 (and (>= cl-end cl-start) cl-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
499 (while (and (< cl-start cl-end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
500 (not (cl-check-test cl-item (aref cl-seq cl-start))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
501 (setq cl-start (1+ cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
502 (and (< cl-start cl-end) cl-start))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
503
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
504 (defun position-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
505 "Find the first item satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
506 Return the index of the matching item, or nil if not found.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
507 Keywords supported: :key :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
508 (apply 'position nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
509
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
510 (defun position-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
511 "Find the first item not satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
512 Return the index of the matching item, or nil if not found.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
513 Keywords supported: :key :start :end :from-end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
514 (apply 'position nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
515
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
516 (defun count (cl-item cl-seq &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
517 "Count the number of occurrences of ITEM in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
518 Keywords supported: :test :test-not :key :start :end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
519 (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
520 (let ((cl-count 0) cl-x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
521 (or cl-end (setq cl-end (length cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
522 (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
523 (while (< cl-start cl-end)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
524 (setq cl-x (if (consp cl-seq) (cl-pop cl-seq) (aref cl-seq cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
525 (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
526 (setq cl-start (1+ cl-start)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
527 cl-count)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
528
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
529 (defun count-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530 "Count the number of items satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531 Keywords supported: :key :start :end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
532 (apply 'count nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
533
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
534 (defun count-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
535 "Count the number of items not satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
536 Keywords supported: :key :start :end"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
537 (apply 'count nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
538
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
539 (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
540 "Compare SEQ1 with SEQ2, return index of first mismatching element.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541 Return nil if the sequences match. If one sequence is a prefix of the
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
542 other, the return value indicates the end of the shorted sequence.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543 Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
544 (cl-parsing-keywords (:test :test-not :key :from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
545 (:start1 0) :end1 (:start2 0) :end2) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
546 (or cl-end1 (setq cl-end1 (length cl-seq1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
547 (or cl-end2 (setq cl-end2 (length cl-seq2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
548 (if cl-from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
549 (progn
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
550 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
551 (cl-check-match (elt cl-seq1 (1- cl-end1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
552 (elt cl-seq2 (1- cl-end2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555 (1- cl-end1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
556 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
557 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
558 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
559 (cl-check-match (if cl-p1 (car cl-p1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
560 (aref cl-seq1 cl-start1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
561 (if cl-p2 (car cl-p2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
562 (aref cl-seq2 cl-start2))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
563 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
564 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
565 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
566 cl-start1)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
568 (defun search (cl-seq1 cl-seq2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569 "Search for SEQ1 as a subsequence of SEQ2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
570 Return the index of the leftmost element of the first match found;
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
571 return nil if there are no matches.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
572 Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
573 (cl-parsing-keywords (:test :test-not :key :from-end
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
574 (:start1 0) :end1 (:start2 0) :end2) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
575 (or cl-end1 (setq cl-end1 (length cl-seq1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
576 (or cl-end2 (setq cl-end2 (length cl-seq2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
577 (if (>= cl-start1 cl-end1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
578 (if cl-from-end cl-end2 cl-start2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
579 (let* ((cl-len (- cl-end1 cl-start1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
580 (cl-first (cl-check-key (elt cl-seq1 cl-start1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
581 (cl-if nil) cl-pos)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
582 (setq cl-end2 (- cl-end2 (1- cl-len)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
583 (while (and (< cl-start2 cl-end2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
584 (setq cl-pos (cl-position cl-first cl-seq2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
585 cl-start2 cl-end2 cl-from-end))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
586 (apply 'mismatch cl-seq1 cl-seq2
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
587 :start1 (1+ cl-start1) :end1 cl-end1
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
588 :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len)
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
589 :from-end nil cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
590 (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
591 (and (< cl-start2 cl-end2) cl-pos)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
593 (defun sort* (cl-seq cl-pred &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
594 "Sort the argument SEQUENCE according to PREDICATE.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
595 This is a destructive function; it reuses the storage of SEQUENCE if possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
596 Keywords supported: :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 (if (nlistp cl-seq)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 (replace cl-seq (apply 'sort* (append cl-seq nil) cl-pred cl-keys))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599 (cl-parsing-keywords (:key) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600 (if (memq cl-key '(nil identity))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
601 (sort cl-seq cl-pred)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
602 (sort cl-seq (function (lambda (cl-x cl-y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
603 (funcall cl-pred (funcall cl-key cl-x)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
604 (funcall cl-key cl-y)))))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
605
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
606 (defun stable-sort (cl-seq cl-pred &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
607 "Sort the argument SEQUENCE stably according to PREDICATE.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
608 This is a destructive function; it reuses the storage of SEQUENCE if possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
609 Keywords supported: :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
610 (apply 'sort* cl-seq cl-pred cl-keys))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
611
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
612 (defun merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
613 "Destructively merge the two sequences to produce a new sequence.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
614 TYPE is the sequence type to return, SEQ1 and SEQ2 are the two
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
615 argument sequences, and PRED is a `less-than' predicate on the elements.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
616 Keywords supported: :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
617 (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
618 (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
619 (cl-parsing-keywords (:key) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
620 (let ((cl-res nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
621 (while (and cl-seq1 cl-seq2)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
622 (if (funcall cl-pred (cl-check-key (car cl-seq2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
623 (cl-check-key (car cl-seq1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
624 (cl-push (cl-pop cl-seq2) cl-res)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
625 (cl-push (cl-pop cl-seq1) cl-res)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
626 (coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
627
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
628 ;;; See compiler macro in cl-macs.el
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
629 (defun member* (cl-item cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
630 "Find the first occurrence of ITEM in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
631 Return the sublist of LIST whose car is ITEM.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
632 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
633 (if cl-keys
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
634 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
635 (while (and cl-list (not (cl-check-test cl-item (car cl-list))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
636 (setq cl-list (cdr cl-list)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
637 cl-list)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
638 (if (and (numberp cl-item) (not (integerp cl-item)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
639 (member cl-item cl-list)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
640 (memq cl-item cl-list))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
641
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
642 (defun member-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
643 "Find the first item satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
644 Return the sublist of LIST whose car matches.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
645 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
646 (apply 'member* nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
647
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
648 (defun member-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
649 "Find the first item not satisfying PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
650 Return the sublist of LIST whose car matches.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
651 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
652 (apply 'member* nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
653
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
654 (defun cl-adjoin (cl-item cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
655 (if (cl-parsing-keywords (:key) t
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
656 (apply 'member* (cl-check-key cl-item) cl-list cl-keys))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
657 cl-list
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
658 (cons cl-item cl-list)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
659
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
660 ;;; See compiler macro in cl-macs.el
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
661 (defun assoc* (cl-item cl-alist &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
662 "Find the first item whose car matches ITEM in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
663 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
664 (if cl-keys
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
665 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
666 (while (and cl-alist
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
667 (or (not (consp (car cl-alist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
668 (not (cl-check-test cl-item (car (car cl-alist))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
669 (setq cl-alist (cdr cl-alist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
670 (and cl-alist (car cl-alist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
671 (if (and (numberp cl-item) (not (integerp cl-item)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
672 (assoc cl-item cl-alist)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
673 (assq cl-item cl-alist))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
674
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
675 (defun assoc-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
676 "Find the first item whose car satisfies PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
677 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
678 (apply 'assoc* nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
679
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
680 (defun assoc-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
681 "Find the first item whose car does not satisfy PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
682 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
683 (apply 'assoc* nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
684
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
685 (defun rassoc* (cl-item cl-alist &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
686 "Find the first item whose cdr matches ITEM in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
687 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
688 (if (or cl-keys (numberp cl-item))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
689 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
690 (while (and cl-alist
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
691 (or (not (consp (car cl-alist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
692 (not (cl-check-test cl-item (cdr (car cl-alist))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
693 (setq cl-alist (cdr cl-alist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
694 (and cl-alist (car cl-alist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
695 (rassq cl-item cl-alist)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
696
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
697 (defun rassoc-if (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
698 "Find the first item whose cdr satisfies PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
699 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
700 (apply 'rassoc* nil cl-list :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
701
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
702 (defun rassoc-if-not (cl-pred cl-list &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
703 "Find the first item whose cdr does not satisfy PREDICATE in LIST.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
704 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
705 (apply 'rassoc* nil cl-list :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
706
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
707 (defun union (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
708 "Combine LIST1 and LIST2 using a set-union operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
709 The result list contains all items that appear in either LIST1 or LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
710 This is a non-destructive function; it makes a copy of the data if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
711 to avoid corrupting the original LIST1 and LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
712 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
713 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
714 ((equal cl-list1 cl-list2) cl-list1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
715 (t
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
716 (or (>= (length cl-list1) (length cl-list2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
717 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
718 (while cl-list2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
719 (if (or cl-keys (numberp (car cl-list2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
720 (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
721 (or (memq (car cl-list2) cl-list1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
722 (cl-push (car cl-list2) cl-list1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
723 (cl-pop cl-list2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
724 cl-list1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
725
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
726 (defun nunion (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
727 "Combine LIST1 and LIST2 using a set-union operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
728 The result list contains all items that appear in either LIST1 or LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
729 This is a destructive function; it reuses the storage of LIST1 and LIST2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
730 whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
731 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
732 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
733 (t (apply 'union cl-list1 cl-list2 cl-keys))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
734
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
735 (defun intersection (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
736 "Combine LIST1 and LIST2 using a set-intersection operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
737 The result list contains all items that appear in both LIST1 and LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
738 This is a non-destructive function; it makes a copy of the data if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
739 to avoid corrupting the original LIST1 and LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
740 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
741 (and cl-list1 cl-list2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
742 (if (equal cl-list1 cl-list2) cl-list1
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
743 (cl-parsing-keywords (:key) (:test :test-not)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
744 (let ((cl-res nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
745 (or (>= (length cl-list1) (length cl-list2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
746 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
747 (while cl-list2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
748 (if (if (or cl-keys (numberp (car cl-list2)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
749 (apply 'member* (cl-check-key (car cl-list2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
750 cl-list1 cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
751 (memq (car cl-list2) cl-list1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
752 (cl-push (car cl-list2) cl-res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
753 (cl-pop cl-list2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
754 cl-res)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
755
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
756 (defun nintersection (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
757 "Combine LIST1 and LIST2 using a set-intersection operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
758 The result list contains all items that appear in both LIST1 and LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
759 This is a destructive function; it reuses the storage of LIST1 and LIST2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
760 whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
761 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
762 (and cl-list1 cl-list2 (apply 'intersection cl-list1 cl-list2 cl-keys)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
763
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
764 (defun set-difference (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
765 "Combine LIST1 and LIST2 using a set-difference operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
766 The result list contains all items that appear in LIST1 but not LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
767 This is a non-destructive function; it makes a copy of the data if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
768 to avoid corrupting the original LIST1 and LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
769 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
770 (if (or (null cl-list1) (null cl-list2)) cl-list1
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
771 (cl-parsing-keywords (:key) (:test :test-not)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
772 (let ((cl-res nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
773 (while cl-list1
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
774 (or (if (or cl-keys (numberp (car cl-list1)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
775 (apply 'member* (cl-check-key (car cl-list1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
776 cl-list2 cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
777 (memq (car cl-list1) cl-list2))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
778 (cl-push (car cl-list1) cl-res))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
779 (cl-pop cl-list1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
780 cl-res))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
781
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
782 (defun nset-difference (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
783 "Combine LIST1 and LIST2 using a set-difference operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
784 The result list contains all items that appear in LIST1 but not LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
785 This is a destructive function; it reuses the storage of LIST1 and LIST2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
786 whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
787 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
788 (if (or (null cl-list1) (null cl-list2)) cl-list1
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
789 (apply 'set-difference cl-list1 cl-list2 cl-keys)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
790
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
791 (defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
792 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
793 The result list contains all items that appear in exactly one of LIST1, LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
794 This is a non-destructive function; it makes a copy of the data if necessary
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
795 to avoid corrupting the original LIST1 and LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
796 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
797 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
798 ((equal cl-list1 cl-list2) nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
799 (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
800 (apply 'set-difference cl-list2 cl-list1 cl-keys)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
801
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
802 (defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
803 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
804 The result list contains all items that appear in exactly one of LIST1, LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
805 This is a destructive function; it reuses the storage of LIST1 and LIST2
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
806 whenever possible.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
807 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
808 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
809 ((equal cl-list1 cl-list2) nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
810 (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
811 (apply 'nset-difference cl-list2 cl-list1 cl-keys)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
812
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
813 (defun subsetp (cl-list1 cl-list2 &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
814 "True if LIST1 is a subset of LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
815 I.e., if every element of LIST1 also appears in LIST2.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
816 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
817 (cond ((null cl-list1) t) ((null cl-list2) nil)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
818 ((equal cl-list1 cl-list2) t)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
819 (t (cl-parsing-keywords (:key) (:test :test-not)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
820 (while (and cl-list1
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
821 (apply 'member* (cl-check-key (car cl-list1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
822 cl-list2 cl-keys))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
823 (cl-pop cl-list1))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
824 (null cl-list1)))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
825
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
826 (defun subst-if (cl-new cl-pred cl-tree &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
827 "Substitute NEW for elements matching PREDICATE in TREE (non-destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
828 Return a copy of TREE with all matching elements replaced by NEW.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
829 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
830 (apply 'sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
831
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
832 (defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
833 "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
834 Return a copy of TREE with all non-matching elements replaced by NEW.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
835 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
836 (apply 'sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
837
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
838 (defun nsubst (cl-new cl-old cl-tree &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
839 "Substitute NEW for OLD everywhere in TREE (destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
840 Any element of TREE which is `eql' to OLD is changed to NEW (via a call
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
841 to `setcar').
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
842 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
843 (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
844
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
845 (defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
846 "Substitute NEW for elements matching PREDICATE in TREE (destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
847 Any element of TREE which matches is changed to NEW (via a call to `setcar').
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
848 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
849 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
850
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
851 (defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
852 "Substitute NEW for elements not matching PREDICATE in TREE (destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
853 Any element of TREE which matches is changed to NEW (via a call to `setcar').
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
854 Keywords supported: :key"
28824
add63b27c709 Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents: 16057
diff changeset
855 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
4355
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
856
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
857 (defun sublis (cl-alist cl-tree &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
858 "Perform substitutions indicated by ALIST in TREE (non-destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
859 Return a copy of TREE with all matching elements replaced.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
860 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
861 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
862 (cl-sublis-rec cl-tree)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
863
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
864 (defvar cl-alist)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
865 (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if*
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
866 (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
867 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
868 (setq cl-p (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
869 (if cl-p (cdr (car cl-p))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
870 (if (consp cl-tree)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
871 (let ((cl-a (cl-sublis-rec (car cl-tree)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
872 (cl-d (cl-sublis-rec (cdr cl-tree))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
873 (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
874 cl-tree
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
875 (cons cl-a cl-d)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
876 cl-tree))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
877
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
878 (defun nsublis (cl-alist cl-tree &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
879 "Perform substitutions indicated by ALIST in TREE (destructively).
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
880 Any matching element of TREE is changed via a call to `setcar'.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
881 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
882 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
883 (let ((cl-hold (list cl-tree)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
884 (cl-nsublis-rec cl-hold)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
885 (car cl-hold))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
886
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
887 (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if*
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
888 (while (consp cl-tree)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
889 (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
890 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
891 (setq cl-p (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
892 (if cl-p (setcar cl-tree (cdr (car cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
893 (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
894 (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
895 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
896 (setq cl-p (cdr cl-p)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
897 (if cl-p
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
898 (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
899 (setq cl-tree (cdr cl-tree))))))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
900
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
901 (defun tree-equal (cl-x cl-y &rest cl-keys)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
902 "T if trees X and Y have `eql' leaves.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
903 Atoms are compared by `eql'; cons cells are compared recursively.
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
904 Keywords supported: :test :test-not :key"
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
905 (cl-parsing-keywords (:test :test-not :key) ()
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
906 (cl-tree-equal-rec cl-x cl-y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
907
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
908 (defun cl-tree-equal-rec (cl-x cl-y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
909 (while (and (consp cl-x) (consp cl-y)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
910 (cl-tree-equal-rec (car cl-x) (car cl-y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
911 (setq cl-x (cdr cl-x) cl-y (cdr cl-y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
912 (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y)))
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
913
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
914
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
915 (run-hooks 'cl-seq-load-hook)
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
916
691a28818487 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
917 ;;; cl-seq.el ends here