Speed up array word size calculation.
[sbcl.git] / src / code / seq.lisp
blobf8439909744a1f7c6d9d1140784c8f3a7d47be9d
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 #!-64-bit double-float)
616 :test #'equal))))
618 (defun vector-fill* (vector item start end)
619 (declare (type index start) (type (or index null) end)
620 (optimize speed))
621 (with-array-data ((vector vector)
622 (start start)
623 (end end)
624 :force-inline t
625 :check-fill-pointer t)
626 (if (simple-vector-p vector)
627 (locally
628 (declare (optimize (speed 3) (safety 0))) ; transform will kick in
629 (fill (truly-the simple-vector vector) item
630 :start start :end end))
631 (let* ((widetag (%other-pointer-widetag vector))
632 (bashers (svref %%fill-bashers%% widetag)))
633 (macrolet ((fill-float (type)
634 `(locally
635 (declare (optimize (speed 3) (safety 0))
636 (type ,type item)
637 (type (simple-array ,type (*))
638 vector))
639 (do ((index start (1+ index)))
640 ((= index end))
641 (declare (index index))
642 (setf (aref vector index) item)))))
643 (cond ((neq bashers 0)
644 (funcall (truly-the function (car (truly-the cons bashers)))
645 (funcall (truly-the function (cdr bashers)) item)
646 vector start (- end start)))
647 #!-64-bit
648 ((eq widetag sb!vm:simple-array-double-float-widetag)
649 (fill-float double-float))
650 #!-64-bit
651 ((eq widetag sb!vm:simple-array-complex-single-float-widetag)
652 (fill-float (complex single-float)))
654 (fill-float (complex double-float))))))))
655 vector)
657 (defun string-fill* (sequence item start end)
658 (declare (string sequence))
659 (with-array-data ((data sequence)
660 (start start)
661 (end end)
662 :force-inline t
663 :check-fill-pointer t)
664 ;; DEFTRANSFORM for FILL will turn these into
665 ;; calls to UB*-BASH-FILL.
666 (etypecase data
667 #!+sb-unicode
668 ((simple-array character (*))
669 (let ((item (locally (declare (optimize (safety 3)))
670 (the character item))))
671 (fill data item :start start :end end)))
672 ((simple-array base-char (*))
673 (let ((item (locally (declare (optimize (safety 3)))
674 (the base-char item))))
675 (fill data item :start start :end end))))))
677 (defun fill (sequence item &key (start 0) end)
678 #!+sb-doc
679 "Replace the specified elements of SEQUENCE with ITEM."
680 (declare (explicit-check sequence :result))
681 (seq-dispatch-checking=>seq sequence
682 (list-fill* sequence item start end)
683 (vector-fill* sequence item start end)
684 (sb!sequence:fill sequence item
685 :start start
686 :end (%check-generic-sequence-bounds sequence start end))))
688 ;;;; REPLACE
690 (eval-when (:compile-toplevel :execute)
692 ;;; If we are copying around in the same vector, be careful not to copy the
693 ;;; same elements over repeatedly. We do this by copying backwards.
694 (sb!xc:defmacro mumble-replace-from-mumble ()
695 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
696 (let ((nelts (min (- target-end target-start)
697 (- source-end source-start))))
698 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
699 (1- target-index))
700 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
701 (1- source-index)))
702 ((= target-index (the fixnum (1- target-start))) target-sequence)
703 (declare (fixnum target-index source-index))
704 ;; disable bounds checking
705 (declare (optimize (safety 0)))
706 (setf (aref target-sequence target-index)
707 (aref source-sequence source-index))))
708 (do ((target-index target-start (1+ target-index))
709 (source-index source-start (1+ source-index)))
710 ((or (= target-index (the fixnum target-end))
711 (= source-index (the fixnum source-end)))
712 target-sequence)
713 (declare (fixnum target-index source-index))
714 ;; disable bounds checking
715 (declare (optimize (safety 0)))
716 (setf (aref target-sequence target-index)
717 (aref source-sequence source-index)))))
719 (sb!xc:defmacro list-replace-from-list ()
720 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
721 (let ((new-elts (subseq source-sequence source-start
722 (+ (the fixnum source-start)
723 (the fixnum
724 (min (- (the fixnum target-end)
725 (the fixnum target-start))
726 (- (the fixnum source-end)
727 (the fixnum source-start))))))))
728 (do ((n new-elts (cdr n))
729 (o (nthcdr target-start target-sequence) (cdr o)))
730 ((null n) target-sequence)
731 (rplaca o (car n))))
732 (do ((target-index target-start (1+ target-index))
733 (source-index source-start (1+ source-index))
734 (target-sequence-ref (nthcdr target-start target-sequence)
735 (cdr target-sequence-ref))
736 (source-sequence-ref (nthcdr source-start source-sequence)
737 (cdr source-sequence-ref)))
738 ((or (= target-index (the fixnum target-end))
739 (= source-index (the fixnum source-end))
740 (null target-sequence-ref) (null source-sequence-ref))
741 target-sequence)
742 (declare (fixnum target-index source-index))
743 (rplaca target-sequence-ref (car source-sequence-ref)))))
745 (sb!xc:defmacro list-replace-from-mumble ()
746 `(do ((target-index target-start (1+ target-index))
747 (source-index source-start (1+ source-index))
748 (target-sequence-ref (nthcdr target-start target-sequence)
749 (cdr target-sequence-ref)))
750 ((or (= target-index (the fixnum target-end))
751 (= source-index (the fixnum source-end))
752 (null target-sequence-ref))
753 target-sequence)
754 (declare (fixnum source-index target-index))
755 (rplaca target-sequence-ref (aref source-sequence source-index))))
757 (sb!xc:defmacro mumble-replace-from-list ()
758 `(do ((target-index target-start (1+ target-index))
759 (source-index source-start (1+ source-index))
760 (source-sequence (nthcdr source-start source-sequence)
761 (cdr source-sequence)))
762 ((or (= target-index (the fixnum target-end))
763 (= source-index (the fixnum source-end))
764 (null source-sequence))
765 target-sequence)
766 (declare (fixnum target-index source-index))
767 (setf (aref target-sequence target-index) (car source-sequence))))
769 ) ; EVAL-WHEN
771 ;;;; The support routines for REPLACE are used by compiler transforms, so we
772 ;;;; worry about dealing with END being supplied or defaulting to NIL
773 ;;;; at this level.
775 (defun list-replace-from-list* (target-sequence source-sequence target-start
776 target-end source-start source-end)
777 (when (null target-end) (setq target-end (length target-sequence)))
778 (when (null source-end) (setq source-end (length source-sequence)))
779 (list-replace-from-list))
781 (defun list-replace-from-vector* (target-sequence source-sequence target-start
782 target-end source-start source-end)
783 (when (null target-end) (setq target-end (length target-sequence)))
784 (when (null source-end) (setq source-end (length source-sequence)))
785 (list-replace-from-mumble))
787 (defun vector-replace-from-list* (target-sequence source-sequence target-start
788 target-end source-start source-end)
789 (when (null target-end) (setq target-end (length target-sequence)))
790 (when (null source-end) (setq source-end (length source-sequence)))
791 (mumble-replace-from-list))
793 (defun vector-replace-from-vector* (target-sequence source-sequence
794 target-start target-end source-start
795 source-end)
796 (when (null target-end) (setq target-end (length target-sequence)))
797 (when (null source-end) (setq source-end (length source-sequence)))
798 (mumble-replace-from-mumble))
800 #!+sb-unicode
801 (defun simple-character-string-replace-from-simple-character-string*
802 (target-sequence source-sequence
803 target-start target-end source-start source-end)
804 (declare (type (simple-array character (*)) target-sequence source-sequence))
805 (when (null target-end) (setq target-end (length target-sequence)))
806 (when (null source-end) (setq source-end (length source-sequence)))
807 (mumble-replace-from-mumble))
809 (define-sequence-traverser replace
810 (sequence1 sequence2 &rest args &key start1 end1 start2 end2)
811 #!+sb-doc
812 "Destructively modifies SEQUENCE1 by copying successive elements
813 into it from the SEQUENCE2.
815 Elements are copied to the subseqeuence bounded by START1 and END1,
816 from the subsequence bounded by START2 and END2. If these subsequences
817 are not of the same length, then the shorter length determines how
818 many elements are copied."
819 (declare (truly-dynamic-extent args))
820 (declare (explicit-check sequence1 sequence2 :result))
821 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
822 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
823 ;; these things here so that legacy code gets the names it's
824 ;; expecting. We could use &AUX instead :-/.
825 (target-sequence sequence1)
826 (source-sequence sequence2)
827 (target-start start1)
828 (source-start start2)
829 (target-end (or end1 length1))
830 (source-end (or end2 length2)))
831 (seq-dispatch-checking target-sequence
832 (seq-dispatch-checking source-sequence
833 (return-from replace (list-replace-from-list))
834 (return-from replace (list-replace-from-mumble))
835 nil)
836 (seq-dispatch-checking source-sequence
837 (return-from replace (mumble-replace-from-list))
838 (return-from replace (mumble-replace-from-mumble))
839 nil)
841 ;; If sequence1 is an extended-sequence, we know nothing about sequence2.
842 ;; If sequence1 was a list or vector, then sequence2 is an extended-sequence
843 ;; or not a sequence. Either way, check it.
844 (the sequence
845 (values (apply #'sb!sequence:replace sequence1
846 (the sequence sequence2) args)))))
848 ;;;; REVERSE
849 (defun reverse (sequence)
850 #!+sb-doc
851 "Return a new sequence containing the same elements but in reverse order."
852 (declare (explicit-check))
853 (seq-dispatch-checking sequence
854 (list-reverse sequence)
855 (vector-reverse sequence)
856 ;; The type deriver says that LIST => LIST and VECTOR => VECTOR
857 ;; but does not claim to know anything about extended-sequences.
858 ;; So this could theoretically return any subtype of SEQUENCE
859 ;; given an EXTENDED-SEQUENCE as input. But fndb says this returns
860 ;; a CONSED-SEQUENCE, which precludes non-simple vectors.
861 ;; But a CLOS sequence can apparently decide to return a LIST when
862 ;; reversed. [Is that too weird? Make this EXTENDED-SEQUENCE maybe?]
863 (the consed-sequence (values (sb!sequence:reverse sequence)))))
865 (defun list-reverse (list)
866 (do ((new-list ()))
867 ((endp list) new-list)
868 (push (pop list) new-list)))
870 (defmacro word-specialized-vector-tag-p (tag)
871 `(or
872 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
873 when (and (eq (sb!vm:saetp-n-bits saetp) sb!vm:n-word-bits)
874 (not (eq (sb!vm:saetp-specifier saetp) t)))
875 collect `(eq ,tag ,(sb!vm:saetp-typecode saetp)))))
877 (defun reverse-word-specialized-vector (from to end)
878 (declare (vector from))
879 (do ((length (length to))
880 (left-index 0 (1+ left-index))
881 (right-index end))
882 ((= left-index length))
883 (declare (type index left-index right-index))
884 (decf right-index)
885 (setf (%vector-raw-bits to left-index)
886 (%vector-raw-bits from right-index)))
889 (defun vector-reverse (vector)
890 (declare (vector vector))
891 (let ((length (length vector)))
892 (with-array-data ((vector vector) (start) (end)
893 :check-fill-pointer t)
894 (declare (ignore start))
895 (let* ((tag (%other-pointer-widetag vector))
896 (new-vector (allocate-vector-with-widetag tag length nil)))
897 (cond ((= tag sb!vm:simple-vector-widetag)
898 (do ((left-index 0 (1+ left-index))
899 (right-index end))
900 ((= left-index length))
901 (declare (type index left-index right-index))
902 (decf right-index)
903 (setf (svref new-vector left-index)
904 (svref vector right-index))))
905 ((word-specialized-vector-tag-p tag)
906 (reverse-word-specialized-vector vector new-vector end))
908 (let ((getter (the function (svref %%data-vector-reffers%% tag)))
909 (setter (the function (svref %%data-vector-setters%% tag))))
910 (declare (fixnum length))
911 (do ((forward-index 0 (1+ forward-index))
912 (backward-index (1- end) (1- backward-index)))
913 ((= forward-index length))
914 (declare (fixnum forward-index backward-index))
915 (funcall setter new-vector forward-index
916 (funcall getter vector backward-index))))))
917 new-vector))))
919 ;;;; NREVERSE
921 (defun list-nreverse (list)
922 (do ((1st (cdr list) (if (endp 1st) 1st (cdr 1st)))
923 (2nd list 1st)
924 (3rd '() 2nd))
925 ((atom 2nd) 3rd)
926 (rplacd 2nd 3rd)))
928 (defun nreverse-word-specialized-vector (vector start end)
929 (do ((left-index start (1+ left-index))
930 (right-index (1- end) (1- right-index)))
931 ((<= right-index left-index))
932 (declare (type index left-index right-index))
933 (let ((left (%vector-raw-bits vector left-index))
934 (right (%vector-raw-bits vector right-index)))
935 (setf (%vector-raw-bits vector left-index) right
936 (%vector-raw-bits vector right-index) left)))
937 vector)
939 (defun vector-nreverse (vector)
940 (declare (vector vector))
941 (when (> (length vector) 1)
942 (with-array-data ((vector vector) (start) (end)
943 :check-fill-pointer t)
944 (let ((tag (%other-pointer-widetag vector)))
945 (cond ((= tag sb!vm:simple-vector-widetag)
946 (do ((left-index start (1+ left-index))
947 (right-index (1- end) (1- right-index)))
948 ((<= right-index left-index))
949 (declare (type index left-index right-index))
950 (let ((left (svref vector left-index))
951 (right (svref vector right-index)))
952 (setf (svref vector left-index) right
953 (svref vector right-index) left))))
954 ((word-specialized-vector-tag-p tag)
955 (nreverse-word-specialized-vector vector start end))
957 (let* ((getter (the function (svref %%data-vector-reffers%% tag)))
958 (setter (the function (svref %%data-vector-setters%% tag))))
959 (do ((left-index start (1+ left-index))
960 (right-index (1- end) (1- right-index)))
961 ((<= right-index left-index))
962 (declare (type index left-index right-index))
963 (let ((left (funcall getter vector left-index))
964 (right (funcall getter vector right-index)))
965 (funcall setter vector left-index right)
966 (funcall setter vector right-index left)))))))))
967 vector)
969 (defun nreverse (sequence)
970 #!+sb-doc
971 "Return a sequence of the same elements in reverse order; the argument
972 is destroyed."
973 (declare (explicit-check))
974 (seq-dispatch-checking sequence
975 (list-nreverse sequence)
976 (vector-nreverse sequence)
977 ;; The type deriver for this is 'result-type-first-arg',
978 ;; meaning it should return definitely an EXTENDED-SEQUENCE
979 ;; and not a list or vector.
980 (the extended-sequence (values (sb!sequence:nreverse sequence)))))
983 (defmacro sb!sequence:dosequence ((element sequence &optional return) &body body)
984 #!+sb-doc
985 "Executes BODY with ELEMENT subsequently bound to each element of
986 SEQUENCE, then returns RETURN."
987 (multiple-value-bind (forms decls) (parse-body body nil)
988 (once-only ((sequence sequence))
989 (with-unique-names (state limit from-end step endp elt)
990 `(block nil
991 (seq-dispatch ,sequence
992 (dolist (,element ,sequence ,return) ,@body)
993 (do-vector-data (,element ,sequence ,return) ,@body)
994 (multiple-value-bind (,state ,limit ,from-end ,step ,endp ,elt)
995 (sb!sequence:make-sequence-iterator ,sequence)
996 (declare (function ,step ,endp ,elt))
997 (do ((,state ,state (funcall ,step ,sequence ,state ,from-end)))
998 ((funcall ,endp ,sequence ,state ,limit ,from-end)
999 (let ((,element nil))
1000 ,@(filter-dolist-declarations decls)
1001 (declare (ignorable ,element))
1002 ,return))
1003 (let ((,element (funcall ,elt ,sequence ,state)))
1004 ,@decls
1005 (tagbody
1006 ,@forms))))))))))
1009 ;;;; CONCATENATE
1011 (defun concatenate (result-type &rest sequences)
1012 #!+sb-doc
1013 "Return a new sequence of all the argument sequences concatenated together
1014 which shares no structure with the original argument sequences of the
1015 specified RESULT-TYPE."
1016 (declare (explicit-check)
1017 (dynamic-extent sequences))
1018 (flet ((concat-to-simple* (type-spec sequences)
1019 (do ((seqs sequences (cdr seqs))
1020 (total-length 0)
1021 (lengths ()))
1022 ((null seqs)
1023 (do ((sequences sequences (cdr sequences))
1024 (lengths lengths (cdr lengths))
1025 (index 0)
1026 (result (make-sequence type-spec total-length)))
1027 ((= index total-length) result)
1028 (declare (fixnum index))
1029 (let ((sequence (car sequences)))
1030 (sb!sequence:dosequence (e sequence)
1031 (setf (aref result index) e)
1032 (incf index)))))
1033 (let ((length (length (car seqs))))
1034 (declare (fixnum length))
1035 (setq lengths (nconc lengths (list length)))
1036 (setq total-length (+ total-length length))))))
1037 (case result-type
1038 ;; Pick up some common cases first
1039 (list
1040 (apply #'%concatenate-to-list sequences))
1041 ((vector simple-vector)
1042 (apply #'%concatenate-to-simple-vector sequences))
1043 #!+sb-unicode
1044 ((string simple-string)
1045 (apply #'%concatenate-to-string sequences))
1046 ((simple-base-string #!-sb-unicode string #!-sb-unicode simple-string)
1047 (apply #'%concatenate-to-base-string sequences))
1049 (let ((type (specifier-type result-type)))
1050 (cond
1051 ((csubtypep type (specifier-type 'list))
1052 (cond
1053 ((type= type (specifier-type 'list))
1054 (apply #'%concatenate-to-list sequences))
1055 ((eq type *empty-type*)
1056 (bad-sequence-type-error nil))
1057 ((type= type (specifier-type 'null))
1058 (unless (every #'emptyp sequences)
1059 (sequence-type-length-mismatch-error
1060 type (reduce #'+ sequences :key #'length))) ; FIXME: circular list issues.
1061 '())
1062 ((cons-type-p type)
1063 (multiple-value-bind (min exactp)
1064 (sb!kernel::cons-type-length-info type)
1065 (let ((length (reduce #'+ sequences :key #'length)))
1066 (if exactp
1067 (unless (= length min)
1068 (sequence-type-length-mismatch-error type length))
1069 (unless (>= length min)
1070 (sequence-type-length-mismatch-error type length)))
1071 (apply #'%concatenate-to-list sequences))))
1072 (t (sequence-type-too-hairy (type-specifier type)))))
1073 ((csubtypep type (specifier-type 'vector))
1074 (concat-to-simple* result-type sequences))
1075 ((when-extended-sequence-type
1076 (result-type type :expandedp nil :prototype prototype)
1077 ;; This function has the EXPLICIT-CHECK declaration,
1078 ;; so we manually assert that it returns a SEQUENCE.
1079 (the extended-sequence
1080 (apply #'sb!sequence:concatenate prototype sequences))))
1082 (bad-sequence-type-error result-type))))))))
1084 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
1085 ;;; CONCATENATE 'STRING &co into these.
1086 (macrolet ((def (name element-type &rest dispatch)
1087 `(defun ,name (&rest sequences)
1088 (declare (explicit-check)
1089 (optimize (sb!c::insert-array-bounds-checks 0)))
1090 (let ((length 0))
1091 (declare (index length))
1092 (do-rest-arg ((seq) sequences)
1093 (incf length (length seq)))
1094 (let ((result (make-array length :element-type ',element-type))
1095 (start 0))
1096 (declare (index start))
1097 (do-rest-arg ((seq) sequences)
1098 (string-dispatch (,@dispatch t)
1100 (let ((length (length seq)))
1101 (replace result seq :start1 start)
1102 (incf start length))))
1103 result)))))
1104 #!+sb-unicode
1105 (def %concatenate-to-string character
1106 (simple-array character (*)) (simple-array base-char (*)))
1107 (def %concatenate-to-base-string base-char
1108 (simple-array base-char (*)) #!+sb-unicode (simple-array character (*)))
1109 (def %concatenate-to-simple-vector t simple-vector))
1111 (defun %concatenate-to-list (&rest sequences)
1112 (declare (explicit-check))
1113 (let* ((result (list nil))
1114 (splice result))
1115 (do-rest-arg ((sequence) sequences)
1116 (sb!sequence:dosequence (e sequence)
1117 (setf splice (cdr (rplacd splice (list e))))))
1118 (cdr result)))
1120 (defun %concatenate-to-vector (widetag &rest sequences)
1121 (declare (explicit-check))
1122 (let ((length 0))
1123 (declare (index length))
1124 (do-rest-arg ((seq) sequences)
1125 (incf length (length seq)))
1126 (let ((result (allocate-vector-with-widetag widetag length nil))
1127 (setter (the function (svref %%data-vector-setters%% widetag)))
1128 (index 0))
1129 (declare (index index))
1130 (do-rest-arg ((seq) sequences)
1131 (sb!sequence:dosequence (e seq)
1132 (funcall setter result index e)
1133 (incf index)))
1134 result)))
1136 ;;;; MAP
1138 ;;; helper functions to handle arity-1 subcases of MAP
1139 (defun %map-to-list-arity-1 (fun sequence)
1140 (declare (explicit-check))
1141 (let ((reversed-result nil)
1142 (really-fun (%coerce-callable-to-fun fun)))
1143 (sb!sequence:dosequence (element sequence)
1144 (push (funcall really-fun element)
1145 reversed-result))
1146 (nreverse reversed-result)))
1147 (defun %map-to-simple-vector-arity-1 (fun sequence)
1148 (declare (explicit-check))
1149 (let ((result (make-array (length sequence)))
1150 (index 0)
1151 (really-fun (%coerce-callable-to-fun fun)))
1152 (declare (type index index))
1153 (sb!sequence:dosequence (element sequence)
1154 (setf (aref result index)
1155 (funcall really-fun element))
1156 (incf index))
1157 result))
1158 (defun %map-for-effect-arity-1 (fun sequence)
1159 (declare (explicit-check))
1160 (let ((really-fun (%coerce-callable-to-fun fun)))
1161 (sb!sequence:dosequence (element sequence)
1162 (funcall really-fun element)))
1163 nil)
1165 (declaim (maybe-inline %map-for-effect))
1166 (defun %map-for-effect (fun sequences)
1167 (declare (type function fun) (type list sequences))
1168 (let ((%sequences sequences)
1169 (%iters (mapcar (lambda (s)
1170 (seq-dispatch s
1173 (multiple-value-list
1174 (sb!sequence:make-sequence-iterator s))))
1175 sequences))
1176 (%apply-args (make-list (length sequences))))
1177 ;; this is almost efficient (except in the general case where we
1178 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
1179 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
1180 ;; TODO: on x86-64, we do have. Now see if the above remark is true.
1181 (declare (type list %sequences %iters %apply-args))
1182 (loop
1183 (do ((in-sequences %sequences (cdr in-sequences))
1184 (in-iters %iters (cdr in-iters))
1185 (in-apply-args %apply-args (cdr in-apply-args)))
1186 ((null in-sequences) (apply fun %apply-args))
1187 (let ((i (car in-iters)))
1188 (declare (type (or list index) i))
1189 (cond
1190 ((listp (car in-sequences))
1191 (if (null i)
1192 (return-from %map-for-effect nil)
1193 (setf (car in-apply-args) (car i)
1194 (car in-iters) (cdr i))))
1195 ((typep i 'index)
1196 (let ((v (the vector (car in-sequences))))
1197 (if (>= i (length v))
1198 (return-from %map-for-effect nil)
1199 (setf (car in-apply-args) (aref v i)
1200 (car in-iters) (1+ i)))))
1202 ;; While on one hand this could benefit from a zero-safety ds-bind,
1203 ;; on the other, why not coerce these tuples to vectors or structs?
1204 (destructuring-bind (state limit from-end step endp elt &rest ignore)
1206 (declare (type function step endp elt)
1207 (ignore ignore))
1208 (let ((s (car in-sequences)))
1209 (if (funcall endp s state limit from-end)
1210 (return-from %map-for-effect nil)
1211 (progn
1212 (setf (car in-apply-args) (funcall elt s state))
1213 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
1214 (defun %map-to-list (fun sequences)
1215 (declare (type function fun)
1216 (type list sequences))
1217 (let ((result nil))
1218 (flet ((f (&rest args)
1219 (declare (truly-dynamic-extent args))
1220 (push (apply fun args) result)))
1221 (declare (truly-dynamic-extent #'f))
1222 (%map-for-effect #'f sequences))
1223 (nreverse result)))
1224 (defun %map-to-vector (output-type-spec fun sequences)
1225 (declare (type function fun)
1226 (type list sequences))
1227 (let ((min-len 0))
1228 (flet ((f (&rest args)
1229 (declare (truly-dynamic-extent args))
1230 (declare (ignore args))
1231 (incf min-len)))
1232 (declare (truly-dynamic-extent #'f))
1233 (%map-for-effect #'f sequences))
1234 (let ((result (make-sequence output-type-spec min-len))
1235 (i 0))
1236 (declare (type (simple-array * (*)) result))
1237 (flet ((f (&rest args)
1238 (declare (truly-dynamic-extent args))
1239 (setf (aref result i) (apply fun args))
1240 (incf i)))
1241 (declare (truly-dynamic-extent #'f))
1242 (%map-for-effect #'f sequences))
1243 result)))
1245 ;;; %MAP is just MAP without the final just-to-be-sure check that
1246 ;;; length of the output sequence matches any length specified
1247 ;;; in RESULT-TYPE.
1248 (defun %map (result-type function &rest sequences)
1249 (declare (explicit-check))
1250 (declare (dynamic-extent sequences))
1251 ;; Everything that we end up calling uses %COERCE-TO-CALLABLE
1252 ;; on FUNCTION so we don't need to declare it of type CALLABLE here.
1253 ;; Additionally all the arity-1 mappers use SEQ-DISPATCH which asserts
1254 ;; that the input is a SEQUENCE. Despite SEQ-DISPATCH being "less safe"
1255 ;; than SEQ-DISPATCH-CHECKING, both are in fact equally safe, because
1256 ;; the ARRAY case (which assumes that all arrays are vectors) utilizes
1257 ;; %WITH-ARRAY-DATA/FP which asserts that its input is a vector.
1258 (labels ((slower-map (type)
1259 (let ((really-fun (%coerce-callable-to-fun function)))
1260 (cond
1261 ((eq type *empty-type*)
1262 (%map-for-effect really-fun sequences))
1263 ((csubtypep type (specifier-type 'list))
1264 (%map-to-list really-fun sequences))
1265 ((csubtypep type (specifier-type 'vector))
1266 (%map-to-vector result-type really-fun sequences))
1267 ((when-extended-sequence-type
1268 (result-type type :expandedp nil :prototype prototype)
1269 ;; This function has the EXPLICIT-CHECK
1270 ;; declaration, so we manually assert that it
1271 ;; returns a SEQUENCE.
1272 (the extended-sequence
1273 (apply #'sb!sequence:map
1274 prototype really-fun sequences))))
1276 (bad-sequence-type-error result-type))))))
1277 ;; Handle some easy cases faster
1278 (if (/= (length sequences) 1)
1279 (slower-map (specifier-type result-type))
1280 (let ((first-sequence (fast-&rest-nth 0 sequences)))
1281 (case result-type
1282 ((nil)
1283 (%map-for-effect-arity-1 function first-sequence))
1284 ((list cons)
1285 (%map-to-list-arity-1 function first-sequence))
1286 ((vector simple-vector)
1287 (%map-to-simple-vector-arity-1 function first-sequence))
1289 (let ((type (specifier-type result-type)))
1290 (cond ((eq type *empty-type*)
1291 (%map-for-effect-arity-1 function first-sequence))
1292 ((csubtypep type (specifier-type 'list))
1293 (%map-to-list-arity-1 function first-sequence))
1294 ((csubtypep type (specifier-type '(vector t)))
1295 (%map-to-simple-vector-arity-1 function first-sequence))
1297 (slower-map type))))))))))
1299 (defun map (result-type function first-sequence &rest more-sequences)
1300 (declare (explicit-check))
1301 (let ((result
1302 (apply #'%map result-type function first-sequence more-sequences)))
1303 (if (or (eq result-type 'nil) (typep result result-type))
1304 result
1305 (error 'simple-type-error
1306 :format-control "MAP result ~S is not a sequence of type ~S"
1307 :datum result
1308 :expected-type result-type
1309 :format-arguments (list result result-type)))))
1311 ;;;; MAP-INTO
1313 (defmacro map-into-lambda (sequences params &body body)
1314 (check-type sequences symbol)
1315 `(flet ((f ,params ,@body))
1316 (declare (truly-dynamic-extent #'f))
1317 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1318 ;; hence the awkward flip between MAP and LOOP.
1319 (if ,sequences
1320 (apply #'%map nil #'f ,sequences)
1321 (loop (f)))))
1323 (!define-array-dispatch vector-map-into (data start end fun sequences)
1324 (declare (type index start end)
1325 (type function fun)
1326 (type list sequences))
1327 (declare (explicit-check))
1328 (let ((index start))
1329 (declare (type index index))
1330 (block mapping
1331 (map-into-lambda sequences (&rest args)
1332 (declare (truly-dynamic-extent args))
1333 (when (eql index end)
1334 (return-from mapping))
1335 (setf (aref data index) (apply fun args))
1336 (incf index)))
1337 index))
1339 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1340 ;;; computing the length of the result sequence since we can detect
1341 ;;; the end during mapping (if MAP even gets that far).
1343 ;;; For each result type, define a mapping function which is
1344 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1345 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1346 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1348 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1349 ;;; Because we are manually doing bounds checking with known types,
1350 ;;; safety is turned off for vectors and lists but kept for generic
1351 ;;; sequences.
1352 (defun map-into (result-sequence function &rest sequences)
1353 (declare (optimize (sb!c::check-tag-existence 0)))
1354 (let ((really-fun (%coerce-callable-to-fun function)))
1355 (etypecase result-sequence
1356 (vector
1357 (with-array-data ((data result-sequence) (start) (end)
1358 ;; MAP-INTO ignores fill pointer when mapping
1359 :check-fill-pointer nil)
1360 (let ((new-end (vector-map-into data start end really-fun sequences)))
1361 (when (array-has-fill-pointer-p result-sequence)
1362 (setf (fill-pointer result-sequence) (- new-end start))))))
1363 (list
1364 (let ((node result-sequence))
1365 (declare (type list node))
1366 (map-into-lambda sequences (&rest args)
1367 (declare (truly-dynamic-extent args))
1368 (cond ((null node)
1369 (return-from map-into result-sequence))
1370 ((not (listp (cdr node)))
1371 (error 'simple-type-error
1372 :format-control "~a is not a proper list"
1373 :format-arguments (list result-sequence)
1374 :expected-type 'list
1375 :datum result-sequence)))
1376 (setf (car node) (apply really-fun args))
1377 (setf node (cdr node)))))
1378 (sequence
1379 (multiple-value-bind (iter limit from-end)
1380 (sb!sequence:make-sequence-iterator result-sequence)
1381 (map-into-lambda sequences (&rest args)
1382 (declare (truly-dynamic-extent args) (optimize speed))
1383 (when (sb!sequence:iterator-endp result-sequence
1384 iter limit from-end)
1385 (return-from map-into result-sequence))
1386 (setf (sb!sequence:iterator-element result-sequence iter)
1387 (apply really-fun args))
1388 (setf iter (sb!sequence:iterator-step result-sequence
1389 iter from-end)))))))
1390 result-sequence)
1392 ;;;; REDUCE
1394 (eval-when (:compile-toplevel :execute)
1396 (sb!xc:defmacro mumble-reduce (function
1397 sequence
1399 start
1401 initial-value
1402 ref)
1403 `(do ((index ,start (1+ index))
1404 (value ,initial-value))
1405 ((>= index ,end) value)
1406 (setq value (funcall ,function value
1407 (apply-key ,key (,ref ,sequence index))))))
1409 (sb!xc:defmacro mumble-reduce-from-end (function
1410 sequence
1412 start
1414 initial-value
1415 ref)
1416 `(do ((index (1- ,end) (1- index))
1417 (value ,initial-value)
1418 (terminus (1- ,start)))
1419 ((<= index terminus) value)
1420 (setq value (funcall ,function
1421 (apply-key ,key (,ref ,sequence index))
1422 value))))
1424 (sb!xc:defmacro list-reduce (function
1425 sequence
1427 start
1429 initial-value
1430 ivp)
1431 `(let ((sequence (nthcdr ,start ,sequence)))
1432 (do ((count (if ,ivp ,start (1+ ,start))
1433 (1+ count))
1434 (sequence (if ,ivp sequence (cdr sequence))
1435 (cdr sequence))
1436 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1437 (funcall ,function value (apply-key ,key (car sequence)))))
1438 ((>= count ,end) value))))
1440 (sb!xc:defmacro list-reduce-from-end (function
1441 sequence
1443 start
1445 initial-value
1446 ivp)
1447 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1448 (reverse ,sequence))))
1449 (do ((count (if ,ivp ,start (1+ ,start))
1450 (1+ count))
1451 (sequence (if ,ivp sequence (cdr sequence))
1452 (cdr sequence))
1453 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1454 (funcall ,function (apply-key ,key (car sequence)) value)))
1455 ((>= count ,end) value))))
1457 ) ; EVAL-WHEN
1459 (define-sequence-traverser reduce (function sequence &rest args &key key
1460 from-end start end (initial-value nil ivp))
1461 (declare (type index start)
1462 (truly-dynamic-extent args))
1463 (declare (explicit-check sequence))
1464 (seq-dispatch-checking sequence
1465 (let ((end (or end length)))
1466 (declare (type index end))
1467 (if (= end start)
1468 (if ivp initial-value (funcall function))
1469 (if from-end
1470 (list-reduce-from-end function sequence key start end
1471 initial-value ivp)
1472 (list-reduce function sequence key start end
1473 initial-value ivp))))
1474 (let ((end (or end length)))
1475 (declare (type index end))
1476 (if (= end start)
1477 (if ivp initial-value (funcall function))
1478 (if from-end
1479 (progn
1480 (when (not ivp)
1481 (setq end (1- (the fixnum end)))
1482 (setq initial-value (apply-key key (aref sequence end))))
1483 (mumble-reduce-from-end function sequence key start end
1484 initial-value aref))
1485 (progn
1486 (when (not ivp)
1487 (setq initial-value (apply-key key (aref sequence start)))
1488 (setq start (1+ start)))
1489 (mumble-reduce function sequence key start end
1490 initial-value aref)))))
1491 (apply #'sb!sequence:reduce function sequence args)))
1493 ;;;; DELETE
1495 (eval-when (:compile-toplevel :execute)
1497 (sb!xc:defmacro mumble-delete (pred)
1498 `(do ((index start (1+ index))
1499 (jndex start)
1500 (number-zapped 0))
1501 ((or (= index (the fixnum end)) (= number-zapped count))
1502 (do ((index index (1+ index)) ; Copy the rest of the vector.
1503 (jndex jndex (1+ jndex)))
1504 ((= index (the fixnum length))
1505 (shrink-vector sequence jndex))
1506 (declare (fixnum index jndex))
1507 (setf (aref sequence jndex) (aref sequence index))))
1508 (declare (fixnum index jndex number-zapped))
1509 (setf (aref sequence jndex) (aref sequence index))
1510 (if ,pred
1511 (incf number-zapped)
1512 (incf jndex))))
1514 (sb!xc:defmacro mumble-delete-from-end (pred)
1515 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1516 (number-zapped 0)
1517 (losers ())
1518 this-element
1519 (terminus (1- start)))
1520 ((or (= index terminus) (= number-zapped count))
1521 (do ((losers losers) ; Delete the losers.
1522 (index start (1+ index))
1523 (jndex start))
1524 ((or (null losers) (= index (the fixnum end)))
1525 (do ((index index (1+ index)) ; Copy the rest of the vector.
1526 (jndex jndex (1+ jndex)))
1527 ((= index (the fixnum length))
1528 (shrink-vector sequence jndex))
1529 (declare (fixnum index jndex))
1530 (setf (aref sequence jndex) (aref sequence index))))
1531 (declare (fixnum index jndex))
1532 (setf (aref sequence jndex) (aref sequence index))
1533 (if (= index (the fixnum (car losers)))
1534 (pop losers)
1535 (incf jndex))))
1536 (declare (fixnum index number-zapped terminus))
1537 (setq this-element (aref sequence index))
1538 (when ,pred
1539 (incf number-zapped)
1540 (push index losers))))
1542 (sb!xc:defmacro normal-mumble-delete ()
1543 `(mumble-delete
1544 (if test-not
1545 (not (funcall test-not item (apply-key key (aref sequence index))))
1546 (funcall test item (apply-key key (aref sequence index))))))
1548 (sb!xc:defmacro normal-mumble-delete-from-end ()
1549 `(mumble-delete-from-end
1550 (if test-not
1551 (not (funcall test-not item (apply-key key this-element)))
1552 (funcall test item (apply-key key this-element)))))
1554 (sb!xc:defmacro list-delete (pred)
1555 `(let ((handle (cons nil sequence)))
1556 (declare (truly-dynamic-extent handle))
1557 (do* ((previous (nthcdr start handle))
1558 (current (cdr previous) (cdr current))
1559 (index start (1+ index))
1560 (number-zapped 0))
1561 ((or (= index end) (= number-zapped count))
1562 (cdr handle))
1563 (declare (index index number-zapped))
1564 (cond (,pred
1565 (rplacd previous (cdr current))
1566 (incf number-zapped))
1568 (pop previous))))))
1570 (sb!xc:defmacro list-delete-from-end (pred)
1571 `(let* ((reverse (nreverse sequence))
1572 (handle (cons nil reverse)))
1573 (declare (truly-dynamic-extent handle))
1574 (do* ((previous (nthcdr (- length end) handle))
1575 (current (cdr previous) (cdr current))
1576 (index start (1+ index))
1577 (number-zapped 0))
1578 ((or (= index end) (= number-zapped count))
1579 (nreverse (cdr handle)))
1580 (declare (index index number-zapped))
1581 (cond (,pred
1582 (rplacd previous (cdr current))
1583 (incf number-zapped))
1585 (pop previous))))))
1587 (sb!xc:defmacro normal-list-delete ()
1588 '(list-delete
1589 (if test-not
1590 (not (funcall test-not item (apply-key key (car current))))
1591 (funcall test item (apply-key key (car current))))))
1593 (sb!xc:defmacro normal-list-delete-from-end ()
1594 '(list-delete-from-end
1595 (if test-not
1596 (not (funcall test-not item (apply-key key (car current))))
1597 (funcall test item (apply-key key (car current))))))
1599 ) ; EVAL-WHEN
1601 (define-sequence-traverser delete
1602 (item sequence &rest args &key from-end test test-not start
1603 end count key)
1604 #!+sb-doc
1605 "Return a sequence formed by destructively removing the specified ITEM from
1606 the given SEQUENCE."
1607 (declare (type fixnum start)
1608 (truly-dynamic-extent args))
1609 (declare (explicit-check sequence :result))
1610 (seq-dispatch-checking=>seq sequence
1611 (let ((end (or end length)))
1612 (declare (type index end))
1613 (if from-end
1614 (normal-list-delete-from-end)
1615 (normal-list-delete)))
1616 (let ((end (or end length)))
1617 (declare (type index end))
1618 (if from-end
1619 (normal-mumble-delete-from-end)
1620 (normal-mumble-delete)))
1621 (apply #'sb!sequence:delete item sequence args)))
1623 (eval-when (:compile-toplevel :execute)
1625 (sb!xc:defmacro if-mumble-delete ()
1626 `(mumble-delete
1627 (funcall predicate (apply-key key (aref sequence index)))))
1629 (sb!xc:defmacro if-mumble-delete-from-end ()
1630 `(mumble-delete-from-end
1631 (funcall predicate (apply-key key this-element))))
1633 (sb!xc:defmacro if-list-delete ()
1634 '(list-delete
1635 (funcall predicate (apply-key key (car current)))))
1637 (sb!xc:defmacro if-list-delete-from-end ()
1638 '(list-delete-from-end
1639 (funcall predicate (apply-key key (car current)))))
1641 ) ; EVAL-WHEN
1643 (define-sequence-traverser delete-if
1644 (predicate sequence &rest args &key from-end start key end count)
1645 #!+sb-doc
1646 "Return a sequence formed by destructively removing the elements satisfying
1647 the specified PREDICATE from the given SEQUENCE."
1648 (declare (type fixnum start)
1649 (truly-dynamic-extent args))
1650 (declare (explicit-check sequence :result))
1651 (seq-dispatch-checking=>seq sequence
1652 (let ((end (or end length)))
1653 (declare (type index end))
1654 (if from-end
1655 (if-list-delete-from-end)
1656 (if-list-delete)))
1657 (let ((end (or end length)))
1658 (declare (type index end))
1659 (if from-end
1660 (if-mumble-delete-from-end)
1661 (if-mumble-delete)))
1662 (apply #'sb!sequence:delete-if predicate sequence args)))
1664 (eval-when (:compile-toplevel :execute)
1666 (sb!xc:defmacro if-not-mumble-delete ()
1667 `(mumble-delete
1668 (not (funcall predicate (apply-key key (aref sequence index))))))
1670 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1671 `(mumble-delete-from-end
1672 (not (funcall predicate (apply-key key this-element)))))
1674 (sb!xc:defmacro if-not-list-delete ()
1675 '(list-delete
1676 (not (funcall predicate (apply-key key (car current))))))
1678 (sb!xc:defmacro if-not-list-delete-from-end ()
1679 '(list-delete-from-end
1680 (not (funcall predicate (apply-key key (car current))))))
1682 ) ; EVAL-WHEN
1684 (define-sequence-traverser delete-if-not
1685 (predicate sequence &rest args &key from-end start end key count)
1686 #!+sb-doc
1687 "Return a sequence formed by destructively removing the elements not
1688 satisfying the specified PREDICATE from the given SEQUENCE."
1689 (declare (type fixnum start)
1690 (truly-dynamic-extent args))
1691 (declare (explicit-check sequence :result))
1692 (seq-dispatch-checking=>seq sequence
1693 (let ((end (or end length)))
1694 (declare (type index end))
1695 (if from-end
1696 (if-not-list-delete-from-end)
1697 (if-not-list-delete)))
1698 (let ((end (or end length)))
1699 (declare (type index end))
1700 (if from-end
1701 (if-not-mumble-delete-from-end)
1702 (if-not-mumble-delete)))
1703 (apply #'sb!sequence:delete-if-not predicate sequence args)))
1705 ;;;; REMOVE
1707 (eval-when (:compile-toplevel :execute)
1709 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1710 ;;; satisfies the predicate.
1711 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1712 `(do ((index ,begin (,bump index))
1713 (result
1714 (do ((index ,left (,bump index))
1715 (result (%make-sequence-like sequence length)))
1716 ((= index (the fixnum ,begin)) result)
1717 (declare (fixnum index))
1718 (setf (aref result index) (aref sequence index))))
1719 (new-index ,begin)
1720 (number-zapped 0)
1721 (this-element))
1722 ((or (= index (the fixnum ,finish))
1723 (= number-zapped count))
1724 (do ((index index (,bump index))
1725 (new-index new-index (,bump new-index)))
1726 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1727 (declare (fixnum index new-index))
1728 (setf (aref result new-index) (aref sequence index))))
1729 (declare (fixnum index new-index number-zapped))
1730 (setq this-element (aref sequence index))
1731 (cond (,pred (incf number-zapped))
1732 (t (setf (aref result new-index) this-element)
1733 (setq new-index (,bump new-index))))))
1735 (sb!xc:defmacro mumble-remove (pred)
1736 `(mumble-remove-macro 1+ 0 start end length ,pred))
1738 (sb!xc:defmacro mumble-remove-from-end (pred)
1739 `(let ((sequence (copy-seq sequence)))
1740 (mumble-delete-from-end ,pred)))
1742 (sb!xc:defmacro normal-mumble-remove ()
1743 `(mumble-remove
1744 (if test-not
1745 (not (funcall test-not item (apply-key key this-element)))
1746 (funcall test item (apply-key key this-element)))))
1748 (sb!xc:defmacro normal-mumble-remove-from-end ()
1749 `(mumble-remove-from-end
1750 (if test-not
1751 (not (funcall test-not item (apply-key key this-element)))
1752 (funcall test item (apply-key key this-element)))))
1754 (sb!xc:defmacro if-mumble-remove ()
1755 `(mumble-remove (funcall predicate (apply-key key this-element))))
1757 (sb!xc:defmacro if-mumble-remove-from-end ()
1758 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1760 (sb!xc:defmacro if-not-mumble-remove ()
1761 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1763 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1764 `(mumble-remove-from-end
1765 (not (funcall predicate (apply-key key this-element)))))
1767 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1768 ;;; the predicate.
1769 (sb!xc:defmacro list-remove-macro (pred reverse?)
1770 `(let* ((sequence ,(if reverse?
1771 '(reverse (the list sequence))
1772 'sequence))
1773 (%start ,(if reverse? '(- length end) 'start))
1774 (%end ,(if reverse? '(- length start) 'end))
1775 (splice (list nil))
1776 (tail (and (/= %end length)
1777 (nthcdr %end sequence)))
1778 (results ,(if reverse?
1779 ;; It's already copied by REVERSE, so it can
1780 ;; be modified here
1781 `(if (plusp %start)
1782 (let* ((tail (nthcdr (1- %start) sequence))
1783 (remaining (cdr tail)))
1784 (setf (cdr tail) nil)
1785 (prog1 splice
1786 (rplacd splice sequence)
1787 (setf splice tail
1788 sequence remaining)))
1789 splice)
1790 `(do ((index 0 (1+ index))
1791 (before-start splice))
1792 ((= index (the fixnum %start)) before-start)
1793 (declare (fixnum index))
1794 (setf splice
1795 (cdr (rplacd splice (list (pop sequence)))))))))
1796 (declare (truly-dynamic-extent splice))
1797 (do ((this-element)
1798 (number-zapped 0))
1799 ((cond ((eq tail sequence)
1800 (rplacd splice tail)
1802 ((= number-zapped count)
1803 (rplacd splice sequence)
1805 ,(if reverse?
1806 '(nreverse (the list (cdr results)))
1807 '(cdr results)))
1808 (declare (index number-zapped))
1809 (setf this-element (pop sequence))
1810 (if ,pred
1811 (incf number-zapped)
1812 (setf splice (cdr (rplacd splice (list this-element))))))))
1814 (sb!xc:defmacro list-remove (pred)
1815 `(list-remove-macro ,pred nil))
1817 (sb!xc:defmacro list-remove-from-end (pred)
1818 `(list-remove-macro ,pred t))
1820 (sb!xc:defmacro normal-list-remove ()
1821 `(list-remove
1822 (if test-not
1823 (not (funcall test-not item (apply-key key this-element)))
1824 (funcall test item (apply-key key this-element)))))
1826 (sb!xc:defmacro normal-list-remove-from-end ()
1827 `(list-remove-from-end
1828 (if test-not
1829 (not (funcall test-not item (apply-key key this-element)))
1830 (funcall test item (apply-key key this-element)))))
1832 (sb!xc:defmacro if-list-remove ()
1833 `(list-remove
1834 (funcall predicate (apply-key key this-element))))
1836 (sb!xc:defmacro if-list-remove-from-end ()
1837 `(list-remove-from-end
1838 (funcall predicate (apply-key key this-element))))
1840 (sb!xc:defmacro if-not-list-remove ()
1841 `(list-remove
1842 (not (funcall predicate (apply-key key this-element)))))
1844 (sb!xc:defmacro if-not-list-remove-from-end ()
1845 `(list-remove-from-end
1846 (not (funcall predicate (apply-key key this-element)))))
1848 ) ; EVAL-WHEN
1850 (define-sequence-traverser remove
1851 (item sequence &rest args &key from-end test test-not start
1852 end count key)
1853 #!+sb-doc
1854 "Return a copy of SEQUENCE with elements satisfying the test (default is
1855 EQL) with ITEM removed."
1856 (declare (type fixnum start)
1857 (truly-dynamic-extent args))
1858 (declare (explicit-check sequence :result))
1859 (seq-dispatch-checking=>seq sequence
1860 (let ((end (or end length)))
1861 (declare (type index end))
1862 (if from-end
1863 (normal-list-remove-from-end)
1864 (normal-list-remove)))
1865 (let ((end (or end length)))
1866 (declare (type index end))
1867 (if from-end
1868 (normal-mumble-remove-from-end)
1869 (normal-mumble-remove)))
1870 (apply #'sb!sequence:remove item sequence args)))
1872 (define-sequence-traverser remove-if
1873 (predicate sequence &rest args &key from-end start end count key)
1874 #!+sb-doc
1875 "Return a copy of sequence with elements satisfying PREDICATE removed."
1876 (declare (type fixnum start)
1877 (truly-dynamic-extent args))
1878 (declare (explicit-check sequence :result))
1879 (seq-dispatch-checking=>seq sequence
1880 (let ((end (or end length)))
1881 (declare (type index end))
1882 (if from-end
1883 (if-list-remove-from-end)
1884 (if-list-remove)))
1885 (let ((end (or end length)))
1886 (declare (type index end))
1887 (if from-end
1888 (if-mumble-remove-from-end)
1889 (if-mumble-remove)))
1890 (apply #'sb!sequence:remove-if predicate sequence args)))
1892 (define-sequence-traverser remove-if-not
1893 (predicate sequence &rest args &key from-end start end count key)
1894 #!+sb-doc
1895 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1896 (declare (type fixnum start)
1897 (truly-dynamic-extent args))
1898 (declare (explicit-check sequence :result))
1899 (seq-dispatch-checking=>seq sequence
1900 (let ((end (or end length)))
1901 (declare (type index end))
1902 (if from-end
1903 (if-not-list-remove-from-end)
1904 (if-not-list-remove)))
1905 (let ((end (or end length)))
1906 (declare (type index end))
1907 (if from-end
1908 (if-not-mumble-remove-from-end)
1909 (if-not-mumble-remove)))
1910 (apply #'sb!sequence:remove-if-not predicate sequence args)))
1912 ;;;; REMOVE-DUPLICATES
1914 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1915 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1916 ;;; if we look into the already copied structure (from after :start) and see
1917 ;;; the item. If we check from beginning we check into the rest of the
1918 ;;; original list up to the :end marker (this we have to do by running a
1919 ;;; do loop down the list that far and using our test.
1920 (defun list-remove-duplicates* (list test test-not start end key from-end)
1921 (declare (fixnum start)
1922 (list list))
1923 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1924 (splice result)
1925 (current list)
1926 (length (length list))
1927 (end (or end length))
1928 (whole (= end length))
1929 (hash (and (> (- end start) 20)
1930 (not key)
1931 (not test-not)
1932 (hash-table-test-p test)
1933 (make-hash-table :test test :size (- end start))))
1934 (tail (and (not whole)
1935 (nthcdr end list))))
1936 (declare (truly-dynamic-extent result))
1937 (do ((index 0 (1+ index)))
1938 ((= index start))
1939 (declare (fixnum index))
1940 (setq splice (cdr (rplacd splice (list (car current)))))
1941 (setq current (cdr current)))
1942 (if hash
1943 (do ()
1944 ((eq current tail))
1945 ;; The hash table contains links from values that are
1946 ;; already in result to the cons cell *preceding* theirs
1947 ;; in the list. That is, for each value v in the list,
1948 ;; v and (cadr (gethash v hash)) are equal under TEST.
1949 (let ((prev (gethash (car current) hash)))
1950 (cond
1951 ((not prev)
1952 (setf (gethash (car current) hash) splice)
1953 (setq splice (cdr (rplacd splice (list (car current))))))
1954 ((not from-end)
1955 (let* ((old (cdr prev))
1956 (next (cdr old)))
1957 (if next
1958 (let ((next-val (car next)))
1959 ;; (assert (eq (gethash next-val hash) old))
1960 (setf (cdr prev) next
1961 (gethash next-val hash) prev
1962 (gethash (car current) hash) splice
1963 splice (cdr (rplacd splice (list (car current))))))
1964 (setf (car old) (car current)))))))
1965 (setq current (cdr current)))
1966 (let ((testp test) ;; for with-member-test
1967 (notp test-not))
1968 (with-member-test (member-test
1969 ((and (not from-end)
1970 (not whole))
1971 (if notp
1972 (if key
1973 (lambda (x y key test)
1974 (not (funcall (truly-the function test) x
1975 (funcall (truly-the function key) y))))
1976 (lambda (x y key test)
1977 (declare (ignore key))
1978 (not (funcall (truly-the function test) x y))))
1979 (if key
1980 (lambda (x y key test)
1981 (funcall (truly-the function test) x
1982 (funcall (truly-the function key) y)))
1983 (lambda (x y key test)
1984 (declare (ignore key))
1985 (funcall (truly-the function test) x y))))))
1986 (do ((copied (nthcdr start result)))
1987 ((eq current tail))
1988 (let ((elt (car current)))
1989 (when (cond (from-end
1990 (not (funcall member-test elt (cdr copied) key test)))
1991 (whole
1992 (not (funcall member-test elt (cdr current) key test)))
1994 (do ((it (apply-key key elt))
1995 (l (cdr current) (cdr l)))
1996 ((eq l tail)
1998 (when (funcall member-test it (car l) key test)
1999 (return)))))
2000 (setf splice (cdr (rplacd splice (list elt))))))
2001 (pop current)))))
2002 (rplacd splice tail)
2003 (cdr result)))
2005 (defun vector-remove-duplicates* (vector test test-not start end key from-end
2006 &optional (length (length vector)))
2007 (declare (vector vector) (fixnum start length))
2008 (when (null end) (setf end (length vector)))
2009 (let ((result (%make-sequence-like vector length))
2010 (index 0)
2011 (jndex start))
2012 (declare (fixnum index jndex))
2013 (do ()
2014 ((= index start))
2015 (setf (aref result index) (aref vector index))
2016 (setq index (1+ index)))
2017 (do ((elt))
2018 ((= index end))
2019 (setq elt (aref vector index))
2020 (unless (or (and from-end
2021 (if test-not
2022 (position (apply-key key elt) result
2023 :start start :end jndex
2024 :test-not test-not :key key)
2025 (position (apply-key key elt) result
2026 :start start :end jndex
2027 :test test :key key)))
2028 (and (not from-end)
2029 (if test-not
2030 (position (apply-key key elt) vector
2031 :start (1+ index) :end end
2032 :test-not test-not :key key)
2033 (position (apply-key key elt) vector
2034 :start (1+ index) :end end
2035 :test test :key key))))
2036 (setf (aref result jndex) elt)
2037 (setq jndex (1+ jndex)))
2038 (setq index (1+ index)))
2039 (do ()
2040 ((= index length))
2041 (setf (aref result jndex) (aref vector index))
2042 (setq index (1+ index))
2043 (setq jndex (1+ jndex)))
2044 (%shrink-vector result jndex)))
2046 (define-sequence-traverser remove-duplicates
2047 (sequence &rest args &key test test-not start end from-end key)
2048 #!+sb-doc
2049 "The elements of SEQUENCE are compared pairwise, and if any two match,
2050 the one occurring earlier is discarded, unless FROM-END is true, in
2051 which case the one later in the sequence is discarded. The resulting
2052 sequence is returned.
2054 The :TEST-NOT argument is deprecated."
2055 (declare (fixnum start)
2056 (truly-dynamic-extent args))
2057 (declare (explicit-check sequence :result))
2058 (seq-dispatch-checking=>seq sequence
2059 (if sequence
2060 (list-remove-duplicates* sequence test test-not
2061 start end key from-end))
2062 (vector-remove-duplicates* sequence test test-not start end key from-end)
2063 (apply #'sb!sequence:remove-duplicates sequence args)))
2065 ;;;; DELETE-DUPLICATES
2066 (defun list-delete-duplicates* (list test test-not key from-end start end)
2067 (declare (index start)
2068 (list list))
2069 (let* ((handle (cons nil list))
2070 (from-end-start (and from-end
2071 (nthcdr (1+ start) handle)))
2072 (length (length list))
2073 (end (or end length))
2074 (tail (and (/= length (truly-the fixnum end))
2075 (nthcdr end list))))
2076 (declare (truly-dynamic-extent handle))
2077 (do* ((previous (nthcdr start handle))
2078 (current (cdr previous) (cdr current)))
2079 ((eq current tail)
2080 (cdr handle))
2081 (if (do ((end (if from-end
2082 current
2083 tail))
2084 (x (if from-end
2085 from-end-start
2086 (cdr current))
2087 (cdr x)))
2088 ((eq x end))
2089 (if (if test-not
2090 (not (funcall (truly-the function test-not)
2091 (apply-key-function key (car current))
2092 (apply-key-function key (car x))))
2093 (funcall (truly-the function test)
2094 (apply-key-function key (car current))
2095 (apply-key-function key (car x))))
2096 (return t)))
2097 (rplacd previous (cdr current))
2098 (pop previous)))))
2100 (defun vector-delete-duplicates* (vector test test-not key from-end start end
2101 &optional (length (length vector)))
2102 (declare (vector vector) (fixnum start length))
2103 (when (null end) (setf end (length vector)))
2104 (do ((index start (1+ index))
2105 (jndex start))
2106 ((= index end)
2107 (do ((index index (1+ index)) ; copy the rest of the vector
2108 (jndex jndex (1+ jndex)))
2109 ((= index length)
2110 (shrink-vector vector jndex))
2111 (setf (aref vector jndex) (aref vector index))))
2112 (declare (fixnum index jndex))
2113 (setf (aref vector jndex) (aref vector index))
2114 (unless (if 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-not test-not)
2119 (position (apply-key key (aref vector index)) vector :key key
2120 :start (if from-end start (1+ index))
2121 :end (if from-end jndex end)
2122 :test test))
2123 (setq jndex (1+ jndex)))))
2125 (define-sequence-traverser delete-duplicates
2126 (sequence &rest args &key test test-not start end from-end key)
2127 #!+sb-doc
2128 "The elements of SEQUENCE are examined, and if any two match, one is
2129 discarded. The resulting sequence, which may be formed by destroying the
2130 given sequence, is returned.
2132 The :TEST-NOT argument is deprecated."
2133 (declare (truly-dynamic-extent args))
2134 (declare (explicit-check sequence :result))
2135 (seq-dispatch-checking=>seq sequence
2136 (when sequence
2137 (list-delete-duplicates* sequence test test-not
2138 key from-end start end))
2139 (vector-delete-duplicates* sequence test test-not key from-end start end)
2140 (apply #'sb!sequence:delete-duplicates sequence args)))
2142 ;;;; SUBSTITUTE
2144 (defun list-substitute* (pred new list start end count key test test-not old)
2145 (declare (fixnum start end count)
2146 (type (or null function) key)
2147 (optimize speed))
2148 (let* ((result (list nil))
2149 (test (or test-not test))
2150 (test-not (or test-not
2151 (eq pred 'if-not)))
2153 (splice result)
2154 (list list)) ; Get a local list for a stepper.
2155 (declare (function test))
2156 (do ((index 0 (1+ index)))
2157 ((= index start))
2158 (declare (fixnum index))
2159 (setf splice (cdr (rplacd splice (list (car list))))
2160 list (cdr list)))
2161 (do ((index start (1+ index)))
2162 ((or (= index end) (null list) (= count 0)))
2163 (declare (fixnum index))
2164 (setf elt (car list)
2165 splice
2166 (cdr (rplacd splice
2167 (list
2168 (cond ((let* ((elt (apply-key key elt))
2169 (value (if (eq pred 'normal)
2170 (funcall test old elt)
2171 (funcall test elt))))
2172 (if test-not
2173 (not value)
2174 value))
2175 (decf count)
2176 new)
2177 (t elt)))))
2178 list (cdr list)))
2179 (do ()
2180 ((null list))
2181 (setf splice (cdr (rplacd splice (list (car list))))
2182 list (cdr list)))
2183 (cdr result)))
2185 ;;; Replace old with new in sequence moving from left to right by incrementer
2186 ;;; on each pass through the loop. Called by all three substitute functions.
2187 (defun vector-substitute* (pred new sequence incrementer left right length
2188 start end count key test test-not old)
2189 (declare (fixnum start count end incrementer right)
2190 (type (or null function) key))
2191 (let* ((result (make-vector-like sequence length))
2192 (getter (the function (svref %%data-vector-reffers%%
2193 (%other-pointer-widetag sequence))))
2194 (setter (the function (svref %%data-vector-setters%%
2195 (%other-pointer-widetag result))))
2196 (test (or test-not test))
2197 (test-not (or test-not
2198 (eq pred 'if-not)))
2199 (index left))
2200 (declare (fixnum index)
2201 (function test))
2202 (do ()
2203 ((= index start))
2204 (funcall setter result index
2205 (funcall getter sequence index))
2206 (incf index incrementer))
2207 (do ((elt))
2208 ((or (= index end) (= count 0)))
2209 (setf elt (funcall getter sequence index))
2210 (funcall setter result index
2211 (cond ((let* ((elt (apply-key key elt))
2212 (value (if (eq pred 'normal)
2213 (funcall test old elt)
2214 (funcall test elt))))
2215 (if test-not
2216 (not value)
2217 value))
2218 (decf count)
2219 new)
2220 (t elt)))
2221 (incf index incrementer))
2222 (do ()
2223 ((= index right))
2224 (funcall setter result index
2225 (funcall getter sequence index))
2226 (incf index incrementer))
2227 result))
2229 (eval-when (:compile-toplevel :execute)
2231 (sb!xc:defmacro subst-dispatch (pred)
2232 `(seq-dispatch-checking=>seq sequence
2233 (let ((end (or end length)))
2234 (declare (type index end))
2235 (if from-end
2236 (nreverse (list-substitute* ,pred
2238 (reverse sequence)
2239 (- (the fixnum length)
2240 (the fixnum end))
2241 (- (the fixnum length)
2242 (the fixnum start))
2243 count key test test-not old))
2244 (list-substitute* ,pred
2245 new sequence start end count key test test-not
2246 old)))
2248 (let ((end (or end length)))
2249 (declare (type index end))
2250 (if from-end
2251 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2252 -1 length (1- (the fixnum end))
2253 (1- (the fixnum start))
2254 count key test test-not old)
2255 (vector-substitute* ,pred new sequence 1 0 length length
2256 start end count key test test-not old)))
2258 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2259 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2260 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2261 ;; dispatch once per element on PRED's run-time identity.
2262 ,(ecase (cadr pred)
2263 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2264 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2265 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2266 ) ; EVAL-WHEN
2268 (define-sequence-traverser substitute
2269 (new old sequence &rest args &key from-end test test-not
2270 start count end key)
2271 #!+sb-doc
2272 "Return a sequence of the same kind as SEQUENCE with the same elements,
2273 except that all elements equal to OLD are replaced with NEW."
2274 (declare (type fixnum start)
2275 (explicit-check sequence :result)
2276 (truly-dynamic-extent args))
2277 (subst-dispatch 'normal))
2279 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2281 (define-sequence-traverser substitute-if
2282 (new predicate sequence &rest args &key from-end start end count key)
2283 #!+sb-doc
2284 "Return a sequence of the same kind as SEQUENCE with the same elements
2285 except that all elements satisfying the PRED are replaced with NEW."
2286 (declare (type fixnum start)
2287 (explicit-check sequence :result)
2288 (truly-dynamic-extent args))
2289 (let ((test predicate)
2290 (test-not nil)
2291 old)
2292 (subst-dispatch 'if)))
2294 (define-sequence-traverser substitute-if-not
2295 (new predicate sequence &rest args &key from-end start end count key)
2296 #!+sb-doc
2297 "Return a sequence of the same kind as SEQUENCE with the same elements
2298 except that all elements not satisfying the PRED are replaced with NEW."
2299 (declare (type fixnum start)
2300 (explicit-check sequence :result)
2301 (truly-dynamic-extent args))
2302 (let ((test predicate)
2303 (test-not nil)
2304 old)
2305 (subst-dispatch 'if-not)))
2307 ;;;; NSUBSTITUTE
2309 (define-sequence-traverser nsubstitute
2310 (new old sequence &rest args &key from-end test test-not
2311 end count key start)
2312 #!+sb-doc
2313 "Return a sequence of the same kind as SEQUENCE with the same elements
2314 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2315 may be destructively modified."
2316 (declare (type fixnum start)
2317 (truly-dynamic-extent args))
2318 (declare (explicit-check sequence :result))
2319 (seq-dispatch-checking=>seq sequence
2320 (let ((end (or end length)))
2321 (declare (type index end))
2322 (if from-end
2323 (nreverse (nlist-substitute*
2324 new old (nreverse (the list sequence))
2325 test test-not (- length end) (- length start)
2326 count key))
2327 (nlist-substitute* new old sequence
2328 test test-not start end count key)))
2329 (let ((end (or end length)))
2330 (declare (type index end))
2331 (if from-end
2332 (nvector-substitute* new old sequence -1
2333 test test-not (1- end) (1- start) count key)
2334 (nvector-substitute* new old sequence 1
2335 test test-not start end count key)))
2336 (apply #'sb!sequence:nsubstitute new old sequence args)))
2338 (defun nlist-substitute* (new old sequence test test-not start end count key)
2339 (declare (fixnum start count end)
2340 (type (or null function) key))
2341 (do ((test (or test-not test))
2342 (list (nthcdr start sequence) (cdr list))
2343 (index start (1+ index)))
2344 ((or (= index end) (null list) (= count 0)) sequence)
2345 (declare (fixnum index)
2346 (function test))
2347 (let ((value (funcall test old (apply-key key (car list)))))
2348 (when (if test-not
2349 (not value)
2350 value)
2351 (rplaca list new)
2352 (decf count)))))
2354 (defun nvector-substitute* (new old sequence incrementer
2355 test test-not start end count key)
2356 (declare (fixnum start count end)
2357 (type (integer -1 1) incrementer)
2358 (type (or null function) key))
2359 (let* ((test (or test-not test))
2360 (tag (%other-pointer-widetag sequence))
2361 (getter (the function (svref %%data-vector-reffers%% tag)))
2362 (setter (the function (svref %%data-vector-setters%% tag))))
2363 (declare (function test))
2364 (do ((index start (+ index incrementer)))
2365 ((or (= index end) (= count 0)) sequence)
2366 (declare (fixnum index))
2367 (let* ((value (apply-key key (funcall getter sequence index)))
2368 (test (and (funcall test old value) 0)))
2369 (when (if test-not
2370 (not test)
2371 test)
2372 (funcall setter sequence index new)
2373 (decf count))))))
2375 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2377 (define-sequence-traverser nsubstitute-if
2378 (new predicate sequence &rest args &key from-end start end count key)
2379 #!+sb-doc
2380 "Return a sequence of the same kind as SEQUENCE with the same elements
2381 except that all elements satisfying PREDICATE are replaced with NEW.
2382 SEQUENCE may be destructively modified."
2383 (declare (type fixnum start)
2384 (truly-dynamic-extent args))
2385 (declare (explicit-check sequence :result))
2386 (seq-dispatch-checking=>seq sequence
2387 (let ((end (or end length)))
2388 (declare (type index end))
2389 (if from-end
2390 (nreverse (nlist-substitute-if*
2391 new predicate (nreverse (the list sequence))
2392 (- length end) (- length start) count key))
2393 (nlist-substitute-if* new predicate sequence
2394 start end count key)))
2395 (let ((end (or end length)))
2396 (declare (type index end))
2397 (if from-end
2398 (nvector-substitute-if* new predicate sequence -1
2399 (1- end) (1- start) count key)
2400 (nvector-substitute-if* new predicate sequence 1
2401 start end count key)))
2402 (apply #'sb!sequence:nsubstitute-if new predicate sequence args)))
2404 (defun nlist-substitute-if* (new test sequence start end count key)
2405 (declare (type fixnum start end count)
2406 (type (or null function) key)
2407 (type function test)) ; coercion is done by caller
2408 (do ((list (nthcdr start sequence) (cdr list))
2409 (index start (1+ index)))
2410 ((or (= index end) (null list) (= count 0)) sequence)
2411 (declare (fixnum index))
2412 (when (funcall test (apply-key key (car list)))
2413 (rplaca list new)
2414 (decf count))))
2416 (defun nvector-substitute-if* (new test sequence incrementer
2417 start end count key)
2418 (declare (type fixnum end count)
2419 (type (integer -1 1) incrementer)
2420 (type (or null function) key)
2421 (type function test)) ; coercion is done by caller
2422 (let* ((tag (%other-pointer-widetag sequence))
2423 (getter (the function (svref %%data-vector-reffers%% tag)))
2424 (setter (the function (svref %%data-vector-setters%% tag))))
2425 (do ((index start (+ index incrementer)))
2426 ((or (= index end) (= count 0)) sequence)
2427 (declare (fixnum index))
2428 (when (funcall test (apply-key key (funcall getter sequence index)))
2429 (funcall setter sequence index new)
2430 (decf count)))))
2432 (define-sequence-traverser nsubstitute-if-not
2433 (new predicate sequence &rest args &key from-end start end count key)
2434 #!+sb-doc
2435 "Return a sequence of the same kind as SEQUENCE with the same elements
2436 except that all elements not satisfying PREDICATE are replaced with NEW.
2437 SEQUENCE may be destructively modified."
2438 (declare (type fixnum start)
2439 (truly-dynamic-extent args))
2440 (declare (explicit-check sequence :result))
2441 (seq-dispatch-checking=>seq sequence
2442 (let ((end (or end length)))
2443 (declare (fixnum end))
2444 (if from-end
2445 (nreverse (nlist-substitute-if-not*
2446 new predicate (nreverse (the list sequence))
2447 (- length end) (- length start) count key))
2448 (nlist-substitute-if-not* new predicate sequence
2449 start end count key)))
2450 (let ((end (or end length)))
2451 (declare (fixnum end))
2452 (if from-end
2453 (nvector-substitute-if-not* new predicate sequence -1
2454 (1- end) (1- start) count key)
2455 (nvector-substitute-if-not* new predicate sequence 1
2456 start end count key)))
2457 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args)))
2459 (defun nlist-substitute-if-not* (new test sequence start end count key)
2460 (declare (type fixnum start end count)
2461 (type (or null function) key)
2462 (type function test)) ; coercion is done by caller
2463 (do ((list (nthcdr start sequence) (cdr list))
2464 (index start (1+ index)))
2465 ((or (= index end) (null list) (= count 0)) sequence)
2466 (declare (fixnum index))
2467 (when (not (funcall test (apply-key key (car list))))
2468 (rplaca list new)
2469 (decf count))))
2471 (defun nvector-substitute-if-not* (new test sequence incrementer
2472 start end count key)
2473 (declare (type fixnum end count)
2474 (type (integer -1 1) incrementer)
2475 (type (or null function) key)
2476 (type function test)) ; coercion is done by caller
2477 (let* ((tag (%other-pointer-widetag sequence))
2478 (getter (the function (svref %%data-vector-reffers%% tag)))
2479 (setter (the function (svref %%data-vector-setters%% tag))))
2480 (do ((index start (+ index incrementer)))
2481 ((or (= index end) (= count 0)) sequence)
2482 (declare (fixnum index))
2483 (when (not (funcall test (apply-key key (funcall getter sequence index))))
2484 (funcall setter sequence index new)
2485 (decf count)))))
2487 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2489 (defun effective-find-position-test (test test-not)
2490 (effective-find-position-test test test-not))
2491 (defun effective-find-position-key (key)
2492 (effective-find-position-key key))
2494 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2495 (macrolet (;; shared logic for defining %FIND-POSITION and
2496 ;; %FIND-POSITION-IF in terms of various inlineable cases
2497 ;; of the expression defined in FROB and VECTOR*-FROB
2498 (frobs (&optional bit-frob)
2499 `(seq-dispatch-checking sequence-arg
2500 (frob sequence-arg from-end)
2501 (with-array-data ((sequence sequence-arg :offset-var offset)
2502 (start start)
2503 (end end)
2504 :check-fill-pointer t)
2505 (multiple-value-bind (f p)
2506 (macrolet ((frob2 () `(if from-end
2507 (frob sequence t)
2508 (frob sequence nil))))
2509 (typecase sequence
2510 #!+sb-unicode
2511 ((simple-array character (*)) (frob2))
2512 ((simple-array base-char (*)) (frob2))
2513 ,@(when bit-frob
2514 `((simple-bit-vector
2515 (if (and (typep item 'bit)
2516 (eq #'identity key)
2517 (or (eq #'eq test)
2518 (eq #'eql test)
2519 (eq #'equal test)))
2520 (let ((p (%bit-position item sequence
2521 from-end start end)))
2522 (if p
2523 (values item p)
2524 (values nil nil)))
2525 (vector*-frob sequence)))))
2527 (vector*-frob sequence))))
2528 (declare (type (or index null) p))
2529 (values f (and p (the index (- p offset))))))
2530 ;; EXTENDED-SEQUENCE is not allowed.
2532 (defun %find-position (item sequence-arg from-end start end key test)
2533 (declare (explicit-check sequence-arg))
2534 (macrolet ((frob (sequence from-end)
2535 `(%find-position item ,sequence
2536 ,from-end start end key test))
2537 (vector*-frob (sequence)
2538 `(%find-position-vector-macro item ,sequence
2539 from-end start end key test)))
2540 (frobs t)))
2541 (defun %find-position-if (predicate sequence-arg from-end start end key)
2542 (declare (explicit-check sequence-arg))
2543 (macrolet ((frob (sequence from-end)
2544 `(%find-position-if predicate ,sequence
2545 ,from-end start end key))
2546 (vector*-frob (sequence)
2547 `(%find-position-if-vector-macro predicate ,sequence
2548 from-end start end key)))
2549 (frobs)))
2550 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2551 (declare (explicit-check sequence-arg))
2552 (macrolet ((frob (sequence from-end)
2553 `(%find-position-if-not predicate ,sequence
2554 ,from-end start end key))
2555 (vector*-frob (sequence)
2556 `(%find-position-if-not-vector-macro predicate ,sequence
2557 from-end start end key)))
2558 (frobs))))
2560 (defun find
2561 (item sequence &rest args &key from-end (start 0) end key test test-not)
2562 (declare (truly-dynamic-extent args))
2563 (declare (explicit-check sequence))
2564 (seq-dispatch-checking sequence
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 (nth-value 0 (%find-position
2570 item sequence from-end start end
2571 (effective-find-position-key key)
2572 (effective-find-position-test test test-not)))
2573 (apply #'sb!sequence:find item sequence args)))
2574 (defun position
2575 (item sequence &rest args &key from-end (start 0) end key test test-not)
2576 (declare (truly-dynamic-extent args))
2577 (declare (explicit-check sequence))
2578 (seq-dispatch-checking sequence
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 (nth-value 1 (%find-position
2584 item sequence from-end start end
2585 (effective-find-position-key key)
2586 (effective-find-position-test test test-not)))
2587 (apply #'sb!sequence:position item sequence args)))
2589 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2590 (declare (truly-dynamic-extent args))
2591 (declare (explicit-check sequence))
2592 (seq-dispatch-checking sequence
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 (nth-value 0 (%find-position-if
2598 (%coerce-callable-to-fun predicate)
2599 sequence from-end start end
2600 (effective-find-position-key key)))
2601 (apply #'sb!sequence:find-if predicate sequence args)))
2602 (defun position-if
2603 (predicate sequence &rest args &key from-end (start 0) end key)
2604 (declare (truly-dynamic-extent args))
2605 (declare (explicit-check sequence))
2606 (seq-dispatch-checking sequence
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 (nth-value 1 (%find-position-if
2612 (%coerce-callable-to-fun predicate)
2613 sequence from-end start end
2614 (effective-find-position-key key)))
2615 (apply #'sb!sequence:position-if predicate sequence args)))
2617 (defun find-if-not
2618 (predicate sequence &rest args &key from-end (start 0) end key)
2619 (declare (truly-dynamic-extent args))
2620 (declare (explicit-check sequence))
2621 (seq-dispatch-checking sequence
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 (nth-value 0 (%find-position-if-not
2627 (%coerce-callable-to-fun predicate)
2628 sequence from-end start end
2629 (effective-find-position-key key)))
2630 (apply #'sb!sequence:find-if-not predicate sequence args)))
2631 (defun position-if-not
2632 (predicate sequence &rest args &key from-end (start 0) end key)
2633 (declare (truly-dynamic-extent args))
2634 (declare (explicit-check sequence))
2635 (seq-dispatch-checking sequence
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 (nth-value 1 (%find-position-if-not
2641 (%coerce-callable-to-fun predicate)
2642 sequence from-end start end
2643 (effective-find-position-key key)))
2644 (apply #'sb!sequence:position-if-not predicate sequence args)))
2646 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2648 (eval-when (:compile-toplevel :execute)
2650 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2651 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2652 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2653 `(let ((%start ,(if from-end-p '(1- end) 'start))
2654 (%end ,(if from-end-p '(1- start) 'end)))
2655 (do ((index %start ,next-index)
2656 (count 0))
2657 ((= index (the fixnum %end)) count)
2658 (declare (fixnum index count))
2659 (,(if notp 'unless 'when) ,pred
2660 (setq count (1+ count)))))))
2662 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2663 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2664 `(let ((%start ,(if from-end-p '(- length end) 'start))
2665 (%end ,(if from-end-p '(- length start) 'end))
2666 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2667 (do ((sequence (nthcdr %start ,sequence))
2668 (index %start (1+ index))
2669 (count 0))
2670 ((or (= index (the fixnum %end)) (null sequence)) count)
2671 (declare (fixnum index count))
2672 (,(if notp 'unless 'when) ,pred
2673 (setq count (1+ count)))))))
2676 ) ; EVAL-WHEN
2678 (define-sequence-traverser count-if
2679 (pred sequence &rest args &key from-end start end key)
2680 #!+sb-doc
2681 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2682 (declare (type fixnum start)
2683 (truly-dynamic-extent args))
2684 (declare (explicit-check sequence))
2685 (let ((pred (%coerce-callable-to-fun pred)))
2686 (seq-dispatch-checking sequence
2687 (let ((end (or end length)))
2688 (declare (type index end))
2689 (if from-end
2690 (list-count-if nil t pred sequence)
2691 (list-count-if nil nil pred sequence)))
2692 (let ((end (or end length)))
2693 (declare (type index end))
2694 (if from-end
2695 (vector-count-if nil t pred sequence)
2696 (vector-count-if nil nil pred sequence)))
2697 (apply #'sb!sequence:count-if pred sequence args))))
2699 (define-sequence-traverser count-if-not
2700 (pred sequence &rest args &key from-end start end key)
2701 #!+sb-doc
2702 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2703 (declare (type fixnum start)
2704 (truly-dynamic-extent args))
2705 (declare (explicit-check sequence))
2706 (let ((pred (%coerce-callable-to-fun pred)))
2707 (seq-dispatch-checking sequence
2708 (let ((end (or end length)))
2709 (declare (type index end))
2710 (if from-end
2711 (list-count-if t t pred sequence)
2712 (list-count-if t nil pred sequence)))
2713 (let ((end (or end length)))
2714 (declare (type index end))
2715 (if from-end
2716 (vector-count-if t t pred sequence)
2717 (vector-count-if t nil pred sequence)))
2718 (apply #'sb!sequence:count-if-not pred sequence args))))
2720 (define-sequence-traverser count
2721 (item sequence &rest args &key from-end start end
2722 key (test #'eql test-p) (test-not nil test-not-p))
2723 #!+sb-doc
2724 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2725 which defaults to EQL."
2726 (declare (type fixnum start)
2727 (truly-dynamic-extent args))
2728 (declare (explicit-check sequence))
2729 (when (and test-p test-not-p)
2730 ;; Use the same wording as EFFECTIVE-FIND-POSITION-TEST
2731 (error "can't specify both :TEST and :TEST-NOT"))
2732 (let ((%test (if test-not-p
2733 (lambda (x)
2734 (not (funcall test-not item x)))
2735 (lambda (x)
2736 (funcall test item x)))))
2737 (seq-dispatch-checking sequence
2738 (let ((end (or end length)))
2739 (declare (type index end))
2740 (if from-end
2741 (list-count-if nil t %test sequence)
2742 (list-count-if nil nil %test sequence)))
2743 (let ((end (or end length)))
2744 (declare (type index end))
2745 (if from-end
2746 (vector-count-if nil t %test sequence)
2747 (vector-count-if nil nil %test sequence)))
2748 (apply #'sb!sequence:count item sequence args))))
2750 ;;;; MISMATCH
2752 (eval-when (:compile-toplevel :execute)
2754 (sb!xc:defmacro match-vars (&rest body)
2755 `(let ((inc (if from-end -1 1))
2756 (start1 (if from-end (1- (the fixnum end1)) start1))
2757 (start2 (if from-end (1- (the fixnum end2)) start2))
2758 (end1 (if from-end (1- (the fixnum start1)) end1))
2759 (end2 (if from-end (1- (the fixnum start2)) end2)))
2760 (declare (fixnum inc start1 start2 end1 end2))
2761 ,@body))
2763 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2764 (declare (ignore end)) ;; ### Should END be used below?
2765 `(let ((,sequence (if from-end
2766 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2767 (reverse (the list ,sequence)))
2768 (nthcdr ,start ,sequence))))
2769 (declare (type list ,sequence))
2770 ,@body))
2772 ) ; EVAL-WHEN
2774 (eval-when (:compile-toplevel :execute)
2776 (sb!xc:defmacro if-mismatch (elt1 elt2)
2777 `(cond ((= (the fixnum index1) (the fixnum end1))
2778 (return (if (= (the fixnum index2) (the fixnum end2))
2780 (if from-end
2781 (1+ (the fixnum index1))
2782 (the fixnum index1)))))
2783 ((= (the fixnum index2) (the fixnum end2))
2784 (return (if from-end (1+ (the fixnum index1)) index1)))
2785 (test-not
2786 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2787 (return (if from-end (1+ (the fixnum index1)) index1))))
2788 (t (if (not (funcall test (apply-key key ,elt1)
2789 (apply-key key ,elt2)))
2790 (return (if from-end (1+ (the fixnum index1)) index1))))))
2792 (sb!xc:defmacro mumble-mumble-mismatch ()
2793 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2794 (index2 start2 (+ index2 (the fixnum inc))))
2795 (())
2796 (declare (fixnum index1 index2))
2797 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2799 (sb!xc:defmacro mumble-list-mismatch ()
2800 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2801 (index2 start2 (+ index2 (the fixnum inc))))
2802 (())
2803 (declare (fixnum index1 index2))
2804 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2806 (sb!xc:defmacro list-mumble-mismatch ()
2807 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2808 (index2 start2 (+ index2 (the fixnum inc))))
2809 (())
2810 (declare (fixnum index1 index2))
2811 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2813 (sb!xc:defmacro list-list-mismatch ()
2814 `(do ((sequence1 sequence1)
2815 (sequence2 sequence2)
2816 (index1 start1 (+ index1 (the fixnum inc)))
2817 (index2 start2 (+ index2 (the fixnum inc))))
2818 (())
2819 (declare (fixnum index1 index2))
2820 (if-mismatch (pop sequence1) (pop sequence2))))
2822 ) ; EVAL-WHEN
2824 (define-sequence-traverser mismatch
2825 (sequence1 sequence2 &rest args &key from-end test test-not
2826 start1 end1 start2 end2 key)
2827 #!+sb-doc
2828 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2829 element-wise. If they are of equal length and match in every element, the
2830 result is NIL. Otherwise, the result is a non-negative integer, the index
2831 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2832 if one is shorter than and a matching prefix of the other, the index within
2833 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2834 :FROM-END argument is given, then one plus the index of the rightmost
2835 position in which the sequences differ is returned."
2836 (declare (type fixnum start1 start2))
2837 (declare (truly-dynamic-extent args))
2838 (declare (explicit-check sequence1 sequence2 :result))
2839 (seq-dispatch-checking sequence1
2840 (seq-dispatch-checking sequence2
2841 (return-from mismatch
2842 (let ((end1 (or end1 length1))
2843 (end2 (or end2 length2)))
2844 (declare (type index end1 end2))
2845 (match-vars
2846 (matchify-list (sequence1 start1 length1 end1)
2847 (matchify-list (sequence2 start2 length2 end2)
2848 (list-list-mismatch))))))
2849 (return-from mismatch
2850 (let ((end1 (or end1 length1))
2851 (end2 (or end2 length2)))
2852 (declare (type index end1 end2))
2853 (match-vars
2854 (matchify-list (sequence1 start1 length1 end1)
2855 (list-mumble-mismatch)))))
2856 nil)
2857 (seq-dispatch-checking sequence2
2858 (return-from mismatch
2859 (let ((end1 (or end1 length1))
2860 (end2 (or end2 length2)))
2861 (declare (type index end1 end2))
2862 (match-vars
2863 (matchify-list (sequence2 start2 length2 end2)
2864 (mumble-list-mismatch)))))
2865 (return-from mismatch
2866 (let ((end1 (or end1 length1))
2867 (end2 (or end2 length2)))
2868 (declare (type index end1 end2))
2869 (match-vars
2870 (mumble-mumble-mismatch))))
2871 nil)
2873 ;; If sequence1 is an extended-sequence, we know nothing about sequence2.
2874 ;; If sequence1 was a list or vector, then sequence2 is an extended-sequence
2875 ;; or not a sequence. Either way, check it.
2876 (the (or index null)
2877 (values (apply #'sb!sequence:mismatch sequence1
2878 (the sequence sequence2) args))))
2880 ;;; search comparison functions
2882 (eval-when (:compile-toplevel :execute)
2884 ;;; Compare two elements and return if they don't match.
2885 (sb!xc:defmacro compare-elements (elt1 elt2)
2886 `(if test-not
2887 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2888 (return nil)
2890 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2891 (return nil)
2892 t)))
2894 (sb!xc:defmacro search-compare-list-list (main sub)
2895 `(do ((main ,main (cdr main))
2896 (jndex start1 (1+ jndex))
2897 (sub (nthcdr start1 ,sub) (cdr sub)))
2898 ((or (endp main) (endp sub) (<= end1 jndex))
2900 (declare (type (integer 0) jndex))
2901 (compare-elements (car sub) (car main))))
2903 (sb!xc:defmacro search-compare-list-vector (main sub)
2904 `(do ((main ,main (cdr main))
2905 (index start1 (1+ index)))
2906 ((or (endp main) (= index end1)) t)
2907 (compare-elements (aref ,sub index) (car main))))
2909 (sb!xc:defmacro search-compare-vector-list (main sub index)
2910 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2911 (jndex start1 (1+ jndex))
2912 (index ,index (1+ index)))
2913 ((or (<= end1 jndex) (endp sub)) t)
2914 (declare (type (integer 0) jndex))
2915 (compare-elements (car sub) (aref ,main index))))
2917 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2918 `(do ((index ,index (1+ index))
2919 (sub-index start1 (1+ sub-index)))
2920 ((= sub-index end1) t)
2921 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2923 (sb!xc:defmacro search-compare (main-type main sub index)
2924 (if (eq main-type 'list)
2925 `(seq-dispatch ,sub
2926 (search-compare-list-list ,main ,sub)
2927 (search-compare-list-vector ,main ,sub)
2928 ;; KLUDGE: just hack it together so that it works
2929 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2930 `(seq-dispatch ,sub
2931 (search-compare-vector-list ,main ,sub ,index)
2932 (search-compare-vector-vector ,main ,sub ,index)
2933 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2935 ) ; EVAL-WHEN
2937 ;;;; SEARCH
2939 (eval-when (:compile-toplevel :execute)
2941 (sb!xc:defmacro list-search (main sub)
2942 `(do ((main (nthcdr start2 ,main) (cdr main))
2943 (index2 start2 (1+ index2))
2944 (terminus (- end2 (the (integer 0) (- end1 start1))))
2945 (last-match ()))
2946 ((> index2 terminus) last-match)
2947 (declare (type (integer 0) index2))
2948 (if (search-compare list main ,sub index2)
2949 (if from-end
2950 (setq last-match index2)
2951 (return index2)))))
2953 (sb!xc:defmacro vector-search (main sub)
2954 `(do ((index2 start2 (1+ index2))
2955 (terminus (- end2 (the (integer 0) (- end1 start1))))
2956 (last-match ()))
2957 ((> index2 terminus) last-match)
2958 (declare (type (integer 0) index2))
2959 (if (search-compare vector ,main ,sub index2)
2960 (if from-end
2961 (setq last-match index2)
2962 (return index2)))))
2964 ) ; EVAL-WHEN
2966 (define-sequence-traverser search
2967 (sequence1 sequence2 &rest args &key
2968 from-end test test-not start1 end1 start2 end2 key)
2969 (declare (type fixnum start1 start2)
2970 (truly-dynamic-extent args))
2971 (declare (explicit-check sequence2))
2972 (seq-dispatch-checking sequence2
2973 (let ((end1 (or end1 length1))
2974 (end2 (or end2 length2)))
2975 (declare (type index end1 end2))
2976 (list-search sequence2 sequence1))
2977 (let ((end1 (or end1 length1))
2978 (end2 (or end2 length2)))
2979 (declare (type index end1 end2))
2980 (vector-search sequence2 sequence1))
2981 (apply #'sb!sequence:search sequence1 sequence2 args)))
2983 ;;; FIXME: this was originally in array.lisp; it might be better to
2984 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2985 ;;; a new early-seq.lisp file.
2986 (macrolet ((body (lambda-list endp-test start-recursion next-layer)
2987 `(let ((index 0))
2988 (labels ((frob ,lambda-list
2989 (cond (,endp-test
2990 (setf (aref vector index) contents)
2991 (incf index))
2993 (unless (typep contents 'sequence)
2994 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2995 sequence, but ~W more layer~:P needed."
2996 contents
2997 (- (length dimensions) axis)))
2998 (let ((k this-dimension)
2999 (l (length contents)))
3000 (unless (= k l)
3001 (error "malformed :INITIAL-CONTENTS: Dimension of ~
3002 axis ~W is ~W, but ~S is ~W long."
3003 axis k contents l)))
3004 (sb!sequence:dosequence (content contents)
3005 ,next-layer)))))
3006 ,start-recursion))))
3008 (defun fill-data-vector (vector dimensions initial-contents)
3009 (declare (explicit-check))
3010 (symbol-macrolet ((this-dimension (car dims)))
3011 (body (axis dims contents) (null dims)
3012 (frob 0 dimensions initial-contents)
3013 (frob (1+ axis) (cdr dims) content))))
3015 ;; Identical to FILL-DATA-VECTOR but avoid reference
3016 ;; to DIMENSIONS as a list except in case of error.
3017 (defun fill-array (initial-contents array)
3018 (declare (explicit-check))
3019 (let ((rank (array-rank array))
3020 (vector (%array-data-vector array)))
3021 (symbol-macrolet ((dimensions (array-dimensions array))
3022 (this-dimension (%array-dimension array axis)))
3023 (body (axis contents) (= axis rank)
3024 (frob 0 initial-contents)
3025 (frob (1+ axis) content))))
3026 array))