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