Optimize full calls to FILL on specialized vectors.
[sbcl.git] / src / code / seq.lisp
blob7cc0424c00e38a2e52a833eadb4cbf528ba5a78e
1 ;;;; generic SEQUENCEs
2 ;;;;
3 ;;;; KLUDGE: comment from original CMU CL source:
4 ;;;; Be careful when modifying code. A lot of the structure of the
5 ;;;; code is affected by the fact that compiler transforms use the
6 ;;;; lower level support functions. If transforms are written for
7 ;;;; some sequence operation, note how the END argument is handled
8 ;;;; in other operations with transforms.
10 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; more information.
12 ;;;;
13 ;;;; This software is derived from the CMU CL system, which was
14 ;;;; written at Carnegie Mellon University and released into the
15 ;;;; public domain. The software is in the public domain and is
16 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
17 ;;;; files for more information.
19 (in-package "SB!IMPL")
21 ;;;; utilities
23 (defun %check-generic-sequence-bounds (seq start end)
24 (let ((length (sb!sequence:length seq)))
25 (if (<= 0 start (or end length) length)
26 (or end length)
27 (sequence-bounding-indices-bad-error seq start end))))
29 (eval-when (:compile-toplevel :load-toplevel :execute)
30 (defparameter *sequence-keyword-info*
31 ;; (name default supplied-p adjustment new-type)
32 `((count nil
33 nil
34 (etypecase count
35 (null (1- most-positive-fixnum))
36 (fixnum (max 0 count))
37 (integer (if (minusp count)
39 (1- most-positive-fixnum))))
40 (mod #.sb!xc:most-positive-fixnum))
41 ;; Entries for {start,end}{,1,2}
42 ,@(mapcan (lambda (names)
43 (destructuring-bind (start end length sequence) names
44 (list
45 `(,start
47 nil
48 ;; Only evaluate LENGTH (which may be expensive)
49 ;; if START is non-NIL.
50 (if (or (zerop ,start) (<= 0 ,start ,length))
51 ,start
52 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
53 index)
54 `(,end
55 nil
56 nil
57 ;; Only evaluate LENGTH (which may be expensive)
58 ;; if END is non-NIL.
59 (if (or (null ,end) (<= ,start ,end ,length))
60 ;; Defaulting of NIL is done inside the
61 ;; bodies, for ease of sharing with compiler
62 ;; transforms.
64 ;; FIXME: defend against non-number non-NIL
65 ;; stuff?
66 ,end
67 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
68 (or null index)))))
69 '((start end length sequence)
70 (start1 end1 length1 sequence1)
71 (start2 end2 length2 sequence2)))
72 (key nil
73 nil
74 (and key (%coerce-callable-to-fun key))
75 (or null function))
76 (test #'eql
77 nil
78 (%coerce-callable-to-fun test)
79 function)
80 (test-not nil
81 nil
82 (and test-not (%coerce-callable-to-fun test-not))
83 (or null function)))))
85 (sb!xc:defmacro define-sequence-traverser (name args &body body)
86 (multiple-value-bind (body declarations docstring) (parse-body body t)
87 (collect ((new-args)
88 (new-declarations)
89 ;; Things which are definitely used in any code path.
90 (rebindings/eager)
91 ;; Things which may be used/are only used in certain
92 ;; code paths (e.g. length).
93 (rebindings/lazy))
94 (dolist (arg args)
95 (case arg
96 ;; FIXME: make this robust. And clean.
97 ((sequence sequence1 sequence2)
98 (let* ((length-var (ecase arg
99 (sequence 'length)
100 (sequence1 'length1)
101 (sequence2 'length2)))
102 (cache-var (symbolicate length-var '#:-cache)))
103 (new-args arg)
104 (rebindings/eager `(,cache-var nil))
105 (rebindings/lazy
106 `(,length-var (truly-the
107 index
108 (or ,cache-var (setf ,cache-var (length ,arg))))))))
109 ((function predicate)
110 (new-args arg)
111 (rebindings/eager `(,arg (%coerce-callable-to-fun ,arg))))
113 (let ((info (cdr (assoc arg *sequence-keyword-info*))))
114 (cond (info
115 (destructuring-bind (default supplied-p adjuster type) info
116 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
117 (rebindings/eager `(,arg ,adjuster))
118 (new-declarations `(type ,type ,arg))))
119 (t (new-args arg)))))))
120 `(defun ,name ,(new-args)
121 ,@(when docstring (list docstring))
122 ,@declarations
123 (symbol-macrolet (,@(rebindings/lazy))
124 (let* (,@(rebindings/eager))
125 (declare ,@(new-declarations))
126 ,@body))))))
128 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
130 ;;; FIXME: It might be worth making three cases here, LIST,
131 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
132 ;;; It tends to make code run faster but be bigger; some benchmarking
133 ;;; is needed to decide.
134 (sb!xc:defmacro seq-dispatch
135 (sequence list-form array-form &optional other-form)
136 `(if (listp ,sequence)
137 (let ((,sequence (truly-the list ,sequence)))
138 (declare (ignorable ,sequence))
139 ,list-form)
140 ,@(if other-form
141 `((if (arrayp ,sequence)
142 (let ((,sequence (truly-the vector ,sequence)))
143 (declare (ignorable ,sequence))
144 ,array-form)
145 ,other-form))
146 `((let ((,sequence (truly-the vector ,sequence)))
147 (declare (ignorable ,sequence))
148 ,array-form)))))
150 ;; Same as above, but don't assume that ARRAYP implies VECTORP,
151 ;; and test EXTENDED-SEQUENCE-P if three cases are allowed.
152 ;; Signal a type error for non-sequences.
153 ;; This is for dispatching within sequence functions that have
154 ;; the EXPLICIT-CHECK attribute on at least their sequence arg(s).
155 (eval-when (:compile-toplevel)
156 (sb!xc:defmacro seq-dispatch-checking
157 (sequence list-form vector-form &optional (other-form nil other-form-p))
158 `(cond ((listp ,sequence)
159 (let ((,sequence (truly-the list ,sequence)))
160 (declare (ignorable ,sequence))
161 ,list-form))
162 ((vectorp ,sequence)
163 (let ((,sequence (truly-the vector ,sequence)))
164 (declare (ignorable ,sequence))
165 ,vector-form))
166 ,@(cond ((not other-form-p)
167 `((t
168 (sb!c::%type-check-error
169 ,sequence '(or list vector)))))
170 (other-form
171 `(((extended-sequence-p ,sequence)
172 (let ((,sequence (truly-the extended-sequence ,sequence)))
173 (declare (ignorable ,sequence))
174 ,other-form))
176 (sb!c::%type-check-error/c
177 ,sequence 'sb!kernel::object-not-sequence-error)))))))
179 ;;; Like SEQ-DISPATCH-CHECKING, but also assert that OTHER-FORM produces
180 ;;; a sequence. This assumes that the containing function declares its
181 ;;; result to be explicitly checked,
182 ;;; and that the LIST and VECTOR cases never fail to return a sequence.
183 (sb!xc:defmacro seq-dispatch-checking=>seq
184 (sequence list-form vector-form other-form)
185 `(seq-dispatch-checking ,sequence ,list-form ,vector-form
186 (the sequence (values ,other-form)))))
188 (sb!xc:defmacro %make-sequence-like (sequence length)
189 #!+sb-doc
190 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
191 `(seq-dispatch ,sequence
192 (make-list ,length)
193 (make-vector-like ,sequence ,length)
194 (sb!sequence:make-sequence-like ,sequence ,length)))
196 (defun bad-sequence-type-error (type-spec)
197 (declare (optimize allow-non-returning-tail-call))
198 (error 'simple-type-error
199 :datum type-spec
200 :expected-type '(satisfies is-a-valid-sequence-type-specifier-p)
201 :format-control "~S is a bad type specifier for sequences."
202 :format-arguments (list type-spec)))
204 (defun sequence-type-length-mismatch-error (type length)
205 (declare (optimize allow-non-returning-tail-call))
206 (error 'simple-type-error
207 :datum length
208 :expected-type (cond ((array-type-p type)
209 `(eql ,(car (array-type-dimensions type))))
210 ((type= type (specifier-type 'null))
211 '(eql 0))
212 ((cons-type-p type)
213 '(integer 1))
214 (t (bug "weird type in S-T-L-M-ERROR")))
215 ;; FIXME: this format control causes ugly printing. There's
216 ;; probably some ~<~@:_~> incantation that would make it
217 ;; nicer. -- CSR, 2002-10-18
218 :format-control "The length requested (~S) does not match the type restriction in ~S."
219 :format-arguments (list length (type-specifier type))))
221 (defun sequence-type-too-hairy (type-spec)
222 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
223 ;; words that give some but not total support to this position in
224 ;; ANSI. Essentially, we are justified in throwing this on
225 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
226 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
228 ;; On the other hand, I'm not sure it deserves to be a type-error,
229 ;; either. -- bem, 2005-08-10
230 (declare (optimize allow-non-returning-tail-call))
231 (error 'simple-program-error
232 :format-control "~S is too hairy for sequence functions."
233 :format-arguments (list type-spec)))
235 (sb!xc:defmacro when-extended-sequence-type
236 ((type-specifier type
237 &key
238 expandedp
239 (expanded (gensym "EXPANDED"))
240 (class (gensym "CLASS"))
241 (prototype (gensym "PROTOTYPE") prototypep))
242 &body body)
243 (once-only ((type-specifier type-specifier) (type type))
244 `(when (csubtypep ,type (specifier-type 'sequence))
245 (binding* ((,expanded ,(if expandedp
246 type-specifier
247 `(typexpand ,type-specifier)))
248 (,class (if (typep ,expanded 'class)
249 ,expanded
250 (find-class ,expanded nil))
251 :exit-if-null)
252 (,prototype (sb!mop:class-prototype
253 (sb!pcl:ensure-class-finalized ,class))))
254 ,@(unless prototypep `((ignore ,prototype)))
255 ,@body))))
257 (defun is-a-valid-sequence-type-specifier-p (type)
258 (let ((type (specifier-type type)))
259 (or (csubtypep type (specifier-type 'list))
260 (csubtypep type (specifier-type 'vector)))))
262 ;;; It's possible with some sequence operations to declare the length
263 ;;; of a result vector, and to be safe, we really ought to verify that
264 ;;; the actual result has the declared length.
265 (defun vector-of-checked-length-given-length (vector declared-length)
266 (declare (type vector vector))
267 (declare (type index declared-length))
268 (let ((actual-length (length vector)))
269 (unless (= actual-length declared-length)
270 (error 'simple-type-error
271 :datum vector
272 :expected-type `(vector ,declared-length)
273 :format-control
274 "Vector length (~W) doesn't match declared length (~W)."
275 :format-arguments (list actual-length declared-length))))
276 vector)
278 (defun sequence-of-checked-length-given-type (sequence result-type)
279 (let ((ctype (specifier-type result-type)))
280 (if (not (array-type-p ctype))
281 sequence
282 (let ((declared-length (first (array-type-dimensions ctype))))
283 (if (eq declared-length '*)
284 sequence
285 (vector-of-checked-length-given-length sequence
286 declared-length))))))
288 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
289 (defun signal-index-too-large-error (sequence index)
290 (declare (optimize allow-non-returning-tail-call))
291 (let* ((length (length sequence))
292 (max-index (and (plusp length)
293 (1- length))))
294 (error 'index-too-large-error
295 :datum index
296 :expected-type (if max-index
297 `(integer 0 ,max-index)
298 ;; This seems silly, is there something better?
299 '(integer 0 (0))))))
301 (declaim (ftype (function (t t t) nil) sequence-bounding-indices-bad-error))
302 (defun sequence-bounding-indices-bad-error (sequence start end)
303 (declare (optimize allow-non-returning-tail-call))
304 (let ((size (length sequence)))
305 (error 'bounding-indices-bad-error
306 :datum (cons start end)
307 :expected-type `(cons (integer 0 ,size)
308 (integer ,start ,size))
309 :object sequence)))
311 (declaim (ftype (function (t t t) nil) array-bounding-indices-bad-error))
312 (defun array-bounding-indices-bad-error (array start end)
313 (declare (optimize allow-non-returning-tail-call))
314 (let ((size (array-total-size array)))
315 (error 'bounding-indices-bad-error
316 :datum (cons start end)
317 :expected-type `(cons (integer 0 ,size)
318 (integer ,start ,size))
319 :object array)))
321 (declaim (ftype (function (t) nil) circular-list-error))
322 (defun circular-list-error (list)
323 (declare (optimize allow-non-returning-tail-call))
324 (error 'simple-type-error
325 :format-control "List is circular:~% ~S"
326 :format-arguments (list list)
327 :datum list
328 :type '(and list (satisfies list-length))))
332 (defun emptyp (sequence)
333 #!+sb-doc
334 "Returns T if SEQUENCE is an empty sequence and NIL
335 otherwise. Signals an error if SEQUENCE is not a sequence."
336 (declare (explicit-check sequence))
337 (seq-dispatch-checking sequence
338 (null sequence)
339 (zerop (length sequence))
340 (sb!sequence:emptyp sequence)))
342 (defun elt (sequence index)
343 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
344 (declare (explicit-check sequence))
345 (seq-dispatch-checking sequence
346 (do ((count index (1- count))
347 (list sequence (cdr list)))
348 ((= count 0)
349 (if (endp list)
350 (signal-index-too-large-error sequence index)
351 (car list)))
352 (declare (type index count)))
353 (progn
354 (when (>= index (length sequence))
355 (signal-index-too-large-error sequence index))
356 (aref sequence index))
357 (sb!sequence:elt sequence index)))
359 (defun %setelt (sequence index newval)
360 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
361 (declare (explicit-check sequence))
362 (seq-dispatch-checking sequence
363 (do ((count index (1- count))
364 (seq sequence))
365 ((= count 0) (rplaca seq newval) newval)
366 (declare (fixnum count))
367 (if (atom (cdr seq))
368 (signal-index-too-large-error sequence index)
369 (setq seq (cdr seq))))
370 (progn
371 (when (>= index (length sequence))
372 (signal-index-too-large-error sequence index))
373 (setf (aref sequence index) newval))
374 (setf (sb!sequence:elt sequence index) newval)))
376 (defun length (sequence)
377 #!+sb-doc "Return an integer that is the length of SEQUENCE."
378 (declare (explicit-check))
379 (seq-dispatch-checking sequence
380 (length sequence)
381 (length sequence)
382 (sb!sequence:length sequence)))
384 (defun make-sequence (result-type length &key (initial-element nil iep))
385 #!+sb-doc
386 "Return a sequence of the given RESULT-TYPE and LENGTH, with
387 elements initialized to INITIAL-ELEMENT."
388 (declare (index length) (explicit-check))
389 (let* ((expanded-type (typexpand result-type))
390 (adjusted-type
391 (typecase expanded-type
392 (atom (cond
393 ((eq expanded-type 'string) '(vector character))
394 ((eq expanded-type 'simple-string)
395 '(simple-array character (*)))
396 (t expanded-type)))
397 (cons (cond
398 ((eq (car expanded-type) 'string)
399 `(vector character ,@(cdr expanded-type)))
400 ((eq (car expanded-type) 'simple-string)
401 `(simple-array character ,(if (cdr expanded-type)
402 (cdr expanded-type)
403 '(*))))
404 (t expanded-type)))))
405 (type (specifier-type adjusted-type))
406 (list-type (specifier-type 'list)))
407 (cond ((csubtypep type list-type)
408 (cond
409 ((type= type list-type)
410 (make-list length :initial-element initial-element))
411 ((eq type *empty-type*)
412 (bad-sequence-type-error nil))
413 ((type= type (specifier-type 'null))
414 (if (= length 0)
415 'nil
416 (sequence-type-length-mismatch-error type length)))
417 ((cons-type-p type)
418 (multiple-value-bind (min exactp)
419 (sb!kernel::cons-type-length-info type)
420 (if exactp
421 (unless (= length min)
422 (sequence-type-length-mismatch-error type length))
423 (unless (>= length min)
424 (sequence-type-length-mismatch-error type length)))
425 (make-list length :initial-element initial-element)))
426 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
427 ;; which may seem strange and non-ideal, but then I'd say
428 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
429 (t (sequence-type-too-hairy (type-specifier type)))))
430 ((csubtypep type (specifier-type 'vector))
431 (cond
432 (;; is it immediately obvious what the result type is?
433 (typep type 'array-type)
434 (aver (= (length (array-type-dimensions type)) 1))
435 (let* ((etype (type-specifier
436 (array-type-specialized-element-type type)))
437 (etype (if (eq etype '*) t etype))
438 (type-length (car (array-type-dimensions type))))
439 (unless (or (eq type-length '*)
440 (= type-length length))
441 (sequence-type-length-mismatch-error type length))
442 (if iep
443 (make-array length :element-type etype
444 :initial-element initial-element)
445 (make-array length :element-type etype))))
446 (t (sequence-type-too-hairy (type-specifier type)))))
447 ((when-extended-sequence-type
448 (expanded-type type :expandedp t :prototype prototype)
449 ;; This function has the EXPLICIT-CHECK declaration, so
450 ;; we manually assert that it returns a SEQUENCE.
451 (the extended-sequence
452 (if iep
453 (sb!sequence:make-sequence-like
454 prototype length :initial-element initial-element)
455 (sb!sequence:make-sequence-like
456 prototype length)))))
457 (t (bad-sequence-type-error (type-specifier type))))))
459 ;;;; SUBSEQ
460 ;;;;
462 (!define-array-dispatch vector-subseq-dispatch (array start end)
463 (declare (optimize speed (safety 0)))
464 (declare (type index start end))
465 (subseq array start end))
467 ;;;; The support routines for SUBSEQ are used by compiler transforms,
468 ;;;; so we worry about dealing with END being supplied or defaulting
469 ;;;; to NIL at this level.
471 (defun vector-subseq* (sequence start end)
472 (declare (type vector sequence))
473 (declare (type index start)
474 (type (or null index) end)
475 (optimize speed))
476 (with-array-data ((data sequence)
477 (start start)
478 (end end)
479 :check-fill-pointer t
480 :force-inline t)
481 (vector-subseq-dispatch data start end)))
483 (defun list-subseq* (sequence start end)
484 (declare (type list sequence)
485 (type unsigned-byte start)
486 (type (or null unsigned-byte) end))
487 (flet ((oops ()
488 (sequence-bounding-indices-bad-error sequence start end)))
489 (let ((pointer sequence))
490 (unless (zerop start)
491 ;; If START > 0 the list cannot be empty. So CDR down to
492 ;; it START-1 times, check that we still have something, then
493 ;; CDR the final time.
495 ;; If START was zero, the list may be empty if END is NIL or
496 ;; also zero.
497 (when (> start 1)
498 (setf pointer (nthcdr (1- start) pointer)))
499 (if pointer
500 (pop pointer)
501 (oops)))
502 (if end
503 (let ((n (- end start)))
504 (declare (integer n))
505 (when (minusp n)
506 (oops))
507 (when (plusp n)
508 (let* ((head (list nil))
509 (tail head))
510 (macrolet ((pop-one ()
511 `(let ((tmp (list (pop pointer))))
512 (setf (cdr tail) tmp
513 tail tmp))))
514 ;; Bignum case
515 (loop until (fixnump n)
516 do (pop-one)
517 (decf n))
518 ;; Fixnum case, but leave last element, so we should
519 ;; still have something left in the sequence.
520 (let ((m (1- n)))
521 (declare (fixnum m))
522 (loop repeat m
523 do (pop-one)))
524 (unless pointer
525 (oops))
526 ;; OK, pop the last one.
527 (pop-one)
528 (cdr head)))))
529 (loop while pointer
530 collect (pop pointer))))))
532 (defun subseq (sequence start &optional end)
533 #!+sb-doc
534 "Return a copy of a subsequence of SEQUENCE starting with element number
535 START and continuing to the end of SEQUENCE or the optional END."
536 (declare (explicit-check sequence :result))
537 (seq-dispatch-checking=>seq sequence
538 (list-subseq* sequence start end)
539 (vector-subseq* sequence start end)
540 (sb!sequence:subseq sequence start end)))
542 ;;;; COPY-SEQ
544 (defun copy-seq (sequence)
545 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
546 (declare (explicit-check sequence :result))
547 (seq-dispatch-checking sequence
548 (list-copy-seq* sequence)
549 (vector-subseq* sequence 0 nil)
550 ;; Copying an extended sequence has to return an extended-sequence
551 ;; and not just any SEQUENCE.
552 (the extended-sequence (values (sb!sequence:copy-seq sequence)))))
554 (defun list-copy-seq* (sequence)
555 (!copy-list-macro sequence :check-proper-list t))
557 ;;;; FILL
559 (defun list-fill* (sequence item start end)
560 (declare (type list sequence)
561 (type unsigned-byte start)
562 (type (or null unsigned-byte) end))
563 (flet ((oops ()
564 (sequence-bounding-indices-bad-error sequence start end)))
565 (let ((pointer sequence))
566 (unless (zerop start)
567 ;; If START > 0 the list cannot be empty. So CDR down to it
568 ;; START-1 times, check that we still have something, then CDR
569 ;; the final time.
571 ;; If START was zero, the list may be empty if END is NIL or
572 ;; also zero.
573 (unless (= start 1)
574 (setf pointer (nthcdr (1- start) pointer)))
575 (if pointer
576 (pop pointer)
577 (oops)))
578 (if end
579 (let ((n (- end start)))
580 (declare (integer n))
581 (when (minusp n)
582 (oops))
583 (when (plusp n)
584 (loop repeat n
585 do (setf pointer (cdr (rplaca pointer item))))))
586 (loop while pointer
587 do (setf pointer (cdr (rplaca pointer item)))))))
588 sequence)
590 (defglobal %%fill-bashers%% (make-array (1+ sb!vm:widetag-mask)))
591 #.`(progn
592 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
593 for et = (sb!vm:saetp-specifier saetp)
594 if (or (null et)
595 (sb!vm:valid-bit-bash-saetp-p saetp))
596 collect
597 (multiple-value-bind (basher value-transform)
598 (if et
599 (sb!c::find-basher saetp)
600 '(lambda (item vector start length)
601 (declare (ignore item start length))
602 (data-nil-vector-ref (truly-the (simple-array nil (*)) vector) 0)))
603 `(setf
604 (aref %%fill-bashers%% ,(sb!vm:saetp-typecode saetp))
605 (cons #',basher
606 ,(if et
607 `(lambda (sb!c::item)
608 (declare (type ,et sb!c::item))
609 ,value-transform)
610 '#'identity))))
611 else do
612 ;; vector-fill* depends on this assertion
613 (assert (member et '(t (complex double-float)
614 #!-64-bit (complex single-float))
615 :test #'equal))))
617 (defun vector-fill* (vector item start end)
618 (declare (type index start) (type (or index null) end)
619 (optimize speed))
620 (with-array-data ((vector vector)
621 (start start)
622 (end end)
623 :force-inline t
624 :check-fill-pointer t)
625 (if (simple-vector-p vector)
626 (locally
627 (declare (optimize (speed 3) (safety 0))) ; transform will kick in
628 (fill (truly-the simple-vector vector) item
629 :start start :end end))
630 (let* ((widetag (%other-pointer-widetag vector))
631 (bashers (svref %%fill-bashers%% widetag)))
632 (macrolet ((fill-complex (type)
633 `(locally
634 (declare (optimize (speed 3) (safety 0))
635 (type (complex ,type) item)
636 (type (simple-array (complex ,type) (*))
637 vector))
638 (do ((index start (1+ index)))
639 ((= index end))
640 (declare (index index))
641 (setf (aref vector index) item)))))
642 (cond ((neq bashers 0)
643 (funcall (truly-the function (car (truly-the cons bashers)))
644 (funcall (truly-the function (cdr bashers)) item)
645 vector start (- end start)))
646 #!-64-bit
647 ((eq widetag sb!vm:simple-array-single-float-widetag)
648 (fill-complex single-float))
650 (fill-complex double-float)))))))
651 vector)
653 (defun string-fill* (sequence item start end)
654 (declare (string sequence))
655 (with-array-data ((data sequence)
656 (start start)
657 (end end)
658 :force-inline t
659 :check-fill-pointer t)
660 ;; DEFTRANSFORM for FILL will turn these into
661 ;; calls to UB*-BASH-FILL.
662 (etypecase data
663 #!+sb-unicode
664 ((simple-array character (*))
665 (let ((item (locally (declare (optimize (safety 3)))
666 (the character item))))
667 (fill data item :start start :end end)))
668 ((simple-array base-char (*))
669 (let ((item (locally (declare (optimize (safety 3)))
670 (the base-char item))))
671 (fill data item :start start :end end))))))
673 (defun fill (sequence item &key (start 0) end)
674 #!+sb-doc
675 "Replace the specified elements of SEQUENCE with ITEM."
676 (declare (explicit-check sequence :result))
677 (seq-dispatch-checking=>seq sequence
678 (list-fill* sequence item start end)
679 (vector-fill* sequence item start end)
680 (sb!sequence:fill sequence item
681 :start start
682 :end (%check-generic-sequence-bounds sequence start end))))
684 ;;;; REPLACE
686 (eval-when (:compile-toplevel :execute)
688 ;;; If we are copying around in the same vector, be careful not to copy the
689 ;;; same elements over repeatedly. We do this by copying backwards.
690 (sb!xc:defmacro mumble-replace-from-mumble ()
691 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
692 (let ((nelts (min (- target-end target-start)
693 (- source-end source-start))))
694 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
695 (1- target-index))
696 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
697 (1- source-index)))
698 ((= target-index (the fixnum (1- target-start))) target-sequence)
699 (declare (fixnum target-index source-index))
700 ;; disable bounds checking
701 (declare (optimize (safety 0)))
702 (setf (aref target-sequence target-index)
703 (aref source-sequence source-index))))
704 (do ((target-index target-start (1+ target-index))
705 (source-index source-start (1+ source-index)))
706 ((or (= target-index (the fixnum target-end))
707 (= source-index (the fixnum source-end)))
708 target-sequence)
709 (declare (fixnum target-index source-index))
710 ;; disable bounds checking
711 (declare (optimize (safety 0)))
712 (setf (aref target-sequence target-index)
713 (aref source-sequence source-index)))))
715 (sb!xc:defmacro list-replace-from-list ()
716 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
717 (let ((new-elts (subseq source-sequence source-start
718 (+ (the fixnum source-start)
719 (the fixnum
720 (min (- (the fixnum target-end)
721 (the fixnum target-start))
722 (- (the fixnum source-end)
723 (the fixnum source-start))))))))
724 (do ((n new-elts (cdr n))
725 (o (nthcdr target-start target-sequence) (cdr o)))
726 ((null n) target-sequence)
727 (rplaca o (car n))))
728 (do ((target-index target-start (1+ target-index))
729 (source-index source-start (1+ source-index))
730 (target-sequence-ref (nthcdr target-start target-sequence)
731 (cdr target-sequence-ref))
732 (source-sequence-ref (nthcdr source-start source-sequence)
733 (cdr source-sequence-ref)))
734 ((or (= target-index (the fixnum target-end))
735 (= source-index (the fixnum source-end))
736 (null target-sequence-ref) (null source-sequence-ref))
737 target-sequence)
738 (declare (fixnum target-index source-index))
739 (rplaca target-sequence-ref (car source-sequence-ref)))))
741 (sb!xc:defmacro list-replace-from-mumble ()
742 `(do ((target-index target-start (1+ target-index))
743 (source-index source-start (1+ source-index))
744 (target-sequence-ref (nthcdr target-start target-sequence)
745 (cdr target-sequence-ref)))
746 ((or (= target-index (the fixnum target-end))
747 (= source-index (the fixnum source-end))
748 (null target-sequence-ref))
749 target-sequence)
750 (declare (fixnum source-index target-index))
751 (rplaca target-sequence-ref (aref source-sequence source-index))))
753 (sb!xc:defmacro mumble-replace-from-list ()
754 `(do ((target-index target-start (1+ target-index))
755 (source-index source-start (1+ source-index))
756 (source-sequence (nthcdr source-start source-sequence)
757 (cdr source-sequence)))
758 ((or (= target-index (the fixnum target-end))
759 (= source-index (the fixnum source-end))
760 (null source-sequence))
761 target-sequence)
762 (declare (fixnum target-index source-index))
763 (setf (aref target-sequence target-index) (car source-sequence))))
765 ) ; EVAL-WHEN
767 ;;;; The support routines for REPLACE are used by compiler transforms, so we
768 ;;;; worry about dealing with END being supplied or defaulting to NIL
769 ;;;; at this level.
771 (defun list-replace-from-list* (target-sequence source-sequence target-start
772 target-end source-start source-end)
773 (when (null target-end) (setq target-end (length target-sequence)))
774 (when (null source-end) (setq source-end (length source-sequence)))
775 (list-replace-from-list))
777 (defun list-replace-from-vector* (target-sequence source-sequence target-start
778 target-end source-start source-end)
779 (when (null target-end) (setq target-end (length target-sequence)))
780 (when (null source-end) (setq source-end (length source-sequence)))
781 (list-replace-from-mumble))
783 (defun vector-replace-from-list* (target-sequence source-sequence target-start
784 target-end source-start source-end)
785 (when (null target-end) (setq target-end (length target-sequence)))
786 (when (null source-end) (setq source-end (length source-sequence)))
787 (mumble-replace-from-list))
789 (defun vector-replace-from-vector* (target-sequence source-sequence
790 target-start target-end source-start
791 source-end)
792 (when (null target-end) (setq target-end (length target-sequence)))
793 (when (null source-end) (setq source-end (length source-sequence)))
794 (mumble-replace-from-mumble))
796 #!+sb-unicode
797 (defun simple-character-string-replace-from-simple-character-string*
798 (target-sequence source-sequence
799 target-start target-end source-start source-end)
800 (declare (type (simple-array character (*)) target-sequence source-sequence))
801 (when (null target-end) (setq target-end (length target-sequence)))
802 (when (null source-end) (setq source-end (length source-sequence)))
803 (mumble-replace-from-mumble))
805 (define-sequence-traverser replace
806 (sequence1 sequence2 &rest args &key start1 end1 start2 end2)
807 #!+sb-doc
808 "Destructively modifies SEQUENCE1 by copying successive elements
809 into it from the SEQUENCE2.
811 Elements are copied to the subseqeuence bounded by START1 and END1,
812 from the subsequence bounded by START2 and END2. If these subsequences
813 are not of the same length, then the shorter length determines how
814 many elements are copied."
815 (declare (truly-dynamic-extent args))
816 (declare (explicit-check sequence1 sequence2 :result))
817 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
818 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
819 ;; these things here so that legacy code gets the names it's
820 ;; expecting. We could use &AUX instead :-/.
821 (target-sequence sequence1)
822 (source-sequence sequence2)
823 (target-start start1)
824 (source-start start2)
825 (target-end (or end1 length1))
826 (source-end (or end2 length2)))
827 (seq-dispatch-checking target-sequence
828 (seq-dispatch-checking source-sequence
829 (return-from replace (list-replace-from-list))
830 (return-from replace (list-replace-from-mumble))
831 nil)
832 (seq-dispatch-checking source-sequence
833 (return-from replace (mumble-replace-from-list))
834 (return-from replace (mumble-replace-from-mumble))
835 nil)
837 ;; If sequence1 is an extended-sequence, we know nothing about sequence2.
838 ;; If sequence1 was a list or vector, then sequence2 is an extended-sequence
839 ;; or not a sequence. Either way, check it.
840 (the sequence
841 (values (apply #'sb!sequence:replace sequence1
842 (the sequence sequence2) args)))))
844 ;;;; REVERSE
845 (defun reverse (sequence)
846 #!+sb-doc
847 "Return a new sequence containing the same elements but in reverse order."
848 (declare (explicit-check))
849 (seq-dispatch-checking sequence
850 (list-reverse sequence)
851 (vector-reverse sequence)
852 ;; The type deriver says that LIST => LIST and VECTOR => VECTOR
853 ;; but does not claim to know anything about extended-sequences.
854 ;; So this could theoretically return any subtype of SEQUENCE
855 ;; given an EXTENDED-SEQUENCE as input. But fndb says this returns
856 ;; a CONSED-SEQUENCE, which precludes non-simple vectors.
857 ;; But a CLOS sequence can apparently decide to return a LIST when
858 ;; reversed. [Is that too weird? Make this EXTENDED-SEQUENCE maybe?]
859 (the consed-sequence (values (sb!sequence:reverse sequence)))))
861 (defun list-reverse (list)
862 (do ((new-list ()))
863 ((endp list) new-list)
864 (push (pop list) new-list)))
866 (defmacro word-specialized-vector-tag-p (tag)
867 `(or
868 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
869 when (and (eq (sb!vm:saetp-n-bits saetp) sb!vm:n-word-bits)
870 (not (eq (sb!vm:saetp-specifier saetp) t)))
871 collect `(eq ,tag ,(sb!vm:saetp-typecode saetp)))))
873 (defun reverse-word-specialized-vector (from to end)
874 (declare (vector from))
875 (do ((length (length to))
876 (left-index 0 (1+ left-index))
877 (right-index end))
878 ((= left-index length))
879 (declare (type index left-index right-index))
880 (decf right-index)
881 (setf (%vector-raw-bits to left-index)
882 (%vector-raw-bits from right-index)))
885 (defun vector-reverse (vector)
886 (declare (vector vector))
887 (let ((length (length vector)))
888 (with-array-data ((vector vector) (start) (end)
889 :check-fill-pointer t)
890 (declare (ignore start))
891 (let* ((tag (%other-pointer-widetag vector))
892 (new-vector (allocate-vector-with-widetag tag length)))
893 (cond ((= tag sb!vm:simple-vector-widetag)
894 (do ((left-index 0 (1+ left-index))
895 (right-index end))
896 ((= left-index length))
897 (declare (type index left-index right-index))
898 (decf right-index)
899 (setf (svref new-vector left-index)
900 (svref vector right-index))))
901 ((word-specialized-vector-tag-p tag)
902 (reverse-word-specialized-vector vector new-vector end))
904 (let ((getter (the function (svref %%data-vector-reffers%% tag)))
905 (setter (the function (svref %%data-vector-setters%% tag))))
906 (declare (fixnum length))
907 (do ((forward-index 0 (1+ forward-index))
908 (backward-index (1- end) (1- backward-index)))
909 ((= forward-index length))
910 (declare (fixnum forward-index backward-index))
911 (funcall setter new-vector forward-index
912 (funcall getter vector backward-index))))))
913 new-vector))))
915 ;;;; NREVERSE
917 (defun list-nreverse (list)
918 (do ((1st (cdr list) (if (endp 1st) 1st (cdr 1st)))
919 (2nd list 1st)
920 (3rd '() 2nd))
921 ((atom 2nd) 3rd)
922 (rplacd 2nd 3rd)))
924 (defun nreverse-word-specialized-vector (vector start end)
925 (do ((left-index start (1+ left-index))
926 (right-index (1- end) (1- right-index)))
927 ((<= right-index left-index))
928 (declare (type index left-index right-index))
929 (let ((left (%vector-raw-bits vector left-index))
930 (right (%vector-raw-bits vector right-index)))
931 (setf (%vector-raw-bits vector left-index) right
932 (%vector-raw-bits vector right-index) left)))
933 vector)
935 (defun vector-nreverse (vector)
936 (declare (vector vector))
937 (when (> (length vector) 1)
938 (with-array-data ((vector vector) (start) (end)
939 :check-fill-pointer t)
940 (let ((tag (%other-pointer-widetag vector)))
941 (cond ((= tag sb!vm:simple-vector-widetag)
942 (do ((left-index start (1+ left-index))
943 (right-index (1- end) (1- right-index)))
944 ((<= right-index left-index))
945 (declare (type index left-index right-index))
946 (let ((left (svref vector left-index))
947 (right (svref vector right-index)))
948 (setf (svref vector left-index) right
949 (svref vector right-index) left))))
950 ((word-specialized-vector-tag-p tag)
951 (nreverse-word-specialized-vector vector start end))
953 (let* ((getter (the function (svref %%data-vector-reffers%% tag)))
954 (setter (the function (svref %%data-vector-setters%% tag))))
955 (do ((left-index start (1+ left-index))
956 (right-index (1- end) (1- right-index)))
957 ((<= right-index left-index))
958 (declare (type index left-index right-index))
959 (let ((left (funcall getter vector left-index))
960 (right (funcall getter vector right-index)))
961 (funcall setter vector left-index right)
962 (funcall setter vector right-index left)))))))))
963 vector)
965 (defun nreverse (sequence)
966 #!+sb-doc
967 "Return a sequence of the same elements in reverse order; the argument
968 is destroyed."
969 (declare (explicit-check))
970 (seq-dispatch-checking sequence
971 (list-nreverse sequence)
972 (vector-nreverse sequence)
973 ;; The type deriver for this is 'result-type-first-arg',
974 ;; meaning it should return definitely an EXTENDED-SEQUENCE
975 ;; and not a list or vector.
976 (the extended-sequence (values (sb!sequence:nreverse sequence)))))
979 (defmacro sb!sequence:dosequence ((element sequence &optional return) &body body)
980 #!+sb-doc
981 "Executes BODY with ELEMENT subsequently bound to each element of
982 SEQUENCE, then returns RETURN."
983 (multiple-value-bind (forms decls) (parse-body body nil)
984 (once-only ((sequence sequence))
985 (with-unique-names (state limit from-end step endp elt)
986 `(block nil
987 (seq-dispatch ,sequence
988 (dolist (,element ,sequence ,return) ,@body)
989 (do-vector-data (,element ,sequence ,return) ,@body)
990 (multiple-value-bind (,state ,limit ,from-end ,step ,endp ,elt)
991 (sb!sequence:make-sequence-iterator ,sequence)
992 (declare (function ,step ,endp ,elt))
993 (do ((,state ,state (funcall ,step ,sequence ,state ,from-end)))
994 ((funcall ,endp ,sequence ,state ,limit ,from-end)
995 (let ((,element nil))
996 ,@(filter-dolist-declarations decls)
997 (declare (ignorable ,element))
998 ,return))
999 (let ((,element (funcall ,elt ,sequence ,state)))
1000 ,@decls
1001 (tagbody
1002 ,@forms))))))))))
1005 ;;;; CONCATENATE
1007 (defun concatenate (result-type &rest sequences)
1008 #!+sb-doc
1009 "Return a new sequence of all the argument sequences concatenated together
1010 which shares no structure with the original argument sequences of the
1011 specified RESULT-TYPE."
1012 (declare (explicit-check)
1013 (dynamic-extent sequences))
1014 (flet ((concat-to-simple* (type-spec sequences)
1015 (do ((seqs sequences (cdr seqs))
1016 (total-length 0)
1017 (lengths ()))
1018 ((null seqs)
1019 (do ((sequences sequences (cdr sequences))
1020 (lengths lengths (cdr lengths))
1021 (index 0)
1022 (result (make-sequence type-spec total-length)))
1023 ((= index total-length) result)
1024 (declare (fixnum index))
1025 (let ((sequence (car sequences)))
1026 (sb!sequence:dosequence (e sequence)
1027 (setf (aref result index) e)
1028 (incf index)))))
1029 (let ((length (length (car seqs))))
1030 (declare (fixnum length))
1031 (setq lengths (nconc lengths (list length)))
1032 (setq total-length (+ total-length length))))))
1033 (case result-type
1034 ;; Pick up some common cases first
1035 (list
1036 (apply #'%concatenate-to-list sequences))
1037 ((vector simple-vector)
1038 (apply #'%concatenate-to-simple-vector sequences))
1039 #!+sb-unicode
1040 ((string simple-string)
1041 (apply #'%concatenate-to-string sequences))
1042 ((simple-base-string #!-sb-unicode string #!-sb-unicode simple-string)
1043 (apply #'%concatenate-to-base-string sequences))
1045 (let ((type (specifier-type result-type)))
1046 (cond
1047 ((csubtypep type (specifier-type 'list))
1048 (cond
1049 ((type= type (specifier-type 'list))
1050 (apply #'%concatenate-to-list sequences))
1051 ((eq type *empty-type*)
1052 (bad-sequence-type-error nil))
1053 ((type= type (specifier-type 'null))
1054 (unless (every #'emptyp sequences)
1055 (sequence-type-length-mismatch-error
1056 type (reduce #'+ sequences :key #'length))) ; FIXME: circular list issues.
1057 '())
1058 ((cons-type-p type)
1059 (multiple-value-bind (min exactp)
1060 (sb!kernel::cons-type-length-info type)
1061 (let ((length (reduce #'+ sequences :key #'length)))
1062 (if exactp
1063 (unless (= length min)
1064 (sequence-type-length-mismatch-error type length))
1065 (unless (>= length min)
1066 (sequence-type-length-mismatch-error type length)))
1067 (apply #'%concatenate-to-list sequences))))
1068 (t (sequence-type-too-hairy (type-specifier type)))))
1069 ((csubtypep type (specifier-type 'vector))
1070 (concat-to-simple* result-type sequences))
1071 ((when-extended-sequence-type
1072 (result-type type :expandedp nil :prototype prototype)
1073 ;; This function has the EXPLICIT-CHECK declaration,
1074 ;; so we manually assert that it returns a SEQUENCE.
1075 (the extended-sequence
1076 (apply #'sb!sequence:concatenate prototype sequences))))
1078 (bad-sequence-type-error result-type))))))))
1080 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
1081 ;;; CONCATENATE 'STRING &co into these.
1082 (macrolet ((def (name element-type &rest dispatch)
1083 `(defun ,name (&rest sequences)
1084 (declare (explicit-check)
1085 (optimize (sb!c::insert-array-bounds-checks 0)))
1086 (let ((length 0))
1087 (declare (index length))
1088 (do-rest-arg ((seq) sequences)
1089 (incf length (length seq)))
1090 (let ((result (make-array length :element-type ',element-type))
1091 (start 0))
1092 (declare (index start))
1093 (do-rest-arg ((seq) sequences)
1094 (string-dispatch (,@dispatch t)
1096 (let ((length (length seq)))
1097 (replace result seq :start1 start)
1098 (incf start length))))
1099 result)))))
1100 #!+sb-unicode
1101 (def %concatenate-to-string character
1102 (simple-array character (*)) (simple-array base-char (*)))
1103 (def %concatenate-to-base-string base-char
1104 (simple-array base-char (*)) #!+sb-unicode (simple-array character (*)))
1105 (def %concatenate-to-simple-vector t simple-vector))
1107 (defun %concatenate-to-list (&rest sequences)
1108 (declare (explicit-check))
1109 (let* ((result (list nil))
1110 (splice result))
1111 (do-rest-arg ((sequence) sequences)
1112 (sb!sequence:dosequence (e sequence)
1113 (setf splice (cdr (rplacd splice (list e))))))
1114 (cdr result)))
1116 (defun %concatenate-to-vector (widetag &rest sequences)
1117 (declare (explicit-check))
1118 (let ((length 0))
1119 (declare (index length))
1120 (do-rest-arg ((seq) sequences)
1121 (incf length (length seq)))
1122 (let ((result (allocate-vector-with-widetag widetag length))
1123 (setter (the function (svref %%data-vector-setters%% widetag)))
1124 (index 0))
1125 (declare (index index))
1126 (do-rest-arg ((seq) sequences)
1127 (sb!sequence:dosequence (e seq)
1128 (funcall setter result index e)
1129 (incf index)))
1130 result)))
1132 ;;;; MAP
1134 ;;; helper functions to handle arity-1 subcases of MAP
1135 (defun %map-to-list-arity-1 (fun sequence)
1136 (declare (explicit-check))
1137 (let ((reversed-result nil)
1138 (really-fun (%coerce-callable-to-fun fun)))
1139 (sb!sequence:dosequence (element sequence)
1140 (push (funcall really-fun element)
1141 reversed-result))
1142 (nreverse reversed-result)))
1143 (defun %map-to-simple-vector-arity-1 (fun sequence)
1144 (declare (explicit-check))
1145 (let ((result (make-array (length sequence)))
1146 (index 0)
1147 (really-fun (%coerce-callable-to-fun fun)))
1148 (declare (type index index))
1149 (sb!sequence:dosequence (element sequence)
1150 (setf (aref result index)
1151 (funcall really-fun element))
1152 (incf index))
1153 result))
1154 (defun %map-for-effect-arity-1 (fun sequence)
1155 (declare (explicit-check))
1156 (let ((really-fun (%coerce-callable-to-fun fun)))
1157 (sb!sequence:dosequence (element sequence)
1158 (funcall really-fun element)))
1159 nil)
1161 (declaim (maybe-inline %map-for-effect))
1162 (defun %map-for-effect (fun sequences)
1163 (declare (type function fun) (type list sequences))
1164 (let ((%sequences sequences)
1165 (%iters (mapcar (lambda (s)
1166 (seq-dispatch s
1169 (multiple-value-list
1170 (sb!sequence:make-sequence-iterator s))))
1171 sequences))
1172 (%apply-args (make-list (length sequences))))
1173 ;; this is almost efficient (except in the general case where we
1174 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
1175 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
1176 ;; TODO: on x86-64, we do have. Now see if the above remark is true.
1177 (declare (type list %sequences %iters %apply-args))
1178 (loop
1179 (do ((in-sequences %sequences (cdr in-sequences))
1180 (in-iters %iters (cdr in-iters))
1181 (in-apply-args %apply-args (cdr in-apply-args)))
1182 ((null in-sequences) (apply fun %apply-args))
1183 (let ((i (car in-iters)))
1184 (declare (type (or list index) i))
1185 (cond
1186 ((listp (car in-sequences))
1187 (if (null i)
1188 (return-from %map-for-effect nil)
1189 (setf (car in-apply-args) (car i)
1190 (car in-iters) (cdr i))))
1191 ((typep i 'index)
1192 (let ((v (the vector (car in-sequences))))
1193 (if (>= i (length v))
1194 (return-from %map-for-effect nil)
1195 (setf (car in-apply-args) (aref v i)
1196 (car in-iters) (1+ i)))))
1198 ;; While on one hand this could benefit from a zero-safety ds-bind,
1199 ;; on the other, why not coerce these tuples to vectors or structs?
1200 (destructuring-bind (state limit from-end step endp elt &rest ignore)
1202 (declare (type function step endp elt)
1203 (ignore ignore))
1204 (let ((s (car in-sequences)))
1205 (if (funcall endp s state limit from-end)
1206 (return-from %map-for-effect nil)
1207 (progn
1208 (setf (car in-apply-args) (funcall elt s state))
1209 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
1210 (defun %map-to-list (fun sequences)
1211 (declare (type function fun)
1212 (type list sequences))
1213 (let ((result nil))
1214 (flet ((f (&rest args)
1215 (declare (truly-dynamic-extent args))
1216 (push (apply fun args) result)))
1217 (declare (truly-dynamic-extent #'f))
1218 (%map-for-effect #'f sequences))
1219 (nreverse result)))
1220 (defun %map-to-vector (output-type-spec fun sequences)
1221 (declare (type function fun)
1222 (type list sequences))
1223 (let ((min-len 0))
1224 (flet ((f (&rest args)
1225 (declare (truly-dynamic-extent args))
1226 (declare (ignore args))
1227 (incf min-len)))
1228 (declare (truly-dynamic-extent #'f))
1229 (%map-for-effect #'f sequences))
1230 (let ((result (make-sequence output-type-spec min-len))
1231 (i 0))
1232 (declare (type (simple-array * (*)) result))
1233 (flet ((f (&rest args)
1234 (declare (truly-dynamic-extent args))
1235 (setf (aref result i) (apply fun args))
1236 (incf i)))
1237 (declare (truly-dynamic-extent #'f))
1238 (%map-for-effect #'f sequences))
1239 result)))
1241 ;;; %MAP is just MAP without the final just-to-be-sure check that
1242 ;;; length of the output sequence matches any length specified
1243 ;;; in RESULT-TYPE.
1244 (defun %map (result-type function &rest sequences)
1245 (declare (explicit-check))
1246 (declare (dynamic-extent sequences))
1247 ;; Everything that we end up calling uses %COERCE-TO-CALLABLE
1248 ;; on FUNCTION so we don't need to declare it of type CALLABLE here.
1249 ;; Additionally all the arity-1 mappers use SEQ-DISPATCH which asserts
1250 ;; that the input is a SEQUENCE. Despite SEQ-DISPATCH being "less safe"
1251 ;; than SEQ-DISPATCH-CHECKING, both are in fact equally safe, because
1252 ;; the ARRAY case (which assumes that all arrays are vectors) utilizes
1253 ;; %WITH-ARRAY-DATA/FP which asserts that its input is a vector.
1254 (labels ((slower-map (type)
1255 (let ((really-fun (%coerce-callable-to-fun function)))
1256 (cond
1257 ((eq type *empty-type*)
1258 (%map-for-effect really-fun sequences))
1259 ((csubtypep type (specifier-type 'list))
1260 (%map-to-list really-fun sequences))
1261 ((csubtypep type (specifier-type 'vector))
1262 (%map-to-vector result-type really-fun sequences))
1263 ((when-extended-sequence-type
1264 (result-type type :expandedp nil :prototype prototype)
1265 ;; This function has the EXPLICIT-CHECK
1266 ;; declaration, so we manually assert that it
1267 ;; returns a SEQUENCE.
1268 (the extended-sequence
1269 (apply #'sb!sequence:map
1270 prototype really-fun sequences))))
1272 (bad-sequence-type-error result-type))))))
1273 ;; Handle some easy cases faster
1274 (if (/= (length sequences) 1)
1275 (slower-map (specifier-type result-type))
1276 (let ((first-sequence (fast-&rest-nth 0 sequences)))
1277 (case result-type
1278 ((nil)
1279 (%map-for-effect-arity-1 function first-sequence))
1280 ((list cons)
1281 (%map-to-list-arity-1 function first-sequence))
1282 ((vector simple-vector)
1283 (%map-to-simple-vector-arity-1 function first-sequence))
1285 (let ((type (specifier-type result-type)))
1286 (cond ((eq type *empty-type*)
1287 (%map-for-effect-arity-1 function first-sequence))
1288 ((csubtypep type (specifier-type 'list))
1289 (%map-to-list-arity-1 function first-sequence))
1290 ((csubtypep type (specifier-type '(vector t)))
1291 (%map-to-simple-vector-arity-1 function first-sequence))
1293 (slower-map type))))))))))
1295 (defun map (result-type function first-sequence &rest more-sequences)
1296 (declare (explicit-check))
1297 (let ((result
1298 (apply #'%map result-type function first-sequence more-sequences)))
1299 (if (or (eq result-type 'nil) (typep result result-type))
1300 result
1301 (error 'simple-type-error
1302 :format-control "MAP result ~S is not a sequence of type ~S"
1303 :datum result
1304 :expected-type result-type
1305 :format-arguments (list result result-type)))))
1307 ;;;; MAP-INTO
1309 (defmacro map-into-lambda (sequences params &body body)
1310 (check-type sequences symbol)
1311 `(flet ((f ,params ,@body))
1312 (declare (truly-dynamic-extent #'f))
1313 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1314 ;; hence the awkward flip between MAP and LOOP.
1315 (if ,sequences
1316 (apply #'%map nil #'f ,sequences)
1317 (loop (f)))))
1319 (!define-array-dispatch vector-map-into (data start end fun sequences)
1320 (declare (type index start end)
1321 (type function fun)
1322 (type list sequences))
1323 (declare (explicit-check))
1324 (let ((index start))
1325 (declare (type index index))
1326 (block mapping
1327 (map-into-lambda sequences (&rest args)
1328 (declare (truly-dynamic-extent args))
1329 (when (eql index end)
1330 (return-from mapping))
1331 (setf (aref data index) (apply fun args))
1332 (incf index)))
1333 index))
1335 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1336 ;;; computing the length of the result sequence since we can detect
1337 ;;; the end during mapping (if MAP even gets that far).
1339 ;;; For each result type, define a mapping function which is
1340 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1341 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1342 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1344 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1345 ;;; Because we are manually doing bounds checking with known types,
1346 ;;; safety is turned off for vectors and lists but kept for generic
1347 ;;; sequences.
1348 (defun map-into (result-sequence function &rest sequences)
1349 (declare (optimize (sb!c::check-tag-existence 0)))
1350 (let ((really-fun (%coerce-callable-to-fun function)))
1351 (etypecase result-sequence
1352 (vector
1353 (with-array-data ((data result-sequence) (start) (end)
1354 ;; MAP-INTO ignores fill pointer when mapping
1355 :check-fill-pointer nil)
1356 (let ((new-end (vector-map-into data start end really-fun sequences)))
1357 (when (array-has-fill-pointer-p result-sequence)
1358 (setf (fill-pointer result-sequence) (- new-end start))))))
1359 (list
1360 (let ((node result-sequence))
1361 (declare (type list node))
1362 (map-into-lambda sequences (&rest args)
1363 (declare (truly-dynamic-extent args))
1364 (cond ((null node)
1365 (return-from map-into result-sequence))
1366 ((not (listp (cdr node)))
1367 (error 'simple-type-error
1368 :format-control "~a is not a proper list"
1369 :format-arguments (list result-sequence)
1370 :expected-type 'list
1371 :datum result-sequence)))
1372 (setf (car node) (apply really-fun args))
1373 (setf node (cdr node)))))
1374 (sequence
1375 (multiple-value-bind (iter limit from-end)
1376 (sb!sequence:make-sequence-iterator result-sequence)
1377 (map-into-lambda sequences (&rest args)
1378 (declare (truly-dynamic-extent args) (optimize speed))
1379 (when (sb!sequence:iterator-endp result-sequence
1380 iter limit from-end)
1381 (return-from map-into result-sequence))
1382 (setf (sb!sequence:iterator-element result-sequence iter)
1383 (apply really-fun args))
1384 (setf iter (sb!sequence:iterator-step result-sequence
1385 iter from-end)))))))
1386 result-sequence)
1388 ;;;; REDUCE
1390 (eval-when (:compile-toplevel :execute)
1392 (sb!xc:defmacro mumble-reduce (function
1393 sequence
1395 start
1397 initial-value
1398 ref)
1399 `(do ((index ,start (1+ index))
1400 (value ,initial-value))
1401 ((>= index ,end) value)
1402 (setq value (funcall ,function value
1403 (apply-key ,key (,ref ,sequence index))))))
1405 (sb!xc:defmacro mumble-reduce-from-end (function
1406 sequence
1408 start
1410 initial-value
1411 ref)
1412 `(do ((index (1- ,end) (1- index))
1413 (value ,initial-value)
1414 (terminus (1- ,start)))
1415 ((<= index terminus) value)
1416 (setq value (funcall ,function
1417 (apply-key ,key (,ref ,sequence index))
1418 value))))
1420 (sb!xc:defmacro list-reduce (function
1421 sequence
1423 start
1425 initial-value
1426 ivp)
1427 `(let ((sequence (nthcdr ,start ,sequence)))
1428 (do ((count (if ,ivp ,start (1+ ,start))
1429 (1+ count))
1430 (sequence (if ,ivp sequence (cdr sequence))
1431 (cdr sequence))
1432 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1433 (funcall ,function value (apply-key ,key (car sequence)))))
1434 ((>= count ,end) value))))
1436 (sb!xc:defmacro list-reduce-from-end (function
1437 sequence
1439 start
1441 initial-value
1442 ivp)
1443 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1444 (reverse ,sequence))))
1445 (do ((count (if ,ivp ,start (1+ ,start))
1446 (1+ count))
1447 (sequence (if ,ivp sequence (cdr sequence))
1448 (cdr sequence))
1449 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1450 (funcall ,function (apply-key ,key (car sequence)) value)))
1451 ((>= count ,end) value))))
1453 ) ; EVAL-WHEN
1455 (define-sequence-traverser reduce (function sequence &rest args &key key
1456 from-end start end (initial-value nil ivp))
1457 (declare (type index start)
1458 (truly-dynamic-extent args))
1459 (declare (explicit-check sequence))
1460 (seq-dispatch-checking sequence
1461 (let ((end (or end length)))
1462 (declare (type index end))
1463 (if (= end start)
1464 (if ivp initial-value (funcall function))
1465 (if from-end
1466 (list-reduce-from-end function sequence key start end
1467 initial-value ivp)
1468 (list-reduce function sequence key start end
1469 initial-value ivp))))
1470 (let ((end (or end length)))
1471 (declare (type index end))
1472 (if (= end start)
1473 (if ivp initial-value (funcall function))
1474 (if from-end
1475 (progn
1476 (when (not ivp)
1477 (setq end (1- (the fixnum end)))
1478 (setq initial-value (apply-key key (aref sequence end))))
1479 (mumble-reduce-from-end function sequence key start end
1480 initial-value aref))
1481 (progn
1482 (when (not ivp)
1483 (setq initial-value (apply-key key (aref sequence start)))
1484 (setq start (1+ start)))
1485 (mumble-reduce function sequence key start end
1486 initial-value aref)))))
1487 (apply #'sb!sequence:reduce function sequence args)))
1489 ;;;; DELETE
1491 (eval-when (:compile-toplevel :execute)
1493 (sb!xc:defmacro mumble-delete (pred)
1494 `(do ((index start (1+ index))
1495 (jndex start)
1496 (number-zapped 0))
1497 ((or (= index (the fixnum end)) (= number-zapped count))
1498 (do ((index index (1+ index)) ; Copy the rest of the vector.
1499 (jndex jndex (1+ jndex)))
1500 ((= index (the fixnum length))
1501 (shrink-vector sequence jndex))
1502 (declare (fixnum index jndex))
1503 (setf (aref sequence jndex) (aref sequence index))))
1504 (declare (fixnum index jndex number-zapped))
1505 (setf (aref sequence jndex) (aref sequence index))
1506 (if ,pred
1507 (incf number-zapped)
1508 (incf jndex))))
1510 (sb!xc:defmacro mumble-delete-from-end (pred)
1511 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1512 (number-zapped 0)
1513 (losers ())
1514 this-element
1515 (terminus (1- start)))
1516 ((or (= index terminus) (= number-zapped count))
1517 (do ((losers losers) ; Delete the losers.
1518 (index start (1+ index))
1519 (jndex start))
1520 ((or (null losers) (= index (the fixnum end)))
1521 (do ((index index (1+ index)) ; Copy the rest of the vector.
1522 (jndex jndex (1+ jndex)))
1523 ((= index (the fixnum length))
1524 (shrink-vector sequence jndex))
1525 (declare (fixnum index jndex))
1526 (setf (aref sequence jndex) (aref sequence index))))
1527 (declare (fixnum index jndex))
1528 (setf (aref sequence jndex) (aref sequence index))
1529 (if (= index (the fixnum (car losers)))
1530 (pop losers)
1531 (incf jndex))))
1532 (declare (fixnum index number-zapped terminus))
1533 (setq this-element (aref sequence index))
1534 (when ,pred
1535 (incf number-zapped)
1536 (push index losers))))
1538 (sb!xc:defmacro normal-mumble-delete ()
1539 `(mumble-delete
1540 (if test-not
1541 (not (funcall test-not item (apply-key key (aref sequence index))))
1542 (funcall test item (apply-key key (aref sequence index))))))
1544 (sb!xc:defmacro normal-mumble-delete-from-end ()
1545 `(mumble-delete-from-end
1546 (if test-not
1547 (not (funcall test-not item (apply-key key this-element)))
1548 (funcall test item (apply-key key this-element)))))
1550 (sb!xc:defmacro list-delete (pred)
1551 `(let ((handle (cons nil sequence)))
1552 (declare (truly-dynamic-extent handle))
1553 (do* ((previous (nthcdr start handle))
1554 (current (cdr previous) (cdr current))
1555 (index start (1+ index))
1556 (number-zapped 0))
1557 ((or (= index end) (= number-zapped count))
1558 (cdr handle))
1559 (declare (index index number-zapped))
1560 (cond (,pred
1561 (rplacd previous (cdr current))
1562 (incf number-zapped))
1564 (pop previous))))))
1566 (sb!xc:defmacro list-delete-from-end (pred)
1567 `(let* ((reverse (nreverse sequence))
1568 (handle (cons nil reverse)))
1569 (declare (truly-dynamic-extent handle))
1570 (do* ((previous (nthcdr (- length end) handle))
1571 (current (cdr previous) (cdr current))
1572 (index start (1+ index))
1573 (number-zapped 0))
1574 ((or (= index end) (= number-zapped count))
1575 (nreverse (cdr handle)))
1576 (declare (index index number-zapped))
1577 (cond (,pred
1578 (rplacd previous (cdr current))
1579 (incf number-zapped))
1581 (pop previous))))))
1583 (sb!xc:defmacro normal-list-delete ()
1584 '(list-delete
1585 (if test-not
1586 (not (funcall test-not item (apply-key key (car current))))
1587 (funcall test item (apply-key key (car current))))))
1589 (sb!xc:defmacro normal-list-delete-from-end ()
1590 '(list-delete-from-end
1591 (if test-not
1592 (not (funcall test-not item (apply-key key (car current))))
1593 (funcall test item (apply-key key (car current))))))
1595 ) ; EVAL-WHEN
1597 (define-sequence-traverser delete
1598 (item sequence &rest args &key from-end test test-not start
1599 end count key)
1600 #!+sb-doc
1601 "Return a sequence formed by destructively removing the specified ITEM from
1602 the given SEQUENCE."
1603 (declare (type fixnum start)
1604 (truly-dynamic-extent args))
1605 (declare (explicit-check sequence :result))
1606 (seq-dispatch-checking=>seq sequence
1607 (let ((end (or end length)))
1608 (declare (type index end))
1609 (if from-end
1610 (normal-list-delete-from-end)
1611 (normal-list-delete)))
1612 (let ((end (or end length)))
1613 (declare (type index end))
1614 (if from-end
1615 (normal-mumble-delete-from-end)
1616 (normal-mumble-delete)))
1617 (apply #'sb!sequence:delete item sequence args)))
1619 (eval-when (:compile-toplevel :execute)
1621 (sb!xc:defmacro if-mumble-delete ()
1622 `(mumble-delete
1623 (funcall predicate (apply-key key (aref sequence index)))))
1625 (sb!xc:defmacro if-mumble-delete-from-end ()
1626 `(mumble-delete-from-end
1627 (funcall predicate (apply-key key this-element))))
1629 (sb!xc:defmacro if-list-delete ()
1630 '(list-delete
1631 (funcall predicate (apply-key key (car current)))))
1633 (sb!xc:defmacro if-list-delete-from-end ()
1634 '(list-delete-from-end
1635 (funcall predicate (apply-key key (car current)))))
1637 ) ; EVAL-WHEN
1639 (define-sequence-traverser delete-if
1640 (predicate sequence &rest args &key from-end start key end count)
1641 #!+sb-doc
1642 "Return a sequence formed by destructively removing the elements satisfying
1643 the specified PREDICATE from the given SEQUENCE."
1644 (declare (type fixnum start)
1645 (truly-dynamic-extent args))
1646 (declare (explicit-check sequence :result))
1647 (seq-dispatch-checking=>seq sequence
1648 (let ((end (or end length)))
1649 (declare (type index end))
1650 (if from-end
1651 (if-list-delete-from-end)
1652 (if-list-delete)))
1653 (let ((end (or end length)))
1654 (declare (type index end))
1655 (if from-end
1656 (if-mumble-delete-from-end)
1657 (if-mumble-delete)))
1658 (apply #'sb!sequence:delete-if predicate sequence args)))
1660 (eval-when (:compile-toplevel :execute)
1662 (sb!xc:defmacro if-not-mumble-delete ()
1663 `(mumble-delete
1664 (not (funcall predicate (apply-key key (aref sequence index))))))
1666 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1667 `(mumble-delete-from-end
1668 (not (funcall predicate (apply-key key this-element)))))
1670 (sb!xc:defmacro if-not-list-delete ()
1671 '(list-delete
1672 (not (funcall predicate (apply-key key (car current))))))
1674 (sb!xc:defmacro if-not-list-delete-from-end ()
1675 '(list-delete-from-end
1676 (not (funcall predicate (apply-key key (car current))))))
1678 ) ; EVAL-WHEN
1680 (define-sequence-traverser delete-if-not
1681 (predicate sequence &rest args &key from-end start end key count)
1682 #!+sb-doc
1683 "Return a sequence formed by destructively removing the elements not
1684 satisfying the specified PREDICATE from the given SEQUENCE."
1685 (declare (type fixnum start)
1686 (truly-dynamic-extent args))
1687 (declare (explicit-check sequence :result))
1688 (seq-dispatch-checking=>seq sequence
1689 (let ((end (or end length)))
1690 (declare (type index end))
1691 (if from-end
1692 (if-not-list-delete-from-end)
1693 (if-not-list-delete)))
1694 (let ((end (or end length)))
1695 (declare (type index end))
1696 (if from-end
1697 (if-not-mumble-delete-from-end)
1698 (if-not-mumble-delete)))
1699 (apply #'sb!sequence:delete-if-not predicate sequence args)))
1701 ;;;; REMOVE
1703 (eval-when (:compile-toplevel :execute)
1705 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1706 ;;; satisfies the predicate.
1707 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1708 `(do ((index ,begin (,bump index))
1709 (result
1710 (do ((index ,left (,bump index))
1711 (result (%make-sequence-like sequence length)))
1712 ((= index (the fixnum ,begin)) result)
1713 (declare (fixnum index))
1714 (setf (aref result index) (aref sequence index))))
1715 (new-index ,begin)
1716 (number-zapped 0)
1717 (this-element))
1718 ((or (= index (the fixnum ,finish))
1719 (= number-zapped count))
1720 (do ((index index (,bump index))
1721 (new-index new-index (,bump new-index)))
1722 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1723 (declare (fixnum index new-index))
1724 (setf (aref result new-index) (aref sequence index))))
1725 (declare (fixnum index new-index number-zapped))
1726 (setq this-element (aref sequence index))
1727 (cond (,pred (incf number-zapped))
1728 (t (setf (aref result new-index) this-element)
1729 (setq new-index (,bump new-index))))))
1731 (sb!xc:defmacro mumble-remove (pred)
1732 `(mumble-remove-macro 1+ 0 start end length ,pred))
1734 (sb!xc:defmacro mumble-remove-from-end (pred)
1735 `(let ((sequence (copy-seq sequence)))
1736 (mumble-delete-from-end ,pred)))
1738 (sb!xc:defmacro normal-mumble-remove ()
1739 `(mumble-remove
1740 (if test-not
1741 (not (funcall test-not item (apply-key key this-element)))
1742 (funcall test item (apply-key key this-element)))))
1744 (sb!xc:defmacro normal-mumble-remove-from-end ()
1745 `(mumble-remove-from-end
1746 (if test-not
1747 (not (funcall test-not item (apply-key key this-element)))
1748 (funcall test item (apply-key key this-element)))))
1750 (sb!xc:defmacro if-mumble-remove ()
1751 `(mumble-remove (funcall predicate (apply-key key this-element))))
1753 (sb!xc:defmacro if-mumble-remove-from-end ()
1754 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1756 (sb!xc:defmacro if-not-mumble-remove ()
1757 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1759 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1760 `(mumble-remove-from-end
1761 (not (funcall predicate (apply-key key this-element)))))
1763 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1764 ;;; the predicate.
1765 (sb!xc:defmacro list-remove-macro (pred reverse?)
1766 `(let* ((sequence ,(if reverse?
1767 '(reverse (the list sequence))
1768 'sequence))
1769 (%start ,(if reverse? '(- length end) 'start))
1770 (%end ,(if reverse? '(- length start) 'end))
1771 (splice (list nil))
1772 (tail (and (/= %end length)
1773 (nthcdr %end sequence)))
1774 (results ,(if reverse?
1775 ;; It's already copied by REVERSE, so it can
1776 ;; be modified here
1777 `(if (plusp %start)
1778 (let* ((tail (nthcdr (1- %start) sequence))
1779 (remaining (cdr tail)))
1780 (setf (cdr tail) nil)
1781 (prog1 splice
1782 (rplacd splice sequence)
1783 (setf splice tail
1784 sequence remaining)))
1785 splice)
1786 `(do ((index 0 (1+ index))
1787 (before-start splice))
1788 ((= index (the fixnum %start)) before-start)
1789 (declare (fixnum index))
1790 (setf splice
1791 (cdr (rplacd splice (list (pop sequence)))))))))
1792 (declare (truly-dynamic-extent splice))
1793 (do ((this-element)
1794 (number-zapped 0))
1795 ((cond ((eq tail sequence)
1796 (rplacd splice tail)
1798 ((= number-zapped count)
1799 (rplacd splice sequence)
1801 ,(if reverse?
1802 '(nreverse (the list (cdr results)))
1803 '(cdr results)))
1804 (declare (index number-zapped))
1805 (setf this-element (pop sequence))
1806 (if ,pred
1807 (incf number-zapped)
1808 (setf splice (cdr (rplacd splice (list this-element))))))))
1810 (sb!xc:defmacro list-remove (pred)
1811 `(list-remove-macro ,pred nil))
1813 (sb!xc:defmacro list-remove-from-end (pred)
1814 `(list-remove-macro ,pred t))
1816 (sb!xc:defmacro normal-list-remove ()
1817 `(list-remove
1818 (if test-not
1819 (not (funcall test-not item (apply-key key this-element)))
1820 (funcall test item (apply-key key this-element)))))
1822 (sb!xc:defmacro normal-list-remove-from-end ()
1823 `(list-remove-from-end
1824 (if test-not
1825 (not (funcall test-not item (apply-key key this-element)))
1826 (funcall test item (apply-key key this-element)))))
1828 (sb!xc:defmacro if-list-remove ()
1829 `(list-remove
1830 (funcall predicate (apply-key key this-element))))
1832 (sb!xc:defmacro if-list-remove-from-end ()
1833 `(list-remove-from-end
1834 (funcall predicate (apply-key key this-element))))
1836 (sb!xc:defmacro if-not-list-remove ()
1837 `(list-remove
1838 (not (funcall predicate (apply-key key this-element)))))
1840 (sb!xc:defmacro if-not-list-remove-from-end ()
1841 `(list-remove-from-end
1842 (not (funcall predicate (apply-key key this-element)))))
1844 ) ; EVAL-WHEN
1846 (define-sequence-traverser remove
1847 (item sequence &rest args &key from-end test test-not start
1848 end count key)
1849 #!+sb-doc
1850 "Return a copy of SEQUENCE with elements satisfying the test (default is
1851 EQL) with ITEM removed."
1852 (declare (type fixnum start)
1853 (truly-dynamic-extent args))
1854 (declare (explicit-check sequence :result))
1855 (seq-dispatch-checking=>seq sequence
1856 (let ((end (or end length)))
1857 (declare (type index end))
1858 (if from-end
1859 (normal-list-remove-from-end)
1860 (normal-list-remove)))
1861 (let ((end (or end length)))
1862 (declare (type index end))
1863 (if from-end
1864 (normal-mumble-remove-from-end)
1865 (normal-mumble-remove)))
1866 (apply #'sb!sequence:remove item sequence args)))
1868 (define-sequence-traverser remove-if
1869 (predicate sequence &rest args &key from-end start end count key)
1870 #!+sb-doc
1871 "Return a copy of sequence with elements satisfying PREDICATE removed."
1872 (declare (type fixnum start)
1873 (truly-dynamic-extent args))
1874 (declare (explicit-check sequence :result))
1875 (seq-dispatch-checking=>seq sequence
1876 (let ((end (or end length)))
1877 (declare (type index end))
1878 (if from-end
1879 (if-list-remove-from-end)
1880 (if-list-remove)))
1881 (let ((end (or end length)))
1882 (declare (type index end))
1883 (if from-end
1884 (if-mumble-remove-from-end)
1885 (if-mumble-remove)))
1886 (apply #'sb!sequence:remove-if predicate sequence args)))
1888 (define-sequence-traverser remove-if-not
1889 (predicate sequence &rest args &key from-end start end count key)
1890 #!+sb-doc
1891 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1892 (declare (type fixnum start)
1893 (truly-dynamic-extent args))
1894 (declare (explicit-check sequence :result))
1895 (seq-dispatch-checking=>seq sequence
1896 (let ((end (or end length)))
1897 (declare (type index end))
1898 (if from-end
1899 (if-not-list-remove-from-end)
1900 (if-not-list-remove)))
1901 (let ((end (or end length)))
1902 (declare (type index end))
1903 (if from-end
1904 (if-not-mumble-remove-from-end)
1905 (if-not-mumble-remove)))
1906 (apply #'sb!sequence:remove-if-not predicate sequence args)))
1908 ;;;; REMOVE-DUPLICATES
1910 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1911 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1912 ;;; if we look into the already copied structure (from after :start) and see
1913 ;;; the item. If we check from beginning we check into the rest of the
1914 ;;; original list up to the :end marker (this we have to do by running a
1915 ;;; do loop down the list that far and using our test.
1916 (defun list-remove-duplicates* (list test test-not start end key from-end)
1917 (declare (fixnum start)
1918 (list list))
1919 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1920 (splice result)
1921 (current list)
1922 (length (length list))
1923 (end (or end length))
1924 (whole (= end length))
1925 (hash (and (> (- end start) 20)
1926 (not key)
1927 (not test-not)
1928 (hash-table-test-p test)
1929 (make-hash-table :test test :size (- end start))))
1930 (tail (and (not whole)
1931 (nthcdr end list))))
1932 (declare (truly-dynamic-extent result))
1933 (do ((index 0 (1+ index)))
1934 ((= index start))
1935 (declare (fixnum index))
1936 (setq splice (cdr (rplacd splice (list (car current)))))
1937 (setq current (cdr current)))
1938 (if hash
1939 (do ()
1940 ((eq current tail))
1941 ;; The hash table contains links from values that are
1942 ;; already in result to the cons cell *preceding* theirs
1943 ;; in the list. That is, for each value v in the list,
1944 ;; v and (cadr (gethash v hash)) are equal under TEST.
1945 (let ((prev (gethash (car current) hash)))
1946 (cond
1947 ((not prev)
1948 (setf (gethash (car current) hash) splice)
1949 (setq splice (cdr (rplacd splice (list (car current))))))
1950 ((not from-end)
1951 (let* ((old (cdr prev))
1952 (next (cdr old)))
1953 (if next
1954 (let ((next-val (car next)))
1955 ;; (assert (eq (gethash next-val hash) old))
1956 (setf (cdr prev) next
1957 (gethash next-val hash) prev
1958 (gethash (car current) hash) splice
1959 splice (cdr (rplacd splice (list (car current))))))
1960 (setf (car old) (car current)))))))
1961 (setq current (cdr current)))
1962 (let ((testp test) ;; for with-member-test
1963 (notp test-not))
1964 (with-member-test (member-test
1965 ((and (not from-end)
1966 (not whole))
1967 (if notp
1968 (if key
1969 (lambda (x y key test)
1970 (not (funcall (truly-the function test) x
1971 (funcall (truly-the function key) y))))
1972 (lambda (x y key test)
1973 (declare (ignore key))
1974 (not (funcall (truly-the function test) x y))))
1975 (if key
1976 (lambda (x y key test)
1977 (funcall (truly-the function test) x
1978 (funcall (truly-the function key) y)))
1979 (lambda (x y key test)
1980 (declare (ignore key))
1981 (funcall (truly-the function test) x y))))))
1982 (do ((copied (nthcdr start result)))
1983 ((eq current tail))
1984 (let ((elt (car current)))
1985 (when (cond (from-end
1986 (not (funcall member-test elt (cdr copied) key test)))
1987 (whole
1988 (not (funcall member-test elt (cdr current) key test)))
1990 (do ((it (apply-key key elt))
1991 (l (cdr current) (cdr l)))
1992 ((eq l tail)
1994 (when (funcall member-test it (car l) key test)
1995 (return)))))
1996 (setf splice (cdr (rplacd splice (list elt))))))
1997 (pop current)))))
1998 (rplacd splice tail)
1999 (cdr result)))
2001 (defun vector-remove-duplicates* (vector test test-not start end key from-end
2002 &optional (length (length vector)))
2003 (declare (vector vector) (fixnum start length))
2004 (when (null end) (setf end (length vector)))
2005 (let ((result (%make-sequence-like vector length))
2006 (index 0)
2007 (jndex start))
2008 (declare (fixnum index jndex))
2009 (do ()
2010 ((= index start))
2011 (setf (aref result index) (aref vector index))
2012 (setq index (1+ index)))
2013 (do ((elt))
2014 ((= index end))
2015 (setq elt (aref vector index))
2016 (unless (or (and from-end
2017 (if test-not
2018 (position (apply-key key elt) result
2019 :start start :end jndex
2020 :test-not test-not :key key)
2021 (position (apply-key key elt) result
2022 :start start :end jndex
2023 :test test :key key)))
2024 (and (not from-end)
2025 (if test-not
2026 (position (apply-key key elt) vector
2027 :start (1+ index) :end end
2028 :test-not test-not :key key)
2029 (position (apply-key key elt) vector
2030 :start (1+ index) :end end
2031 :test test :key key))))
2032 (setf (aref result jndex) elt)
2033 (setq jndex (1+ jndex)))
2034 (setq index (1+ index)))
2035 (do ()
2036 ((= index length))
2037 (setf (aref result jndex) (aref vector index))
2038 (setq index (1+ index))
2039 (setq jndex (1+ jndex)))
2040 (%shrink-vector result jndex)))
2042 (define-sequence-traverser remove-duplicates
2043 (sequence &rest args &key test test-not start end from-end key)
2044 #!+sb-doc
2045 "The elements of SEQUENCE are compared pairwise, and if any two match,
2046 the one occurring earlier is discarded, unless FROM-END is true, in
2047 which case the one later in the sequence is discarded. The resulting
2048 sequence is returned.
2050 The :TEST-NOT argument is deprecated."
2051 (declare (fixnum start)
2052 (truly-dynamic-extent args))
2053 (declare (explicit-check sequence :result))
2054 (seq-dispatch-checking=>seq sequence
2055 (if sequence
2056 (list-remove-duplicates* sequence test test-not
2057 start end key from-end))
2058 (vector-remove-duplicates* sequence test test-not start end key from-end)
2059 (apply #'sb!sequence:remove-duplicates sequence args)))
2061 ;;;; DELETE-DUPLICATES
2062 (defun list-delete-duplicates* (list test test-not key from-end start end)
2063 (declare (index start)
2064 (list list))
2065 (let* ((handle (cons nil list))
2066 (from-end-start (and from-end
2067 (nthcdr (1+ start) handle)))
2068 (length (length list))
2069 (end (or end length))
2070 (tail (and (/= length (truly-the fixnum end))
2071 (nthcdr end list))))
2072 (declare (truly-dynamic-extent handle))
2073 (do* ((previous (nthcdr start handle))
2074 (current (cdr previous) (cdr current)))
2075 ((eq current tail)
2076 (cdr handle))
2077 (if (do ((end (if from-end
2078 current
2079 tail))
2080 (x (if from-end
2081 from-end-start
2082 (cdr current))
2083 (cdr x)))
2084 ((eq x end))
2085 (if (if test-not
2086 (not (funcall (truly-the function test-not)
2087 (apply-key-function key (car current))
2088 (apply-key-function key (car x))))
2089 (funcall (truly-the function test)
2090 (apply-key-function key (car current))
2091 (apply-key-function key (car x))))
2092 (return t)))
2093 (rplacd previous (cdr current))
2094 (pop previous)))))
2096 (defun vector-delete-duplicates* (vector test test-not key from-end start end
2097 &optional (length (length vector)))
2098 (declare (vector vector) (fixnum start length))
2099 (when (null end) (setf end (length vector)))
2100 (do ((index start (1+ index))
2101 (jndex start))
2102 ((= index end)
2103 (do ((index index (1+ index)) ; copy the rest of the vector
2104 (jndex jndex (1+ jndex)))
2105 ((= index length)
2106 (shrink-vector vector jndex))
2107 (setf (aref vector jndex) (aref vector index))))
2108 (declare (fixnum index jndex))
2109 (setf (aref vector jndex) (aref vector index))
2110 (unless (if test-not
2111 (position (apply-key key (aref vector index)) vector :key key
2112 :start (if from-end start (1+ index))
2113 :end (if from-end jndex end)
2114 :test-not test-not)
2115 (position (apply-key key (aref vector index)) vector :key key
2116 :start (if from-end start (1+ index))
2117 :end (if from-end jndex end)
2118 :test test))
2119 (setq jndex (1+ jndex)))))
2121 (define-sequence-traverser delete-duplicates
2122 (sequence &rest args &key test test-not start end from-end key)
2123 #!+sb-doc
2124 "The elements of SEQUENCE are examined, and if any two match, one is
2125 discarded. The resulting sequence, which may be formed by destroying the
2126 given sequence, is returned.
2128 The :TEST-NOT argument is deprecated."
2129 (declare (truly-dynamic-extent args))
2130 (declare (explicit-check sequence :result))
2131 (seq-dispatch-checking=>seq sequence
2132 (when sequence
2133 (list-delete-duplicates* sequence test test-not
2134 key from-end start end))
2135 (vector-delete-duplicates* sequence test test-not key from-end start end)
2136 (apply #'sb!sequence:delete-duplicates sequence args)))
2138 ;;;; SUBSTITUTE
2140 (defun list-substitute* (pred new list start end count key test test-not old)
2141 (declare (fixnum start end count)
2142 (type (or null function) key)
2143 (optimize speed))
2144 (let* ((result (list nil))
2145 (test (or test-not test))
2146 (test-not (or test-not
2147 (eq pred 'if-not)))
2149 (splice result)
2150 (list list)) ; Get a local list for a stepper.
2151 (declare (function test))
2152 (do ((index 0 (1+ index)))
2153 ((= index start))
2154 (declare (fixnum index))
2155 (setf splice (cdr (rplacd splice (list (car list))))
2156 list (cdr list)))
2157 (do ((index start (1+ index)))
2158 ((or (= index end) (null list) (= count 0)))
2159 (declare (fixnum index))
2160 (setf elt (car list)
2161 splice
2162 (cdr (rplacd splice
2163 (list
2164 (cond ((let* ((elt (apply-key key elt))
2165 (value (if (eq pred 'normal)
2166 (funcall test old elt)
2167 (funcall test elt))))
2168 (if test-not
2169 (not value)
2170 value))
2171 (decf count)
2172 new)
2173 (t elt)))))
2174 list (cdr list)))
2175 (do ()
2176 ((null list))
2177 (setf splice (cdr (rplacd splice (list (car list))))
2178 list (cdr list)))
2179 (cdr result)))
2181 ;;; Replace old with new in sequence moving from left to right by incrementer
2182 ;;; on each pass through the loop. Called by all three substitute functions.
2183 (defun vector-substitute* (pred new sequence incrementer left right length
2184 start end count key test test-not old)
2185 (declare (fixnum start count end incrementer right)
2186 (type (or null function) key))
2187 (let* ((result (make-vector-like sequence length))
2188 (getter (the function (svref %%data-vector-reffers%%
2189 (%other-pointer-widetag sequence))))
2190 (setter (the function (svref %%data-vector-setters%%
2191 (%other-pointer-widetag result))))
2192 (test (or test-not test))
2193 (test-not (or test-not
2194 (eq pred 'if-not)))
2195 (index left))
2196 (declare (fixnum index)
2197 (function test))
2198 (do ()
2199 ((= index start))
2200 (funcall setter result index
2201 (funcall getter sequence index))
2202 (incf index incrementer))
2203 (do ((elt))
2204 ((or (= index end) (= count 0)))
2205 (setf elt (funcall getter sequence index))
2206 (funcall setter result index
2207 (cond ((let* ((elt (apply-key key elt))
2208 (value (if (eq pred 'normal)
2209 (funcall test old elt)
2210 (funcall test elt))))
2211 (if test-not
2212 (not value)
2213 value))
2214 (decf count)
2215 new)
2216 (t elt)))
2217 (incf index incrementer))
2218 (do ()
2219 ((= index right))
2220 (funcall setter result index
2221 (funcall getter sequence index))
2222 (incf index incrementer))
2223 result))
2225 (eval-when (:compile-toplevel :execute)
2227 (sb!xc:defmacro subst-dispatch (pred)
2228 `(seq-dispatch-checking=>seq sequence
2229 (let ((end (or end length)))
2230 (declare (type index end))
2231 (if from-end
2232 (nreverse (list-substitute* ,pred
2234 (reverse sequence)
2235 (- (the fixnum length)
2236 (the fixnum end))
2237 (- (the fixnum length)
2238 (the fixnum start))
2239 count key test test-not old))
2240 (list-substitute* ,pred
2241 new sequence start end count key test test-not
2242 old)))
2244 (let ((end (or end length)))
2245 (declare (type index end))
2246 (if from-end
2247 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2248 -1 length (1- (the fixnum end))
2249 (1- (the fixnum start))
2250 count key test test-not old)
2251 (vector-substitute* ,pred new sequence 1 0 length length
2252 start end count key test test-not old)))
2254 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2255 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2256 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2257 ;; dispatch once per element on PRED's run-time identity.
2258 ,(ecase (cadr pred)
2259 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2260 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2261 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2262 ) ; EVAL-WHEN
2264 (define-sequence-traverser substitute
2265 (new old sequence &rest args &key from-end test test-not
2266 start count end key)
2267 #!+sb-doc
2268 "Return a sequence of the same kind as SEQUENCE with the same elements,
2269 except that all elements equal to OLD are replaced with NEW."
2270 (declare (type fixnum start)
2271 (explicit-check sequence :result)
2272 (truly-dynamic-extent args))
2273 (subst-dispatch 'normal))
2275 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2277 (define-sequence-traverser substitute-if
2278 (new predicate sequence &rest args &key from-end start end count key)
2279 #!+sb-doc
2280 "Return a sequence of the same kind as SEQUENCE with the same elements
2281 except that all elements satisfying the PRED are replaced with NEW."
2282 (declare (type fixnum start)
2283 (explicit-check sequence :result)
2284 (truly-dynamic-extent args))
2285 (let ((test predicate)
2286 (test-not nil)
2287 old)
2288 (subst-dispatch 'if)))
2290 (define-sequence-traverser substitute-if-not
2291 (new predicate sequence &rest args &key from-end start end count key)
2292 #!+sb-doc
2293 "Return a sequence of the same kind as SEQUENCE with the same elements
2294 except that all elements not satisfying the PRED are replaced with NEW."
2295 (declare (type fixnum start)
2296 (explicit-check sequence :result)
2297 (truly-dynamic-extent args))
2298 (let ((test predicate)
2299 (test-not nil)
2300 old)
2301 (subst-dispatch 'if-not)))
2303 ;;;; NSUBSTITUTE
2305 (define-sequence-traverser nsubstitute
2306 (new old sequence &rest args &key from-end test test-not
2307 end count key start)
2308 #!+sb-doc
2309 "Return a sequence of the same kind as SEQUENCE with the same elements
2310 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2311 may be destructively modified."
2312 (declare (type fixnum start)
2313 (truly-dynamic-extent args))
2314 (declare (explicit-check sequence :result))
2315 (seq-dispatch-checking=>seq sequence
2316 (let ((end (or end length)))
2317 (declare (type index end))
2318 (if from-end
2319 (nreverse (nlist-substitute*
2320 new old (nreverse (the list sequence))
2321 test test-not (- length end) (- length start)
2322 count key))
2323 (nlist-substitute* new old sequence
2324 test test-not start end count key)))
2325 (let ((end (or end length)))
2326 (declare (type index end))
2327 (if from-end
2328 (nvector-substitute* new old sequence -1
2329 test test-not (1- end) (1- start) count key)
2330 (nvector-substitute* new old sequence 1
2331 test test-not start end count key)))
2332 (apply #'sb!sequence:nsubstitute new old sequence args)))
2334 (defun nlist-substitute* (new old sequence test test-not start end count key)
2335 (declare (fixnum start count end)
2336 (type (or null function) key))
2337 (do ((test (or test-not test))
2338 (list (nthcdr start sequence) (cdr list))
2339 (index start (1+ index)))
2340 ((or (= index end) (null list) (= count 0)) sequence)
2341 (declare (fixnum index)
2342 (function test))
2343 (let ((value (funcall test old (apply-key key (car list)))))
2344 (when (if test-not
2345 (not value)
2346 value)
2347 (rplaca list new)
2348 (decf count)))))
2350 (defun nvector-substitute* (new old sequence incrementer
2351 test test-not start end count key)
2352 (declare (fixnum start count end)
2353 (type (integer -1 1) incrementer)
2354 (type (or null function) key))
2355 (let* ((test (or test-not test))
2356 (tag (%other-pointer-widetag sequence))
2357 (getter (the function (svref %%data-vector-reffers%% tag)))
2358 (setter (the function (svref %%data-vector-setters%% tag))))
2359 (declare (function test))
2360 (do ((index start (+ index incrementer)))
2361 ((or (= index end) (= count 0)) sequence)
2362 (declare (fixnum index))
2363 (let* ((value (apply-key key (funcall getter sequence index)))
2364 (test (and (funcall test old value) 0)))
2365 (when (if test-not
2366 (not test)
2367 test)
2368 (funcall setter sequence index new)
2369 (decf count))))))
2371 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2373 (define-sequence-traverser nsubstitute-if
2374 (new predicate sequence &rest args &key from-end start end count key)
2375 #!+sb-doc
2376 "Return a sequence of the same kind as SEQUENCE with the same elements
2377 except that all elements satisfying PREDICATE are replaced with NEW.
2378 SEQUENCE may be destructively modified."
2379 (declare (type fixnum start)
2380 (truly-dynamic-extent args))
2381 (declare (explicit-check sequence :result))
2382 (seq-dispatch-checking=>seq sequence
2383 (let ((end (or end length)))
2384 (declare (type index end))
2385 (if from-end
2386 (nreverse (nlist-substitute-if*
2387 new predicate (nreverse (the list sequence))
2388 (- length end) (- length start) count key))
2389 (nlist-substitute-if* new predicate sequence
2390 start end count key)))
2391 (let ((end (or end length)))
2392 (declare (type index end))
2393 (if from-end
2394 (nvector-substitute-if* new predicate sequence -1
2395 (1- end) (1- start) count key)
2396 (nvector-substitute-if* new predicate sequence 1
2397 start end count key)))
2398 (apply #'sb!sequence:nsubstitute-if new predicate sequence args)))
2400 (defun nlist-substitute-if* (new test sequence start end count key)
2401 (declare (type fixnum start end count)
2402 (type (or null function) key)
2403 (type function test)) ; coercion is done by caller
2404 (do ((list (nthcdr start sequence) (cdr list))
2405 (index start (1+ index)))
2406 ((or (= index end) (null list) (= count 0)) sequence)
2407 (declare (fixnum index))
2408 (when (funcall test (apply-key key (car list)))
2409 (rplaca list new)
2410 (decf count))))
2412 (defun nvector-substitute-if* (new test sequence incrementer
2413 start end count key)
2414 (declare (type fixnum end count)
2415 (type (integer -1 1) incrementer)
2416 (type (or null function) key)
2417 (type function test)) ; coercion is done by caller
2418 (let* ((tag (%other-pointer-widetag sequence))
2419 (getter (the function (svref %%data-vector-reffers%% tag)))
2420 (setter (the function (svref %%data-vector-setters%% tag))))
2421 (do ((index start (+ index incrementer)))
2422 ((or (= index end) (= count 0)) sequence)
2423 (declare (fixnum index))
2424 (when (funcall test (apply-key key (funcall getter sequence index)))
2425 (funcall setter sequence index new)
2426 (decf count)))))
2428 (define-sequence-traverser nsubstitute-if-not
2429 (new predicate sequence &rest args &key from-end start end count key)
2430 #!+sb-doc
2431 "Return a sequence of the same kind as SEQUENCE with the same elements
2432 except that all elements not satisfying PREDICATE are replaced with NEW.
2433 SEQUENCE may be destructively modified."
2434 (declare (type fixnum start)
2435 (truly-dynamic-extent args))
2436 (declare (explicit-check sequence :result))
2437 (seq-dispatch-checking=>seq sequence
2438 (let ((end (or end length)))
2439 (declare (fixnum end))
2440 (if from-end
2441 (nreverse (nlist-substitute-if-not*
2442 new predicate (nreverse (the list sequence))
2443 (- length end) (- length start) count key))
2444 (nlist-substitute-if-not* new predicate sequence
2445 start end count key)))
2446 (let ((end (or end length)))
2447 (declare (fixnum end))
2448 (if from-end
2449 (nvector-substitute-if-not* new predicate sequence -1
2450 (1- end) (1- start) count key)
2451 (nvector-substitute-if-not* new predicate sequence 1
2452 start end count key)))
2453 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args)))
2455 (defun nlist-substitute-if-not* (new test sequence start end count key)
2456 (declare (type fixnum start end count)
2457 (type (or null function) key)
2458 (type function test)) ; coercion is done by caller
2459 (do ((list (nthcdr start sequence) (cdr list))
2460 (index start (1+ index)))
2461 ((or (= index end) (null list) (= count 0)) sequence)
2462 (declare (fixnum index))
2463 (when (not (funcall test (apply-key key (car list))))
2464 (rplaca list new)
2465 (decf count))))
2467 (defun nvector-substitute-if-not* (new test sequence incrementer
2468 start end count key)
2469 (declare (type fixnum end count)
2470 (type (integer -1 1) incrementer)
2471 (type (or null function) key)
2472 (type function test)) ; coercion is done by caller
2473 (let* ((tag (%other-pointer-widetag sequence))
2474 (getter (the function (svref %%data-vector-reffers%% tag)))
2475 (setter (the function (svref %%data-vector-setters%% tag))))
2476 (do ((index start (+ index incrementer)))
2477 ((or (= index end) (= count 0)) sequence)
2478 (declare (fixnum index))
2479 (when (not (funcall test (apply-key key (funcall getter sequence index))))
2480 (funcall setter sequence index new)
2481 (decf count)))))
2483 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2485 (defun effective-find-position-test (test test-not)
2486 (effective-find-position-test test test-not))
2487 (defun effective-find-position-key (key)
2488 (effective-find-position-key key))
2490 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2491 (macrolet (;; shared logic for defining %FIND-POSITION and
2492 ;; %FIND-POSITION-IF in terms of various inlineable cases
2493 ;; of the expression defined in FROB and VECTOR*-FROB
2494 (frobs (&optional bit-frob)
2495 `(seq-dispatch-checking sequence-arg
2496 (frob sequence-arg from-end)
2497 (with-array-data ((sequence sequence-arg :offset-var offset)
2498 (start start)
2499 (end end)
2500 :check-fill-pointer t)
2501 (multiple-value-bind (f p)
2502 (macrolet ((frob2 () `(if from-end
2503 (frob sequence t)
2504 (frob sequence nil))))
2505 (typecase sequence
2506 #!+sb-unicode
2507 ((simple-array character (*)) (frob2))
2508 ((simple-array base-char (*)) (frob2))
2509 ,@(when bit-frob
2510 `((simple-bit-vector
2511 (if (and (typep item 'bit)
2512 (eq #'identity key)
2513 (or (eq #'eq test)
2514 (eq #'eql test)
2515 (eq #'equal test)))
2516 (let ((p (%bit-position item sequence
2517 from-end start end)))
2518 (if p
2519 (values item p)
2520 (values nil nil)))
2521 (vector*-frob sequence)))))
2523 (vector*-frob sequence))))
2524 (declare (type (or index null) p))
2525 (values f (and p (the index (- p offset))))))
2526 ;; EXTENDED-SEQUENCE is not allowed.
2528 (defun %find-position (item sequence-arg from-end start end key test)
2529 (declare (explicit-check sequence-arg))
2530 (macrolet ((frob (sequence from-end)
2531 `(%find-position item ,sequence
2532 ,from-end start end key test))
2533 (vector*-frob (sequence)
2534 `(%find-position-vector-macro item ,sequence
2535 from-end start end key test)))
2536 (frobs t)))
2537 (defun %find-position-if (predicate sequence-arg from-end start end key)
2538 (declare (explicit-check sequence-arg))
2539 (macrolet ((frob (sequence from-end)
2540 `(%find-position-if predicate ,sequence
2541 ,from-end start end key))
2542 (vector*-frob (sequence)
2543 `(%find-position-if-vector-macro predicate ,sequence
2544 from-end start end key)))
2545 (frobs)))
2546 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2547 (declare (explicit-check sequence-arg))
2548 (macrolet ((frob (sequence from-end)
2549 `(%find-position-if-not predicate ,sequence
2550 ,from-end start end key))
2551 (vector*-frob (sequence)
2552 `(%find-position-if-not-vector-macro predicate ,sequence
2553 from-end start end key)))
2554 (frobs))))
2556 (defun find
2557 (item sequence &rest args &key from-end (start 0) end key test test-not)
2558 (declare (truly-dynamic-extent args))
2559 (declare (explicit-check sequence))
2560 (seq-dispatch-checking sequence
2561 (nth-value 0 (%find-position
2562 item sequence from-end start end
2563 (effective-find-position-key key)
2564 (effective-find-position-test test test-not)))
2565 (nth-value 0 (%find-position
2566 item sequence from-end start end
2567 (effective-find-position-key key)
2568 (effective-find-position-test test test-not)))
2569 (apply #'sb!sequence:find item sequence args)))
2570 (defun position
2571 (item sequence &rest args &key from-end (start 0) end key test test-not)
2572 (declare (truly-dynamic-extent args))
2573 (declare (explicit-check sequence))
2574 (seq-dispatch-checking sequence
2575 (nth-value 1 (%find-position
2576 item sequence from-end start end
2577 (effective-find-position-key key)
2578 (effective-find-position-test test test-not)))
2579 (nth-value 1 (%find-position
2580 item sequence from-end start end
2581 (effective-find-position-key key)
2582 (effective-find-position-test test test-not)))
2583 (apply #'sb!sequence:position item sequence args)))
2585 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2586 (declare (truly-dynamic-extent args))
2587 (declare (explicit-check sequence))
2588 (seq-dispatch-checking sequence
2589 (nth-value 0 (%find-position-if
2590 (%coerce-callable-to-fun predicate)
2591 sequence from-end start end
2592 (effective-find-position-key key)))
2593 (nth-value 0 (%find-position-if
2594 (%coerce-callable-to-fun predicate)
2595 sequence from-end start end
2596 (effective-find-position-key key)))
2597 (apply #'sb!sequence:find-if predicate sequence args)))
2598 (defun position-if
2599 (predicate sequence &rest args &key from-end (start 0) end key)
2600 (declare (truly-dynamic-extent args))
2601 (declare (explicit-check sequence))
2602 (seq-dispatch-checking sequence
2603 (nth-value 1 (%find-position-if
2604 (%coerce-callable-to-fun predicate)
2605 sequence from-end start end
2606 (effective-find-position-key key)))
2607 (nth-value 1 (%find-position-if
2608 (%coerce-callable-to-fun predicate)
2609 sequence from-end start end
2610 (effective-find-position-key key)))
2611 (apply #'sb!sequence:position-if predicate sequence args)))
2613 (defun find-if-not
2614 (predicate sequence &rest args &key from-end (start 0) end key)
2615 (declare (truly-dynamic-extent args))
2616 (declare (explicit-check sequence))
2617 (seq-dispatch-checking sequence
2618 (nth-value 0 (%find-position-if-not
2619 (%coerce-callable-to-fun predicate)
2620 sequence from-end start end
2621 (effective-find-position-key key)))
2622 (nth-value 0 (%find-position-if-not
2623 (%coerce-callable-to-fun predicate)
2624 sequence from-end start end
2625 (effective-find-position-key key)))
2626 (apply #'sb!sequence:find-if-not predicate sequence args)))
2627 (defun position-if-not
2628 (predicate sequence &rest args &key from-end (start 0) end key)
2629 (declare (truly-dynamic-extent args))
2630 (declare (explicit-check sequence))
2631 (seq-dispatch-checking sequence
2632 (nth-value 1 (%find-position-if-not
2633 (%coerce-callable-to-fun predicate)
2634 sequence from-end start end
2635 (effective-find-position-key key)))
2636 (nth-value 1 (%find-position-if-not
2637 (%coerce-callable-to-fun predicate)
2638 sequence from-end start end
2639 (effective-find-position-key key)))
2640 (apply #'sb!sequence:position-if-not predicate sequence args)))
2642 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2644 (eval-when (:compile-toplevel :execute)
2646 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2647 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2648 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2649 `(let ((%start ,(if from-end-p '(1- end) 'start))
2650 (%end ,(if from-end-p '(1- start) 'end)))
2651 (do ((index %start ,next-index)
2652 (count 0))
2653 ((= index (the fixnum %end)) count)
2654 (declare (fixnum index count))
2655 (,(if notp 'unless 'when) ,pred
2656 (setq count (1+ count)))))))
2658 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2659 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2660 `(let ((%start ,(if from-end-p '(- length end) 'start))
2661 (%end ,(if from-end-p '(- length start) 'end))
2662 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2663 (do ((sequence (nthcdr %start ,sequence))
2664 (index %start (1+ index))
2665 (count 0))
2666 ((or (= index (the fixnum %end)) (null sequence)) count)
2667 (declare (fixnum index count))
2668 (,(if notp 'unless 'when) ,pred
2669 (setq count (1+ count)))))))
2672 ) ; EVAL-WHEN
2674 (define-sequence-traverser count-if
2675 (pred sequence &rest args &key from-end start end key)
2676 #!+sb-doc
2677 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2678 (declare (type fixnum start)
2679 (truly-dynamic-extent args))
2680 (declare (explicit-check sequence))
2681 (let ((pred (%coerce-callable-to-fun pred)))
2682 (seq-dispatch-checking sequence
2683 (let ((end (or end length)))
2684 (declare (type index end))
2685 (if from-end
2686 (list-count-if nil t pred sequence)
2687 (list-count-if nil nil pred sequence)))
2688 (let ((end (or end length)))
2689 (declare (type index end))
2690 (if from-end
2691 (vector-count-if nil t pred sequence)
2692 (vector-count-if nil nil pred sequence)))
2693 (apply #'sb!sequence:count-if pred sequence args))))
2695 (define-sequence-traverser count-if-not
2696 (pred sequence &rest args &key from-end start end key)
2697 #!+sb-doc
2698 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2699 (declare (type fixnum start)
2700 (truly-dynamic-extent args))
2701 (declare (explicit-check sequence))
2702 (let ((pred (%coerce-callable-to-fun pred)))
2703 (seq-dispatch-checking sequence
2704 (let ((end (or end length)))
2705 (declare (type index end))
2706 (if from-end
2707 (list-count-if t t pred sequence)
2708 (list-count-if t nil pred sequence)))
2709 (let ((end (or end length)))
2710 (declare (type index end))
2711 (if from-end
2712 (vector-count-if t t pred sequence)
2713 (vector-count-if t nil pred sequence)))
2714 (apply #'sb!sequence:count-if-not pred sequence args))))
2716 (define-sequence-traverser count
2717 (item sequence &rest args &key from-end start end
2718 key (test #'eql test-p) (test-not nil test-not-p))
2719 #!+sb-doc
2720 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2721 which defaults to EQL."
2722 (declare (type fixnum start)
2723 (truly-dynamic-extent args))
2724 (declare (explicit-check sequence))
2725 (when (and test-p test-not-p)
2726 ;; Use the same wording as EFFECTIVE-FIND-POSITION-TEST
2727 (error "can't specify both :TEST and :TEST-NOT"))
2728 (let ((%test (if test-not-p
2729 (lambda (x)
2730 (not (funcall test-not item x)))
2731 (lambda (x)
2732 (funcall test item x)))))
2733 (seq-dispatch-checking sequence
2734 (let ((end (or end length)))
2735 (declare (type index end))
2736 (if from-end
2737 (list-count-if nil t %test sequence)
2738 (list-count-if nil nil %test sequence)))
2739 (let ((end (or end length)))
2740 (declare (type index end))
2741 (if from-end
2742 (vector-count-if nil t %test sequence)
2743 (vector-count-if nil nil %test sequence)))
2744 (apply #'sb!sequence:count item sequence args))))
2746 ;;;; MISMATCH
2748 (eval-when (:compile-toplevel :execute)
2750 (sb!xc:defmacro match-vars (&rest body)
2751 `(let ((inc (if from-end -1 1))
2752 (start1 (if from-end (1- (the fixnum end1)) start1))
2753 (start2 (if from-end (1- (the fixnum end2)) start2))
2754 (end1 (if from-end (1- (the fixnum start1)) end1))
2755 (end2 (if from-end (1- (the fixnum start2)) end2)))
2756 (declare (fixnum inc start1 start2 end1 end2))
2757 ,@body))
2759 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2760 (declare (ignore end)) ;; ### Should END be used below?
2761 `(let ((,sequence (if from-end
2762 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2763 (reverse (the list ,sequence)))
2764 (nthcdr ,start ,sequence))))
2765 (declare (type list ,sequence))
2766 ,@body))
2768 ) ; EVAL-WHEN
2770 (eval-when (:compile-toplevel :execute)
2772 (sb!xc:defmacro if-mismatch (elt1 elt2)
2773 `(cond ((= (the fixnum index1) (the fixnum end1))
2774 (return (if (= (the fixnum index2) (the fixnum end2))
2776 (if from-end
2777 (1+ (the fixnum index1))
2778 (the fixnum index1)))))
2779 ((= (the fixnum index2) (the fixnum end2))
2780 (return (if from-end (1+ (the fixnum index1)) index1)))
2781 (test-not
2782 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2783 (return (if from-end (1+ (the fixnum index1)) index1))))
2784 (t (if (not (funcall test (apply-key key ,elt1)
2785 (apply-key key ,elt2)))
2786 (return (if from-end (1+ (the fixnum index1)) index1))))))
2788 (sb!xc:defmacro mumble-mumble-mismatch ()
2789 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2790 (index2 start2 (+ index2 (the fixnum inc))))
2791 (())
2792 (declare (fixnum index1 index2))
2793 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2795 (sb!xc:defmacro mumble-list-mismatch ()
2796 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2797 (index2 start2 (+ index2 (the fixnum inc))))
2798 (())
2799 (declare (fixnum index1 index2))
2800 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2802 (sb!xc:defmacro list-mumble-mismatch ()
2803 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2804 (index2 start2 (+ index2 (the fixnum inc))))
2805 (())
2806 (declare (fixnum index1 index2))
2807 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2809 (sb!xc:defmacro list-list-mismatch ()
2810 `(do ((sequence1 sequence1)
2811 (sequence2 sequence2)
2812 (index1 start1 (+ index1 (the fixnum inc)))
2813 (index2 start2 (+ index2 (the fixnum inc))))
2814 (())
2815 (declare (fixnum index1 index2))
2816 (if-mismatch (pop sequence1) (pop sequence2))))
2818 ) ; EVAL-WHEN
2820 (define-sequence-traverser mismatch
2821 (sequence1 sequence2 &rest args &key from-end test test-not
2822 start1 end1 start2 end2 key)
2823 #!+sb-doc
2824 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2825 element-wise. If they are of equal length and match in every element, the
2826 result is NIL. Otherwise, the result is a non-negative integer, the index
2827 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2828 if one is shorter than and a matching prefix of the other, the index within
2829 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2830 :FROM-END argument is given, then one plus the index of the rightmost
2831 position in which the sequences differ is returned."
2832 (declare (type fixnum start1 start2))
2833 (declare (truly-dynamic-extent args))
2834 (declare (explicit-check sequence1 sequence2 :result))
2835 (seq-dispatch-checking sequence1
2836 (seq-dispatch-checking sequence2
2837 (return-from mismatch
2838 (let ((end1 (or end1 length1))
2839 (end2 (or end2 length2)))
2840 (declare (type index end1 end2))
2841 (match-vars
2842 (matchify-list (sequence1 start1 length1 end1)
2843 (matchify-list (sequence2 start2 length2 end2)
2844 (list-list-mismatch))))))
2845 (return-from mismatch
2846 (let ((end1 (or end1 length1))
2847 (end2 (or end2 length2)))
2848 (declare (type index end1 end2))
2849 (match-vars
2850 (matchify-list (sequence1 start1 length1 end1)
2851 (list-mumble-mismatch)))))
2852 nil)
2853 (seq-dispatch-checking sequence2
2854 (return-from mismatch
2855 (let ((end1 (or end1 length1))
2856 (end2 (or end2 length2)))
2857 (declare (type index end1 end2))
2858 (match-vars
2859 (matchify-list (sequence2 start2 length2 end2)
2860 (mumble-list-mismatch)))))
2861 (return-from mismatch
2862 (let ((end1 (or end1 length1))
2863 (end2 (or end2 length2)))
2864 (declare (type index end1 end2))
2865 (match-vars
2866 (mumble-mumble-mismatch))))
2867 nil)
2869 ;; If sequence1 is an extended-sequence, we know nothing about sequence2.
2870 ;; If sequence1 was a list or vector, then sequence2 is an extended-sequence
2871 ;; or not a sequence. Either way, check it.
2872 (the (or index null)
2873 (values (apply #'sb!sequence:mismatch sequence1
2874 (the sequence sequence2) args))))
2876 ;;; search comparison functions
2878 (eval-when (:compile-toplevel :execute)
2880 ;;; Compare two elements and return if they don't match.
2881 (sb!xc:defmacro compare-elements (elt1 elt2)
2882 `(if test-not
2883 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2884 (return nil)
2886 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2887 (return nil)
2888 t)))
2890 (sb!xc:defmacro search-compare-list-list (main sub)
2891 `(do ((main ,main (cdr main))
2892 (jndex start1 (1+ jndex))
2893 (sub (nthcdr start1 ,sub) (cdr sub)))
2894 ((or (endp main) (endp sub) (<= end1 jndex))
2896 (declare (type (integer 0) jndex))
2897 (compare-elements (car sub) (car main))))
2899 (sb!xc:defmacro search-compare-list-vector (main sub)
2900 `(do ((main ,main (cdr main))
2901 (index start1 (1+ index)))
2902 ((or (endp main) (= index end1)) t)
2903 (compare-elements (aref ,sub index) (car main))))
2905 (sb!xc:defmacro search-compare-vector-list (main sub index)
2906 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2907 (jndex start1 (1+ jndex))
2908 (index ,index (1+ index)))
2909 ((or (<= end1 jndex) (endp sub)) t)
2910 (declare (type (integer 0) jndex))
2911 (compare-elements (car sub) (aref ,main index))))
2913 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2914 `(do ((index ,index (1+ index))
2915 (sub-index start1 (1+ sub-index)))
2916 ((= sub-index end1) t)
2917 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2919 (sb!xc:defmacro search-compare (main-type main sub index)
2920 (if (eq main-type 'list)
2921 `(seq-dispatch ,sub
2922 (search-compare-list-list ,main ,sub)
2923 (search-compare-list-vector ,main ,sub)
2924 ;; KLUDGE: just hack it together so that it works
2925 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2926 `(seq-dispatch ,sub
2927 (search-compare-vector-list ,main ,sub ,index)
2928 (search-compare-vector-vector ,main ,sub ,index)
2929 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2931 ) ; EVAL-WHEN
2933 ;;;; SEARCH
2935 (eval-when (:compile-toplevel :execute)
2937 (sb!xc:defmacro list-search (main sub)
2938 `(do ((main (nthcdr start2 ,main) (cdr main))
2939 (index2 start2 (1+ index2))
2940 (terminus (- end2 (the (integer 0) (- end1 start1))))
2941 (last-match ()))
2942 ((> index2 terminus) last-match)
2943 (declare (type (integer 0) index2))
2944 (if (search-compare list main ,sub index2)
2945 (if from-end
2946 (setq last-match index2)
2947 (return index2)))))
2949 (sb!xc:defmacro vector-search (main sub)
2950 `(do ((index2 start2 (1+ index2))
2951 (terminus (- end2 (the (integer 0) (- end1 start1))))
2952 (last-match ()))
2953 ((> index2 terminus) last-match)
2954 (declare (type (integer 0) index2))
2955 (if (search-compare vector ,main ,sub index2)
2956 (if from-end
2957 (setq last-match index2)
2958 (return index2)))))
2960 ) ; EVAL-WHEN
2962 (define-sequence-traverser search
2963 (sequence1 sequence2 &rest args &key
2964 from-end test test-not start1 end1 start2 end2 key)
2965 (declare (type fixnum start1 start2)
2966 (truly-dynamic-extent args))
2967 (declare (explicit-check sequence2))
2968 (seq-dispatch-checking sequence2
2969 (let ((end1 (or end1 length1))
2970 (end2 (or end2 length2)))
2971 (declare (type index end1 end2))
2972 (list-search sequence2 sequence1))
2973 (let ((end1 (or end1 length1))
2974 (end2 (or end2 length2)))
2975 (declare (type index end1 end2))
2976 (vector-search sequence2 sequence1))
2977 (apply #'sb!sequence:search sequence1 sequence2 args)))
2979 ;;; FIXME: this was originally in array.lisp; it might be better to
2980 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2981 ;;; a new early-seq.lisp file.
2982 (macrolet ((body (lambda-list endp-test start-recursion next-layer)
2983 `(let ((index 0))
2984 (labels ((frob ,lambda-list
2985 (cond (,endp-test
2986 (setf (aref vector index) contents)
2987 (incf index))
2989 (unless (typep contents 'sequence)
2990 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2991 sequence, but ~W more layer~:P needed."
2992 contents
2993 (- (length dimensions) axis)))
2994 (let ((k this-dimension)
2995 (l (length contents)))
2996 (unless (= k l)
2997 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2998 axis ~W is ~W, but ~S is ~W long."
2999 axis k contents l)))
3000 (sb!sequence:dosequence (content contents)
3001 ,next-layer)))))
3002 ,start-recursion))))
3004 (defun fill-data-vector (vector dimensions initial-contents)
3005 (declare (explicit-check))
3006 (symbol-macrolet ((this-dimension (car dims)))
3007 (body (axis dims contents) (null dims)
3008 (frob 0 dimensions initial-contents)
3009 (frob (1+ axis) (cdr dims) content))))
3011 ;; Identical to FILL-DATA-VECTOR but avoid reference
3012 ;; to DIMENSIONS as a list except in case of error.
3013 (defun fill-array (initial-contents array)
3014 (declare (explicit-check))
3015 (let ((rank (array-rank array))
3016 (vector (%array-data-vector array)))
3017 (symbol-macrolet ((dimensions (array-dimensions array))
3018 (this-dimension (%array-dimension array axis)))
3019 (body (axis contents) (= axis rank)
3020 (frob 0 initial-contents)
3021 (frob (1+ axis) content))))
3022 array))