0.6.11.17:
[sbcl.git] / src / code / seq.lisp
1 ;;;; generic SEQUENCEs
2 ;;;;
3 ;;;; KLUDGE: comment from original CMU CL source:
4 ;;;;   Be careful when modifying code. A lot of the structure of the
5 ;;;;   code is affected by the fact that compiler transforms use the
6 ;;;;   lower level support functions. If transforms are written for
7 ;;;;   some sequence operation, note how the END argument is handled
8 ;;;;   in other operations with transforms.
9
10 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; more information.
12 ;;;;
13 ;;;; This software is derived from the CMU CL system, which was
14 ;;;; written at Carnegie Mellon University and released into the
15 ;;;; public domain. The software is in the public domain and is
16 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
17 ;;;; files for more information.
18
19 (in-package "SB!IMPL")
20 \f
21 ;;;; utilities
22
23 (eval-when (:compile-toplevel)
24
25 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
26 ;;;
27 ;;; FIXME: It might be worth making three cases here, LIST,
28 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
29 ;;; It tend to make code run faster but be bigger; some benchmarking
30 ;;; is needed to decide.
31 (sb!xc:defmacro seq-dispatch (sequence list-form array-form)
32   `(if (listp ,sequence)
33        ,list-form
34        ,array-form))
35
36 (sb!xc:defmacro make-sequence-like (sequence length)
37   #!+sb-doc
38   "Returns a sequence of the same type as SEQUENCE and the given LENGTH."
39   `(make-sequence-of-type (type-of ,sequence) ,length))
40
41 (sb!xc:defmacro type-specifier-atom (type)
42   #!+sb-doc "Returns the broad class of which TYPE is a specific subclass."
43   `(if (atom ,type) ,type (car ,type)))
44
45 ) ; EVAL-WHEN
46
47 ;;; It's possible with some sequence operations to declare the length
48 ;;; of a result vector, and to be safe, we really ought to verify that
49 ;;; the actual result has the declared length.
50 (defun vector-of-checked-length-given-length (vector declared-length)
51   (declare (type vector vector))
52   (declare (type index declared-length))
53   (let ((actual-length (length vector)))
54     (unless (= actual-length declared-length)
55       (error 'simple-type-error
56              :datum vector
57              :expected-type `(vector ,declared-length)
58              :format-control
59              "Vector length (~D) doesn't match declared length (~D)."
60              :format-arguments (list actual-length declared-length))))
61   vector)
62 (defun sequence-of-checked-length-given-type (sequence result-type)
63   (let ((ctype (specifier-type result-type)))
64     (if (not (array-type-p ctype))
65         sequence
66         (let ((declared-length (first (array-type-dimensions ctype))))
67           (if (eq declared-length '*)
68               sequence
69               (vector-of-checked-length-given-length sequence
70                                                      declared-length))))))
71
72 ;;; Given an arbitrary type specifier, return a sane sequence type
73 ;;; specifier that we can directly match.
74 (defun result-type-or-lose (type &optional nil-ok)
75   (let ((type (specifier-type type)))
76     (cond
77       ((eq type *empty-type*)
78        (if nil-ok
79            nil
80            (error 'simple-type-error
81                   :datum type
82                   :expected-type '(or vector cons)
83                   :format-control
84                   "A NIL output type is invalid for this sequence function."
85                   :format-arguments ())))
86       ((dolist (seq-type '(list string simple-vector bit-vector))
87          (when (csubtypep type (specifier-type seq-type))
88            (return seq-type))))
89       ((csubtypep type (specifier-type 'vector))
90        (type-specifier type))
91       (t
92        (error 'simple-type-error
93               :datum type
94               :expected-type 'sequence
95               :format-control
96               "~S is not a legal type specifier for sequence functions."
97               :format-arguments (list type))))))
98
99 (defun signal-index-too-large-error (sequence index)
100   (let* ((length (length sequence))
101          (max-index (and (plusp length) (1- length))))
102     (error 'index-too-large-error
103            :datum index
104            :expected-type (if max-index
105                               `(integer 0 ,max-index)
106                               ;; This seems silly, is there something better?
107                               '(integer (0) (0))))))
108
109 (defun make-sequence-of-type (type length)
110   #!+sb-doc "Returns a sequence of the given TYPE and LENGTH."
111   (declare (fixnum length))
112   (case (type-specifier-atom type)
113     (list (make-list length))
114     ((bit-vector simple-bit-vector) (make-array length :element-type '(mod 2)))
115     ((string simple-string base-string simple-base-string)
116      (make-string length))
117     (simple-vector (make-array length))
118     ((array simple-array vector)
119      (if (listp type)
120          (make-array length :element-type (cadr type))
121          (make-array length)))
122     (t
123      (make-sequence-of-type (result-type-or-lose type) length))))
124 \f
125 (defun elt (sequence index)
126   #!+sb-doc "Returns the element of SEQUENCE specified by INDEX."
127   (etypecase sequence
128     (list
129      (do ((count index (1- count))
130           (list sequence (cdr list)))
131          ((= count 0)
132           (if (endp list)
133               (signal-index-too-large-error sequence index)
134               (car list)))
135        (declare (type (integer 0) count))))
136     (vector
137      (when (>= index (length sequence))
138        (signal-index-too-large-error sequence index))
139      (aref sequence index))))
140
141 (defun %setelt (sequence index newval)
142   #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
143   (etypecase sequence
144     (list
145      (do ((count index (1- count))
146           (seq sequence))
147          ((= count 0) (rplaca seq newval) newval)
148        (declare (fixnum count))
149        (if (atom (cdr seq))
150            (signal-index-too-large-error sequence index)
151            (setq seq (cdr seq)))))
152     (vector
153      (when (>= index (length sequence))
154        (signal-index-too-large-error sequence index))
155      (setf (aref sequence index) newval))))
156
157 (defun length (sequence)
158   #!+sb-doc "Return an integer that is the length of SEQUENCE."
159   (etypecase sequence
160     (vector (length (truly-the vector sequence)))
161     (list (length (truly-the list sequence)))))
162
163 (defun make-sequence (type length &key (initial-element NIL iep))
164   #!+sb-doc
165   "Return a sequence of the given TYPE and LENGTH, with elements initialized
166   to :INITIAL-ELEMENT."
167   (declare (fixnum length))
168   (let ((type (specifier-type type)))
169     (cond ((csubtypep type (specifier-type 'list))
170            (make-list length :initial-element initial-element))
171           ((csubtypep type (specifier-type 'string))
172            (if iep
173                (make-string length :initial-element initial-element)
174                (make-string length)))
175           ((csubtypep type (specifier-type 'simple-vector))
176            (make-array length :initial-element initial-element))
177           ((csubtypep type (specifier-type 'bit-vector))
178            (if iep
179                (make-array length :element-type '(mod 2)
180                            :initial-element initial-element)
181                (make-array length :element-type '(mod 2))))
182           ((csubtypep type (specifier-type 'vector))
183            (if (typep type 'array-type)
184                (let ((etype (type-specifier
185                              (array-type-specialized-element-type type)))
186                      (vlen (car (array-type-dimensions type))))
187                  (if (and (numberp vlen) (/= vlen length))
188                    (error 'simple-type-error
189                           ;; These two are under-specified by ANSI.
190                           :datum (type-specifier type)
191                           :expected-type (type-specifier type)
192                           :format-control
193                           "The length of ~S does not match the specified ~
194                            length=~S."
195                           :format-arguments
196                           (list (type-specifier type) length)))
197                  (if iep
198                      (make-array length :element-type etype
199                                  :initial-element initial-element)
200                      (make-array length :element-type etype)))
201                (make-array length :initial-element initial-element)))
202           (t (error 'simple-type-error
203                     :datum type
204                     :expected-type 'sequence
205                     :format-control "~S is a bad type specifier for sequences."
206                     :format-arguments (list type))))))
207 \f
208 ;;;; SUBSEQ
209 ;;;;
210 ;;;; The support routines for SUBSEQ are used by compiler transforms, so we
211 ;;;; worry about dealing with END being supplied or defaulting to NIL
212 ;;;; at this level.
213
214 (defun vector-subseq* (sequence start &optional end)
215   (declare (type vector sequence))
216   (declare (type fixnum start))
217   (declare (type (or null fixnum) end))
218   (when (null end) (setf end (length sequence)))
219   (do ((old-index start (1+ old-index))
220        (new-index 0 (1+ new-index))
221        (copy (make-sequence-like sequence (- end start))))
222       ((= old-index end) copy)
223     (declare (fixnum old-index new-index))
224     (setf (aref copy new-index) (aref sequence old-index))))
225
226 (defun list-subseq* (sequence start &optional end)
227   (declare (type list sequence))
228   (declare (type fixnum start))
229   (declare (type (or null fixnum) end))
230   (if (and end (>= start (the fixnum end)))
231       ()
232       (let* ((groveled (nthcdr start sequence))
233              (result (list (car groveled))))
234         (if groveled
235             (do ((list (cdr groveled) (cdr list))
236                  (splice result (cdr (rplacd splice (list (car list)))))
237                  (index (1+ start) (1+ index)))
238                 ((or (atom list) (and end (= index (the fixnum end))))
239                  result)
240               (declare (fixnum index)))
241             ()))))
242
243 ;;; SUBSEQ cannot default end to the length of sequence since it is not
244 ;;; an error to supply nil for its value. We must test for end being nil
245 ;;; in the body of the function, and this is actually done in the support
246 ;;; routines for other reasons (see above).
247 (defun subseq (sequence start &optional end)
248   #!+sb-doc
249   "Returns a copy of a subsequence of SEQUENCE starting with element number
250    START and continuing to the end of SEQUENCE or the optional END."
251   (seq-dispatch sequence
252                 (list-subseq* sequence start end)
253                 (vector-subseq* sequence start end)))
254 \f
255 ;;;; COPY-SEQ
256
257 (eval-when (:compile-toplevel :execute)
258
259 (sb!xc:defmacro vector-copy-seq (sequence type)
260   `(let ((length (length (the vector ,sequence))))
261      (declare (fixnum length))
262      (do ((index 0 (1+ index))
263           (copy (make-sequence-of-type ,type length)))
264          ((= index length) copy)
265        (declare (fixnum index))
266        (setf (aref copy index) (aref ,sequence index)))))
267
268 (sb!xc:defmacro list-copy-seq (list)
269   `(if (atom ,list) '()
270        (let ((result (cons (car ,list) '()) ))
271          (do ((x (cdr ,list) (cdr x))
272               (splice result
273                       (cdr (rplacd splice (cons (car x) '() ))) ))
274              ((atom x) (unless (null x)
275                                (rplacd splice x))
276                        result)))))
277
278 ) ; EVAL-WHEN
279
280 (defun copy-seq (sequence)
281   #!+sb-doc "Returns a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
282   (seq-dispatch sequence
283                 (list-copy-seq* sequence)
284                 (vector-copy-seq* sequence)))
285
286 ;;; internal frobs
287
288 (defun list-copy-seq* (sequence)
289   (list-copy-seq sequence))
290
291 (defun vector-copy-seq* (sequence)
292   (vector-copy-seq sequence (type-of sequence)))
293 \f
294 ;;;; FILL
295
296 (eval-when (:compile-toplevel :execute)
297
298 (sb!xc:defmacro vector-fill (sequence item start end)
299   `(do ((index ,start (1+ index)))
300        ((= index (the fixnum ,end)) ,sequence)
301      (declare (fixnum index))
302      (setf (aref ,sequence index) ,item)))
303
304 (sb!xc:defmacro list-fill (sequence item start end)
305   `(do ((current (nthcdr ,start ,sequence) (cdr current))
306         (index ,start (1+ index)))
307        ((or (atom current) (and end (= index (the fixnum ,end))))
308         sequence)
309      (declare (fixnum index))
310      (rplaca current ,item)))
311
312 ) ; EVAL-WHEN
313
314 ;;; The support routines for FILL are used by compiler transforms, so we
315 ;;; worry about dealing with END being supplied or defaulting to NIL
316 ;;; at this level.
317
318 (defun list-fill* (sequence item start end)
319   (declare (list sequence))
320   (list-fill sequence item start end))
321
322 (defun vector-fill* (sequence item start end)
323   (declare (vector sequence))
324   (when (null end) (setq end (length sequence)))
325   (vector-fill sequence item start end))
326
327 ;;; FILL cannot default end to the length of sequence since it is not
328 ;;; an error to supply nil for its value. We must test for end being nil
329 ;;; in the body of the function, and this is actually done in the support
330 ;;; routines for other reasons (see above).
331 (defun fill (sequence item &key (start 0) end)
332   #!+sb-doc "Replace the specified elements of SEQUENCE with ITEM."
333   (seq-dispatch sequence
334                 (list-fill* sequence item start end)
335                 (vector-fill* sequence item start end)))
336 \f
337 ;;;; REPLACE
338
339 (eval-when (:compile-toplevel :execute)
340
341 ;;; If we are copying around in the same vector, be careful not to copy the
342 ;;; same elements over repeatedly. We do this by copying backwards.
343 (sb!xc:defmacro mumble-replace-from-mumble ()
344   `(if (and (eq target-sequence source-sequence) (> target-start source-start))
345        (let ((nelts (min (- target-end target-start)
346                          (- source-end source-start))))
347          (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
348                             (1- target-index))
349               (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
350                             (1- source-index)))
351              ((= target-index (the fixnum (1- target-start))) target-sequence)
352            (declare (fixnum target-index source-index))
353            (setf (aref target-sequence target-index)
354                  (aref source-sequence source-index))))
355        (do ((target-index target-start (1+ target-index))
356             (source-index source-start (1+ source-index)))
357            ((or (= target-index (the fixnum target-end))
358                 (= source-index (the fixnum source-end)))
359             target-sequence)
360          (declare (fixnum target-index source-index))
361          (setf (aref target-sequence target-index)
362                (aref source-sequence source-index)))))
363
364 (sb!xc:defmacro list-replace-from-list ()
365   `(if (and (eq target-sequence source-sequence) (> target-start source-start))
366        (let ((new-elts (subseq source-sequence source-start
367                                (+ (the fixnum source-start)
368                                   (the fixnum
369                                        (min (- (the fixnum target-end)
370                                                (the fixnum target-start))
371                                             (- (the fixnum source-end)
372                                                (the fixnum source-start))))))))
373          (do ((n new-elts (cdr n))
374               (o (nthcdr target-start target-sequence) (cdr o)))
375              ((null n) target-sequence)
376            (rplaca o (car n))))
377        (do ((target-index target-start (1+ target-index))
378             (source-index source-start (1+ source-index))
379             (target-sequence-ref (nthcdr target-start target-sequence)
380                                  (cdr target-sequence-ref))
381             (source-sequence-ref (nthcdr source-start source-sequence)
382                                  (cdr source-sequence-ref)))
383            ((or (= target-index (the fixnum target-end))
384                 (= source-index (the fixnum source-end))
385                 (null target-sequence-ref) (null source-sequence-ref))
386             target-sequence)
387          (declare (fixnum target-index source-index))
388          (rplaca target-sequence-ref (car source-sequence-ref)))))
389
390 (sb!xc:defmacro list-replace-from-mumble ()
391   `(do ((target-index target-start (1+ target-index))
392         (source-index source-start (1+ source-index))
393         (target-sequence-ref (nthcdr target-start target-sequence)
394                              (cdr target-sequence-ref)))
395        ((or (= target-index (the fixnum target-end))
396             (= source-index (the fixnum source-end))
397             (null target-sequence-ref))
398         target-sequence)
399      (declare (fixnum source-index target-index))
400      (rplaca target-sequence-ref (aref source-sequence source-index))))
401
402 (sb!xc:defmacro mumble-replace-from-list ()
403   `(do ((target-index target-start (1+ target-index))
404         (source-index source-start (1+ source-index))
405         (source-sequence (nthcdr source-start source-sequence)
406                          (cdr source-sequence)))
407        ((or (= target-index (the fixnum target-end))
408             (= source-index (the fixnum source-end))
409             (null source-sequence))
410         target-sequence)
411      (declare (fixnum target-index source-index))
412      (setf (aref target-sequence target-index) (car source-sequence))))
413
414 ) ; EVAL-WHEN
415
416 ;;;; The support routines for REPLACE are used by compiler transforms, so we
417 ;;;; worry about dealing with END being supplied or defaulting to NIL
418 ;;;; at this level.
419
420 (defun list-replace-from-list* (target-sequence source-sequence target-start
421                                 target-end source-start source-end)
422   (when (null target-end) (setq target-end (length target-sequence)))
423   (when (null source-end) (setq source-end (length source-sequence)))
424   (list-replace-from-list))
425
426 (defun list-replace-from-vector* (target-sequence source-sequence target-start
427                                   target-end source-start source-end)
428   (when (null target-end) (setq target-end (length target-sequence)))
429   (when (null source-end) (setq source-end (length source-sequence)))
430   (list-replace-from-mumble))
431
432 (defun vector-replace-from-list* (target-sequence source-sequence target-start
433                                   target-end source-start source-end)
434   (when (null target-end) (setq target-end (length target-sequence)))
435   (when (null source-end) (setq source-end (length source-sequence)))
436   (mumble-replace-from-list))
437
438 (defun vector-replace-from-vector* (target-sequence source-sequence
439                                     target-start target-end source-start
440                                     source-end)
441   (when (null target-end) (setq target-end (length target-sequence)))
442   (when (null source-end) (setq source-end (length source-sequence)))
443   (mumble-replace-from-mumble))
444
445 ;;; REPLACE cannot default END arguments to the length of SEQUENCE since it
446 ;;; is not an error to supply NIL for their values. We must test for ENDs
447 ;;; being NIL in the body of the function.
448 (defun replace (target-sequence source-sequence &key
449                 ((:start1 target-start) 0)
450                 ((:end1 target-end))
451                 ((:start2 source-start) 0)
452                 ((:end2 source-end)))
453   #!+sb-doc
454   "The target sequence is destructively modified by copying successive
455    elements into it from the source sequence."
456   (let ((target-end (or target-end (length target-sequence)))
457         (source-end (or source-end (length source-sequence))))
458     (seq-dispatch target-sequence
459                   (seq-dispatch source-sequence
460                                 (list-replace-from-list)
461                                 (list-replace-from-mumble))
462                   (seq-dispatch source-sequence
463                                 (mumble-replace-from-list)
464                                 (mumble-replace-from-mumble)))))
465 \f
466 ;;;; REVERSE
467
468 (eval-when (:compile-toplevel :execute)
469
470 (sb!xc:defmacro vector-reverse (sequence type)
471   `(let ((length (length ,sequence)))
472      (declare (fixnum length))
473      (do ((forward-index 0 (1+ forward-index))
474           (backward-index (1- length) (1- backward-index))
475           (new-sequence (make-sequence-of-type ,type length)))
476          ((= forward-index length) new-sequence)
477        (declare (fixnum forward-index backward-index))
478        (setf (aref new-sequence forward-index)
479              (aref ,sequence backward-index)))))
480
481 (sb!xc:defmacro list-reverse-macro (sequence)
482   `(do ((new-list ()))
483        ((atom ,sequence) new-list)
484      (push (pop ,sequence) new-list)))
485
486 ) ; EVAL-WHEN
487
488 (defun reverse (sequence)
489   #!+sb-doc
490   "Returns a new sequence containing the same elements but in reverse order."
491   (seq-dispatch sequence
492                 (list-reverse* sequence)
493                 (vector-reverse* sequence)))
494
495 ;;; internal frobs
496
497 (defun list-reverse* (sequence)
498   (list-reverse-macro sequence))
499
500 (defun vector-reverse* (sequence)
501   (vector-reverse sequence (type-of sequence)))
502 \f
503 ;;;; NREVERSE
504
505 (eval-when (:compile-toplevel :execute)
506
507 (sb!xc:defmacro vector-nreverse (sequence)
508   `(let ((length (length (the vector ,sequence))))
509      (declare (fixnum length))
510      (do ((left-index 0 (1+ left-index))
511           (right-index (1- length) (1- right-index))
512           (half-length (truncate length 2)))
513          ((= left-index half-length) ,sequence)
514        (declare (fixnum left-index right-index half-length))
515        (rotatef (aref ,sequence left-index)
516                 (aref ,sequence right-index)))))
517
518 (sb!xc:defmacro list-nreverse-macro (list)
519   `(do ((1st (cdr ,list) (if (atom 1st) 1st (cdr 1st)))
520         (2nd ,list 1st)
521         (3rd '() 2nd))
522        ((atom 2nd) 3rd)
523      (rplacd 2nd 3rd)))
524
525 ) ; EVAL-WHEN
526
527 (defun list-nreverse* (sequence)
528   (list-nreverse-macro sequence))
529
530 (defun vector-nreverse* (sequence)
531   (vector-nreverse sequence))
532
533 (defun nreverse (sequence)
534   #!+sb-doc
535   "Returns a sequence of the same elements in reverse order; the argument
536    is destroyed."
537   (seq-dispatch sequence
538                 (list-nreverse* sequence)
539                 (vector-nreverse* sequence)))
540 \f
541 ;;;; CONCATENATE
542
543 (eval-when (:compile-toplevel :execute)
544
545 (sb!xc:defmacro concatenate-to-list (sequences)
546   `(let ((result (list nil)))
547      (do ((sequences ,sequences (cdr sequences))
548           (splice result))
549          ((null sequences) (cdr result))
550        (let ((sequence (car sequences)))
551          ;; FIXME: It appears to me that this and CONCATENATE-TO-MUMBLE
552          ;; could benefit from a DO-SEQUENCE macro.
553          (seq-dispatch sequence
554                        (do ((sequence sequence (cdr sequence)))
555                            ((atom sequence))
556                          (setq splice
557                                (cdr (rplacd splice (list (car sequence))))))
558                        (do ((index 0 (1+ index))
559                             (length (length sequence)))
560                            ((= index length))
561                          (declare (fixnum index length))
562                          (setq splice
563                                (cdr (rplacd splice
564                                             (list (aref sequence index)))))))))))
565
566 (sb!xc:defmacro concatenate-to-mumble (output-type-spec sequences)
567   `(do ((seqs ,sequences (cdr seqs))
568         (total-length 0)
569         (lengths ()))
570        ((null seqs)
571         (do ((sequences ,sequences (cdr sequences))
572              (lengths lengths (cdr lengths))
573              (index 0)
574              (result (make-sequence-of-type ,output-type-spec total-length)))
575             ((= index total-length) result)
576           (declare (fixnum index))
577           (let ((sequence (car sequences)))
578             (seq-dispatch sequence
579                           (do ((sequence sequence (cdr sequence)))
580                               ((atom sequence))
581                             (setf (aref result index) (car sequence))
582                             (setq index (1+ index)))
583                           (do ((jndex 0 (1+ jndex))
584                                (this-length (car lengths)))
585                               ((= jndex this-length))
586                             (declare (fixnum jndex this-length))
587                             (setf (aref result index)
588                                   (aref sequence jndex))
589                             (setq index (1+ index)))))))
590      (let ((length (length (car seqs))))
591        (declare (fixnum length))
592        (setq lengths (nconc lengths (list length)))
593        (setq total-length (+ total-length length)))))
594
595 ) ; EVAL-WHEN
596 \f
597 ;;; FIXME: Make a compiler macro or transform for this which efficiently
598 ;;; handles the case of constant 'STRING first argument. (It's not just time
599 ;;; efficiency, but space efficiency..)
600 (defun concatenate (output-type-spec &rest sequences)
601   #!+sb-doc
602   "Returns a new sequence of all the argument sequences concatenated together
603   which shares no structure with the original argument sequences of the
604   specified OUTPUT-TYPE-SPEC."
605   (case (type-specifier-atom output-type-spec)
606     ((simple-vector simple-string vector string array simple-array
607                     bit-vector simple-bit-vector base-string
608                     simple-base-string) ; FIXME: unifying principle here?
609      (let ((result (apply #'concat-to-simple* output-type-spec sequences)))
610        #!+high-security
611        (check-type-var result output-type-spec)
612        result))
613     (list (apply #'concat-to-list* sequences))
614     (t
615      (apply #'concatenate (result-type-or-lose output-type-spec) sequences))))
616
617 ;;; internal frobs
618 ;;; FIXME: These are weird. They're never called anywhere except in
619 ;;; CONCATENATE. It seems to me that the macros ought to just
620 ;;; be expanded directly in CONCATENATE, or in CONCATENATE-STRING
621 ;;; and CONCATENATE-LIST variants. Failing that, these ought to be local
622 ;;; functions (FLET).
623 (defun concat-to-list* (&rest sequences)
624   (concatenate-to-list sequences))
625 (defun concat-to-simple* (type &rest sequences)
626   (concatenate-to-mumble type sequences))
627 \f
628 ;;;; MAP and MAP-INTO
629
630 ;;; helper functions to handle arity-1 subcases of MAP
631 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
632 (declaim (ftype (function (function sequence) simple-vector)
633                 %map-simple-vector-arity-1))
634 (macrolet ((dosequence ((i sequence) &body body)
635              (once-only ((sequence sequence))
636                `(etypecase ,sequence
637                   (list (dolist (,i ,sequence) ,@body))
638                   (simple-vector (dovector (,i sequence) ,@body))
639                   (vector (dovector (,i sequence) ,@body))))))
640   (defun %map-to-list-arity-1 (fun sequence)
641     (let ((reversed-result nil)
642           (really-fun (%coerce-callable-to-function fun)))
643       (dosequence (element sequence)
644         (push (funcall really-fun element)
645               reversed-result))
646       (nreverse reversed-result)))
647   (defun %map-to-simple-vector-arity-1 (fun sequence)
648     (let ((result (make-array (length sequence)))
649           (index 0)
650           (really-fun (%coerce-callable-to-function fun)))
651       (declare (type index index))
652       (dosequence (element sequence)
653         (setf (aref result index)
654               (funcall really-fun element))
655         (incf index))
656       result))
657   (defun %map-for-effect-arity-1 (fun sequence)
658     (let ((really-fun (%coerce-callable-to-function fun)))
659       (dosequence (element sequence)
660         (funcall really-fun element)))
661     nil))
662
663 ;;; helper functions to handle arity-N subcases of MAP
664 ;;;
665 ;;; KLUDGE: This is hairier, and larger, than need be, because we
666 ;;; don't have DYNAMIC-EXTENT. With DYNAMIC-EXTENT, we could define
667 ;;; %MAP-FOR-EFFECT, and then implement the
668 ;;; other %MAP-TO-FOO functions reasonably efficiently by passing closures to
669 ;;; %MAP-FOR-EFFECT. (DYNAMIC-EXTENT would help a little by avoiding
670 ;;; consing each closure, and would help a lot by allowing us to define
671 ;;; a closure (LAMBDA (&REST REST) <do something with (APPLY FUN REST)>)
672 ;;; with the REST list allocated with DYNAMIC-EXTENT. -- WHN 20000920
673 (macrolet (;; Execute BODY in a context where the machinery for
674            ;; UPDATED-MAP-APPLY-ARGS has been set up.
675            (with-map-state (sequences &body body)
676              `(let* ((%sequences ,sequences)
677                      (%iters (mapcar (lambda (sequence)
678                                        (etypecase sequence
679                                          (list sequence)
680                                          (vector 0)))
681                                      %sequences))
682                      (%apply-args (make-list (length %sequences))))
683                 (declare (type list %sequences %iters %apply-args))
684                 ,@body))
685            ;; Return a list of args to pass to APPLY for the next
686            ;; function call in the mapping, or NIL if no more function
687            ;; calls should be made (because we've reached the end of a
688            ;; sequence arg).
689            (updated-map-apply-args ()
690              '(do ((in-sequences  %sequences  (cdr in-sequences))
691                    (in-iters      %iters      (cdr in-iters))
692                    (in-apply-args %apply-args (cdr in-apply-args)))
693                   ((null in-sequences)
694                    %apply-args)
695                 (declare (type list in-sequences in-iters in-apply-args))
696                 (let ((i (car in-iters)))
697                   (declare (type (or list index) i))
698                   (if (listp i)
699                       (if (null i)      ; if end of this sequence
700                           (return nil)
701                           (setf (car in-apply-args) (car i)
702                                 (car in-iters) (cdr i)))
703                       (let ((v (the vector (car in-sequences))))
704                         (if (>= i (length v)) ; if end of this sequence
705                             (return nil)
706                             (setf (car in-apply-args) (aref v i)
707                                   (car in-iters) (1+ i)))))))))
708   (defun %map-to-list (func sequences)
709     (declare (type function func))
710     (declare (type list sequences))
711     (with-map-state sequences
712       (loop with updated-map-apply-args 
713             while (setf updated-map-apply-args (updated-map-apply-args))
714             collect (apply func updated-map-apply-args))))
715   (defun %map-to-vector (output-type-spec func sequences)
716     (declare (type function func))
717     (declare (type list sequences))
718     (let ((min-len (with-map-state sequences
719                      (do ((counter 0 (1+ counter)))
720                          ;; Note: Doing everything in
721                          ;; UPDATED-MAP-APPLY-ARGS here is somewhat
722                          ;; wasteful; we even do some extra consing.
723                          ;; And stepping over every element of
724                          ;; VECTORs, instead of just grabbing their
725                          ;; LENGTH, is also wasteful. But it's easy
726                          ;; and safe. (If you do rewrite it, please
727                          ;; try to make sure that
728                          ;;   (MAP NIL #'F SOME-CIRCULAR-LIST #(1))
729                          ;; does the right thing.)
730                          ((not (updated-map-apply-args))
731                           counter)
732                        (declare (type index counter))))))
733       (declare (type index min-len))
734       (with-map-state sequences
735         (let ((result (make-sequence-of-type output-type-spec min-len))
736               (index 0))
737           (declare (type index index))
738           (loop with updated-map-apply-args
739                 while (setf updated-map-apply-args (updated-map-apply-args))
740                 do
741                 (setf (aref result index)
742                       (apply func updated-map-apply-args))
743                 (incf index))
744           result))))
745   (defun %map-for-effect (func sequences)
746     (declare (type function func))
747     (declare (type list sequences))
748     (with-map-state sequences
749       (loop with updated-map-apply-args
750             while (setf updated-map-apply-args (updated-map-apply-args))
751             do
752             (apply func updated-map-apply-args))
753       nil)))
754
755   "FUNCTION must take as many arguments as there are sequences provided.  
756   The result is a sequence of type OUTPUT-TYPE-SPEC such that element I 
757   is the result of applying FUNCTION to element I of each of the argument
758   sequences."
759
760 ;;; %MAP is just MAP without the final just-to-be-sure check that
761 ;;; length of the output sequence matches any length specified
762 ;;; in RESULT-TYPE.
763 (defun %map (result-type function first-sequence &rest more-sequences)
764   (let ((really-function (%coerce-callable-to-function function)))
765     ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
766     ;; it into something which can be DEFTRANSFORMed away. (It's
767     ;; fairly important to handle this case efficiently, since
768     ;; quantifiers like SOME are transformed into this case, and since
769     ;; there's no consing overhead to dwarf our inefficiency.)
770     (if (and (null more-sequences)
771              (null result-type))
772         (%map-for-effect-arity-1 really-function first-sequence)
773         ;; Otherwise, use the industrial-strength full-generality
774         ;; approach, consing O(N-ARGS) temporary storage (which can have
775         ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
776         (let ((sequences (cons first-sequence more-sequences)))
777           (case (type-specifier-atom result-type)
778             ((nil) (%map-for-effect really-function sequences))
779             (list (%map-to-list really-function sequences))
780             ((simple-vector simple-string vector string array simple-array
781               bit-vector simple-bit-vector base-string simple-base-string)
782              (%map-to-vector result-type really-function sequences))
783             (t
784              (apply #'map
785                     (result-type-or-lose result-type t)
786                     really-function
787                     sequences)))))))
788
789 (defun map (result-type function first-sequence &rest more-sequences)
790   (sequence-of-checked-length-given-type (apply #'%map
791                                                 result-type
792                                                 function
793                                                 first-sequence
794                                                 more-sequences)
795                                          ;; (The RESULT-TYPE isn't
796                                          ;; strictly the type of the
797                                          ;; result, because when
798                                          ;; RESULT-TYPE=NIL, the result
799                                          ;; actually has NULL type. But
800                                          ;; that special case doesn't
801                                          ;; matter here, since we only
802                                          ;; look closely at vector
803                                          ;; types; so we can just pass
804                                          ;; RESULT-TYPE straight through
805                                          ;; as a type specifier.)
806                                          result-type))
807
808 ;;; KLUDGE: MAP has been rewritten substantially since the fork from
809 ;;; CMU CL in order to give reasonable performance, but this
810 ;;; implementation of MAP-INTO still has the same problems as the old
811 ;;; MAP code. Ideally, MAP-INTO should be rewritten to be efficient in
812 ;;; the same way that the corresponding cases of MAP have been
813 ;;; rewritten. Instead of doing it now, though, it's easier to wait
814 ;;; until we have DYNAMIC-EXTENT, at which time it should become
815 ;;; extremely easy to define a reasonably efficient MAP-INTO in terms
816 ;;; of (MAP NIL ..). -- WHN 20000920
817 (defun map-into (result-sequence function &rest sequences)
818   (let* ((fp-result
819           (and (arrayp result-sequence)
820                (array-has-fill-pointer-p result-sequence)))
821          (len (apply #'min
822                      (if fp-result
823                          (array-dimension result-sequence 0)
824                          (length result-sequence))
825                      (mapcar #'length sequences))))
826
827     (when fp-result
828       (setf (fill-pointer result-sequence) len))
829
830     (let ((really-fun (%coerce-callable-to-function function)))
831       (dotimes (index len)
832         (setf (elt result-sequence index)
833               (apply really-fun
834                      (mapcar #'(lambda (seq) (elt seq index))
835                              sequences))))))
836   result-sequence)
837 \f
838 ;;;; quantifiers
839
840 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
841 ;;; arbitrary sequence arguments, both in the full call case and in
842 ;;; the open code case.
843 (macrolet ((defquantifier (name found-test found-result
844                                 &key doc (unfound-result (not found-result)))
845              `(progn 
846                 ;; KLUDGE: It would be really nice if we could simply
847                 ;; do something like this
848                 ;;  (declaim (inline ,name))
849                 ;;  (defun ,name (pred first-seq &rest more-seqs)
850                 ;;    ,doc
851                 ;;    (flet ((map-me (&rest rest)
852                 ;;             (let ((pred-value (apply pred rest)))
853                 ;;               (,found-test pred-value
854                 ;;                 (return-from ,name
855                 ;;                   ,found-result)))))
856                 ;;      (declare (inline map-me))
857                 ;;      (apply #'map nil #'map-me first-seq more-seqs)
858                 ;;      ,unfound-result))
859                 ;; but Python doesn't seem to be smart enough about
860                 ;; inlining and APPLY to recognize that it can use
861                 ;; the DEFTRANSFORM for MAP in the resulting inline
862                 ;; expansion. I don't have any appetite for deep
863                 ;; compiler hacking right now, so I'll just work
864                 ;; around the apparent problem by using a compiler
865                 ;; macro instead. -- WHN 20000410
866                 (defun ,name (pred first-seq &rest more-seqs)
867                   #!+sb-doc ,doc
868                   (flet ((map-me (&rest rest)
869                            (let ((pred-value (apply pred rest)))
870                              (,found-test pred-value
871                                           (return-from ,name
872                                             ,found-result)))))
873                     (declare (inline map-me))
874                     (apply #'map nil #'map-me first-seq more-seqs)
875                     ,unfound-result))
876                 ;; KLUDGE: It would be more obviously correct -- but
877                 ;; also significantly messier -- for PRED-VALUE to be
878                 ;; a gensym. However, a private symbol really does
879                 ;; seem to be good enough; and anyway the really
880                 ;; obviously correct solution is to make Python smart
881                 ;; enough that we can use an inline function instead
882                 ;; of a compiler macro (as above). -- WHN 20000410
883                 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
884                   (let ((elements (make-gensym-list (1+ (length more-seqs))))
885                         (blockname (gensym "BLOCK")))
886                     (once-only ((pred pred))
887                       `(block ,blockname
888                          (map nil
889                               (lambda (,@elements)
890                                 (let ((pred-value (funcall ,pred ,@elements)))
891                                   (,',found-test pred-value
892                                     (return-from ,blockname
893                                       ,',found-result))))
894                               ,first-seq
895                               ,@more-seqs)
896                          ,',unfound-result)))))))
897   (defquantifier some when pred-value :unfound-result nil :doc
898   "PREDICATE is applied to the elements with index 0 of the sequences, then 
899    possibly to those with index 1, and so on. SOME returns the first 
900    non-NIL value encountered, or NIL if the end of a sequence is reached.")
901   (defquantifier every unless nil :doc
902   "PREDICATE is applied to the elements with index 0 of the sequences, then
903    possibly to those with index 1, and so on. EVERY returns NIL as soon
904    as any invocation of PREDICATE returns NIL, or T if every invocation
905    is non-NIL.")
906   (defquantifier notany when nil :doc
907   "PREDICATE is applied to the elements with index 0 of the sequences, then 
908    possibly to those with index 1, and so on. NOTANY returns NIL as soon
909    as any invocation of PREDICATE returns a non-NIL value, or T if the end
910    of a sequence is reached.")
911   (defquantifier notevery unless t :doc
912   "PREDICATE is applied to the elements with index 0 of the sequences, then
913    possibly to those with index 1, and so on. NOTEVERY returns T as soon
914    as any invocation of PREDICATE returns NIL, or NIL if every invocation
915    is non-NIL."))
916 \f
917 ;;;; REDUCE
918
919 (eval-when (:compile-toplevel :execute)
920
921 (sb!xc:defmacro mumble-reduce (function
922                                sequence
923                                key
924                                start
925                                end
926                                initial-value
927                                ref)
928   `(do ((index ,start (1+ index))
929         (value ,initial-value))
930        ((= index (the fixnum ,end)) value)
931      (declare (fixnum index))
932      (setq value (funcall ,function value
933                           (apply-key ,key (,ref ,sequence index))))))
934
935 (sb!xc:defmacro mumble-reduce-from-end (function
936                                         sequence
937                                         key
938                                         start
939                                         end
940                                         initial-value
941                                         ref)
942   `(do ((index (1- ,end) (1- index))
943         (value ,initial-value)
944         (terminus (1- ,start)))
945        ((= index terminus) value)
946      (declare (fixnum index terminus))
947      (setq value (funcall ,function
948                           (apply-key ,key (,ref ,sequence index))
949                           value))))
950
951 (sb!xc:defmacro list-reduce (function
952                              sequence
953                              key
954                              start
955                              end
956                              initial-value
957                              ivp)
958   `(let ((sequence (nthcdr ,start ,sequence)))
959      (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
960                  (1+ count))
961           (sequence (if ,ivp sequence (cdr sequence))
962                     (cdr sequence))
963           (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
964                  (funcall ,function value (apply-key ,key (car sequence)))))
965          ((= count (the fixnum ,end)) value)
966        (declare (fixnum count)))))
967
968 (sb!xc:defmacro list-reduce-from-end (function
969                                       sequence
970                                       key
971                                       start
972                                       end
973                                       initial-value
974                                       ivp)
975   `(let ((sequence (nthcdr (- (the fixnum (length ,sequence))
976                               (the fixnum ,end))
977                            (reverse ,sequence))))
978      (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
979                  (1+ count))
980           (sequence (if ,ivp sequence (cdr sequence))
981                     (cdr sequence))
982           (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
983                  (funcall ,function (apply-key ,key (car sequence)) value)))
984          ((= count (the fixnum ,end)) value)
985        (declare (fixnum count)))))
986
987 ) ; EVAL-WHEN
988
989 (defun reduce (function sequence &key key from-end (start 0)
990                         end (initial-value nil ivp))
991   (declare (type index start))
992   (let ((start start)
993         (end (or end (length sequence))))
994     (declare (type index start end))
995     (cond ((= end start)
996            (if ivp initial-value (funcall function)))
997           ((listp sequence)
998            (if from-end
999                (list-reduce-from-end function sequence key start end
1000                                      initial-value ivp)
1001                (list-reduce function sequence key start end
1002                             initial-value ivp)))
1003           (from-end
1004            (when (not ivp)
1005              (setq end (1- (the fixnum end)))
1006              (setq initial-value (apply-key key (aref sequence end))))
1007            (mumble-reduce-from-end function sequence key start end
1008                                    initial-value aref))
1009           (t
1010            (when (not ivp)
1011              (setq initial-value (apply-key key (aref sequence start)))
1012              (setq start (1+ start)))
1013            (mumble-reduce function sequence key start end
1014                           initial-value aref)))))
1015 \f
1016 ;;;; DELETE
1017
1018 (eval-when (:compile-toplevel :execute)
1019
1020 (sb!xc:defmacro mumble-delete (pred)
1021   `(do ((index start (1+ index))
1022         (jndex start)
1023         (number-zapped 0))
1024        ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1025         (do ((index index (1+ index))           ; Copy the rest of the vector.
1026              (jndex jndex (1+ jndex)))
1027             ((= index (the fixnum length))
1028              (shrink-vector sequence jndex))
1029           (declare (fixnum index jndex))
1030           (setf (aref sequence jndex) (aref sequence index))))
1031      (declare (fixnum index jndex number-zapped))
1032      (setf (aref sequence jndex) (aref sequence index))
1033      (if ,pred
1034          (setq number-zapped (1+ number-zapped))
1035          (setq jndex (1+ jndex)))))
1036
1037 (sb!xc:defmacro mumble-delete-from-end (pred)
1038   `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1039         (number-zapped 0)
1040         (losers ())
1041         this-element
1042         (terminus (1- start)))
1043        ((or (= index terminus) (= number-zapped (the fixnum count)))
1044         (do ((losers losers)                     ; Delete the losers.
1045              (index start (1+ index))
1046              (jndex start))
1047             ((or (null losers) (= index (the fixnum end)))
1048              (do ((index index (1+ index))       ; Copy the rest of the vector.
1049                   (jndex jndex (1+ jndex)))
1050                  ((= index (the fixnum length))
1051                   (shrink-vector sequence jndex))
1052                (declare (fixnum index jndex))
1053                (setf (aref sequence jndex) (aref sequence index))))
1054           (declare (fixnum index jndex))
1055           (setf (aref sequence jndex) (aref sequence index))
1056           (if (= index (the fixnum (car losers)))
1057               (pop losers)
1058               (setq jndex (1+ jndex)))))
1059      (declare (fixnum index number-zapped terminus))
1060      (setq this-element (aref sequence index))
1061      (when ,pred
1062        (setq number-zapped (1+ number-zapped))
1063        (push index losers))))
1064
1065 (sb!xc:defmacro normal-mumble-delete ()
1066   `(mumble-delete
1067     (if test-not
1068         (not (funcall test-not item (apply-key key (aref sequence index))))
1069         (funcall test item (apply-key key (aref sequence index))))))
1070
1071 (sb!xc:defmacro normal-mumble-delete-from-end ()
1072   `(mumble-delete-from-end
1073     (if test-not
1074         (not (funcall test-not item (apply-key key this-element)))
1075         (funcall test item (apply-key key this-element)))))
1076
1077 (sb!xc:defmacro list-delete (pred)
1078   `(let ((handle (cons nil sequence)))
1079      (do ((current (nthcdr start sequence) (cdr current))
1080           (previous (nthcdr start handle))
1081           (index start (1+ index))
1082           (number-zapped 0))
1083          ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1084           (cdr handle))
1085        (declare (fixnum index number-zapped))
1086        (cond (,pred
1087               (rplacd previous (cdr current))
1088               (setq number-zapped (1+ number-zapped)))
1089              (t
1090               (setq previous (cdr previous)))))))
1091
1092 (sb!xc:defmacro list-delete-from-end (pred)
1093   `(let* ((reverse (nreverse (the list sequence)))
1094           (handle (cons nil reverse)))
1095      (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1096                    (cdr current))
1097           (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1098           (index start (1+ index))
1099           (number-zapped 0))
1100          ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1101           (nreverse (cdr handle)))
1102        (declare (fixnum index number-zapped))
1103        (cond (,pred
1104               (rplacd previous (cdr current))
1105               (setq number-zapped (1+ number-zapped)))
1106              (t
1107               (setq previous (cdr previous)))))))
1108
1109 (sb!xc:defmacro normal-list-delete ()
1110   '(list-delete
1111     (if test-not
1112         (not (funcall test-not item (apply-key key (car current))))
1113         (funcall test item (apply-key key (car current))))))
1114
1115 (sb!xc:defmacro normal-list-delete-from-end ()
1116   '(list-delete-from-end
1117     (if test-not
1118         (not (funcall test-not item (apply-key key (car current))))
1119         (funcall test item (apply-key key (car current))))))
1120
1121 ) ; EVAL-WHEN
1122
1123 (defun delete (item sequence &key from-end (test #'eql) test-not (start 0)
1124                 end count key)
1125   #!+sb-doc
1126   "Returns a sequence formed by destructively removing the specified Item from
1127   the given Sequence."
1128   (declare (fixnum start))
1129   (let* ((length (length sequence))
1130          (end (or end length))
1131          (count (or count most-positive-fixnum)))
1132     (declare (type index length end)
1133              (fixnum count))
1134     (seq-dispatch sequence
1135                   (if from-end
1136                       (normal-list-delete-from-end)
1137                       (normal-list-delete))
1138                   (if from-end
1139                       (normal-mumble-delete-from-end)
1140                       (normal-mumble-delete)))))
1141
1142 (eval-when (:compile-toplevel :execute)
1143
1144 (sb!xc:defmacro if-mumble-delete ()
1145   `(mumble-delete
1146     (funcall predicate (apply-key key (aref sequence index)))))
1147
1148 (sb!xc:defmacro if-mumble-delete-from-end ()
1149   `(mumble-delete-from-end
1150     (funcall predicate (apply-key key this-element))))
1151
1152 (sb!xc:defmacro if-list-delete ()
1153   '(list-delete
1154     (funcall predicate (apply-key key (car current)))))
1155
1156 (sb!xc:defmacro if-list-delete-from-end ()
1157   '(list-delete-from-end
1158     (funcall predicate (apply-key key (car current)))))
1159
1160 ) ; EVAL-WHEN
1161
1162 (defun delete-if (predicate sequence &key from-end (start 0) key end count)
1163   #!+sb-doc
1164   "Returns a sequence formed by destructively removing the elements satisfying
1165   the specified Predicate from the given Sequence."
1166   (declare (fixnum start))
1167   (let* ((length (length sequence))
1168          (end (or end length))
1169          (count (or count most-positive-fixnum)))
1170     (declare (type index length end)
1171              (fixnum count))
1172     (seq-dispatch sequence
1173                   (if from-end
1174                       (if-list-delete-from-end)
1175                       (if-list-delete))
1176                   (if from-end
1177                       (if-mumble-delete-from-end)
1178                       (if-mumble-delete)))))
1179
1180 (eval-when (:compile-toplevel :execute)
1181
1182 (sb!xc:defmacro if-not-mumble-delete ()
1183   `(mumble-delete
1184     (not (funcall predicate (apply-key key (aref sequence index))))))
1185
1186 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1187   `(mumble-delete-from-end
1188     (not (funcall predicate (apply-key key this-element)))))
1189
1190 (sb!xc:defmacro if-not-list-delete ()
1191   '(list-delete
1192     (not (funcall predicate (apply-key key (car current))))))
1193
1194 (sb!xc:defmacro if-not-list-delete-from-end ()
1195   '(list-delete-from-end
1196     (not (funcall predicate (apply-key key (car current))))))
1197
1198 ) ; EVAL-WHEN
1199
1200 (defun delete-if-not (predicate sequence &key from-end (start 0) end key count)
1201   #!+sb-doc
1202   "Returns a sequence formed by destructively removing the elements not
1203   satisfying the specified Predicate from the given Sequence."
1204   (declare (fixnum start))
1205   (let* ((length (length sequence))
1206          (end (or end length))
1207          (count (or count most-positive-fixnum)))
1208     (declare (type index length end)
1209              (fixnum count))
1210     (seq-dispatch sequence
1211                   (if from-end
1212                       (if-not-list-delete-from-end)
1213                       (if-not-list-delete))
1214                   (if from-end
1215                       (if-not-mumble-delete-from-end)
1216                       (if-not-mumble-delete)))))
1217 \f
1218 ;;;; REMOVE
1219
1220 (eval-when (:compile-toplevel :execute)
1221
1222 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1223 ;;; satisfies the predicate.
1224 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1225   `(do ((index ,begin (,bump index))
1226         (result
1227          (do ((index ,left (,bump index))
1228               (result (make-sequence-like sequence length)))
1229              ((= index (the fixnum ,begin)) result)
1230            (declare (fixnum index))
1231            (setf (aref result index) (aref sequence index))))
1232         (new-index ,begin)
1233         (number-zapped 0)
1234         (this-element))
1235        ((or (= index (the fixnum ,finish))
1236             (= number-zapped (the fixnum count)))
1237         (do ((index index (,bump index))
1238              (new-index new-index (,bump new-index)))
1239             ((= index (the fixnum ,right)) (shrink-vector result new-index))
1240           (declare (fixnum index new-index))
1241           (setf (aref result new-index) (aref sequence index))))
1242      (declare (fixnum index new-index number-zapped))
1243      (setq this-element (aref sequence index))
1244      (cond (,pred (setq number-zapped (1+ number-zapped)))
1245            (t (setf (aref result new-index) this-element)
1246               (setq new-index (,bump new-index))))))
1247
1248 (sb!xc:defmacro mumble-remove (pred)
1249   `(mumble-remove-macro 1+ 0 start end length ,pred))
1250
1251 (sb!xc:defmacro mumble-remove-from-end (pred)
1252   `(let ((sequence (copy-seq sequence)))
1253      (mumble-delete-from-end ,pred)))
1254
1255 (sb!xc:defmacro normal-mumble-remove ()
1256   `(mumble-remove
1257     (if test-not
1258         (not (funcall test-not item (apply-key key this-element)))
1259         (funcall test item (apply-key key this-element)))))
1260
1261 (sb!xc:defmacro normal-mumble-remove-from-end ()
1262   `(mumble-remove-from-end
1263     (if test-not
1264         (not (funcall test-not item (apply-key key this-element)))
1265         (funcall test item (apply-key key this-element)))))
1266
1267 (sb!xc:defmacro if-mumble-remove ()
1268   `(mumble-remove (funcall predicate (apply-key key this-element))))
1269
1270 (sb!xc:defmacro if-mumble-remove-from-end ()
1271   `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1272
1273 (sb!xc:defmacro if-not-mumble-remove ()
1274   `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1275
1276 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1277   `(mumble-remove-from-end
1278     (not (funcall predicate (apply-key key this-element)))))
1279
1280 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1281 ;;; the predicate.
1282 (sb!xc:defmacro list-remove-macro (pred reverse?)
1283   `(let* ((sequence ,(if reverse?
1284                          '(reverse (the list sequence))
1285                          'sequence))
1286           (splice (list nil))
1287           (results (do ((index 0 (1+ index))
1288                         (before-start splice))
1289                        ((= index (the fixnum start)) before-start)
1290                      (declare (fixnum index))
1291                      (setq splice
1292                            (cdr (rplacd splice (list (pop sequence))))))))
1293      (do ((index start (1+ index))
1294           (this-element)
1295           (number-zapped 0))
1296          ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1297           (do ((index index (1+ index)))
1298               ((null sequence)
1299                ,(if reverse?
1300                     '(nreverse (the list (cdr results)))
1301                     '(cdr results)))
1302             (declare (fixnum index))
1303             (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1304        (declare (fixnum index number-zapped))
1305        (setq this-element (pop sequence))
1306        (if ,pred
1307            (setq number-zapped (1+ number-zapped))
1308            (setq splice (cdr (rplacd splice (list this-element))))))))
1309
1310 (sb!xc:defmacro list-remove (pred)
1311   `(list-remove-macro ,pred nil))
1312
1313 (sb!xc:defmacro list-remove-from-end (pred)
1314   `(list-remove-macro ,pred t))
1315
1316 (sb!xc:defmacro normal-list-remove ()
1317   `(list-remove
1318     (if test-not
1319         (not (funcall test-not item (apply-key key this-element)))
1320         (funcall test item (apply-key key this-element)))))
1321
1322 (sb!xc:defmacro normal-list-remove-from-end ()
1323   `(list-remove-from-end
1324     (if test-not
1325         (not (funcall test-not item (apply-key key this-element)))
1326         (funcall test item (apply-key key this-element)))))
1327
1328 (sb!xc:defmacro if-list-remove ()
1329   `(list-remove
1330     (funcall predicate (apply-key key this-element))))
1331
1332 (sb!xc:defmacro if-list-remove-from-end ()
1333   `(list-remove-from-end
1334     (funcall predicate (apply-key key this-element))))
1335
1336 (sb!xc:defmacro if-not-list-remove ()
1337   `(list-remove
1338     (not (funcall predicate (apply-key key this-element)))))
1339
1340 (sb!xc:defmacro if-not-list-remove-from-end ()
1341   `(list-remove-from-end
1342     (not (funcall predicate (apply-key key this-element)))))
1343
1344 ) ; EVAL-WHEN
1345
1346 (defun remove (item sequence &key from-end (test #'eql) test-not (start 0)
1347                 end count key)
1348   #!+sb-doc
1349   "Returns a copy of SEQUENCE with elements satisfying the test (default is
1350    EQL) with ITEM removed."
1351   (declare (fixnum start))
1352   (let* ((length (length sequence))
1353          (end (or end length))
1354          (count (or count most-positive-fixnum)))
1355     (declare (type index length end)
1356              (fixnum count))
1357     (seq-dispatch sequence
1358                   (if from-end
1359                       (normal-list-remove-from-end)
1360                       (normal-list-remove))
1361                   (if from-end
1362                       (normal-mumble-remove-from-end)
1363                       (normal-mumble-remove)))))
1364
1365 (defun remove-if (predicate sequence &key from-end (start 0) end count key)
1366   #!+sb-doc
1367   "Returns a copy of sequence with elements such that predicate(element)
1368    is non-null are removed"
1369   (declare (fixnum start))
1370   (let* ((length (length sequence))
1371          (end (or end length))
1372          (count (or count most-positive-fixnum)))
1373     (declare (type index length end)
1374              (fixnum count))
1375     (seq-dispatch sequence
1376                   (if from-end
1377                       (if-list-remove-from-end)
1378                       (if-list-remove))
1379                   (if from-end
1380                       (if-mumble-remove-from-end)
1381                       (if-mumble-remove)))))
1382
1383 (defun remove-if-not (predicate sequence &key from-end (start 0) end count key)
1384   #!+sb-doc
1385   "Returns a copy of sequence with elements such that predicate(element)
1386    is null are removed"
1387   (declare (fixnum start))
1388   (let* ((length (length sequence))
1389          (end (or end length))
1390          (count (or count most-positive-fixnum)))
1391     (declare (type index length end)
1392              (fixnum count))
1393     (seq-dispatch sequence
1394                   (if from-end
1395                       (if-not-list-remove-from-end)
1396                       (if-not-list-remove))
1397                   (if from-end
1398                       (if-not-mumble-remove-from-end)
1399                       (if-not-mumble-remove)))))
1400 \f
1401 ;;;; REMOVE-DUPLICATES
1402
1403 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1404 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1405 ;;; if we look into the already copied structure (from after :start) and see
1406 ;;; the item. If we check from beginning we check into the rest of the
1407 ;;; original list up to the :end marker (this we have to do by running a
1408 ;;; do loop down the list that far and using our test.
1409 (defun list-remove-duplicates* (list test test-not start end key from-end)
1410   (declare (fixnum start))
1411   (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1412          (splice result)
1413          (current list))
1414     (do ((index 0 (1+ index)))
1415         ((= index start))
1416       (declare (fixnum index))
1417       (setq splice (cdr (rplacd splice (list (car current)))))
1418       (setq current (cdr current)))
1419     (do ((index 0 (1+ index)))
1420         ((or (and end (= index (the fixnum end)))
1421              (atom current)))
1422       (declare (fixnum index))
1423       (if (or (and from-end
1424                    (not (member (apply-key key (car current))
1425                                 (nthcdr (1+ start) result)
1426                                 :test test
1427                                 :test-not test-not
1428                                 :key key)))
1429               (and (not from-end)
1430                    (not (do ((it (apply-key key (car current)))
1431                              (l (cdr current) (cdr l))
1432                              (i (1+ index) (1+ i)))
1433                             ((or (atom l) (and end (= i (the fixnum end))))
1434                              ())
1435                           (declare (fixnum i))
1436                           (if (if test-not
1437                                   (not (funcall test-not it (apply-key key (car l))))
1438                                   (funcall test it (apply-key key (car l))))
1439                               (return t))))))
1440           (setq splice (cdr (rplacd splice (list (car current))))))
1441       (setq current (cdr current)))
1442     (do ()
1443         ((atom current))
1444       (setq splice (cdr (rplacd splice (list (car current)))))
1445       (setq current (cdr current)))
1446     (cdr result)))
1447
1448 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1449                                          &optional (length (length vector)))
1450   (declare (vector vector) (fixnum start length))
1451   (when (null end) (setf end (length vector)))
1452   (let ((result (make-sequence-like vector length))
1453         (index 0)
1454         (jndex start))
1455     (declare (fixnum index jndex))
1456     (do ()
1457         ((= index start))
1458       (setf (aref result index) (aref vector index))
1459       (setq index (1+ index)))
1460     (do ((elt))
1461         ((= index end))
1462       (setq elt (aref vector index))
1463       (unless (or (and from-end
1464                         (position (apply-key key elt) result :start start
1465                            :end jndex :test test :test-not test-not :key key))
1466                   (and (not from-end)
1467                         (position (apply-key key elt) vector :start (1+ index)
1468                            :end end :test test :test-not test-not :key key)))
1469         (setf (aref result jndex) elt)
1470         (setq jndex (1+ jndex)))
1471       (setq index (1+ index)))
1472     (do ()
1473         ((= index length))
1474       (setf (aref result jndex) (aref vector index))
1475       (setq index (1+ index))
1476       (setq jndex (1+ jndex)))
1477     (shrink-vector result jndex)))
1478
1479 (defun remove-duplicates (sequence &key
1480                                    (test #'eql)
1481                                    test-not
1482                                    (start 0)
1483                                    from-end
1484                                    end
1485                                    key)
1486   #!+sb-doc
1487   "The elements of Sequence are compared pairwise, and if any two match,
1488    the one occurring earlier is discarded, unless FROM-END is true, in
1489    which case the one later in the sequence is discarded. The resulting
1490    sequence is returned.
1491
1492    The :TEST-NOT argument is depreciated."
1493   (declare (fixnum start))
1494   (seq-dispatch sequence
1495                 (if sequence
1496                     (list-remove-duplicates* sequence test test-not
1497                                               start end key from-end))
1498                 (vector-remove-duplicates* sequence test test-not
1499                                             start end key from-end)))
1500 \f
1501 ;;;; DELETE-DUPLICATES
1502
1503 (defun list-delete-duplicates* (list test test-not key from-end start end)
1504   (declare (fixnum start))
1505   (let ((handle (cons nil list)))
1506     (do ((current (nthcdr start list) (cdr current))
1507          (previous (nthcdr start handle))
1508          (index start (1+ index)))
1509         ((or (and end (= index (the fixnum end))) (null current))
1510          (cdr handle))
1511       (declare (fixnum index))
1512       (if (do ((x (if from-end
1513                       (nthcdr (1+ start) handle)
1514                       (cdr current))
1515                   (cdr x))
1516                (i (1+ index) (1+ i)))
1517               ((or (null x)
1518                    (and (not from-end) end (= i (the fixnum end)))
1519                    (eq x current))
1520                nil)
1521             (declare (fixnum i))
1522             (if (if test-not
1523                     (not (funcall test-not
1524                                   (apply-key key (car current))
1525                                   (apply-key key (car x))))
1526                     (funcall test
1527                              (apply-key key (car current))
1528                              (apply-key key (car x))))
1529                 (return t)))
1530           (rplacd previous (cdr current))
1531           (setq previous (cdr previous))))))
1532
1533 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1534                                          &optional (length (length vector)))
1535   (declare (vector vector) (fixnum start length))
1536   (when (null end) (setf end (length vector)))
1537   (do ((index start (1+ index))
1538        (jndex start))
1539       ((= index end)
1540        (do ((index index (1+ index))            ; copy the rest of the vector
1541             (jndex jndex (1+ jndex)))
1542            ((= index length)
1543             (shrink-vector vector jndex)
1544             vector)
1545          (setf (aref vector jndex) (aref vector index))))
1546     (declare (fixnum index jndex))
1547     (setf (aref vector jndex) (aref vector index))
1548     (unless (position (apply-key key (aref vector index)) vector :key key
1549                       :start (if from-end start (1+ index)) :test test
1550                       :end (if from-end jndex end) :test-not test-not)
1551       (setq jndex (1+ jndex)))))
1552
1553 (defun delete-duplicates (sequence &key
1554                                    (test #'eql)
1555                                    test-not
1556                                    (start 0)
1557                                    from-end
1558                                    end
1559                                    key)
1560   #!+sb-doc
1561   "The elements of Sequence are examined, and if any two match, one is
1562    discarded. The resulting sequence, which may be formed by destroying the
1563    given sequence, is returned.
1564
1565    The :TEST-NOT argument is depreciated."
1566   (seq-dispatch sequence
1567     (if sequence
1568         (list-delete-duplicates* sequence test test-not key from-end start end))
1569   (vector-delete-duplicates* sequence test test-not key from-end start end)))
1570 \f
1571 ;;;; SUBSTITUTE
1572
1573 (defun list-substitute* (pred new list start end count key test test-not old)
1574   (declare (fixnum start end count))
1575   (let* ((result (list nil))
1576          elt
1577          (splice result)
1578          (list list))      ; Get a local list for a stepper.
1579     (do ((index 0 (1+ index)))
1580         ((= index start))
1581       (declare (fixnum index))
1582       (setq splice (cdr (rplacd splice (list (car list)))))
1583       (setq list (cdr list)))
1584     (do ((index start (1+ index)))
1585         ((or (= index end) (null list) (= count 0)))
1586       (declare (fixnum index))
1587       (setq elt (car list))
1588       (setq splice
1589             (cdr (rplacd splice
1590                          (list
1591                           (cond
1592                            ((case pred
1593                                    (normal
1594                                     (if test-not
1595                                         (not
1596                                          (funcall test-not old (apply-key key elt)))
1597                                         (funcall test old (apply-key key elt))))
1598                                    (if (funcall test (apply-key key elt)))
1599                                    (if-not (not (funcall test (apply-key key elt)))))
1600                             (setq count (1- count))
1601                             new)
1602                                 (t elt))))))
1603       (setq list (cdr list)))
1604     (do ()
1605         ((null list))
1606       (setq splice (cdr (rplacd splice (list (car list)))))
1607       (setq list (cdr list)))
1608     (cdr result)))
1609
1610 ;;; Replace old with new in sequence moving from left to right by incrementer
1611 ;;; on each pass through the loop. Called by all three substitute functions.
1612 (defun vector-substitute* (pred new sequence incrementer left right length
1613                            start end count key test test-not old)
1614   (declare (fixnum start count end incrementer right))
1615   (let ((result (make-sequence-like sequence length))
1616         (index left))
1617     (declare (fixnum index))
1618     (do ()
1619         ((= index start))
1620       (setf (aref result index) (aref sequence index))
1621       (setq index (+ index incrementer)))
1622     (do ((elt))
1623         ((or (= index end) (= count 0)))
1624       (setq elt (aref sequence index))
1625       (setf (aref result index)
1626             (cond ((case pred
1627                           (normal
1628                             (if test-not
1629                                 (not (funcall test-not old (apply-key key elt)))
1630                                 (funcall test old (apply-key key elt))))
1631                           (if (funcall test (apply-key key elt)))
1632                           (if-not (not (funcall test (apply-key key elt)))))
1633                    (setq count (1- count))
1634                    new)
1635                   (t elt)))
1636       (setq index (+ index incrementer)))
1637     (do ()
1638         ((= index right))
1639       (setf (aref result index) (aref sequence index))
1640       (setq index (+ index incrementer)))
1641     result))
1642
1643 (eval-when (:compile-toplevel :execute)
1644
1645 (sb!xc:defmacro subst-dispatch (pred)
1646   `(if (listp sequence)
1647        (if from-end
1648            (nreverse (list-substitute* ,pred
1649                                        new
1650                                        (reverse sequence)
1651                                        (- (the fixnum length)
1652                                           (the fixnum end))
1653                                        (- (the fixnum length)
1654                                           (the fixnum start))
1655                                        count key test test-not old))
1656            (list-substitute* ,pred
1657                              new sequence start end count key test test-not
1658                              old))
1659       (if from-end
1660           (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
1661                               -1 length (1- (the fixnum end))
1662                               (1- (the fixnum start))
1663                               count key test test-not old)
1664           (vector-substitute* ,pred new sequence 1 0 length length
1665            start end count key test test-not old))))
1666
1667 ) ; EVAL-WHEN
1668
1669 (defun substitute (new old sequence &key from-end (test #'eql) test-not
1670                    (start 0) count end key)
1671   #!+sb-doc
1672   "Returns a sequence of the same kind as Sequence with the same elements
1673   except that all elements equal to Old are replaced with New. See manual
1674   for details."
1675   (declare (fixnum start))
1676   (let* ((length (length sequence))
1677          (end (or end length))
1678          (count (or count most-positive-fixnum)))
1679     (declare (type index length end)
1680              (fixnum count))
1681     (subst-dispatch 'normal)))
1682 \f
1683 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
1684
1685 (defun substitute-if (new test sequence &key from-end (start 0) end count key)
1686   #!+sb-doc
1687   "Returns a sequence of the same kind as Sequence with the same elements
1688   except that all elements satisfying the Test are replaced with New. See
1689   manual for details."
1690   (declare (fixnum start))
1691   (let* ((length (length sequence))
1692          (end (or end length))
1693          (count (or count most-positive-fixnum))
1694          test-not
1695          old)
1696     (declare (type index length end)
1697              (fixnum count))
1698     (subst-dispatch 'if)))
1699
1700 (defun substitute-if-not (new test sequence &key from-end (start 0)
1701                            end count key)
1702   #!+sb-doc
1703   "Returns a sequence of the same kind as Sequence with the same elements
1704   except that all elements not satisfying the Test are replaced with New.
1705   See manual for details."
1706   (declare (fixnum start))
1707   (let* ((length (length sequence))
1708          (end (or end length))
1709          (count (or count most-positive-fixnum))
1710          test-not
1711          old)
1712     (declare (type index length end)
1713              (fixnum count))
1714     (subst-dispatch 'if-not)))
1715 \f
1716 ;;;; NSUBSTITUTE
1717
1718 (defun nsubstitute (new old sequence &key from-end (test #'eql) test-not
1719                      end count key (start 0))
1720   #!+sb-doc
1721   "Returns a sequence of the same kind as Sequence with the same elements
1722   except that all elements equal to Old are replaced with New. The Sequence
1723   may be destroyed. See manual for details."
1724   (declare (fixnum start))
1725   (let ((end (or end (length sequence)))
1726         (count (or count most-positive-fixnum)))
1727     (declare (fixnum count))
1728     (if (listp sequence)
1729         (if from-end
1730             (nreverse (nlist-substitute*
1731                        new old (nreverse (the list sequence))
1732                        test test-not start end count key))
1733             (nlist-substitute* new old sequence
1734                                test test-not start end count key))
1735         (if from-end
1736             (nvector-substitute* new old sequence -1
1737                                  test test-not (1- end) (1- start) count key)
1738             (nvector-substitute* new old sequence 1
1739                                  test test-not start end count key)))))
1740
1741 (defun nlist-substitute* (new old sequence test test-not start end count key)
1742   (declare (fixnum start count end))
1743   (do ((list (nthcdr start sequence) (cdr list))
1744        (index start (1+ index)))
1745       ((or (= index end) (null list) (= count 0)) sequence)
1746     (declare (fixnum index))
1747     (when (if test-not
1748               (not (funcall test-not old (apply-key key (car list))))
1749               (funcall test old (apply-key key (car list))))
1750       (rplaca list new)
1751       (setq count (1- count)))))
1752
1753 (defun nvector-substitute* (new old sequence incrementer
1754                             test test-not start end count key)
1755   (declare (fixnum start incrementer count end))
1756   (do ((index start (+ index incrementer)))
1757       ((or (= index end) (= count 0)) sequence)
1758     (declare (fixnum index))
1759     (when (if test-not
1760               (not (funcall test-not
1761                             old
1762                             (apply-key key (aref sequence index))))
1763               (funcall test old (apply-key key (aref sequence index))))
1764       (setf (aref sequence index) new)
1765       (setq count (1- count)))))
1766 \f
1767 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
1768
1769 (defun nsubstitute-if (new test sequence &key from-end (start 0) end count key)
1770   #!+sb-doc
1771   "Returns a sequence of the same kind as Sequence with the same elements
1772    except that all elements satisfying the Test are replaced with New. The
1773    Sequence may be destroyed. See manual for details."
1774   (declare (fixnum start))
1775   (let ((end (or end (length sequence)))
1776         (count (or count most-positive-fixnum)))
1777     (declare (fixnum end count))
1778     (if (listp sequence)
1779         (if from-end
1780             (nreverse (nlist-substitute-if*
1781                        new test (nreverse (the list sequence))
1782                        start end count key))
1783             (nlist-substitute-if* new test sequence
1784                                   start end count key))
1785         (if from-end
1786             (nvector-substitute-if* new test sequence -1
1787                                     (1- end) (1- start) count key)
1788             (nvector-substitute-if* new test sequence 1
1789                                     start end count key)))))
1790
1791 (defun nlist-substitute-if* (new test sequence start end count key)
1792   (declare (fixnum end))
1793   (do ((list (nthcdr start sequence) (cdr list))
1794        (index start (1+ index)))
1795       ((or (= index end) (null list) (= count 0)) sequence)
1796     (when (funcall test (apply-key key (car list)))
1797       (rplaca list new)
1798       (setq count (1- count)))))
1799
1800 (defun nvector-substitute-if* (new test sequence incrementer
1801                                start end count key)
1802   (do ((index start (+ index incrementer)))
1803       ((or (= index end) (= count 0)) sequence)
1804     (when (funcall test (apply-key key (aref sequence index)))
1805       (setf (aref sequence index) new)
1806       (setq count (1- count)))))
1807
1808 (defun nsubstitute-if-not (new test sequence &key from-end (start 0)
1809                                end count key)
1810   #!+sb-doc
1811   "Returns a sequence of the same kind as Sequence with the same elements
1812    except that all elements not satisfying the Test are replaced with New.
1813    The Sequence may be destroyed. See manual for details."
1814   (declare (fixnum start))
1815   (let ((end (or end (length sequence)))
1816         (count (or count most-positive-fixnum)))
1817     (declare (fixnum end count))
1818     (if (listp sequence)
1819         (if from-end
1820             (nreverse (nlist-substitute-if-not*
1821                        new test (nreverse (the list sequence))
1822                        start end count key))
1823             (nlist-substitute-if-not* new test sequence
1824                                       start end count key))
1825         (if from-end
1826             (nvector-substitute-if-not* new test sequence -1
1827                                         (1- end) (1- start) count key)
1828             (nvector-substitute-if-not* new test sequence 1
1829                                         start end count key)))))
1830
1831 (defun nlist-substitute-if-not* (new test sequence start end count key)
1832   (declare (fixnum end))
1833   (do ((list (nthcdr start sequence) (cdr list))
1834        (index start (1+ index)))
1835       ((or (= index end) (null list) (= count 0)) sequence)
1836     (when (not (funcall test (apply-key key (car list))))
1837       (rplaca list new)
1838       (setq count (1- count)))))
1839
1840 (defun nvector-substitute-if-not* (new test sequence incrementer
1841                                    start end count key)
1842   (do ((index start (+ index incrementer)))
1843       ((or (= index end) (= count 0)) sequence)
1844     (when (not (funcall test (apply-key key (aref sequence index))))
1845       (setf (aref sequence index) new)
1846       (setq count (1- count)))))
1847 \f
1848 ;;; locater macros used by FIND and POSITION
1849
1850 (eval-when (:compile-toplevel :execute)
1851
1852 (sb!xc:defmacro vector-locater-macro (sequence body-form return-type)
1853   `(let ((incrementer (if from-end -1 1))
1854          (start (if from-end (1- (the fixnum end)) start))
1855          (end (if from-end (1- (the fixnum start)) end)))
1856      (declare (fixnum start end incrementer))
1857      (do ((index start (+ index incrementer))
1858           ,@(case return-type (:position nil) (:element '(current))))
1859          ((= index end) ())
1860        (declare (fixnum index))
1861        ,@(case return-type
1862            (:position nil)
1863            (:element `((setf current (aref ,sequence index)))))
1864        ,body-form)))
1865
1866 (sb!xc:defmacro locater-test-not (item sequence seq-type return-type)
1867   (let ((seq-ref (case return-type
1868                    (:position
1869                     (case seq-type
1870                       (:vector `(aref ,sequence index))
1871                       (:list `(pop ,sequence))))
1872                    (:element 'current)))
1873         (return (case return-type
1874                   (:position 'index)
1875                   (:element 'current))))
1876     `(if test-not
1877          (if (not (funcall test-not ,item (apply-key key ,seq-ref)))
1878              (return ,return))
1879          (if (funcall test ,item (apply-key key ,seq-ref))
1880              (return ,return)))))
1881
1882 (sb!xc:defmacro vector-locater (item sequence return-type)
1883   `(vector-locater-macro ,sequence
1884                          (locater-test-not ,item ,sequence :vector ,return-type)
1885                          ,return-type))
1886
1887 (sb!xc:defmacro locater-if-test (test sequence seq-type return-type sense)
1888   (let ((seq-ref (case return-type
1889                    (:position
1890                     (case seq-type
1891                       (:vector `(aref ,sequence index))
1892                       (:list `(pop ,sequence))))
1893                    (:element 'current)))
1894         (return (case return-type
1895                   (:position 'index)
1896                   (:element 'current))))
1897     (if sense
1898         `(if (funcall ,test (apply-key key ,seq-ref))
1899              (return ,return))
1900         `(if (not (funcall ,test (apply-key key ,seq-ref)))
1901              (return ,return)))))
1902
1903 (sb!xc:defmacro vector-locater-if-macro (test sequence return-type sense)
1904   `(vector-locater-macro ,sequence
1905                          (locater-if-test ,test ,sequence :vector ,return-type ,sense)
1906                          ,return-type))
1907
1908 (sb!xc:defmacro vector-locater-if (test sequence return-type)
1909   `(vector-locater-if-macro ,test ,sequence ,return-type t))
1910
1911 (sb!xc:defmacro vector-locater-if-not (test sequence return-type)
1912   `(vector-locater-if-macro ,test ,sequence ,return-type nil))
1913
1914 (sb!xc:defmacro list-locater-macro (sequence body-form return-type)
1915   `(if from-end
1916        (do ((sequence (nthcdr (- (the fixnum (length sequence))
1917                                  (the fixnum end))
1918                               (reverse (the list ,sequence))))
1919             (index (1- (the fixnum end)) (1- index))
1920             (terminus (1- (the fixnum start)))
1921             ,@(case return-type (:position nil) (:element '(current))))
1922            ((or (= index terminus) (null sequence)) ())
1923          (declare (fixnum index terminus))
1924          ,@(case return-type
1925              (:position nil)
1926              (:element `((setf current (pop ,sequence)))))
1927          ,body-form)
1928        (do ((sequence (nthcdr start ,sequence))
1929             (index start (1+ index))
1930             ,@(case return-type (:position nil) (:element '(current))))
1931            ((or (= index (the fixnum end)) (null sequence)) ())
1932          (declare (fixnum index))
1933          ,@(case return-type
1934              (:position nil)
1935              (:element `((setf current (pop ,sequence)))))
1936          ,body-form)))
1937
1938 (sb!xc:defmacro list-locater (item sequence return-type)
1939   `(list-locater-macro ,sequence
1940                        (locater-test-not ,item ,sequence :list ,return-type)
1941                        ,return-type))
1942
1943 (sb!xc:defmacro list-locater-if-macro (test sequence return-type sense)
1944   `(list-locater-macro ,sequence
1945                        (locater-if-test ,test ,sequence :list ,return-type ,sense)
1946                        ,return-type))
1947
1948 (sb!xc:defmacro list-locater-if (test sequence return-type)
1949   `(list-locater-if-macro ,test ,sequence ,return-type t))
1950
1951 (sb!xc:defmacro list-locater-if-not (test sequence return-type)
1952   `(list-locater-if-macro ,test ,sequence ,return-type nil))
1953
1954 ) ; EVAL-WHEN
1955 \f
1956 ;;;; POSITION
1957
1958 (eval-when (:compile-toplevel :execute)
1959
1960 (sb!xc:defmacro vector-position (item sequence)
1961   `(vector-locater ,item ,sequence :position))
1962
1963 (sb!xc:defmacro list-position (item sequence)
1964   `(list-locater ,item ,sequence :position))
1965
1966 ) ; EVAL-WHEN
1967
1968 ;;; POSITION cannot default end to the length of sequence since it is not
1969 ;;; an error to supply nil for its value. We must test for END being NIL
1970 ;;; in the body of the function, and this is actually done in the support
1971 ;;; routines for other reasons (see below).
1972 (defun position (item sequence &key from-end (test #'eql) test-not (start 0)
1973                   end key)
1974   #!+sb-doc
1975   "Returns the zero-origin index of the first element in SEQUENCE
1976    satisfying the test (default is EQL) with the given ITEM"
1977   (seq-dispatch sequence
1978     (list-position* item sequence from-end test test-not start end key)
1979     (vector-position* item sequence from-end test test-not start end key)))
1980
1981 ;;; The support routines for SUBSEQ are used by compiler transforms, so we
1982 ;;; worry about dealing with END being supplied or defaulting to NIL
1983 ;;; at this level.
1984
1985 (defun list-position* (item sequence from-end test test-not start end key)
1986   (declare (fixnum start))
1987   (when (null end) (setf end (length sequence)))
1988   (list-position item sequence))
1989
1990 (defun vector-position* (item sequence from-end test test-not start end key)
1991   (declare (fixnum start))
1992   (when (null end) (setf end (length sequence)))
1993   (vector-position item sequence))
1994 \f
1995 ;;;; POSITION-IF
1996
1997 (eval-when (:compile-toplevel :execute)
1998
1999 (sb!xc:defmacro vector-position-if (test sequence)
2000   `(vector-locater-if ,test ,sequence :position))
2001
2002 (sb!xc:defmacro list-position-if (test sequence)
2003   `(list-locater-if ,test ,sequence :position))
2004
2005 ) ; EVAL-WHEN
2006
2007 (defun position-if (test sequence &key from-end (start 0) key end)
2008   #!+sb-doc
2009   "Returns the zero-origin index of the first element satisfying test(el)"
2010   (declare (fixnum start))
2011   (let ((end (or end (length sequence))))
2012     (declare (type index end))
2013     (seq-dispatch sequence
2014                   (list-position-if test sequence)
2015                   (vector-position-if test sequence))))
2016 \f
2017 ;;;; POSITION-IF-NOT
2018
2019 (eval-when (:compile-toplevel :execute)
2020
2021 (sb!xc:defmacro vector-position-if-not (test sequence)
2022   `(vector-locater-if-not ,test ,sequence :position))
2023
2024 (sb!xc:defmacro list-position-if-not (test sequence)
2025   `(list-locater-if-not ,test ,sequence :position))
2026
2027 ) ; EVAL-WHEN
2028
2029 (defun position-if-not (test sequence &key from-end (start 0) key end)
2030   #!+sb-doc
2031   "Returns the zero-origin index of the first element not satisfying test(el)"
2032   (declare (fixnum start))
2033   (let ((end (or end (length sequence))))
2034     (declare (type index end))
2035     (seq-dispatch sequence
2036                   (list-position-if-not test sequence)
2037                   (vector-position-if-not test sequence))))
2038 \f
2039 ;;;; FIND
2040
2041 (eval-when (:compile-toplevel :execute)
2042
2043 (sb!xc:defmacro vector-find (item sequence)
2044   `(vector-locater ,item ,sequence :element))
2045
2046 (sb!xc:defmacro list-find (item sequence)
2047   `(list-locater ,item ,sequence :element))
2048
2049 ) ; EVAL-WHEN
2050
2051 ;;; Note: FIND cannot default end to the length of sequence since it
2052 ;;; is not an error to supply NIL for its value. We must test for end
2053 ;;; being NIL in the body of the function, and this is actually done
2054 ;;; in the support routines for other reasons (see above).
2055 (defun find (item sequence &key from-end (test #'eql) test-not (start 0)
2056                   end key)
2057   #!+sb-doc
2058   "Returns the first element in SEQUENCE satisfying the test (default
2059    is EQL) with the given ITEM"
2060   (declare (fixnum start))
2061   (seq-dispatch sequence
2062     (list-find* item sequence from-end test test-not start end key)
2063     (vector-find* item sequence from-end test test-not start end key)))
2064
2065 ;;; The support routines for FIND are used by compiler transforms, so we
2066 ;;; worry about dealing with END being supplied or defaulting to NIL
2067 ;;; at this level.
2068
2069 (defun list-find* (item sequence from-end test test-not start end key)
2070   (when (null end) (setf end (length sequence)))
2071   (list-find item sequence))
2072
2073 (defun vector-find* (item sequence from-end test test-not start end key)
2074   (when (null end) (setf end (length sequence)))
2075   (vector-find item sequence))
2076 \f
2077 ;;;; FIND-IF and FIND-IF-NOT
2078
2079 (eval-when (:compile-toplevel :execute)
2080
2081 (sb!xc:defmacro vector-find-if (test sequence)
2082   `(vector-locater-if ,test ,sequence :element))
2083
2084 (sb!xc:defmacro list-find-if (test sequence)
2085   `(list-locater-if ,test ,sequence :element))
2086
2087 ) ; EVAL-WHEN
2088
2089 (defun find-if (test sequence &key from-end (start 0) end key)
2090   #!+sb-doc
2091   "Returns the zero-origin index of the first element satisfying the test."
2092   (declare (fixnum start))
2093   (let ((end (or end (length sequence))))
2094     (declare (type index end))
2095     (seq-dispatch sequence
2096                   (list-find-if test sequence)
2097                   (vector-find-if test sequence))))
2098
2099 (eval-when (:compile-toplevel :execute)
2100
2101 (sb!xc:defmacro vector-find-if-not (test sequence)
2102   `(vector-locater-if-not ,test ,sequence :element))
2103
2104 (sb!xc:defmacro list-find-if-not (test sequence)
2105   `(list-locater-if-not ,test ,sequence :element))
2106
2107 ) ; EVAL-WHEN
2108
2109 (defun find-if-not (test sequence &key from-end (start 0) end key)
2110   #!+sb-doc
2111   "Returns the zero-origin index of the first element not satisfying the test."
2112   (declare (fixnum start))
2113   (let ((end (or end (length sequence))))
2114     (declare (type index end))
2115     (seq-dispatch sequence
2116                   (list-find-if-not test sequence)
2117                   (vector-find-if-not test sequence))))
2118 \f
2119 ;;;; COUNT
2120
2121 (eval-when (:compile-toplevel :execute)
2122
2123 (sb!xc:defmacro vector-count (item sequence)
2124   `(do ((index start (1+ index))
2125         (count 0))
2126        ((= index (the fixnum end)) count)
2127      (declare (fixnum index count))
2128      (if test-not
2129          (unless (funcall test-not ,item
2130                           (apply-key key (aref ,sequence index)))
2131            (setq count (1+ count)))
2132          (when (funcall test ,item (apply-key key (aref ,sequence index)))
2133            (setq count (1+ count))))))
2134
2135 (sb!xc:defmacro list-count (item sequence)
2136   `(do ((sequence (nthcdr start ,sequence))
2137         (index start (1+ index))
2138         (count 0))
2139        ((or (= index (the fixnum end)) (null sequence)) count)
2140      (declare (fixnum index count))
2141      (if test-not
2142          (unless (funcall test-not ,item (apply-key key (pop sequence)))
2143            (setq count (1+ count)))
2144          (when (funcall test ,item (apply-key key (pop sequence)))
2145            (setq count (1+ count))))))
2146
2147 ) ; EVAL-WHEN
2148
2149 (defun count (item sequence &key from-end (test #'eql) test-not (start 0)
2150                 end key)
2151   #!+sb-doc
2152   "Returns the number of elements in SEQUENCE satisfying a test with ITEM,
2153    which defaults to EQL."
2154   (declare (ignore from-end) (fixnum start))
2155   (let ((end (or end (length sequence))))
2156     (declare (type index end))
2157     (seq-dispatch sequence
2158                   (list-count item sequence)
2159                   (vector-count item sequence))))
2160 \f
2161 ;;;; COUNT-IF and COUNT-IF-NOT
2162
2163 (eval-when (:compile-toplevel :execute)
2164
2165 (sb!xc:defmacro vector-count-if (predicate sequence)
2166   `(do ((index start (1+ index))
2167         (count 0))
2168        ((= index (the fixnum end)) count)
2169      (declare (fixnum index count))
2170      (if (funcall ,predicate (apply-key key (aref ,sequence index)))
2171          (setq count (1+ count)))))
2172
2173 (sb!xc:defmacro list-count-if (predicate sequence)
2174   `(do ((sequence (nthcdr start ,sequence))
2175         (index start (1+ index))
2176         (count 0))
2177        ((or (= index (the fixnum end)) (null sequence)) count)
2178      (declare (fixnum index count))
2179      (if (funcall ,predicate (apply-key key (pop sequence)))
2180          (setq count (1+ count)))))
2181
2182 ) ; EVAL-WHEN
2183
2184 (defun count-if (test sequence &key from-end (start 0) end key)
2185   #!+sb-doc
2186   "Returns the number of elements in SEQUENCE satisfying TEST(el)."
2187   (declare (ignore from-end) (fixnum start))
2188   (let ((end (or end (length sequence))))
2189     (declare (type index end))
2190     (seq-dispatch sequence
2191                   (list-count-if test sequence)
2192                   (vector-count-if test sequence))))
2193
2194 (eval-when (:compile-toplevel :execute)
2195
2196 (sb!xc:defmacro vector-count-if-not (predicate sequence)
2197   `(do ((index start (1+ index))
2198         (count 0))
2199        ((= index (the fixnum end)) count)
2200      (declare (fixnum index count))
2201      (if (not (funcall ,predicate (apply-key key (aref ,sequence index))))
2202          (setq count (1+ count)))))
2203
2204 (sb!xc:defmacro list-count-if-not (predicate sequence)
2205   `(do ((sequence (nthcdr start ,sequence))
2206         (index start (1+ index))
2207         (count 0))
2208        ((or (= index (the fixnum end)) (null sequence)) count)
2209      (declare (fixnum index count))
2210      (if (not (funcall ,predicate (apply-key key (pop sequence))))
2211          (setq count (1+ count)))))
2212
2213 ) ; EVAL-WHEN
2214
2215 (defun count-if-not (test sequence &key from-end (start 0) end key)
2216   #!+sb-doc
2217   "Returns the number of elements in SEQUENCE not satisfying TEST(el)."
2218   (declare (ignore from-end) (fixnum start))
2219   (let ((end (or end (length sequence))))
2220     (declare (type index end))
2221     (seq-dispatch sequence
2222                   (list-count-if-not test sequence)
2223                   (vector-count-if-not test sequence))))
2224 \f
2225 ;;;; MISMATCH
2226
2227 (eval-when (:compile-toplevel :execute)
2228
2229 (sb!xc:defmacro match-vars (&rest body)
2230   `(let ((inc (if from-end -1 1))
2231          (start1 (if from-end (1- (the fixnum end1)) start1))
2232          (start2 (if from-end (1- (the fixnum end2)) start2))
2233          (end1 (if from-end (1- (the fixnum start1)) end1))
2234          (end2 (if from-end (1- (the fixnum start2)) end2)))
2235      (declare (fixnum inc start1 start2 end1 end2))
2236      ,@body))
2237
2238 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2239   (declare (ignore end)) ;; ### Should END be used below?
2240   `(let ((,sequence (if from-end
2241                         (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2242                                 (reverse (the list ,sequence)))
2243                         (nthcdr ,start ,sequence))))
2244      (declare (type list ,sequence))
2245      ,@body))
2246
2247 ) ; EVAL-WHEN
2248
2249 (eval-when (:compile-toplevel :execute)
2250
2251 (sb!xc:defmacro if-mismatch (elt1 elt2)
2252   `(cond ((= (the fixnum index1) (the fixnum end1))
2253           (return (if (= (the fixnum index2) (the fixnum end2))
2254                       nil
2255                       (if from-end
2256                           (1+ (the fixnum index1))
2257                           (the fixnum index1)))))
2258          ((= (the fixnum index2) (the fixnum end2))
2259           (return (if from-end (1+ (the fixnum index1)) index1)))
2260          (test-not
2261           (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2262               (return (if from-end (1+ (the fixnum index1)) index1))))
2263          (t (if (not (funcall test (apply-key key ,elt1)
2264                               (apply-key key ,elt2)))
2265                 (return (if from-end (1+ (the fixnum index1)) index1))))))
2266
2267 (sb!xc:defmacro mumble-mumble-mismatch ()
2268   `(do ((index1 start1 (+ index1 (the fixnum inc)))
2269         (index2 start2 (+ index2 (the fixnum inc))))
2270        (())
2271      (declare (fixnum index1 index2))
2272      (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2273
2274 (sb!xc:defmacro mumble-list-mismatch ()
2275   `(do ((index1 start1 (+ index1 (the fixnum inc)))
2276         (index2 start2 (+ index2 (the fixnum inc))))
2277        (())
2278      (declare (fixnum index1 index2))
2279      (if-mismatch (aref sequence1 index1) (pop sequence2))))
2280 \f
2281 (sb!xc:defmacro list-mumble-mismatch ()
2282   `(do ((index1 start1 (+ index1 (the fixnum inc)))
2283         (index2 start2 (+ index2 (the fixnum inc))))
2284        (())
2285      (declare (fixnum index1 index2))
2286      (if-mismatch (pop sequence1) (aref sequence2 index2))))
2287
2288 (sb!xc:defmacro list-list-mismatch ()
2289   `(do ((sequence1 sequence1)
2290         (sequence2 sequence2)
2291         (index1 start1 (+ index1 (the fixnum inc)))
2292         (index2 start2 (+ index2 (the fixnum inc))))
2293        (())
2294      (declare (fixnum index1 index2))
2295      (if-mismatch (pop sequence1) (pop sequence2))))
2296
2297 ) ; EVAL-WHEN
2298
2299 (defun mismatch (sequence1 sequence2 &key from-end (test #'eql) test-not
2300                            (start1 0) end1 (start2 0) end2 key)
2301   #!+sb-doc
2302   "The specified subsequences of Sequence1 and Sequence2 are compared
2303    element-wise. If they are of equal length and match in every element, the
2304    result is Nil. Otherwise, the result is a non-negative integer, the index
2305    within Sequence1 of the leftmost position at which they fail to match; or,
2306    if one is shorter than and a matching prefix of the other, the index within
2307    Sequence1 beyond the last position tested is returned. If a non-NIL
2308    :FROM-END argument is given, then one plus the index of the rightmost
2309    position in which the sequences differ is returned."
2310   (declare (fixnum start1 start2))
2311   (let* ((length1 (length sequence1))
2312          (end1 (or end1 length1))
2313          (length2 (length sequence2))
2314          (end2 (or end2 length2)))
2315     (declare (type index length1 end1 length2 end2))
2316     (match-vars
2317      (seq-dispatch sequence1
2318        (matchify-list (sequence1 start1 length1 end1)
2319          (seq-dispatch sequence2
2320            (matchify-list (sequence2 start2 length2 end2)
2321              (list-list-mismatch))
2322            (list-mumble-mismatch)))
2323        (seq-dispatch sequence2
2324          (matchify-list (sequence2 start2 length2 end2)
2325            (mumble-list-mismatch))
2326          (mumble-mumble-mismatch))))))
2327 \f
2328 ;;; search comparison functions
2329
2330 (eval-when (:compile-toplevel :execute)
2331
2332 ;;; Compare two elements and return if they don't match.
2333 (sb!xc:defmacro compare-elements (elt1 elt2)
2334   `(if test-not
2335        (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2336            (return nil)
2337            t)
2338        (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2339            (return nil)
2340            t)))
2341
2342 (sb!xc:defmacro search-compare-list-list (main sub)
2343   `(do ((main ,main (cdr main))
2344         (jndex start1 (1+ jndex))
2345         (sub (nthcdr start1 ,sub) (cdr sub)))
2346        ((or (null main) (null sub) (= (the fixnum end1) jndex))
2347         t)
2348      (declare (fixnum jndex))
2349      (compare-elements (car main) (car sub))))
2350
2351 (sb!xc:defmacro search-compare-list-vector (main sub)
2352   `(do ((main ,main (cdr main))
2353         (index start1 (1+ index)))
2354        ((or (null main) (= index (the fixnum end1))) t)
2355      (declare (fixnum index))
2356      (compare-elements (car main) (aref ,sub index))))
2357
2358 (sb!xc:defmacro search-compare-vector-list (main sub index)
2359   `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2360         (jndex start1 (1+ jndex))
2361         (index ,index (1+ index)))
2362        ((or (= (the fixnum end1) jndex) (null sub)) t)
2363      (declare (fixnum jndex index))
2364      (compare-elements (aref ,main index) (car sub))))
2365
2366 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2367   `(do ((index ,index (1+ index))
2368         (sub-index start1 (1+ sub-index)))
2369        ((= sub-index (the fixnum end1)) t)
2370      (declare (fixnum sub-index index))
2371      (compare-elements (aref ,main index) (aref ,sub sub-index))))
2372
2373 (sb!xc:defmacro search-compare (main-type main sub index)
2374   (if (eq main-type 'list)
2375       `(seq-dispatch ,sub
2376                      (search-compare-list-list ,main ,sub)
2377                      (search-compare-list-vector ,main ,sub))
2378       `(seq-dispatch ,sub
2379                      (search-compare-vector-list ,main ,sub ,index)
2380                      (search-compare-vector-vector ,main ,sub ,index))))
2381
2382 ) ; EVAL-WHEN
2383 \f
2384 ;;;; SEARCH
2385
2386 (eval-when (:compile-toplevel :execute)
2387
2388 (sb!xc:defmacro list-search (main sub)
2389   `(do ((main (nthcdr start2 ,main) (cdr main))
2390         (index2 start2 (1+ index2))
2391         (terminus (- (the fixnum end2)
2392                      (the fixnum (- (the fixnum end1)
2393                                     (the fixnum start1)))))
2394         (last-match ()))
2395        ((> index2 terminus) last-match)
2396      (declare (fixnum index2 terminus))
2397      (if (search-compare list main ,sub index2)
2398          (if from-end
2399              (setq last-match index2)
2400              (return index2)))))
2401
2402 (sb!xc:defmacro vector-search (main sub)
2403   `(do ((index2 start2 (1+ index2))
2404         (terminus (- (the fixnum end2)
2405                      (the fixnum (- (the fixnum end1)
2406                                     (the fixnum start1)))))
2407         (last-match ()))
2408        ((> index2 terminus) last-match)
2409      (declare (fixnum index2 terminus))
2410      (if (search-compare vector ,main ,sub index2)
2411          (if from-end
2412              (setq last-match index2)
2413              (return index2)))))
2414
2415 ) ; EVAL-WHEN
2416
2417 (defun search (sequence1 sequence2 &key from-end (test #'eql) test-not
2418                 (start1 0) end1 (start2 0) end2 key)
2419   (declare (fixnum start1 start2))
2420   (let ((end1 (or end1 (length sequence1)))
2421         (end2 (or end2 (length sequence2))))
2422     (seq-dispatch sequence2
2423                   (list-search sequence2 sequence1)
2424                   (vector-search sequence2 sequence1))))