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.
10 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; more information.
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.
19 (in-package "SB!IMPL")
23 (eval-when (:compile-toplevel)
25 (defparameter *sequence-keyword-info*
26 ;; (name default supplied-p adjustment new-type)
30 (null (1- most-positive-fixnum))
31 (fixnum (max 0 count))
32 (integer (if (minusp count)
34 (1- most-positive-fixnum))))
35 (mod #.sb!xc:most-positive-fixnum))
36 ,@(mapcan (lambda (names)
37 (destructuring-bind (start end length sequence) names
42 (if (<= 0 ,start ,length)
44 (signal-bounding-indices-bad-error ,sequence
50 (if (or (null ,end) (<= ,start ,end ,length))
51 ;; Defaulting of NIL is done inside the
52 ;; bodies, for ease of sharing with compiler
55 ;; FIXME: defend against non-number non-NIL
58 (signal-bounding-indices-bad-error ,sequence
61 '((start end length sequence)
62 (start1 end1 length1 sequence1)
63 (start2 end2 length2 sequence2)))
66 (and key (%coerce-callable-to-fun key))
70 (%coerce-callable-to-fun test)
74 (and test-not (%coerce-callable-to-fun test-not))
78 (sb!xc:defmacro define-sequence-traverser (name args &body body)
79 (multiple-value-bind (body declarations docstring)
80 (parse-body body :doc-string-allowed t)
81 (collect ((new-args) (new-declarations) (adjustments))
84 ;; FIXME: make this robust. And clean.
87 (adjustments '(length (etypecase sequence
88 (list (length sequence))
89 (vector (length sequence)))))
90 (new-declarations '(type index length)))
93 (adjustments '(length1 (etypecase sequence1
94 (list (length sequence1))
95 (vector (length sequence1)))))
96 (new-declarations '(type index length1)))
99 (adjustments '(length2 (etypecase sequence2
100 (list (length sequence2))
101 (vector (length sequence2)))))
102 (new-declarations '(type index length2)))
103 (t (let ((info (cdr (assoc arg *sequence-keyword-info*))))
105 (destructuring-bind (default supplied-p adjuster type) info
106 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
107 (adjustments `(,arg ,adjuster))
108 (new-declarations `(type ,type ,arg))))
109 (t (new-args arg)))))))
110 `(defun ,name ,(new-args)
111 ,@(when docstring (list docstring))
113 (let* (,@(adjustments))
114 (declare ,@(new-declarations))
117 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
119 ;;; FIXME: It might be worth making three cases here, LIST,
120 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
121 ;;; It tends to make code run faster but be bigger; some benchmarking
122 ;;; is needed to decide.
123 (sb!xc:defmacro seq-dispatch (sequence list-form array-form)
124 `(if (listp ,sequence)
128 (sb!xc:defmacro make-sequence-like (sequence length)
130 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
131 `(if (typep ,sequence 'list)
134 ;; This is only called from places which have already deduced
135 ;; that the SEQUENCE argument is actually a sequence. So
136 ;; this would be a candidate place for (AVER (TYPEP ,SEQUENCE
137 ;; 'VECTOR)), except that this seems to be a performance
140 :element-type (array-element-type ,sequence)))))
142 (sb!xc:defmacro bad-sequence-type-error (type-spec)
143 `(error 'simple-type-error
145 ;; FIXME: This is actually wrong, and should be something
146 ;; like (SATISFIES IS-A-VALID-SEQUENCE-TYPE-SPECIFIER-P).
147 :expected-type 'sequence
148 :format-control "~S is a bad type specifier for sequences."
149 :format-arguments (list ,type-spec)))
151 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
152 `(error 'simple-type-error
154 :expected-type (cond ((array-type-p ,type)
155 `(eql ,(car (array-type-dimensions ,type))))
156 ((type= ,type (specifier-type 'null))
160 (t (bug "weird type in S-T-L-M-ERROR")))
161 ;; FIXME: this format control causes ugly printing. There's
162 ;; probably some ~<~@:_~> incantation that would make it
163 ;; nicer. -- CSR, 2002-10-18
164 :format-control "The length requested (~S) does not match the type restriction in ~S."
165 :format-arguments (list ,length (type-specifier ,type))))
167 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
168 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
169 ;; words that give some but not total support to this position in
170 ;; ANSI. Essentially, we are justified in throwing this on
171 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
172 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
173 `(error 'simple-type-error
175 ;; FIXME: as in BAD-SEQUENCE-TYPE-ERROR, this is wrong.
176 :expected-type 'sequence
177 :format-control "~S is too hairy for sequence functions."
178 :format-arguments (list ,type-spec)))
181 ;;; It's possible with some sequence operations to declare the length
182 ;;; of a result vector, and to be safe, we really ought to verify that
183 ;;; the actual result has the declared length.
184 (defun vector-of-checked-length-given-length (vector declared-length)
185 (declare (type vector vector))
186 (declare (type index declared-length))
187 (let ((actual-length (length vector)))
188 (unless (= actual-length declared-length)
189 (error 'simple-type-error
191 :expected-type `(vector ,declared-length)
193 "Vector length (~W) doesn't match declared length (~W)."
194 :format-arguments (list actual-length declared-length))))
196 (defun sequence-of-checked-length-given-type (sequence result-type)
197 (let ((ctype (specifier-type result-type)))
198 (if (not (array-type-p ctype))
200 (let ((declared-length (first (array-type-dimensions ctype))))
201 (if (eq declared-length '*)
203 (vector-of-checked-length-given-length sequence
204 declared-length))))))
206 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
207 (defun signal-index-too-large-error (sequence index)
208 (let* ((length (length sequence))
209 (max-index (and (plusp length)
211 (error 'index-too-large-error
213 :expected-type (if max-index
214 `(integer 0 ,max-index)
215 ;; This seems silly, is there something better?
218 (defun signal-bounding-indices-bad-error (sequence start end)
219 (let ((length (length sequence)))
220 (error 'bounding-indices-bad-error
221 :datum (cons start end)
222 :expected-type `(cons (integer 0 ,length)
223 (or null (integer ,start ,length)))
226 (defun elt (sequence index)
227 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
230 (do ((count index (1- count))
231 (list sequence (cdr list)))
234 (signal-index-too-large-error sequence index)
236 (declare (type (integer 0) count))))
238 (when (>= index (length sequence))
239 (signal-index-too-large-error sequence index))
240 (aref sequence index))))
242 (defun %setelt (sequence index newval)
243 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
246 (do ((count index (1- count))
248 ((= count 0) (rplaca seq newval) newval)
249 (declare (fixnum count))
251 (signal-index-too-large-error sequence index)
252 (setq seq (cdr seq)))))
254 (when (>= index (length sequence))
255 (signal-index-too-large-error sequence index))
256 (setf (aref sequence index) newval))))
258 (defun length (sequence)
259 #!+sb-doc "Return an integer that is the length of SEQUENCE."
261 (vector (length (truly-the vector sequence)))
262 (list (length (truly-the list sequence)))))
264 (defun make-sequence (type length &key (initial-element nil iep))
266 "Return a sequence of the given TYPE and LENGTH, with elements initialized
268 (declare (fixnum length))
269 (let* ((adjusted-type
272 ((eq type 'string) '(vector character))
273 ((eq type 'simple-string) '(simple-array character (*)))
276 ((eq (car type) 'string) `(vector character ,@(cdr type)))
277 ((eq (car type) 'simple-string)
278 `(simple-array character ,@(when (cdr type)
282 (type (specifier-type adjusted-type)))
283 (cond ((csubtypep type (specifier-type 'list))
285 ((type= type (specifier-type 'list))
286 (make-list length :initial-element initial-element))
287 ((eq type *empty-type*)
288 (bad-sequence-type-error nil))
289 ((type= type (specifier-type 'null))
292 (sequence-type-length-mismatch-error type length)))
294 (multiple-value-bind (min exactp)
295 (sb!kernel::cons-type-length-info type)
297 (unless (= length min)
298 (sequence-type-length-mismatch-error type length))
299 (unless (>= length min)
300 (sequence-type-length-mismatch-error type length)))
301 (make-list length :initial-element initial-element)))
302 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
303 ;; which may seem strange and non-ideal, but then I'd say
304 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
305 (t (sequence-type-too-hairy (type-specifier type)))))
306 ((csubtypep type (specifier-type 'vector))
308 (;; is it immediately obvious what the result type is?
309 (typep type 'array-type)
311 (aver (= (length (array-type-dimensions type)) 1))
312 (let* ((etype (type-specifier
313 (array-type-specialized-element-type type)))
314 (etype (if (eq etype '*) t etype))
315 (type-length (car (array-type-dimensions type))))
316 (unless (or (eq type-length '*)
317 (= type-length length))
318 (sequence-type-length-mismatch-error type length))
319 ;; FIXME: These calls to MAKE-ARRAY can't be
320 ;; open-coded, as the :ELEMENT-TYPE argument isn't
321 ;; constant. Probably we ought to write a
322 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
325 (make-array length :element-type etype
326 :initial-element initial-element)
327 (make-array length :element-type etype)))))
328 (t (sequence-type-too-hairy (type-specifier type)))))
329 (t (bad-sequence-type-error (type-specifier type))))))
333 ;;;; The support routines for SUBSEQ are used by compiler transforms,
334 ;;;; so we worry about dealing with END being supplied or defaulting
335 ;;;; to NIL at this level.
337 (defun vector-subseq* (sequence start &optional end)
338 (declare (type vector sequence))
339 (declare (type index start))
340 (declare (type (or null index) end))
342 (setf end (length sequence)))
343 (unless (<= 0 start end (length sequence))
344 (signal-bounding-indices-bad-error sequence start end))
345 (do ((old-index start (1+ old-index))
346 (new-index 0 (1+ new-index))
347 (copy (make-sequence-like sequence (- end start))))
348 ((= old-index end) copy)
349 (declare (fixnum old-index new-index))
350 (setf (aref copy new-index)
351 (aref sequence old-index))))
353 (defun list-subseq* (sequence start &optional end)
354 (declare (type list sequence))
355 ;; the INDEX declaration isn't actually mandatory, but it's true for
356 ;; all practical purposes.
357 (declare (type index start))
358 (declare (type (or null index) end))
359 (do ((list sequence (cdr list))
364 ((null list) (if (or (and end (> end index))
366 (signal-bounding-indices-bad-error sequence start end)
367 (return (nreverse result))))
368 ((< index start) nil)
369 ((and end (= index end)) (return (nreverse result)))
370 (t (push (car list) result)))))
372 (defun subseq (sequence start &optional end)
374 "Return a copy of a subsequence of SEQUENCE starting with element number
375 START and continuing to the end of SEQUENCE or the optional END."
376 (seq-dispatch sequence
377 (list-subseq* sequence start end)
378 (vector-subseq* sequence start end)))
382 (eval-when (:compile-toplevel :execute)
384 (sb!xc:defmacro vector-copy-seq (sequence)
385 `(let ((length (length (the vector ,sequence))))
386 (declare (fixnum length))
387 (do ((index 0 (1+ index))
388 (copy (make-sequence-like ,sequence length)))
389 ((= index length) copy)
390 (declare (fixnum index))
391 (setf (aref copy index) (aref ,sequence index)))))
393 (sb!xc:defmacro list-copy-seq (list)
394 `(if (atom ,list) '()
395 (let ((result (cons (car ,list) '()) ))
396 (do ((x (cdr ,list) (cdr x))
398 (cdr (rplacd splice (cons (car x) '() ))) ))
399 ((atom x) (unless (null x)
405 (defun copy-seq (sequence)
406 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
407 (seq-dispatch sequence
408 (list-copy-seq* sequence)
409 (vector-copy-seq* sequence)))
413 (defun list-copy-seq* (sequence)
414 (list-copy-seq sequence))
416 (defun vector-copy-seq* (sequence)
417 (declare (type vector sequence))
418 (vector-copy-seq sequence))
422 (eval-when (:compile-toplevel :execute)
424 (sb!xc:defmacro vector-fill (sequence item start end)
425 `(do ((index ,start (1+ index)))
426 ((= index (the fixnum ,end)) ,sequence)
427 (declare (fixnum index))
428 (setf (aref ,sequence index) ,item)))
430 (sb!xc:defmacro list-fill (sequence item start end)
431 `(do ((current (nthcdr ,start ,sequence) (cdr current))
432 (index ,start (1+ index)))
433 ((or (atom current) (and end (= index (the fixnum ,end))))
435 (declare (fixnum index))
436 (rplaca current ,item)))
440 ;;; The support routines for FILL are used by compiler transforms, so we
441 ;;; worry about dealing with END being supplied or defaulting to NIL
444 (defun list-fill* (sequence item start end)
445 (declare (list sequence))
446 (list-fill sequence item start end))
448 (defun vector-fill* (sequence item start end)
449 (declare (vector sequence))
450 (when (null end) (setq end (length sequence)))
451 (vector-fill sequence item start end))
453 (define-sequence-traverser fill (sequence item &key start end)
454 #!+sb-doc "Replace the specified elements of SEQUENCE with ITEM."
455 (seq-dispatch sequence
456 (list-fill* sequence item start end)
457 (vector-fill* sequence item start end)))
461 (eval-when (:compile-toplevel :execute)
463 ;;; If we are copying around in the same vector, be careful not to copy the
464 ;;; same elements over repeatedly. We do this by copying backwards.
465 (sb!xc:defmacro mumble-replace-from-mumble ()
466 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
467 (let ((nelts (min (- target-end target-start)
468 (- source-end source-start))))
469 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
471 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
473 ((= target-index (the fixnum (1- target-start))) target-sequence)
474 (declare (fixnum target-index source-index))
475 (setf (aref target-sequence target-index)
476 (aref source-sequence source-index))))
477 (do ((target-index target-start (1+ target-index))
478 (source-index source-start (1+ source-index)))
479 ((or (= target-index (the fixnum target-end))
480 (= source-index (the fixnum source-end)))
482 (declare (fixnum target-index source-index))
483 (setf (aref target-sequence target-index)
484 (aref source-sequence source-index)))))
486 (sb!xc:defmacro list-replace-from-list ()
487 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
488 (let ((new-elts (subseq source-sequence source-start
489 (+ (the fixnum source-start)
491 (min (- (the fixnum target-end)
492 (the fixnum target-start))
493 (- (the fixnum source-end)
494 (the fixnum source-start))))))))
495 (do ((n new-elts (cdr n))
496 (o (nthcdr target-start target-sequence) (cdr o)))
497 ((null n) target-sequence)
499 (do ((target-index target-start (1+ target-index))
500 (source-index source-start (1+ source-index))
501 (target-sequence-ref (nthcdr target-start target-sequence)
502 (cdr target-sequence-ref))
503 (source-sequence-ref (nthcdr source-start source-sequence)
504 (cdr source-sequence-ref)))
505 ((or (= target-index (the fixnum target-end))
506 (= source-index (the fixnum source-end))
507 (null target-sequence-ref) (null source-sequence-ref))
509 (declare (fixnum target-index source-index))
510 (rplaca target-sequence-ref (car source-sequence-ref)))))
512 (sb!xc:defmacro list-replace-from-mumble ()
513 `(do ((target-index target-start (1+ target-index))
514 (source-index source-start (1+ source-index))
515 (target-sequence-ref (nthcdr target-start target-sequence)
516 (cdr target-sequence-ref)))
517 ((or (= target-index (the fixnum target-end))
518 (= source-index (the fixnum source-end))
519 (null target-sequence-ref))
521 (declare (fixnum source-index target-index))
522 (rplaca target-sequence-ref (aref source-sequence source-index))))
524 (sb!xc:defmacro mumble-replace-from-list ()
525 `(do ((target-index target-start (1+ target-index))
526 (source-index source-start (1+ source-index))
527 (source-sequence (nthcdr source-start source-sequence)
528 (cdr source-sequence)))
529 ((or (= target-index (the fixnum target-end))
530 (= source-index (the fixnum source-end))
531 (null source-sequence))
533 (declare (fixnum target-index source-index))
534 (setf (aref target-sequence target-index) (car source-sequence))))
538 ;;;; The support routines for REPLACE are used by compiler transforms, so we
539 ;;;; worry about dealing with END being supplied or defaulting to NIL
542 (defun list-replace-from-list* (target-sequence source-sequence target-start
543 target-end source-start source-end)
544 (when (null target-end) (setq target-end (length target-sequence)))
545 (when (null source-end) (setq source-end (length source-sequence)))
546 (list-replace-from-list))
548 (defun list-replace-from-vector* (target-sequence source-sequence target-start
549 target-end source-start source-end)
550 (when (null target-end) (setq target-end (length target-sequence)))
551 (when (null source-end) (setq source-end (length source-sequence)))
552 (list-replace-from-mumble))
554 (defun vector-replace-from-list* (target-sequence source-sequence target-start
555 target-end source-start source-end)
556 (when (null target-end) (setq target-end (length target-sequence)))
557 (when (null source-end) (setq source-end (length source-sequence)))
558 (mumble-replace-from-list))
560 (defun vector-replace-from-vector* (target-sequence source-sequence
561 target-start target-end source-start
563 (when (null target-end) (setq target-end (length target-sequence)))
564 (when (null source-end) (setq source-end (length source-sequence)))
565 (mumble-replace-from-mumble))
567 (define-sequence-traverser replace
568 (sequence1 sequence2 &key start1 end1 start2 end2)
570 "The target sequence is destructively modified by copying successive
571 elements into it from the source sequence."
572 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
573 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
574 ;; these things here so that legacy code gets the names it's
575 ;; expecting. We could use &AUX instead :-/.
576 (target-sequence sequence1)
577 (source-sequence sequence2)
578 (target-start start1)
579 (source-start start2)
580 (target-end (or end1 length1))
581 (source-end (or end2 length2)))
582 (seq-dispatch target-sequence
583 (seq-dispatch source-sequence
584 (list-replace-from-list)
585 (list-replace-from-mumble))
586 (seq-dispatch source-sequence
587 (mumble-replace-from-list)
588 (mumble-replace-from-mumble)))))
592 (eval-when (:compile-toplevel :execute)
594 (sb!xc:defmacro vector-reverse (sequence)
595 `(let ((length (length ,sequence)))
596 (declare (fixnum length))
597 (do ((forward-index 0 (1+ forward-index))
598 (backward-index (1- length) (1- backward-index))
599 (new-sequence (make-sequence-like sequence length)))
600 ((= forward-index length) new-sequence)
601 (declare (fixnum forward-index backward-index))
602 (setf (aref new-sequence forward-index)
603 (aref ,sequence backward-index)))))
605 (sb!xc:defmacro list-reverse-macro (sequence)
607 ((endp ,sequence) new-list)
608 (push (pop ,sequence) new-list)))
612 (defun reverse (sequence)
614 "Return a new sequence containing the same elements but in reverse order."
615 (seq-dispatch sequence
616 (list-reverse* sequence)
617 (vector-reverse* sequence)))
621 (defun list-reverse* (sequence)
622 (list-reverse-macro sequence))
624 (defun vector-reverse* (sequence)
625 (vector-reverse sequence))
629 (eval-when (:compile-toplevel :execute)
631 (sb!xc:defmacro vector-nreverse (sequence)
632 `(let ((length (length (the vector ,sequence))))
634 (do ((left-index 0 (1+ left-index))
635 (right-index (1- length) (1- right-index)))
636 ((<= right-index left-index))
637 (declare (type index left-index right-index))
638 (rotatef (aref ,sequence left-index)
639 (aref ,sequence right-index))))
642 (sb!xc:defmacro list-nreverse-macro (list)
643 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
651 (defun list-nreverse* (sequence)
652 (list-nreverse-macro sequence))
654 (defun vector-nreverse* (sequence)
655 (vector-nreverse sequence))
657 (defun nreverse (sequence)
659 "Return a sequence of the same elements in reverse order; the argument
661 (seq-dispatch sequence
662 (list-nreverse* sequence)
663 (vector-nreverse* sequence)))
667 (eval-when (:compile-toplevel :execute)
669 (sb!xc:defmacro concatenate-to-list (sequences)
670 `(let ((result (list nil)))
671 (do ((sequences ,sequences (cdr sequences))
673 ((null sequences) (cdr result))
674 (let ((sequence (car sequences)))
675 ;; FIXME: It appears to me that this and CONCATENATE-TO-MUMBLE
676 ;; could benefit from a DO-SEQUENCE macro.
677 (seq-dispatch sequence
678 (do ((sequence sequence (cdr sequence)))
681 (cdr (rplacd splice (list (car sequence))))))
682 (do ((index 0 (1+ index))
683 (length (length sequence)))
685 (declare (fixnum index length))
688 (list (aref sequence index)))))))))))
690 (sb!xc:defmacro concatenate-to-mumble (output-type-spec sequences)
691 `(do ((seqs ,sequences (cdr seqs))
695 (do ((sequences ,sequences (cdr sequences))
696 (lengths lengths (cdr lengths))
698 (result (make-sequence ,output-type-spec total-length)))
699 ((= index total-length) result)
700 (declare (fixnum index))
701 (let ((sequence (car sequences)))
702 (seq-dispatch sequence
703 (do ((sequence sequence (cdr sequence)))
705 (setf (aref result index) (car sequence))
706 (setq index (1+ index)))
707 (do ((jndex 0 (1+ jndex))
708 (this-length (car lengths)))
709 ((= jndex this-length))
710 (declare (fixnum jndex this-length))
711 (setf (aref result index)
712 (aref sequence jndex))
713 (setq index (1+ index)))))))
714 (let ((length (length (car seqs))))
715 (declare (fixnum length))
716 (setq lengths (nconc lengths (list length)))
717 (setq total-length (+ total-length length)))))
721 (defun concatenate (output-type-spec &rest sequences)
723 "Return a new sequence of all the argument sequences concatenated together
724 which shares no structure with the original argument sequences of the
725 specified OUTPUT-TYPE-SPEC."
726 (let ((type (specifier-type output-type-spec)))
728 ((csubtypep type (specifier-type 'list))
730 ((type= type (specifier-type 'list))
731 (apply #'concat-to-list* sequences))
732 ((eq type *empty-type*)
733 (bad-sequence-type-error nil))
734 ((type= type (specifier-type 'null))
735 (if (every (lambda (x) (or (null x)
736 (and (vectorp x) (= (length x) 0))))
739 (sequence-type-length-mismatch-error
741 ;; FIXME: circular list issues.
742 (reduce #'+ sequences :key #'length))))
744 (multiple-value-bind (min exactp)
745 (sb!kernel::cons-type-length-info type)
746 (let ((length (reduce #'+ sequences :key #'length)))
748 (unless (= length min)
749 (sequence-type-length-mismatch-error type length))
750 (unless (>= length min)
751 (sequence-type-length-mismatch-error type length)))
752 (apply #'concat-to-list* sequences))))
753 (t (sequence-type-too-hairy (type-specifier type)))))
754 ((csubtypep type (specifier-type 'vector))
755 (apply #'concat-to-simple* output-type-spec sequences))
757 (bad-sequence-type-error output-type-spec)))))
760 ;;; FIXME: These are weird. They're never called anywhere except in
761 ;;; CONCATENATE. It seems to me that the macros ought to just
762 ;;; be expanded directly in CONCATENATE, or in CONCATENATE-STRING
763 ;;; and CONCATENATE-LIST variants. Failing that, these ought to be local
764 ;;; functions (FLET).
765 (defun concat-to-list* (&rest sequences)
766 (concatenate-to-list sequences))
767 (defun concat-to-simple* (type &rest sequences)
768 (concatenate-to-mumble type sequences))
770 ;;;; MAP and MAP-INTO
772 ;;; helper functions to handle arity-1 subcases of MAP
773 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
774 (declaim (ftype (function (function sequence) simple-vector)
775 %map-simple-vector-arity-1))
776 (macrolet ((dosequence ((i sequence) &body body)
777 (once-only ((sequence sequence))
778 `(etypecase ,sequence
779 (list (dolist (,i ,sequence) ,@body))
780 (simple-vector (dovector (,i sequence) ,@body))
781 (vector (dovector (,i sequence) ,@body))))))
782 (defun %map-to-list-arity-1 (fun sequence)
783 (let ((reversed-result nil)
784 (really-fun (%coerce-callable-to-fun fun)))
785 (dosequence (element sequence)
786 (push (funcall really-fun element)
788 (nreverse reversed-result)))
789 (defun %map-to-simple-vector-arity-1 (fun sequence)
790 (let ((result (make-array (length sequence)))
792 (really-fun (%coerce-callable-to-fun fun)))
793 (declare (type index index))
794 (dosequence (element sequence)
795 (setf (aref result index)
796 (funcall really-fun element))
799 (defun %map-for-effect-arity-1 (fun sequence)
800 (let ((really-fun (%coerce-callable-to-fun fun)))
801 (dosequence (element sequence)
802 (funcall really-fun element)))
805 ;;; helper functions to handle arity-N subcases of MAP
807 ;;; KLUDGE: This is hairier, and larger, than need be, because we
808 ;;; don't have DYNAMIC-EXTENT. With DYNAMIC-EXTENT, we could define
809 ;;; %MAP-FOR-EFFECT, and then implement the
810 ;;; other %MAP-TO-FOO functions reasonably efficiently by passing closures to
811 ;;; %MAP-FOR-EFFECT. (DYNAMIC-EXTENT would help a little by avoiding
812 ;;; consing each closure, and would help a lot by allowing us to define
813 ;;; a closure (LAMBDA (&REST REST) <do something with (APPLY FUN REST)>)
814 ;;; with the REST list allocated with DYNAMIC-EXTENT. -- WHN 20000920
815 (macrolet (;; Execute BODY in a context where the machinery for
816 ;; UPDATED-MAP-APPLY-ARGS has been set up.
817 (with-map-state (sequences &body body)
818 `(let* ((%sequences ,sequences)
819 (%iters (mapcar (lambda (sequence)
824 (%apply-args (make-list (length %sequences))))
825 (declare (type list %sequences %iters %apply-args))
827 ;; Return a list of args to pass to APPLY for the next
828 ;; function call in the mapping, or NIL if no more function
829 ;; calls should be made (because we've reached the end of a
831 (updated-map-apply-args ()
832 '(do ((in-sequences %sequences (cdr in-sequences))
833 (in-iters %iters (cdr in-iters))
834 (in-apply-args %apply-args (cdr in-apply-args)))
837 (declare (type list in-sequences in-iters in-apply-args))
838 (let ((i (car in-iters)))
839 (declare (type (or list index) i))
841 (if (null i) ; if end of this sequence
843 (setf (car in-apply-args) (car i)
844 (car in-iters) (cdr i)))
845 (let ((v (the vector (car in-sequences))))
846 (if (>= i (length v)) ; if end of this sequence
848 (setf (car in-apply-args) (aref v i)
849 (car in-iters) (1+ i)))))))))
850 (defun %map-to-list (func sequences)
851 (declare (type function func))
852 (declare (type list sequences))
853 (with-map-state sequences
854 (loop with updated-map-apply-args
855 while (setf updated-map-apply-args (updated-map-apply-args))
856 collect (apply func updated-map-apply-args))))
857 (defun %map-to-vector (output-type-spec func sequences)
858 (declare (type function func))
859 (declare (type list sequences))
860 (let ((min-len (with-map-state sequences
861 (do ((counter 0 (1+ counter)))
862 ;; Note: Doing everything in
863 ;; UPDATED-MAP-APPLY-ARGS here is somewhat
864 ;; wasteful; we even do some extra consing.
865 ;; And stepping over every element of
866 ;; VECTORs, instead of just grabbing their
867 ;; LENGTH, is also wasteful. But it's easy
868 ;; and safe. (If you do rewrite it, please
869 ;; try to make sure that
870 ;; (MAP NIL #'F SOME-CIRCULAR-LIST #(1))
871 ;; does the right thing.)
872 ((not (updated-map-apply-args))
874 (declare (type index counter))))))
875 (declare (type index min-len))
876 (with-map-state sequences
877 (let ((result (make-sequence output-type-spec min-len))
879 (declare (type index index))
880 (loop with updated-map-apply-args
881 while (setf updated-map-apply-args (updated-map-apply-args))
883 (setf (aref result index)
884 (apply func updated-map-apply-args))
887 (defun %map-for-effect (func sequences)
888 (declare (type function func))
889 (declare (type list sequences))
890 (with-map-state sequences
891 (loop with updated-map-apply-args
892 while (setf updated-map-apply-args (updated-map-apply-args))
894 (apply func updated-map-apply-args))
897 "FUNCTION must take as many arguments as there are sequences provided.
898 The result is a sequence of type OUTPUT-TYPE-SPEC such that element I
899 is the result of applying FUNCTION to element I of each of the argument
902 ;;; %MAP is just MAP without the final just-to-be-sure check that
903 ;;; length of the output sequence matches any length specified
905 (defun %map (result-type function first-sequence &rest more-sequences)
906 (let ((really-fun (%coerce-callable-to-fun function))
907 (type (specifier-type result-type)))
908 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
909 ;; it into something which can be DEFTRANSFORMed away. (It's
910 ;; fairly important to handle this case efficiently, since
911 ;; quantifiers like SOME are transformed into this case, and since
912 ;; there's no consing overhead to dwarf our inefficiency.)
913 (if (and (null more-sequences)
915 (%map-for-effect-arity-1 really-fun first-sequence)
916 ;; Otherwise, use the industrial-strength full-generality
917 ;; approach, consing O(N-ARGS) temporary storage (which can have
918 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
919 (let ((sequences (cons first-sequence more-sequences)))
921 ((eq type *empty-type*) (%map-for-effect really-fun sequences))
922 ((csubtypep type (specifier-type 'list))
923 (%map-to-list really-fun sequences))
924 ((csubtypep type (specifier-type 'vector))
925 (%map-to-vector result-type really-fun sequences))
927 (bad-sequence-type-error result-type)))))))
929 (defun map (result-type function first-sequence &rest more-sequences)
936 ;;; KLUDGE: MAP has been rewritten substantially since the fork from
937 ;;; CMU CL in order to give reasonable performance, but this
938 ;;; implementation of MAP-INTO still has the same problems as the old
939 ;;; MAP code. Ideally, MAP-INTO should be rewritten to be efficient in
940 ;;; the same way that the corresponding cases of MAP have been
941 ;;; rewritten. Instead of doing it now, though, it's easier to wait
942 ;;; until we have DYNAMIC-EXTENT, at which time it should become
943 ;;; extremely easy to define a reasonably efficient MAP-INTO in terms
944 ;;; of (MAP NIL ..). -- WHN 20000920
945 (defun map-into (result-sequence function &rest sequences)
947 (and (arrayp result-sequence)
948 (array-has-fill-pointer-p result-sequence)))
951 (array-dimension result-sequence 0)
952 (length result-sequence))
953 (mapcar #'length sequences))))
956 (setf (fill-pointer result-sequence) len))
958 (let ((really-fun (%coerce-callable-to-fun function)))
960 (setf (elt result-sequence index)
962 (mapcar (lambda (seq) (elt seq index))
968 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
969 ;;; arbitrary sequence arguments, both in the full call case and in
970 ;;; the open code case.
971 (macrolet ((defquantifier (name found-test found-result
972 &key doc (unfound-result (not found-result)))
974 ;; KLUDGE: It would be really nice if we could simply
975 ;; do something like this
976 ;; (declaim (inline ,name))
977 ;; (defun ,name (pred first-seq &rest more-seqs)
979 ;; (flet ((map-me (&rest rest)
980 ;; (let ((pred-value (apply pred rest)))
981 ;; (,found-test pred-value
982 ;; (return-from ,name
983 ;; ,found-result)))))
984 ;; (declare (inline map-me))
985 ;; (apply #'map nil #'map-me first-seq more-seqs)
987 ;; but Python doesn't seem to be smart enough about
988 ;; inlining and APPLY to recognize that it can use
989 ;; the DEFTRANSFORM for MAP in the resulting inline
990 ;; expansion. I don't have any appetite for deep
991 ;; compiler hacking right now, so I'll just work
992 ;; around the apparent problem by using a compiler
993 ;; macro instead. -- WHN 20000410
994 (defun ,name (pred first-seq &rest more-seqs)
996 (flet ((map-me (&rest rest)
997 (let ((pred-value (apply pred rest)))
998 (,found-test pred-value
1001 (declare (inline map-me))
1002 (apply #'map nil #'map-me first-seq more-seqs)
1004 ;; KLUDGE: It would be more obviously correct -- but
1005 ;; also significantly messier -- for PRED-VALUE to be
1006 ;; a gensym. However, a private symbol really does
1007 ;; seem to be good enough; and anyway the really
1008 ;; obviously correct solution is to make Python smart
1009 ;; enough that we can use an inline function instead
1010 ;; of a compiler macro (as above). -- WHN 20000410
1012 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
1013 ;; important for performance, and it'd be good to have
1014 ;; it be visible throughout the compilation of all the
1015 ;; target SBCL code. That could be done by defining
1016 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
1017 ;; moving this DEFQUANTIFIER stuff (and perhaps other
1018 ;; inline definitions in seq.lisp as well) into a new
1019 ;; seq.lisp, and moving remaining target-only stuff
1020 ;; from the old seq.lisp into target-seq.lisp.
1021 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
1022 (let ((elements (make-gensym-list (1+ (length more-seqs))))
1023 (blockname (gensym "BLOCK")))
1024 (once-only ((pred pred))
1027 (lambda (,@elements)
1028 (let ((pred-value (funcall ,pred ,@elements)))
1029 (,',found-test pred-value
1030 (return-from ,blockname
1034 ,',unfound-result)))))))
1035 (defquantifier some when pred-value :unfound-result nil :doc
1036 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1037 possibly to those with index 1, and so on. Return the first
1038 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1039 (defquantifier every unless nil :doc
1040 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1041 possibly to those with index 1, and so on. Return NIL as soon
1042 as any invocation of PREDICATE returns NIL, or T if every invocation
1044 (defquantifier notany when nil :doc
1045 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1046 possibly to those with index 1, and so on. Return NIL as soon
1047 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1048 of any sequence is reached.")
1049 (defquantifier notevery unless t :doc
1050 "Apply PREDICATE to 0-indexed elements of the sequences, then
1051 possibly to those with index 1, and so on. Return T as soon
1052 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1057 (eval-when (:compile-toplevel :execute)
1059 (sb!xc:defmacro mumble-reduce (function
1066 `(do ((index ,start (1+ index))
1067 (value ,initial-value))
1068 ((= index (the fixnum ,end)) value)
1069 (declare (fixnum index))
1070 (setq value (funcall ,function value
1071 (apply-key ,key (,ref ,sequence index))))))
1073 (sb!xc:defmacro mumble-reduce-from-end (function
1080 `(do ((index (1- ,end) (1- index))
1081 (value ,initial-value)
1082 (terminus (1- ,start)))
1083 ((= index terminus) value)
1084 (declare (fixnum index terminus))
1085 (setq value (funcall ,function
1086 (apply-key ,key (,ref ,sequence index))
1089 (sb!xc:defmacro list-reduce (function
1096 `(let ((sequence (nthcdr ,start ,sequence)))
1097 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
1099 (sequence (if ,ivp sequence (cdr sequence))
1101 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1102 (funcall ,function value (apply-key ,key (car sequence)))))
1103 ((= count (the fixnum ,end)) value)
1104 (declare (fixnum count)))))
1106 (sb!xc:defmacro list-reduce-from-end (function
1113 `(let ((sequence (nthcdr (- (the fixnum (length ,sequence))
1115 (reverse ,sequence))))
1116 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
1118 (sequence (if ,ivp sequence (cdr sequence))
1120 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1121 (funcall ,function (apply-key ,key (car sequence)) value)))
1122 ((= count (the fixnum ,end)) value)
1123 (declare (fixnum count)))))
1127 (define-sequence-traverser reduce
1128 (function sequence &key key from-end start end (initial-value nil ivp))
1129 (declare (type index start))
1131 (end (or end length)))
1132 (declare (type index start end))
1133 (cond ((= end start)
1134 (if ivp initial-value (funcall function)))
1137 (list-reduce-from-end function sequence key start end
1139 (list-reduce function sequence key start end
1140 initial-value ivp)))
1143 (setq end (1- (the fixnum end)))
1144 (setq initial-value (apply-key key (aref sequence end))))
1145 (mumble-reduce-from-end function sequence key start end
1146 initial-value aref))
1149 (setq initial-value (apply-key key (aref sequence start)))
1150 (setq start (1+ start)))
1151 (mumble-reduce function sequence key start end
1152 initial-value aref)))))
1156 (eval-when (:compile-toplevel :execute)
1158 (sb!xc:defmacro mumble-delete (pred)
1159 `(do ((index start (1+ index))
1162 ((or (= index (the fixnum end)) (= number-zapped count))
1163 (do ((index index (1+ index)) ; Copy the rest of the vector.
1164 (jndex jndex (1+ jndex)))
1165 ((= index (the fixnum length))
1166 (shrink-vector sequence jndex))
1167 (declare (fixnum index jndex))
1168 (setf (aref sequence jndex) (aref sequence index))))
1169 (declare (fixnum index jndex number-zapped))
1170 (setf (aref sequence jndex) (aref sequence index))
1172 (incf number-zapped)
1175 (sb!xc:defmacro mumble-delete-from-end (pred)
1176 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1180 (terminus (1- start)))
1181 ((or (= index terminus) (= number-zapped count))
1182 (do ((losers losers) ; Delete the losers.
1183 (index start (1+ index))
1185 ((or (null losers) (= index (the fixnum end)))
1186 (do ((index index (1+ index)) ; Copy the rest of the vector.
1187 (jndex jndex (1+ jndex)))
1188 ((= index (the fixnum length))
1189 (shrink-vector sequence jndex))
1190 (declare (fixnum index jndex))
1191 (setf (aref sequence jndex) (aref sequence index))))
1192 (declare (fixnum index jndex))
1193 (setf (aref sequence jndex) (aref sequence index))
1194 (if (= index (the fixnum (car losers)))
1197 (declare (fixnum index number-zapped terminus))
1198 (setq this-element (aref sequence index))
1200 (incf number-zapped)
1201 (push index losers))))
1203 (sb!xc:defmacro normal-mumble-delete ()
1206 (not (funcall test-not item (apply-key key (aref sequence index))))
1207 (funcall test item (apply-key key (aref sequence index))))))
1209 (sb!xc:defmacro normal-mumble-delete-from-end ()
1210 `(mumble-delete-from-end
1212 (not (funcall test-not item (apply-key key this-element)))
1213 (funcall test item (apply-key key this-element)))))
1215 (sb!xc:defmacro list-delete (pred)
1216 `(let ((handle (cons nil sequence)))
1217 (do ((current (nthcdr start sequence) (cdr current))
1218 (previous (nthcdr start handle))
1219 (index start (1+ index))
1221 ((or (= index (the fixnum end)) (= number-zapped count))
1223 (declare (fixnum index number-zapped))
1225 (rplacd previous (cdr current))
1226 (incf number-zapped))
1228 (setq previous (cdr previous)))))))
1230 (sb!xc:defmacro list-delete-from-end (pred)
1231 `(let* ((reverse (nreverse (the list sequence)))
1232 (handle (cons nil reverse)))
1233 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1235 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1236 (index start (1+ index))
1238 ((or (= index (the fixnum end)) (= number-zapped count))
1239 (nreverse (cdr handle)))
1240 (declare (fixnum index number-zapped))
1242 (rplacd previous (cdr current))
1243 (incf number-zapped))
1245 (setq previous (cdr previous)))))))
1247 (sb!xc:defmacro normal-list-delete ()
1250 (not (funcall test-not item (apply-key key (car current))))
1251 (funcall test item (apply-key key (car current))))))
1253 (sb!xc:defmacro normal-list-delete-from-end ()
1254 '(list-delete-from-end
1256 (not (funcall test-not item (apply-key key (car current))))
1257 (funcall test item (apply-key key (car current))))))
1261 (define-sequence-traverser delete
1262 (item sequence &key from-end test test-not start
1265 "Return a sequence formed by destructively removing the specified ITEM from
1266 the given SEQUENCE."
1267 (declare (fixnum start))
1268 (let ((end (or end length)))
1269 (declare (type index end))
1270 (seq-dispatch sequence
1272 (normal-list-delete-from-end)
1273 (normal-list-delete))
1275 (normal-mumble-delete-from-end)
1276 (normal-mumble-delete)))))
1278 (eval-when (:compile-toplevel :execute)
1280 (sb!xc:defmacro if-mumble-delete ()
1282 (funcall predicate (apply-key key (aref sequence index)))))
1284 (sb!xc:defmacro if-mumble-delete-from-end ()
1285 `(mumble-delete-from-end
1286 (funcall predicate (apply-key key this-element))))
1288 (sb!xc:defmacro if-list-delete ()
1290 (funcall predicate (apply-key key (car current)))))
1292 (sb!xc:defmacro if-list-delete-from-end ()
1293 '(list-delete-from-end
1294 (funcall predicate (apply-key key (car current)))))
1298 (define-sequence-traverser delete-if
1299 (predicate sequence &key from-end start key end count)
1301 "Return a sequence formed by destructively removing the elements satisfying
1302 the specified PREDICATE from the given SEQUENCE."
1303 (declare (fixnum start))
1304 (let ((end (or end length)))
1305 (declare (type index end))
1306 (seq-dispatch sequence
1308 (if-list-delete-from-end)
1311 (if-mumble-delete-from-end)
1312 (if-mumble-delete)))))
1314 (eval-when (:compile-toplevel :execute)
1316 (sb!xc:defmacro if-not-mumble-delete ()
1318 (not (funcall predicate (apply-key key (aref sequence index))))))
1320 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1321 `(mumble-delete-from-end
1322 (not (funcall predicate (apply-key key this-element)))))
1324 (sb!xc:defmacro if-not-list-delete ()
1326 (not (funcall predicate (apply-key key (car current))))))
1328 (sb!xc:defmacro if-not-list-delete-from-end ()
1329 '(list-delete-from-end
1330 (not (funcall predicate (apply-key key (car current))))))
1334 (define-sequence-traverser delete-if-not
1335 (predicate sequence &key from-end start end key count)
1337 "Return a sequence formed by destructively removing the elements not
1338 satisfying the specified PREDICATE from the given SEQUENCE."
1339 (declare (fixnum start))
1340 (let ((end (or end length)))
1341 (declare (type index end))
1342 (seq-dispatch sequence
1344 (if-not-list-delete-from-end)
1345 (if-not-list-delete))
1347 (if-not-mumble-delete-from-end)
1348 (if-not-mumble-delete)))))
1352 (eval-when (:compile-toplevel :execute)
1354 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1355 ;;; satisfies the predicate.
1356 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1357 `(do ((index ,begin (,bump index))
1359 (do ((index ,left (,bump index))
1360 (result (make-sequence-like sequence length)))
1361 ((= index (the fixnum ,begin)) result)
1362 (declare (fixnum index))
1363 (setf (aref result index) (aref sequence index))))
1367 ((or (= index (the fixnum ,finish))
1368 (= number-zapped count))
1369 (do ((index index (,bump index))
1370 (new-index new-index (,bump new-index)))
1371 ((= index (the fixnum ,right)) (shrink-vector result new-index))
1372 (declare (fixnum index new-index))
1373 (setf (aref result new-index) (aref sequence index))))
1374 (declare (fixnum index new-index number-zapped))
1375 (setq this-element (aref sequence index))
1376 (cond (,pred (incf number-zapped))
1377 (t (setf (aref result new-index) this-element)
1378 (setq new-index (,bump new-index))))))
1380 (sb!xc:defmacro mumble-remove (pred)
1381 `(mumble-remove-macro 1+ 0 start end length ,pred))
1383 (sb!xc:defmacro mumble-remove-from-end (pred)
1384 `(let ((sequence (copy-seq sequence)))
1385 (mumble-delete-from-end ,pred)))
1387 (sb!xc:defmacro normal-mumble-remove ()
1390 (not (funcall test-not item (apply-key key this-element)))
1391 (funcall test item (apply-key key this-element)))))
1393 (sb!xc:defmacro normal-mumble-remove-from-end ()
1394 `(mumble-remove-from-end
1396 (not (funcall test-not item (apply-key key this-element)))
1397 (funcall test item (apply-key key this-element)))))
1399 (sb!xc:defmacro if-mumble-remove ()
1400 `(mumble-remove (funcall predicate (apply-key key this-element))))
1402 (sb!xc:defmacro if-mumble-remove-from-end ()
1403 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1405 (sb!xc:defmacro if-not-mumble-remove ()
1406 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1408 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1409 `(mumble-remove-from-end
1410 (not (funcall predicate (apply-key key this-element)))))
1412 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1414 (sb!xc:defmacro list-remove-macro (pred reverse?)
1415 `(let* ((sequence ,(if reverse?
1416 '(reverse (the list sequence))
1418 (%start ,(if reverse? '(- length end) 'start))
1419 (%end ,(if reverse? '(- length start) 'end))
1421 (results (do ((index 0 (1+ index))
1422 (before-start splice))
1423 ((= index (the fixnum %start)) before-start)
1424 (declare (fixnum index))
1426 (cdr (rplacd splice (list (pop sequence))))))))
1427 (do ((index %start (1+ index))
1430 ((or (= index (the fixnum %end)) (= number-zapped count))
1431 (do ((index index (1+ index)))
1434 '(nreverse (the list (cdr results)))
1436 (declare (fixnum index))
1437 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1438 (declare (fixnum index number-zapped))
1439 (setq this-element (pop sequence))
1441 (setq number-zapped (1+ number-zapped))
1442 (setq splice (cdr (rplacd splice (list this-element))))))))
1444 (sb!xc:defmacro list-remove (pred)
1445 `(list-remove-macro ,pred nil))
1447 (sb!xc:defmacro list-remove-from-end (pred)
1448 `(list-remove-macro ,pred t))
1450 (sb!xc:defmacro normal-list-remove ()
1453 (not (funcall test-not item (apply-key key this-element)))
1454 (funcall test item (apply-key key this-element)))))
1456 (sb!xc:defmacro normal-list-remove-from-end ()
1457 `(list-remove-from-end
1459 (not (funcall test-not item (apply-key key this-element)))
1460 (funcall test item (apply-key key this-element)))))
1462 (sb!xc:defmacro if-list-remove ()
1464 (funcall predicate (apply-key key this-element))))
1466 (sb!xc:defmacro if-list-remove-from-end ()
1467 `(list-remove-from-end
1468 (funcall predicate (apply-key key this-element))))
1470 (sb!xc:defmacro if-not-list-remove ()
1472 (not (funcall predicate (apply-key key this-element)))))
1474 (sb!xc:defmacro if-not-list-remove-from-end ()
1475 `(list-remove-from-end
1476 (not (funcall predicate (apply-key key this-element)))))
1480 (define-sequence-traverser remove
1481 (item sequence &key from-end test test-not start
1484 "Return a copy of SEQUENCE with elements satisfying the test (default is
1485 EQL) with ITEM removed."
1486 (declare (fixnum start))
1487 (let ((end (or end length)))
1488 (declare (type index end))
1489 (seq-dispatch sequence
1491 (normal-list-remove-from-end)
1492 (normal-list-remove))
1494 (normal-mumble-remove-from-end)
1495 (normal-mumble-remove)))))
1497 (define-sequence-traverser remove-if
1498 (predicate sequence &key from-end start end count key)
1500 "Return a copy of sequence with elements such that predicate(element)
1501 is non-null removed"
1502 (declare (fixnum start))
1503 (let ((end (or end length)))
1504 (declare (type index end))
1505 (seq-dispatch sequence
1507 (if-list-remove-from-end)
1510 (if-mumble-remove-from-end)
1511 (if-mumble-remove)))))
1513 (define-sequence-traverser remove-if-not
1514 (predicate sequence &key from-end start end count key)
1516 "Return a copy of sequence with elements such that predicate(element)
1518 (declare (fixnum start))
1519 (let ((end (or end length)))
1520 (declare (type index end))
1521 (seq-dispatch sequence
1523 (if-not-list-remove-from-end)
1524 (if-not-list-remove))
1526 (if-not-mumble-remove-from-end)
1527 (if-not-mumble-remove)))))
1529 ;;;; REMOVE-DUPLICATES
1531 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1532 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1533 ;;; if we look into the already copied structure (from after :start) and see
1534 ;;; the item. If we check from beginning we check into the rest of the
1535 ;;; original list up to the :end marker (this we have to do by running a
1536 ;;; do loop down the list that far and using our test.
1537 (defun list-remove-duplicates* (list test test-not start end key from-end)
1538 (declare (fixnum start))
1539 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1542 (do ((index 0 (1+ index)))
1544 (declare (fixnum index))
1545 (setq splice (cdr (rplacd splice (list (car current)))))
1546 (setq current (cdr current)))
1547 (do ((index 0 (1+ index)))
1548 ((or (and end (= index (the fixnum end)))
1550 (declare (fixnum index))
1551 (if (or (and from-end
1553 (member (apply-key key (car current))
1554 (nthcdr (1+ start) result)
1557 (member (apply-key key (car current))
1558 (nthcdr (1+ start) result)
1562 (not (do ((it (apply-key key (car current)))
1563 (l (cdr current) (cdr l))
1564 (i (1+ index) (1+ i)))
1565 ((or (atom l) (and end (= i (the fixnum end))))
1567 (declare (fixnum i))
1569 (not (funcall test-not
1571 (apply-key key (car l))))
1572 (funcall test it (apply-key key (car l))))
1574 (setq splice (cdr (rplacd splice (list (car current))))))
1575 (setq current (cdr current)))
1578 (setq splice (cdr (rplacd splice (list (car current)))))
1579 (setq current (cdr current)))
1582 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1583 &optional (length (length vector)))
1584 (declare (vector vector) (fixnum start length))
1585 (when (null end) (setf end (length vector)))
1586 (let ((result (make-sequence-like vector length))
1589 (declare (fixnum index jndex))
1592 (setf (aref result index) (aref vector index))
1593 (setq index (1+ index)))
1596 (setq elt (aref vector index))
1597 ;; FIXME: Relying on POSITION allowing both :TEST and :TEST-NOT
1598 ;; arguments simultaneously is a little fragile, since ANSI says
1599 ;; we can't depend on it, so we need to remember to keep that
1600 ;; extension in our implementation. It'd probably be better to
1601 ;; rewrite this to avoid passing both (as
1602 ;; LIST-REMOVE-DUPLICATES* was rewritten ca. sbcl-0.7.12.18).
1603 (unless (or (and from-end
1604 (position (apply-key key elt) result
1605 :start start :end jndex
1606 :test test :test-not test-not :key key))
1608 (position (apply-key key elt) vector
1609 :start (1+ index) :end end
1610 :test test :test-not test-not :key key)))
1611 (setf (aref result jndex) elt)
1612 (setq jndex (1+ jndex)))
1613 (setq index (1+ index)))
1616 (setf (aref result jndex) (aref vector index))
1617 (setq index (1+ index))
1618 (setq jndex (1+ jndex)))
1619 (shrink-vector result jndex)))
1621 (define-sequence-traverser remove-duplicates
1622 (sequence &key test test-not start end from-end key)
1624 "The elements of SEQUENCE are compared pairwise, and if any two match,
1625 the one occurring earlier is discarded, unless FROM-END is true, in
1626 which case the one later in the sequence is discarded. The resulting
1627 sequence is returned.
1629 The :TEST-NOT argument is deprecated."
1630 (declare (fixnum start))
1631 (seq-dispatch sequence
1633 (list-remove-duplicates* sequence test test-not
1634 start end key from-end))
1635 (vector-remove-duplicates* sequence test test-not
1636 start end key from-end)))
1638 ;;;; DELETE-DUPLICATES
1640 (defun list-delete-duplicates* (list test test-not key from-end start end)
1641 (declare (fixnum start))
1642 (let ((handle (cons nil list)))
1643 (do ((current (nthcdr start list) (cdr current))
1644 (previous (nthcdr start handle))
1645 (index start (1+ index)))
1646 ((or (and end (= index (the fixnum end))) (null current))
1648 (declare (fixnum index))
1649 (if (do ((x (if from-end
1650 (nthcdr (1+ start) handle)
1653 (i (1+ index) (1+ i)))
1655 (and (not from-end) end (= i (the fixnum end)))
1658 (declare (fixnum i))
1660 (not (funcall test-not
1661 (apply-key key (car current))
1662 (apply-key key (car x))))
1664 (apply-key key (car current))
1665 (apply-key key (car x))))
1667 (rplacd previous (cdr current))
1668 (setq previous (cdr previous))))))
1670 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1671 &optional (length (length vector)))
1672 (declare (vector vector) (fixnum start length))
1673 (when (null end) (setf end (length vector)))
1674 (do ((index start (1+ index))
1677 (do ((index index (1+ index)) ; copy the rest of the vector
1678 (jndex jndex (1+ jndex)))
1680 (shrink-vector vector jndex)
1682 (setf (aref vector jndex) (aref vector index))))
1683 (declare (fixnum index jndex))
1684 (setf (aref vector jndex) (aref vector index))
1685 (unless (position (apply-key key (aref vector index)) vector :key key
1686 :start (if from-end start (1+ index)) :test test
1687 :end (if from-end jndex end) :test-not test-not)
1688 (setq jndex (1+ jndex)))))
1690 (define-sequence-traverser delete-duplicates
1691 (sequence &key test test-not start end from-end key)
1693 "The elements of SEQUENCE are examined, and if any two match, one is
1694 discarded. The resulting sequence, which may be formed by destroying the
1695 given sequence, is returned.
1697 The :TEST-NOT argument is deprecated."
1698 (seq-dispatch sequence
1700 (list-delete-duplicates* sequence test test-not key from-end start end))
1701 (vector-delete-duplicates* sequence test test-not key from-end start end)))
1705 (defun list-substitute* (pred new list start end count key test test-not old)
1706 (declare (fixnum start end count))
1707 (let* ((result (list nil))
1710 (list list)) ; Get a local list for a stepper.
1711 (do ((index 0 (1+ index)))
1713 (declare (fixnum index))
1714 (setq splice (cdr (rplacd splice (list (car list)))))
1715 (setq list (cdr list)))
1716 (do ((index start (1+ index)))
1717 ((or (= index end) (null list) (= count 0)))
1718 (declare (fixnum index))
1719 (setq elt (car list))
1728 (funcall test-not old (apply-key key elt)))
1729 (funcall test old (apply-key key elt))))
1730 (if (funcall test (apply-key key elt)))
1731 (if-not (not (funcall test (apply-key key elt)))))
1735 (setq list (cdr list)))
1738 (setq splice (cdr (rplacd splice (list (car list)))))
1739 (setq list (cdr list)))
1742 ;;; Replace old with new in sequence moving from left to right by incrementer
1743 ;;; on each pass through the loop. Called by all three substitute functions.
1744 (defun vector-substitute* (pred new sequence incrementer left right length
1745 start end count key test test-not old)
1746 (declare (fixnum start count end incrementer right))
1747 (let ((result (make-sequence-like sequence length))
1749 (declare (fixnum index))
1752 (setf (aref result index) (aref sequence index))
1753 (setq index (+ index incrementer)))
1755 ((or (= index end) (= count 0)))
1756 (setq elt (aref sequence index))
1757 (setf (aref result index)
1761 (not (funcall test-not old (apply-key key elt)))
1762 (funcall test old (apply-key key elt))))
1763 (if (funcall test (apply-key key elt)))
1764 (if-not (not (funcall test (apply-key key elt)))))
1765 (setq count (1- count))
1768 (setq index (+ index incrementer)))
1771 (setf (aref result index) (aref sequence index))
1772 (setq index (+ index incrementer)))
1775 (eval-when (:compile-toplevel :execute)
1777 (sb!xc:defmacro subst-dispatch (pred)
1778 `(if (listp sequence)
1780 (nreverse (list-substitute* ,pred
1783 (- (the fixnum length)
1785 (- (the fixnum length)
1787 count key test test-not old))
1788 (list-substitute* ,pred
1789 new sequence start end count key test test-not
1792 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
1793 -1 length (1- (the fixnum end))
1794 (1- (the fixnum start))
1795 count key test test-not old)
1796 (vector-substitute* ,pred new sequence 1 0 length length
1797 start end count key test test-not old))))
1801 (define-sequence-traverser substitute
1802 (new old sequence &key from-end test test-not
1803 start count end key)
1805 "Return a sequence of the same kind as SEQUENCE with the same elements,
1806 except that all elements equal to OLD are replaced with NEW. See manual
1808 (declare (fixnum start))
1809 (let ((end (or end length)))
1810 (declare (type index end))
1811 (subst-dispatch 'normal)))
1813 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
1815 (define-sequence-traverser substitute-if
1816 (new pred sequence &key from-end start end count key)
1818 "Return a sequence of the same kind as SEQUENCE with the same elements
1819 except that all elements satisfying the PRED are replaced with NEW. See
1820 manual for details."
1821 (declare (fixnum start))
1822 (let ((end (or end length))
1826 (declare (type index length end))
1827 (subst-dispatch 'if)))
1829 (define-sequence-traverser substitute-if-not
1830 (new pred sequence &key from-end start end count key)
1832 "Return a sequence of the same kind as SEQUENCE with the same elements
1833 except that all elements not satisfying the PRED are replaced with NEW.
1834 See manual for details."
1835 (declare (fixnum start))
1836 (let ((end (or end length))
1840 (declare (type index length end))
1841 (subst-dispatch 'if-not)))
1845 (define-sequence-traverser nsubstitute
1846 (new old sequence &key from-end test test-not
1847 end count key start)
1849 "Return a sequence of the same kind as SEQUENCE with the same elements
1850 except that all elements equal to OLD are replaced with NEW. The SEQUENCE
1851 may be destructively modified. See manual for details."
1852 (declare (fixnum start))
1853 (let ((end (or end length)))
1854 (if (listp sequence)
1856 (let ((length (length sequence)))
1857 (nreverse (nlist-substitute*
1858 new old (nreverse (the list sequence))
1859 test test-not (- length end) (- length start)
1861 (nlist-substitute* new old sequence
1862 test test-not start end count key))
1864 (nvector-substitute* new old sequence -1
1865 test test-not (1- end) (1- start) count key)
1866 (nvector-substitute* new old sequence 1
1867 test test-not start end count key)))))
1869 (defun nlist-substitute* (new old sequence test test-not start end count key)
1870 (declare (fixnum start count end))
1871 (do ((list (nthcdr start sequence) (cdr list))
1872 (index start (1+ index)))
1873 ((or (= index end) (null list) (= count 0)) sequence)
1874 (declare (fixnum index))
1876 (not (funcall test-not old (apply-key key (car list))))
1877 (funcall test old (apply-key key (car list))))
1879 (setq count (1- count)))))
1881 (defun nvector-substitute* (new old sequence incrementer
1882 test test-not start end count key)
1883 (declare (fixnum start incrementer count end))
1884 (do ((index start (+ index incrementer)))
1885 ((or (= index end) (= count 0)) sequence)
1886 (declare (fixnum index))
1888 (not (funcall test-not
1890 (apply-key key (aref sequence index))))
1891 (funcall test old (apply-key key (aref sequence index))))
1892 (setf (aref sequence index) new)
1893 (setq count (1- count)))))
1895 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
1897 (define-sequence-traverser nsubstitute-if
1898 (new pred sequence &key from-end start end count key)
1900 "Return a sequence of the same kind as SEQUENCE with the same elements
1901 except that all elements satisfying the PRED are replaced with NEW.
1902 SEQUENCE may be destructively modified. See manual for details."
1903 (declare (fixnum start))
1904 (let ((end (or end length)))
1905 (declare (fixnum end))
1906 (if (listp sequence)
1908 (let ((length (length sequence)))
1909 (nreverse (nlist-substitute-if*
1910 new pred (nreverse (the list sequence))
1911 (- length end) (- length start) count key)))
1912 (nlist-substitute-if* new pred sequence
1913 start end count key))
1915 (nvector-substitute-if* new pred sequence -1
1916 (1- end) (1- start) count key)
1917 (nvector-substitute-if* new pred sequence 1
1918 start end count key)))))
1920 (defun nlist-substitute-if* (new test sequence start end count key)
1921 (declare (fixnum end))
1922 (do ((list (nthcdr start sequence) (cdr list))
1923 (index start (1+ index)))
1924 ((or (= index end) (null list) (= count 0)) sequence)
1925 (when (funcall test (apply-key key (car list)))
1927 (setq count (1- count)))))
1929 (defun nvector-substitute-if* (new test sequence incrementer
1930 start end count key)
1931 (do ((index start (+ index incrementer)))
1932 ((or (= index end) (= count 0)) sequence)
1933 (when (funcall test (apply-key key (aref sequence index)))
1934 (setf (aref sequence index) new)
1935 (setq count (1- count)))))
1937 (define-sequence-traverser nsubstitute-if-not
1938 (new pred sequence &key from-end start end count key)
1940 "Return a sequence of the same kind as SEQUENCE with the same elements
1941 except that all elements not satisfying the TEST are replaced with NEW.
1942 SEQUENCE may be destructively modified. See manual for details."
1943 (declare (fixnum start))
1944 (let ((end (or end length)))
1945 (declare (fixnum end))
1946 (if (listp sequence)
1948 (let ((length (length sequence)))
1949 (nreverse (nlist-substitute-if-not*
1950 new pred (nreverse (the list sequence))
1951 (- length end) (- length start) count key)))
1952 (nlist-substitute-if-not* new pred sequence
1953 start end count key))
1955 (nvector-substitute-if-not* new pred sequence -1
1956 (1- end) (1- start) count key)
1957 (nvector-substitute-if-not* new pred sequence 1
1958 start end count key)))))
1960 (defun nlist-substitute-if-not* (new test sequence start end count key)
1961 (declare (fixnum end))
1962 (do ((list (nthcdr start sequence) (cdr list))
1963 (index start (1+ index)))
1964 ((or (= index end) (null list) (= count 0)) sequence)
1965 (when (not (funcall test (apply-key key (car list))))
1969 (defun nvector-substitute-if-not* (new test sequence incrementer
1970 start end count key)
1971 (do ((index start (+ index incrementer)))
1972 ((or (= index end) (= count 0)) sequence)
1973 (when (not (funcall test (apply-key key (aref sequence index))))
1974 (setf (aref sequence index) new)
1977 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
1979 (defun effective-find-position-test (test test-not)
1980 (effective-find-position-test test test-not))
1981 (defun effective-find-position-key (key)
1982 (effective-find-position-key key))
1984 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
1985 (macrolet (;; shared logic for defining %FIND-POSITION and
1986 ;; %FIND-POSITION-IF in terms of various inlineable cases
1987 ;; of the expression defined in FROB and VECTOR*-FROB
1989 `(etypecase sequence-arg
1990 (list (frob sequence-arg from-end))
1992 (with-array-data ((sequence sequence-arg :offset-var offset)
1994 (end (%check-vector-sequence-bounds
1995 sequence-arg start end)))
1996 (multiple-value-bind (f p)
1997 (macrolet ((frob2 () '(if from-end
1999 (frob sequence nil))))
2001 (simple-vector (frob2))
2002 (simple-base-string (frob2))
2003 (t (vector*-frob sequence))))
2004 (declare (type (or index null) p))
2005 (values f (and p (the index (+ p offset))))))))))
2006 (defun %find-position (item sequence-arg from-end start end key test)
2007 (macrolet ((frob (sequence from-end)
2008 `(%find-position item ,sequence
2009 ,from-end start end key test))
2010 (vector*-frob (sequence)
2011 `(%find-position-vector-macro item ,sequence
2012 from-end start end key test)))
2014 (defun %find-position-if (predicate sequence-arg from-end start end key)
2015 (macrolet ((frob (sequence from-end)
2016 `(%find-position-if predicate ,sequence
2017 ,from-end start end key))
2018 (vector*-frob (sequence)
2019 `(%find-position-if-vector-macro predicate ,sequence
2020 from-end start end key)))
2022 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2023 (macrolet ((frob (sequence from-end)
2024 `(%find-position-if-not predicate ,sequence
2025 ,from-end start end key))
2026 (vector*-frob (sequence)
2027 `(%find-position-if-not-vector-macro predicate ,sequence
2028 from-end start end key)))
2031 ;;; the user interface to FIND and POSITION: just interpreter stubs,
2033 (defun find (item sequence &key from-end (start 0) end key test test-not)
2034 ;; FIXME: this can't be the way to go, surely?
2035 (find item sequence :from-end from-end :start start :end end :key key
2036 :test test :test-not test-not))
2037 (defun position (item sequence &key from-end (start 0) end key test test-not)
2038 (position item sequence :from-end from-end :start start :end end :key key
2039 :test test :test-not test-not))
2041 ;;; the user interface to FIND-IF and POSITION-IF, entirely analogous
2042 ;;; to the interface to FIND and POSITION
2043 (defun find-if (predicate sequence &key from-end (start 0) end key)
2044 (find-if predicate sequence :from-end from-end :start start
2046 (defun position-if (predicate sequence &key from-end (start 0) end key)
2047 (position-if predicate sequence :from-end from-end :start start
2050 (defun find-if-not (predicate sequence &key from-end (start 0) end key)
2051 (find-if-not predicate sequence :from-end from-end :start start
2053 (defun position-if-not (predicate sequence &key from-end (start 0) end key)
2054 (position-if-not predicate sequence :from-end from-end :start start
2057 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2059 (eval-when (:compile-toplevel :execute)
2061 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2062 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2063 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2064 `(let ((%start ,(if from-end-p '(1- end) 'start))
2065 (%end ,(if from-end-p '(1- start) 'end)))
2066 (do ((index %start ,next-index)
2068 ((= index (the fixnum %end)) count)
2069 (declare (fixnum index count))
2070 (,(if notp 'unless 'when) ,pred
2071 (setq count (1+ count)))))))
2073 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2074 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2075 `(let ((%start ,(if from-end-p '(- length end) 'start))
2076 (%end ,(if from-end-p '(- length start) 'end))
2077 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2078 (do ((sequence (nthcdr %start ,sequence))
2079 (index %start (1+ index))
2081 ((or (= index (the fixnum %end)) (null sequence)) count)
2082 (declare (fixnum index count))
2083 (,(if notp 'unless 'when) ,pred
2084 (setq count (1+ count)))))))
2089 (define-sequence-traverser count-if (pred sequence &key from-end start end key)
2091 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2092 (declare (fixnum start))
2093 (let ((end (or end length)))
2094 (declare (type index end))
2095 (seq-dispatch sequence
2097 (list-count-if nil t pred sequence)
2098 (list-count-if nil nil pred sequence))
2100 (vector-count-if nil t pred sequence)
2101 (vector-count-if nil nil pred sequence)))))
2103 (define-sequence-traverser count-if-not
2104 (pred sequence &key from-end start end key)
2106 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2107 (declare (fixnum start))
2108 (let ((end (or end length)))
2109 (declare (type index end))
2110 (seq-dispatch sequence
2112 (list-count-if t t pred sequence)
2113 (list-count-if t nil pred sequence))
2115 (vector-count-if t t pred sequence)
2116 (vector-count-if t nil pred sequence)))))
2118 (define-sequence-traverser count
2119 (item sequence &key from-end start end
2120 key (test #'eql test-p) (test-not nil test-not-p))
2122 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2123 which defaults to EQL."
2124 (declare (fixnum start))
2125 (when (and test-p test-not-p)
2126 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2128 (error ":TEST and :TEST-NOT are both present."))
2129 (let ((end (or end length)))
2130 (declare (type index end))
2131 (let ((%test (if test-not-p
2133 (not (funcall test-not item x)))
2135 (funcall test item x)))))
2136 (seq-dispatch sequence
2138 (list-count-if nil t %test sequence)
2139 (list-count-if nil nil %test sequence))
2141 (vector-count-if nil t %test sequence)
2142 (vector-count-if nil nil %test sequence))))))
2148 (eval-when (:compile-toplevel :execute)
2150 (sb!xc:defmacro match-vars (&rest body)
2151 `(let ((inc (if from-end -1 1))
2152 (start1 (if from-end (1- (the fixnum end1)) start1))
2153 (start2 (if from-end (1- (the fixnum end2)) start2))
2154 (end1 (if from-end (1- (the fixnum start1)) end1))
2155 (end2 (if from-end (1- (the fixnum start2)) end2)))
2156 (declare (fixnum inc start1 start2 end1 end2))
2159 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2160 (declare (ignore end)) ;; ### Should END be used below?
2161 `(let ((,sequence (if from-end
2162 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2163 (reverse (the list ,sequence)))
2164 (nthcdr ,start ,sequence))))
2165 (declare (type list ,sequence))
2170 (eval-when (:compile-toplevel :execute)
2172 (sb!xc:defmacro if-mismatch (elt1 elt2)
2173 `(cond ((= (the fixnum index1) (the fixnum end1))
2174 (return (if (= (the fixnum index2) (the fixnum end2))
2177 (1+ (the fixnum index1))
2178 (the fixnum index1)))))
2179 ((= (the fixnum index2) (the fixnum end2))
2180 (return (if from-end (1+ (the fixnum index1)) index1)))
2182 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2183 (return (if from-end (1+ (the fixnum index1)) index1))))
2184 (t (if (not (funcall test (apply-key key ,elt1)
2185 (apply-key key ,elt2)))
2186 (return (if from-end (1+ (the fixnum index1)) index1))))))
2188 (sb!xc:defmacro mumble-mumble-mismatch ()
2189 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2190 (index2 start2 (+ index2 (the fixnum inc))))
2192 (declare (fixnum index1 index2))
2193 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2195 (sb!xc:defmacro mumble-list-mismatch ()
2196 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2197 (index2 start2 (+ index2 (the fixnum inc))))
2199 (declare (fixnum index1 index2))
2200 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2202 (sb!xc:defmacro list-mumble-mismatch ()
2203 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2204 (index2 start2 (+ index2 (the fixnum inc))))
2206 (declare (fixnum index1 index2))
2207 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2209 (sb!xc:defmacro list-list-mismatch ()
2210 `(do ((sequence1 sequence1)
2211 (sequence2 sequence2)
2212 (index1 start1 (+ index1 (the fixnum inc)))
2213 (index2 start2 (+ index2 (the fixnum inc))))
2215 (declare (fixnum index1 index2))
2216 (if-mismatch (pop sequence1) (pop sequence2))))
2220 (define-sequence-traverser mismatch
2221 (sequence1 sequence2
2222 &key from-end test test-not
2223 start1 end1 start2 end2 key)
2225 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2226 element-wise. If they are of equal length and match in every element, the
2227 result is NIL. Otherwise, the result is a non-negative integer, the index
2228 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2229 if one is shorter than and a matching prefix of the other, the index within
2230 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2231 :FROM-END argument is given, then one plus the index of the rightmost
2232 position in which the sequences differ is returned."
2233 (declare (fixnum start1 start2))
2234 (let* ((end1 (or end1 length1))
2235 (end2 (or end2 length2)))
2236 (declare (type index end1 end2))
2238 (seq-dispatch sequence1
2239 (matchify-list (sequence1 start1 length1 end1)
2240 (seq-dispatch sequence2
2241 (matchify-list (sequence2 start2 length2 end2)
2242 (list-list-mismatch))
2243 (list-mumble-mismatch)))
2244 (seq-dispatch sequence2
2245 (matchify-list (sequence2 start2 length2 end2)
2246 (mumble-list-mismatch))
2247 (mumble-mumble-mismatch))))))
2249 ;;; search comparison functions
2251 (eval-when (:compile-toplevel :execute)
2253 ;;; Compare two elements and return if they don't match.
2254 (sb!xc:defmacro compare-elements (elt1 elt2)
2256 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2259 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2263 (sb!xc:defmacro search-compare-list-list (main sub)
2264 `(do ((main ,main (cdr main))
2265 (jndex start1 (1+ jndex))
2266 (sub (nthcdr start1 ,sub) (cdr sub)))
2267 ((or (endp main) (endp sub) (<= end1 jndex))
2269 (declare (type (integer 0) jndex))
2270 (compare-elements (car sub) (car main))))
2272 (sb!xc:defmacro search-compare-list-vector (main sub)
2273 `(do ((main ,main (cdr main))
2274 (index start1 (1+ index)))
2275 ((or (endp main) (= index end1)) t)
2276 (compare-elements (aref ,sub index) (car main))))
2278 (sb!xc:defmacro search-compare-vector-list (main sub index)
2279 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2280 (jndex start1 (1+ jndex))
2281 (index ,index (1+ index)))
2282 ((or (<= end1 jndex) (endp sub)) t)
2283 (declare (type (integer 0) jndex))
2284 (compare-elements (car sub) (aref ,main index))))
2286 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2287 `(do ((index ,index (1+ index))
2288 (sub-index start1 (1+ sub-index)))
2289 ((= sub-index end1) t)
2290 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2292 (sb!xc:defmacro search-compare (main-type main sub index)
2293 (if (eq main-type 'list)
2295 (search-compare-list-list ,main ,sub)
2296 (search-compare-list-vector ,main ,sub))
2298 (search-compare-vector-list ,main ,sub ,index)
2299 (search-compare-vector-vector ,main ,sub ,index))))
2305 (eval-when (:compile-toplevel :execute)
2307 (sb!xc:defmacro list-search (main sub)
2308 `(do ((main (nthcdr start2 ,main) (cdr main))
2309 (index2 start2 (1+ index2))
2310 (terminus (- end2 (the (integer 0) (- end1 start1))))
2312 ((> index2 terminus) last-match)
2313 (declare (type (integer 0) index2))
2314 (if (search-compare list main ,sub index2)
2316 (setq last-match index2)
2319 (sb!xc:defmacro vector-search (main sub)
2320 `(do ((index2 start2 (1+ index2))
2321 (terminus (- end2 (the (integer 0) (- end1 start1))))
2323 ((> index2 terminus) last-match)
2324 (declare (type (integer 0) index2))
2325 (if (search-compare vector ,main ,sub index2)
2327 (setq last-match index2)
2332 (define-sequence-traverser search
2333 (sequence1 sequence2
2334 &key from-end test test-not
2335 start1 end1 start2 end2 key)
2336 (declare (fixnum start1 start2))
2337 (let ((end1 (or end1 length1))
2338 (end2 (or end2 length2)))
2339 (seq-dispatch sequence2
2340 (list-search sequence2 sequence1)
2341 (vector-search sequence2 sequence1))))