Make full call of MAP strictly check its output type.
[sbcl.git] / src / code / seq.lisp
blob915543d430a75783a037d8627fd461a1d1d870b3
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)
31 (defparameter *sequence-keyword-info*
32 ;; (name default supplied-p adjustment new-type)
33 `((count nil
34 nil
35 (etypecase count
36 (null (1- most-positive-fixnum))
37 (fixnum (max 0 count))
38 (integer (if (minusp count)
40 (1- most-positive-fixnum))))
41 (mod #.sb!xc:most-positive-fixnum))
42 ;; Entries for {start,end}{,1,2}
43 ,@(mapcan (lambda (names)
44 (destructuring-bind (start end length sequence) names
45 (list
46 `(,start
48 nil
49 ;; Only evaluate LENGTH (which may be expensive)
50 ;; if START is non-NIL.
51 (if (or (zerop ,start) (<= 0 ,start ,length))
52 ,start
53 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
54 index)
55 `(,end
56 nil
57 nil
58 ;; Only evaluate LENGTH (which may be expensive)
59 ;; if END is non-NIL.
60 (if (or (null ,end) (<= ,start ,end ,length))
61 ;; Defaulting of NIL is done inside the
62 ;; bodies, for ease of sharing with compiler
63 ;; transforms.
65 ;; FIXME: defend against non-number non-NIL
66 ;; stuff?
67 ,end
68 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
69 (or null index)))))
70 '((start end length sequence)
71 (start1 end1 length1 sequence1)
72 (start2 end2 length2 sequence2)))
73 (key nil
74 nil
75 (and key (%coerce-callable-to-fun key))
76 (or null function))
77 (test #'eql
78 nil
79 (%coerce-callable-to-fun test)
80 function)
81 (test-not nil
82 nil
83 (and test-not (%coerce-callable-to-fun test-not))
84 (or null function))))
86 (sb!xc:defmacro define-sequence-traverser (name args &body body)
87 (multiple-value-bind (body declarations docstring)
88 (parse-body body :doc-string-allowed t)
89 (collect ((new-args)
90 (new-declarations)
91 ;; Things which are definitely used in any code path.
92 (rebindings/eager)
93 ;; Things which may be used/are only used in certain
94 ;; code paths (e.g. length).
95 (rebindings/lazy))
96 (dolist (arg args)
97 (case arg
98 ;; FIXME: make this robust. And clean.
99 ((sequence sequence1 sequence2)
100 (let* ((length-var (ecase arg
101 (sequence 'length)
102 (sequence1 'length1)
103 (sequence2 'length2)))
104 (cache-var (symbolicate length-var '#:-cache)))
105 (new-args arg)
106 (rebindings/eager `(,cache-var nil))
107 (rebindings/lazy
108 `(,length-var (truly-the
109 index
110 (or ,cache-var (setf ,cache-var (length ,arg))))))))
111 ((function predicate)
112 (new-args arg)
113 (rebindings/eager `(,arg (%coerce-callable-to-fun ,arg))))
115 (let ((info (cdr (assoc arg *sequence-keyword-info*))))
116 (cond (info
117 (destructuring-bind (default supplied-p adjuster type) info
118 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
119 (rebindings/eager `(,arg ,adjuster))
120 (new-declarations `(type ,type ,arg))))
121 (t (new-args arg)))))))
122 `(defun ,name ,(new-args)
123 ,@(when docstring (list docstring))
124 ,@declarations
125 (symbol-macrolet (,@(rebindings/lazy))
126 (let* (,@(rebindings/eager))
127 (declare ,@(new-declarations))
128 ,@body
129 ))))))
131 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
133 ;;; FIXME: It might be worth making three cases here, LIST,
134 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
135 ;;; It tends to make code run faster but be bigger; some benchmarking
136 ;;; is needed to decide.
137 (sb!xc:defmacro seq-dispatch
138 (sequence list-form array-form &optional other-form)
139 `(if (listp ,sequence)
140 (let ((,sequence (truly-the list ,sequence)))
141 (declare (ignorable ,sequence))
142 ,list-form)
143 ,@(if other-form
144 `((if (arrayp ,sequence)
145 (let ((,sequence (truly-the vector ,sequence)))
146 (declare (ignorable ,sequence))
147 ,array-form)
148 ,other-form))
149 `((let ((,sequence (truly-the vector ,sequence)))
150 (declare (ignorable ,sequence))
151 ,array-form)))))
153 ;; Same as above, but don't assume that ARRAYP implies VECTORP,
154 ;; and call SEQUENCEP only after checking for not LISTP and not VECTORP.
155 ;; This is for dispatching within sequence functions that have the
156 ;; EXPLICIT-CHECK attribute. [Because there is no way to have the compiler
157 ;; insert checks for a subset of arguments, it is inconvenient to declare
158 ;; things like DELETE-IF with explicit-check to suppress checking of the
159 ;; 'sequence' because then you have to manually check all other arguments.]
160 (sb!xc:defmacro seq-dispatch-checking
161 (sequence list-form array-form &optional other-form)
162 `(if (listp ,sequence)
163 (let ((,sequence (truly-the list ,sequence)))
164 (declare (ignorable ,sequence))
165 ,list-form)
166 ,@(if other-form
167 `((if (vectorp ,sequence)
168 (let ((,sequence (truly-the vector ,sequence)))
169 (declare (ignorable ,sequence))
170 ,array-form)
171 ;; This could assert (THE EXTENDED-SEQUENCE ,sequence)
172 ;; for a slight win, however the error string would be wrong.
173 ;; It needs to be "<x> is not a SEQUENCE", not "<x> is not
174 ;; an EXTENDED-SEQUENCE".
175 (let ((,sequence (the sequence ,sequence)))
176 ,other-form)))
177 `((let ((,sequence (the vector ,sequence)))
178 (declare (ignorable ,sequence))
179 ,array-form)))))
181 (sb!xc:defmacro %make-sequence-like (sequence length)
182 #!+sb-doc
183 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
184 `(seq-dispatch ,sequence
185 (make-list ,length)
186 (make-array ,length :element-type (array-element-type ,sequence))
187 (sb!sequence:make-sequence-like ,sequence ,length)))
189 (sb!xc:defmacro bad-sequence-type-error (type-spec)
190 `(error 'simple-type-error
191 :datum ,type-spec
192 :expected-type '(satisfies is-a-valid-sequence-type-specifier-p)
193 :format-control "~S is a bad type specifier for sequences."
194 :format-arguments (list ,type-spec)))
196 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
197 `(error 'simple-type-error
198 :datum ,length
199 :expected-type (cond ((array-type-p ,type)
200 `(eql ,(car (array-type-dimensions ,type))))
201 ((type= ,type (specifier-type 'null))
202 '(eql 0))
203 ((cons-type-p ,type)
204 '(integer 1))
205 (t (bug "weird type in S-T-L-M-ERROR")))
206 ;; FIXME: this format control causes ugly printing. There's
207 ;; probably some ~<~@:_~> incantation that would make it
208 ;; nicer. -- CSR, 2002-10-18
209 :format-control "The length requested (~S) does not match the type restriction in ~S."
210 :format-arguments (list ,length (type-specifier ,type))))
212 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
213 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
214 ;; words that give some but not total support to this position in
215 ;; ANSI. Essentially, we are justified in throwing this on
216 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
217 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
219 ;; On the other hand, I'm not sure it deserves to be a type-error,
220 ;; either. -- bem, 2005-08-10
221 `(error 'simple-program-error
222 :format-control "~S is too hairy for sequence functions."
223 :format-arguments (list ,type-spec)))
224 ) ; EVAL-WHEN
226 (defun is-a-valid-sequence-type-specifier-p (type)
227 (let ((type (specifier-type type)))
228 (or (csubtypep type (specifier-type 'list))
229 (csubtypep type (specifier-type 'vector)))))
231 ;;; It's possible with some sequence operations to declare the length
232 ;;; of a result vector, and to be safe, we really ought to verify that
233 ;;; the actual result has the declared length.
234 (defun vector-of-checked-length-given-length (vector declared-length)
235 (declare (type vector vector))
236 (declare (type index declared-length))
237 (let ((actual-length (length vector)))
238 (unless (= actual-length declared-length)
239 (error 'simple-type-error
240 :datum vector
241 :expected-type `(vector ,declared-length)
242 :format-control
243 "Vector length (~W) doesn't match declared length (~W)."
244 :format-arguments (list actual-length declared-length))))
245 vector)
247 (defun sequence-of-checked-length-given-type (sequence result-type)
248 (let ((ctype (specifier-type result-type)))
249 (if (not (array-type-p ctype))
250 sequence
251 (let ((declared-length (first (array-type-dimensions ctype))))
252 (if (eq declared-length '*)
253 sequence
254 (vector-of-checked-length-given-length sequence
255 declared-length))))))
257 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
258 (defun signal-index-too-large-error (sequence index)
259 (let* ((length (length sequence))
260 (max-index (and (plusp length)
261 (1- length))))
262 (error 'index-too-large-error
263 :datum index
264 :expected-type (if max-index
265 `(integer 0 ,max-index)
266 ;; This seems silly, is there something better?
267 '(integer 0 (0))))))
269 (declaim (ftype (function (t t t) nil) sequence-bounding-indices-bad-error))
270 (defun sequence-bounding-indices-bad-error (sequence start end)
271 (let ((size (length sequence)))
272 (error 'bounding-indices-bad-error
273 :datum (cons start end)
274 :expected-type `(cons (integer 0 ,size)
275 (integer ,start ,size))
276 :object sequence)))
278 (declaim (ftype (function (t t t) nil) array-bounding-indices-bad-error))
279 (defun array-bounding-indices-bad-error (array start end)
280 (let ((size (array-total-size array)))
281 (error 'bounding-indices-bad-error
282 :datum (cons start end)
283 :expected-type `(cons (integer 0 ,size)
284 (integer ,start ,size))
285 :object array)))
287 (declaim (ftype (function (t) nil) circular-list-error))
288 (defun circular-list-error (list)
289 (let ((*print-circle* t))
290 (error 'simple-type-error
291 :format-control "List is circular:~% ~S"
292 :format-arguments (list list)
293 :datum list
294 :type '(and list (satisfies list-length)))))
298 (defun emptyp (sequence)
299 #!+sb-doc
300 "Returns T if SEQUENCE is an empty sequence and NIL
301 otherwise. Signals an error if SEQUENCE is not a sequence."
302 (seq-dispatch sequence
303 (null sequence)
304 (zerop (length sequence))
305 (sb!sequence:emptyp sequence)))
307 (defun elt (sequence index)
308 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
309 (seq-dispatch sequence
310 (do ((count index (1- count))
311 (list sequence (cdr list)))
312 ((= count 0)
313 (if (endp list)
314 (signal-index-too-large-error sequence index)
315 (car list)))
316 (declare (type (integer 0) count)))
317 (progn
318 (when (>= index (length sequence))
319 (signal-index-too-large-error sequence index))
320 (aref sequence index))
321 (sb!sequence:elt sequence index)))
323 (defun %setelt (sequence index newval)
324 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
325 (seq-dispatch sequence
326 (do ((count index (1- count))
327 (seq sequence))
328 ((= count 0) (rplaca seq newval) newval)
329 (declare (fixnum count))
330 (if (atom (cdr seq))
331 (signal-index-too-large-error sequence index)
332 (setq seq (cdr seq))))
333 (progn
334 (when (>= index (length sequence))
335 (signal-index-too-large-error sequence index))
336 (setf (aref sequence index) newval))
337 (setf (sb!sequence:elt sequence index) newval)))
339 (defun length (sequence)
340 #!+sb-doc "Return an integer that is the length of SEQUENCE."
341 (seq-dispatch-checking sequence
342 (length sequence)
343 (length sequence)
344 (sb!sequence:length sequence)))
346 (defun make-sequence (type length &key (initial-element nil iep))
347 #!+sb-doc
348 "Return a sequence of the given TYPE and LENGTH, with elements initialized
349 to INITIAL-ELEMENT."
350 (declare (index length))
351 (let* ((expanded-type (typexpand type))
352 (adjusted-type
353 (typecase expanded-type
354 (atom (cond
355 ((eq expanded-type 'string) '(vector character))
356 ((eq expanded-type 'simple-string)
357 '(simple-array character (*)))
358 (t expanded-type)))
359 (cons (cond
360 ((eq (car expanded-type) 'string)
361 `(vector character ,@(cdr expanded-type)))
362 ((eq (car expanded-type) 'simple-string)
363 `(simple-array character ,(if (cdr expanded-type)
364 (cdr expanded-type)
365 '(*))))
366 (t expanded-type)))))
367 (type (specifier-type adjusted-type)))
368 (cond ((csubtypep type (specifier-type 'list))
369 (cond
370 ((type= type (specifier-type 'list))
371 (make-list length :initial-element initial-element))
372 ((eq type *empty-type*)
373 (bad-sequence-type-error nil))
374 ((type= type (specifier-type 'null))
375 (if (= length 0)
376 'nil
377 (sequence-type-length-mismatch-error type length)))
378 ((cons-type-p type)
379 (multiple-value-bind (min exactp)
380 (sb!kernel::cons-type-length-info type)
381 (if exactp
382 (unless (= length min)
383 (sequence-type-length-mismatch-error type length))
384 (unless (>= length min)
385 (sequence-type-length-mismatch-error type length)))
386 (make-list length :initial-element initial-element)))
387 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
388 ;; which may seem strange and non-ideal, but then I'd say
389 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
390 (t (sequence-type-too-hairy (type-specifier type)))))
391 ((csubtypep type (specifier-type 'vector))
392 (cond
393 (;; is it immediately obvious what the result type is?
394 (typep type 'array-type)
395 (progn
396 (aver (= (length (array-type-dimensions type)) 1))
397 (let* ((etype (type-specifier
398 (array-type-specialized-element-type type)))
399 (etype (if (eq etype '*) t etype))
400 (type-length (car (array-type-dimensions type))))
401 (unless (or (eq type-length '*)
402 (= type-length length))
403 (sequence-type-length-mismatch-error type length))
404 (if iep
405 (make-array length :element-type etype
406 :initial-element initial-element)
407 (make-array length :element-type etype)))))
408 (t (sequence-type-too-hairy (type-specifier type)))))
409 ((and (csubtypep type (specifier-type 'sequence))
410 (awhen (find-class adjusted-type nil)
411 (let ((prototype (sb!mop:class-prototype
412 (sb!pcl:ensure-class-finalized it))))
413 ;; This function is DEFKNOWNed with EXPLICIT-CHECK,
414 ;; so we must manually assert that user-written methods
415 ;; return a subtype of SEQUENCE.
416 (the sequence
417 (if iep
418 (sb!sequence:make-sequence-like
419 prototype length :initial-element initial-element)
420 (sb!sequence:make-sequence-like
421 prototype length)))))))
422 (t (bad-sequence-type-error (type-specifier type))))))
424 ;;;; SUBSEQ
425 ;;;;
427 (define-array-dispatch vector-subseq-dispatch (array start end)
428 (declare (optimize speed (safety 0)))
429 (declare (type index start end))
430 (subseq array start end))
432 ;;;; The support routines for SUBSEQ are used by compiler transforms,
433 ;;;; so we worry about dealing with END being supplied or defaulting
434 ;;;; to NIL at this level.
436 (defun vector-subseq* (sequence start end)
437 (declare (type vector sequence))
438 (declare (type index start)
439 (type (or null index) end)
440 (optimize speed))
441 (with-array-data ((data sequence)
442 (start start)
443 (end end)
444 :check-fill-pointer t
445 :force-inline t)
446 (vector-subseq-dispatch data start end)))
448 (defun list-subseq* (sequence start end)
449 (declare (type list sequence)
450 (type unsigned-byte start)
451 (type (or null unsigned-byte) end))
452 (flet ((oops ()
453 (sequence-bounding-indices-bad-error sequence start end)))
454 (let ((pointer sequence))
455 (unless (zerop start)
456 ;; If START > 0 the list cannot be empty. So CDR down to
457 ;; it START-1 times, check that we still have something, then
458 ;; CDR the final time.
460 ;; If START was zero, the list may be empty if END is NIL or
461 ;; also zero.
462 (when (> start 1)
463 (setf pointer (nthcdr (1- start) pointer)))
464 (if pointer
465 (pop pointer)
466 (oops)))
467 (if end
468 (let ((n (- end start)))
469 (declare (integer n))
470 (when (minusp n)
471 (oops))
472 (when (plusp n)
473 (let* ((head (list nil))
474 (tail head))
475 (macrolet ((pop-one ()
476 `(let ((tmp (list (pop pointer))))
477 (setf (cdr tail) tmp
478 tail tmp))))
479 ;; Bignum case
480 (loop until (fixnump n)
481 do (pop-one)
482 (decf n))
483 ;; Fixnum case, but leave last element, so we should
484 ;; still have something left in the sequence.
485 (let ((m (1- n)))
486 (declare (fixnum m))
487 (loop repeat m
488 do (pop-one)))
489 (unless pointer
490 (oops))
491 ;; OK, pop the last one.
492 (pop-one)
493 (cdr head)))))
494 (loop while pointer
495 collect (pop pointer))))))
497 (defun subseq (sequence start &optional end)
498 #!+sb-doc
499 "Return a copy of a subsequence of SEQUENCE starting with element number
500 START and continuing to the end of SEQUENCE or the optional END."
501 (seq-dispatch sequence
502 (list-subseq* sequence start end)
503 (vector-subseq* sequence start end)
504 (sb!sequence:subseq sequence start end)))
506 ;;;; COPY-SEQ
508 (defun copy-seq (sequence)
509 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
510 (seq-dispatch sequence
511 (list-copy-seq* sequence)
512 (vector-subseq* sequence 0 nil)
513 (sb!sequence:copy-seq sequence)))
515 (defun list-copy-seq* (sequence)
516 (!copy-list-macro sequence :check-proper-list t))
518 ;;;; FILL
520 (defun list-fill* (sequence item start end)
521 (declare (type list sequence)
522 (type unsigned-byte start)
523 (type (or null unsigned-byte) end))
524 (flet ((oops ()
525 (sequence-bounding-indices-bad-error sequence start end)))
526 (let ((pointer sequence))
527 (unless (zerop start)
528 ;; If START > 0 the list cannot be empty. So CDR down to it
529 ;; START-1 times, check that we still have something, then CDR
530 ;; the final time.
532 ;; If START was zero, the list may be empty if END is NIL or
533 ;; also zero.
534 (unless (= start 1)
535 (setf pointer (nthcdr (1- start) pointer)))
536 (if pointer
537 (pop pointer)
538 (oops)))
539 (if end
540 (let ((n (- end start)))
541 (declare (integer n))
542 (when (minusp n)
543 (oops))
544 (when (plusp n)
545 (loop repeat n
546 do (setf pointer (cdr (rplaca pointer item))))))
547 (loop while pointer
548 do (setf pointer (cdr (rplaca pointer item)))))))
549 sequence)
551 (defun vector-fill* (sequence item start end)
552 (with-array-data ((data sequence)
553 (start start)
554 (end end)
555 :force-inline t
556 :check-fill-pointer t)
557 (let ((setter (!find-data-vector-setter data)))
558 (declare (optimize (speed 3) (safety 0)))
559 (do ((index start (1+ index)))
560 ((= index end) sequence)
561 (declare (index index))
562 (funcall setter data index item)))))
564 (defun string-fill* (sequence item start end)
565 (declare (string sequence))
566 (with-array-data ((data sequence)
567 (start start)
568 (end end)
569 :force-inline t
570 :check-fill-pointer t)
571 ;; DEFTRANSFORM for FILL will turn these into
572 ;; calls to UB*-BASH-FILL.
573 (etypecase data
574 #!+sb-unicode
575 ((simple-array character (*))
576 (let ((item (locally (declare (optimize (safety 3)))
577 (the character item))))
578 (fill data item :start start :end end)))
579 ((simple-array base-char (*))
580 (let ((item (locally (declare (optimize (safety 3)))
581 (the base-char item))))
582 (fill data item :start start :end end))))))
584 (defun fill (sequence item &key (start 0) end)
585 #!+sb-doc
586 "Replace the specified elements of SEQUENCE with ITEM."
587 (seq-dispatch sequence
588 (list-fill* sequence item start end)
589 (vector-fill* sequence item start end)
590 (sb!sequence:fill sequence item
591 :start start
592 :end (%check-generic-sequence-bounds sequence start end))))
594 ;;;; REPLACE
596 (eval-when (:compile-toplevel :execute)
598 ;;; If we are copying around in the same vector, be careful not to copy the
599 ;;; same elements over repeatedly. We do this by copying backwards.
600 (sb!xc:defmacro mumble-replace-from-mumble ()
601 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
602 (let ((nelts (min (- target-end target-start)
603 (- source-end source-start))))
604 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
605 (1- target-index))
606 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
607 (1- source-index)))
608 ((= target-index (the fixnum (1- target-start))) target-sequence)
609 (declare (fixnum target-index source-index))
610 ;; disable bounds checking
611 (declare (optimize (safety 0)))
612 (setf (aref target-sequence target-index)
613 (aref source-sequence source-index))))
614 (do ((target-index target-start (1+ target-index))
615 (source-index source-start (1+ source-index)))
616 ((or (= target-index (the fixnum target-end))
617 (= source-index (the fixnum source-end)))
618 target-sequence)
619 (declare (fixnum target-index source-index))
620 ;; disable bounds checking
621 (declare (optimize (safety 0)))
622 (setf (aref target-sequence target-index)
623 (aref source-sequence source-index)))))
625 (sb!xc:defmacro list-replace-from-list ()
626 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
627 (let ((new-elts (subseq source-sequence source-start
628 (+ (the fixnum source-start)
629 (the fixnum
630 (min (- (the fixnum target-end)
631 (the fixnum target-start))
632 (- (the fixnum source-end)
633 (the fixnum source-start))))))))
634 (do ((n new-elts (cdr n))
635 (o (nthcdr target-start target-sequence) (cdr o)))
636 ((null n) target-sequence)
637 (rplaca o (car n))))
638 (do ((target-index target-start (1+ target-index))
639 (source-index source-start (1+ source-index))
640 (target-sequence-ref (nthcdr target-start target-sequence)
641 (cdr target-sequence-ref))
642 (source-sequence-ref (nthcdr source-start source-sequence)
643 (cdr source-sequence-ref)))
644 ((or (= target-index (the fixnum target-end))
645 (= source-index (the fixnum source-end))
646 (null target-sequence-ref) (null source-sequence-ref))
647 target-sequence)
648 (declare (fixnum target-index source-index))
649 (rplaca target-sequence-ref (car source-sequence-ref)))))
651 (sb!xc:defmacro list-replace-from-mumble ()
652 `(do ((target-index target-start (1+ target-index))
653 (source-index source-start (1+ source-index))
654 (target-sequence-ref (nthcdr target-start target-sequence)
655 (cdr target-sequence-ref)))
656 ((or (= target-index (the fixnum target-end))
657 (= source-index (the fixnum source-end))
658 (null target-sequence-ref))
659 target-sequence)
660 (declare (fixnum source-index target-index))
661 (rplaca target-sequence-ref (aref source-sequence source-index))))
663 (sb!xc:defmacro mumble-replace-from-list ()
664 `(do ((target-index target-start (1+ target-index))
665 (source-index source-start (1+ source-index))
666 (source-sequence (nthcdr source-start source-sequence)
667 (cdr source-sequence)))
668 ((or (= target-index (the fixnum target-end))
669 (= source-index (the fixnum source-end))
670 (null source-sequence))
671 target-sequence)
672 (declare (fixnum target-index source-index))
673 (setf (aref target-sequence target-index) (car source-sequence))))
675 ) ; EVAL-WHEN
677 ;;;; The support routines for REPLACE are used by compiler transforms, so we
678 ;;;; worry about dealing with END being supplied or defaulting to NIL
679 ;;;; at this level.
681 (defun list-replace-from-list* (target-sequence source-sequence target-start
682 target-end source-start source-end)
683 (when (null target-end) (setq target-end (length target-sequence)))
684 (when (null source-end) (setq source-end (length source-sequence)))
685 (list-replace-from-list))
687 (defun list-replace-from-vector* (target-sequence source-sequence target-start
688 target-end source-start source-end)
689 (when (null target-end) (setq target-end (length target-sequence)))
690 (when (null source-end) (setq source-end (length source-sequence)))
691 (list-replace-from-mumble))
693 (defun vector-replace-from-list* (target-sequence source-sequence target-start
694 target-end source-start source-end)
695 (when (null target-end) (setq target-end (length target-sequence)))
696 (when (null source-end) (setq source-end (length source-sequence)))
697 (mumble-replace-from-list))
699 (defun vector-replace-from-vector* (target-sequence source-sequence
700 target-start target-end source-start
701 source-end)
702 (when (null target-end) (setq target-end (length target-sequence)))
703 (when (null source-end) (setq source-end (length source-sequence)))
704 (mumble-replace-from-mumble))
706 #!+sb-unicode
707 (defun simple-character-string-replace-from-simple-character-string*
708 (target-sequence source-sequence
709 target-start target-end source-start source-end)
710 (declare (type (simple-array character (*)) target-sequence source-sequence))
711 (when (null target-end) (setq target-end (length target-sequence)))
712 (when (null source-end) (setq source-end (length source-sequence)))
713 (mumble-replace-from-mumble))
715 (define-sequence-traverser replace
716 (sequence1 sequence2 &rest args &key start1 end1 start2 end2)
717 #!+sb-doc
718 "Destructively modifies SEQUENCE1 by copying successive elements
719 into it from the SEQUENCE2.
721 Elements are copied to the subseqeuence bounded by START1 and END1,
722 from the subsequence bounded by START2 and END2. If these subsequences
723 are not of the same length, then the shorter length determines how
724 many elements are copied."
725 (declare (truly-dynamic-extent args))
726 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
727 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
728 ;; these things here so that legacy code gets the names it's
729 ;; expecting. We could use &AUX instead :-/.
730 (target-sequence sequence1)
731 (source-sequence sequence2)
732 (target-start start1)
733 (source-start start2)
734 (target-end (or end1 length1))
735 (source-end (or end2 length2)))
736 (seq-dispatch target-sequence
737 (seq-dispatch source-sequence
738 (list-replace-from-list)
739 (list-replace-from-mumble)
740 (apply #'sb!sequence:replace sequence1 sequence2 args))
741 (seq-dispatch source-sequence
742 (mumble-replace-from-list)
743 (mumble-replace-from-mumble)
744 (apply #'sb!sequence:replace sequence1 sequence2 args))
745 (apply #'sb!sequence:replace sequence1 sequence2 args))))
747 ;;;; REVERSE
749 (eval-when (:compile-toplevel :execute)
751 (sb!xc:defmacro vector-reverse (sequence)
752 `(let ((length (length ,sequence)))
753 (declare (fixnum length))
754 (do ((forward-index 0 (1+ forward-index))
755 (backward-index (1- length) (1- backward-index))
756 (new-sequence (%make-sequence-like sequence length)))
757 ((= forward-index length) new-sequence)
758 (declare (fixnum forward-index backward-index))
759 (setf (aref new-sequence forward-index)
760 (aref ,sequence backward-index)))))
762 (sb!xc:defmacro list-reverse-macro (sequence)
763 `(do ((new-list ()))
764 ((endp ,sequence) new-list)
765 (push (pop ,sequence) new-list)))
767 ) ; EVAL-WHEN
769 (defun reverse (sequence)
770 #!+sb-doc
771 "Return a new sequence containing the same elements but in reverse order."
772 (seq-dispatch-checking sequence
773 (list-reverse* sequence)
774 (vector-reverse* sequence)
775 ;; The type deriver says that LIST => LIST and VECTOR => VECTOR
776 ;; but does not claim to know anything about extended-sequences.
777 ;; So this could theoretically return any subtype of SEQUENCE
778 ;; given an EXTENDED-SEQUENCE as input. But fndb says this returns
779 ;; a CONSED-SEQUENCE, which precludes non-simple vectors.
780 ;; But a CLOS sequence can apparently decide to return a LIST when
781 ;; reversed. [Is that too weird? Make this EXTENDED-SEQUENCE maybe?]
782 (the consed-sequence (values (sb!sequence:reverse sequence)))))
784 ;;; internal frobs
786 (defun list-reverse* (sequence)
787 (list-reverse-macro sequence))
789 (defun vector-reverse* (sequence)
790 (vector-reverse sequence))
792 ;;;; NREVERSE
794 (eval-when (:compile-toplevel :execute)
796 (sb!xc:defmacro vector-nreverse (sequence)
797 `(let ((length (length (the vector ,sequence))))
798 (when (>= length 2)
799 (do ((left-index 0 (1+ left-index))
800 (right-index (1- length) (1- right-index)))
801 ((<= right-index left-index))
802 (declare (type index left-index right-index))
803 (rotatef (aref ,sequence left-index)
804 (aref ,sequence right-index))))
805 ,sequence))
807 (sb!xc:defmacro list-nreverse-macro (list)
808 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
809 (2nd ,list 1st)
810 (3rd '() 2nd))
811 ((atom 2nd) 3rd)
812 (rplacd 2nd 3rd)))
814 ) ; EVAL-WHEN
816 (defun list-nreverse* (sequence)
817 (list-nreverse-macro sequence))
819 (defun vector-nreverse* (sequence)
820 (vector-nreverse sequence))
822 (defun nreverse (sequence)
823 #!+sb-doc
824 "Return a sequence of the same elements in reverse order; the argument
825 is destroyed."
826 (seq-dispatch-checking sequence
827 (list-nreverse* sequence)
828 (vector-nreverse* sequence)
829 ;; The type deriver for this is 'result-type-first-arg',
830 ;; meaning it should return definitely an EXTENDED-SEQUENCE
831 ;; and not a list or vector.
832 (the extended-sequence (values (sb!sequence:nreverse sequence)))))
834 ;;;; CONCATENATE
836 (defmacro sb!sequence:dosequence ((element sequence &optional return) &body body)
837 #!+sb-doc
838 "Executes BODY with ELEMENT subsequently bound to each element of
839 SEQUENCE, then returns RETURN."
840 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
841 (once-only ((sequence sequence))
842 (with-unique-names (state limit from-end step endp elt)
843 `(block nil
844 (seq-dispatch ,sequence
845 (dolist (,element ,sequence ,return) ,@body)
846 (do-vector-data (,element ,sequence ,return) ,@body)
847 (multiple-value-bind (,state ,limit ,from-end ,step ,endp ,elt)
848 (sb!sequence:make-sequence-iterator ,sequence)
849 (do ((,state ,state (funcall ,step ,sequence ,state ,from-end)))
850 ((funcall ,endp ,sequence ,state ,limit ,from-end)
851 (let ((,element nil))
852 ,@(filter-dolist-declarations decls)
853 (declare (ignorable ,element))
854 ,return))
855 (let ((,element (funcall ,elt ,sequence ,state)))
856 ,@decls
857 (tagbody
858 ,@forms))))))))))
861 (defun concatenate (output-type-spec &rest sequences)
862 #!+sb-doc
863 "Return a new sequence of all the argument sequences concatenated together
864 which shares no structure with the original argument sequences of the
865 specified OUTPUT-TYPE-SPEC."
866 (flet ((concat-to-list* (sequences)
867 (let ((result (list nil)))
868 (do ((sequences sequences (cdr sequences))
869 (splice result))
870 ((null sequences) (cdr result))
871 (let ((sequence (car sequences)))
872 (sb!sequence:dosequence (e sequence)
873 (setq splice (cdr (rplacd splice (list e)))))))))
874 (concat-to-simple* (type-spec sequences)
875 (do ((seqs sequences (cdr seqs))
876 (total-length 0)
877 (lengths ()))
878 ((null seqs)
879 (do ((sequences sequences (cdr sequences))
880 (lengths lengths (cdr lengths))
881 (index 0)
882 (result (make-sequence type-spec total-length)))
883 ((= index total-length) result)
884 (declare (fixnum index))
885 (let ((sequence (car sequences)))
886 (sb!sequence:dosequence (e sequence)
887 (setf (aref result index) e)
888 (incf index)))))
889 (let ((length (length (car seqs))))
890 (declare (fixnum length))
891 (setq lengths (nconc lengths (list length)))
892 (setq total-length (+ total-length length))))))
893 (let ((type (specifier-type output-type-spec)))
894 (cond
895 ((csubtypep type (specifier-type 'list))
896 (cond
897 ((type= type (specifier-type 'list))
898 (concat-to-list* sequences))
899 ((eq type *empty-type*)
900 (bad-sequence-type-error nil))
901 ((type= type (specifier-type 'null))
902 (unless (every #'emptyp sequences)
903 (sequence-type-length-mismatch-error
904 type (reduce #'+ sequences :key #'length))) ; FIXME: circular list issues.
905 '())
906 ((cons-type-p type)
907 (multiple-value-bind (min exactp)
908 (sb!kernel::cons-type-length-info type)
909 (let ((length (reduce #'+ sequences :key #'length)))
910 (if exactp
911 (unless (= length min)
912 (sequence-type-length-mismatch-error type length))
913 (unless (>= length min)
914 (sequence-type-length-mismatch-error type length)))
915 (concat-to-list* sequences))))
916 (t (sequence-type-too-hairy (type-specifier type)))))
917 ((csubtypep type (specifier-type 'vector))
918 (concat-to-simple* output-type-spec sequences))
919 ((and (csubtypep type (specifier-type 'sequence))
920 (awhen (find-class output-type-spec nil)
921 ;; This function is DEFKNOWNed with EXPLICIT-CHECK,
922 ;; so we must manually assert that user-written methods
923 ;; return a subtype of SEQUENCE.
924 (the sequence
925 (apply #'sb!sequence:concatenate
926 (sb!mop:class-prototype
927 (sb!pcl:ensure-class-finalized it))
928 sequences)))))
930 (bad-sequence-type-error output-type-spec))))))
932 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
933 ;;; CONCATENATE 'STRING &co into these.
934 (macrolet ((def (name element-type)
935 `(defun ,name (&rest sequences)
936 (declare (dynamic-extent sequences)
937 (optimize speed)
938 (optimize (sb!c::insert-array-bounds-checks 0)))
939 (let* ((lengths (mapcar #'length sequences))
940 (result (make-array (the integer (apply #'+ lengths))
941 :element-type ',element-type))
942 (start 0))
943 (declare (index start))
944 (dolist (seq sequences)
945 (string-dispatch
946 ((simple-array character (*))
947 (simple-array base-char (*))
950 (replace result seq :start1 start))
951 (incf start (the index (pop lengths))))
952 result))))
953 (def %concatenate-to-string character)
954 (def %concatenate-to-base-string base-char))
957 ;;;; MAP
959 ;;; helper functions to handle arity-1 subcases of MAP
960 (defun %map-to-list-arity-1 (fun sequence)
961 (let ((reversed-result nil)
962 (really-fun (%coerce-callable-to-fun fun)))
963 (sb!sequence:dosequence (element sequence)
964 (push (funcall really-fun element)
965 reversed-result))
966 (nreverse reversed-result)))
967 (defun %map-to-simple-vector-arity-1 (fun sequence)
968 (let ((result (make-array (length sequence)))
969 (index 0)
970 (really-fun (%coerce-callable-to-fun fun)))
971 (declare (type index index))
972 (sb!sequence:dosequence (element sequence)
973 (setf (aref result index)
974 (funcall really-fun element))
975 (incf index))
976 result))
977 (defun %map-for-effect-arity-1 (fun sequence)
978 (let ((really-fun (%coerce-callable-to-fun fun)))
979 (sb!sequence:dosequence (element sequence)
980 (funcall really-fun element)))
981 nil)
983 (declaim (maybe-inline %map-for-effect))
984 (defun %map-for-effect (fun sequences)
985 (declare (type function fun) (type list sequences))
986 (let ((%sequences sequences)
987 (%iters (mapcar (lambda (s)
988 (seq-dispatch s
991 (multiple-value-list
992 (sb!sequence:make-sequence-iterator s))))
993 sequences))
994 (%apply-args (make-list (length sequences))))
995 ;; this is almost efficient (except in the general case where we
996 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
997 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
998 (declare (type list %sequences %iters %apply-args))
999 (loop
1000 (do ((in-sequences %sequences (cdr in-sequences))
1001 (in-iters %iters (cdr in-iters))
1002 (in-apply-args %apply-args (cdr in-apply-args)))
1003 ((null in-sequences) (apply fun %apply-args))
1004 (let ((i (car in-iters)))
1005 (declare (type (or list index) i))
1006 (cond
1007 ((listp (car in-sequences))
1008 (if (null i)
1009 (return-from %map-for-effect nil)
1010 (setf (car in-apply-args) (car i)
1011 (car in-iters) (cdr i))))
1012 ((typep i 'index)
1013 (let ((v (the vector (car in-sequences))))
1014 (if (>= i (length v))
1015 (return-from %map-for-effect nil)
1016 (setf (car in-apply-args) (aref v i)
1017 (car in-iters) (1+ i)))))
1019 ;; While on one hand this could benefit from a zero-safety ds-bind,
1020 ;; on the other, why not coerce these tuples to vectors or structs?
1021 (destructuring-bind (state limit from-end step endp elt &rest ignore)
1023 (declare (type function step endp elt)
1024 (ignore ignore))
1025 (let ((s (car in-sequences)))
1026 (if (funcall endp s state limit from-end)
1027 (return-from %map-for-effect nil)
1028 (progn
1029 (setf (car in-apply-args) (funcall elt s state))
1030 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
1031 (defun %map-to-list (fun sequences)
1032 (declare (type function fun)
1033 (type list sequences))
1034 (let ((result nil))
1035 (flet ((f (&rest args)
1036 (declare (truly-dynamic-extent args))
1037 (push (apply fun args) result)))
1038 (declare (truly-dynamic-extent #'f))
1039 (%map-for-effect #'f sequences))
1040 (nreverse result)))
1041 (defun %map-to-vector (output-type-spec fun sequences)
1042 (declare (type function fun)
1043 (type list sequences))
1044 (let ((min-len 0))
1045 (flet ((f (&rest args)
1046 (declare (truly-dynamic-extent args))
1047 (declare (ignore args))
1048 (incf min-len)))
1049 (declare (truly-dynamic-extent #'f))
1050 (%map-for-effect #'f sequences))
1051 (let ((result (make-sequence output-type-spec min-len))
1052 (i 0))
1053 (declare (type (simple-array * (*)) result))
1054 (flet ((f (&rest args)
1055 (declare (truly-dynamic-extent args))
1056 (setf (aref result i) (apply fun args))
1057 (incf i)))
1058 (declare (truly-dynamic-extent #'f))
1059 (%map-for-effect #'f sequences))
1060 result)))
1062 ;;; %MAP is just MAP without the final just-to-be-sure check that
1063 ;;; length of the output sequence matches any length specified
1064 ;;; in RESULT-TYPE.
1065 (defun %map (result-type function &rest sequences)
1066 (declare (dynamic-extent sequences))
1067 ;; Everything that we end up calling uses %COERCE-TO-CALLABLE
1068 ;; on FUNCTION so we don't need to declare it of type CALLABLE here.
1069 ;; Additionally all the arity-1 mappers use SEQ-DISPATCH which asserts
1070 ;; that the input is a SEQUENCE. Despite SEQ-DISPATCH being "less safe"
1071 ;; than SEQ-DISPATCH-CHECKING, both are in fact equally safe, because
1072 ;; the ARRAY case (which assumes that all arrays are vectors) utilizes
1073 ;; %WITH-ARRAY-DATA/FP which asserts that its input is a vector.
1074 (labels ((slower-map (type)
1075 (let ((really-fun (%coerce-callable-to-fun function)))
1076 (cond
1077 ((eq type *empty-type*)
1078 (%map-for-effect really-fun sequences))
1079 ((csubtypep type (specifier-type 'list))
1080 (%map-to-list really-fun sequences))
1081 ((csubtypep type (specifier-type 'vector))
1082 (%map-to-vector result-type really-fun sequences))
1083 ((and (csubtypep type (specifier-type 'sequence))
1084 (awhen (find-class result-type nil)
1085 ;; This function is DEFKNOWNed with EXPLICIT-CHECK,
1086 ;; so we must manually assert that user-written methods
1087 ;; return a subtype of SEQUENCE.
1088 (the sequence
1089 (apply #'sb!sequence:map
1090 (sb!mop:class-prototype
1091 (sb!pcl:ensure-class-finalized it))
1092 really-fun sequences)))))
1094 (bad-sequence-type-error result-type))))))
1095 ;; Handle some easy cases faster
1096 (if (/= (length sequences) 1)
1097 (slower-map (specifier-type result-type))
1098 (let ((first-sequence (fast-&rest-nth 0 sequences)))
1099 (case result-type
1100 ((nil)
1101 (%map-for-effect-arity-1 function first-sequence))
1102 ((list cons)
1103 (%map-to-list-arity-1 function first-sequence))
1104 ((vector simple-vector)
1105 (%map-to-simple-vector-arity-1 function first-sequence))
1107 (let ((type (specifier-type result-type)))
1108 (cond ((eq type *empty-type*)
1109 (%map-for-effect-arity-1 function first-sequence))
1110 ((csubtypep type (specifier-type 'list))
1111 (%map-to-list-arity-1 function first-sequence))
1112 ((csubtypep type (specifier-type '(vector t)))
1113 (%map-to-simple-vector-arity-1 function first-sequence))
1115 (slower-map type))))))))))
1117 (defun map (result-type function first-sequence &rest more-sequences)
1118 (let ((result
1119 (apply #'%map result-type function first-sequence more-sequences)))
1120 (if (or (eq result-type 'nil) (typep result result-type))
1121 result
1122 (error 'simple-type-error
1123 :format-control "MAP result ~S is not a sequence of type ~S"
1124 :datum result
1125 :expected-type result-type
1126 :format-arguments (list result result-type)))))
1128 ;;;; MAP-INTO
1130 (defmacro map-into-lambda (sequences params &body body)
1131 (check-type sequences symbol)
1132 `(flet ((f ,params ,@body))
1133 (declare (truly-dynamic-extent #'f))
1134 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1135 ;; hence the awkward flip between MAP and LOOP.
1136 (if ,sequences
1137 (apply #'%map nil #'f ,sequences)
1138 (loop (f)))))
1140 (define-array-dispatch vector-map-into (data start end fun sequences)
1141 (declare (type index start end)
1142 (type function fun)
1143 (type list sequences))
1144 (let ((index start))
1145 (declare (type index index))
1146 (block mapping
1147 (map-into-lambda sequences (&rest args)
1148 (declare (truly-dynamic-extent args))
1149 (when (eql index end)
1150 (return-from mapping))
1151 (setf (aref data index) (apply fun args))
1152 (incf index)))
1153 index))
1155 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1156 ;;; computing the length of the result sequence since we can detect
1157 ;;; the end during mapping (if MAP even gets that far).
1159 ;;; For each result type, define a mapping function which is
1160 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1161 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1162 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1164 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1165 ;;; Because we are manually doing bounds checking with known types,
1166 ;;; safety is turned off for vectors and lists but kept for generic
1167 ;;; sequences.
1168 (defun map-into (result-sequence function &rest sequences)
1169 (let ((really-fun (%coerce-callable-to-fun function)))
1170 (etypecase result-sequence
1171 (vector
1172 (with-array-data ((data result-sequence) (start) (end)
1173 ;; MAP-INTO ignores fill pointer when mapping
1174 :check-fill-pointer nil)
1175 (let ((new-end (vector-map-into data start end really-fun sequences)))
1176 (when (array-has-fill-pointer-p result-sequence)
1177 (setf (fill-pointer result-sequence) (- new-end start))))))
1178 (list
1179 (let ((node result-sequence))
1180 (declare (type list node))
1181 (map-into-lambda sequences (&rest args)
1182 (declare (truly-dynamic-extent args))
1183 (cond ((null node)
1184 (return-from map-into result-sequence))
1185 ((not (listp (cdr node)))
1186 (error 'simple-type-error
1187 :format-control "~a is not a proper list"
1188 :format-arguments (list result-sequence)
1189 :expected-type 'list
1190 :datum result-sequence)))
1191 (setf (car node) (apply really-fun args))
1192 (setf node (cdr node)))))
1193 (sequence
1194 (multiple-value-bind (iter limit from-end)
1195 (sb!sequence:make-sequence-iterator result-sequence)
1196 (map-into-lambda sequences (&rest args)
1197 (declare (truly-dynamic-extent args) (optimize speed))
1198 (when (sb!sequence:iterator-endp result-sequence
1199 iter limit from-end)
1200 (return-from map-into result-sequence))
1201 (setf (sb!sequence:iterator-element result-sequence iter)
1202 (apply really-fun args))
1203 (setf iter (sb!sequence:iterator-step result-sequence
1204 iter from-end)))))))
1205 result-sequence)
1207 ;;;; REDUCE
1209 (eval-when (:compile-toplevel :execute)
1211 (sb!xc:defmacro mumble-reduce (function
1212 sequence
1214 start
1216 initial-value
1217 ref)
1218 `(do ((index ,start (1+ index))
1219 (value ,initial-value))
1220 ((>= index ,end) value)
1221 (setq value (funcall ,function value
1222 (apply-key ,key (,ref ,sequence index))))))
1224 (sb!xc:defmacro mumble-reduce-from-end (function
1225 sequence
1227 start
1229 initial-value
1230 ref)
1231 `(do ((index (1- ,end) (1- index))
1232 (value ,initial-value)
1233 (terminus (1- ,start)))
1234 ((<= index terminus) value)
1235 (setq value (funcall ,function
1236 (apply-key ,key (,ref ,sequence index))
1237 value))))
1239 (sb!xc:defmacro list-reduce (function
1240 sequence
1242 start
1244 initial-value
1245 ivp)
1246 `(let ((sequence (nthcdr ,start ,sequence)))
1247 (do ((count (if ,ivp ,start (1+ ,start))
1248 (1+ count))
1249 (sequence (if ,ivp sequence (cdr sequence))
1250 (cdr sequence))
1251 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1252 (funcall ,function value (apply-key ,key (car sequence)))))
1253 ((>= count ,end) value))))
1255 (sb!xc:defmacro list-reduce-from-end (function
1256 sequence
1258 start
1260 initial-value
1261 ivp)
1262 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1263 (reverse ,sequence))))
1264 (do ((count (if ,ivp ,start (1+ ,start))
1265 (1+ count))
1266 (sequence (if ,ivp sequence (cdr sequence))
1267 (cdr sequence))
1268 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1269 (funcall ,function (apply-key ,key (car sequence)) value)))
1270 ((>= count ,end) value))))
1272 ) ; EVAL-WHEN
1274 (define-sequence-traverser reduce (function sequence &rest args &key key
1275 from-end start end (initial-value nil ivp))
1276 (declare (type index start)
1277 (truly-dynamic-extent args))
1278 (seq-dispatch sequence
1279 (let ((end (or end length)))
1280 (declare (type index end))
1281 (if (= end start)
1282 (if ivp initial-value (funcall function))
1283 (if from-end
1284 (list-reduce-from-end function sequence key start end
1285 initial-value ivp)
1286 (list-reduce function sequence key start end
1287 initial-value ivp))))
1288 (let ((end (or end length)))
1289 (declare (type index end))
1290 (if (= end start)
1291 (if ivp initial-value (funcall function))
1292 (if from-end
1293 (progn
1294 (when (not ivp)
1295 (setq end (1- (the fixnum end)))
1296 (setq initial-value (apply-key key (aref sequence end))))
1297 (mumble-reduce-from-end function sequence key start end
1298 initial-value aref))
1299 (progn
1300 (when (not ivp)
1301 (setq initial-value (apply-key key (aref sequence start)))
1302 (setq start (1+ start)))
1303 (mumble-reduce function sequence key start end
1304 initial-value aref)))))
1305 (apply #'sb!sequence:reduce function sequence args)))
1307 ;;;; DELETE
1309 (eval-when (:compile-toplevel :execute)
1311 (sb!xc:defmacro mumble-delete (pred)
1312 `(do ((index start (1+ index))
1313 (jndex start)
1314 (number-zapped 0))
1315 ((or (= index (the fixnum end)) (= number-zapped count))
1316 (do ((index index (1+ index)) ; Copy the rest of the vector.
1317 (jndex jndex (1+ jndex)))
1318 ((= index (the fixnum length))
1319 (shrink-vector sequence jndex))
1320 (declare (fixnum index jndex))
1321 (setf (aref sequence jndex) (aref sequence index))))
1322 (declare (fixnum index jndex number-zapped))
1323 (setf (aref sequence jndex) (aref sequence index))
1324 (if ,pred
1325 (incf number-zapped)
1326 (incf jndex))))
1328 (sb!xc:defmacro mumble-delete-from-end (pred)
1329 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1330 (number-zapped 0)
1331 (losers ())
1332 this-element
1333 (terminus (1- start)))
1334 ((or (= index terminus) (= number-zapped count))
1335 (do ((losers losers) ; Delete the losers.
1336 (index start (1+ index))
1337 (jndex start))
1338 ((or (null losers) (= index (the fixnum end)))
1339 (do ((index index (1+ index)) ; Copy the rest of the vector.
1340 (jndex jndex (1+ jndex)))
1341 ((= index (the fixnum length))
1342 (shrink-vector sequence jndex))
1343 (declare (fixnum index jndex))
1344 (setf (aref sequence jndex) (aref sequence index))))
1345 (declare (fixnum index jndex))
1346 (setf (aref sequence jndex) (aref sequence index))
1347 (if (= index (the fixnum (car losers)))
1348 (pop losers)
1349 (incf jndex))))
1350 (declare (fixnum index number-zapped terminus))
1351 (setq this-element (aref sequence index))
1352 (when ,pred
1353 (incf number-zapped)
1354 (push index losers))))
1356 (sb!xc:defmacro normal-mumble-delete ()
1357 `(mumble-delete
1358 (if test-not
1359 (not (funcall test-not item (apply-key key (aref sequence index))))
1360 (funcall test item (apply-key key (aref sequence index))))))
1362 (sb!xc:defmacro normal-mumble-delete-from-end ()
1363 `(mumble-delete-from-end
1364 (if test-not
1365 (not (funcall test-not item (apply-key key this-element)))
1366 (funcall test item (apply-key key this-element)))))
1368 (sb!xc:defmacro list-delete (pred)
1369 `(let ((handle (cons nil sequence)))
1370 (declare (truly-dynamic-extent handle))
1371 (do ((current (nthcdr start sequence) (cdr current))
1372 (previous (nthcdr start handle))
1373 (index start (1+ index))
1374 (number-zapped 0))
1375 ((or (= index (the fixnum end)) (= number-zapped count))
1376 (cdr handle))
1377 (declare (fixnum index number-zapped))
1378 (cond (,pred
1379 (rplacd previous (cdr current))
1380 (incf number-zapped))
1382 (setq previous (cdr previous)))))))
1384 (sb!xc:defmacro list-delete-from-end (pred)
1385 `(let* ((reverse (nreverse (the list sequence)))
1386 (handle (cons nil reverse)))
1387 (declare (truly-dynamic-extent handle))
1388 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1389 (cdr current))
1390 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1391 (index start (1+ index))
1392 (number-zapped 0))
1393 ((or (= index (the fixnum end)) (= number-zapped count))
1394 (nreverse (cdr handle)))
1395 (declare (fixnum index number-zapped))
1396 (cond (,pred
1397 (rplacd previous (cdr current))
1398 (incf number-zapped))
1400 (setq previous (cdr previous)))))))
1402 (sb!xc:defmacro normal-list-delete ()
1403 '(list-delete
1404 (if test-not
1405 (not (funcall test-not item (apply-key key (car current))))
1406 (funcall test item (apply-key key (car current))))))
1408 (sb!xc:defmacro normal-list-delete-from-end ()
1409 '(list-delete-from-end
1410 (if test-not
1411 (not (funcall test-not item (apply-key key (car current))))
1412 (funcall test item (apply-key key (car current))))))
1414 ) ; EVAL-WHEN
1416 (define-sequence-traverser delete
1417 (item sequence &rest args &key from-end test test-not start
1418 end count key)
1419 #!+sb-doc
1420 "Return a sequence formed by destructively removing the specified ITEM from
1421 the given SEQUENCE."
1422 (declare (type fixnum start)
1423 (truly-dynamic-extent args))
1424 (seq-dispatch sequence
1425 (let ((end (or end length)))
1426 (declare (type index end))
1427 (if from-end
1428 (normal-list-delete-from-end)
1429 (normal-list-delete)))
1430 (let ((end (or end length)))
1431 (declare (type index end))
1432 (if from-end
1433 (normal-mumble-delete-from-end)
1434 (normal-mumble-delete)))
1435 (apply #'sb!sequence:delete item sequence args)))
1437 (eval-when (:compile-toplevel :execute)
1439 (sb!xc:defmacro if-mumble-delete ()
1440 `(mumble-delete
1441 (funcall predicate (apply-key key (aref sequence index)))))
1443 (sb!xc:defmacro if-mumble-delete-from-end ()
1444 `(mumble-delete-from-end
1445 (funcall predicate (apply-key key this-element))))
1447 (sb!xc:defmacro if-list-delete ()
1448 '(list-delete
1449 (funcall predicate (apply-key key (car current)))))
1451 (sb!xc:defmacro if-list-delete-from-end ()
1452 '(list-delete-from-end
1453 (funcall predicate (apply-key key (car current)))))
1455 ) ; EVAL-WHEN
1457 (define-sequence-traverser delete-if
1458 (predicate sequence &rest args &key from-end start key end count)
1459 #!+sb-doc
1460 "Return a sequence formed by destructively removing the elements satisfying
1461 the specified PREDICATE from the given SEQUENCE."
1462 (declare (type fixnum start)
1463 (truly-dynamic-extent args))
1464 (seq-dispatch sequence
1465 (let ((end (or end length)))
1466 (declare (type index end))
1467 (if from-end
1468 (if-list-delete-from-end)
1469 (if-list-delete)))
1470 (let ((end (or end length)))
1471 (declare (type index end))
1472 (if from-end
1473 (if-mumble-delete-from-end)
1474 (if-mumble-delete)))
1475 (apply #'sb!sequence:delete-if predicate sequence args)))
1477 (eval-when (:compile-toplevel :execute)
1479 (sb!xc:defmacro if-not-mumble-delete ()
1480 `(mumble-delete
1481 (not (funcall predicate (apply-key key (aref sequence index))))))
1483 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1484 `(mumble-delete-from-end
1485 (not (funcall predicate (apply-key key this-element)))))
1487 (sb!xc:defmacro if-not-list-delete ()
1488 '(list-delete
1489 (not (funcall predicate (apply-key key (car current))))))
1491 (sb!xc:defmacro if-not-list-delete-from-end ()
1492 '(list-delete-from-end
1493 (not (funcall predicate (apply-key key (car current))))))
1495 ) ; EVAL-WHEN
1497 (define-sequence-traverser delete-if-not
1498 (predicate sequence &rest args &key from-end start end key count)
1499 #!+sb-doc
1500 "Return a sequence formed by destructively removing the elements not
1501 satisfying the specified PREDICATE from the given SEQUENCE."
1502 (declare (type fixnum start)
1503 (truly-dynamic-extent args))
1504 (seq-dispatch sequence
1505 (let ((end (or end length)))
1506 (declare (type index end))
1507 (if from-end
1508 (if-not-list-delete-from-end)
1509 (if-not-list-delete)))
1510 (let ((end (or end length)))
1511 (declare (type index end))
1512 (if from-end
1513 (if-not-mumble-delete-from-end)
1514 (if-not-mumble-delete)))
1515 (apply #'sb!sequence:delete-if-not predicate sequence args)))
1517 ;;;; REMOVE
1519 (eval-when (:compile-toplevel :execute)
1521 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1522 ;;; satisfies the predicate.
1523 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1524 `(do ((index ,begin (,bump index))
1525 (result
1526 (do ((index ,left (,bump index))
1527 (result (%make-sequence-like sequence length)))
1528 ((= index (the fixnum ,begin)) result)
1529 (declare (fixnum index))
1530 (setf (aref result index) (aref sequence index))))
1531 (new-index ,begin)
1532 (number-zapped 0)
1533 (this-element))
1534 ((or (= index (the fixnum ,finish))
1535 (= number-zapped count))
1536 (do ((index index (,bump index))
1537 (new-index new-index (,bump new-index)))
1538 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1539 (declare (fixnum index new-index))
1540 (setf (aref result new-index) (aref sequence index))))
1541 (declare (fixnum index new-index number-zapped))
1542 (setq this-element (aref sequence index))
1543 (cond (,pred (incf number-zapped))
1544 (t (setf (aref result new-index) this-element)
1545 (setq new-index (,bump new-index))))))
1547 (sb!xc:defmacro mumble-remove (pred)
1548 `(mumble-remove-macro 1+ 0 start end length ,pred))
1550 (sb!xc:defmacro mumble-remove-from-end (pred)
1551 `(let ((sequence (copy-seq sequence)))
1552 (mumble-delete-from-end ,pred)))
1554 (sb!xc:defmacro normal-mumble-remove ()
1555 `(mumble-remove
1556 (if test-not
1557 (not (funcall test-not item (apply-key key this-element)))
1558 (funcall test item (apply-key key this-element)))))
1560 (sb!xc:defmacro normal-mumble-remove-from-end ()
1561 `(mumble-remove-from-end
1562 (if test-not
1563 (not (funcall test-not item (apply-key key this-element)))
1564 (funcall test item (apply-key key this-element)))))
1566 (sb!xc:defmacro if-mumble-remove ()
1567 `(mumble-remove (funcall predicate (apply-key key this-element))))
1569 (sb!xc:defmacro if-mumble-remove-from-end ()
1570 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1572 (sb!xc:defmacro if-not-mumble-remove ()
1573 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1575 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1576 `(mumble-remove-from-end
1577 (not (funcall predicate (apply-key key this-element)))))
1579 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1580 ;;; the predicate.
1581 (sb!xc:defmacro list-remove-macro (pred reverse?)
1582 `(let* ((sequence ,(if reverse?
1583 '(reverse (the list sequence))
1584 'sequence))
1585 (%start ,(if reverse? '(- length end) 'start))
1586 (%end ,(if reverse? '(- length start) 'end))
1587 (splice (list nil))
1588 (results (do ((index 0 (1+ index))
1589 (before-start splice))
1590 ((= index (the fixnum %start)) before-start)
1591 (declare (fixnum index))
1592 (setq splice
1593 (cdr (rplacd splice (list (pop sequence))))))))
1594 (do ((index %start (1+ index))
1595 (this-element)
1596 (number-zapped 0))
1597 ((or (= index (the fixnum %end)) (= number-zapped count))
1598 (do ((index index (1+ index)))
1599 ((null sequence)
1600 ,(if reverse?
1601 '(nreverse (the list (cdr results)))
1602 '(cdr results)))
1603 (declare (fixnum index))
1604 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1605 (declare (fixnum index number-zapped))
1606 (setq this-element (pop sequence))
1607 (if ,pred
1608 (setq number-zapped (1+ number-zapped))
1609 (setq splice (cdr (rplacd splice (list this-element))))))))
1611 (sb!xc:defmacro list-remove (pred)
1612 `(list-remove-macro ,pred nil))
1614 (sb!xc:defmacro list-remove-from-end (pred)
1615 `(list-remove-macro ,pred t))
1617 (sb!xc:defmacro normal-list-remove ()
1618 `(list-remove
1619 (if test-not
1620 (not (funcall test-not item (apply-key key this-element)))
1621 (funcall test item (apply-key key this-element)))))
1623 (sb!xc:defmacro normal-list-remove-from-end ()
1624 `(list-remove-from-end
1625 (if test-not
1626 (not (funcall test-not item (apply-key key this-element)))
1627 (funcall test item (apply-key key this-element)))))
1629 (sb!xc:defmacro if-list-remove ()
1630 `(list-remove
1631 (funcall predicate (apply-key key this-element))))
1633 (sb!xc:defmacro if-list-remove-from-end ()
1634 `(list-remove-from-end
1635 (funcall predicate (apply-key key this-element))))
1637 (sb!xc:defmacro if-not-list-remove ()
1638 `(list-remove
1639 (not (funcall predicate (apply-key key this-element)))))
1641 (sb!xc:defmacro if-not-list-remove-from-end ()
1642 `(list-remove-from-end
1643 (not (funcall predicate (apply-key key this-element)))))
1645 ) ; EVAL-WHEN
1647 (define-sequence-traverser remove
1648 (item sequence &rest args &key from-end test test-not start
1649 end count key)
1650 #!+sb-doc
1651 "Return a copy of SEQUENCE with elements satisfying the test (default is
1652 EQL) with ITEM removed."
1653 (declare (type fixnum start)
1654 (truly-dynamic-extent args))
1655 (seq-dispatch sequence
1656 (let ((end (or end length)))
1657 (declare (type index end))
1658 (if from-end
1659 (normal-list-remove-from-end)
1660 (normal-list-remove)))
1661 (let ((end (or end length)))
1662 (declare (type index end))
1663 (if from-end
1664 (normal-mumble-remove-from-end)
1665 (normal-mumble-remove)))
1666 (apply #'sb!sequence:remove item sequence args)))
1668 (define-sequence-traverser remove-if
1669 (predicate sequence &rest args &key from-end start end count key)
1670 #!+sb-doc
1671 "Return a copy of sequence with elements satisfying PREDICATE removed."
1672 (declare (type fixnum start)
1673 (truly-dynamic-extent args))
1674 (seq-dispatch sequence
1675 (let ((end (or end length)))
1676 (declare (type index end))
1677 (if from-end
1678 (if-list-remove-from-end)
1679 (if-list-remove)))
1680 (let ((end (or end length)))
1681 (declare (type index end))
1682 (if from-end
1683 (if-mumble-remove-from-end)
1684 (if-mumble-remove)))
1685 (apply #'sb!sequence:remove-if predicate sequence args)))
1687 (define-sequence-traverser remove-if-not
1688 (predicate sequence &rest args &key from-end start end count key)
1689 #!+sb-doc
1690 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1691 (declare (type fixnum start)
1692 (truly-dynamic-extent args))
1693 (seq-dispatch sequence
1694 (let ((end (or end length)))
1695 (declare (type index end))
1696 (if from-end
1697 (if-not-list-remove-from-end)
1698 (if-not-list-remove)))
1699 (let ((end (or end length)))
1700 (declare (type index end))
1701 (if from-end
1702 (if-not-mumble-remove-from-end)
1703 (if-not-mumble-remove)))
1704 (apply #'sb!sequence:remove-if-not predicate sequence args)))
1706 ;;;; REMOVE-DUPLICATES
1708 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1709 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1710 ;;; if we look into the already copied structure (from after :start) and see
1711 ;;; the item. If we check from beginning we check into the rest of the
1712 ;;; original list up to the :end marker (this we have to do by running a
1713 ;;; do loop down the list that far and using our test.
1714 (defun list-remove-duplicates* (list test test-not start end key from-end)
1715 (declare (fixnum start))
1716 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1717 (splice result)
1718 (current list)
1719 (end (or end (length list)))
1720 (hash (and (> (- end start) 20)
1721 test
1722 (not key)
1723 (not test-not)
1724 (or (eql test #'eql)
1725 (eql test #'eq)
1726 (eql test #'equal)
1727 (eql test #'equalp))
1728 (make-hash-table :test test :size (- end start)))))
1729 (do ((index 0 (1+ index)))
1730 ((= index start))
1731 (declare (fixnum index))
1732 (setq splice (cdr (rplacd splice (list (car current)))))
1733 (setq current (cdr current)))
1734 (if hash
1735 (do ((index start (1+ index)))
1736 ((or (and end (= index (the fixnum end)))
1737 (atom current)))
1738 (declare (fixnum index))
1739 ;; The hash table contains links from values that are
1740 ;; already in result to the cons cell *preceding* theirs
1741 ;; in the list. That is, for each value v in the list,
1742 ;; v and (cadr (gethash v hash)) are equal under TEST.
1743 (let ((prev (gethash (car current) hash)))
1744 (cond
1745 ((not prev)
1746 (setf (gethash (car current) hash) splice)
1747 (setq splice (cdr (rplacd splice (list (car current))))))
1748 ((not from-end)
1749 (let* ((old (cdr prev))
1750 (next (cdr old)))
1751 (if next
1752 (let ((next-val (car next)))
1753 ;; (assert (eq (gethash next-val hash) old))
1754 (setf (cdr prev) next
1755 (gethash next-val hash) prev
1756 (gethash (car current) hash) splice
1757 splice (cdr (rplacd splice (list (car current))))))
1758 (setf (car old) (car current)))))))
1759 (setq current (cdr current)))
1760 (do ((index start (1+ index)))
1761 ((or (and end (= index (the fixnum end)))
1762 (atom current)))
1763 (declare (fixnum index))
1764 (if (or (and from-end
1765 (not (if test-not
1766 (member (apply-key key (car current))
1767 (nthcdr (1+ start) result)
1768 :test-not test-not
1769 :key key)
1770 (member (apply-key key (car current))
1771 (nthcdr (1+ start) result)
1772 :test test
1773 :key key))))
1774 (and (not from-end)
1775 (not (do ((it (apply-key key (car current)))
1776 (l (cdr current) (cdr l))
1777 (i (1+ index) (1+ i)))
1778 ((or (atom l) (and end (= i (the fixnum end))))
1780 (declare (fixnum i))
1781 (if (if test-not
1782 (not (funcall test-not
1784 (apply-key key (car l))))
1785 (funcall test it (apply-key key (car l))))
1786 (return t))))))
1787 (setq splice (cdr (rplacd splice (list (car current))))))
1788 (setq current (cdr current))))
1789 (do ()
1790 ((atom current))
1791 (setq splice (cdr (rplacd splice (list (car current)))))
1792 (setq current (cdr current)))
1793 (cdr result)))
1795 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1796 &optional (length (length vector)))
1797 (declare (vector vector) (fixnum start length))
1798 (when (null end) (setf end (length vector)))
1799 (let ((result (%make-sequence-like vector length))
1800 (index 0)
1801 (jndex start))
1802 (declare (fixnum index jndex))
1803 (do ()
1804 ((= index start))
1805 (setf (aref result index) (aref vector index))
1806 (setq index (1+ index)))
1807 (do ((elt))
1808 ((= index end))
1809 (setq elt (aref vector index))
1810 (unless (or (and from-end
1811 (if test-not
1812 (position (apply-key key elt) result
1813 :start start :end jndex
1814 :test-not test-not :key key)
1815 (position (apply-key key elt) result
1816 :start start :end jndex
1817 :test test :key key)))
1818 (and (not from-end)
1819 (if test-not
1820 (position (apply-key key elt) vector
1821 :start (1+ index) :end end
1822 :test-not test-not :key key)
1823 (position (apply-key key elt) vector
1824 :start (1+ index) :end end
1825 :test test :key key))))
1826 (setf (aref result jndex) elt)
1827 (setq jndex (1+ jndex)))
1828 (setq index (1+ index)))
1829 (do ()
1830 ((= index length))
1831 (setf (aref result jndex) (aref vector index))
1832 (setq index (1+ index))
1833 (setq jndex (1+ jndex)))
1834 (%shrink-vector result jndex)))
1836 (define-sequence-traverser remove-duplicates
1837 (sequence &rest args &key test test-not start end from-end key)
1838 #!+sb-doc
1839 "The elements of SEQUENCE are compared pairwise, and if any two match,
1840 the one occurring earlier is discarded, unless FROM-END is true, in
1841 which case the one later in the sequence is discarded. The resulting
1842 sequence is returned.
1844 The :TEST-NOT argument is deprecated."
1845 (declare (fixnum start)
1846 (truly-dynamic-extent args))
1847 (seq-dispatch sequence
1848 (if sequence
1849 (list-remove-duplicates* sequence test test-not
1850 start end key from-end))
1851 (vector-remove-duplicates* sequence test test-not start end key from-end)
1852 (apply #'sb!sequence:remove-duplicates sequence args)))
1854 ;;;; DELETE-DUPLICATES
1856 (defun list-delete-duplicates* (list test test-not key from-end start end)
1857 (declare (fixnum start))
1858 (let ((handle (cons nil list)))
1859 (declare (truly-dynamic-extent handle))
1860 (do ((current (nthcdr start list) (cdr current))
1861 (previous (nthcdr start handle))
1862 (index start (1+ index)))
1863 ((or (and end (= index (the fixnum end))) (null current))
1864 (cdr handle))
1865 (declare (fixnum index))
1866 (if (do ((x (if from-end
1867 (nthcdr (1+ start) handle)
1868 (cdr current))
1869 (cdr x))
1870 (i (1+ index) (1+ i)))
1871 ((or (null x)
1872 (and (not from-end) end (= i (the fixnum end)))
1873 (eq x current))
1874 nil)
1875 (declare (fixnum i))
1876 (if (if test-not
1877 (not (funcall test-not
1878 (apply-key key (car current))
1879 (apply-key key (car x))))
1880 (funcall test
1881 (apply-key key (car current))
1882 (apply-key key (car x))))
1883 (return t)))
1884 (rplacd previous (cdr current))
1885 (setq previous (cdr previous))))))
1887 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1888 &optional (length (length vector)))
1889 (declare (vector vector) (fixnum start length))
1890 (when (null end) (setf end (length vector)))
1891 (do ((index start (1+ index))
1892 (jndex start))
1893 ((= index end)
1894 (do ((index index (1+ index)) ; copy the rest of the vector
1895 (jndex jndex (1+ jndex)))
1896 ((= index length)
1897 (shrink-vector vector jndex))
1898 (setf (aref vector jndex) (aref vector index))))
1899 (declare (fixnum index jndex))
1900 (setf (aref vector jndex) (aref vector index))
1901 (unless (if test-not
1902 (position (apply-key key (aref vector index)) vector :key key
1903 :start (if from-end start (1+ index))
1904 :end (if from-end jndex end)
1905 :test-not test-not)
1906 (position (apply-key key (aref vector index)) vector :key key
1907 :start (if from-end start (1+ index))
1908 :end (if from-end jndex end)
1909 :test test))
1910 (setq jndex (1+ jndex)))))
1912 (define-sequence-traverser delete-duplicates
1913 (sequence &rest args &key test test-not start end from-end key)
1914 #!+sb-doc
1915 "The elements of SEQUENCE are examined, and if any two match, one is
1916 discarded. The resulting sequence, which may be formed by destroying the
1917 given sequence, is returned.
1919 The :TEST-NOT argument is deprecated."
1920 (declare (truly-dynamic-extent args))
1921 (seq-dispatch sequence
1922 (when sequence
1923 (list-delete-duplicates* sequence test test-not
1924 key from-end start end))
1925 (vector-delete-duplicates* sequence test test-not key from-end start end)
1926 (apply #'sb!sequence:delete-duplicates sequence args)))
1928 ;;;; SUBSTITUTE
1930 (defun list-substitute* (pred new list start end count key test test-not old)
1931 (declare (fixnum start end count))
1932 (let* ((result (list nil))
1934 (splice result)
1935 (list list)) ; Get a local list for a stepper.
1936 (do ((index 0 (1+ index)))
1937 ((= index start))
1938 (declare (fixnum index))
1939 (setq splice (cdr (rplacd splice (list (car list)))))
1940 (setq list (cdr list)))
1941 (do ((index start (1+ index)))
1942 ((or (= index end) (null list) (= count 0)))
1943 (declare (fixnum index))
1944 (setq elt (car list))
1945 (setq splice
1946 (cdr (rplacd splice
1947 (list
1948 (cond
1949 ((case pred
1950 (normal
1951 (if test-not
1952 (not
1953 (funcall test-not old (apply-key key elt)))
1954 (funcall test old (apply-key key elt))))
1955 (if (funcall test (apply-key key elt)))
1956 (if-not (not (funcall test (apply-key key elt)))))
1957 (decf count)
1958 new)
1959 (t elt))))))
1960 (setq list (cdr list)))
1961 (do ()
1962 ((null list))
1963 (setq splice (cdr (rplacd splice (list (car list)))))
1964 (setq list (cdr list)))
1965 (cdr result)))
1967 ;;; Replace old with new in sequence moving from left to right by incrementer
1968 ;;; on each pass through the loop. Called by all three substitute functions.
1969 (defun vector-substitute* (pred new sequence incrementer left right length
1970 start end count key test test-not old)
1971 (declare (fixnum start count end incrementer right))
1972 (let ((result (%make-sequence-like sequence length))
1973 (index left))
1974 (declare (fixnum index))
1975 (do ()
1976 ((= index start))
1977 (setf (aref result index) (aref sequence index))
1978 (setq index (+ index incrementer)))
1979 (do ((elt))
1980 ((or (= index end) (= count 0)))
1981 (setq elt (aref sequence index))
1982 (setf (aref result index)
1983 (cond ((case pred
1984 (normal
1985 (if test-not
1986 (not (funcall test-not old (apply-key key elt)))
1987 (funcall test old (apply-key key elt))))
1988 (if (funcall test (apply-key key elt)))
1989 (if-not (not (funcall test (apply-key key elt)))))
1990 (setq count (1- count))
1991 new)
1992 (t elt)))
1993 (setq index (+ index incrementer)))
1994 (do ()
1995 ((= index right))
1996 (setf (aref result index) (aref sequence index))
1997 (setq index (+ index incrementer)))
1998 result))
2000 (eval-when (:compile-toplevel :execute)
2002 (sb!xc:defmacro subst-dispatch (pred)
2003 `(seq-dispatch sequence
2004 (let ((end (or end length)))
2005 (declare (type index end))
2006 (if from-end
2007 (nreverse (list-substitute* ,pred
2009 (reverse sequence)
2010 (- (the fixnum length)
2011 (the fixnum end))
2012 (- (the fixnum length)
2013 (the fixnum start))
2014 count key test test-not old))
2015 (list-substitute* ,pred
2016 new sequence start end count key test test-not
2017 old)))
2019 (let ((end (or end length)))
2020 (declare (type index end))
2021 (if from-end
2022 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2023 -1 length (1- (the fixnum end))
2024 (1- (the fixnum start))
2025 count key test test-not old)
2026 (vector-substitute* ,pred new sequence 1 0 length length
2027 start end count key test test-not old)))
2029 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2030 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2031 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2032 ;; dispatch once per element on PRED's run-time identity.
2033 ,(ecase (cadr pred)
2034 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2035 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2036 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2037 ) ; EVAL-WHEN
2039 (define-sequence-traverser substitute
2040 (new old sequence &rest args &key from-end test test-not
2041 start count end key)
2042 #!+sb-doc
2043 "Return a sequence of the same kind as SEQUENCE with the same elements,
2044 except that all elements equal to OLD are replaced with NEW."
2045 (declare (type fixnum start)
2046 (truly-dynamic-extent args))
2047 (subst-dispatch 'normal))
2049 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2051 (define-sequence-traverser substitute-if
2052 (new predicate sequence &rest args &key from-end start end count key)
2053 #!+sb-doc
2054 "Return a sequence of the same kind as SEQUENCE with the same elements
2055 except that all elements satisfying the PRED are replaced with NEW."
2056 (declare (type fixnum start)
2057 (truly-dynamic-extent args))
2058 (let ((test predicate)
2059 (test-not nil)
2060 old)
2061 (subst-dispatch 'if)))
2063 (define-sequence-traverser substitute-if-not
2064 (new predicate sequence &rest args &key from-end start end count key)
2065 #!+sb-doc
2066 "Return a sequence of the same kind as SEQUENCE with the same elements
2067 except that all elements not satisfying the PRED are replaced with NEW."
2068 (declare (type fixnum start)
2069 (truly-dynamic-extent args))
2070 (let ((test predicate)
2071 (test-not nil)
2072 old)
2073 (subst-dispatch 'if-not)))
2075 ;;;; NSUBSTITUTE
2077 (define-sequence-traverser nsubstitute
2078 (new old sequence &rest args &key from-end test test-not
2079 end count key start)
2080 #!+sb-doc
2081 "Return a sequence of the same kind as SEQUENCE with the same elements
2082 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2083 may be destructively modified."
2084 (declare (type fixnum start)
2085 (truly-dynamic-extent args))
2086 (seq-dispatch sequence
2087 (let ((end (or end length)))
2088 (declare (type index end))
2089 (if from-end
2090 (nreverse (nlist-substitute*
2091 new old (nreverse (the list sequence))
2092 test test-not (- length end) (- length start)
2093 count key))
2094 (nlist-substitute* new old sequence
2095 test test-not start end count key)))
2096 (let ((end (or end length)))
2097 (declare (type index end))
2098 (if from-end
2099 (nvector-substitute* new old sequence -1
2100 test test-not (1- end) (1- start) count key)
2101 (nvector-substitute* new old sequence 1
2102 test test-not start end count key)))
2103 (apply #'sb!sequence:nsubstitute new old sequence args)))
2105 (defun nlist-substitute* (new old sequence test test-not start end count key)
2106 (declare (fixnum start count end))
2107 (do ((list (nthcdr start sequence) (cdr list))
2108 (index start (1+ index)))
2109 ((or (= index end) (null list) (= count 0)) sequence)
2110 (declare (fixnum index))
2111 (when (if test-not
2112 (not (funcall test-not old (apply-key key (car list))))
2113 (funcall test old (apply-key key (car list))))
2114 (rplaca list new)
2115 (setq count (1- count)))))
2117 (defun nvector-substitute* (new old sequence incrementer
2118 test test-not start end count key)
2119 (declare (fixnum start incrementer count end))
2120 (do ((index start (+ index incrementer)))
2121 ((or (= index end) (= count 0)) sequence)
2122 (declare (fixnum index))
2123 (when (if test-not
2124 (not (funcall test-not
2126 (apply-key key (aref sequence index))))
2127 (funcall test old (apply-key key (aref sequence index))))
2128 (setf (aref sequence index) new)
2129 (setq count (1- count)))))
2131 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2133 (define-sequence-traverser nsubstitute-if
2134 (new predicate sequence &rest args &key from-end start end count key)
2135 #!+sb-doc
2136 "Return a sequence of the same kind as SEQUENCE with the same elements
2137 except that all elements satisfying PREDICATE are replaced with NEW.
2138 SEQUENCE may be destructively modified."
2139 (declare (type fixnum start)
2140 (truly-dynamic-extent args))
2141 (seq-dispatch sequence
2142 (let ((end (or end length)))
2143 (declare (type index end))
2144 (if from-end
2145 (nreverse (nlist-substitute-if*
2146 new predicate (nreverse (the list sequence))
2147 (- length end) (- length start) count key))
2148 (nlist-substitute-if* new predicate sequence
2149 start end count key)))
2150 (let ((end (or end length)))
2151 (declare (type index end))
2152 (if from-end
2153 (nvector-substitute-if* new predicate sequence -1
2154 (1- end) (1- start) count key)
2155 (nvector-substitute-if* new predicate sequence 1
2156 start end count key)))
2157 (apply #'sb!sequence:nsubstitute-if new predicate sequence args)))
2159 (defun nlist-substitute-if* (new test sequence start end count key)
2160 (declare (type fixnum end)
2161 (type function test)) ; coercion is done by caller
2162 (do ((list (nthcdr start sequence) (cdr list))
2163 (index start (1+ index)))
2164 ((or (= index end) (null list) (= count 0)) sequence)
2165 (when (funcall test (apply-key key (car list)))
2166 (rplaca list new)
2167 (setq count (1- count)))))
2169 (defun nvector-substitute-if* (new test sequence incrementer
2170 start end count key)
2171 (declare (type fixnum end)
2172 (type function test)) ; coercion is done by caller
2173 (do ((index start (+ index incrementer)))
2174 ((or (= index end) (= count 0)) sequence)
2175 (when (funcall test (apply-key key (aref sequence index)))
2176 (setf (aref sequence index) new)
2177 (setq count (1- count)))))
2179 (define-sequence-traverser nsubstitute-if-not
2180 (new predicate sequence &rest args &key from-end start end count key)
2181 #!+sb-doc
2182 "Return a sequence of the same kind as SEQUENCE with the same elements
2183 except that all elements not satisfying PREDICATE are replaced with NEW.
2184 SEQUENCE may be destructively modified."
2185 (declare (type fixnum start)
2186 (truly-dynamic-extent args))
2187 (seq-dispatch sequence
2188 (let ((end (or end length)))
2189 (declare (fixnum end))
2190 (if from-end
2191 (nreverse (nlist-substitute-if-not*
2192 new predicate (nreverse (the list sequence))
2193 (- length end) (- length start) count key))
2194 (nlist-substitute-if-not* new predicate sequence
2195 start end count key)))
2196 (let ((end (or end length)))
2197 (declare (fixnum end))
2198 (if from-end
2199 (nvector-substitute-if-not* new predicate sequence -1
2200 (1- end) (1- start) count key)
2201 (nvector-substitute-if-not* new predicate sequence 1
2202 start end count key)))
2203 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args)))
2205 (defun nlist-substitute-if-not* (new test sequence start end count key)
2206 (declare (type fixnum end)
2207 (type function test)) ; coercion is done by caller
2208 (do ((list (nthcdr start sequence) (cdr list))
2209 (index start (1+ index)))
2210 ((or (= index end) (null list) (= count 0)) sequence)
2211 (when (not (funcall test (apply-key key (car list))))
2212 (rplaca list new)
2213 (decf count))))
2215 (defun nvector-substitute-if-not* (new test sequence incrementer
2216 start end count key)
2217 (declare (type fixnum end)
2218 (type function test)) ; coercion is done by caller
2219 (do ((index start (+ index incrementer)))
2220 ((or (= index end) (= count 0)) sequence)
2221 (when (not (funcall test (apply-key key (aref sequence index))))
2222 (setf (aref sequence index) new)
2223 (decf count))))
2225 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2227 (defun effective-find-position-test (test test-not)
2228 (effective-find-position-test test test-not))
2229 (defun effective-find-position-key (key)
2230 (effective-find-position-key key))
2232 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2233 (macrolet (;; shared logic for defining %FIND-POSITION and
2234 ;; %FIND-POSITION-IF in terms of various inlineable cases
2235 ;; of the expression defined in FROB and VECTOR*-FROB
2236 (frobs (&optional bit-frob)
2237 `(seq-dispatch sequence-arg
2238 (frob sequence-arg from-end)
2239 (with-array-data ((sequence sequence-arg :offset-var offset)
2240 (start start)
2241 (end end)
2242 :check-fill-pointer t)
2243 (multiple-value-bind (f p)
2244 (macrolet ((frob2 () `(if from-end
2245 (frob sequence t)
2246 (frob sequence nil))))
2247 (typecase sequence
2248 #!+sb-unicode
2249 ((simple-array character (*)) (frob2))
2250 ((simple-array base-char (*)) (frob2))
2251 ,@(when bit-frob
2252 `((simple-bit-vector
2253 (if (and (typep item 'bit)
2254 (eq #'identity key)
2255 (or (eq #'eq test)
2256 (eq #'eql test)
2257 (eq #'equal test)))
2258 (let ((p (%bit-position item sequence
2259 from-end start end)))
2260 (if p
2261 (values item p)
2262 (values nil nil)))
2263 (vector*-frob sequence)))))
2265 (vector*-frob sequence))))
2266 (declare (type (or index null) p))
2267 (values f (and p (the index (- p offset)))))))))
2268 (defun %find-position (item sequence-arg from-end start end key test)
2269 (macrolet ((frob (sequence from-end)
2270 `(%find-position item ,sequence
2271 ,from-end start end key test))
2272 (vector*-frob (sequence)
2273 `(%find-position-vector-macro item ,sequence
2274 from-end start end key test)))
2275 (frobs t)))
2276 (defun %find-position-if (predicate sequence-arg from-end start end key)
2277 (macrolet ((frob (sequence from-end)
2278 `(%find-position-if predicate ,sequence
2279 ,from-end start end key))
2280 (vector*-frob (sequence)
2281 `(%find-position-if-vector-macro predicate ,sequence
2282 from-end start end key)))
2283 (frobs)))
2284 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2285 (macrolet ((frob (sequence from-end)
2286 `(%find-position-if-not predicate ,sequence
2287 ,from-end start end key))
2288 (vector*-frob (sequence)
2289 `(%find-position-if-not-vector-macro predicate ,sequence
2290 from-end start end key)))
2291 (frobs))))
2293 (defun find
2294 (item sequence &rest args &key from-end (start 0) end key test test-not)
2295 (declare (truly-dynamic-extent args))
2296 (seq-dispatch sequence
2297 (nth-value 0 (%find-position
2298 item sequence from-end start end
2299 (effective-find-position-key key)
2300 (effective-find-position-test test test-not)))
2301 (nth-value 0 (%find-position
2302 item sequence from-end start end
2303 (effective-find-position-key key)
2304 (effective-find-position-test test test-not)))
2305 (apply #'sb!sequence:find item sequence args)))
2306 (defun position
2307 (item sequence &rest args &key from-end (start 0) end key test test-not)
2308 (declare (truly-dynamic-extent args))
2309 (seq-dispatch sequence
2310 (nth-value 1 (%find-position
2311 item sequence from-end start end
2312 (effective-find-position-key key)
2313 (effective-find-position-test test test-not)))
2314 (nth-value 1 (%find-position
2315 item sequence from-end start end
2316 (effective-find-position-key key)
2317 (effective-find-position-test test test-not)))
2318 (apply #'sb!sequence:position item sequence args)))
2320 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2321 (declare (truly-dynamic-extent args))
2322 (seq-dispatch sequence
2323 (nth-value 0 (%find-position-if
2324 (%coerce-callable-to-fun predicate)
2325 sequence from-end start end
2326 (effective-find-position-key key)))
2327 (nth-value 0 (%find-position-if
2328 (%coerce-callable-to-fun predicate)
2329 sequence from-end start end
2330 (effective-find-position-key key)))
2331 (apply #'sb!sequence:find-if predicate sequence args)))
2332 (defun position-if
2333 (predicate sequence &rest args &key from-end (start 0) end key)
2334 (declare (truly-dynamic-extent args))
2335 (seq-dispatch sequence
2336 (nth-value 1 (%find-position-if
2337 (%coerce-callable-to-fun predicate)
2338 sequence from-end start end
2339 (effective-find-position-key key)))
2340 (nth-value 1 (%find-position-if
2341 (%coerce-callable-to-fun predicate)
2342 sequence from-end start end
2343 (effective-find-position-key key)))
2344 (apply #'sb!sequence:position-if predicate sequence args)))
2346 (defun find-if-not
2347 (predicate sequence &rest args &key from-end (start 0) end key)
2348 (declare (truly-dynamic-extent args))
2349 (seq-dispatch sequence
2350 (nth-value 0 (%find-position-if-not
2351 (%coerce-callable-to-fun predicate)
2352 sequence from-end start end
2353 (effective-find-position-key key)))
2354 (nth-value 0 (%find-position-if-not
2355 (%coerce-callable-to-fun predicate)
2356 sequence from-end start end
2357 (effective-find-position-key key)))
2358 (apply #'sb!sequence:find-if-not predicate sequence args)))
2359 (defun position-if-not
2360 (predicate sequence &rest args &key from-end (start 0) end key)
2361 (declare (truly-dynamic-extent args))
2362 (seq-dispatch sequence
2363 (nth-value 1 (%find-position-if-not
2364 (%coerce-callable-to-fun predicate)
2365 sequence from-end start end
2366 (effective-find-position-key key)))
2367 (nth-value 1 (%find-position-if-not
2368 (%coerce-callable-to-fun predicate)
2369 sequence from-end start end
2370 (effective-find-position-key key)))
2371 (apply #'sb!sequence:position-if-not predicate sequence args)))
2373 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2375 (eval-when (:compile-toplevel :execute)
2377 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2378 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2379 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2380 `(let ((%start ,(if from-end-p '(1- end) 'start))
2381 (%end ,(if from-end-p '(1- start) 'end)))
2382 (do ((index %start ,next-index)
2383 (count 0))
2384 ((= index (the fixnum %end)) count)
2385 (declare (fixnum index count))
2386 (,(if notp 'unless 'when) ,pred
2387 (setq count (1+ count)))))))
2389 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2390 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2391 `(let ((%start ,(if from-end-p '(- length end) 'start))
2392 (%end ,(if from-end-p '(- length start) 'end))
2393 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2394 (do ((sequence (nthcdr %start ,sequence))
2395 (index %start (1+ index))
2396 (count 0))
2397 ((or (= index (the fixnum %end)) (null sequence)) count)
2398 (declare (fixnum index count))
2399 (,(if notp 'unless 'when) ,pred
2400 (setq count (1+ count)))))))
2403 ) ; EVAL-WHEN
2405 (define-sequence-traverser count-if
2406 (pred sequence &rest args &key from-end start end key)
2407 #!+sb-doc
2408 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2409 (declare (type fixnum start)
2410 (truly-dynamic-extent args))
2411 (let ((pred (%coerce-callable-to-fun pred)))
2412 (seq-dispatch sequence
2413 (let ((end (or end length)))
2414 (declare (type index end))
2415 (if from-end
2416 (list-count-if nil t pred sequence)
2417 (list-count-if nil nil pred sequence)))
2418 (let ((end (or end length)))
2419 (declare (type index end))
2420 (if from-end
2421 (vector-count-if nil t pred sequence)
2422 (vector-count-if nil nil pred sequence)))
2423 (apply #'sb!sequence:count-if pred sequence args))))
2425 (define-sequence-traverser count-if-not
2426 (pred sequence &rest args &key from-end start end key)
2427 #!+sb-doc
2428 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2429 (declare (type fixnum start)
2430 (truly-dynamic-extent args))
2431 (let ((pred (%coerce-callable-to-fun pred)))
2432 (seq-dispatch sequence
2433 (let ((end (or end length)))
2434 (declare (type index end))
2435 (if from-end
2436 (list-count-if t t pred sequence)
2437 (list-count-if t nil pred sequence)))
2438 (let ((end (or end length)))
2439 (declare (type index end))
2440 (if from-end
2441 (vector-count-if t t pred sequence)
2442 (vector-count-if t nil pred sequence)))
2443 (apply #'sb!sequence:count-if-not pred sequence args))))
2445 (define-sequence-traverser count
2446 (item sequence &rest args &key from-end start end
2447 key (test #'eql test-p) (test-not nil test-not-p))
2448 #!+sb-doc
2449 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2450 which defaults to EQL."
2451 (declare (type fixnum start)
2452 (truly-dynamic-extent args))
2453 (when (and test-p test-not-p)
2454 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2455 ;; (CLHS 17.2.1)
2456 (error ":TEST and :TEST-NOT are both present."))
2457 (let ((%test (if test-not-p
2458 (lambda (x)
2459 (not (funcall test-not item x)))
2460 (lambda (x)
2461 (funcall test item x)))))
2462 (seq-dispatch sequence
2463 (let ((end (or end length)))
2464 (declare (type index end))
2465 (if from-end
2466 (list-count-if nil t %test sequence)
2467 (list-count-if nil nil %test sequence)))
2468 (let ((end (or end length)))
2469 (declare (type index end))
2470 (if from-end
2471 (vector-count-if nil t %test sequence)
2472 (vector-count-if nil nil %test sequence)))
2473 (apply #'sb!sequence:count item sequence args))))
2475 ;;;; MISMATCH
2477 (eval-when (:compile-toplevel :execute)
2479 (sb!xc:defmacro match-vars (&rest body)
2480 `(let ((inc (if from-end -1 1))
2481 (start1 (if from-end (1- (the fixnum end1)) start1))
2482 (start2 (if from-end (1- (the fixnum end2)) start2))
2483 (end1 (if from-end (1- (the fixnum start1)) end1))
2484 (end2 (if from-end (1- (the fixnum start2)) end2)))
2485 (declare (fixnum inc start1 start2 end1 end2))
2486 ,@body))
2488 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2489 (declare (ignore end)) ;; ### Should END be used below?
2490 `(let ((,sequence (if from-end
2491 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2492 (reverse (the list ,sequence)))
2493 (nthcdr ,start ,sequence))))
2494 (declare (type list ,sequence))
2495 ,@body))
2497 ) ; EVAL-WHEN
2499 (eval-when (:compile-toplevel :execute)
2501 (sb!xc:defmacro if-mismatch (elt1 elt2)
2502 `(cond ((= (the fixnum index1) (the fixnum end1))
2503 (return (if (= (the fixnum index2) (the fixnum end2))
2505 (if from-end
2506 (1+ (the fixnum index1))
2507 (the fixnum index1)))))
2508 ((= (the fixnum index2) (the fixnum end2))
2509 (return (if from-end (1+ (the fixnum index1)) index1)))
2510 (test-not
2511 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2512 (return (if from-end (1+ (the fixnum index1)) index1))))
2513 (t (if (not (funcall test (apply-key key ,elt1)
2514 (apply-key key ,elt2)))
2515 (return (if from-end (1+ (the fixnum index1)) index1))))))
2517 (sb!xc:defmacro mumble-mumble-mismatch ()
2518 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2519 (index2 start2 (+ index2 (the fixnum inc))))
2520 (())
2521 (declare (fixnum index1 index2))
2522 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2524 (sb!xc:defmacro mumble-list-mismatch ()
2525 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2526 (index2 start2 (+ index2 (the fixnum inc))))
2527 (())
2528 (declare (fixnum index1 index2))
2529 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2531 (sb!xc:defmacro list-mumble-mismatch ()
2532 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2533 (index2 start2 (+ index2 (the fixnum inc))))
2534 (())
2535 (declare (fixnum index1 index2))
2536 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2538 (sb!xc:defmacro list-list-mismatch ()
2539 `(do ((sequence1 sequence1)
2540 (sequence2 sequence2)
2541 (index1 start1 (+ index1 (the fixnum inc)))
2542 (index2 start2 (+ index2 (the fixnum inc))))
2543 (())
2544 (declare (fixnum index1 index2))
2545 (if-mismatch (pop sequence1) (pop sequence2))))
2547 ) ; EVAL-WHEN
2549 (define-sequence-traverser mismatch
2550 (sequence1 sequence2 &rest args &key from-end test test-not
2551 start1 end1 start2 end2 key)
2552 #!+sb-doc
2553 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2554 element-wise. If they are of equal length and match in every element, the
2555 result is NIL. Otherwise, the result is a non-negative integer, the index
2556 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2557 if one is shorter than and a matching prefix of the other, the index within
2558 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2559 :FROM-END argument is given, then one plus the index of the rightmost
2560 position in which the sequences differ is returned."
2561 (declare (type fixnum start1 start2))
2562 (declare (truly-dynamic-extent args))
2563 (seq-dispatch sequence1
2564 (seq-dispatch sequence2
2565 (let ((end1 (or end1 length1))
2566 (end2 (or end2 length2)))
2567 (declare (type index end1 end2))
2568 (match-vars
2569 (matchify-list (sequence1 start1 length1 end1)
2570 (matchify-list (sequence2 start2 length2 end2)
2571 (list-list-mismatch)))))
2572 (let ((end1 (or end1 length1))
2573 (end2 (or end2 length2)))
2574 (declare (type index end1 end2))
2575 (match-vars
2576 (matchify-list (sequence1 start1 length1 end1)
2577 (list-mumble-mismatch))))
2578 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2579 (seq-dispatch sequence2
2580 (let ((end1 (or end1 length1))
2581 (end2 (or end2 length2)))
2582 (declare (type index end1 end2))
2583 (match-vars
2584 (matchify-list (sequence2 start2 length2 end2)
2585 (mumble-list-mismatch))))
2586 (let ((end1 (or end1 length1))
2587 (end2 (or end2 length2)))
2588 (declare (type index end1 end2))
2589 (match-vars
2590 (mumble-mumble-mismatch)))
2591 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2592 (apply #'sb!sequence:mismatch sequence1 sequence2 args)))
2595 ;;; search comparison functions
2597 (eval-when (:compile-toplevel :execute)
2599 ;;; Compare two elements and return if they don't match.
2600 (sb!xc:defmacro compare-elements (elt1 elt2)
2601 `(if test-not
2602 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2603 (return nil)
2605 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2606 (return nil)
2607 t)))
2609 (sb!xc:defmacro search-compare-list-list (main sub)
2610 `(do ((main ,main (cdr main))
2611 (jndex start1 (1+ jndex))
2612 (sub (nthcdr start1 ,sub) (cdr sub)))
2613 ((or (endp main) (endp sub) (<= end1 jndex))
2615 (declare (type (integer 0) jndex))
2616 (compare-elements (car sub) (car main))))
2618 (sb!xc:defmacro search-compare-list-vector (main sub)
2619 `(do ((main ,main (cdr main))
2620 (index start1 (1+ index)))
2621 ((or (endp main) (= index end1)) t)
2622 (compare-elements (aref ,sub index) (car main))))
2624 (sb!xc:defmacro search-compare-vector-list (main sub index)
2625 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2626 (jndex start1 (1+ jndex))
2627 (index ,index (1+ index)))
2628 ((or (<= end1 jndex) (endp sub)) t)
2629 (declare (type (integer 0) jndex))
2630 (compare-elements (car sub) (aref ,main index))))
2632 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2633 `(do ((index ,index (1+ index))
2634 (sub-index start1 (1+ sub-index)))
2635 ((= sub-index end1) t)
2636 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2638 (sb!xc:defmacro search-compare (main-type main sub index)
2639 (if (eq main-type 'list)
2640 `(seq-dispatch ,sub
2641 (search-compare-list-list ,main ,sub)
2642 (search-compare-list-vector ,main ,sub)
2643 ;; KLUDGE: just hack it together so that it works
2644 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2645 `(seq-dispatch ,sub
2646 (search-compare-vector-list ,main ,sub ,index)
2647 (search-compare-vector-vector ,main ,sub ,index)
2648 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2650 ) ; EVAL-WHEN
2652 ;;;; SEARCH
2654 (eval-when (:compile-toplevel :execute)
2656 (sb!xc:defmacro list-search (main sub)
2657 `(do ((main (nthcdr start2 ,main) (cdr main))
2658 (index2 start2 (1+ index2))
2659 (terminus (- end2 (the (integer 0) (- end1 start1))))
2660 (last-match ()))
2661 ((> index2 terminus) last-match)
2662 (declare (type (integer 0) index2))
2663 (if (search-compare list main ,sub index2)
2664 (if from-end
2665 (setq last-match index2)
2666 (return index2)))))
2668 (sb!xc:defmacro vector-search (main sub)
2669 `(do ((index2 start2 (1+ index2))
2670 (terminus (- end2 (the (integer 0) (- end1 start1))))
2671 (last-match ()))
2672 ((> index2 terminus) last-match)
2673 (declare (type (integer 0) index2))
2674 (if (search-compare vector ,main ,sub index2)
2675 (if from-end
2676 (setq last-match index2)
2677 (return index2)))))
2679 ) ; EVAL-WHEN
2681 (define-sequence-traverser search
2682 (sequence1 sequence2 &rest args &key
2683 from-end test test-not start1 end1 start2 end2 key)
2684 (declare (type fixnum start1 start2)
2685 (truly-dynamic-extent args))
2686 (seq-dispatch sequence2
2687 (let ((end1 (or end1 length1))
2688 (end2 (or end2 length2)))
2689 (declare (type index end1 end2))
2690 (list-search sequence2 sequence1))
2691 (let ((end1 (or end1 length1))
2692 (end2 (or end2 length2)))
2693 (declare (type index end1 end2))
2694 (vector-search sequence2 sequence1))
2695 (apply #'sb!sequence:search sequence1 sequence2 args)))
2697 ;;; FIXME: this was originally in array.lisp; it might be better to
2698 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2699 ;;; a new early-seq.lisp file.
2700 (defun fill-data-vector (vector dimensions initial-contents)
2701 (let ((index 0))
2702 (labels ((frob (axis dims contents)
2703 (cond ((null dims)
2704 (setf (aref vector index) contents)
2705 (incf index))
2707 (unless (typep contents 'sequence)
2708 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2709 sequence, but ~W more layer~:P needed."
2710 contents
2711 (- (length dimensions) axis)))
2712 (unless (= (length contents) (car dims))
2713 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2714 axis ~W is ~W, but ~S is ~W long."
2715 axis (car dims) contents (length contents)))
2716 (sb!sequence:dosequence (content contents)
2717 (frob (1+ axis) (cdr dims) content))))))
2718 (frob 0 dimensions initial-contents))))