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 (defun %check-generic-sequence-bounds (seq start end)
24 (let ((length (sb!sequence:length seq)))
25 (if (<= 0 start (or end length) length)
27 (sequence-bounding-indices-bad-error seq start end))))
29 (eval-when (:compile-toplevel :load-toplevel :execute)
31 (defparameter *sequence-keyword-info*
32 ;; (name default supplied-p adjustment new-type)
36 (null (1- most-positive-fixnum))
37 (fixnum (max 0 count))
38 (integer (if (minusp count)
40 (1- most-positive-fixnum))))
41 (mod #.sb!xc:most-positive-fixnum))
42 ,@(mapcan (lambda (names)
43 (destructuring-bind (start end length sequence) names
48 (if (<= 0 ,start ,length)
50 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
55 (if (or (null ,end) (<= ,start ,end ,length))
56 ;; Defaulting of NIL is done inside the
57 ;; bodies, for ease of sharing with compiler
60 ;; FIXME: defend against non-number non-NIL
63 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
65 '((start end length sequence)
66 (start1 end1 length1 sequence1)
67 (start2 end2 length2 sequence2)))
70 (and key (%coerce-callable-to-fun key))
74 (%coerce-callable-to-fun test)
78 (and test-not (%coerce-callable-to-fun test-not))
82 (sb!xc:defmacro define-sequence-traverser (name args &body body)
83 (multiple-value-bind (body declarations docstring)
84 (parse-body body :doc-string-allowed t)
85 (collect ((new-args) (new-declarations) (adjustments))
88 ;; FIXME: make this robust. And clean.
91 (adjustments '(length (length sequence)))
92 (new-declarations '(type index length)))
95 (adjustments '(length1 (length sequence1)))
96 (new-declarations '(type index length1)))
99 (adjustments '(length2 (length sequence2)))
100 (new-declarations '(type index length2)))
101 ((function predicate)
103 (adjustments `(,arg (%coerce-callable-to-fun ,arg))))
104 (t (let ((info (cdr (assoc arg *sequence-keyword-info*))))
106 (destructuring-bind (default supplied-p adjuster type) info
107 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
108 (adjustments `(,arg ,adjuster))
109 (new-declarations `(type ,type ,arg))))
110 (t (new-args arg)))))))
111 `(defun ,name ,(new-args)
112 ,@(when docstring (list docstring))
114 (let* (,@(adjustments))
115 (declare ,@(new-declarations))
118 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
120 ;;; FIXME: It might be worth making three cases here, LIST,
121 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
122 ;;; It tends to make code run faster but be bigger; some benchmarking
123 ;;; is needed to decide.
124 (sb!xc:defmacro seq-dispatch
125 (sequence list-form array-form &optional other-form)
126 `(if (listp ,sequence)
127 (let ((,sequence (truly-the list ,sequence)))
128 (declare (ignorable ,sequence))
131 `((if (arrayp ,sequence)
132 (let ((,sequence (truly-the vector ,sequence)))
133 (declare (ignorable ,sequence))
136 `((let ((,sequence (truly-the vector ,sequence)))
137 (declare (ignorable ,sequence))
140 (sb!xc:defmacro %make-sequence-like (sequence length)
142 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
143 `(seq-dispatch ,sequence
145 (make-array ,length :element-type (array-element-type ,sequence))
146 (sb!sequence:make-sequence-like ,sequence ,length)))
148 (sb!xc:defmacro bad-sequence-type-error (type-spec)
149 `(error 'simple-type-error
151 :expected-type '(satisfies is-a-valid-sequence-type-specifier-p)
152 :format-control "~S is a bad type specifier for sequences."
153 :format-arguments (list ,type-spec)))
155 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
156 `(error 'simple-type-error
158 :expected-type (cond ((array-type-p ,type)
159 `(eql ,(car (array-type-dimensions ,type))))
160 ((type= ,type (specifier-type 'null))
164 (t (bug "weird type in S-T-L-M-ERROR")))
165 ;; FIXME: this format control causes ugly printing. There's
166 ;; probably some ~<~@:_~> incantation that would make it
167 ;; nicer. -- CSR, 2002-10-18
168 :format-control "The length requested (~S) does not match the type restriction in ~S."
169 :format-arguments (list ,length (type-specifier ,type))))
171 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
172 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
173 ;; words that give some but not total support to this position in
174 ;; ANSI. Essentially, we are justified in throwing this on
175 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
176 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
178 ;; On the other hand, I'm not sure it deserves to be a type-error,
179 ;; either. -- bem, 2005-08-10
180 `(error 'simple-program-error
181 :format-control "~S is too hairy for sequence functions."
182 :format-arguments (list ,type-spec)))
185 (defun is-a-valid-sequence-type-specifier-p (type)
186 (let ((type (specifier-type type)))
187 (or (csubtypep type (specifier-type 'list))
188 (csubtypep type (specifier-type 'vector)))))
190 ;;; It's possible with some sequence operations to declare the length
191 ;;; of a result vector, and to be safe, we really ought to verify that
192 ;;; the actual result has the declared length.
193 (defun vector-of-checked-length-given-length (vector declared-length)
194 (declare (type vector vector))
195 (declare (type index declared-length))
196 (let ((actual-length (length vector)))
197 (unless (= actual-length declared-length)
198 (error 'simple-type-error
200 :expected-type `(vector ,declared-length)
202 "Vector length (~W) doesn't match declared length (~W)."
203 :format-arguments (list actual-length declared-length))))
205 (defun sequence-of-checked-length-given-type (sequence result-type)
206 (let ((ctype (specifier-type result-type)))
207 (if (not (array-type-p ctype))
209 (let ((declared-length (first (array-type-dimensions ctype))))
210 (if (eq declared-length '*)
212 (vector-of-checked-length-given-length sequence
213 declared-length))))))
215 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
216 (defun signal-index-too-large-error (sequence index)
217 (let* ((length (length sequence))
218 (max-index (and (plusp length)
220 (error 'index-too-large-error
222 :expected-type (if max-index
223 `(integer 0 ,max-index)
224 ;; This seems silly, is there something better?
227 (declaim (ftype (function (t t t) nil) sequence-bounding-indices-bad-error))
228 (defun sequence-bounding-indices-bad-error (sequence start end)
229 (let ((size (length sequence)))
230 (error 'bounding-indices-bad-error
231 :datum (cons start end)
232 :expected-type `(cons (integer 0 ,size)
233 (integer ,start ,size))
236 (declaim (ftype (function (t t t) nil) array-bounding-indices-bad-error))
237 (defun array-bounding-indices-bad-error (array start end)
238 (let ((size (array-total-size array)))
239 (error 'bounding-indices-bad-error
240 :datum (cons start end)
241 :expected-type `(cons (integer 0 ,size)
242 (integer ,start ,size))
245 (declaim (ftype (function (t) nil) circular-list-error))
246 (defun circular-list-error (list)
247 (let ((*print-circle* t))
248 (error 'simple-type-error
249 :format-control "List is circular:~% ~S"
250 :format-arguments (list list)
252 :type '(and list (satisfies list-length)))))
255 (defun elt (sequence index)
256 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
257 (seq-dispatch sequence
258 (do ((count index (1- count))
259 (list sequence (cdr list)))
262 (signal-index-too-large-error sequence index)
264 (declare (type (integer 0) count)))
266 (when (>= index (length sequence))
267 (signal-index-too-large-error sequence index))
268 (aref sequence index))
269 (sb!sequence:elt sequence index)))
271 (defun %setelt (sequence index newval)
272 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
273 (seq-dispatch sequence
274 (do ((count index (1- count))
276 ((= count 0) (rplaca seq newval) newval)
277 (declare (fixnum count))
279 (signal-index-too-large-error sequence index)
280 (setq seq (cdr seq))))
282 (when (>= index (length sequence))
283 (signal-index-too-large-error sequence index))
284 (setf (aref sequence index) newval))
285 (setf (sb!sequence:elt sequence index) newval)))
287 (defun length (sequence)
288 #!+sb-doc "Return an integer that is the length of SEQUENCE."
289 (seq-dispatch sequence
292 (sb!sequence:length sequence)))
294 (defun make-sequence (type length &key (initial-element nil iep))
296 "Return a sequence of the given TYPE and LENGTH, with elements initialized
298 (declare (fixnum length))
299 (let* ((expanded-type (typexpand type))
301 (typecase expanded-type
303 ((eq expanded-type 'string) '(vector character))
304 ((eq expanded-type 'simple-string)
305 '(simple-array character (*)))
308 ((eq (car expanded-type) 'string)
309 `(vector character ,@(cdr expanded-type)))
310 ((eq (car expanded-type) 'simple-string)
311 `(simple-array character ,(if (cdr expanded-type)
315 (type (specifier-type adjusted-type)))
316 (cond ((csubtypep type (specifier-type 'list))
318 ((type= type (specifier-type 'list))
319 (make-list length :initial-element initial-element))
320 ((eq type *empty-type*)
321 (bad-sequence-type-error nil))
322 ((type= type (specifier-type 'null))
325 (sequence-type-length-mismatch-error type length)))
327 (multiple-value-bind (min exactp)
328 (sb!kernel::cons-type-length-info type)
330 (unless (= length min)
331 (sequence-type-length-mismatch-error type length))
332 (unless (>= length min)
333 (sequence-type-length-mismatch-error type length)))
334 (make-list length :initial-element initial-element)))
335 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
336 ;; which may seem strange and non-ideal, but then I'd say
337 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
338 (t (sequence-type-too-hairy (type-specifier type)))))
339 ((csubtypep type (specifier-type 'vector))
341 (;; is it immediately obvious what the result type is?
342 (typep type 'array-type)
344 (aver (= (length (array-type-dimensions type)) 1))
345 (let* ((etype (type-specifier
346 (array-type-specialized-element-type type)))
347 (etype (if (eq etype '*) t etype))
348 (type-length (car (array-type-dimensions type))))
349 (unless (or (eq type-length '*)
350 (= type-length length))
351 (sequence-type-length-mismatch-error type length))
352 ;; FIXME: These calls to MAKE-ARRAY can't be
353 ;; open-coded, as the :ELEMENT-TYPE argument isn't
354 ;; constant. Probably we ought to write a
355 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
358 (make-array length :element-type etype
359 :initial-element initial-element)
360 (make-array length :element-type etype)))))
361 (t (sequence-type-too-hairy (type-specifier type)))))
362 ((and (csubtypep type (specifier-type 'sequence))
363 (find-class adjusted-type nil))
364 (let* ((class (find-class adjusted-type nil)))
365 (unless (sb!mop:class-finalized-p class)
366 (sb!mop:finalize-inheritance class))
368 (sb!sequence:make-sequence-like
369 (sb!mop:class-prototype class) length
370 :initial-element initial-element)
371 (sb!sequence:make-sequence-like
372 (sb!mop:class-prototype class) length))))
373 (t (bad-sequence-type-error (type-specifier type))))))
378 (define-array-dispatch vector-subseq-dispatch (array start end)
379 (declare (optimize speed (safety 0)))
380 (declare (type index start end))
381 (subseq array start end))
383 ;;;; The support routines for SUBSEQ are used by compiler transforms,
384 ;;;; so we worry about dealing with END being supplied or defaulting
385 ;;;; to NIL at this level.
387 (defun vector-subseq* (sequence start end)
388 (declare (type vector sequence))
389 (declare (type index start)
390 (type (or null index) end)
392 (with-array-data ((data sequence)
395 :check-fill-pointer t
397 (vector-subseq-dispatch data start end)))
399 (defun list-subseq* (sequence start end)
400 (declare (type list sequence)
401 (type unsigned-byte start)
402 (type (or null unsigned-byte) end))
404 (sequence-bounding-indices-bad-error sequence start end)))
405 (let ((pointer sequence))
406 (unless (zerop start)
407 ;; If START > 0 the list cannot be empty. So CDR down to
408 ;; it START-1 times, check that we still have something, then
409 ;; CDR the final time.
411 ;; If START was zero, the list may be empty if END is NIL or
414 (setf pointer (nthcdr (1- start) pointer)))
419 (let ((n (- end start)))
420 (declare (integer n))
424 (let* ((head (list nil))
426 (macrolet ((pop-one ()
427 `(let ((tmp (list (pop pointer))))
431 (loop until (fixnump n)
434 ;; Fixnum case, but leave last element, so we should
435 ;; still have something left in the sequence.
442 ;; OK, pop the last one.
446 collect (pop pointer))))))
448 (defun subseq (sequence start &optional end)
450 "Return a copy of a subsequence of SEQUENCE starting with element number
451 START and continuing to the end of SEQUENCE or the optional END."
452 (seq-dispatch sequence
453 (list-subseq* sequence start end)
454 (vector-subseq* sequence start end)
455 (sb!sequence:subseq sequence start end)))
459 (defun copy-seq (sequence)
460 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
461 (seq-dispatch sequence
462 (list-copy-seq* sequence)
463 (vector-subseq* sequence 0 nil)
464 (sb!sequence:copy-seq sequence)))
466 (defun list-copy-seq* (sequence)
467 (!copy-list-macro sequence :check-proper-list t))
471 (defun list-fill* (sequence item start end)
472 (declare (type list sequence)
473 (type unsigned-byte start)
474 (type (or null unsigned-byte) end))
476 (sequence-bounding-indices-bad-error sequence start end)))
477 (let ((pointer sequence))
478 (unless (zerop start)
479 ;; If START > 0 the list cannot be empty. So CDR down to it
480 ;; START-1 times, check that we still have something, then CDR
483 ;; If START was zero, the list may be empty if END is NIL or
486 (setf pointer (nthcdr (1- start) pointer)))
491 (let ((n (- end start)))
492 (declare (integer n))
497 do (setf pointer (cdr (rplaca pointer item))))))
499 do (setf pointer (cdr (rplaca pointer item)))))))
502 (defun vector-fill* (sequence item start end)
503 (with-array-data ((data sequence)
507 :check-fill-pointer t)
508 (let ((setter (!find-data-vector-setter data)))
509 (declare (optimize (speed 3) (safety 0)))
510 (do ((index start (1+ index)))
511 ((= index end) sequence)
512 (declare (index index))
513 (funcall setter data index item)))))
515 (defun string-fill* (sequence item start end)
516 (declare (string sequence))
517 (with-array-data ((data sequence)
521 :check-fill-pointer t)
522 ;; DEFTRANSFORM for FILL will turn these into
523 ;; calls to UB*-BASH-FILL.
526 ((simple-array character (*))
527 (let ((item (locally (declare (optimize (safety 3)))
528 (the character item))))
529 (fill data item :start start :end end)))
530 ((simple-array base-char (*))
531 (let ((item (locally (declare (optimize (safety 3)))
532 (the base-char item))))
533 (fill data item :start start :end end))))))
535 (defun fill (sequence item &key (start 0) end)
537 "Replace the specified elements of SEQUENCE with ITEM."
538 (seq-dispatch sequence
539 (list-fill* sequence item start end)
540 (vector-fill* sequence item start end)
541 (sb!sequence:fill sequence item
543 :end (%check-generic-sequence-bounds sequence start end))))
547 (eval-when (:compile-toplevel :execute)
549 ;;; If we are copying around in the same vector, be careful not to copy the
550 ;;; same elements over repeatedly. We do this by copying backwards.
551 (sb!xc:defmacro mumble-replace-from-mumble ()
552 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
553 (let ((nelts (min (- target-end target-start)
554 (- source-end source-start))))
555 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
557 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
559 ((= target-index (the fixnum (1- target-start))) target-sequence)
560 (declare (fixnum target-index source-index))
561 ;; disable bounds checking
562 (declare (optimize (safety 0)))
563 (setf (aref target-sequence target-index)
564 (aref source-sequence source-index))))
565 (do ((target-index target-start (1+ target-index))
566 (source-index source-start (1+ source-index)))
567 ((or (= target-index (the fixnum target-end))
568 (= source-index (the fixnum source-end)))
570 (declare (fixnum target-index source-index))
571 ;; disable bounds checking
572 (declare (optimize (safety 0)))
573 (setf (aref target-sequence target-index)
574 (aref source-sequence source-index)))))
576 (sb!xc:defmacro list-replace-from-list ()
577 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
578 (let ((new-elts (subseq source-sequence source-start
579 (+ (the fixnum source-start)
581 (min (- (the fixnum target-end)
582 (the fixnum target-start))
583 (- (the fixnum source-end)
584 (the fixnum source-start))))))))
585 (do ((n new-elts (cdr n))
586 (o (nthcdr target-start target-sequence) (cdr o)))
587 ((null n) target-sequence)
589 (do ((target-index target-start (1+ target-index))
590 (source-index source-start (1+ source-index))
591 (target-sequence-ref (nthcdr target-start target-sequence)
592 (cdr target-sequence-ref))
593 (source-sequence-ref (nthcdr source-start source-sequence)
594 (cdr source-sequence-ref)))
595 ((or (= target-index (the fixnum target-end))
596 (= source-index (the fixnum source-end))
597 (null target-sequence-ref) (null source-sequence-ref))
599 (declare (fixnum target-index source-index))
600 (rplaca target-sequence-ref (car source-sequence-ref)))))
602 (sb!xc:defmacro list-replace-from-mumble ()
603 `(do ((target-index target-start (1+ target-index))
604 (source-index source-start (1+ source-index))
605 (target-sequence-ref (nthcdr target-start target-sequence)
606 (cdr target-sequence-ref)))
607 ((or (= target-index (the fixnum target-end))
608 (= source-index (the fixnum source-end))
609 (null target-sequence-ref))
611 (declare (fixnum source-index target-index))
612 (rplaca target-sequence-ref (aref source-sequence source-index))))
614 (sb!xc:defmacro mumble-replace-from-list ()
615 `(do ((target-index target-start (1+ target-index))
616 (source-index source-start (1+ source-index))
617 (source-sequence (nthcdr source-start source-sequence)
618 (cdr source-sequence)))
619 ((or (= target-index (the fixnum target-end))
620 (= source-index (the fixnum source-end))
621 (null source-sequence))
623 (declare (fixnum target-index source-index))
624 (setf (aref target-sequence target-index) (car source-sequence))))
628 ;;;; The support routines for REPLACE are used by compiler transforms, so we
629 ;;;; worry about dealing with END being supplied or defaulting to NIL
632 (defun list-replace-from-list* (target-sequence source-sequence target-start
633 target-end source-start source-end)
634 (when (null target-end) (setq target-end (length target-sequence)))
635 (when (null source-end) (setq source-end (length source-sequence)))
636 (list-replace-from-list))
638 (defun list-replace-from-vector* (target-sequence source-sequence target-start
639 target-end source-start source-end)
640 (when (null target-end) (setq target-end (length target-sequence)))
641 (when (null source-end) (setq source-end (length source-sequence)))
642 (list-replace-from-mumble))
644 (defun vector-replace-from-list* (target-sequence source-sequence target-start
645 target-end source-start source-end)
646 (when (null target-end) (setq target-end (length target-sequence)))
647 (when (null source-end) (setq source-end (length source-sequence)))
648 (mumble-replace-from-list))
650 (defun vector-replace-from-vector* (target-sequence source-sequence
651 target-start target-end source-start
653 (when (null target-end) (setq target-end (length target-sequence)))
654 (when (null source-end) (setq source-end (length source-sequence)))
655 (mumble-replace-from-mumble))
658 (defun simple-character-string-replace-from-simple-character-string*
659 (target-sequence source-sequence
660 target-start target-end source-start source-end)
661 (declare (type (simple-array character (*)) target-sequence source-sequence))
662 (when (null target-end) (setq target-end (length target-sequence)))
663 (when (null source-end) (setq source-end (length source-sequence)))
664 (mumble-replace-from-mumble))
666 (define-sequence-traverser replace
667 (sequence1 sequence2 &rest args &key start1 end1 start2 end2)
669 "Destructively modifies SEQUENCE1 by copying successive elements
670 into it from the SEQUENCE2.
672 Elements are copied to the subseqeuence bounded by START1 and END1,
673 from the subsequence bounded by START2 and END2. If these subsequences
674 are not of the same length, then the shorter length determines how
675 many elements are copied."
676 (declare (truly-dynamic-extent args))
677 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
678 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
679 ;; these things here so that legacy code gets the names it's
680 ;; expecting. We could use &AUX instead :-/.
681 (target-sequence sequence1)
682 (source-sequence sequence2)
683 (target-start start1)
684 (source-start start2)
685 (target-end (or end1 length1))
686 (source-end (or end2 length2)))
687 (seq-dispatch target-sequence
688 (seq-dispatch source-sequence
689 (list-replace-from-list)
690 (list-replace-from-mumble)
691 (apply #'sb!sequence:replace sequence1 sequence2 args))
692 (seq-dispatch source-sequence
693 (mumble-replace-from-list)
694 (mumble-replace-from-mumble)
695 (apply #'sb!sequence:replace sequence1 sequence2 args))
696 (apply #'sb!sequence:replace sequence1 sequence2 args))))
700 (eval-when (:compile-toplevel :execute)
702 (sb!xc:defmacro vector-reverse (sequence)
703 `(let ((length (length ,sequence)))
704 (declare (fixnum length))
705 (do ((forward-index 0 (1+ forward-index))
706 (backward-index (1- length) (1- backward-index))
707 (new-sequence (%make-sequence-like sequence length)))
708 ((= forward-index length) new-sequence)
709 (declare (fixnum forward-index backward-index))
710 (setf (aref new-sequence forward-index)
711 (aref ,sequence backward-index)))))
713 (sb!xc:defmacro list-reverse-macro (sequence)
715 ((endp ,sequence) new-list)
716 (push (pop ,sequence) new-list)))
720 (defun reverse (sequence)
722 "Return a new sequence containing the same elements but in reverse order."
723 (seq-dispatch sequence
724 (list-reverse* sequence)
725 (vector-reverse* sequence)
726 (sb!sequence:reverse sequence)))
730 (defun list-reverse* (sequence)
731 (list-reverse-macro sequence))
733 (defun vector-reverse* (sequence)
734 (vector-reverse sequence))
738 (eval-when (:compile-toplevel :execute)
740 (sb!xc:defmacro vector-nreverse (sequence)
741 `(let ((length (length (the vector ,sequence))))
743 (do ((left-index 0 (1+ left-index))
744 (right-index (1- length) (1- right-index)))
745 ((<= right-index left-index))
746 (declare (type index left-index right-index))
747 (rotatef (aref ,sequence left-index)
748 (aref ,sequence right-index))))
751 (sb!xc:defmacro list-nreverse-macro (list)
752 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
760 (defun list-nreverse* (sequence)
761 (list-nreverse-macro sequence))
763 (defun vector-nreverse* (sequence)
764 (vector-nreverse sequence))
766 (defun nreverse (sequence)
768 "Return a sequence of the same elements in reverse order; the argument
770 (seq-dispatch sequence
771 (list-nreverse* sequence)
772 (vector-nreverse* sequence)
773 (sb!sequence:nreverse sequence)))
777 (defmacro sb!sequence:dosequence ((e sequence &optional return) &body body)
778 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
780 (sequence (gensym "SEQUENCE")))
782 (let ((,sequence ,s))
783 (seq-dispatch ,sequence
784 (dolist (,e ,sequence ,return) ,@body)
785 (do-vector-data (,e ,sequence ,return) ,@body)
786 (multiple-value-bind (state limit from-end step endp elt)
787 (sb!sequence:make-sequence-iterator ,sequence)
788 (do ((state state (funcall step ,sequence state from-end)))
789 ((funcall endp ,sequence state limit from-end)
791 ,@(filter-dolist-declarations decls)
794 (let ((,e (funcall elt ,sequence state)))
799 (defun concatenate (output-type-spec &rest sequences)
801 "Return a new sequence of all the argument sequences concatenated together
802 which shares no structure with the original argument sequences of the
803 specified OUTPUT-TYPE-SPEC."
804 (flet ((concat-to-list* (sequences)
805 (let ((result (list nil)))
806 (do ((sequences sequences (cdr sequences))
808 ((null sequences) (cdr result))
809 (let ((sequence (car sequences)))
810 (sb!sequence:dosequence (e sequence)
811 (setq splice (cdr (rplacd splice (list e)))))))))
812 (concat-to-simple* (type-spec sequences)
813 (do ((seqs sequences (cdr seqs))
817 (do ((sequences sequences (cdr sequences))
818 (lengths lengths (cdr lengths))
820 (result (make-sequence type-spec total-length)))
821 ((= index total-length) result)
822 (declare (fixnum index))
823 (let ((sequence (car sequences)))
824 (sb!sequence:dosequence (e sequence)
825 (setf (aref result index) e)
827 (let ((length (length (car seqs))))
828 (declare (fixnum length))
829 (setq lengths (nconc lengths (list length)))
830 (setq total-length (+ total-length length))))))
831 (let ((type (specifier-type output-type-spec)))
833 ((csubtypep type (specifier-type 'list))
835 ((type= type (specifier-type 'list))
836 (concat-to-list* sequences))
837 ((eq type *empty-type*)
838 (bad-sequence-type-error nil))
839 ((type= type (specifier-type 'null))
840 (if (every (lambda (x) (or (null x)
841 (and (vectorp x) (= (length x) 0))))
844 (sequence-type-length-mismatch-error
846 ;; FIXME: circular list issues.
847 (reduce #'+ sequences :key #'length))))
849 (multiple-value-bind (min exactp)
850 (sb!kernel::cons-type-length-info type)
851 (let ((length (reduce #'+ sequences :key #'length)))
853 (unless (= length min)
854 (sequence-type-length-mismatch-error type length))
855 (unless (>= length min)
856 (sequence-type-length-mismatch-error type length)))
857 (concat-to-list* sequences))))
858 (t (sequence-type-too-hairy (type-specifier type)))))
859 ((csubtypep type (specifier-type 'vector))
860 (concat-to-simple* output-type-spec sequences))
861 ((and (csubtypep type (specifier-type 'sequence))
862 (find-class output-type-spec nil))
863 (coerce (concat-to-simple* 'vector sequences) output-type-spec))
865 (bad-sequence-type-error output-type-spec))))))
867 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
868 ;;; CONCATENATE 'STRING &co into these.
869 (macrolet ((def (name element-type)
870 `(defun ,name (&rest sequences)
871 (declare (dynamic-extent sequences)
873 (optimize (sb!c::insert-array-bounds-checks 0)))
874 (let* ((lengths (mapcar #'length sequences))
875 (result (make-array (the integer (apply #'+ lengths))
876 :element-type ',element-type))
878 (declare (index start))
879 (dolist (seq sequences)
881 ((simple-array character (*))
882 (simple-array base-char (*))
885 (replace result seq :start1 start))
886 (incf start (the index (pop lengths))))
888 (def %concatenate-to-string character)
889 (def %concatenate-to-base-string base-char))
893 ;;; helper functions to handle arity-1 subcases of MAP
894 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
895 (declaim (ftype (function (function sequence) simple-vector)
896 %map-simple-vector-arity-1))
897 (defun %map-to-list-arity-1 (fun sequence)
898 (let ((reversed-result nil)
899 (really-fun (%coerce-callable-to-fun fun)))
900 (sb!sequence:dosequence (element sequence)
901 (push (funcall really-fun element)
903 (nreverse reversed-result)))
904 (defun %map-to-simple-vector-arity-1 (fun sequence)
905 (let ((result (make-array (length sequence)))
907 (really-fun (%coerce-callable-to-fun fun)))
908 (declare (type index index))
909 (sb!sequence:dosequence (element sequence)
910 (setf (aref result index)
911 (funcall really-fun element))
914 (defun %map-for-effect-arity-1 (fun sequence)
915 (let ((really-fun (%coerce-callable-to-fun fun)))
916 (sb!sequence:dosequence (element sequence)
917 (funcall really-fun element)))
920 (declaim (maybe-inline %map-for-effect))
921 (defun %map-for-effect (fun sequences)
922 (declare (type function fun) (type list sequences))
923 (let ((%sequences sequences)
924 (%iters (mapcar (lambda (s)
929 (sb!sequence:make-sequence-iterator s))))
931 (%apply-args (make-list (length sequences))))
932 ;; this is almost efficient (except in the general case where we
933 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
934 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
935 (declare (type list %sequences %iters %apply-args))
937 (do ((in-sequences %sequences (cdr in-sequences))
938 (in-iters %iters (cdr in-iters))
939 (in-apply-args %apply-args (cdr in-apply-args)))
940 ((null in-sequences) (apply fun %apply-args))
941 (let ((i (car in-iters)))
942 (declare (type (or list index) i))
944 ((listp (car in-sequences))
946 (return-from %map-for-effect nil)
947 (setf (car in-apply-args) (car i)
948 (car in-iters) (cdr i))))
950 (let ((v (the vector (car in-sequences))))
951 (if (>= i (length v))
952 (return-from %map-for-effect nil)
953 (setf (car in-apply-args) (aref v i)
954 (car in-iters) (1+ i)))))
956 (destructuring-bind (state limit from-end step endp elt &rest ignore)
958 (declare (type function step endp elt)
960 (let ((s (car in-sequences)))
961 (if (funcall endp s state limit from-end)
962 (return-from %map-for-effect nil)
964 (setf (car in-apply-args) (funcall elt s state))
965 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
966 (defun %map-to-list (fun sequences)
967 (declare (type function fun)
968 (type list sequences))
970 (flet ((f (&rest args)
971 (declare (truly-dynamic-extent args))
972 (push (apply fun args) result)))
973 (declare (truly-dynamic-extent #'f))
974 (%map-for-effect #'f sequences))
976 (defun %map-to-vector (output-type-spec fun sequences)
977 (declare (type function fun)
978 (type list sequences))
980 (flet ((f (&rest args)
981 (declare (truly-dynamic-extent args))
982 (declare (ignore args))
984 (declare (truly-dynamic-extent #'f))
985 (%map-for-effect #'f sequences))
986 (let ((result (make-sequence output-type-spec min-len))
988 (declare (type (simple-array * (*)) result))
989 (flet ((f (&rest args)
990 (declare (truly-dynamic-extent args))
991 (setf (aref result i) (apply fun args))
993 (declare (truly-dynamic-extent #'f))
994 (%map-for-effect #'f sequences))
996 (defun %map-to-sequence (result-type fun sequences)
997 (declare (type function fun)
998 (type list sequences))
1000 (flet ((f (&rest args)
1001 (declare (truly-dynamic-extent args))
1002 (declare (ignore args))
1004 (declare (truly-dynamic-extent #'f))
1005 (%map-for-effect #'f sequences))
1006 (let ((result (make-sequence result-type min-len)))
1007 (multiple-value-bind (state limit from-end step endp elt setelt)
1008 (sb!sequence:make-sequence-iterator result)
1009 (declare (ignore limit endp elt))
1010 (flet ((f (&rest args)
1011 (declare (truly-dynamic-extent args))
1012 (funcall setelt (apply fun args) result state)
1013 (setq state (funcall step result state from-end))))
1014 (declare (truly-dynamic-extent #'f))
1015 (%map-for-effect #'f sequences)))
1018 ;;; %MAP is just MAP without the final just-to-be-sure check that
1019 ;;; length of the output sequence matches any length specified
1021 (defun %map (result-type function first-sequence &rest more-sequences)
1022 (let ((really-fun (%coerce-callable-to-fun function))
1023 (type (specifier-type result-type)))
1024 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
1025 ;; it into something which can be DEFTRANSFORMed away. (It's
1026 ;; fairly important to handle this case efficiently, since
1027 ;; quantifiers like SOME are transformed into this case, and since
1028 ;; there's no consing overhead to dwarf our inefficiency.)
1029 (if (and (null more-sequences)
1031 (%map-for-effect-arity-1 really-fun first-sequence)
1032 ;; Otherwise, use the industrial-strength full-generality
1033 ;; approach, consing O(N-ARGS) temporary storage (which can have
1034 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
1035 (let ((sequences (cons first-sequence more-sequences)))
1037 ((eq type *empty-type*) (%map-for-effect really-fun sequences))
1038 ((csubtypep type (specifier-type 'list))
1039 (%map-to-list really-fun sequences))
1040 ((csubtypep type (specifier-type 'vector))
1041 (%map-to-vector result-type really-fun sequences))
1042 ((and (csubtypep type (specifier-type 'sequence))
1043 (find-class result-type nil))
1044 (%map-to-sequence result-type really-fun sequences))
1046 (bad-sequence-type-error result-type)))))))
1048 (defun map (result-type function first-sequence &rest more-sequences)
1057 (defmacro map-into-lambda (sequences params &body body)
1058 (check-type sequences symbol)
1059 `(flet ((f ,params ,@body))
1060 (declare (truly-dynamic-extent #'f))
1061 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1062 ;; hence the awkward flip between MAP and LOOP.
1064 (apply #'map nil #'f ,sequences)
1067 (define-array-dispatch vector-map-into (data start end fun sequences)
1068 (declare (optimize speed (safety 0))
1069 (type index start end)
1071 (type list sequences))
1072 (let ((index start))
1073 (declare (type index index))
1075 (map-into-lambda sequences (&rest args)
1076 (declare (truly-dynamic-extent args))
1077 (when (eql index end)
1078 (return-from mapping))
1079 (setf (aref data index) (apply fun args))
1083 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1084 ;;; computing the length of the result sequence since we can detect
1085 ;;; the end during mapping (if MAP even gets that far).
1087 ;;; For each result type, define a mapping function which is
1088 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1089 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1090 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1092 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1093 ;;; Because we are manually doing bounds checking with known types,
1094 ;;; safety is turned off for vectors and lists but kept for generic
1096 (defun map-into (result-sequence function &rest sequences)
1097 (let ((really-fun (%coerce-callable-to-fun function)))
1098 (etypecase result-sequence
1100 (with-array-data ((data result-sequence) (start) (end)
1101 ;; MAP-INTO ignores fill pointer when mapping
1102 :check-fill-pointer nil)
1103 (let ((new-end (vector-map-into data start end really-fun sequences)))
1104 (when (array-has-fill-pointer-p result-sequence)
1105 (setf (fill-pointer result-sequence) (- new-end start))))))
1107 (let ((node result-sequence))
1108 (declare (type list node))
1109 (map-into-lambda sequences (&rest args)
1110 (declare (truly-dynamic-extent args)
1111 (optimize speed (safety 0)))
1113 (return-from map-into result-sequence))
1114 (setf (car node) (apply really-fun args))
1115 (setf node (cdr node)))))
1117 (multiple-value-bind (iter limit from-end)
1118 (sb!sequence:make-sequence-iterator result-sequence)
1119 (map-into-lambda sequences (&rest args)
1120 (declare (truly-dynamic-extent args) (optimize speed))
1121 (when (sb!sequence:iterator-endp result-sequence
1122 iter limit from-end)
1123 (return-from map-into result-sequence))
1124 (setf (sb!sequence:iterator-element result-sequence iter)
1125 (apply really-fun args))
1126 (setf iter (sb!sequence:iterator-step result-sequence
1127 iter from-end)))))))
1132 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
1133 ;;; arbitrary sequence arguments, both in the full call case and in
1134 ;;; the open code case.
1135 (macrolet ((defquantifier (name found-test found-result
1136 &key doc (unfound-result (not found-result)))
1138 ;; KLUDGE: It would be really nice if we could simply
1139 ;; do something like this
1140 ;; (declaim (inline ,name))
1141 ;; (defun ,name (pred first-seq &rest more-seqs)
1143 ;; (flet ((map-me (&rest rest)
1144 ;; (let ((pred-value (apply pred rest)))
1145 ;; (,found-test pred-value
1146 ;; (return-from ,name
1147 ;; ,found-result)))))
1148 ;; (declare (inline map-me))
1149 ;; (apply #'map nil #'map-me first-seq more-seqs)
1150 ;; ,unfound-result))
1151 ;; but Python doesn't seem to be smart enough about
1152 ;; inlining and APPLY to recognize that it can use
1153 ;; the DEFTRANSFORM for MAP in the resulting inline
1154 ;; expansion. I don't have any appetite for deep
1155 ;; compiler hacking right now, so I'll just work
1156 ;; around the apparent problem by using a compiler
1157 ;; macro instead. -- WHN 20000410
1158 (defun ,name (pred first-seq &rest more-seqs)
1160 (flet ((map-me (&rest rest)
1161 (let ((pred-value (apply pred rest)))
1162 (,found-test pred-value
1165 (declare (inline map-me))
1166 (apply #'map nil #'map-me first-seq more-seqs)
1168 ;; KLUDGE: It would be more obviously correct -- but
1169 ;; also significantly messier -- for PRED-VALUE to be
1170 ;; a gensym. However, a private symbol really does
1171 ;; seem to be good enough; and anyway the really
1172 ;; obviously correct solution is to make Python smart
1173 ;; enough that we can use an inline function instead
1174 ;; of a compiler macro (as above). -- WHN 20000410
1176 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
1177 ;; important for performance, and it'd be good to have
1178 ;; it be visible throughout the compilation of all the
1179 ;; target SBCL code. That could be done by defining
1180 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
1181 ;; moving this DEFQUANTIFIER stuff (and perhaps other
1182 ;; inline definitions in seq.lisp as well) into a new
1183 ;; seq.lisp, and moving remaining target-only stuff
1184 ;; from the old seq.lisp into target-seq.lisp.
1185 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
1186 (let ((elements (make-gensym-list (1+ (length more-seqs))))
1187 (blockname (sb!xc:gensym "BLOCK")))
1188 (once-only ((pred pred))
1191 (lambda (,@elements)
1192 (let ((pred-value (funcall ,pred ,@elements)))
1193 (,',found-test pred-value
1194 (return-from ,blockname
1198 ,',unfound-result)))))))
1199 (defquantifier some when pred-value :unfound-result nil :doc
1200 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1201 possibly to those with index 1, and so on. Return the first
1202 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1203 (defquantifier every unless nil :doc
1204 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1205 possibly to those with index 1, and so on. Return NIL as soon
1206 as any invocation of PREDICATE returns NIL, or T if every invocation
1208 (defquantifier notany when nil :doc
1209 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1210 possibly to those with index 1, and so on. Return NIL as soon
1211 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1212 of any sequence is reached.")
1213 (defquantifier notevery unless t :doc
1214 "Apply PREDICATE to 0-indexed elements of the sequences, then
1215 possibly to those with index 1, and so on. Return T as soon
1216 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1221 (eval-when (:compile-toplevel :execute)
1223 (sb!xc:defmacro mumble-reduce (function
1230 `(do ((index ,start (1+ index))
1231 (value ,initial-value))
1232 ((>= index ,end) value)
1233 (setq value (funcall ,function value
1234 (apply-key ,key (,ref ,sequence index))))))
1236 (sb!xc:defmacro mumble-reduce-from-end (function
1243 `(do ((index (1- ,end) (1- index))
1244 (value ,initial-value)
1245 (terminus (1- ,start)))
1246 ((<= index terminus) value)
1247 (setq value (funcall ,function
1248 (apply-key ,key (,ref ,sequence index))
1251 (sb!xc:defmacro list-reduce (function
1258 `(let ((sequence (nthcdr ,start ,sequence)))
1259 (do ((count (if ,ivp ,start (1+ ,start))
1261 (sequence (if ,ivp sequence (cdr sequence))
1263 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1264 (funcall ,function value (apply-key ,key (car sequence)))))
1265 ((>= count ,end) value))))
1267 (sb!xc:defmacro list-reduce-from-end (function
1274 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1275 (reverse ,sequence))))
1276 (do ((count (if ,ivp ,start (1+ ,start))
1278 (sequence (if ,ivp sequence (cdr sequence))
1280 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1281 (funcall ,function (apply-key ,key (car sequence)) value)))
1282 ((>= count ,end) value))))
1286 (define-sequence-traverser reduce (function sequence &rest args &key key
1287 from-end start end (initial-value nil ivp))
1288 (declare (type index start))
1289 (declare (truly-dynamic-extent args))
1291 (end (or end length)))
1292 (declare (type index start end))
1293 (seq-dispatch sequence
1295 (if ivp initial-value (funcall function))
1297 (list-reduce-from-end function sequence key start end
1299 (list-reduce function sequence key start end
1300 initial-value ivp)))
1302 (if ivp initial-value (funcall function))
1306 (setq end (1- (the fixnum end)))
1307 (setq initial-value (apply-key key (aref sequence end))))
1308 (mumble-reduce-from-end function sequence key start end
1309 initial-value aref))
1312 (setq initial-value (apply-key key (aref sequence start)))
1313 (setq start (1+ start)))
1314 (mumble-reduce function sequence key start end
1315 initial-value aref))))
1316 (apply #'sb!sequence:reduce function sequence args))))
1320 (eval-when (:compile-toplevel :execute)
1322 (sb!xc:defmacro mumble-delete (pred)
1323 `(do ((index start (1+ index))
1326 ((or (= index (the fixnum end)) (= number-zapped count))
1327 (do ((index index (1+ index)) ; Copy the rest of the vector.
1328 (jndex jndex (1+ jndex)))
1329 ((= index (the fixnum length))
1330 (shrink-vector sequence jndex))
1331 (declare (fixnum index jndex))
1332 (setf (aref sequence jndex) (aref sequence index))))
1333 (declare (fixnum index jndex number-zapped))
1334 (setf (aref sequence jndex) (aref sequence index))
1336 (incf number-zapped)
1339 (sb!xc:defmacro mumble-delete-from-end (pred)
1340 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1344 (terminus (1- start)))
1345 ((or (= index terminus) (= number-zapped count))
1346 (do ((losers losers) ; Delete the losers.
1347 (index start (1+ index))
1349 ((or (null losers) (= index (the fixnum end)))
1350 (do ((index index (1+ index)) ; Copy the rest of the vector.
1351 (jndex jndex (1+ jndex)))
1352 ((= index (the fixnum length))
1353 (shrink-vector sequence jndex))
1354 (declare (fixnum index jndex))
1355 (setf (aref sequence jndex) (aref sequence index))))
1356 (declare (fixnum index jndex))
1357 (setf (aref sequence jndex) (aref sequence index))
1358 (if (= index (the fixnum (car losers)))
1361 (declare (fixnum index number-zapped terminus))
1362 (setq this-element (aref sequence index))
1364 (incf number-zapped)
1365 (push index losers))))
1367 (sb!xc:defmacro normal-mumble-delete ()
1370 (not (funcall test-not item (apply-key key (aref sequence index))))
1371 (funcall test item (apply-key key (aref sequence index))))))
1373 (sb!xc:defmacro normal-mumble-delete-from-end ()
1374 `(mumble-delete-from-end
1376 (not (funcall test-not item (apply-key key this-element)))
1377 (funcall test item (apply-key key this-element)))))
1379 (sb!xc:defmacro list-delete (pred)
1380 `(let ((handle (cons nil sequence)))
1381 (do ((current (nthcdr start sequence) (cdr current))
1382 (previous (nthcdr start handle))
1383 (index start (1+ index))
1385 ((or (= index (the fixnum end)) (= number-zapped count))
1387 (declare (fixnum index number-zapped))
1389 (rplacd previous (cdr current))
1390 (incf number-zapped))
1392 (setq previous (cdr previous)))))))
1394 (sb!xc:defmacro list-delete-from-end (pred)
1395 `(let* ((reverse (nreverse (the list sequence)))
1396 (handle (cons nil reverse)))
1397 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1399 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1400 (index start (1+ index))
1402 ((or (= index (the fixnum end)) (= number-zapped count))
1403 (nreverse (cdr handle)))
1404 (declare (fixnum index number-zapped))
1406 (rplacd previous (cdr current))
1407 (incf number-zapped))
1409 (setq previous (cdr previous)))))))
1411 (sb!xc:defmacro normal-list-delete ()
1414 (not (funcall test-not item (apply-key key (car current))))
1415 (funcall test item (apply-key key (car current))))))
1417 (sb!xc:defmacro normal-list-delete-from-end ()
1418 '(list-delete-from-end
1420 (not (funcall test-not item (apply-key key (car current))))
1421 (funcall test item (apply-key key (car current))))))
1425 (define-sequence-traverser delete
1426 (item sequence &rest args &key from-end test test-not start
1429 "Return a sequence formed by destructively removing the specified ITEM from
1430 the given SEQUENCE."
1431 (declare (fixnum start))
1432 (declare (truly-dynamic-extent args))
1433 (let ((end (or end length)))
1434 (declare (type index end))
1435 (seq-dispatch sequence
1437 (normal-list-delete-from-end)
1438 (normal-list-delete))
1440 (normal-mumble-delete-from-end)
1441 (normal-mumble-delete))
1442 (apply #'sb!sequence:delete item sequence args))))
1444 (eval-when (:compile-toplevel :execute)
1446 (sb!xc:defmacro if-mumble-delete ()
1448 (funcall predicate (apply-key key (aref sequence index)))))
1450 (sb!xc:defmacro if-mumble-delete-from-end ()
1451 `(mumble-delete-from-end
1452 (funcall predicate (apply-key key this-element))))
1454 (sb!xc:defmacro if-list-delete ()
1456 (funcall predicate (apply-key key (car current)))))
1458 (sb!xc:defmacro if-list-delete-from-end ()
1459 '(list-delete-from-end
1460 (funcall predicate (apply-key key (car current)))))
1464 (define-sequence-traverser delete-if
1465 (predicate sequence &rest args &key from-end start key end count)
1467 "Return a sequence formed by destructively removing the elements satisfying
1468 the specified PREDICATE from the given SEQUENCE."
1469 (declare (fixnum start))
1470 (declare (truly-dynamic-extent args))
1471 (let ((end (or end length)))
1472 (declare (type index end))
1473 (seq-dispatch sequence
1475 (if-list-delete-from-end)
1478 (if-mumble-delete-from-end)
1480 (apply #'sb!sequence:delete-if predicate sequence args))))
1482 (eval-when (:compile-toplevel :execute)
1484 (sb!xc:defmacro if-not-mumble-delete ()
1486 (not (funcall predicate (apply-key key (aref sequence index))))))
1488 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1489 `(mumble-delete-from-end
1490 (not (funcall predicate (apply-key key this-element)))))
1492 (sb!xc:defmacro if-not-list-delete ()
1494 (not (funcall predicate (apply-key key (car current))))))
1496 (sb!xc:defmacro if-not-list-delete-from-end ()
1497 '(list-delete-from-end
1498 (not (funcall predicate (apply-key key (car current))))))
1502 (define-sequence-traverser delete-if-not
1503 (predicate sequence &rest args &key from-end start end key count)
1505 "Return a sequence formed by destructively removing the elements not
1506 satisfying the specified PREDICATE from the given SEQUENCE."
1507 (declare (fixnum start))
1508 (declare (truly-dynamic-extent args))
1509 (let ((end (or end length)))
1510 (declare (type index end))
1511 (seq-dispatch sequence
1513 (if-not-list-delete-from-end)
1514 (if-not-list-delete))
1516 (if-not-mumble-delete-from-end)
1517 (if-not-mumble-delete))
1518 (apply #'sb!sequence:delete-if-not predicate sequence args))))
1522 (eval-when (:compile-toplevel :execute)
1524 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1525 ;;; satisfies the predicate.
1526 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1527 `(do ((index ,begin (,bump index))
1529 (do ((index ,left (,bump index))
1530 (result (%make-sequence-like sequence length)))
1531 ((= index (the fixnum ,begin)) result)
1532 (declare (fixnum index))
1533 (setf (aref result index) (aref sequence index))))
1537 ((or (= index (the fixnum ,finish))
1538 (= number-zapped count))
1539 (do ((index index (,bump index))
1540 (new-index new-index (,bump new-index)))
1541 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1542 (declare (fixnum index new-index))
1543 (setf (aref result new-index) (aref sequence index))))
1544 (declare (fixnum index new-index number-zapped))
1545 (setq this-element (aref sequence index))
1546 (cond (,pred (incf number-zapped))
1547 (t (setf (aref result new-index) this-element)
1548 (setq new-index (,bump new-index))))))
1550 (sb!xc:defmacro mumble-remove (pred)
1551 `(mumble-remove-macro 1+ 0 start end length ,pred))
1553 (sb!xc:defmacro mumble-remove-from-end (pred)
1554 `(let ((sequence (copy-seq sequence)))
1555 (mumble-delete-from-end ,pred)))
1557 (sb!xc:defmacro normal-mumble-remove ()
1560 (not (funcall test-not item (apply-key key this-element)))
1561 (funcall test item (apply-key key this-element)))))
1563 (sb!xc:defmacro normal-mumble-remove-from-end ()
1564 `(mumble-remove-from-end
1566 (not (funcall test-not item (apply-key key this-element)))
1567 (funcall test item (apply-key key this-element)))))
1569 (sb!xc:defmacro if-mumble-remove ()
1570 `(mumble-remove (funcall predicate (apply-key key this-element))))
1572 (sb!xc:defmacro if-mumble-remove-from-end ()
1573 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1575 (sb!xc:defmacro if-not-mumble-remove ()
1576 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1578 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1579 `(mumble-remove-from-end
1580 (not (funcall predicate (apply-key key this-element)))))
1582 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1584 (sb!xc:defmacro list-remove-macro (pred reverse?)
1585 `(let* ((sequence ,(if reverse?
1586 '(reverse (the list sequence))
1588 (%start ,(if reverse? '(- length end) 'start))
1589 (%end ,(if reverse? '(- length start) 'end))
1591 (results (do ((index 0 (1+ index))
1592 (before-start splice))
1593 ((= index (the fixnum %start)) before-start)
1594 (declare (fixnum index))
1596 (cdr (rplacd splice (list (pop sequence))))))))
1597 (do ((index %start (1+ index))
1600 ((or (= index (the fixnum %end)) (= number-zapped count))
1601 (do ((index index (1+ index)))
1604 '(nreverse (the list (cdr results)))
1606 (declare (fixnum index))
1607 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1608 (declare (fixnum index number-zapped))
1609 (setq this-element (pop sequence))
1611 (setq number-zapped (1+ number-zapped))
1612 (setq splice (cdr (rplacd splice (list this-element))))))))
1614 (sb!xc:defmacro list-remove (pred)
1615 `(list-remove-macro ,pred nil))
1617 (sb!xc:defmacro list-remove-from-end (pred)
1618 `(list-remove-macro ,pred t))
1620 (sb!xc:defmacro normal-list-remove ()
1623 (not (funcall test-not item (apply-key key this-element)))
1624 (funcall test item (apply-key key this-element)))))
1626 (sb!xc:defmacro normal-list-remove-from-end ()
1627 `(list-remove-from-end
1629 (not (funcall test-not item (apply-key key this-element)))
1630 (funcall test item (apply-key key this-element)))))
1632 (sb!xc:defmacro if-list-remove ()
1634 (funcall predicate (apply-key key this-element))))
1636 (sb!xc:defmacro if-list-remove-from-end ()
1637 `(list-remove-from-end
1638 (funcall predicate (apply-key key this-element))))
1640 (sb!xc:defmacro if-not-list-remove ()
1642 (not (funcall predicate (apply-key key this-element)))))
1644 (sb!xc:defmacro if-not-list-remove-from-end ()
1645 `(list-remove-from-end
1646 (not (funcall predicate (apply-key key this-element)))))
1650 (define-sequence-traverser remove
1651 (item sequence &rest args &key from-end test test-not start
1654 "Return a copy of SEQUENCE with elements satisfying the test (default is
1655 EQL) with ITEM removed."
1656 (declare (fixnum start))
1657 (declare (truly-dynamic-extent args))
1658 (let ((end (or end length)))
1659 (declare (type index end))
1660 (seq-dispatch sequence
1662 (normal-list-remove-from-end)
1663 (normal-list-remove))
1665 (normal-mumble-remove-from-end)
1666 (normal-mumble-remove))
1667 (apply #'sb!sequence:remove item sequence args))))
1669 (define-sequence-traverser remove-if
1670 (predicate sequence &rest args &key from-end start end count key)
1672 "Return a copy of sequence with elements satisfying PREDICATE removed."
1673 (declare (fixnum start))
1674 (declare (truly-dynamic-extent args))
1675 (let ((end (or end length)))
1676 (declare (type index end))
1677 (seq-dispatch sequence
1679 (if-list-remove-from-end)
1682 (if-mumble-remove-from-end)
1684 (apply #'sb!sequence:remove-if predicate sequence args))))
1686 (define-sequence-traverser remove-if-not
1687 (predicate sequence &rest args &key from-end start end count key)
1689 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1690 (declare (fixnum start))
1691 (declare (truly-dynamic-extent args))
1692 (let ((end (or end length)))
1693 (declare (type index end))
1694 (seq-dispatch sequence
1696 (if-not-list-remove-from-end)
1697 (if-not-list-remove))
1699 (if-not-mumble-remove-from-end)
1700 (if-not-mumble-remove))
1701 (apply #'sb!sequence:remove-if-not predicate sequence args))))
1703 ;;;; REMOVE-DUPLICATES
1705 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1706 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1707 ;;; if we look into the already copied structure (from after :start) and see
1708 ;;; the item. If we check from beginning we check into the rest of the
1709 ;;; original list up to the :end marker (this we have to do by running a
1710 ;;; do loop down the list that far and using our test.
1711 (defun list-remove-duplicates* (list test test-not start end key from-end)
1712 (declare (fixnum start))
1713 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1716 (end (or end (length list)))
1717 (hash (and (> (- end start) 20)
1721 (or (eql test #'eql)
1724 (eql test #'equalp))
1725 (make-hash-table :test test :size (- end start)))))
1726 (do ((index 0 (1+ index)))
1728 (declare (fixnum index))
1729 (setq splice (cdr (rplacd splice (list (car current)))))
1730 (setq current (cdr current)))
1732 (do ((index start (1+ index)))
1733 ((or (and end (= index (the fixnum end)))
1735 (declare (fixnum index))
1736 ;; The hash table contains links from values that are
1737 ;; already in result to the cons cell *preceding* theirs
1738 ;; in the list. That is, for each value v in the list,
1739 ;; v and (cadr (gethash v hash)) are equal under TEST.
1740 (let ((prev (gethash (car current) hash)))
1743 (setf (gethash (car current) hash) splice)
1744 (setq splice (cdr (rplacd splice (list (car current))))))
1746 (let* ((old (cdr prev))
1749 (let ((next-val (car next)))
1750 ;; (assert (eq (gethash next-val hash) old))
1751 (setf (cdr prev) next
1752 (gethash next-val hash) prev
1753 (gethash (car current) hash) splice
1754 splice (cdr (rplacd splice (list (car current))))))
1755 (setf (car old) (car current)))))))
1756 (setq current (cdr current)))
1757 (do ((index start (1+ index)))
1758 ((or (and end (= index (the fixnum end)))
1760 (declare (fixnum index))
1761 (if (or (and from-end
1763 (member (apply-key key (car current))
1764 (nthcdr (1+ start) result)
1767 (member (apply-key key (car current))
1768 (nthcdr (1+ start) result)
1772 (not (do ((it (apply-key key (car current)))
1773 (l (cdr current) (cdr l))
1774 (i (1+ index) (1+ i)))
1775 ((or (atom l) (and end (= i (the fixnum end))))
1777 (declare (fixnum i))
1779 (not (funcall test-not
1781 (apply-key key (car l))))
1782 (funcall test it (apply-key key (car l))))
1784 (setq splice (cdr (rplacd splice (list (car current))))))
1785 (setq current (cdr current))))
1788 (setq splice (cdr (rplacd splice (list (car current)))))
1789 (setq current (cdr current)))
1792 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1793 &optional (length (length vector)))
1794 (declare (vector vector) (fixnum start length))
1795 (when (null end) (setf end (length vector)))
1796 (let ((result (%make-sequence-like vector length))
1799 (declare (fixnum index jndex))
1802 (setf (aref result index) (aref vector index))
1803 (setq index (1+ index)))
1806 (setq elt (aref vector index))
1807 (unless (or (and from-end
1809 (position (apply-key key elt) result
1810 :start start :end jndex
1811 :test-not test-not :key key)
1812 (position (apply-key key elt) result
1813 :start start :end jndex
1814 :test test :key key)))
1817 (position (apply-key key elt) vector
1818 :start (1+ index) :end end
1819 :test-not test-not :key key)
1820 (position (apply-key key elt) vector
1821 :start (1+ index) :end end
1822 :test test :key key))))
1823 (setf (aref result jndex) elt)
1824 (setq jndex (1+ jndex)))
1825 (setq index (1+ index)))
1828 (setf (aref result jndex) (aref vector index))
1829 (setq index (1+ index))
1830 (setq jndex (1+ jndex)))
1831 (%shrink-vector result jndex)))
1833 (define-sequence-traverser remove-duplicates
1834 (sequence &rest args &key test test-not start end from-end key)
1836 "The elements of SEQUENCE are compared pairwise, and if any two match,
1837 the one occurring earlier is discarded, unless FROM-END is true, in
1838 which case the one later in the sequence is discarded. The resulting
1839 sequence is returned.
1841 The :TEST-NOT argument is deprecated."
1842 (declare (fixnum start))
1843 (declare (truly-dynamic-extent args))
1844 (seq-dispatch sequence
1846 (list-remove-duplicates* sequence test test-not
1847 start end key from-end))
1848 (vector-remove-duplicates* sequence test test-not start end key from-end)
1849 (apply #'sb!sequence:remove-duplicates sequence args)))
1851 ;;;; DELETE-DUPLICATES
1853 (defun list-delete-duplicates* (list test test-not key from-end start end)
1854 (declare (fixnum start))
1855 (let ((handle (cons nil list)))
1856 (do ((current (nthcdr start list) (cdr current))
1857 (previous (nthcdr start handle))
1858 (index start (1+ index)))
1859 ((or (and end (= index (the fixnum end))) (null current))
1861 (declare (fixnum index))
1862 (if (do ((x (if from-end
1863 (nthcdr (1+ start) handle)
1866 (i (1+ index) (1+ i)))
1868 (and (not from-end) end (= i (the fixnum end)))
1871 (declare (fixnum i))
1873 (not (funcall test-not
1874 (apply-key key (car current))
1875 (apply-key key (car x))))
1877 (apply-key key (car current))
1878 (apply-key key (car x))))
1880 (rplacd previous (cdr current))
1881 (setq previous (cdr previous))))))
1883 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1884 &optional (length (length vector)))
1885 (declare (vector vector) (fixnum start length))
1886 (when (null end) (setf end (length vector)))
1887 (do ((index start (1+ index))
1890 (do ((index index (1+ index)) ; copy the rest of the vector
1891 (jndex jndex (1+ jndex)))
1893 (shrink-vector vector jndex))
1894 (setf (aref vector jndex) (aref vector index))))
1895 (declare (fixnum index jndex))
1896 (setf (aref vector jndex) (aref vector index))
1897 (unless (if test-not
1898 (position (apply-key key (aref vector index)) vector :key key
1899 :start (if from-end start (1+ index))
1900 :end (if from-end jndex end)
1902 (position (apply-key key (aref vector index)) vector :key key
1903 :start (if from-end start (1+ index))
1904 :end (if from-end jndex end)
1906 (setq jndex (1+ jndex)))))
1908 (define-sequence-traverser delete-duplicates
1909 (sequence &rest args &key test test-not start end from-end key)
1911 "The elements of SEQUENCE are examined, and if any two match, one is
1912 discarded. The resulting sequence, which may be formed by destroying the
1913 given sequence, is returned.
1915 The :TEST-NOT argument is deprecated."
1916 (declare (truly-dynamic-extent args))
1917 (seq-dispatch sequence
1919 (list-delete-duplicates* sequence test test-not
1920 key from-end start end))
1921 (vector-delete-duplicates* sequence test test-not key from-end start end)
1922 (apply #'sb!sequence:delete-duplicates sequence args)))
1926 (defun list-substitute* (pred new list start end count key test test-not old)
1927 (declare (fixnum start end count))
1928 (let* ((result (list nil))
1931 (list list)) ; Get a local list for a stepper.
1932 (do ((index 0 (1+ index)))
1934 (declare (fixnum index))
1935 (setq splice (cdr (rplacd splice (list (car list)))))
1936 (setq list (cdr list)))
1937 (do ((index start (1+ index)))
1938 ((or (= index end) (null list) (= count 0)))
1939 (declare (fixnum index))
1940 (setq elt (car list))
1949 (funcall test-not old (apply-key key elt)))
1950 (funcall test old (apply-key key elt))))
1951 (if (funcall test (apply-key key elt)))
1952 (if-not (not (funcall test (apply-key key elt)))))
1956 (setq list (cdr list)))
1959 (setq splice (cdr (rplacd splice (list (car list)))))
1960 (setq list (cdr list)))
1963 ;;; Replace old with new in sequence moving from left to right by incrementer
1964 ;;; on each pass through the loop. Called by all three substitute functions.
1965 (defun vector-substitute* (pred new sequence incrementer left right length
1966 start end count key test test-not old)
1967 (declare (fixnum start count end incrementer right))
1968 (let ((result (%make-sequence-like sequence length))
1970 (declare (fixnum index))
1973 (setf (aref result index) (aref sequence index))
1974 (setq index (+ index incrementer)))
1976 ((or (= index end) (= count 0)))
1977 (setq elt (aref sequence index))
1978 (setf (aref result index)
1982 (not (funcall test-not old (apply-key key elt)))
1983 (funcall test old (apply-key key elt))))
1984 (if (funcall test (apply-key key elt)))
1985 (if-not (not (funcall test (apply-key key elt)))))
1986 (setq count (1- count))
1989 (setq index (+ index incrementer)))
1992 (setf (aref result index) (aref sequence index))
1993 (setq index (+ index incrementer)))
1996 (eval-when (:compile-toplevel :execute)
1998 (sb!xc:defmacro subst-dispatch (pred)
1999 `(seq-dispatch sequence
2001 (nreverse (list-substitute* ,pred
2004 (- (the fixnum length)
2006 (- (the fixnum length)
2008 count key test test-not old))
2009 (list-substitute* ,pred
2010 new sequence start end count key test test-not
2013 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2014 -1 length (1- (the fixnum end))
2015 (1- (the fixnum start))
2016 count key test test-not old)
2017 (vector-substitute* ,pred new sequence 1 0 length length
2018 start end count key test test-not old))
2019 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2020 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2021 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2022 ;; dispatch once per element on PRED's run-time identity.
2024 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2025 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2026 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2029 (define-sequence-traverser substitute
2030 (new old sequence &rest args &key from-end test test-not
2031 start count end key)
2033 "Return a sequence of the same kind as SEQUENCE with the same elements,
2034 except that all elements equal to OLD are replaced with NEW."
2035 (declare (fixnum start))
2036 (declare (truly-dynamic-extent args))
2037 (let ((end (or end length)))
2038 (declare (type index end))
2039 (subst-dispatch 'normal)))
2041 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2043 (define-sequence-traverser substitute-if
2044 (new predicate sequence &rest args &key from-end start end count key)
2046 "Return a sequence of the same kind as SEQUENCE with the same elements
2047 except that all elements satisfying the PRED are replaced with NEW."
2048 (declare (truly-dynamic-extent args))
2049 (declare (fixnum start))
2050 (let ((end (or end length))
2054 (declare (type index length end))
2055 (subst-dispatch 'if)))
2057 (define-sequence-traverser substitute-if-not
2058 (new predicate sequence &rest args &key from-end start end count key)
2060 "Return a sequence of the same kind as SEQUENCE with the same elements
2061 except that all elements not satisfying the PRED are replaced with NEW."
2062 (declare (truly-dynamic-extent args))
2063 (declare (fixnum start))
2064 (let ((end (or end length))
2068 (declare (type index length end))
2069 (subst-dispatch 'if-not)))
2073 (define-sequence-traverser nsubstitute
2074 (new old sequence &rest args &key from-end test test-not
2075 end count key start)
2077 "Return a sequence of the same kind as SEQUENCE with the same elements
2078 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2079 may be destructively modified."
2080 (declare (fixnum start))
2081 (declare (truly-dynamic-extent args))
2082 (let ((end (or end length)))
2083 (seq-dispatch sequence
2085 (let ((length (length sequence)))
2086 (nreverse (nlist-substitute*
2087 new old (nreverse (the list sequence))
2088 test test-not (- length end) (- length start)
2090 (nlist-substitute* new old sequence
2091 test test-not start end count key))
2093 (nvector-substitute* new old sequence -1
2094 test test-not (1- end) (1- start) count key)
2095 (nvector-substitute* new old sequence 1
2096 test test-not start end count key))
2097 (apply #'sb!sequence:nsubstitute new old sequence args))))
2099 (defun nlist-substitute* (new old sequence test test-not start end count key)
2100 (declare (fixnum start count end))
2101 (do ((list (nthcdr start sequence) (cdr list))
2102 (index start (1+ index)))
2103 ((or (= index end) (null list) (= count 0)) sequence)
2104 (declare (fixnum index))
2106 (not (funcall test-not old (apply-key key (car list))))
2107 (funcall test old (apply-key key (car list))))
2109 (setq count (1- count)))))
2111 (defun nvector-substitute* (new old sequence incrementer
2112 test test-not start end count key)
2113 (declare (fixnum start incrementer count end))
2114 (do ((index start (+ index incrementer)))
2115 ((or (= index end) (= count 0)) sequence)
2116 (declare (fixnum index))
2118 (not (funcall test-not
2120 (apply-key key (aref sequence index))))
2121 (funcall test old (apply-key key (aref sequence index))))
2122 (setf (aref sequence index) new)
2123 (setq count (1- count)))))
2125 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2127 (define-sequence-traverser nsubstitute-if
2128 (new predicate sequence &rest args &key from-end start end count key)
2130 "Return a sequence of the same kind as SEQUENCE with the same elements
2131 except that all elements satisfying PREDICATE are replaced with NEW.
2132 SEQUENCE may be destructively modified."
2133 (declare (fixnum start))
2134 (declare (truly-dynamic-extent args))
2135 (let ((end (or end length)))
2136 (declare (fixnum end))
2137 (seq-dispatch sequence
2139 (let ((length (length sequence)))
2140 (nreverse (nlist-substitute-if*
2141 new predicate (nreverse (the list sequence))
2142 (- length end) (- length start) count key)))
2143 (nlist-substitute-if* new predicate sequence
2144 start end count key))
2146 (nvector-substitute-if* new predicate sequence -1
2147 (1- end) (1- start) count key)
2148 (nvector-substitute-if* new predicate sequence 1
2149 start end count key))
2150 (apply #'sb!sequence:nsubstitute-if new predicate sequence args))))
2152 (defun nlist-substitute-if* (new test sequence start end count key)
2153 (declare (fixnum end))
2154 (do ((list (nthcdr start sequence) (cdr list))
2155 (index start (1+ index)))
2156 ((or (= index end) (null list) (= count 0)) sequence)
2157 (when (funcall test (apply-key key (car list)))
2159 (setq count (1- count)))))
2161 (defun nvector-substitute-if* (new test sequence incrementer
2162 start end count key)
2163 (do ((index start (+ index incrementer)))
2164 ((or (= index end) (= count 0)) sequence)
2165 (when (funcall test (apply-key key (aref sequence index)))
2166 (setf (aref sequence index) new)
2167 (setq count (1- count)))))
2169 (define-sequence-traverser nsubstitute-if-not
2170 (new predicate sequence &rest args &key from-end start end count key)
2172 "Return a sequence of the same kind as SEQUENCE with the same elements
2173 except that all elements not satisfying PREDICATE are replaced with NEW.
2174 SEQUENCE may be destructively modified."
2175 (declare (fixnum start))
2176 (declare (truly-dynamic-extent args))
2177 (let ((end (or end length)))
2178 (declare (fixnum end))
2179 (seq-dispatch sequence
2181 (let ((length (length sequence)))
2182 (nreverse (nlist-substitute-if-not*
2183 new predicate (nreverse (the list sequence))
2184 (- length end) (- length start) count key)))
2185 (nlist-substitute-if-not* new predicate sequence
2186 start end count key))
2188 (nvector-substitute-if-not* new predicate sequence -1
2189 (1- end) (1- start) count key)
2190 (nvector-substitute-if-not* new predicate sequence 1
2191 start end count key))
2192 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args))))
2194 (defun nlist-substitute-if-not* (new test sequence start end count key)
2195 (declare (fixnum end))
2196 (do ((list (nthcdr start sequence) (cdr list))
2197 (index start (1+ index)))
2198 ((or (= index end) (null list) (= count 0)) sequence)
2199 (when (not (funcall test (apply-key key (car list))))
2203 (defun nvector-substitute-if-not* (new test sequence incrementer
2204 start end count key)
2205 (do ((index start (+ index incrementer)))
2206 ((or (= index end) (= count 0)) sequence)
2207 (when (not (funcall test (apply-key key (aref sequence index))))
2208 (setf (aref sequence index) new)
2211 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2213 (defun effective-find-position-test (test test-not)
2214 (effective-find-position-test test test-not))
2215 (defun effective-find-position-key (key)
2216 (effective-find-position-key key))
2218 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2219 (macrolet (;; shared logic for defining %FIND-POSITION and
2220 ;; %FIND-POSITION-IF in terms of various inlineable cases
2221 ;; of the expression defined in FROB and VECTOR*-FROB
2222 (frobs (&optional bit-frob)
2223 `(seq-dispatch sequence-arg
2224 (frob sequence-arg from-end)
2225 (with-array-data ((sequence sequence-arg :offset-var offset)
2228 :check-fill-pointer t)
2229 (multiple-value-bind (f p)
2230 (macrolet ((frob2 () `(if from-end
2232 (frob sequence nil))))
2235 ((simple-array character (*)) (frob2))
2236 ((simple-array base-char (*)) (frob2))
2238 `((simple-bit-vector
2239 (if (and (typep item 'bit)
2244 (let ((p (%bit-position item sequence
2245 from-end start end)))
2249 (vector*-frob sequence)))))
2251 (vector*-frob sequence))))
2252 (declare (type (or index null) p))
2253 (values f (and p (the index (- p offset)))))))))
2254 (defun %find-position (item sequence-arg from-end start end key test)
2255 (macrolet ((frob (sequence from-end)
2256 `(%find-position item ,sequence
2257 ,from-end start end key test))
2258 (vector*-frob (sequence)
2259 `(%find-position-vector-macro item ,sequence
2260 from-end start end key test)))
2262 (defun %find-position-if (predicate sequence-arg from-end start end key)
2263 (macrolet ((frob (sequence from-end)
2264 `(%find-position-if predicate ,sequence
2265 ,from-end start end key))
2266 (vector*-frob (sequence)
2267 `(%find-position-if-vector-macro predicate ,sequence
2268 from-end start end key)))
2270 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2271 (macrolet ((frob (sequence from-end)
2272 `(%find-position-if-not predicate ,sequence
2273 ,from-end start end key))
2274 (vector*-frob (sequence)
2275 `(%find-position-if-not-vector-macro predicate ,sequence
2276 from-end start end key)))
2280 (item sequence &rest args &key from-end (start 0) end key test test-not)
2281 (declare (truly-dynamic-extent args))
2282 (seq-dispatch sequence
2283 (nth-value 0 (%find-position
2284 item sequence from-end start end
2285 (effective-find-position-key key)
2286 (effective-find-position-test test test-not)))
2287 (nth-value 0 (%find-position
2288 item sequence from-end start end
2289 (effective-find-position-key key)
2290 (effective-find-position-test test test-not)))
2291 (apply #'sb!sequence:find item sequence args)))
2293 (item sequence &rest args &key from-end (start 0) end key test test-not)
2294 (declare (truly-dynamic-extent args))
2295 (seq-dispatch sequence
2296 (nth-value 1 (%find-position
2297 item sequence from-end start end
2298 (effective-find-position-key key)
2299 (effective-find-position-test test test-not)))
2300 (nth-value 1 (%find-position
2301 item sequence from-end start end
2302 (effective-find-position-key key)
2303 (effective-find-position-test test test-not)))
2304 (apply #'sb!sequence:position item sequence args)))
2306 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2307 (declare (truly-dynamic-extent args))
2308 (seq-dispatch sequence
2309 (nth-value 0 (%find-position-if
2310 (%coerce-callable-to-fun predicate)
2311 sequence from-end start end
2312 (effective-find-position-key key)))
2313 (nth-value 0 (%find-position-if
2314 (%coerce-callable-to-fun predicate)
2315 sequence from-end start end
2316 (effective-find-position-key key)))
2317 (apply #'sb!sequence:find-if predicate sequence args)))
2319 (predicate sequence &rest args &key from-end (start 0) end key)
2320 (declare (truly-dynamic-extent args))
2321 (seq-dispatch sequence
2322 (nth-value 1 (%find-position-if
2323 (%coerce-callable-to-fun predicate)
2324 sequence from-end start end
2325 (effective-find-position-key key)))
2326 (nth-value 1 (%find-position-if
2327 (%coerce-callable-to-fun predicate)
2328 sequence from-end start end
2329 (effective-find-position-key key)))
2330 (apply #'sb!sequence:position-if predicate sequence args)))
2333 (predicate sequence &rest args &key from-end (start 0) end key)
2334 (declare (truly-dynamic-extent args))
2335 (seq-dispatch sequence
2336 (nth-value 0 (%find-position-if-not
2337 (%coerce-callable-to-fun predicate)
2338 sequence from-end start end
2339 (effective-find-position-key key)))
2340 (nth-value 0 (%find-position-if-not
2341 (%coerce-callable-to-fun predicate)
2342 sequence from-end start end
2343 (effective-find-position-key key)))
2344 (apply #'sb!sequence:find-if-not predicate sequence args)))
2345 (defun position-if-not
2346 (predicate sequence &rest args &key from-end (start 0) end key)
2347 (declare (truly-dynamic-extent args))
2348 (seq-dispatch sequence
2349 (nth-value 1 (%find-position-if-not
2350 (%coerce-callable-to-fun predicate)
2351 sequence from-end start end
2352 (effective-find-position-key key)))
2353 (nth-value 1 (%find-position-if-not
2354 (%coerce-callable-to-fun predicate)
2355 sequence from-end start end
2356 (effective-find-position-key key)))
2357 (apply #'sb!sequence:position-if-not predicate sequence args)))
2359 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2361 (eval-when (:compile-toplevel :execute)
2363 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2364 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2365 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2366 `(let ((%start ,(if from-end-p '(1- end) 'start))
2367 (%end ,(if from-end-p '(1- start) 'end)))
2368 (do ((index %start ,next-index)
2370 ((= index (the fixnum %end)) count)
2371 (declare (fixnum index count))
2372 (,(if notp 'unless 'when) ,pred
2373 (setq count (1+ count)))))))
2375 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2376 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2377 `(let ((%start ,(if from-end-p '(- length end) 'start))
2378 (%end ,(if from-end-p '(- length start) 'end))
2379 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2380 (do ((sequence (nthcdr %start ,sequence))
2381 (index %start (1+ index))
2383 ((or (= index (the fixnum %end)) (null sequence)) count)
2384 (declare (fixnum index count))
2385 (,(if notp 'unless 'when) ,pred
2386 (setq count (1+ count)))))))
2391 (define-sequence-traverser count-if
2392 (pred sequence &rest args &key from-end start end key)
2394 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2395 (declare (fixnum start))
2396 (declare (truly-dynamic-extent args))
2397 (let ((end (or end length))
2398 (pred (%coerce-callable-to-fun pred)))
2399 (declare (type index end))
2400 (seq-dispatch sequence
2402 (list-count-if nil t pred sequence)
2403 (list-count-if nil nil pred sequence))
2405 (vector-count-if nil t pred sequence)
2406 (vector-count-if nil nil pred sequence))
2407 (apply #'sb!sequence:count-if pred sequence args))))
2409 (define-sequence-traverser count-if-not
2410 (pred sequence &rest args &key from-end start end key)
2412 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2413 (declare (fixnum start))
2414 (declare (truly-dynamic-extent args))
2415 (let ((end (or end length))
2416 (pred (%coerce-callable-to-fun pred)))
2417 (declare (type index end))
2418 (seq-dispatch sequence
2420 (list-count-if t t pred sequence)
2421 (list-count-if t nil pred sequence))
2423 (vector-count-if t t pred sequence)
2424 (vector-count-if t nil pred sequence))
2425 (apply #'sb!sequence:count-if-not pred sequence args))))
2427 (define-sequence-traverser count
2428 (item sequence &rest args &key from-end start end
2429 key (test #'eql test-p) (test-not nil test-not-p))
2431 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2432 which defaults to EQL."
2433 (declare (fixnum start))
2434 (declare (truly-dynamic-extent args))
2435 (when (and test-p test-not-p)
2436 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2438 (error ":TEST and :TEST-NOT are both present."))
2439 (let ((end (or end length)))
2440 (declare (type index end))
2441 (let ((%test (if test-not-p
2443 (not (funcall test-not item x)))
2445 (funcall test item x)))))
2446 (seq-dispatch sequence
2448 (list-count-if nil t %test sequence)
2449 (list-count-if nil nil %test sequence))
2451 (vector-count-if nil t %test sequence)
2452 (vector-count-if nil nil %test sequence))
2453 (apply #'sb!sequence:count item sequence args)))))
2457 (eval-when (:compile-toplevel :execute)
2459 (sb!xc:defmacro match-vars (&rest body)
2460 `(let ((inc (if from-end -1 1))
2461 (start1 (if from-end (1- (the fixnum end1)) start1))
2462 (start2 (if from-end (1- (the fixnum end2)) start2))
2463 (end1 (if from-end (1- (the fixnum start1)) end1))
2464 (end2 (if from-end (1- (the fixnum start2)) end2)))
2465 (declare (fixnum inc start1 start2 end1 end2))
2468 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2469 (declare (ignore end)) ;; ### Should END be used below?
2470 `(let ((,sequence (if from-end
2471 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2472 (reverse (the list ,sequence)))
2473 (nthcdr ,start ,sequence))))
2474 (declare (type list ,sequence))
2479 (eval-when (:compile-toplevel :execute)
2481 (sb!xc:defmacro if-mismatch (elt1 elt2)
2482 `(cond ((= (the fixnum index1) (the fixnum end1))
2483 (return (if (= (the fixnum index2) (the fixnum end2))
2486 (1+ (the fixnum index1))
2487 (the fixnum index1)))))
2488 ((= (the fixnum index2) (the fixnum end2))
2489 (return (if from-end (1+ (the fixnum index1)) index1)))
2491 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2492 (return (if from-end (1+ (the fixnum index1)) index1))))
2493 (t (if (not (funcall test (apply-key key ,elt1)
2494 (apply-key key ,elt2)))
2495 (return (if from-end (1+ (the fixnum index1)) index1))))))
2497 (sb!xc:defmacro mumble-mumble-mismatch ()
2498 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2499 (index2 start2 (+ index2 (the fixnum inc))))
2501 (declare (fixnum index1 index2))
2502 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2504 (sb!xc:defmacro mumble-list-mismatch ()
2505 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2506 (index2 start2 (+ index2 (the fixnum inc))))
2508 (declare (fixnum index1 index2))
2509 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2511 (sb!xc:defmacro list-mumble-mismatch ()
2512 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2513 (index2 start2 (+ index2 (the fixnum inc))))
2515 (declare (fixnum index1 index2))
2516 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2518 (sb!xc:defmacro list-list-mismatch ()
2519 `(do ((sequence1 sequence1)
2520 (sequence2 sequence2)
2521 (index1 start1 (+ index1 (the fixnum inc)))
2522 (index2 start2 (+ index2 (the fixnum inc))))
2524 (declare (fixnum index1 index2))
2525 (if-mismatch (pop sequence1) (pop sequence2))))
2529 (define-sequence-traverser mismatch
2530 (sequence1 sequence2 &rest args &key from-end test test-not
2531 start1 end1 start2 end2 key)
2533 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2534 element-wise. If they are of equal length and match in every element, the
2535 result is NIL. Otherwise, the result is a non-negative integer, the index
2536 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2537 if one is shorter than and a matching prefix of the other, the index within
2538 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2539 :FROM-END argument is given, then one plus the index of the rightmost
2540 position in which the sequences differ is returned."
2541 (declare (fixnum start1 start2))
2542 (declare (truly-dynamic-extent args))
2543 (let* ((end1 (or end1 length1))
2544 (end2 (or end2 length2)))
2545 (declare (type index end1 end2))
2547 (seq-dispatch sequence1
2548 (seq-dispatch sequence2
2549 (matchify-list (sequence1 start1 length1 end1)
2550 (matchify-list (sequence2 start2 length2 end2)
2551 (list-list-mismatch)))
2552 (matchify-list (sequence1 start1 length1 end1)
2553 (list-mumble-mismatch))
2554 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2555 (seq-dispatch sequence2
2556 (matchify-list (sequence2 start2 length2 end2)
2557 (mumble-list-mismatch))
2558 (mumble-mumble-mismatch)
2559 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2560 (apply #'sb!sequence:mismatch sequence1 sequence2 args)))))
2562 ;;; search comparison functions
2564 (eval-when (:compile-toplevel :execute)
2566 ;;; Compare two elements and return if they don't match.
2567 (sb!xc:defmacro compare-elements (elt1 elt2)
2569 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2572 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2576 (sb!xc:defmacro search-compare-list-list (main sub)
2577 `(do ((main ,main (cdr main))
2578 (jndex start1 (1+ jndex))
2579 (sub (nthcdr start1 ,sub) (cdr sub)))
2580 ((or (endp main) (endp sub) (<= end1 jndex))
2582 (declare (type (integer 0) jndex))
2583 (compare-elements (car sub) (car main))))
2585 (sb!xc:defmacro search-compare-list-vector (main sub)
2586 `(do ((main ,main (cdr main))
2587 (index start1 (1+ index)))
2588 ((or (endp main) (= index end1)) t)
2589 (compare-elements (aref ,sub index) (car main))))
2591 (sb!xc:defmacro search-compare-vector-list (main sub index)
2592 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2593 (jndex start1 (1+ jndex))
2594 (index ,index (1+ index)))
2595 ((or (<= end1 jndex) (endp sub)) t)
2596 (declare (type (integer 0) jndex))
2597 (compare-elements (car sub) (aref ,main index))))
2599 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2600 `(do ((index ,index (1+ index))
2601 (sub-index start1 (1+ sub-index)))
2602 ((= sub-index end1) t)
2603 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2605 (sb!xc:defmacro search-compare (main-type main sub index)
2606 (if (eq main-type 'list)
2608 (search-compare-list-list ,main ,sub)
2609 (search-compare-list-vector ,main ,sub)
2610 ;; KLUDGE: just hack it together so that it works
2611 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2613 (search-compare-vector-list ,main ,sub ,index)
2614 (search-compare-vector-vector ,main ,sub ,index)
2615 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2621 (eval-when (:compile-toplevel :execute)
2623 (sb!xc:defmacro list-search (main sub)
2624 `(do ((main (nthcdr start2 ,main) (cdr main))
2625 (index2 start2 (1+ index2))
2626 (terminus (- end2 (the (integer 0) (- end1 start1))))
2628 ((> index2 terminus) last-match)
2629 (declare (type (integer 0) index2))
2630 (if (search-compare list main ,sub index2)
2632 (setq last-match index2)
2635 (sb!xc:defmacro vector-search (main sub)
2636 `(do ((index2 start2 (1+ index2))
2637 (terminus (- end2 (the (integer 0) (- end1 start1))))
2639 ((> index2 terminus) last-match)
2640 (declare (type (integer 0) index2))
2641 (if (search-compare vector ,main ,sub index2)
2643 (setq last-match index2)
2648 (define-sequence-traverser search
2649 (sequence1 sequence2 &rest args &key
2650 from-end test test-not start1 end1 start2 end2 key)
2651 (declare (fixnum start1 start2))
2652 (declare (truly-dynamic-extent args))
2653 (let ((end1 (or end1 length1))
2654 (end2 (or end2 length2)))
2655 (seq-dispatch sequence2
2656 (list-search sequence2 sequence1)
2657 (vector-search sequence2 sequence1)
2658 (apply #'sb!sequence:search sequence1 sequence2 args))))
2660 ;;; FIXME: this was originally in array.lisp; it might be better to
2661 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2662 ;;; a new early-seq.lisp file.
2663 (defun fill-data-vector (vector dimensions initial-contents)
2665 (labels ((frob (axis dims contents)
2667 (setf (aref vector index) contents)
2670 (unless (typep contents 'sequence)
2671 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2672 sequence, but ~W more layer~:P needed."
2674 (- (length dimensions) axis)))
2675 (unless (= (length contents) (car dims))
2676 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2677 axis ~W is ~W, but ~S is ~W long."
2678 axis (car dims) contents (length contents)))
2679 (sb!sequence:dosequence (content contents)
2680 (frob (1+ axis) (cdr dims) content))))))
2681 (frob 0 dimensions initial-contents))))