annotate lisp/emacs-lisp/cl-seq.el @ 11933:9b53ce243465

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