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 ;; Entries for {start,end}{,1,2}
43 ,@(mapcan (lambda (names)
44 (destructuring-bind (start end length sequence) names
49 ;; Only evaluate LENGTH (which may be expensive)
50 ;; if START is non-NIL.
51 (if (or (zerop ,start) (<= 0 ,start ,length))
53 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
58 ;; Only evaluate LENGTH (which may be expensive)
60 (if (or (null ,end) (<= ,start ,end ,length))
61 ;; Defaulting of NIL is done inside the
62 ;; bodies, for ease of sharing with compiler
65 ;; FIXME: defend against non-number non-NIL
68 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
70 '((start end length sequence)
71 (start1 end1 length1 sequence1)
72 (start2 end2 length2 sequence2)))
75 (and key (%coerce-callable-to-fun key))
79 (%coerce-callable-to-fun test)
83 (and test-not (%coerce-callable-to-fun test-not))
86 (sb!xc:defmacro define-sequence-traverser (name args &body body)
87 (multiple-value-bind (body declarations docstring)
88 (parse-body body :doc-string-allowed t)
91 ;; Things which are definitely used in any code path.
93 ;; Things which may be used/are only used in certain
94 ;; code paths (e.g. length).
98 ;; FIXME: make this robust. And clean.
99 ((sequence sequence1 sequence2)
100 (let* ((length-var (ecase arg
103 (sequence2 'length2)))
104 (cache-var (symbolicate length-var '#:-cache)))
106 (rebindings/eager `(,cache-var nil))
108 `(,length-var (truly-the
110 (or ,cache-var (setf ,cache-var (length ,arg))))))))
111 ((function predicate)
113 (rebindings/eager `(,arg (%coerce-callable-to-fun ,arg))))
115 (let ((info (cdr (assoc arg *sequence-keyword-info*))))
117 (destructuring-bind (default supplied-p adjuster type) info
118 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
119 (rebindings/eager `(,arg ,adjuster))
120 (new-declarations `(type ,type ,arg))))
121 (t (new-args arg)))))))
122 `(defun ,name ,(new-args)
123 ,@(when docstring (list docstring))
125 (symbol-macrolet (,@(rebindings/lazy))
126 (let* (,@(rebindings/eager))
127 (declare ,@(new-declarations))
131 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
133 ;;; FIXME: It might be worth making three cases here, LIST,
134 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
135 ;;; It tends to make code run faster but be bigger; some benchmarking
136 ;;; is needed to decide.
137 (sb!xc:defmacro seq-dispatch
138 (sequence list-form array-form &optional other-form)
139 `(if (listp ,sequence)
140 (let ((,sequence (truly-the list ,sequence)))
141 (declare (ignorable ,sequence))
144 `((if (arrayp ,sequence)
145 (let ((,sequence (truly-the vector ,sequence)))
146 (declare (ignorable ,sequence))
149 `((let ((,sequence (truly-the vector ,sequence)))
150 (declare (ignorable ,sequence))
153 (sb!xc:defmacro %make-sequence-like (sequence length)
155 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
156 `(seq-dispatch ,sequence
158 (make-array ,length :element-type (array-element-type ,sequence))
159 (sb!sequence:make-sequence-like ,sequence ,length)))
161 (sb!xc:defmacro bad-sequence-type-error (type-spec)
162 `(error 'simple-type-error
164 :expected-type '(satisfies is-a-valid-sequence-type-specifier-p)
165 :format-control "~S is a bad type specifier for sequences."
166 :format-arguments (list ,type-spec)))
168 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
169 `(error 'simple-type-error
171 :expected-type (cond ((array-type-p ,type)
172 `(eql ,(car (array-type-dimensions ,type))))
173 ((type= ,type (specifier-type 'null))
177 (t (bug "weird type in S-T-L-M-ERROR")))
178 ;; FIXME: this format control causes ugly printing. There's
179 ;; probably some ~<~@:_~> incantation that would make it
180 ;; nicer. -- CSR, 2002-10-18
181 :format-control "The length requested (~S) does not match the type restriction in ~S."
182 :format-arguments (list ,length (type-specifier ,type))))
184 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
185 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
186 ;; words that give some but not total support to this position in
187 ;; ANSI. Essentially, we are justified in throwing this on
188 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
189 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
191 ;; On the other hand, I'm not sure it deserves to be a type-error,
192 ;; either. -- bem, 2005-08-10
193 `(error 'simple-program-error
194 :format-control "~S is too hairy for sequence functions."
195 :format-arguments (list ,type-spec)))
198 (defun is-a-valid-sequence-type-specifier-p (type)
199 (let ((type (specifier-type type)))
200 (or (csubtypep type (specifier-type 'list))
201 (csubtypep type (specifier-type 'vector)))))
203 ;;; It's possible with some sequence operations to declare the length
204 ;;; of a result vector, and to be safe, we really ought to verify that
205 ;;; the actual result has the declared length.
206 (defun vector-of-checked-length-given-length (vector declared-length)
207 (declare (type vector vector))
208 (declare (type index declared-length))
209 (let ((actual-length (length vector)))
210 (unless (= actual-length declared-length)
211 (error 'simple-type-error
213 :expected-type `(vector ,declared-length)
215 "Vector length (~W) doesn't match declared length (~W)."
216 :format-arguments (list actual-length declared-length))))
219 (defun sequence-of-checked-length-given-type (sequence result-type)
220 (let ((ctype (specifier-type result-type)))
221 (if (not (array-type-p ctype))
223 (let ((declared-length (first (array-type-dimensions ctype))))
224 (if (eq declared-length '*)
226 (vector-of-checked-length-given-length sequence
227 declared-length))))))
229 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
230 (defun signal-index-too-large-error (sequence index)
231 (let* ((length (length sequence))
232 (max-index (and (plusp length)
234 (error 'index-too-large-error
236 :expected-type (if max-index
237 `(integer 0 ,max-index)
238 ;; This seems silly, is there something better?
241 (declaim (ftype (function (t t t) nil) sequence-bounding-indices-bad-error))
242 (defun sequence-bounding-indices-bad-error (sequence start end)
243 (let ((size (length sequence)))
244 (error 'bounding-indices-bad-error
245 :datum (cons start end)
246 :expected-type `(cons (integer 0 ,size)
247 (integer ,start ,size))
250 (declaim (ftype (function (t t t) nil) array-bounding-indices-bad-error))
251 (defun array-bounding-indices-bad-error (array start end)
252 (let ((size (array-total-size array)))
253 (error 'bounding-indices-bad-error
254 :datum (cons start end)
255 :expected-type `(cons (integer 0 ,size)
256 (integer ,start ,size))
259 (declaim (ftype (function (t) nil) circular-list-error))
260 (defun circular-list-error (list)
261 (let ((*print-circle* t))
262 (error 'simple-type-error
263 :format-control "List is circular:~% ~S"
264 :format-arguments (list list)
266 :type '(and list (satisfies list-length)))))
270 (defun emptyp (sequence)
272 "Returns T if SEQUENCE is an empty sequence and NIL
273 otherwise. Signals an error if SEQUENCE is not a sequence."
274 (seq-dispatch sequence
276 (zerop (length sequence))
277 (sb!sequence:emptyp sequence)))
279 (defun elt (sequence index)
280 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
281 (seq-dispatch sequence
282 (do ((count index (1- count))
283 (list sequence (cdr list)))
286 (signal-index-too-large-error sequence index)
288 (declare (type (integer 0) count)))
290 (when (>= index (length sequence))
291 (signal-index-too-large-error sequence index))
292 (aref sequence index))
293 (sb!sequence:elt sequence index)))
295 (defun %setelt (sequence index newval)
296 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
297 (seq-dispatch sequence
298 (do ((count index (1- count))
300 ((= count 0) (rplaca seq newval) newval)
301 (declare (fixnum count))
303 (signal-index-too-large-error sequence index)
304 (setq seq (cdr seq))))
306 (when (>= index (length sequence))
307 (signal-index-too-large-error sequence index))
308 (setf (aref sequence index) newval))
309 (setf (sb!sequence:elt sequence index) newval)))
311 (defun length (sequence)
312 #!+sb-doc "Return an integer that is the length of SEQUENCE."
313 (seq-dispatch sequence
316 (sb!sequence:length sequence)))
318 (defun make-sequence (type length &key (initial-element nil iep))
320 "Return a sequence of the given TYPE and LENGTH, with elements initialized
322 (declare (fixnum length))
323 (let* ((expanded-type (typexpand type))
325 (typecase expanded-type
327 ((eq expanded-type 'string) '(vector character))
328 ((eq expanded-type 'simple-string)
329 '(simple-array character (*)))
332 ((eq (car expanded-type) 'string)
333 `(vector character ,@(cdr expanded-type)))
334 ((eq (car expanded-type) 'simple-string)
335 `(simple-array character ,(if (cdr expanded-type)
339 (type (specifier-type adjusted-type)))
340 (cond ((csubtypep type (specifier-type 'list))
342 ((type= type (specifier-type 'list))
343 (make-list length :initial-element initial-element))
344 ((eq type *empty-type*)
345 (bad-sequence-type-error nil))
346 ((type= type (specifier-type 'null))
349 (sequence-type-length-mismatch-error type length)))
351 (multiple-value-bind (min exactp)
352 (sb!kernel::cons-type-length-info type)
354 (unless (= length min)
355 (sequence-type-length-mismatch-error type length))
356 (unless (>= length min)
357 (sequence-type-length-mismatch-error type length)))
358 (make-list length :initial-element initial-element)))
359 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
360 ;; which may seem strange and non-ideal, but then I'd say
361 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
362 (t (sequence-type-too-hairy (type-specifier type)))))
363 ((csubtypep type (specifier-type 'vector))
365 (;; is it immediately obvious what the result type is?
366 (typep type 'array-type)
368 (aver (= (length (array-type-dimensions type)) 1))
369 (let* ((etype (type-specifier
370 (array-type-specialized-element-type type)))
371 (etype (if (eq etype '*) t etype))
372 (type-length (car (array-type-dimensions type))))
373 (unless (or (eq type-length '*)
374 (= type-length length))
375 (sequence-type-length-mismatch-error type length))
376 ;; FIXME: These calls to MAKE-ARRAY can't be
377 ;; open-coded, as the :ELEMENT-TYPE argument isn't
378 ;; constant. Probably we ought to write a
379 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
382 (make-array length :element-type etype
383 :initial-element initial-element)
384 (make-array length :element-type etype)))))
385 (t (sequence-type-too-hairy (type-specifier type)))))
386 ((and (csubtypep type (specifier-type 'sequence))
387 (find-class adjusted-type nil))
388 (let* ((class (find-class adjusted-type nil)))
389 (unless (sb!mop:class-finalized-p class)
390 (sb!mop:finalize-inheritance class))
392 (sb!sequence:make-sequence-like
393 (sb!mop:class-prototype class) length
394 :initial-element initial-element)
395 (sb!sequence:make-sequence-like
396 (sb!mop:class-prototype class) length))))
397 (t (bad-sequence-type-error (type-specifier type))))))
402 (define-array-dispatch vector-subseq-dispatch (array start end)
403 (declare (optimize speed (safety 0)))
404 (declare (type index start end))
405 (subseq array start end))
407 ;;;; The support routines for SUBSEQ are used by compiler transforms,
408 ;;;; so we worry about dealing with END being supplied or defaulting
409 ;;;; to NIL at this level.
411 (defun vector-subseq* (sequence start end)
412 (declare (type vector sequence))
413 (declare (type index start)
414 (type (or null index) end)
416 (with-array-data ((data sequence)
419 :check-fill-pointer t
421 (vector-subseq-dispatch data start end)))
423 (defun list-subseq* (sequence start end)
424 (declare (type list sequence)
425 (type unsigned-byte start)
426 (type (or null unsigned-byte) end))
428 (sequence-bounding-indices-bad-error sequence start end)))
429 (let ((pointer sequence))
430 (unless (zerop start)
431 ;; If START > 0 the list cannot be empty. So CDR down to
432 ;; it START-1 times, check that we still have something, then
433 ;; CDR the final time.
435 ;; If START was zero, the list may be empty if END is NIL or
438 (setf pointer (nthcdr (1- start) pointer)))
443 (let ((n (- end start)))
444 (declare (integer n))
448 (let* ((head (list nil))
450 (macrolet ((pop-one ()
451 `(let ((tmp (list (pop pointer))))
455 (loop until (fixnump n)
458 ;; Fixnum case, but leave last element, so we should
459 ;; still have something left in the sequence.
466 ;; OK, pop the last one.
470 collect (pop pointer))))))
472 (defun subseq (sequence start &optional end)
474 "Return a copy of a subsequence of SEQUENCE starting with element number
475 START and continuing to the end of SEQUENCE or the optional END."
476 (seq-dispatch sequence
477 (list-subseq* sequence start end)
478 (vector-subseq* sequence start end)
479 (sb!sequence:subseq sequence start end)))
483 (defun copy-seq (sequence)
484 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
485 (seq-dispatch sequence
486 (list-copy-seq* sequence)
487 (vector-subseq* sequence 0 nil)
488 (sb!sequence:copy-seq sequence)))
490 (defun list-copy-seq* (sequence)
491 (!copy-list-macro sequence :check-proper-list t))
495 (defun list-fill* (sequence item start end)
496 (declare (type list sequence)
497 (type unsigned-byte start)
498 (type (or null unsigned-byte) end))
500 (sequence-bounding-indices-bad-error sequence start end)))
501 (let ((pointer sequence))
502 (unless (zerop start)
503 ;; If START > 0 the list cannot be empty. So CDR down to it
504 ;; START-1 times, check that we still have something, then CDR
507 ;; If START was zero, the list may be empty if END is NIL or
510 (setf pointer (nthcdr (1- start) pointer)))
515 (let ((n (- end start)))
516 (declare (integer n))
521 do (setf pointer (cdr (rplaca pointer item))))))
523 do (setf pointer (cdr (rplaca pointer item)))))))
526 (defun vector-fill* (sequence item start end)
527 (with-array-data ((data sequence)
531 :check-fill-pointer t)
532 (let ((setter (!find-data-vector-setter data)))
533 (declare (optimize (speed 3) (safety 0)))
534 (do ((index start (1+ index)))
535 ((= index end) sequence)
536 (declare (index index))
537 (funcall setter data index item)))))
539 (defun string-fill* (sequence item start end)
540 (declare (string sequence))
541 (with-array-data ((data sequence)
545 :check-fill-pointer t)
546 ;; DEFTRANSFORM for FILL will turn these into
547 ;; calls to UB*-BASH-FILL.
550 ((simple-array character (*))
551 (let ((item (locally (declare (optimize (safety 3)))
552 (the character item))))
553 (fill data item :start start :end end)))
554 ((simple-array base-char (*))
555 (let ((item (locally (declare (optimize (safety 3)))
556 (the base-char item))))
557 (fill data item :start start :end end))))))
559 (defun fill (sequence item &key (start 0) end)
561 "Replace the specified elements of SEQUENCE with ITEM."
562 (seq-dispatch sequence
563 (list-fill* sequence item start end)
564 (vector-fill* sequence item start end)
565 (sb!sequence:fill sequence item
567 :end (%check-generic-sequence-bounds sequence start end))))
571 (eval-when (:compile-toplevel :execute)
573 ;;; If we are copying around in the same vector, be careful not to copy the
574 ;;; same elements over repeatedly. We do this by copying backwards.
575 (sb!xc:defmacro mumble-replace-from-mumble ()
576 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
577 (let ((nelts (min (- target-end target-start)
578 (- source-end source-start))))
579 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
581 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
583 ((= target-index (the fixnum (1- target-start))) target-sequence)
584 (declare (fixnum target-index source-index))
585 ;; disable bounds checking
586 (declare (optimize (safety 0)))
587 (setf (aref target-sequence target-index)
588 (aref source-sequence source-index))))
589 (do ((target-index target-start (1+ target-index))
590 (source-index source-start (1+ source-index)))
591 ((or (= target-index (the fixnum target-end))
592 (= source-index (the fixnum source-end)))
594 (declare (fixnum target-index source-index))
595 ;; disable bounds checking
596 (declare (optimize (safety 0)))
597 (setf (aref target-sequence target-index)
598 (aref source-sequence source-index)))))
600 (sb!xc:defmacro list-replace-from-list ()
601 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
602 (let ((new-elts (subseq source-sequence source-start
603 (+ (the fixnum source-start)
605 (min (- (the fixnum target-end)
606 (the fixnum target-start))
607 (- (the fixnum source-end)
608 (the fixnum source-start))))))))
609 (do ((n new-elts (cdr n))
610 (o (nthcdr target-start target-sequence) (cdr o)))
611 ((null n) target-sequence)
613 (do ((target-index target-start (1+ target-index))
614 (source-index source-start (1+ source-index))
615 (target-sequence-ref (nthcdr target-start target-sequence)
616 (cdr target-sequence-ref))
617 (source-sequence-ref (nthcdr source-start source-sequence)
618 (cdr source-sequence-ref)))
619 ((or (= target-index (the fixnum target-end))
620 (= source-index (the fixnum source-end))
621 (null target-sequence-ref) (null source-sequence-ref))
623 (declare (fixnum target-index source-index))
624 (rplaca target-sequence-ref (car source-sequence-ref)))))
626 (sb!xc:defmacro list-replace-from-mumble ()
627 `(do ((target-index target-start (1+ target-index))
628 (source-index source-start (1+ source-index))
629 (target-sequence-ref (nthcdr target-start target-sequence)
630 (cdr target-sequence-ref)))
631 ((or (= target-index (the fixnum target-end))
632 (= source-index (the fixnum source-end))
633 (null target-sequence-ref))
635 (declare (fixnum source-index target-index))
636 (rplaca target-sequence-ref (aref source-sequence source-index))))
638 (sb!xc:defmacro mumble-replace-from-list ()
639 `(do ((target-index target-start (1+ target-index))
640 (source-index source-start (1+ source-index))
641 (source-sequence (nthcdr source-start source-sequence)
642 (cdr source-sequence)))
643 ((or (= target-index (the fixnum target-end))
644 (= source-index (the fixnum source-end))
645 (null source-sequence))
647 (declare (fixnum target-index source-index))
648 (setf (aref target-sequence target-index) (car source-sequence))))
652 ;;;; The support routines for REPLACE are used by compiler transforms, so we
653 ;;;; worry about dealing with END being supplied or defaulting to NIL
656 (defun list-replace-from-list* (target-sequence source-sequence target-start
657 target-end source-start source-end)
658 (when (null target-end) (setq target-end (length target-sequence)))
659 (when (null source-end) (setq source-end (length source-sequence)))
660 (list-replace-from-list))
662 (defun list-replace-from-vector* (target-sequence source-sequence target-start
663 target-end source-start source-end)
664 (when (null target-end) (setq target-end (length target-sequence)))
665 (when (null source-end) (setq source-end (length source-sequence)))
666 (list-replace-from-mumble))
668 (defun vector-replace-from-list* (target-sequence source-sequence target-start
669 target-end source-start source-end)
670 (when (null target-end) (setq target-end (length target-sequence)))
671 (when (null source-end) (setq source-end (length source-sequence)))
672 (mumble-replace-from-list))
674 (defun vector-replace-from-vector* (target-sequence source-sequence
675 target-start target-end source-start
677 (when (null target-end) (setq target-end (length target-sequence)))
678 (when (null source-end) (setq source-end (length source-sequence)))
679 (mumble-replace-from-mumble))
682 (defun simple-character-string-replace-from-simple-character-string*
683 (target-sequence source-sequence
684 target-start target-end source-start source-end)
685 (declare (type (simple-array character (*)) target-sequence source-sequence))
686 (when (null target-end) (setq target-end (length target-sequence)))
687 (when (null source-end) (setq source-end (length source-sequence)))
688 (mumble-replace-from-mumble))
690 (define-sequence-traverser replace
691 (sequence1 sequence2 &rest args &key start1 end1 start2 end2)
693 "Destructively modifies SEQUENCE1 by copying successive elements
694 into it from the SEQUENCE2.
696 Elements are copied to the subseqeuence bounded by START1 and END1,
697 from the subsequence bounded by START2 and END2. If these subsequences
698 are not of the same length, then the shorter length determines how
699 many elements are copied."
700 (declare (truly-dynamic-extent args))
701 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
702 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
703 ;; these things here so that legacy code gets the names it's
704 ;; expecting. We could use &AUX instead :-/.
705 (target-sequence sequence1)
706 (source-sequence sequence2)
707 (target-start start1)
708 (source-start start2)
709 (target-end (or end1 length1))
710 (source-end (or end2 length2)))
711 (seq-dispatch target-sequence
712 (seq-dispatch source-sequence
713 (list-replace-from-list)
714 (list-replace-from-mumble)
715 (apply #'sb!sequence:replace sequence1 sequence2 args))
716 (seq-dispatch source-sequence
717 (mumble-replace-from-list)
718 (mumble-replace-from-mumble)
719 (apply #'sb!sequence:replace sequence1 sequence2 args))
720 (apply #'sb!sequence:replace sequence1 sequence2 args))))
724 (eval-when (:compile-toplevel :execute)
726 (sb!xc:defmacro vector-reverse (sequence)
727 `(let ((length (length ,sequence)))
728 (declare (fixnum length))
729 (do ((forward-index 0 (1+ forward-index))
730 (backward-index (1- length) (1- backward-index))
731 (new-sequence (%make-sequence-like sequence length)))
732 ((= forward-index length) new-sequence)
733 (declare (fixnum forward-index backward-index))
734 (setf (aref new-sequence forward-index)
735 (aref ,sequence backward-index)))))
737 (sb!xc:defmacro list-reverse-macro (sequence)
739 ((endp ,sequence) new-list)
740 (push (pop ,sequence) new-list)))
744 (defun reverse (sequence)
746 "Return a new sequence containing the same elements but in reverse order."
747 (seq-dispatch sequence
748 (list-reverse* sequence)
749 (vector-reverse* sequence)
750 (sb!sequence:reverse sequence)))
754 (defun list-reverse* (sequence)
755 (list-reverse-macro sequence))
757 (defun vector-reverse* (sequence)
758 (vector-reverse sequence))
762 (eval-when (:compile-toplevel :execute)
764 (sb!xc:defmacro vector-nreverse (sequence)
765 `(let ((length (length (the vector ,sequence))))
767 (do ((left-index 0 (1+ left-index))
768 (right-index (1- length) (1- right-index)))
769 ((<= right-index left-index))
770 (declare (type index left-index right-index))
771 (rotatef (aref ,sequence left-index)
772 (aref ,sequence right-index))))
775 (sb!xc:defmacro list-nreverse-macro (list)
776 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
784 (defun list-nreverse* (sequence)
785 (list-nreverse-macro sequence))
787 (defun vector-nreverse* (sequence)
788 (vector-nreverse sequence))
790 (defun nreverse (sequence)
792 "Return a sequence of the same elements in reverse order; the argument
794 (seq-dispatch sequence
795 (list-nreverse* sequence)
796 (vector-nreverse* sequence)
797 (sb!sequence:nreverse sequence)))
801 (defmacro sb!sequence:dosequence ((element sequence &optional return) &body body)
803 "Executes BODY with ELEMENT subsequently bound to each element of
804 SEQUENCE, then returns RETURN."
805 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
807 (sequence (gensym "SEQUENCE")))
809 (let ((,sequence ,s))
810 (seq-dispatch ,sequence
811 (dolist (,element ,sequence ,return) ,@body)
812 (do-vector-data (,element ,sequence ,return) ,@body)
813 (multiple-value-bind (state limit from-end step endp elt)
814 (sb!sequence:make-sequence-iterator ,sequence)
815 (do ((state state (funcall step ,sequence state from-end)))
816 ((funcall endp ,sequence state limit from-end)
817 (let ((,element nil))
818 ,@(filter-dolist-declarations decls)
821 (let ((,element (funcall elt ,sequence state)))
826 (defun concatenate (output-type-spec &rest sequences)
828 "Return a new sequence of all the argument sequences concatenated together
829 which shares no structure with the original argument sequences of the
830 specified OUTPUT-TYPE-SPEC."
831 (flet ((concat-to-list* (sequences)
832 (let ((result (list nil)))
833 (do ((sequences sequences (cdr sequences))
835 ((null sequences) (cdr result))
836 (let ((sequence (car sequences)))
837 (sb!sequence:dosequence (e sequence)
838 (setq splice (cdr (rplacd splice (list e)))))))))
839 (concat-to-simple* (type-spec sequences)
840 (do ((seqs sequences (cdr seqs))
844 (do ((sequences sequences (cdr sequences))
845 (lengths lengths (cdr lengths))
847 (result (make-sequence type-spec total-length)))
848 ((= index total-length) result)
849 (declare (fixnum index))
850 (let ((sequence (car sequences)))
851 (sb!sequence:dosequence (e sequence)
852 (setf (aref result index) e)
854 (let ((length (length (car seqs))))
855 (declare (fixnum length))
856 (setq lengths (nconc lengths (list length)))
857 (setq total-length (+ total-length length))))))
858 (let ((type (specifier-type output-type-spec)))
860 ((csubtypep type (specifier-type 'list))
862 ((type= type (specifier-type 'list))
863 (concat-to-list* sequences))
864 ((eq type *empty-type*)
865 (bad-sequence-type-error nil))
866 ((type= type (specifier-type 'null))
867 (unless (every #'emptyp sequences)
868 (sequence-type-length-mismatch-error
869 type (reduce #'+ sequences :key #'length))) ; FIXME: circular list issues.
872 (multiple-value-bind (min exactp)
873 (sb!kernel::cons-type-length-info type)
874 (let ((length (reduce #'+ sequences :key #'length)))
876 (unless (= length min)
877 (sequence-type-length-mismatch-error type length))
878 (unless (>= length min)
879 (sequence-type-length-mismatch-error type length)))
880 (concat-to-list* sequences))))
881 (t (sequence-type-too-hairy (type-specifier type)))))
882 ((csubtypep type (specifier-type 'vector))
883 (concat-to-simple* output-type-spec sequences))
884 ((and (csubtypep type (specifier-type 'sequence))
885 (find-class output-type-spec nil))
886 (coerce (concat-to-simple* 'vector sequences) output-type-spec))
888 (bad-sequence-type-error output-type-spec))))))
890 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
891 ;;; CONCATENATE 'STRING &co into these.
892 (macrolet ((def (name element-type)
893 `(defun ,name (&rest sequences)
894 (declare (dynamic-extent sequences)
896 (optimize (sb!c::insert-array-bounds-checks 0)))
897 (let* ((lengths (mapcar #'length sequences))
898 (result (make-array (the integer (apply #'+ lengths))
899 :element-type ',element-type))
901 (declare (index start))
902 (dolist (seq sequences)
904 ((simple-array character (*))
905 (simple-array base-char (*))
908 (replace result seq :start1 start))
909 (incf start (the index (pop lengths))))
911 (def %concatenate-to-string character)
912 (def %concatenate-to-base-string base-char))
916 ;;; helper functions to handle arity-1 subcases of MAP
917 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
918 (declaim (ftype (function (function sequence) simple-vector)
919 %map-simple-vector-arity-1))
920 (defun %map-to-list-arity-1 (fun sequence)
921 (let ((reversed-result nil)
922 (really-fun (%coerce-callable-to-fun fun)))
923 (sb!sequence:dosequence (element sequence)
924 (push (funcall really-fun element)
926 (nreverse reversed-result)))
927 (defun %map-to-simple-vector-arity-1 (fun sequence)
928 (let ((result (make-array (length sequence)))
930 (really-fun (%coerce-callable-to-fun fun)))
931 (declare (type index index))
932 (sb!sequence:dosequence (element sequence)
933 (setf (aref result index)
934 (funcall really-fun element))
937 (defun %map-for-effect-arity-1 (fun sequence)
938 (let ((really-fun (%coerce-callable-to-fun fun)))
939 (sb!sequence:dosequence (element sequence)
940 (funcall really-fun element)))
943 (declaim (maybe-inline %map-for-effect))
944 (defun %map-for-effect (fun sequences)
945 (declare (type function fun) (type list sequences))
946 (let ((%sequences sequences)
947 (%iters (mapcar (lambda (s)
952 (sb!sequence:make-sequence-iterator s))))
954 (%apply-args (make-list (length sequences))))
955 ;; this is almost efficient (except in the general case where we
956 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
957 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
958 (declare (type list %sequences %iters %apply-args))
960 (do ((in-sequences %sequences (cdr in-sequences))
961 (in-iters %iters (cdr in-iters))
962 (in-apply-args %apply-args (cdr in-apply-args)))
963 ((null in-sequences) (apply fun %apply-args))
964 (let ((i (car in-iters)))
965 (declare (type (or list index) i))
967 ((listp (car in-sequences))
969 (return-from %map-for-effect nil)
970 (setf (car in-apply-args) (car i)
971 (car in-iters) (cdr i))))
973 (let ((v (the vector (car in-sequences))))
974 (if (>= i (length v))
975 (return-from %map-for-effect nil)
976 (setf (car in-apply-args) (aref v i)
977 (car in-iters) (1+ i)))))
979 (destructuring-bind (state limit from-end step endp elt &rest ignore)
981 (declare (type function step endp elt)
983 (let ((s (car in-sequences)))
984 (if (funcall endp s state limit from-end)
985 (return-from %map-for-effect nil)
987 (setf (car in-apply-args) (funcall elt s state))
988 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
989 (defun %map-to-list (fun sequences)
990 (declare (type function fun)
991 (type list sequences))
993 (flet ((f (&rest args)
994 (declare (truly-dynamic-extent args))
995 (push (apply fun args) result)))
996 (declare (truly-dynamic-extent #'f))
997 (%map-for-effect #'f sequences))
999 (defun %map-to-vector (output-type-spec fun sequences)
1000 (declare (type function fun)
1001 (type list sequences))
1003 (flet ((f (&rest args)
1004 (declare (truly-dynamic-extent args))
1005 (declare (ignore args))
1007 (declare (truly-dynamic-extent #'f))
1008 (%map-for-effect #'f sequences))
1009 (let ((result (make-sequence output-type-spec min-len))
1011 (declare (type (simple-array * (*)) result))
1012 (flet ((f (&rest args)
1013 (declare (truly-dynamic-extent args))
1014 (setf (aref result i) (apply fun args))
1016 (declare (truly-dynamic-extent #'f))
1017 (%map-for-effect #'f sequences))
1019 (defun %map-to-sequence (result-type fun sequences)
1020 (declare (type function fun)
1021 (type list sequences))
1023 (flet ((f (&rest args)
1024 (declare (truly-dynamic-extent args))
1025 (declare (ignore args))
1027 (declare (truly-dynamic-extent #'f))
1028 (%map-for-effect #'f sequences))
1029 (let ((result (make-sequence result-type min-len)))
1030 (multiple-value-bind (state limit from-end step endp elt setelt)
1031 (sb!sequence:make-sequence-iterator result)
1032 (declare (ignore limit endp elt))
1033 (flet ((f (&rest args)
1034 (declare (truly-dynamic-extent args))
1035 (funcall setelt (apply fun args) result state)
1036 (setq state (funcall step result state from-end))))
1037 (declare (truly-dynamic-extent #'f))
1038 (%map-for-effect #'f sequences)))
1041 ;;; %MAP is just MAP without the final just-to-be-sure check that
1042 ;;; length of the output sequence matches any length specified
1044 (defun %map (result-type function first-sequence &rest more-sequences)
1045 (let ((really-fun (%coerce-callable-to-fun function))
1046 (type (specifier-type result-type)))
1047 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
1048 ;; it into something which can be DEFTRANSFORMed away. (It's
1049 ;; fairly important to handle this case efficiently, since
1050 ;; quantifiers like SOME are transformed into this case, and since
1051 ;; there's no consing overhead to dwarf our inefficiency.)
1052 (if (and (null more-sequences)
1054 (%map-for-effect-arity-1 really-fun first-sequence)
1055 ;; Otherwise, use the industrial-strength full-generality
1056 ;; approach, consing O(N-ARGS) temporary storage (which can have
1057 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
1058 (let ((sequences (cons first-sequence more-sequences)))
1060 ((eq type *empty-type*) (%map-for-effect really-fun sequences))
1061 ((csubtypep type (specifier-type 'list))
1062 (%map-to-list really-fun sequences))
1063 ((csubtypep type (specifier-type 'vector))
1064 (%map-to-vector result-type really-fun sequences))
1065 ((and (csubtypep type (specifier-type 'sequence))
1066 (find-class result-type nil))
1067 (%map-to-sequence result-type really-fun sequences))
1069 (bad-sequence-type-error result-type)))))))
1071 (defun map (result-type function first-sequence &rest more-sequences)
1080 (defmacro map-into-lambda (sequences params &body body)
1081 (check-type sequences symbol)
1082 `(flet ((f ,params ,@body))
1083 (declare (truly-dynamic-extent #'f))
1084 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1085 ;; hence the awkward flip between MAP and LOOP.
1087 (apply #'map nil #'f ,sequences)
1090 (define-array-dispatch vector-map-into (data start end fun sequences)
1091 (declare (optimize speed (safety 0))
1092 (type index start end)
1094 (type list sequences))
1095 (let ((index start))
1096 (declare (type index index))
1098 (map-into-lambda sequences (&rest args)
1099 (declare (truly-dynamic-extent args))
1100 (when (eql index end)
1101 (return-from mapping))
1102 (setf (aref data index) (apply fun args))
1106 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1107 ;;; computing the length of the result sequence since we can detect
1108 ;;; the end during mapping (if MAP even gets that far).
1110 ;;; For each result type, define a mapping function which is
1111 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1112 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1113 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1115 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1116 ;;; Because we are manually doing bounds checking with known types,
1117 ;;; safety is turned off for vectors and lists but kept for generic
1119 (defun map-into (result-sequence function &rest sequences)
1120 (let ((really-fun (%coerce-callable-to-fun function)))
1121 (etypecase result-sequence
1123 (with-array-data ((data result-sequence) (start) (end)
1124 ;; MAP-INTO ignores fill pointer when mapping
1125 :check-fill-pointer nil)
1126 (let ((new-end (vector-map-into data start end really-fun sequences)))
1127 (when (array-has-fill-pointer-p result-sequence)
1128 (setf (fill-pointer result-sequence) (- new-end start))))))
1130 (let ((node result-sequence))
1131 (declare (type list node))
1132 (map-into-lambda sequences (&rest args)
1133 (declare (truly-dynamic-extent args)
1134 (optimize speed (safety 0)))
1136 (return-from map-into result-sequence))
1137 (setf (car node) (apply really-fun args))
1138 (setf node (cdr node)))))
1140 (multiple-value-bind (iter limit from-end)
1141 (sb!sequence:make-sequence-iterator result-sequence)
1142 (map-into-lambda sequences (&rest args)
1143 (declare (truly-dynamic-extent args) (optimize speed))
1144 (when (sb!sequence:iterator-endp result-sequence
1145 iter limit from-end)
1146 (return-from map-into result-sequence))
1147 (setf (sb!sequence:iterator-element result-sequence iter)
1148 (apply really-fun args))
1149 (setf iter (sb!sequence:iterator-step result-sequence
1150 iter from-end)))))))
1155 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
1156 ;;; arbitrary sequence arguments, both in the full call case and in
1157 ;;; the open code case.
1158 (macrolet ((defquantifier (name found-test found-result
1159 &key doc (unfound-result (not found-result)))
1161 ;; KLUDGE: It would be really nice if we could simply
1162 ;; do something like this
1163 ;; (declaim (inline ,name))
1164 ;; (defun ,name (pred first-seq &rest more-seqs)
1166 ;; (flet ((map-me (&rest rest)
1167 ;; (let ((pred-value (apply pred rest)))
1168 ;; (,found-test pred-value
1169 ;; (return-from ,name
1170 ;; ,found-result)))))
1171 ;; (declare (inline map-me))
1172 ;; (apply #'map nil #'map-me first-seq more-seqs)
1173 ;; ,unfound-result))
1174 ;; but Python doesn't seem to be smart enough about
1175 ;; inlining and APPLY to recognize that it can use
1176 ;; the DEFTRANSFORM for MAP in the resulting inline
1177 ;; expansion. I don't have any appetite for deep
1178 ;; compiler hacking right now, so I'll just work
1179 ;; around the apparent problem by using a compiler
1180 ;; macro instead. -- WHN 20000410
1181 (defun ,name (pred first-seq &rest more-seqs)
1183 (flet ((map-me (&rest rest)
1184 (let ((pred-value (apply pred rest)))
1185 (,found-test pred-value
1188 (declare (inline map-me))
1189 (apply #'map nil #'map-me first-seq more-seqs)
1191 ;; KLUDGE: It would be more obviously correct -- but
1192 ;; also significantly messier -- for PRED-VALUE to be
1193 ;; a gensym. However, a private symbol really does
1194 ;; seem to be good enough; and anyway the really
1195 ;; obviously correct solution is to make Python smart
1196 ;; enough that we can use an inline function instead
1197 ;; of a compiler macro (as above). -- WHN 20000410
1199 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
1200 ;; important for performance, and it'd be good to have
1201 ;; it be visible throughout the compilation of all the
1202 ;; target SBCL code. That could be done by defining
1203 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
1204 ;; moving this DEFQUANTIFIER stuff (and perhaps other
1205 ;; inline definitions in seq.lisp as well) into a new
1206 ;; seq.lisp, and moving remaining target-only stuff
1207 ;; from the old seq.lisp into target-seq.lisp.
1208 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
1209 (let ((elements (make-gensym-list (1+ (length more-seqs))))
1210 (blockname (sb!xc:gensym "BLOCK")))
1211 (once-only ((pred pred))
1214 (lambda (,@elements)
1215 (let ((pred-value (funcall ,pred ,@elements)))
1216 (,',found-test pred-value
1217 (return-from ,blockname
1221 ,',unfound-result)))))))
1222 (defquantifier some when pred-value :unfound-result nil :doc
1223 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1224 possibly to those with index 1, and so on. Return the first
1225 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1226 (defquantifier every unless nil :doc
1227 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1228 possibly to those with index 1, and so on. Return NIL as soon
1229 as any invocation of PREDICATE returns NIL, or T if every invocation
1231 (defquantifier notany when nil :doc
1232 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1233 possibly to those with index 1, and so on. Return NIL as soon
1234 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1235 of any sequence is reached.")
1236 (defquantifier notevery unless t :doc
1237 "Apply PREDICATE to 0-indexed elements of the sequences, then
1238 possibly to those with index 1, and so on. Return T as soon
1239 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1244 (eval-when (:compile-toplevel :execute)
1246 (sb!xc:defmacro mumble-reduce (function
1253 `(do ((index ,start (1+ index))
1254 (value ,initial-value))
1255 ((>= index ,end) value)
1256 (setq value (funcall ,function value
1257 (apply-key ,key (,ref ,sequence index))))))
1259 (sb!xc:defmacro mumble-reduce-from-end (function
1266 `(do ((index (1- ,end) (1- index))
1267 (value ,initial-value)
1268 (terminus (1- ,start)))
1269 ((<= index terminus) value)
1270 (setq value (funcall ,function
1271 (apply-key ,key (,ref ,sequence index))
1274 (sb!xc:defmacro list-reduce (function
1281 `(let ((sequence (nthcdr ,start ,sequence)))
1282 (do ((count (if ,ivp ,start (1+ ,start))
1284 (sequence (if ,ivp sequence (cdr sequence))
1286 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1287 (funcall ,function value (apply-key ,key (car sequence)))))
1288 ((>= count ,end) value))))
1290 (sb!xc:defmacro list-reduce-from-end (function
1297 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1298 (reverse ,sequence))))
1299 (do ((count (if ,ivp ,start (1+ ,start))
1301 (sequence (if ,ivp sequence (cdr sequence))
1303 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1304 (funcall ,function (apply-key ,key (car sequence)) value)))
1305 ((>= count ,end) value))))
1309 (define-sequence-traverser reduce (function sequence &rest args &key key
1310 from-end start end (initial-value nil ivp))
1311 (declare (type index start)
1312 (truly-dynamic-extent args))
1313 (seq-dispatch sequence
1314 (let ((end (or end length)))
1315 (declare (type index end))
1317 (if ivp initial-value (funcall function))
1319 (list-reduce-from-end function sequence key start end
1321 (list-reduce function sequence key start end
1322 initial-value ivp))))
1323 (let ((end (or end length)))
1324 (declare (type index end))
1326 (if ivp initial-value (funcall function))
1330 (setq end (1- (the fixnum end)))
1331 (setq initial-value (apply-key key (aref sequence end))))
1332 (mumble-reduce-from-end function sequence key start end
1333 initial-value aref))
1336 (setq initial-value (apply-key key (aref sequence start)))
1337 (setq start (1+ start)))
1338 (mumble-reduce function sequence key start end
1339 initial-value aref)))))
1340 (apply #'sb!sequence:reduce function sequence args)))
1344 (eval-when (:compile-toplevel :execute)
1346 (sb!xc:defmacro mumble-delete (pred)
1347 `(do ((index start (1+ index))
1350 ((or (= index (the fixnum end)) (= number-zapped count))
1351 (do ((index index (1+ index)) ; Copy the rest of the vector.
1352 (jndex jndex (1+ jndex)))
1353 ((= index (the fixnum length))
1354 (shrink-vector sequence jndex))
1355 (declare (fixnum index jndex))
1356 (setf (aref sequence jndex) (aref sequence index))))
1357 (declare (fixnum index jndex number-zapped))
1358 (setf (aref sequence jndex) (aref sequence index))
1360 (incf number-zapped)
1363 (sb!xc:defmacro mumble-delete-from-end (pred)
1364 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1368 (terminus (1- start)))
1369 ((or (= index terminus) (= number-zapped count))
1370 (do ((losers losers) ; Delete the losers.
1371 (index start (1+ index))
1373 ((or (null losers) (= index (the fixnum end)))
1374 (do ((index index (1+ index)) ; Copy the rest of the vector.
1375 (jndex jndex (1+ jndex)))
1376 ((= index (the fixnum length))
1377 (shrink-vector sequence jndex))
1378 (declare (fixnum index jndex))
1379 (setf (aref sequence jndex) (aref sequence index))))
1380 (declare (fixnum index jndex))
1381 (setf (aref sequence jndex) (aref sequence index))
1382 (if (= index (the fixnum (car losers)))
1385 (declare (fixnum index number-zapped terminus))
1386 (setq this-element (aref sequence index))
1388 (incf number-zapped)
1389 (push index losers))))
1391 (sb!xc:defmacro normal-mumble-delete ()
1394 (not (funcall test-not item (apply-key key (aref sequence index))))
1395 (funcall test item (apply-key key (aref sequence index))))))
1397 (sb!xc:defmacro normal-mumble-delete-from-end ()
1398 `(mumble-delete-from-end
1400 (not (funcall test-not item (apply-key key this-element)))
1401 (funcall test item (apply-key key this-element)))))
1403 (sb!xc:defmacro list-delete (pred)
1404 `(let ((handle (cons nil sequence)))
1405 (do ((current (nthcdr start sequence) (cdr current))
1406 (previous (nthcdr start handle))
1407 (index start (1+ index))
1409 ((or (= index (the fixnum end)) (= number-zapped count))
1411 (declare (fixnum index number-zapped))
1413 (rplacd previous (cdr current))
1414 (incf number-zapped))
1416 (setq previous (cdr previous)))))))
1418 (sb!xc:defmacro list-delete-from-end (pred)
1419 `(let* ((reverse (nreverse (the list sequence)))
1420 (handle (cons nil reverse)))
1421 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1423 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1424 (index start (1+ index))
1426 ((or (= index (the fixnum end)) (= number-zapped count))
1427 (nreverse (cdr handle)))
1428 (declare (fixnum index number-zapped))
1430 (rplacd previous (cdr current))
1431 (incf number-zapped))
1433 (setq previous (cdr previous)))))))
1435 (sb!xc:defmacro normal-list-delete ()
1438 (not (funcall test-not item (apply-key key (car current))))
1439 (funcall test item (apply-key key (car current))))))
1441 (sb!xc:defmacro normal-list-delete-from-end ()
1442 '(list-delete-from-end
1444 (not (funcall test-not item (apply-key key (car current))))
1445 (funcall test item (apply-key key (car current))))))
1449 (define-sequence-traverser delete
1450 (item sequence &rest args &key from-end test test-not start
1453 "Return a sequence formed by destructively removing the specified ITEM from
1454 the given SEQUENCE."
1455 (declare (type fixnum start)
1456 (truly-dynamic-extent args))
1457 (seq-dispatch sequence
1458 (let ((end (or end length)))
1459 (declare (type index end))
1461 (normal-list-delete-from-end)
1462 (normal-list-delete)))
1463 (let ((end (or end length)))
1464 (declare (type index end))
1466 (normal-mumble-delete-from-end)
1467 (normal-mumble-delete)))
1468 (apply #'sb!sequence:delete item sequence args)))
1470 (eval-when (:compile-toplevel :execute)
1472 (sb!xc:defmacro if-mumble-delete ()
1474 (funcall predicate (apply-key key (aref sequence index)))))
1476 (sb!xc:defmacro if-mumble-delete-from-end ()
1477 `(mumble-delete-from-end
1478 (funcall predicate (apply-key key this-element))))
1480 (sb!xc:defmacro if-list-delete ()
1482 (funcall predicate (apply-key key (car current)))))
1484 (sb!xc:defmacro if-list-delete-from-end ()
1485 '(list-delete-from-end
1486 (funcall predicate (apply-key key (car current)))))
1490 (define-sequence-traverser delete-if
1491 (predicate sequence &rest args &key from-end start key end count)
1493 "Return a sequence formed by destructively removing the elements satisfying
1494 the specified PREDICATE from the given SEQUENCE."
1495 (declare (type fixnum start)
1496 (truly-dynamic-extent args))
1497 (seq-dispatch sequence
1498 (let ((end (or end length)))
1499 (declare (type index end))
1501 (if-list-delete-from-end)
1503 (let ((end (or end length)))
1504 (declare (type index end))
1506 (if-mumble-delete-from-end)
1507 (if-mumble-delete)))
1508 (apply #'sb!sequence:delete-if predicate sequence args)))
1510 (eval-when (:compile-toplevel :execute)
1512 (sb!xc:defmacro if-not-mumble-delete ()
1514 (not (funcall predicate (apply-key key (aref sequence index))))))
1516 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1517 `(mumble-delete-from-end
1518 (not (funcall predicate (apply-key key this-element)))))
1520 (sb!xc:defmacro if-not-list-delete ()
1522 (not (funcall predicate (apply-key key (car current))))))
1524 (sb!xc:defmacro if-not-list-delete-from-end ()
1525 '(list-delete-from-end
1526 (not (funcall predicate (apply-key key (car current))))))
1530 (define-sequence-traverser delete-if-not
1531 (predicate sequence &rest args &key from-end start end key count)
1533 "Return a sequence formed by destructively removing the elements not
1534 satisfying the specified PREDICATE from the given SEQUENCE."
1535 (declare (type fixnum start)
1536 (truly-dynamic-extent args))
1537 (seq-dispatch sequence
1538 (let ((end (or end length)))
1539 (declare (type index end))
1541 (if-not-list-delete-from-end)
1542 (if-not-list-delete)))
1543 (let ((end (or end length)))
1544 (declare (type index end))
1546 (if-not-mumble-delete-from-end)
1547 (if-not-mumble-delete)))
1548 (apply #'sb!sequence:delete-if-not predicate sequence args)))
1552 (eval-when (:compile-toplevel :execute)
1554 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1555 ;;; satisfies the predicate.
1556 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1557 `(do ((index ,begin (,bump index))
1559 (do ((index ,left (,bump index))
1560 (result (%make-sequence-like sequence length)))
1561 ((= index (the fixnum ,begin)) result)
1562 (declare (fixnum index))
1563 (setf (aref result index) (aref sequence index))))
1567 ((or (= index (the fixnum ,finish))
1568 (= number-zapped count))
1569 (do ((index index (,bump index))
1570 (new-index new-index (,bump new-index)))
1571 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1572 (declare (fixnum index new-index))
1573 (setf (aref result new-index) (aref sequence index))))
1574 (declare (fixnum index new-index number-zapped))
1575 (setq this-element (aref sequence index))
1576 (cond (,pred (incf number-zapped))
1577 (t (setf (aref result new-index) this-element)
1578 (setq new-index (,bump new-index))))))
1580 (sb!xc:defmacro mumble-remove (pred)
1581 `(mumble-remove-macro 1+ 0 start end length ,pred))
1583 (sb!xc:defmacro mumble-remove-from-end (pred)
1584 `(let ((sequence (copy-seq sequence)))
1585 (mumble-delete-from-end ,pred)))
1587 (sb!xc:defmacro normal-mumble-remove ()
1590 (not (funcall test-not item (apply-key key this-element)))
1591 (funcall test item (apply-key key this-element)))))
1593 (sb!xc:defmacro normal-mumble-remove-from-end ()
1594 `(mumble-remove-from-end
1596 (not (funcall test-not item (apply-key key this-element)))
1597 (funcall test item (apply-key key this-element)))))
1599 (sb!xc:defmacro if-mumble-remove ()
1600 `(mumble-remove (funcall predicate (apply-key key this-element))))
1602 (sb!xc:defmacro if-mumble-remove-from-end ()
1603 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1605 (sb!xc:defmacro if-not-mumble-remove ()
1606 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1608 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1609 `(mumble-remove-from-end
1610 (not (funcall predicate (apply-key key this-element)))))
1612 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1614 (sb!xc:defmacro list-remove-macro (pred reverse?)
1615 `(let* ((sequence ,(if reverse?
1616 '(reverse (the list sequence))
1618 (%start ,(if reverse? '(- length end) 'start))
1619 (%end ,(if reverse? '(- length start) 'end))
1621 (results (do ((index 0 (1+ index))
1622 (before-start splice))
1623 ((= index (the fixnum %start)) before-start)
1624 (declare (fixnum index))
1626 (cdr (rplacd splice (list (pop sequence))))))))
1627 (do ((index %start (1+ index))
1630 ((or (= index (the fixnum %end)) (= number-zapped count))
1631 (do ((index index (1+ index)))
1634 '(nreverse (the list (cdr results)))
1636 (declare (fixnum index))
1637 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1638 (declare (fixnum index number-zapped))
1639 (setq this-element (pop sequence))
1641 (setq number-zapped (1+ number-zapped))
1642 (setq splice (cdr (rplacd splice (list this-element))))))))
1644 (sb!xc:defmacro list-remove (pred)
1645 `(list-remove-macro ,pred nil))
1647 (sb!xc:defmacro list-remove-from-end (pred)
1648 `(list-remove-macro ,pred t))
1650 (sb!xc:defmacro normal-list-remove ()
1653 (not (funcall test-not item (apply-key key this-element)))
1654 (funcall test item (apply-key key this-element)))))
1656 (sb!xc:defmacro normal-list-remove-from-end ()
1657 `(list-remove-from-end
1659 (not (funcall test-not item (apply-key key this-element)))
1660 (funcall test item (apply-key key this-element)))))
1662 (sb!xc:defmacro if-list-remove ()
1664 (funcall predicate (apply-key key this-element))))
1666 (sb!xc:defmacro if-list-remove-from-end ()
1667 `(list-remove-from-end
1668 (funcall predicate (apply-key key this-element))))
1670 (sb!xc:defmacro if-not-list-remove ()
1672 (not (funcall predicate (apply-key key this-element)))))
1674 (sb!xc:defmacro if-not-list-remove-from-end ()
1675 `(list-remove-from-end
1676 (not (funcall predicate (apply-key key this-element)))))
1680 (define-sequence-traverser remove
1681 (item sequence &rest args &key from-end test test-not start
1684 "Return a copy of SEQUENCE with elements satisfying the test (default is
1685 EQL) with ITEM removed."
1686 (declare (type fixnum start)
1687 (truly-dynamic-extent args))
1688 (seq-dispatch sequence
1689 (let ((end (or end length)))
1690 (declare (type index end))
1692 (normal-list-remove-from-end)
1693 (normal-list-remove)))
1694 (let ((end (or end length)))
1695 (declare (type index end))
1697 (normal-mumble-remove-from-end)
1698 (normal-mumble-remove)))
1699 (apply #'sb!sequence:remove item sequence args)))
1701 (define-sequence-traverser remove-if
1702 (predicate sequence &rest args &key from-end start end count key)
1704 "Return a copy of sequence with elements satisfying PREDICATE removed."
1705 (declare (type fixnum start)
1706 (truly-dynamic-extent args))
1707 (seq-dispatch sequence
1708 (let ((end (or end length)))
1709 (declare (type index end))
1711 (if-list-remove-from-end)
1713 (let ((end (or end length)))
1714 (declare (type index end))
1716 (if-mumble-remove-from-end)
1717 (if-mumble-remove)))
1718 (apply #'sb!sequence:remove-if predicate sequence args)))
1720 (define-sequence-traverser remove-if-not
1721 (predicate sequence &rest args &key from-end start end count key)
1723 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1724 (declare (type fixnum start)
1725 (truly-dynamic-extent args))
1726 (seq-dispatch sequence
1727 (let ((end (or end length)))
1728 (declare (type index end))
1730 (if-not-list-remove-from-end)
1731 (if-not-list-remove)))
1732 (let ((end (or end length)))
1733 (declare (type index end))
1735 (if-not-mumble-remove-from-end)
1736 (if-not-mumble-remove)))
1737 (apply #'sb!sequence:remove-if-not predicate sequence args)))
1739 ;;;; REMOVE-DUPLICATES
1741 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1742 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1743 ;;; if we look into the already copied structure (from after :start) and see
1744 ;;; the item. If we check from beginning we check into the rest of the
1745 ;;; original list up to the :end marker (this we have to do by running a
1746 ;;; do loop down the list that far and using our test.
1747 (defun list-remove-duplicates* (list test test-not start end key from-end)
1748 (declare (fixnum start))
1749 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1752 (end (or end (length list)))
1753 (hash (and (> (- end start) 20)
1757 (or (eql test #'eql)
1760 (eql test #'equalp))
1761 (make-hash-table :test test :size (- end start)))))
1762 (do ((index 0 (1+ index)))
1764 (declare (fixnum index))
1765 (setq splice (cdr (rplacd splice (list (car current)))))
1766 (setq current (cdr current)))
1768 (do ((index start (1+ index)))
1769 ((or (and end (= index (the fixnum end)))
1771 (declare (fixnum index))
1772 ;; The hash table contains links from values that are
1773 ;; already in result to the cons cell *preceding* theirs
1774 ;; in the list. That is, for each value v in the list,
1775 ;; v and (cadr (gethash v hash)) are equal under TEST.
1776 (let ((prev (gethash (car current) hash)))
1779 (setf (gethash (car current) hash) splice)
1780 (setq splice (cdr (rplacd splice (list (car current))))))
1782 (let* ((old (cdr prev))
1785 (let ((next-val (car next)))
1786 ;; (assert (eq (gethash next-val hash) old))
1787 (setf (cdr prev) next
1788 (gethash next-val hash) prev
1789 (gethash (car current) hash) splice
1790 splice (cdr (rplacd splice (list (car current))))))
1791 (setf (car old) (car current)))))))
1792 (setq current (cdr current)))
1793 (do ((index start (1+ index)))
1794 ((or (and end (= index (the fixnum end)))
1796 (declare (fixnum index))
1797 (if (or (and from-end
1799 (member (apply-key key (car current))
1800 (nthcdr (1+ start) result)
1803 (member (apply-key key (car current))
1804 (nthcdr (1+ start) result)
1808 (not (do ((it (apply-key key (car current)))
1809 (l (cdr current) (cdr l))
1810 (i (1+ index) (1+ i)))
1811 ((or (atom l) (and end (= i (the fixnum end))))
1813 (declare (fixnum i))
1815 (not (funcall test-not
1817 (apply-key key (car l))))
1818 (funcall test it (apply-key key (car l))))
1820 (setq splice (cdr (rplacd splice (list (car current))))))
1821 (setq current (cdr current))))
1824 (setq splice (cdr (rplacd splice (list (car current)))))
1825 (setq current (cdr current)))
1828 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1829 &optional (length (length vector)))
1830 (declare (vector vector) (fixnum start length))
1831 (when (null end) (setf end (length vector)))
1832 (let ((result (%make-sequence-like vector length))
1835 (declare (fixnum index jndex))
1838 (setf (aref result index) (aref vector index))
1839 (setq index (1+ index)))
1842 (setq elt (aref vector index))
1843 (unless (or (and from-end
1845 (position (apply-key key elt) result
1846 :start start :end jndex
1847 :test-not test-not :key key)
1848 (position (apply-key key elt) result
1849 :start start :end jndex
1850 :test test :key key)))
1853 (position (apply-key key elt) vector
1854 :start (1+ index) :end end
1855 :test-not test-not :key key)
1856 (position (apply-key key elt) vector
1857 :start (1+ index) :end end
1858 :test test :key key))))
1859 (setf (aref result jndex) elt)
1860 (setq jndex (1+ jndex)))
1861 (setq index (1+ index)))
1864 (setf (aref result jndex) (aref vector index))
1865 (setq index (1+ index))
1866 (setq jndex (1+ jndex)))
1867 (%shrink-vector result jndex)))
1869 (define-sequence-traverser remove-duplicates
1870 (sequence &rest args &key test test-not start end from-end key)
1872 "The elements of SEQUENCE are compared pairwise, and if any two match,
1873 the one occurring earlier is discarded, unless FROM-END is true, in
1874 which case the one later in the sequence is discarded. The resulting
1875 sequence is returned.
1877 The :TEST-NOT argument is deprecated."
1878 (declare (fixnum start)
1879 (truly-dynamic-extent args))
1880 (seq-dispatch sequence
1882 (list-remove-duplicates* sequence test test-not
1883 start end key from-end))
1884 (vector-remove-duplicates* sequence test test-not start end key from-end)
1885 (apply #'sb!sequence:remove-duplicates sequence args)))
1887 ;;;; DELETE-DUPLICATES
1889 (defun list-delete-duplicates* (list test test-not key from-end start end)
1890 (declare (fixnum start))
1891 (let ((handle (cons nil list)))
1892 (do ((current (nthcdr start list) (cdr current))
1893 (previous (nthcdr start handle))
1894 (index start (1+ index)))
1895 ((or (and end (= index (the fixnum end))) (null current))
1897 (declare (fixnum index))
1898 (if (do ((x (if from-end
1899 (nthcdr (1+ start) handle)
1902 (i (1+ index) (1+ i)))
1904 (and (not from-end) end (= i (the fixnum end)))
1907 (declare (fixnum i))
1909 (not (funcall test-not
1910 (apply-key key (car current))
1911 (apply-key key (car x))))
1913 (apply-key key (car current))
1914 (apply-key key (car x))))
1916 (rplacd previous (cdr current))
1917 (setq previous (cdr previous))))))
1919 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1920 &optional (length (length vector)))
1921 (declare (vector vector) (fixnum start length))
1922 (when (null end) (setf end (length vector)))
1923 (do ((index start (1+ index))
1926 (do ((index index (1+ index)) ; copy the rest of the vector
1927 (jndex jndex (1+ jndex)))
1929 (shrink-vector vector jndex))
1930 (setf (aref vector jndex) (aref vector index))))
1931 (declare (fixnum index jndex))
1932 (setf (aref vector jndex) (aref vector index))
1933 (unless (if test-not
1934 (position (apply-key key (aref vector index)) vector :key key
1935 :start (if from-end start (1+ index))
1936 :end (if from-end jndex end)
1938 (position (apply-key key (aref vector index)) vector :key key
1939 :start (if from-end start (1+ index))
1940 :end (if from-end jndex end)
1942 (setq jndex (1+ jndex)))))
1944 (define-sequence-traverser delete-duplicates
1945 (sequence &rest args &key test test-not start end from-end key)
1947 "The elements of SEQUENCE are examined, and if any two match, one is
1948 discarded. The resulting sequence, which may be formed by destroying the
1949 given sequence, is returned.
1951 The :TEST-NOT argument is deprecated."
1952 (declare (truly-dynamic-extent args))
1953 (seq-dispatch sequence
1955 (list-delete-duplicates* sequence test test-not
1956 key from-end start end))
1957 (vector-delete-duplicates* sequence test test-not key from-end start end)
1958 (apply #'sb!sequence:delete-duplicates sequence args)))
1962 (defun list-substitute* (pred new list start end count key test test-not old)
1963 (declare (fixnum start end count))
1964 (let* ((result (list nil))
1967 (list list)) ; Get a local list for a stepper.
1968 (do ((index 0 (1+ index)))
1970 (declare (fixnum index))
1971 (setq splice (cdr (rplacd splice (list (car list)))))
1972 (setq list (cdr list)))
1973 (do ((index start (1+ index)))
1974 ((or (= index end) (null list) (= count 0)))
1975 (declare (fixnum index))
1976 (setq elt (car list))
1985 (funcall test-not old (apply-key key elt)))
1986 (funcall test old (apply-key key elt))))
1987 (if (funcall test (apply-key key elt)))
1988 (if-not (not (funcall test (apply-key key elt)))))
1992 (setq list (cdr list)))
1995 (setq splice (cdr (rplacd splice (list (car list)))))
1996 (setq list (cdr list)))
1999 ;;; Replace old with new in sequence moving from left to right by incrementer
2000 ;;; on each pass through the loop. Called by all three substitute functions.
2001 (defun vector-substitute* (pred new sequence incrementer left right length
2002 start end count key test test-not old)
2003 (declare (fixnum start count end incrementer right))
2004 (let ((result (%make-sequence-like sequence length))
2006 (declare (fixnum index))
2009 (setf (aref result index) (aref sequence index))
2010 (setq index (+ index incrementer)))
2012 ((or (= index end) (= count 0)))
2013 (setq elt (aref sequence index))
2014 (setf (aref result index)
2018 (not (funcall test-not old (apply-key key elt)))
2019 (funcall test old (apply-key key elt))))
2020 (if (funcall test (apply-key key elt)))
2021 (if-not (not (funcall test (apply-key key elt)))))
2022 (setq count (1- count))
2025 (setq index (+ index incrementer)))
2028 (setf (aref result index) (aref sequence index))
2029 (setq index (+ index incrementer)))
2032 (eval-when (:compile-toplevel :execute)
2034 (sb!xc:defmacro subst-dispatch (pred)
2035 `(seq-dispatch sequence
2036 (let ((end (or end length)))
2037 (declare (type index end))
2039 (nreverse (list-substitute* ,pred
2042 (- (the fixnum length)
2044 (- (the fixnum length)
2046 count key test test-not old))
2047 (list-substitute* ,pred
2048 new sequence start end count key test test-not
2051 (let ((end (or end length)))
2052 (declare (type index end))
2054 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2055 -1 length (1- (the fixnum end))
2056 (1- (the fixnum start))
2057 count key test test-not old)
2058 (vector-substitute* ,pred new sequence 1 0 length length
2059 start end count key test test-not old)))
2061 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2062 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2063 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2064 ;; dispatch once per element on PRED's run-time identity.
2066 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2067 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2068 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2071 (define-sequence-traverser substitute
2072 (new old sequence &rest args &key from-end test test-not
2073 start count end key)
2075 "Return a sequence of the same kind as SEQUENCE with the same elements,
2076 except that all elements equal to OLD are replaced with NEW."
2077 (declare (type fixnum start)
2078 (truly-dynamic-extent args))
2079 (subst-dispatch 'normal))
2081 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2083 (define-sequence-traverser substitute-if
2084 (new predicate sequence &rest args &key from-end start end count key)
2086 "Return a sequence of the same kind as SEQUENCE with the same elements
2087 except that all elements satisfying the PRED are replaced with NEW."
2088 (declare (type fixnum start)
2089 (truly-dynamic-extent args))
2090 (let ((test predicate)
2093 (subst-dispatch 'if)))
2095 (define-sequence-traverser substitute-if-not
2096 (new predicate sequence &rest args &key from-end start end count key)
2098 "Return a sequence of the same kind as SEQUENCE with the same elements
2099 except that all elements not satisfying the PRED are replaced with NEW."
2100 (declare (type fixnum start)
2101 (truly-dynamic-extent args))
2102 (let ((test predicate)
2105 (subst-dispatch 'if-not)))
2109 (define-sequence-traverser nsubstitute
2110 (new old sequence &rest args &key from-end test test-not
2111 end count key start)
2113 "Return a sequence of the same kind as SEQUENCE with the same elements
2114 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2115 may be destructively modified."
2116 (declare (type fixnum start)
2117 (truly-dynamic-extent args))
2118 (seq-dispatch sequence
2119 (let ((end (or end length)))
2120 (declare (type index end))
2122 (nreverse (nlist-substitute*
2123 new old (nreverse (the list sequence))
2124 test test-not (- length end) (- length start)
2126 (nlist-substitute* new old sequence
2127 test test-not start end count key)))
2128 (let ((end (or end length)))
2129 (declare (type index end))
2131 (nvector-substitute* new old sequence -1
2132 test test-not (1- end) (1- start) count key)
2133 (nvector-substitute* new old sequence 1
2134 test test-not start end count key)))
2135 (apply #'sb!sequence:nsubstitute new old sequence args)))
2137 (defun nlist-substitute* (new old sequence test test-not start end count key)
2138 (declare (fixnum start count end))
2139 (do ((list (nthcdr start sequence) (cdr list))
2140 (index start (1+ index)))
2141 ((or (= index end) (null list) (= count 0)) sequence)
2142 (declare (fixnum index))
2144 (not (funcall test-not old (apply-key key (car list))))
2145 (funcall test old (apply-key key (car list))))
2147 (setq count (1- count)))))
2149 (defun nvector-substitute* (new old sequence incrementer
2150 test test-not start end count key)
2151 (declare (fixnum start incrementer count end))
2152 (do ((index start (+ index incrementer)))
2153 ((or (= index end) (= count 0)) sequence)
2154 (declare (fixnum index))
2156 (not (funcall test-not
2158 (apply-key key (aref sequence index))))
2159 (funcall test old (apply-key key (aref sequence index))))
2160 (setf (aref sequence index) new)
2161 (setq count (1- count)))))
2163 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2165 (define-sequence-traverser nsubstitute-if
2166 (new predicate sequence &rest args &key from-end start end count key)
2168 "Return a sequence of the same kind as SEQUENCE with the same elements
2169 except that all elements satisfying PREDICATE are replaced with NEW.
2170 SEQUENCE may be destructively modified."
2171 (declare (type fixnum start)
2172 (truly-dynamic-extent args))
2173 (seq-dispatch sequence
2174 (let ((end (or end length)))
2175 (declare (type index end))
2177 (nreverse (nlist-substitute-if*
2178 new predicate (nreverse (the list sequence))
2179 (- length end) (- length start) count key))
2180 (nlist-substitute-if* new predicate sequence
2181 start end count key)))
2182 (let ((end (or end length)))
2183 (declare (type index end))
2185 (nvector-substitute-if* new predicate sequence -1
2186 (1- end) (1- start) count key)
2187 (nvector-substitute-if* new predicate sequence 1
2188 start end count key)))
2189 (apply #'sb!sequence:nsubstitute-if new predicate sequence args)))
2191 (defun nlist-substitute-if* (new test sequence start end count key)
2192 (declare (type fixnum end)
2193 (type function test)) ; coercion is done by caller
2194 (do ((list (nthcdr start sequence) (cdr list))
2195 (index start (1+ index)))
2196 ((or (= index end) (null list) (= count 0)) sequence)
2197 (when (funcall test (apply-key key (car list)))
2199 (setq count (1- count)))))
2201 (defun nvector-substitute-if* (new test sequence incrementer
2202 start end count key)
2203 (declare (type fixnum end)
2204 (type function test)) ; coercion is done by caller
2205 (do ((index start (+ index incrementer)))
2206 ((or (= index end) (= count 0)) sequence)
2207 (when (funcall test (apply-key key (aref sequence index)))
2208 (setf (aref sequence index) new)
2209 (setq count (1- count)))))
2211 (define-sequence-traverser nsubstitute-if-not
2212 (new predicate sequence &rest args &key from-end start end count key)
2214 "Return a sequence of the same kind as SEQUENCE with the same elements
2215 except that all elements not satisfying PREDICATE are replaced with NEW.
2216 SEQUENCE may be destructively modified."
2217 (declare (type fixnum start)
2218 (truly-dynamic-extent args))
2219 (seq-dispatch sequence
2220 (let ((end (or end length)))
2221 (declare (fixnum end))
2223 (nreverse (nlist-substitute-if-not*
2224 new predicate (nreverse (the list sequence))
2225 (- length end) (- length start) count key))
2226 (nlist-substitute-if-not* new predicate sequence
2227 start end count key)))
2228 (let ((end (or end length)))
2229 (declare (fixnum end))
2231 (nvector-substitute-if-not* new predicate sequence -1
2232 (1- end) (1- start) count key)
2233 (nvector-substitute-if-not* new predicate sequence 1
2234 start end count key)))
2235 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args)))
2237 (defun nlist-substitute-if-not* (new test sequence start end count key)
2238 (declare (type fixnum end)
2239 (type function test)) ; coercion is done by caller
2240 (do ((list (nthcdr start sequence) (cdr list))
2241 (index start (1+ index)))
2242 ((or (= index end) (null list) (= count 0)) sequence)
2243 (when (not (funcall test (apply-key key (car list))))
2247 (defun nvector-substitute-if-not* (new test sequence incrementer
2248 start end count key)
2249 (declare (type fixnum end)
2250 (type function test)) ; coercion is done by caller
2251 (do ((index start (+ index incrementer)))
2252 ((or (= index end) (= count 0)) sequence)
2253 (when (not (funcall test (apply-key key (aref sequence index))))
2254 (setf (aref sequence index) new)
2257 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2259 (defun effective-find-position-test (test test-not)
2260 (effective-find-position-test test test-not))
2261 (defun effective-find-position-key (key)
2262 (effective-find-position-key key))
2264 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2265 (macrolet (;; shared logic for defining %FIND-POSITION and
2266 ;; %FIND-POSITION-IF in terms of various inlineable cases
2267 ;; of the expression defined in FROB and VECTOR*-FROB
2268 (frobs (&optional bit-frob)
2269 `(seq-dispatch sequence-arg
2270 (frob sequence-arg from-end)
2271 (with-array-data ((sequence sequence-arg :offset-var offset)
2274 :check-fill-pointer t)
2275 (multiple-value-bind (f p)
2276 (macrolet ((frob2 () `(if from-end
2278 (frob sequence nil))))
2281 ((simple-array character (*)) (frob2))
2282 ((simple-array base-char (*)) (frob2))
2284 `((simple-bit-vector
2285 (if (and (typep item 'bit)
2290 (let ((p (%bit-position item sequence
2291 from-end start end)))
2295 (vector*-frob sequence)))))
2297 (vector*-frob sequence))))
2298 (declare (type (or index null) p))
2299 (values f (and p (the index (- p offset)))))))))
2300 (defun %find-position (item sequence-arg from-end start end key test)
2301 (macrolet ((frob (sequence from-end)
2302 `(%find-position item ,sequence
2303 ,from-end start end key test))
2304 (vector*-frob (sequence)
2305 `(%find-position-vector-macro item ,sequence
2306 from-end start end key test)))
2308 (defun %find-position-if (predicate sequence-arg from-end start end key)
2309 (macrolet ((frob (sequence from-end)
2310 `(%find-position-if predicate ,sequence
2311 ,from-end start end key))
2312 (vector*-frob (sequence)
2313 `(%find-position-if-vector-macro predicate ,sequence
2314 from-end start end key)))
2316 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2317 (macrolet ((frob (sequence from-end)
2318 `(%find-position-if-not predicate ,sequence
2319 ,from-end start end key))
2320 (vector*-frob (sequence)
2321 `(%find-position-if-not-vector-macro predicate ,sequence
2322 from-end start end key)))
2326 (item sequence &rest args &key from-end (start 0) end key test test-not)
2327 (declare (truly-dynamic-extent args))
2328 (seq-dispatch sequence
2329 (nth-value 0 (%find-position
2330 item sequence from-end start end
2331 (effective-find-position-key key)
2332 (effective-find-position-test test test-not)))
2333 (nth-value 0 (%find-position
2334 item sequence from-end start end
2335 (effective-find-position-key key)
2336 (effective-find-position-test test test-not)))
2337 (apply #'sb!sequence:find item sequence args)))
2339 (item sequence &rest args &key from-end (start 0) end key test test-not)
2340 (declare (truly-dynamic-extent args))
2341 (seq-dispatch sequence
2342 (nth-value 1 (%find-position
2343 item sequence from-end start end
2344 (effective-find-position-key key)
2345 (effective-find-position-test test test-not)))
2346 (nth-value 1 (%find-position
2347 item sequence from-end start end
2348 (effective-find-position-key key)
2349 (effective-find-position-test test test-not)))
2350 (apply #'sb!sequence:position item sequence args)))
2352 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2353 (declare (truly-dynamic-extent args))
2354 (seq-dispatch sequence
2355 (nth-value 0 (%find-position-if
2356 (%coerce-callable-to-fun predicate)
2357 sequence from-end start end
2358 (effective-find-position-key key)))
2359 (nth-value 0 (%find-position-if
2360 (%coerce-callable-to-fun predicate)
2361 sequence from-end start end
2362 (effective-find-position-key key)))
2363 (apply #'sb!sequence:find-if predicate sequence args)))
2365 (predicate sequence &rest args &key from-end (start 0) end key)
2366 (declare (truly-dynamic-extent args))
2367 (seq-dispatch sequence
2368 (nth-value 1 (%find-position-if
2369 (%coerce-callable-to-fun predicate)
2370 sequence from-end start end
2371 (effective-find-position-key key)))
2372 (nth-value 1 (%find-position-if
2373 (%coerce-callable-to-fun predicate)
2374 sequence from-end start end
2375 (effective-find-position-key key)))
2376 (apply #'sb!sequence:position-if predicate sequence args)))
2379 (predicate sequence &rest args &key from-end (start 0) end key)
2380 (declare (truly-dynamic-extent args))
2381 (seq-dispatch sequence
2382 (nth-value 0 (%find-position-if-not
2383 (%coerce-callable-to-fun predicate)
2384 sequence from-end start end
2385 (effective-find-position-key key)))
2386 (nth-value 0 (%find-position-if-not
2387 (%coerce-callable-to-fun predicate)
2388 sequence from-end start end
2389 (effective-find-position-key key)))
2390 (apply #'sb!sequence:find-if-not predicate sequence args)))
2391 (defun position-if-not
2392 (predicate sequence &rest args &key from-end (start 0) end key)
2393 (declare (truly-dynamic-extent args))
2394 (seq-dispatch sequence
2395 (nth-value 1 (%find-position-if-not
2396 (%coerce-callable-to-fun predicate)
2397 sequence from-end start end
2398 (effective-find-position-key key)))
2399 (nth-value 1 (%find-position-if-not
2400 (%coerce-callable-to-fun predicate)
2401 sequence from-end start end
2402 (effective-find-position-key key)))
2403 (apply #'sb!sequence:position-if-not predicate sequence args)))
2405 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2407 (eval-when (:compile-toplevel :execute)
2409 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2410 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2411 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2412 `(let ((%start ,(if from-end-p '(1- end) 'start))
2413 (%end ,(if from-end-p '(1- start) 'end)))
2414 (do ((index %start ,next-index)
2416 ((= index (the fixnum %end)) count)
2417 (declare (fixnum index count))
2418 (,(if notp 'unless 'when) ,pred
2419 (setq count (1+ count)))))))
2421 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2422 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2423 `(let ((%start ,(if from-end-p '(- length end) 'start))
2424 (%end ,(if from-end-p '(- length start) 'end))
2425 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2426 (do ((sequence (nthcdr %start ,sequence))
2427 (index %start (1+ index))
2429 ((or (= index (the fixnum %end)) (null sequence)) count)
2430 (declare (fixnum index count))
2431 (,(if notp 'unless 'when) ,pred
2432 (setq count (1+ count)))))))
2437 (define-sequence-traverser count-if
2438 (pred sequence &rest args &key from-end start end key)
2440 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2441 (declare (type fixnum start)
2442 (truly-dynamic-extent args))
2443 (let ((pred (%coerce-callable-to-fun pred)))
2444 (seq-dispatch sequence
2445 (let ((end (or end length)))
2446 (declare (type index end))
2448 (list-count-if nil t pred sequence)
2449 (list-count-if nil nil pred sequence)))
2450 (let ((end (or end length)))
2451 (declare (type index end))
2453 (vector-count-if nil t pred sequence)
2454 (vector-count-if nil nil pred sequence)))
2455 (apply #'sb!sequence:count-if pred sequence args))))
2457 (define-sequence-traverser count-if-not
2458 (pred sequence &rest args &key from-end start end key)
2460 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2461 (declare (type fixnum start)
2462 (truly-dynamic-extent args))
2463 (let ((pred (%coerce-callable-to-fun pred)))
2464 (seq-dispatch sequence
2465 (let ((end (or end length)))
2466 (declare (type index end))
2468 (list-count-if t t pred sequence)
2469 (list-count-if t nil pred sequence)))
2470 (let ((end (or end length)))
2471 (declare (type index end))
2473 (vector-count-if t t pred sequence)
2474 (vector-count-if t nil pred sequence)))
2475 (apply #'sb!sequence:count-if-not pred sequence args))))
2477 (define-sequence-traverser count
2478 (item sequence &rest args &key from-end start end
2479 key (test #'eql test-p) (test-not nil test-not-p))
2481 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2482 which defaults to EQL."
2483 (declare (type fixnum start)
2484 (truly-dynamic-extent args))
2485 (when (and test-p test-not-p)
2486 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2488 (error ":TEST and :TEST-NOT are both present."))
2489 (let ((%test (if test-not-p
2491 (not (funcall test-not item x)))
2493 (funcall test item x)))))
2494 (seq-dispatch sequence
2495 (let ((end (or end length)))
2496 (declare (type index end))
2498 (list-count-if nil t %test sequence)
2499 (list-count-if nil nil %test sequence)))
2500 (let ((end (or end length)))
2501 (declare (type index end))
2503 (vector-count-if nil t %test sequence)
2504 (vector-count-if nil nil %test sequence)))
2505 (apply #'sb!sequence:count item sequence args))))
2509 (eval-when (:compile-toplevel :execute)
2511 (sb!xc:defmacro match-vars (&rest body)
2512 `(let ((inc (if from-end -1 1))
2513 (start1 (if from-end (1- (the fixnum end1)) start1))
2514 (start2 (if from-end (1- (the fixnum end2)) start2))
2515 (end1 (if from-end (1- (the fixnum start1)) end1))
2516 (end2 (if from-end (1- (the fixnum start2)) end2)))
2517 (declare (fixnum inc start1 start2 end1 end2))
2520 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2521 (declare (ignore end)) ;; ### Should END be used below?
2522 `(let ((,sequence (if from-end
2523 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2524 (reverse (the list ,sequence)))
2525 (nthcdr ,start ,sequence))))
2526 (declare (type list ,sequence))
2531 (eval-when (:compile-toplevel :execute)
2533 (sb!xc:defmacro if-mismatch (elt1 elt2)
2534 `(cond ((= (the fixnum index1) (the fixnum end1))
2535 (return (if (= (the fixnum index2) (the fixnum end2))
2538 (1+ (the fixnum index1))
2539 (the fixnum index1)))))
2540 ((= (the fixnum index2) (the fixnum end2))
2541 (return (if from-end (1+ (the fixnum index1)) index1)))
2543 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2544 (return (if from-end (1+ (the fixnum index1)) index1))))
2545 (t (if (not (funcall test (apply-key key ,elt1)
2546 (apply-key key ,elt2)))
2547 (return (if from-end (1+ (the fixnum index1)) index1))))))
2549 (sb!xc:defmacro mumble-mumble-mismatch ()
2550 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2551 (index2 start2 (+ index2 (the fixnum inc))))
2553 (declare (fixnum index1 index2))
2554 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2556 (sb!xc:defmacro mumble-list-mismatch ()
2557 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2558 (index2 start2 (+ index2 (the fixnum inc))))
2560 (declare (fixnum index1 index2))
2561 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2563 (sb!xc:defmacro list-mumble-mismatch ()
2564 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2565 (index2 start2 (+ index2 (the fixnum inc))))
2567 (declare (fixnum index1 index2))
2568 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2570 (sb!xc:defmacro list-list-mismatch ()
2571 `(do ((sequence1 sequence1)
2572 (sequence2 sequence2)
2573 (index1 start1 (+ index1 (the fixnum inc)))
2574 (index2 start2 (+ index2 (the fixnum inc))))
2576 (declare (fixnum index1 index2))
2577 (if-mismatch (pop sequence1) (pop sequence2))))
2581 (define-sequence-traverser mismatch
2582 (sequence1 sequence2 &rest args &key from-end test test-not
2583 start1 end1 start2 end2 key)
2585 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2586 element-wise. If they are of equal length and match in every element, the
2587 result is NIL. Otherwise, the result is a non-negative integer, the index
2588 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2589 if one is shorter than and a matching prefix of the other, the index within
2590 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2591 :FROM-END argument is given, then one plus the index of the rightmost
2592 position in which the sequences differ is returned."
2593 (declare (type fixnum start1 start2))
2594 (declare (truly-dynamic-extent args))
2595 (seq-dispatch sequence1
2596 (seq-dispatch sequence2
2597 (let ((end1 (or end1 length1))
2598 (end2 (or end2 length2)))
2599 (declare (type index end1 end2))
2601 (matchify-list (sequence1 start1 length1 end1)
2602 (matchify-list (sequence2 start2 length2 end2)
2603 (list-list-mismatch)))))
2604 (let ((end1 (or end1 length1))
2605 (end2 (or end2 length2)))
2606 (declare (type index end1 end2))
2608 (matchify-list (sequence1 start1 length1 end1)
2609 (list-mumble-mismatch))))
2610 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2611 (seq-dispatch sequence2
2612 (let ((end1 (or end1 length1))
2613 (end2 (or end2 length2)))
2614 (declare (type index end1 end2))
2616 (matchify-list (sequence2 start2 length2 end2)
2617 (mumble-list-mismatch))))
2618 (let ((end1 (or end1 length1))
2619 (end2 (or end2 length2)))
2620 (declare (type index end1 end2))
2622 (mumble-mumble-mismatch)))
2623 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2624 (apply #'sb!sequence:mismatch sequence1 sequence2 args)))
2627 ;;; search comparison functions
2629 (eval-when (:compile-toplevel :execute)
2631 ;;; Compare two elements and return if they don't match.
2632 (sb!xc:defmacro compare-elements (elt1 elt2)
2634 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2637 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2641 (sb!xc:defmacro search-compare-list-list (main sub)
2642 `(do ((main ,main (cdr main))
2643 (jndex start1 (1+ jndex))
2644 (sub (nthcdr start1 ,sub) (cdr sub)))
2645 ((or (endp main) (endp sub) (<= end1 jndex))
2647 (declare (type (integer 0) jndex))
2648 (compare-elements (car sub) (car main))))
2650 (sb!xc:defmacro search-compare-list-vector (main sub)
2651 `(do ((main ,main (cdr main))
2652 (index start1 (1+ index)))
2653 ((or (endp main) (= index end1)) t)
2654 (compare-elements (aref ,sub index) (car main))))
2656 (sb!xc:defmacro search-compare-vector-list (main sub index)
2657 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2658 (jndex start1 (1+ jndex))
2659 (index ,index (1+ index)))
2660 ((or (<= end1 jndex) (endp sub)) t)
2661 (declare (type (integer 0) jndex))
2662 (compare-elements (car sub) (aref ,main index))))
2664 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2665 `(do ((index ,index (1+ index))
2666 (sub-index start1 (1+ sub-index)))
2667 ((= sub-index end1) t)
2668 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2670 (sb!xc:defmacro search-compare (main-type main sub index)
2671 (if (eq main-type 'list)
2673 (search-compare-list-list ,main ,sub)
2674 (search-compare-list-vector ,main ,sub)
2675 ;; KLUDGE: just hack it together so that it works
2676 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2678 (search-compare-vector-list ,main ,sub ,index)
2679 (search-compare-vector-vector ,main ,sub ,index)
2680 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2686 (eval-when (:compile-toplevel :execute)
2688 (sb!xc:defmacro list-search (main sub)
2689 `(do ((main (nthcdr start2 ,main) (cdr main))
2690 (index2 start2 (1+ index2))
2691 (terminus (- end2 (the (integer 0) (- end1 start1))))
2693 ((> index2 terminus) last-match)
2694 (declare (type (integer 0) index2))
2695 (if (search-compare list main ,sub index2)
2697 (setq last-match index2)
2700 (sb!xc:defmacro vector-search (main sub)
2701 `(do ((index2 start2 (1+ index2))
2702 (terminus (- end2 (the (integer 0) (- end1 start1))))
2704 ((> index2 terminus) last-match)
2705 (declare (type (integer 0) index2))
2706 (if (search-compare vector ,main ,sub index2)
2708 (setq last-match index2)
2713 (define-sequence-traverser search
2714 (sequence1 sequence2 &rest args &key
2715 from-end test test-not start1 end1 start2 end2 key)
2716 (declare (type fixnum start1 start2)
2717 (truly-dynamic-extent args))
2718 (seq-dispatch sequence2
2719 (let ((end1 (or end1 length1))
2720 (end2 (or end2 length2)))
2721 (declare (type index end1 end2))
2722 (list-search sequence2 sequence1))
2723 (let ((end1 (or end1 length1))
2724 (end2 (or end2 length2)))
2725 (declare (type index end1 end2))
2726 (vector-search sequence2 sequence1))
2727 (apply #'sb!sequence:search sequence1 sequence2 args)))
2729 ;;; FIXME: this was originally in array.lisp; it might be better to
2730 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2731 ;;; a new early-seq.lisp file.
2732 (defun fill-data-vector (vector dimensions initial-contents)
2734 (labels ((frob (axis dims contents)
2736 (setf (aref vector index) contents)
2739 (unless (typep contents 'sequence)
2740 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2741 sequence, but ~W more layer~:P needed."
2743 (- (length dimensions) axis)))
2744 (unless (= (length contents) (car dims))
2745 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2746 axis ~W is ~W, but ~S is ~W long."
2747 axis (car dims) contents (length contents)))
2748 (sb!sequence:dosequence (content contents)
2749 (frob (1+ axis) (cdr dims) content))))))
2750 (frob 0 dimensions initial-contents))))