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