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 (sb!xc:defmacro define-sequence-traverser (name args &body body)
67 (multiple-value-bind (body declarations docstring)
69 (collect ((new-args) (new-declarations) (adjustments))
72 ;; FIXME: make this robust. And clean.
75 (adjustments '(length (etypecase sequence
76 (list (length sequence))
77 (vector (length sequence)))))
78 (new-declarations '(type index length)))
81 (adjustments '(length1 (etypecase sequence1
82 (list (length sequence1))
83 (vector (length sequence1)))))
84 (new-declarations '(type index length1)))
87 (adjustments '(length2 (etypecase sequence2
88 (list (length sequence2))
89 (vector (length sequence2)))))
90 (new-declarations '(type index length2)))
91 (t (let ((info (cdr (assoc arg *sequence-keyword-info*))))
93 (destructuring-bind (default supplied-p adjuster type) info
94 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
95 (adjustments `(,arg ,adjuster))
96 (new-declarations `(type ,type ,arg))))
97 (t (new-args arg)))))))
98 `(defun ,name ,(new-args)
99 ,@(when docstring (list docstring))
101 (let* (,@(adjustments))
102 (declare ,@(new-declarations))
105 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
107 ;;; FIXME: It might be worth making three cases here, LIST,
108 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
109 ;;; It tends to make code run faster but be bigger; some benchmarking
110 ;;; is needed to decide.
111 (sb!xc:defmacro seq-dispatch (sequence list-form array-form)
112 `(if (listp ,sequence)
116 (sb!xc:defmacro make-sequence-like (sequence length)
118 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
119 `(if (typep ,sequence 'list)
122 ;; This is only called from places which have already deduced
123 ;; that the SEQUENCE argument is actually a sequence. So
124 ;; this would be a candidate place for (AVER (TYPEP ,SEQUENCE
125 ;; 'VECTOR)), except that this seems to be a performance
128 :element-type (array-element-type ,sequence)))))
130 (sb!xc:defmacro bad-sequence-type-error (type-spec)
131 `(error 'simple-type-error
133 ;; FIXME: This is actually wrong, and should be something
134 ;; like (SATISFIES IS-A-VALID-SEQUENCE-TYPE-SPECIFIER-P).
135 :expected-type 'sequence
136 :format-control "~S is a bad type specifier for sequences."
137 :format-arguments (list ,type-spec)))
139 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
140 `(error 'simple-type-error
142 :expected-type (cond ((array-type-p ,type)
143 `(eql ,(car (array-type-dimensions ,type))))
144 ((type= ,type (specifier-type 'null))
148 (t (bug "weird type in S-T-L-M-ERROR")))
149 ;; FIXME: this format control causes ugly printing. There's
150 ;; probably some ~<~@:_~> incantation that would make it
151 ;; nicer. -- CSR, 2002-10-18
152 :format-control "The length requested (~S) does not match the type restriction in ~S."
153 :format-arguments (list ,length (type-specifier ,type))))
155 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
156 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
157 ;; words that give some but not total support to this position in
158 ;; ANSI. Essentially, we are justified in throwing this on
159 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
160 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
161 `(error 'simple-type-error
163 ;; FIXME: as in BAD-SEQUENCE-TYPE-ERROR, this is wrong.
164 :expected-type 'sequence
165 :format-control "~S is too hairy for sequence functions."
166 :format-arguments (list ,type-spec)))
169 ;;; It's possible with some sequence operations to declare the length
170 ;;; of a result vector, and to be safe, we really ought to verify that
171 ;;; the actual result has the declared length.
172 (defun vector-of-checked-length-given-length (vector declared-length)
173 (declare (type vector vector))
174 (declare (type index declared-length))
175 (let ((actual-length (length vector)))
176 (unless (= actual-length declared-length)
177 (error 'simple-type-error
179 :expected-type `(vector ,declared-length)
181 "Vector length (~W) doesn't match declared length (~W)."
182 :format-arguments (list actual-length declared-length))))
184 (defun sequence-of-checked-length-given-type (sequence result-type)
185 (let ((ctype (specifier-type result-type)))
186 (if (not (array-type-p ctype))
188 (let ((declared-length (first (array-type-dimensions ctype))))
189 (if (eq declared-length '*)
191 (vector-of-checked-length-given-length sequence
192 declared-length))))))
194 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
195 (defun signal-index-too-large-error (sequence index)
196 (let* ((length (length sequence))
197 (max-index (and (plusp length)
199 (error 'index-too-large-error
201 :expected-type (if max-index
202 `(integer 0 ,max-index)
203 ;; This seems silly, is there something better?
206 (declaim (ftype (function (sequence index index) nil)
207 signal-bounding-indices-bad-error))
208 (defun signal-bounding-indices-bad-error (sequence start end)
209 (let ((length (length sequence)))
210 (error 'bounding-indices-bad-error
211 :datum (cons start end)
212 :expected-type `(cons (integer 0 ,length)
213 (or null (integer ,start ,length)))
216 (defun elt (sequence index)
217 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
220 (do ((count index (1- count))
221 (list sequence (cdr list)))
224 (signal-index-too-large-error sequence index)
226 (declare (type (integer 0) count))))
228 (when (>= index (length sequence))
229 (signal-index-too-large-error sequence index))
230 (aref sequence index))))
232 (defun %setelt (sequence index newval)
233 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
236 (do ((count index (1- count))
238 ((= count 0) (rplaca seq newval) newval)
239 (declare (fixnum count))
241 (signal-index-too-large-error sequence index)
242 (setq seq (cdr seq)))))
244 (when (>= index (length sequence))
245 (signal-index-too-large-error sequence index))
246 (setf (aref sequence index) newval))))
248 (defun length (sequence)
249 #!+sb-doc "Return an integer that is the length of SEQUENCE."
251 (vector (length (truly-the vector sequence)))
252 (list (length (truly-the list sequence)))))
254 (defun make-sequence (type length &key (initial-element nil iep))
256 "Return a sequence of the given TYPE and LENGTH, with elements initialized
257 to :INITIAL-ELEMENT."
258 (declare (fixnum length))
259 (let ((type (specifier-type type)))
260 (cond ((csubtypep type (specifier-type 'list))
262 ((type= type (specifier-type 'list))
263 (make-list length :initial-element initial-element))
264 ((eq type *empty-type*)
265 (bad-sequence-type-error nil))
266 ((type= type (specifier-type 'null))
269 (sequence-type-length-mismatch-error type length)))
270 ((csubtypep (specifier-type '(cons nil t)) type)
271 ;; The above is quite a neat way of finding out if
272 ;; there's a type restriction on the CDR of the
273 ;; CONS... if there is, I think it's probably fair to
274 ;; give up; if there isn't, then the list to be made
275 ;; must have a length of more than 0.
277 (make-list length :initial-element initial-element)
278 (sequence-type-length-mismatch-error type length)))
279 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
280 ;; which may seem strange and non-ideal, but then I'd say
281 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
282 (t (sequence-type-too-hairy (type-specifier type)))))
283 ((csubtypep type (specifier-type 'vector))
284 (if (typep type 'array-type)
285 ;; KLUDGE: the above test essentially asks "Do we know
286 ;; what the upgraded-array-element-type is?" [consider
287 ;; (OR STRING BIT-VECTOR)]
289 (aver (= (length (array-type-dimensions type)) 1))
290 (let ((etype (type-specifier
291 (array-type-specialized-element-type type)))
292 (type-length (car (array-type-dimensions type))))
293 (unless (or (eq type-length '*)
294 (= type-length length))
295 (sequence-type-length-mismatch-error type length))
296 ;; FIXME: These calls to MAKE-ARRAY can't be
297 ;; open-coded, as the :ELEMENT-TYPE argument isn't
298 ;; constant. Probably we ought to write a
299 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
302 (make-array length :element-type etype
303 :initial-element initial-element)
304 (make-array length :element-type etype))))
305 (sequence-type-too-hairy (type-specifier type))))
306 (t (bad-sequence-type-error (type-specifier type))))))
310 ;;;; The support routines for SUBSEQ are used by compiler transforms,
311 ;;;; so we worry about dealing with END being supplied or defaulting
312 ;;;; to NIL at this level.
314 (defun vector-subseq* (sequence start &optional end)
315 (declare (type vector sequence))
316 (declare (type index start))
317 (declare (type (or null index) end))
319 (setf end (length sequence)))
320 (unless (<= 0 start end (length sequence))
321 (signal-bounding-indices-bad-error sequence start end))
322 (do ((old-index start (1+ old-index))
323 (new-index 0 (1+ new-index))
324 (copy (make-sequence-like sequence (- end start))))
325 ((= old-index end) copy)
326 (declare (fixnum old-index new-index))
327 (setf (aref copy new-index)
328 (aref sequence old-index))))
330 (defun list-subseq* (sequence start &optional end)
331 (declare (type list sequence))
332 ;; the INDEX declaration isn't actually mandatory, but it's true for
333 ;; all practical purposes.
334 (declare (type index start))
335 (declare (type (or null index) end))
336 (do ((list sequence (cdr list))
341 ((null list) (if (or (and end (> end index))
343 (signal-bounding-indices-bad-error sequence start end)
344 (return (nreverse result))))
345 ((< index start) nil)
346 ((and end (= index end)) (return (nreverse result)))
347 (t (push (car list) result)))))
349 (defun subseq (sequence start &optional end)
351 "Return a copy of a subsequence of SEQUENCE starting with element number
352 START and continuing to the end of SEQUENCE or the optional END."
353 (seq-dispatch sequence
354 (list-subseq* sequence start end)
355 (vector-subseq* sequence start end)))
359 (eval-when (:compile-toplevel :execute)
361 (sb!xc:defmacro vector-copy-seq (sequence)
362 `(let ((length (length (the vector ,sequence))))
363 (declare (fixnum length))
364 (do ((index 0 (1+ index))
365 (copy (make-sequence-like ,sequence length)))
366 ((= index length) copy)
367 (declare (fixnum index))
368 (setf (aref copy index) (aref ,sequence index)))))
370 (sb!xc:defmacro list-copy-seq (list)
371 `(if (atom ,list) '()
372 (let ((result (cons (car ,list) '()) ))
373 (do ((x (cdr ,list) (cdr x))
375 (cdr (rplacd splice (cons (car x) '() ))) ))
376 ((atom x) (unless (null x)
382 (defun copy-seq (sequence)
383 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
384 (seq-dispatch sequence
385 (list-copy-seq* sequence)
386 (vector-copy-seq* sequence)))
390 (defun list-copy-seq* (sequence)
391 (list-copy-seq sequence))
393 (defun vector-copy-seq* (sequence)
394 (declare (type vector sequence))
395 (vector-copy-seq sequence))
399 (eval-when (:compile-toplevel :execute)
401 (sb!xc:defmacro vector-fill (sequence item start end)
402 `(do ((index ,start (1+ index)))
403 ((= index (the fixnum ,end)) ,sequence)
404 (declare (fixnum index))
405 (setf (aref ,sequence index) ,item)))
407 (sb!xc:defmacro list-fill (sequence item start end)
408 `(do ((current (nthcdr ,start ,sequence) (cdr current))
409 (index ,start (1+ index)))
410 ((or (atom current) (and end (= index (the fixnum ,end))))
412 (declare (fixnum index))
413 (rplaca current ,item)))
417 ;;; The support routines for FILL are used by compiler transforms, so we
418 ;;; worry about dealing with END being supplied or defaulting to NIL
421 (defun list-fill* (sequence item start end)
422 (declare (list sequence))
423 (list-fill sequence item start end))
425 (defun vector-fill* (sequence item start end)
426 (declare (vector sequence))
427 (when (null end) (setq end (length sequence)))
428 (vector-fill sequence item start end))
430 (define-sequence-traverser fill (sequence item &key start end)
431 #!+sb-doc "Replace the specified elements of SEQUENCE with ITEM."
432 (seq-dispatch sequence
433 (list-fill* sequence item start end)
434 (vector-fill* sequence item start end)))
438 (eval-when (:compile-toplevel :execute)
440 ;;; If we are copying around in the same vector, be careful not to copy the
441 ;;; same elements over repeatedly. We do this by copying backwards.
442 (sb!xc:defmacro mumble-replace-from-mumble ()
443 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
444 (let ((nelts (min (- target-end target-start)
445 (- source-end source-start))))
446 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
448 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
450 ((= target-index (the fixnum (1- target-start))) target-sequence)
451 (declare (fixnum target-index source-index))
452 (setf (aref target-sequence target-index)
453 (aref source-sequence source-index))))
454 (do ((target-index target-start (1+ target-index))
455 (source-index source-start (1+ source-index)))
456 ((or (= target-index (the fixnum target-end))
457 (= source-index (the fixnum source-end)))
459 (declare (fixnum target-index source-index))
460 (setf (aref target-sequence target-index)
461 (aref source-sequence source-index)))))
463 (sb!xc:defmacro list-replace-from-list ()
464 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
465 (let ((new-elts (subseq source-sequence source-start
466 (+ (the fixnum source-start)
468 (min (- (the fixnum target-end)
469 (the fixnum target-start))
470 (- (the fixnum source-end)
471 (the fixnum source-start))))))))
472 (do ((n new-elts (cdr n))
473 (o (nthcdr target-start target-sequence) (cdr o)))
474 ((null n) target-sequence)
476 (do ((target-index target-start (1+ target-index))
477 (source-index source-start (1+ source-index))
478 (target-sequence-ref (nthcdr target-start target-sequence)
479 (cdr target-sequence-ref))
480 (source-sequence-ref (nthcdr source-start source-sequence)
481 (cdr source-sequence-ref)))
482 ((or (= target-index (the fixnum target-end))
483 (= source-index (the fixnum source-end))
484 (null target-sequence-ref) (null source-sequence-ref))
486 (declare (fixnum target-index source-index))
487 (rplaca target-sequence-ref (car source-sequence-ref)))))
489 (sb!xc:defmacro list-replace-from-mumble ()
490 `(do ((target-index target-start (1+ target-index))
491 (source-index source-start (1+ source-index))
492 (target-sequence-ref (nthcdr target-start target-sequence)
493 (cdr target-sequence-ref)))
494 ((or (= target-index (the fixnum target-end))
495 (= source-index (the fixnum source-end))
496 (null target-sequence-ref))
498 (declare (fixnum source-index target-index))
499 (rplaca target-sequence-ref (aref source-sequence source-index))))
501 (sb!xc:defmacro mumble-replace-from-list ()
502 `(do ((target-index target-start (1+ target-index))
503 (source-index source-start (1+ source-index))
504 (source-sequence (nthcdr source-start source-sequence)
505 (cdr source-sequence)))
506 ((or (= target-index (the fixnum target-end))
507 (= source-index (the fixnum source-end))
508 (null source-sequence))
510 (declare (fixnum target-index source-index))
511 (setf (aref target-sequence target-index) (car source-sequence))))
515 ;;;; The support routines for REPLACE are used by compiler transforms, so we
516 ;;;; worry about dealing with END being supplied or defaulting to NIL
519 (defun list-replace-from-list* (target-sequence source-sequence target-start
520 target-end source-start source-end)
521 (when (null target-end) (setq target-end (length target-sequence)))
522 (when (null source-end) (setq source-end (length source-sequence)))
523 (list-replace-from-list))
525 (defun list-replace-from-vector* (target-sequence source-sequence target-start
526 target-end source-start source-end)
527 (when (null target-end) (setq target-end (length target-sequence)))
528 (when (null source-end) (setq source-end (length source-sequence)))
529 (list-replace-from-mumble))
531 (defun vector-replace-from-list* (target-sequence source-sequence target-start
532 target-end source-start source-end)
533 (when (null target-end) (setq target-end (length target-sequence)))
534 (when (null source-end) (setq source-end (length source-sequence)))
535 (mumble-replace-from-list))
537 (defun vector-replace-from-vector* (target-sequence source-sequence
538 target-start target-end source-start
540 (when (null target-end) (setq target-end (length target-sequence)))
541 (when (null source-end) (setq source-end (length source-sequence)))
542 (mumble-replace-from-mumble))
544 (define-sequence-traverser replace
545 (sequence1 sequence2 &key start1 end1 start2 end2)
547 "The target sequence is destructively modified by copying successive
548 elements into it from the source sequence."
549 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
550 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
551 ;; these things here so that legacy code gets the names it's
552 ;; expecting. We could use &AUX instead :-/.
553 (target-sequence sequence1)
554 (source-sequence sequence2)
555 (target-start start1)
556 (source-start start2)
557 (target-end (or end1 length1))
558 (source-end (or end2 length2)))
559 (seq-dispatch target-sequence
560 (seq-dispatch source-sequence
561 (list-replace-from-list)
562 (list-replace-from-mumble))
563 (seq-dispatch source-sequence
564 (mumble-replace-from-list)
565 (mumble-replace-from-mumble)))))
569 (eval-when (:compile-toplevel :execute)
571 (sb!xc:defmacro vector-reverse (sequence)
572 `(let ((length (length ,sequence)))
573 (declare (fixnum length))
574 (do ((forward-index 0 (1+ forward-index))
575 (backward-index (1- length) (1- backward-index))
576 (new-sequence (make-sequence-like sequence length)))
577 ((= forward-index length) new-sequence)
578 (declare (fixnum forward-index backward-index))
579 (setf (aref new-sequence forward-index)
580 (aref ,sequence backward-index)))))
582 (sb!xc:defmacro list-reverse-macro (sequence)
584 ((atom ,sequence) new-list)
585 (push (pop ,sequence) new-list)))
589 (defun reverse (sequence)
591 "Return a new sequence containing the same elements but in reverse order."
592 (seq-dispatch sequence
593 (list-reverse* sequence)
594 (vector-reverse* sequence)))
598 (defun list-reverse* (sequence)
599 (list-reverse-macro sequence))
601 (defun vector-reverse* (sequence)
602 (vector-reverse sequence))
606 (eval-when (:compile-toplevel :execute)
608 (sb!xc:defmacro vector-nreverse (sequence)
609 `(let ((length (length (the vector ,sequence))))
610 (declare (fixnum length))
611 (do ((left-index 0 (1+ left-index))
612 (right-index (1- length) (1- right-index))
613 (half-length (truncate length 2)))
614 ((= left-index half-length) ,sequence)
615 (declare (fixnum left-index right-index half-length))
616 (rotatef (aref ,sequence left-index)
617 (aref ,sequence right-index)))))
619 (sb!xc:defmacro list-nreverse-macro (list)
620 `(do ((1st (cdr ,list) (if (atom 1st) 1st (cdr 1st)))
628 (defun list-nreverse* (sequence)
629 (list-nreverse-macro sequence))
631 (defun vector-nreverse* (sequence)
632 (vector-nreverse sequence))
634 (defun nreverse (sequence)
636 "Return a sequence of the same elements in reverse order; the argument
638 (seq-dispatch sequence
639 (list-nreverse* sequence)
640 (vector-nreverse* sequence)))
644 (eval-when (:compile-toplevel :execute)
646 (sb!xc:defmacro concatenate-to-list (sequences)
647 `(let ((result (list nil)))
648 (do ((sequences ,sequences (cdr sequences))
650 ((null sequences) (cdr result))
651 (let ((sequence (car sequences)))
652 ;; FIXME: It appears to me that this and CONCATENATE-TO-MUMBLE
653 ;; could benefit from a DO-SEQUENCE macro.
654 (seq-dispatch sequence
655 (do ((sequence sequence (cdr sequence)))
658 (cdr (rplacd splice (list (car sequence))))))
659 (do ((index 0 (1+ index))
660 (length (length sequence)))
662 (declare (fixnum index length))
665 (list (aref sequence index)))))))))))
667 (sb!xc:defmacro concatenate-to-mumble (output-type-spec sequences)
668 `(do ((seqs ,sequences (cdr seqs))
672 (do ((sequences ,sequences (cdr sequences))
673 (lengths lengths (cdr lengths))
675 (result (make-sequence ,output-type-spec total-length)))
676 ((= index total-length) result)
677 (declare (fixnum index))
678 (let ((sequence (car sequences)))
679 (seq-dispatch sequence
680 (do ((sequence sequence (cdr sequence)))
682 (setf (aref result index) (car sequence))
683 (setq index (1+ index)))
684 (do ((jndex 0 (1+ jndex))
685 (this-length (car lengths)))
686 ((= jndex this-length))
687 (declare (fixnum jndex this-length))
688 (setf (aref result index)
689 (aref sequence jndex))
690 (setq index (1+ index)))))))
691 (let ((length (length (car seqs))))
692 (declare (fixnum length))
693 (setq lengths (nconc lengths (list length)))
694 (setq total-length (+ total-length length)))))
698 (defun concatenate (output-type-spec &rest sequences)
700 "Return a new sequence of all the argument sequences concatenated together
701 which shares no structure with the original argument sequences of the
702 specified OUTPUT-TYPE-SPEC."
703 (let ((type (specifier-type output-type-spec)))
705 ((csubtypep type (specifier-type 'list))
707 ((type= type (specifier-type 'list))
708 (apply #'concat-to-list* sequences))
709 ((eq type *empty-type*)
710 (bad-sequence-type-error nil))
711 ((type= type (specifier-type 'null))
712 (if (every (lambda (x) (or (null x)
713 (and (vectorp x) (= (length x) 0))))
716 (sequence-type-length-mismatch-error type
723 ((csubtypep (specifier-type '(cons nil t)) type)
724 (if (notevery (lambda (x) (or (null x)
725 (and (vectorp x) (= (length x) 0))))
727 (apply #'concat-to-list* sequences)
728 (sequence-type-length-mismatch-error type 0)))
729 (t (sequence-type-too-hairy (type-specifier type)))))
730 ((csubtypep type (specifier-type 'vector))
731 (apply #'concat-to-simple* output-type-spec sequences))
733 (bad-sequence-type-error output-type-spec)))))
736 ;;; FIXME: These are weird. They're never called anywhere except in
737 ;;; CONCATENATE. It seems to me that the macros ought to just
738 ;;; be expanded directly in CONCATENATE, or in CONCATENATE-STRING
739 ;;; and CONCATENATE-LIST variants. Failing that, these ought to be local
740 ;;; functions (FLET).
741 (defun concat-to-list* (&rest sequences)
742 (concatenate-to-list sequences))
743 (defun concat-to-simple* (type &rest sequences)
744 (concatenate-to-mumble type sequences))
746 ;;;; MAP and MAP-INTO
748 ;;; helper functions to handle arity-1 subcases of MAP
749 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
750 (declaim (ftype (function (function sequence) simple-vector)
751 %map-simple-vector-arity-1))
752 (macrolet ((dosequence ((i sequence) &body body)
753 (once-only ((sequence sequence))
754 `(etypecase ,sequence
755 (list (dolist (,i ,sequence) ,@body))
756 (simple-vector (dovector (,i sequence) ,@body))
757 (vector (dovector (,i sequence) ,@body))))))
758 (defun %map-to-list-arity-1 (fun sequence)
759 (let ((reversed-result nil)
760 (really-fun (%coerce-callable-to-fun fun)))
761 (dosequence (element sequence)
762 (push (funcall really-fun element)
764 (nreverse reversed-result)))
765 (defun %map-to-simple-vector-arity-1 (fun sequence)
766 (let ((result (make-array (length sequence)))
768 (really-fun (%coerce-callable-to-fun fun)))
769 (declare (type index index))
770 (dosequence (element sequence)
771 (setf (aref result index)
772 (funcall really-fun element))
775 (defun %map-for-effect-arity-1 (fun sequence)
776 (let ((really-fun (%coerce-callable-to-fun fun)))
777 (dosequence (element sequence)
778 (funcall really-fun element)))
781 ;;; helper functions to handle arity-N subcases of MAP
783 ;;; KLUDGE: This is hairier, and larger, than need be, because we
784 ;;; don't have DYNAMIC-EXTENT. With DYNAMIC-EXTENT, we could define
785 ;;; %MAP-FOR-EFFECT, and then implement the
786 ;;; other %MAP-TO-FOO functions reasonably efficiently by passing closures to
787 ;;; %MAP-FOR-EFFECT. (DYNAMIC-EXTENT would help a little by avoiding
788 ;;; consing each closure, and would help a lot by allowing us to define
789 ;;; a closure (LAMBDA (&REST REST) <do something with (APPLY FUN REST)>)
790 ;;; with the REST list allocated with DYNAMIC-EXTENT. -- WHN 20000920
791 (macrolet (;; Execute BODY in a context where the machinery for
792 ;; UPDATED-MAP-APPLY-ARGS has been set up.
793 (with-map-state (sequences &body body)
794 `(let* ((%sequences ,sequences)
795 (%iters (mapcar (lambda (sequence)
800 (%apply-args (make-list (length %sequences))))
801 (declare (type list %sequences %iters %apply-args))
803 ;; Return a list of args to pass to APPLY for the next
804 ;; function call in the mapping, or NIL if no more function
805 ;; calls should be made (because we've reached the end of a
807 (updated-map-apply-args ()
808 '(do ((in-sequences %sequences (cdr in-sequences))
809 (in-iters %iters (cdr in-iters))
810 (in-apply-args %apply-args (cdr in-apply-args)))
813 (declare (type list in-sequences in-iters in-apply-args))
814 (let ((i (car in-iters)))
815 (declare (type (or list index) i))
817 (if (null i) ; if end of this sequence
819 (setf (car in-apply-args) (car i)
820 (car in-iters) (cdr i)))
821 (let ((v (the vector (car in-sequences))))
822 (if (>= i (length v)) ; if end of this sequence
824 (setf (car in-apply-args) (aref v i)
825 (car in-iters) (1+ i)))))))))
826 (defun %map-to-list (func sequences)
827 (declare (type function func))
828 (declare (type list sequences))
829 (with-map-state sequences
830 (loop with updated-map-apply-args
831 while (setf updated-map-apply-args (updated-map-apply-args))
832 collect (apply func updated-map-apply-args))))
833 (defun %map-to-vector (output-type-spec func sequences)
834 (declare (type function func))
835 (declare (type list sequences))
836 (let ((min-len (with-map-state sequences
837 (do ((counter 0 (1+ counter)))
838 ;; Note: Doing everything in
839 ;; UPDATED-MAP-APPLY-ARGS here is somewhat
840 ;; wasteful; we even do some extra consing.
841 ;; And stepping over every element of
842 ;; VECTORs, instead of just grabbing their
843 ;; LENGTH, is also wasteful. But it's easy
844 ;; and safe. (If you do rewrite it, please
845 ;; try to make sure that
846 ;; (MAP NIL #'F SOME-CIRCULAR-LIST #(1))
847 ;; does the right thing.)
848 ((not (updated-map-apply-args))
850 (declare (type index counter))))))
851 (declare (type index min-len))
852 (with-map-state sequences
853 (let ((result (make-sequence output-type-spec min-len))
855 (declare (type index index))
856 (loop with updated-map-apply-args
857 while (setf updated-map-apply-args (updated-map-apply-args))
859 (setf (aref result index)
860 (apply func updated-map-apply-args))
863 (defun %map-for-effect (func sequences)
864 (declare (type function func))
865 (declare (type list sequences))
866 (with-map-state sequences
867 (loop with updated-map-apply-args
868 while (setf updated-map-apply-args (updated-map-apply-args))
870 (apply func updated-map-apply-args))
873 "FUNCTION must take as many arguments as there are sequences provided.
874 The result is a sequence of type OUTPUT-TYPE-SPEC such that element I
875 is the result of applying FUNCTION to element I of each of the argument
878 ;;; %MAP is just MAP without the final just-to-be-sure check that
879 ;;; length of the output sequence matches any length specified
881 (defun %map (result-type function first-sequence &rest more-sequences)
882 (let ((really-fun (%coerce-callable-to-fun function))
883 (type (specifier-type result-type)))
884 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
885 ;; it into something which can be DEFTRANSFORMed away. (It's
886 ;; fairly important to handle this case efficiently, since
887 ;; quantifiers like SOME are transformed into this case, and since
888 ;; there's no consing overhead to dwarf our inefficiency.)
889 (if (and (null more-sequences)
891 (%map-for-effect-arity-1 really-fun first-sequence)
892 ;; Otherwise, use the industrial-strength full-generality
893 ;; approach, consing O(N-ARGS) temporary storage (which can have
894 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
895 (let ((sequences (cons first-sequence more-sequences)))
897 ((eq type *empty-type*) (%map-for-effect really-fun sequences))
898 ((csubtypep type (specifier-type 'list))
899 (%map-to-list really-fun sequences))
900 ((csubtypep type (specifier-type 'vector))
901 (%map-to-vector result-type really-fun sequences))
903 (bad-sequence-type-error result-type)))))))
905 (defun map (result-type function first-sequence &rest more-sequences)
912 ;;; KLUDGE: MAP has been rewritten substantially since the fork from
913 ;;; CMU CL in order to give reasonable performance, but this
914 ;;; implementation of MAP-INTO still has the same problems as the old
915 ;;; MAP code. Ideally, MAP-INTO should be rewritten to be efficient in
916 ;;; the same way that the corresponding cases of MAP have been
917 ;;; rewritten. Instead of doing it now, though, it's easier to wait
918 ;;; until we have DYNAMIC-EXTENT, at which time it should become
919 ;;; extremely easy to define a reasonably efficient MAP-INTO in terms
920 ;;; of (MAP NIL ..). -- WHN 20000920
921 (defun map-into (result-sequence function &rest sequences)
923 (and (arrayp result-sequence)
924 (array-has-fill-pointer-p result-sequence)))
927 (array-dimension result-sequence 0)
928 (length result-sequence))
929 (mapcar #'length sequences))))
932 (setf (fill-pointer result-sequence) len))
934 (let ((really-fun (%coerce-callable-to-fun function)))
936 (setf (elt result-sequence index)
938 (mapcar (lambda (seq) (elt seq index))
944 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
945 ;;; arbitrary sequence arguments, both in the full call case and in
946 ;;; the open code case.
947 (macrolet ((defquantifier (name found-test found-result
948 &key doc (unfound-result (not found-result)))
950 ;; KLUDGE: It would be really nice if we could simply
951 ;; do something like this
952 ;; (declaim (inline ,name))
953 ;; (defun ,name (pred first-seq &rest more-seqs)
955 ;; (flet ((map-me (&rest rest)
956 ;; (let ((pred-value (apply pred rest)))
957 ;; (,found-test pred-value
958 ;; (return-from ,name
959 ;; ,found-result)))))
960 ;; (declare (inline map-me))
961 ;; (apply #'map nil #'map-me first-seq more-seqs)
963 ;; but Python doesn't seem to be smart enough about
964 ;; inlining and APPLY to recognize that it can use
965 ;; the DEFTRANSFORM for MAP in the resulting inline
966 ;; expansion. I don't have any appetite for deep
967 ;; compiler hacking right now, so I'll just work
968 ;; around the apparent problem by using a compiler
969 ;; macro instead. -- WHN 20000410
970 (defun ,name (pred first-seq &rest more-seqs)
972 (flet ((map-me (&rest rest)
973 (let ((pred-value (apply pred rest)))
974 (,found-test pred-value
977 (declare (inline map-me))
978 (apply #'map nil #'map-me first-seq more-seqs)
980 ;; KLUDGE: It would be more obviously correct -- but
981 ;; also significantly messier -- for PRED-VALUE to be
982 ;; a gensym. However, a private symbol really does
983 ;; seem to be good enough; and anyway the really
984 ;; obviously correct solution is to make Python smart
985 ;; enough that we can use an inline function instead
986 ;; of a compiler macro (as above). -- WHN 20000410
988 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
989 ;; important for performance, and it'd be good to have
990 ;; it be visible throughout the compilation of all the
991 ;; target SBCL code. That could be done by defining
992 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
993 ;; moving this DEFQUANTIFIER stuff (and perhaps other
994 ;; inline definitions in seq.lisp as well) into a new
995 ;; seq.lisp, and moving remaining target-only stuff
996 ;; from the old seq.lisp into target-seq.lisp.
997 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
998 (let ((elements (make-gensym-list (1+ (length more-seqs))))
999 (blockname (gensym "BLOCK")))
1000 (once-only ((pred pred))
1003 (lambda (,@elements)
1004 (let ((pred-value (funcall ,pred ,@elements)))
1005 (,',found-test pred-value
1006 (return-from ,blockname
1010 ,',unfound-result)))))))
1011 (defquantifier some when pred-value :unfound-result nil :doc
1012 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1013 possibly to those with index 1, and so on. Return the first
1014 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1015 (defquantifier every unless nil :doc
1016 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1017 possibly to those with index 1, and so on. Return NIL as soon
1018 as any invocation of PREDICATE returns NIL, or T if every invocation
1020 (defquantifier notany when nil :doc
1021 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1022 possibly to those with index 1, and so on. Return NIL as soon
1023 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1024 of any sequence is reached.")
1025 (defquantifier notevery unless t :doc
1026 "Apply PREDICATE to 0-indexed elements of the sequences, then
1027 possibly to those with index 1, and so on. Return T as soon
1028 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1033 (eval-when (:compile-toplevel :execute)
1035 (sb!xc:defmacro mumble-reduce (function
1042 `(do ((index ,start (1+ index))
1043 (value ,initial-value))
1044 ((= index (the fixnum ,end)) value)
1045 (declare (fixnum index))
1046 (setq value (funcall ,function value
1047 (apply-key ,key (,ref ,sequence index))))))
1049 (sb!xc:defmacro mumble-reduce-from-end (function
1056 `(do ((index (1- ,end) (1- index))
1057 (value ,initial-value)
1058 (terminus (1- ,start)))
1059 ((= index terminus) value)
1060 (declare (fixnum index terminus))
1061 (setq value (funcall ,function
1062 (apply-key ,key (,ref ,sequence index))
1065 (sb!xc:defmacro list-reduce (function
1072 `(let ((sequence (nthcdr ,start ,sequence)))
1073 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
1075 (sequence (if ,ivp sequence (cdr sequence))
1077 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1078 (funcall ,function value (apply-key ,key (car sequence)))))
1079 ((= count (the fixnum ,end)) value)
1080 (declare (fixnum count)))))
1082 (sb!xc:defmacro list-reduce-from-end (function
1089 `(let ((sequence (nthcdr (- (the fixnum (length ,sequence))
1091 (reverse ,sequence))))
1092 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
1094 (sequence (if ,ivp sequence (cdr sequence))
1096 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1097 (funcall ,function (apply-key ,key (car sequence)) value)))
1098 ((= count (the fixnum ,end)) value)
1099 (declare (fixnum count)))))
1103 (define-sequence-traverser reduce
1104 (function sequence &key key from-end start end (initial-value nil ivp))
1105 (declare (type index start))
1107 (end (or end length)))
1108 (declare (type index start end))
1109 (cond ((= end start)
1110 (if ivp initial-value (funcall function)))
1113 (list-reduce-from-end function sequence key start end
1115 (list-reduce function sequence key start end
1116 initial-value ivp)))
1119 (setq end (1- (the fixnum end)))
1120 (setq initial-value (apply-key key (aref sequence end))))
1121 (mumble-reduce-from-end function sequence key start end
1122 initial-value aref))
1125 (setq initial-value (apply-key key (aref sequence start)))
1126 (setq start (1+ start)))
1127 (mumble-reduce function sequence key start end
1128 initial-value aref)))))
1132 (eval-when (:compile-toplevel :execute)
1134 (sb!xc:defmacro mumble-delete (pred)
1135 `(do ((index start (1+ index))
1138 ((or (= index (the fixnum end)) (= number-zapped count))
1139 (do ((index index (1+ index)) ; Copy the rest of the vector.
1140 (jndex jndex (1+ jndex)))
1141 ((= index (the fixnum length))
1142 (shrink-vector sequence jndex))
1143 (declare (fixnum index jndex))
1144 (setf (aref sequence jndex) (aref sequence index))))
1145 (declare (fixnum index jndex number-zapped))
1146 (setf (aref sequence jndex) (aref sequence index))
1148 (incf number-zapped)
1151 (sb!xc:defmacro mumble-delete-from-end (pred)
1152 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1156 (terminus (1- start)))
1157 ((or (= index terminus) (= number-zapped count))
1158 (do ((losers losers) ; Delete the losers.
1159 (index start (1+ index))
1161 ((or (null losers) (= index (the fixnum end)))
1162 (do ((index index (1+ index)) ; Copy the rest of the vector.
1163 (jndex jndex (1+ jndex)))
1164 ((= index (the fixnum length))
1165 (shrink-vector sequence jndex))
1166 (declare (fixnum index jndex))
1167 (setf (aref sequence jndex) (aref sequence index))))
1168 (declare (fixnum index jndex))
1169 (setf (aref sequence jndex) (aref sequence index))
1170 (if (= index (the fixnum (car losers)))
1173 (declare (fixnum index number-zapped terminus))
1174 (setq this-element (aref sequence index))
1176 (incf number-zapped)
1177 (push index losers))))
1179 (sb!xc:defmacro normal-mumble-delete ()
1182 (not (funcall test-not item (apply-key key (aref sequence index))))
1183 (funcall test item (apply-key key (aref sequence index))))))
1185 (sb!xc:defmacro normal-mumble-delete-from-end ()
1186 `(mumble-delete-from-end
1188 (not (funcall test-not item (apply-key key this-element)))
1189 (funcall test item (apply-key key this-element)))))
1191 (sb!xc:defmacro list-delete (pred)
1192 `(let ((handle (cons nil sequence)))
1193 (do ((current (nthcdr start sequence) (cdr current))
1194 (previous (nthcdr start handle))
1195 (index start (1+ index))
1197 ((or (= index (the fixnum end)) (= number-zapped count))
1199 (declare (fixnum index number-zapped))
1201 (rplacd previous (cdr current))
1202 (incf number-zapped))
1204 (setq previous (cdr previous)))))))
1206 (sb!xc:defmacro list-delete-from-end (pred)
1207 `(let* ((reverse (nreverse (the list sequence)))
1208 (handle (cons nil reverse)))
1209 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1211 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1212 (index start (1+ index))
1214 ((or (= index (the fixnum end)) (= number-zapped count))
1215 (nreverse (cdr handle)))
1216 (declare (fixnum index number-zapped))
1218 (rplacd previous (cdr current))
1219 (incf number-zapped))
1221 (setq previous (cdr previous)))))))
1223 (sb!xc:defmacro normal-list-delete ()
1226 (not (funcall test-not item (apply-key key (car current))))
1227 (funcall test item (apply-key key (car current))))))
1229 (sb!xc:defmacro normal-list-delete-from-end ()
1230 '(list-delete-from-end
1232 (not (funcall test-not item (apply-key key (car current))))
1233 (funcall test item (apply-key key (car current))))))
1237 (define-sequence-traverser delete
1238 (item sequence &key from-end (test #'eql) test-not start
1241 "Return a sequence formed by destructively removing the specified ITEM from
1242 the given SEQUENCE."
1243 (declare (fixnum start))
1244 (let ((end (or end length)))
1245 (declare (type index end))
1246 (seq-dispatch sequence
1248 (normal-list-delete-from-end)
1249 (normal-list-delete))
1251 (normal-mumble-delete-from-end)
1252 (normal-mumble-delete)))))
1254 (eval-when (:compile-toplevel :execute)
1256 (sb!xc:defmacro if-mumble-delete ()
1258 (funcall predicate (apply-key key (aref sequence index)))))
1260 (sb!xc:defmacro if-mumble-delete-from-end ()
1261 `(mumble-delete-from-end
1262 (funcall predicate (apply-key key this-element))))
1264 (sb!xc:defmacro if-list-delete ()
1266 (funcall predicate (apply-key key (car current)))))
1268 (sb!xc:defmacro if-list-delete-from-end ()
1269 '(list-delete-from-end
1270 (funcall predicate (apply-key key (car current)))))
1274 (define-sequence-traverser delete-if
1275 (predicate sequence &key from-end start key end count)
1277 "Return a sequence formed by destructively removing the elements satisfying
1278 the specified PREDICATE from the given SEQUENCE."
1279 (declare (fixnum start))
1280 (let ((end (or end length)))
1281 (declare (type index end))
1282 (seq-dispatch sequence
1284 (if-list-delete-from-end)
1287 (if-mumble-delete-from-end)
1288 (if-mumble-delete)))))
1290 (eval-when (:compile-toplevel :execute)
1292 (sb!xc:defmacro if-not-mumble-delete ()
1294 (not (funcall predicate (apply-key key (aref sequence index))))))
1296 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1297 `(mumble-delete-from-end
1298 (not (funcall predicate (apply-key key this-element)))))
1300 (sb!xc:defmacro if-not-list-delete ()
1302 (not (funcall predicate (apply-key key (car current))))))
1304 (sb!xc:defmacro if-not-list-delete-from-end ()
1305 '(list-delete-from-end
1306 (not (funcall predicate (apply-key key (car current))))))
1310 (define-sequence-traverser delete-if-not
1311 (predicate sequence &key from-end start end key count)
1313 "Return a sequence formed by destructively removing the elements not
1314 satisfying the specified PREDICATE from the given SEQUENCE."
1315 (declare (fixnum start))
1316 (let ((end (or end length)))
1317 (declare (type index end))
1318 (seq-dispatch sequence
1320 (if-not-list-delete-from-end)
1321 (if-not-list-delete))
1323 (if-not-mumble-delete-from-end)
1324 (if-not-mumble-delete)))))
1328 (eval-when (:compile-toplevel :execute)
1330 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1331 ;;; satisfies the predicate.
1332 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1333 `(do ((index ,begin (,bump index))
1335 (do ((index ,left (,bump index))
1336 (result (make-sequence-like sequence length)))
1337 ((= index (the fixnum ,begin)) result)
1338 (declare (fixnum index))
1339 (setf (aref result index) (aref sequence index))))
1343 ((or (= index (the fixnum ,finish))
1344 (= number-zapped count))
1345 (do ((index index (,bump index))
1346 (new-index new-index (,bump new-index)))
1347 ((= index (the fixnum ,right)) (shrink-vector result new-index))
1348 (declare (fixnum index new-index))
1349 (setf (aref result new-index) (aref sequence index))))
1350 (declare (fixnum index new-index number-zapped))
1351 (setq this-element (aref sequence index))
1352 (cond (,pred (incf number-zapped))
1353 (t (setf (aref result new-index) this-element)
1354 (setq new-index (,bump new-index))))))
1356 (sb!xc:defmacro mumble-remove (pred)
1357 `(mumble-remove-macro 1+ 0 start end length ,pred))
1359 (sb!xc:defmacro mumble-remove-from-end (pred)
1360 `(let ((sequence (copy-seq sequence)))
1361 (mumble-delete-from-end ,pred)))
1363 (sb!xc:defmacro normal-mumble-remove ()
1366 (not (funcall test-not item (apply-key key this-element)))
1367 (funcall test item (apply-key key this-element)))))
1369 (sb!xc:defmacro normal-mumble-remove-from-end ()
1370 `(mumble-remove-from-end
1372 (not (funcall test-not item (apply-key key this-element)))
1373 (funcall test item (apply-key key this-element)))))
1375 (sb!xc:defmacro if-mumble-remove ()
1376 `(mumble-remove (funcall predicate (apply-key key this-element))))
1378 (sb!xc:defmacro if-mumble-remove-from-end ()
1379 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1381 (sb!xc:defmacro if-not-mumble-remove ()
1382 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1384 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1385 `(mumble-remove-from-end
1386 (not (funcall predicate (apply-key key this-element)))))
1388 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1390 (sb!xc:defmacro list-remove-macro (pred reverse?)
1391 `(let* ((sequence ,(if reverse?
1392 '(reverse (the list sequence))
1394 (%start ,(if reverse? '(- length end) 'start))
1395 (%end ,(if reverse? '(- length start) 'end))
1397 (results (do ((index 0 (1+ index))
1398 (before-start splice))
1399 ((= index (the fixnum %start)) before-start)
1400 (declare (fixnum index))
1402 (cdr (rplacd splice (list (pop sequence))))))))
1403 (do ((index %start (1+ index))
1406 ((or (= index (the fixnum %end)) (= number-zapped count))
1407 (do ((index index (1+ index)))
1410 '(nreverse (the list (cdr results)))
1412 (declare (fixnum index))
1413 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1414 (declare (fixnum index number-zapped))
1415 (setq this-element (pop sequence))
1417 (setq number-zapped (1+ number-zapped))
1418 (setq splice (cdr (rplacd splice (list this-element))))))))
1420 (sb!xc:defmacro list-remove (pred)
1421 `(list-remove-macro ,pred nil))
1423 (sb!xc:defmacro list-remove-from-end (pred)
1424 `(list-remove-macro ,pred t))
1426 (sb!xc:defmacro normal-list-remove ()
1429 (not (funcall test-not item (apply-key key this-element)))
1430 (funcall test item (apply-key key this-element)))))
1432 (sb!xc:defmacro normal-list-remove-from-end ()
1433 `(list-remove-from-end
1435 (not (funcall test-not item (apply-key key this-element)))
1436 (funcall test item (apply-key key this-element)))))
1438 (sb!xc:defmacro if-list-remove ()
1440 (funcall predicate (apply-key key this-element))))
1442 (sb!xc:defmacro if-list-remove-from-end ()
1443 `(list-remove-from-end
1444 (funcall predicate (apply-key key this-element))))
1446 (sb!xc:defmacro if-not-list-remove ()
1448 (not (funcall predicate (apply-key key this-element)))))
1450 (sb!xc:defmacro if-not-list-remove-from-end ()
1451 `(list-remove-from-end
1452 (not (funcall predicate (apply-key key this-element)))))
1456 (define-sequence-traverser remove
1457 (item sequence &key from-end (test #'eql) test-not start
1460 "Return a copy of SEQUENCE with elements satisfying the test (default is
1461 EQL) with ITEM removed."
1462 (declare (fixnum start))
1463 (let ((end (or end length)))
1464 (declare (type index end))
1465 (seq-dispatch sequence
1467 (normal-list-remove-from-end)
1468 (normal-list-remove))
1470 (normal-mumble-remove-from-end)
1471 (normal-mumble-remove)))))
1473 (define-sequence-traverser remove-if
1474 (predicate sequence &key from-end start end count key)
1476 "Return a copy of sequence with elements such that predicate(element)
1477 is non-null removed"
1478 (declare (fixnum start))
1479 (let ((end (or end length)))
1480 (declare (type index end))
1481 (seq-dispatch sequence
1483 (if-list-remove-from-end)
1486 (if-mumble-remove-from-end)
1487 (if-mumble-remove)))))
1489 (define-sequence-traverser remove-if-not
1490 (predicate sequence &key from-end start end count key)
1492 "Return a copy of sequence with elements such that predicate(element)
1494 (declare (fixnum start))
1495 (let ((end (or end length)))
1496 (declare (type index end))
1497 (seq-dispatch sequence
1499 (if-not-list-remove-from-end)
1500 (if-not-list-remove))
1502 (if-not-mumble-remove-from-end)
1503 (if-not-mumble-remove)))))
1505 ;;;; REMOVE-DUPLICATES
1507 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1508 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1509 ;;; if we look into the already copied structure (from after :start) and see
1510 ;;; the item. If we check from beginning we check into the rest of the
1511 ;;; original list up to the :end marker (this we have to do by running a
1512 ;;; do loop down the list that far and using our test.
1513 (defun list-remove-duplicates* (list test test-not start end key from-end)
1514 (declare (fixnum start))
1515 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1518 (do ((index 0 (1+ index)))
1520 (declare (fixnum index))
1521 (setq splice (cdr (rplacd splice (list (car current)))))
1522 (setq current (cdr current)))
1523 (do ((index 0 (1+ index)))
1524 ((or (and end (= index (the fixnum end)))
1526 (declare (fixnum index))
1527 (if (or (and from-end
1529 (member (apply-key key (car current))
1530 (nthcdr (1+ start) result)
1533 (member (apply-key key (car current))
1534 (nthcdr (1+ start) result)
1538 (not (do ((it (apply-key key (car current)))
1539 (l (cdr current) (cdr l))
1540 (i (1+ index) (1+ i)))
1541 ((or (atom l) (and end (= i (the fixnum end))))
1543 (declare (fixnum i))
1545 (not (funcall test-not
1547 (apply-key key (car l))))
1548 (funcall test it (apply-key key (car l))))
1550 (setq splice (cdr (rplacd splice (list (car current))))))
1551 (setq current (cdr current)))
1554 (setq splice (cdr (rplacd splice (list (car current)))))
1555 (setq current (cdr current)))
1558 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1559 &optional (length (length vector)))
1560 (declare (vector vector) (fixnum start length))
1561 (when (null end) (setf end (length vector)))
1562 (let ((result (make-sequence-like vector length))
1565 (declare (fixnum index jndex))
1568 (setf (aref result index) (aref vector index))
1569 (setq index (1+ index)))
1572 (setq elt (aref vector index))
1573 ;; FIXME: Relying on POSITION allowing both :TEST and :TEST-NOT
1574 ;; arguments simultaneously is a little fragile, since ANSI says
1575 ;; we can't depend on it, so we need to remember to keep that
1576 ;; extension in our implementation. It'd probably be better to
1577 ;; rewrite this to avoid passing both (as
1578 ;; LIST-REMOVE-DUPLICATES* was rewritten ca. sbcl-0.7.12.18).
1579 (unless (or (and from-end
1580 (position (apply-key key elt) result
1581 :start start :end jndex
1582 :test test :test-not test-not :key key))
1584 (position (apply-key key elt) vector
1585 :start (1+ index) :end end
1586 :test test :test-not test-not :key key)))
1587 (setf (aref result jndex) elt)
1588 (setq jndex (1+ jndex)))
1589 (setq index (1+ index)))
1592 (setf (aref result jndex) (aref vector index))
1593 (setq index (1+ index))
1594 (setq jndex (1+ jndex)))
1595 (shrink-vector result jndex)))
1597 (define-sequence-traverser remove-duplicates
1598 (sequence &key (test #'eql) test-not (start 0) end from-end key)
1600 "The elements of SEQUENCE are compared pairwise, and if any two match,
1601 the one occurring earlier is discarded, unless FROM-END is true, in
1602 which case the one later in the sequence is discarded. The resulting
1603 sequence is returned.
1605 The :TEST-NOT argument is deprecated."
1606 (declare (fixnum start))
1607 (seq-dispatch sequence
1609 (list-remove-duplicates* sequence test test-not
1610 start end key from-end))
1611 (vector-remove-duplicates* sequence test test-not
1612 start end key from-end)))
1614 ;;;; DELETE-DUPLICATES
1616 (defun list-delete-duplicates* (list test test-not key from-end start end)
1617 (declare (fixnum start))
1618 (let ((handle (cons nil list)))
1619 (do ((current (nthcdr start list) (cdr current))
1620 (previous (nthcdr start handle))
1621 (index start (1+ index)))
1622 ((or (and end (= index (the fixnum end))) (null current))
1624 (declare (fixnum index))
1625 (if (do ((x (if from-end
1626 (nthcdr (1+ start) handle)
1629 (i (1+ index) (1+ i)))
1631 (and (not from-end) end (= i (the fixnum end)))
1634 (declare (fixnum i))
1636 (not (funcall test-not
1637 (apply-key key (car current))
1638 (apply-key key (car x))))
1640 (apply-key key (car current))
1641 (apply-key key (car x))))
1643 (rplacd previous (cdr current))
1644 (setq previous (cdr previous))))))
1646 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1647 &optional (length (length vector)))
1648 (declare (vector vector) (fixnum start length))
1649 (when (null end) (setf end (length vector)))
1650 (do ((index start (1+ index))
1653 (do ((index index (1+ index)) ; copy the rest of the vector
1654 (jndex jndex (1+ jndex)))
1656 (shrink-vector vector jndex)
1658 (setf (aref vector jndex) (aref vector index))))
1659 (declare (fixnum index jndex))
1660 (setf (aref vector jndex) (aref vector index))
1661 (unless (position (apply-key key (aref vector index)) vector :key key
1662 :start (if from-end start (1+ index)) :test test
1663 :end (if from-end jndex end) :test-not test-not)
1664 (setq jndex (1+ jndex)))))
1666 (define-sequence-traverser delete-duplicates
1667 (sequence &key (test #'eql) test-not (start 0) end from-end key)
1669 "The elements of SEQUENCE are examined, and if any two match, one is
1670 discarded. The resulting sequence, which may be formed by destroying the
1671 given sequence, is returned.
1673 The :TEST-NOT argument is deprecated."
1674 (seq-dispatch sequence
1676 (list-delete-duplicates* sequence test test-not key from-end start end))
1677 (vector-delete-duplicates* sequence test test-not key from-end start end)))
1681 (defun list-substitute* (pred new list start end count key test test-not old)
1682 (declare (fixnum start end count))
1683 (let* ((result (list nil))
1686 (list list)) ; Get a local list for a stepper.
1687 (do ((index 0 (1+ index)))
1689 (declare (fixnum index))
1690 (setq splice (cdr (rplacd splice (list (car list)))))
1691 (setq list (cdr list)))
1692 (do ((index start (1+ index)))
1693 ((or (= index end) (null list) (= count 0)))
1694 (declare (fixnum index))
1695 (setq elt (car list))
1704 (funcall test-not old (apply-key key elt)))
1705 (funcall test old (apply-key key elt))))
1706 (if (funcall test (apply-key key elt)))
1707 (if-not (not (funcall test (apply-key key elt)))))
1711 (setq list (cdr list)))
1714 (setq splice (cdr (rplacd splice (list (car list)))))
1715 (setq list (cdr list)))
1718 ;;; Replace old with new in sequence moving from left to right by incrementer
1719 ;;; on each pass through the loop. Called by all three substitute functions.
1720 (defun vector-substitute* (pred new sequence incrementer left right length
1721 start end count key test test-not old)
1722 (declare (fixnum start count end incrementer right))
1723 (let ((result (make-sequence-like sequence length))
1725 (declare (fixnum index))
1728 (setf (aref result index) (aref sequence index))
1729 (setq index (+ index incrementer)))
1731 ((or (= index end) (= count 0)))
1732 (setq elt (aref sequence index))
1733 (setf (aref result index)
1737 (not (funcall test-not old (apply-key key elt)))
1738 (funcall test old (apply-key key elt))))
1739 (if (funcall test (apply-key key elt)))
1740 (if-not (not (funcall test (apply-key key elt)))))
1741 (setq count (1- count))
1744 (setq index (+ index incrementer)))
1747 (setf (aref result index) (aref sequence index))
1748 (setq index (+ index incrementer)))
1751 (eval-when (:compile-toplevel :execute)
1753 (sb!xc:defmacro subst-dispatch (pred)
1754 `(if (listp sequence)
1756 (nreverse (list-substitute* ,pred
1759 (- (the fixnum length)
1761 (- (the fixnum length)
1763 count key test test-not old))
1764 (list-substitute* ,pred
1765 new sequence start end count key test test-not
1768 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
1769 -1 length (1- (the fixnum end))
1770 (1- (the fixnum start))
1771 count key test test-not old)
1772 (vector-substitute* ,pred new sequence 1 0 length length
1773 start end count key test test-not old))))
1777 (define-sequence-traverser substitute
1778 (new old sequence &key from-end (test #'eql) test-not
1779 start count end key)
1781 "Return a sequence of the same kind as SEQUENCE with the same elements,
1782 except that all elements equal to OLD are replaced with NEW. See manual
1784 (declare (fixnum start))
1785 (let ((end (or end length)))
1786 (declare (type index end))
1787 (subst-dispatch 'normal)))
1789 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
1791 (define-sequence-traverser substitute-if
1792 (new test sequence &key from-end start end count key)
1794 "Return a sequence of the same kind as SEQUENCE with the same elements
1795 except that all elements satisfying the TEST are replaced with NEW. See
1796 manual for details."
1797 (declare (fixnum start))
1798 (let ((end (or end length))
1801 (declare (type index length end))
1802 (subst-dispatch 'if)))
1804 (define-sequence-traverser substitute-if-not
1805 (new test sequence &key from-end start end count key)
1807 "Return a sequence of the same kind as SEQUENCE with the same elements
1808 except that all elements not satisfying the TEST are replaced with NEW.
1809 See manual for details."
1810 (declare (fixnum start))
1811 (let ((end (or end length))
1814 (declare (type index length end))
1815 (subst-dispatch 'if-not)))
1819 (define-sequence-traverser nsubstitute
1820 (new old sequence &key from-end (test #'eql) test-not
1821 end count key start)
1823 "Return a sequence of the same kind as SEQUENCE with the same elements
1824 except that all elements equal to OLD are replaced with NEW. The SEQUENCE
1825 may be destructively modified. See manual for details."
1826 (declare (fixnum start))
1827 (let ((end (or end length)))
1828 (if (listp sequence)
1830 (let ((length (length sequence)))
1831 (nreverse (nlist-substitute*
1832 new old (nreverse (the list sequence))
1833 test test-not (- length end) (- length start)
1835 (nlist-substitute* new old sequence
1836 test test-not start end count key))
1838 (nvector-substitute* new old sequence -1
1839 test test-not (1- end) (1- start) count key)
1840 (nvector-substitute* new old sequence 1
1841 test test-not start end count key)))))
1843 (defun nlist-substitute* (new old sequence test test-not start end count key)
1844 (declare (fixnum start count end))
1845 (do ((list (nthcdr start sequence) (cdr list))
1846 (index start (1+ index)))
1847 ((or (= index end) (null list) (= count 0)) sequence)
1848 (declare (fixnum index))
1850 (not (funcall test-not old (apply-key key (car list))))
1851 (funcall test old (apply-key key (car list))))
1853 (setq count (1- count)))))
1855 (defun nvector-substitute* (new old sequence incrementer
1856 test test-not start end count key)
1857 (declare (fixnum start incrementer count end))
1858 (do ((index start (+ index incrementer)))
1859 ((or (= index end) (= count 0)) sequence)
1860 (declare (fixnum index))
1862 (not (funcall test-not
1864 (apply-key key (aref sequence index))))
1865 (funcall test old (apply-key key (aref sequence index))))
1866 (setf (aref sequence index) new)
1867 (setq count (1- count)))))
1869 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
1871 (define-sequence-traverser nsubstitute-if
1872 (new test sequence &key from-end start end count key)
1874 "Return a sequence of the same kind as SEQUENCE with the same elements
1875 except that all elements satisfying the TEST are replaced with NEW.
1876 SEQUENCE may be destructively modified. See manual for details."
1877 (declare (fixnum start))
1878 (let ((end (or end length)))
1879 (declare (fixnum end))
1880 (if (listp sequence)
1882 (let ((length (length sequence)))
1883 (nreverse (nlist-substitute-if*
1884 new test (nreverse (the list sequence))
1885 (- length end) (- length start) count key)))
1886 (nlist-substitute-if* new test sequence
1887 start end count key))
1889 (nvector-substitute-if* new test sequence -1
1890 (1- end) (1- start) count key)
1891 (nvector-substitute-if* new test sequence 1
1892 start end count key)))))
1894 (defun nlist-substitute-if* (new test sequence start end count key)
1895 (declare (fixnum end))
1896 (do ((list (nthcdr start sequence) (cdr list))
1897 (index start (1+ index)))
1898 ((or (= index end) (null list) (= count 0)) sequence)
1899 (when (funcall test (apply-key key (car list)))
1901 (setq count (1- count)))))
1903 (defun nvector-substitute-if* (new test sequence incrementer
1904 start end count key)
1905 (do ((index start (+ index incrementer)))
1906 ((or (= index end) (= count 0)) sequence)
1907 (when (funcall test (apply-key key (aref sequence index)))
1908 (setf (aref sequence index) new)
1909 (setq count (1- count)))))
1911 (define-sequence-traverser nsubstitute-if-not
1912 (new test sequence &key from-end start end count key)
1914 "Return a sequence of the same kind as SEQUENCE with the same elements
1915 except that all elements not satisfying the TEST are replaced with NEW.
1916 SEQUENCE may be destructively modified. See manual for details."
1917 (declare (fixnum start))
1918 (let ((end (or end length)))
1919 (declare (fixnum end))
1920 (if (listp sequence)
1922 (let ((length (length sequence)))
1923 (nreverse (nlist-substitute-if-not*
1924 new test (nreverse (the list sequence))
1925 (- length end) (- length start) count key)))
1926 (nlist-substitute-if-not* new test sequence
1927 start end count key))
1929 (nvector-substitute-if-not* new test sequence -1
1930 (1- end) (1- start) count key)
1931 (nvector-substitute-if-not* new test sequence 1
1932 start end count key)))))
1934 (defun nlist-substitute-if-not* (new test sequence start end count key)
1935 (declare (fixnum end))
1936 (do ((list (nthcdr start sequence) (cdr list))
1937 (index start (1+ index)))
1938 ((or (= index end) (null list) (= count 0)) sequence)
1939 (when (not (funcall test (apply-key key (car list))))
1943 (defun nvector-substitute-if-not* (new test sequence incrementer
1944 start end count key)
1945 (do ((index start (+ index incrementer)))
1946 ((or (= index end) (= count 0)) sequence)
1947 (when (not (funcall test (apply-key key (aref sequence index))))
1948 (setf (aref sequence index) new)
1951 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
1953 (defun effective-find-position-test (test test-not)
1954 (effective-find-position-test test test-not))
1955 (defun effective-find-position-key (key)
1956 (effective-find-position-key key))
1958 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
1959 (macrolet (;; shared logic for defining %FIND-POSITION and
1960 ;; %FIND-POSITION-IF in terms of various inlineable cases
1961 ;; of the expression defined in FROB and VECTOR*-FROB
1963 `(etypecase sequence-arg
1964 (list (frob sequence-arg from-end))
1966 (with-array-data ((sequence sequence-arg :offset-var offset)
1968 (end (%check-vector-sequence-bounds
1969 sequence-arg start end)))
1970 (multiple-value-bind (f p)
1971 (macrolet ((frob2 () '(if from-end
1973 (frob sequence nil))))
1975 (simple-vector (frob2))
1976 (simple-string (frob2))
1977 (t (vector*-frob sequence))))
1978 (declare (type (or index null) p))
1979 (values f (and p (the index (+ p offset))))))))))
1980 (defun %find-position (item sequence-arg from-end start end key test)
1981 (macrolet ((frob (sequence from-end)
1982 `(%find-position item ,sequence
1983 ,from-end start end key test))
1984 (vector*-frob (sequence)
1985 `(%find-position-vector-macro item ,sequence
1986 from-end start end key test)))
1988 (defun %find-position-if (predicate sequence-arg from-end start end key)
1989 (macrolet ((frob (sequence from-end)
1990 `(%find-position-if predicate ,sequence
1991 ,from-end start end key))
1992 (vector*-frob (sequence)
1993 `(%find-position-if-vector-macro predicate ,sequence
1994 from-end start end key)))
1996 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
1997 (macrolet ((frob (sequence from-end)
1998 `(%find-position-if-not predicate ,sequence
1999 ,from-end start end key))
2000 (vector*-frob (sequence)
2001 `(%find-position-if-not-vector-macro predicate ,sequence
2002 from-end start end key)))
2005 ;;; the user interface to FIND and POSITION: just interpreter stubs,
2007 (defun find (item sequence &key from-end (start 0) end key test test-not)
2008 ;; FIXME: this can't be the way to go, surely?
2009 (find item sequence :from-end from-end :start start :end end :key key
2010 :test test :test-not test-not))
2011 (defun position (item sequence &key from-end (start 0) end key test test-not)
2012 (position item sequence :from-end from-end :start start :end end :key key
2013 :test test :test-not test-not))
2015 ;;; the user interface to FIND-IF and POSITION-IF, entirely analogous
2016 ;;; to the interface to FIND and POSITION
2017 (defun find-if (predicate sequence &key from-end (start 0) end key)
2018 (find-if predicate sequence :from-end from-end :start start
2020 (defun position-if (predicate sequence &key from-end (start 0) end key)
2021 (position-if predicate sequence :from-end from-end :start start
2024 (defun find-if-not (predicate sequence &key from-end (start 0) end key)
2025 (find-if-not predicate sequence :from-end from-end :start start
2027 (defun position-if-not (predicate sequence &key from-end (start 0) end key)
2028 (position-if-not predicate sequence :from-end from-end :start start
2031 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2033 (eval-when (:compile-toplevel :execute)
2035 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2036 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2037 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2038 `(let ((%start ,(if from-end-p '(1- end) 'start))
2039 (%end ,(if from-end-p '(1- start) 'end)))
2040 (do ((index %start ,next-index)
2042 ((= index (the fixnum %end)) count)
2043 (declare (fixnum index count))
2044 (,(if notp 'unless 'when) ,pred
2045 (setq count (1+ count)))))))
2047 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2048 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2049 `(let ((%start ,(if from-end-p '(- length end) 'start))
2050 (%end ,(if from-end-p '(- length start) 'end))
2051 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2052 (do ((sequence (nthcdr %start ,sequence))
2053 (index %start (1+ index))
2055 ((or (= index (the fixnum %end)) (null sequence)) count)
2056 (declare (fixnum index count))
2057 (,(if notp 'unless 'when) ,pred
2058 (setq count (1+ count)))))))
2063 (define-sequence-traverser count-if (test sequence &key from-end start end key)
2065 "Return the number of elements in SEQUENCE satisfying TEST(el)."
2066 (declare (fixnum start))
2067 (let ((end (or end length)))
2068 (declare (type index end))
2069 (seq-dispatch sequence
2071 (list-count-if nil t test sequence)
2072 (list-count-if nil nil test sequence))
2074 (vector-count-if nil t test sequence)
2075 (vector-count-if nil nil test sequence)))))
2077 (define-sequence-traverser count-if-not
2078 (test sequence &key from-end start end key)
2080 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2081 (declare (fixnum start))
2082 (let ((end (or end length)))
2083 (declare (type index end))
2084 (seq-dispatch sequence
2086 (list-count-if t t test sequence)
2087 (list-count-if t nil test sequence))
2089 (vector-count-if t t test sequence)
2090 (vector-count-if t nil test sequence)))))
2092 (define-sequence-traverser count
2093 (item sequence &key from-end start end
2094 key (test #'eql test-p) (test-not nil test-not-p))
2096 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2097 which defaults to EQL."
2098 (declare (fixnum start))
2099 (when (and test-p test-not-p)
2100 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2102 (error ":TEST and :TEST-NOT are both present."))
2103 (let ((end (or end length)))
2104 (declare (type index end))
2105 (let ((%test (if test-not-p
2107 (not (funcall test-not item x)))
2109 (funcall test item x)))))
2110 (seq-dispatch sequence
2112 (list-count-if nil t %test sequence)
2113 (list-count-if nil nil %test sequence))
2115 (vector-count-if nil t %test sequence)
2116 (vector-count-if nil nil %test sequence))))))
2122 (eval-when (:compile-toplevel :execute)
2124 (sb!xc:defmacro match-vars (&rest body)
2125 `(let ((inc (if from-end -1 1))
2126 (start1 (if from-end (1- (the fixnum end1)) start1))
2127 (start2 (if from-end (1- (the fixnum end2)) start2))
2128 (end1 (if from-end (1- (the fixnum start1)) end1))
2129 (end2 (if from-end (1- (the fixnum start2)) end2)))
2130 (declare (fixnum inc start1 start2 end1 end2))
2133 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2134 (declare (ignore end)) ;; ### Should END be used below?
2135 `(let ((,sequence (if from-end
2136 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2137 (reverse (the list ,sequence)))
2138 (nthcdr ,start ,sequence))))
2139 (declare (type list ,sequence))
2144 (eval-when (:compile-toplevel :execute)
2146 (sb!xc:defmacro if-mismatch (elt1 elt2)
2147 `(cond ((= (the fixnum index1) (the fixnum end1))
2148 (return (if (= (the fixnum index2) (the fixnum end2))
2151 (1+ (the fixnum index1))
2152 (the fixnum index1)))))
2153 ((= (the fixnum index2) (the fixnum end2))
2154 (return (if from-end (1+ (the fixnum index1)) index1)))
2156 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2157 (return (if from-end (1+ (the fixnum index1)) index1))))
2158 (t (if (not (funcall test (apply-key key ,elt1)
2159 (apply-key key ,elt2)))
2160 (return (if from-end (1+ (the fixnum index1)) index1))))))
2162 (sb!xc:defmacro mumble-mumble-mismatch ()
2163 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2164 (index2 start2 (+ index2 (the fixnum inc))))
2166 (declare (fixnum index1 index2))
2167 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2169 (sb!xc:defmacro mumble-list-mismatch ()
2170 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2171 (index2 start2 (+ index2 (the fixnum inc))))
2173 (declare (fixnum index1 index2))
2174 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2176 (sb!xc:defmacro list-mumble-mismatch ()
2177 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2178 (index2 start2 (+ index2 (the fixnum inc))))
2180 (declare (fixnum index1 index2))
2181 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2183 (sb!xc:defmacro list-list-mismatch ()
2184 `(do ((sequence1 sequence1)
2185 (sequence2 sequence2)
2186 (index1 start1 (+ index1 (the fixnum inc)))
2187 (index2 start2 (+ index2 (the fixnum inc))))
2189 (declare (fixnum index1 index2))
2190 (if-mismatch (pop sequence1) (pop sequence2))))
2194 (define-sequence-traverser mismatch
2195 (sequence1 sequence2
2196 &key from-end (test #'eql) test-not
2197 start1 end1 start2 end2 key)
2199 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2200 element-wise. If they are of equal length and match in every element, the
2201 result is NIL. Otherwise, the result is a non-negative integer, the index
2202 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2203 if one is shorter than and a matching prefix of the other, the index within
2204 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2205 :FROM-END argument is given, then one plus the index of the rightmost
2206 position in which the sequences differ is returned."
2207 (declare (fixnum start1 start2))
2208 (let* ((end1 (or end1 length1))
2209 (end2 (or end2 length2)))
2210 (declare (type index end1 end2))
2212 (seq-dispatch sequence1
2213 (matchify-list (sequence1 start1 length1 end1)
2214 (seq-dispatch sequence2
2215 (matchify-list (sequence2 start2 length2 end2)
2216 (list-list-mismatch))
2217 (list-mumble-mismatch)))
2218 (seq-dispatch sequence2
2219 (matchify-list (sequence2 start2 length2 end2)
2220 (mumble-list-mismatch))
2221 (mumble-mumble-mismatch))))))
2223 ;;; search comparison functions
2225 (eval-when (:compile-toplevel :execute)
2227 ;;; Compare two elements and return if they don't match.
2228 (sb!xc:defmacro compare-elements (elt1 elt2)
2230 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2233 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2237 (sb!xc:defmacro search-compare-list-list (main sub)
2238 `(do ((main ,main (cdr main))
2239 (jndex start1 (1+ jndex))
2240 (sub (nthcdr start1 ,sub) (cdr sub)))
2241 ((or (null main) (null sub) (= (the fixnum end1) jndex))
2243 (declare (fixnum jndex))
2244 (compare-elements (car main) (car sub))))
2246 (sb!xc:defmacro search-compare-list-vector (main sub)
2247 `(do ((main ,main (cdr main))
2248 (index start1 (1+ index)))
2249 ((or (null main) (= index (the fixnum end1))) t)
2250 (declare (fixnum index))
2251 (compare-elements (car main) (aref ,sub index))))
2253 (sb!xc:defmacro search-compare-vector-list (main sub index)
2254 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2255 (jndex start1 (1+ jndex))
2256 (index ,index (1+ index)))
2257 ((or (= (the fixnum end1) jndex) (null sub)) t)
2258 (declare (fixnum jndex index))
2259 (compare-elements (aref ,main index) (car sub))))
2261 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2262 `(do ((index ,index (1+ index))
2263 (sub-index start1 (1+ sub-index)))
2264 ((= sub-index (the fixnum end1)) t)
2265 (declare (fixnum sub-index index))
2266 (compare-elements (aref ,main index) (aref ,sub sub-index))))
2268 (sb!xc:defmacro search-compare (main-type main sub index)
2269 (if (eq main-type 'list)
2271 (search-compare-list-list ,main ,sub)
2272 (search-compare-list-vector ,main ,sub))
2274 (search-compare-vector-list ,main ,sub ,index)
2275 (search-compare-vector-vector ,main ,sub ,index))))
2281 (eval-when (:compile-toplevel :execute)
2283 (sb!xc:defmacro list-search (main sub)
2284 `(do ((main (nthcdr start2 ,main) (cdr main))
2285 (index2 start2 (1+ index2))
2286 (terminus (- (the fixnum end2)
2287 (the fixnum (- (the fixnum end1)
2288 (the fixnum start1)))))
2290 ((> index2 terminus) last-match)
2291 (declare (fixnum index2 terminus))
2292 (if (search-compare list main ,sub index2)
2294 (setq last-match index2)
2297 (sb!xc:defmacro vector-search (main sub)
2298 `(do ((index2 start2 (1+ index2))
2299 (terminus (- (the fixnum end2)
2300 (the fixnum (- (the fixnum end1)
2301 (the fixnum start1)))))
2303 ((> index2 terminus) last-match)
2304 (declare (fixnum index2 terminus))
2305 (if (search-compare vector ,main ,sub index2)
2307 (setq last-match index2)
2312 (define-sequence-traverser search
2313 (sequence1 sequence2
2314 &key from-end (test #'eql) test-not
2315 start1 end1 start2 end2 key)
2316 (declare (fixnum start1 start2))
2317 (let ((end1 (or end1 length1))
2318 (end2 (or end2 length2)))
2319 (seq-dispatch sequence2
2320 (list-search sequence2 sequence1)
2321 (vector-search sequence2 sequence1))))