Declare EXPLICIT-CHECK on CONCATENATE, MAKE-STRING, SET-PPRINT-DISPATCH.
[sbcl.git] / src / code / seq.lisp
blob9a75ed48eee035dff723645dcf88331d5560cc1a
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 (destructuring-bind (state limit from-end step endp elt &rest ignore)
1021 (declare (type function step endp elt)
1022 (ignore ignore))
1023 (let ((s (car in-sequences)))
1024 (if (funcall endp s state limit from-end)
1025 (return-from %map-for-effect nil)
1026 (progn
1027 (setf (car in-apply-args) (funcall elt s state))
1028 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
1029 (defun %map-to-list (fun sequences)
1030 (declare (type function fun)
1031 (type list sequences))
1032 (let ((result nil))
1033 (flet ((f (&rest args)
1034 (declare (truly-dynamic-extent args))
1035 (push (apply fun args) result)))
1036 (declare (truly-dynamic-extent #'f))
1037 (%map-for-effect #'f sequences))
1038 (nreverse result)))
1039 (defun %map-to-vector (output-type-spec fun sequences)
1040 (declare (type function fun)
1041 (type list sequences))
1042 (let ((min-len 0))
1043 (flet ((f (&rest args)
1044 (declare (truly-dynamic-extent args))
1045 (declare (ignore args))
1046 (incf min-len)))
1047 (declare (truly-dynamic-extent #'f))
1048 (%map-for-effect #'f sequences))
1049 (let ((result (make-sequence output-type-spec min-len))
1050 (i 0))
1051 (declare (type (simple-array * (*)) result))
1052 (flet ((f (&rest args)
1053 (declare (truly-dynamic-extent args))
1054 (setf (aref result i) (apply fun args))
1055 (incf i)))
1056 (declare (truly-dynamic-extent #'f))
1057 (%map-for-effect #'f sequences))
1058 result)))
1060 ;;; %MAP is just MAP without the final just-to-be-sure check that
1061 ;;; length of the output sequence matches any length specified
1062 ;;; in RESULT-TYPE.
1063 (defun %map (result-type function first-sequence &rest more-sequences)
1064 (labels ((slower-map (type)
1065 (let ((really-fun (%coerce-callable-to-fun function))
1066 (sequences (cons first-sequence more-sequences)))
1067 (cond
1068 ((eq type *empty-type*)
1069 (%map-for-effect really-fun sequences))
1070 ((csubtypep type (specifier-type 'list))
1071 (%map-to-list really-fun sequences))
1072 ((csubtypep type (specifier-type 'vector))
1073 (%map-to-vector result-type really-fun sequences))
1074 ((and (csubtypep type (specifier-type 'sequence))
1075 (awhen (find-class result-type nil)
1076 (apply #'sb!sequence:map
1077 (sb!mop:class-prototype
1078 (sb!pcl:ensure-class-finalized it))
1079 really-fun sequences))))
1081 (bad-sequence-type-error result-type)))))
1082 (slow-map ()
1083 (let ((type (specifier-type result-type)))
1084 (cond
1085 (more-sequences
1086 (slower-map type))
1087 ((eq type *empty-type*)
1088 (%map-for-effect-arity-1 function first-sequence))
1089 ((csubtypep type (specifier-type 'list))
1090 (%map-to-list-arity-1 function first-sequence))
1091 ((or (csubtypep type (specifier-type 'simple-vector))
1092 (csubtypep type (specifier-type '(vector t))))
1093 (%map-to-simple-vector-arity-1 function first-sequence))
1095 (slower-map type))))))
1096 ;; Handle some easy cases faster
1097 (cond (more-sequences
1098 (slow-map))
1099 ((null result-type)
1100 (%map-for-effect-arity-1 function first-sequence))
1101 ((or (eq result-type 'list)
1102 (eq result-type 'cons))
1103 (%map-to-list-arity-1 function first-sequence))
1104 ((or (eq result-type 'vector)
1105 (eq result-type 'simple-vector))
1106 (%map-to-simple-vector-arity-1 function first-sequence))
1108 (slow-map)))))
1110 (defun map (result-type function first-sequence &rest more-sequences)
1111 (apply #'%map
1112 result-type
1113 function
1114 first-sequence
1115 more-sequences))
1117 ;;;; MAP-INTO
1119 (defmacro map-into-lambda (sequences params &body body)
1120 (check-type sequences symbol)
1121 `(flet ((f ,params ,@body))
1122 (declare (truly-dynamic-extent #'f))
1123 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1124 ;; hence the awkward flip between MAP and LOOP.
1125 (if ,sequences
1126 (apply #'map nil #'f ,sequences)
1127 (loop (f)))))
1129 (define-array-dispatch vector-map-into (data start end fun sequences)
1130 (declare (type index start end)
1131 (type function fun)
1132 (type list sequences))
1133 (let ((index start))
1134 (declare (type index index))
1135 (block mapping
1136 (map-into-lambda sequences (&rest args)
1137 (declare (truly-dynamic-extent args))
1138 (when (eql index end)
1139 (return-from mapping))
1140 (setf (aref data index) (apply fun args))
1141 (incf index)))
1142 index))
1144 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1145 ;;; computing the length of the result sequence since we can detect
1146 ;;; the end during mapping (if MAP even gets that far).
1148 ;;; For each result type, define a mapping function which is
1149 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1150 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1151 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1153 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1154 ;;; Because we are manually doing bounds checking with known types,
1155 ;;; safety is turned off for vectors and lists but kept for generic
1156 ;;; sequences.
1157 (defun map-into (result-sequence function &rest sequences)
1158 (let ((really-fun (%coerce-callable-to-fun function)))
1159 (etypecase result-sequence
1160 (vector
1161 (with-array-data ((data result-sequence) (start) (end)
1162 ;; MAP-INTO ignores fill pointer when mapping
1163 :check-fill-pointer nil)
1164 (let ((new-end (vector-map-into data start end really-fun sequences)))
1165 (when (array-has-fill-pointer-p result-sequence)
1166 (setf (fill-pointer result-sequence) (- new-end start))))))
1167 (list
1168 (let ((node result-sequence))
1169 (declare (type list node))
1170 (map-into-lambda sequences (&rest args)
1171 (declare (truly-dynamic-extent args))
1172 (cond ((null node)
1173 (return-from map-into result-sequence))
1174 ((not (listp (cdr node)))
1175 (error 'simple-type-error
1176 :format-control "~a is not a proper list"
1177 :format-arguments (list result-sequence)
1178 :expected-type 'list
1179 :datum result-sequence)))
1180 (setf (car node) (apply really-fun args))
1181 (setf node (cdr node)))))
1182 (sequence
1183 (multiple-value-bind (iter limit from-end)
1184 (sb!sequence:make-sequence-iterator result-sequence)
1185 (map-into-lambda sequences (&rest args)
1186 (declare (truly-dynamic-extent args) (optimize speed))
1187 (when (sb!sequence:iterator-endp result-sequence
1188 iter limit from-end)
1189 (return-from map-into result-sequence))
1190 (setf (sb!sequence:iterator-element result-sequence iter)
1191 (apply really-fun args))
1192 (setf iter (sb!sequence:iterator-step result-sequence
1193 iter from-end)))))))
1194 result-sequence)
1196 ;;;; REDUCE
1198 (eval-when (:compile-toplevel :execute)
1200 (sb!xc:defmacro mumble-reduce (function
1201 sequence
1203 start
1205 initial-value
1206 ref)
1207 `(do ((index ,start (1+ index))
1208 (value ,initial-value))
1209 ((>= index ,end) value)
1210 (setq value (funcall ,function value
1211 (apply-key ,key (,ref ,sequence index))))))
1213 (sb!xc:defmacro mumble-reduce-from-end (function
1214 sequence
1216 start
1218 initial-value
1219 ref)
1220 `(do ((index (1- ,end) (1- index))
1221 (value ,initial-value)
1222 (terminus (1- ,start)))
1223 ((<= index terminus) value)
1224 (setq value (funcall ,function
1225 (apply-key ,key (,ref ,sequence index))
1226 value))))
1228 (sb!xc:defmacro list-reduce (function
1229 sequence
1231 start
1233 initial-value
1234 ivp)
1235 `(let ((sequence (nthcdr ,start ,sequence)))
1236 (do ((count (if ,ivp ,start (1+ ,start))
1237 (1+ count))
1238 (sequence (if ,ivp sequence (cdr sequence))
1239 (cdr sequence))
1240 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1241 (funcall ,function value (apply-key ,key (car sequence)))))
1242 ((>= count ,end) value))))
1244 (sb!xc:defmacro list-reduce-from-end (function
1245 sequence
1247 start
1249 initial-value
1250 ivp)
1251 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1252 (reverse ,sequence))))
1253 (do ((count (if ,ivp ,start (1+ ,start))
1254 (1+ count))
1255 (sequence (if ,ivp sequence (cdr sequence))
1256 (cdr sequence))
1257 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1258 (funcall ,function (apply-key ,key (car sequence)) value)))
1259 ((>= count ,end) value))))
1261 ) ; EVAL-WHEN
1263 (define-sequence-traverser reduce (function sequence &rest args &key key
1264 from-end start end (initial-value nil ivp))
1265 (declare (type index start)
1266 (truly-dynamic-extent args))
1267 (seq-dispatch sequence
1268 (let ((end (or end length)))
1269 (declare (type index end))
1270 (if (= end start)
1271 (if ivp initial-value (funcall function))
1272 (if from-end
1273 (list-reduce-from-end function sequence key start end
1274 initial-value ivp)
1275 (list-reduce function sequence key start end
1276 initial-value ivp))))
1277 (let ((end (or end length)))
1278 (declare (type index end))
1279 (if (= end start)
1280 (if ivp initial-value (funcall function))
1281 (if from-end
1282 (progn
1283 (when (not ivp)
1284 (setq end (1- (the fixnum end)))
1285 (setq initial-value (apply-key key (aref sequence end))))
1286 (mumble-reduce-from-end function sequence key start end
1287 initial-value aref))
1288 (progn
1289 (when (not ivp)
1290 (setq initial-value (apply-key key (aref sequence start)))
1291 (setq start (1+ start)))
1292 (mumble-reduce function sequence key start end
1293 initial-value aref)))))
1294 (apply #'sb!sequence:reduce function sequence args)))
1296 ;;;; DELETE
1298 (eval-when (:compile-toplevel :execute)
1300 (sb!xc:defmacro mumble-delete (pred)
1301 `(do ((index start (1+ index))
1302 (jndex start)
1303 (number-zapped 0))
1304 ((or (= index (the fixnum end)) (= number-zapped count))
1305 (do ((index index (1+ index)) ; Copy the rest of the vector.
1306 (jndex jndex (1+ jndex)))
1307 ((= index (the fixnum length))
1308 (shrink-vector sequence jndex))
1309 (declare (fixnum index jndex))
1310 (setf (aref sequence jndex) (aref sequence index))))
1311 (declare (fixnum index jndex number-zapped))
1312 (setf (aref sequence jndex) (aref sequence index))
1313 (if ,pred
1314 (incf number-zapped)
1315 (incf jndex))))
1317 (sb!xc:defmacro mumble-delete-from-end (pred)
1318 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1319 (number-zapped 0)
1320 (losers ())
1321 this-element
1322 (terminus (1- start)))
1323 ((or (= index terminus) (= number-zapped count))
1324 (do ((losers losers) ; Delete the losers.
1325 (index start (1+ index))
1326 (jndex start))
1327 ((or (null losers) (= index (the fixnum end)))
1328 (do ((index index (1+ index)) ; Copy the rest of the vector.
1329 (jndex jndex (1+ jndex)))
1330 ((= index (the fixnum length))
1331 (shrink-vector sequence jndex))
1332 (declare (fixnum index jndex))
1333 (setf (aref sequence jndex) (aref sequence index))))
1334 (declare (fixnum index jndex))
1335 (setf (aref sequence jndex) (aref sequence index))
1336 (if (= index (the fixnum (car losers)))
1337 (pop losers)
1338 (incf jndex))))
1339 (declare (fixnum index number-zapped terminus))
1340 (setq this-element (aref sequence index))
1341 (when ,pred
1342 (incf number-zapped)
1343 (push index losers))))
1345 (sb!xc:defmacro normal-mumble-delete ()
1346 `(mumble-delete
1347 (if test-not
1348 (not (funcall test-not item (apply-key key (aref sequence index))))
1349 (funcall test item (apply-key key (aref sequence index))))))
1351 (sb!xc:defmacro normal-mumble-delete-from-end ()
1352 `(mumble-delete-from-end
1353 (if test-not
1354 (not (funcall test-not item (apply-key key this-element)))
1355 (funcall test item (apply-key key this-element)))))
1357 (sb!xc:defmacro list-delete (pred)
1358 `(let ((handle (cons nil sequence)))
1359 (declare (truly-dynamic-extent handle))
1360 (do ((current (nthcdr start sequence) (cdr current))
1361 (previous (nthcdr start handle))
1362 (index start (1+ index))
1363 (number-zapped 0))
1364 ((or (= index (the fixnum end)) (= number-zapped count))
1365 (cdr handle))
1366 (declare (fixnum index number-zapped))
1367 (cond (,pred
1368 (rplacd previous (cdr current))
1369 (incf number-zapped))
1371 (setq previous (cdr previous)))))))
1373 (sb!xc:defmacro list-delete-from-end (pred)
1374 `(let* ((reverse (nreverse (the list sequence)))
1375 (handle (cons nil reverse)))
1376 (declare (truly-dynamic-extent handle))
1377 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1378 (cdr current))
1379 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1380 (index start (1+ index))
1381 (number-zapped 0))
1382 ((or (= index (the fixnum end)) (= number-zapped count))
1383 (nreverse (cdr handle)))
1384 (declare (fixnum index number-zapped))
1385 (cond (,pred
1386 (rplacd previous (cdr current))
1387 (incf number-zapped))
1389 (setq previous (cdr previous)))))))
1391 (sb!xc:defmacro normal-list-delete ()
1392 '(list-delete
1393 (if test-not
1394 (not (funcall test-not item (apply-key key (car current))))
1395 (funcall test item (apply-key key (car current))))))
1397 (sb!xc:defmacro normal-list-delete-from-end ()
1398 '(list-delete-from-end
1399 (if test-not
1400 (not (funcall test-not item (apply-key key (car current))))
1401 (funcall test item (apply-key key (car current))))))
1403 ) ; EVAL-WHEN
1405 (define-sequence-traverser delete
1406 (item sequence &rest args &key from-end test test-not start
1407 end count key)
1408 #!+sb-doc
1409 "Return a sequence formed by destructively removing the specified ITEM from
1410 the given SEQUENCE."
1411 (declare (type fixnum start)
1412 (truly-dynamic-extent args))
1413 (seq-dispatch sequence
1414 (let ((end (or end length)))
1415 (declare (type index end))
1416 (if from-end
1417 (normal-list-delete-from-end)
1418 (normal-list-delete)))
1419 (let ((end (or end length)))
1420 (declare (type index end))
1421 (if from-end
1422 (normal-mumble-delete-from-end)
1423 (normal-mumble-delete)))
1424 (apply #'sb!sequence:delete item sequence args)))
1426 (eval-when (:compile-toplevel :execute)
1428 (sb!xc:defmacro if-mumble-delete ()
1429 `(mumble-delete
1430 (funcall predicate (apply-key key (aref sequence index)))))
1432 (sb!xc:defmacro if-mumble-delete-from-end ()
1433 `(mumble-delete-from-end
1434 (funcall predicate (apply-key key this-element))))
1436 (sb!xc:defmacro if-list-delete ()
1437 '(list-delete
1438 (funcall predicate (apply-key key (car current)))))
1440 (sb!xc:defmacro if-list-delete-from-end ()
1441 '(list-delete-from-end
1442 (funcall predicate (apply-key key (car current)))))
1444 ) ; EVAL-WHEN
1446 (define-sequence-traverser delete-if
1447 (predicate sequence &rest args &key from-end start key end count)
1448 #!+sb-doc
1449 "Return a sequence formed by destructively removing the elements satisfying
1450 the specified PREDICATE from the given SEQUENCE."
1451 (declare (type fixnum start)
1452 (truly-dynamic-extent args))
1453 (seq-dispatch sequence
1454 (let ((end (or end length)))
1455 (declare (type index end))
1456 (if from-end
1457 (if-list-delete-from-end)
1458 (if-list-delete)))
1459 (let ((end (or end length)))
1460 (declare (type index end))
1461 (if from-end
1462 (if-mumble-delete-from-end)
1463 (if-mumble-delete)))
1464 (apply #'sb!sequence:delete-if predicate sequence args)))
1466 (eval-when (:compile-toplevel :execute)
1468 (sb!xc:defmacro if-not-mumble-delete ()
1469 `(mumble-delete
1470 (not (funcall predicate (apply-key key (aref sequence index))))))
1472 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1473 `(mumble-delete-from-end
1474 (not (funcall predicate (apply-key key this-element)))))
1476 (sb!xc:defmacro if-not-list-delete ()
1477 '(list-delete
1478 (not (funcall predicate (apply-key key (car current))))))
1480 (sb!xc:defmacro if-not-list-delete-from-end ()
1481 '(list-delete-from-end
1482 (not (funcall predicate (apply-key key (car current))))))
1484 ) ; EVAL-WHEN
1486 (define-sequence-traverser delete-if-not
1487 (predicate sequence &rest args &key from-end start end key count)
1488 #!+sb-doc
1489 "Return a sequence formed by destructively removing the elements not
1490 satisfying the specified PREDICATE from the given SEQUENCE."
1491 (declare (type fixnum start)
1492 (truly-dynamic-extent args))
1493 (seq-dispatch sequence
1494 (let ((end (or end length)))
1495 (declare (type index end))
1496 (if from-end
1497 (if-not-list-delete-from-end)
1498 (if-not-list-delete)))
1499 (let ((end (or end length)))
1500 (declare (type index end))
1501 (if from-end
1502 (if-not-mumble-delete-from-end)
1503 (if-not-mumble-delete)))
1504 (apply #'sb!sequence:delete-if-not predicate sequence args)))
1506 ;;;; REMOVE
1508 (eval-when (:compile-toplevel :execute)
1510 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1511 ;;; satisfies the predicate.
1512 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1513 `(do ((index ,begin (,bump index))
1514 (result
1515 (do ((index ,left (,bump index))
1516 (result (%make-sequence-like sequence length)))
1517 ((= index (the fixnum ,begin)) result)
1518 (declare (fixnum index))
1519 (setf (aref result index) (aref sequence index))))
1520 (new-index ,begin)
1521 (number-zapped 0)
1522 (this-element))
1523 ((or (= index (the fixnum ,finish))
1524 (= number-zapped count))
1525 (do ((index index (,bump index))
1526 (new-index new-index (,bump new-index)))
1527 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1528 (declare (fixnum index new-index))
1529 (setf (aref result new-index) (aref sequence index))))
1530 (declare (fixnum index new-index number-zapped))
1531 (setq this-element (aref sequence index))
1532 (cond (,pred (incf number-zapped))
1533 (t (setf (aref result new-index) this-element)
1534 (setq new-index (,bump new-index))))))
1536 (sb!xc:defmacro mumble-remove (pred)
1537 `(mumble-remove-macro 1+ 0 start end length ,pred))
1539 (sb!xc:defmacro mumble-remove-from-end (pred)
1540 `(let ((sequence (copy-seq sequence)))
1541 (mumble-delete-from-end ,pred)))
1543 (sb!xc:defmacro normal-mumble-remove ()
1544 `(mumble-remove
1545 (if test-not
1546 (not (funcall test-not item (apply-key key this-element)))
1547 (funcall test item (apply-key key this-element)))))
1549 (sb!xc:defmacro normal-mumble-remove-from-end ()
1550 `(mumble-remove-from-end
1551 (if test-not
1552 (not (funcall test-not item (apply-key key this-element)))
1553 (funcall test item (apply-key key this-element)))))
1555 (sb!xc:defmacro if-mumble-remove ()
1556 `(mumble-remove (funcall predicate (apply-key key this-element))))
1558 (sb!xc:defmacro if-mumble-remove-from-end ()
1559 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1561 (sb!xc:defmacro if-not-mumble-remove ()
1562 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1564 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1565 `(mumble-remove-from-end
1566 (not (funcall predicate (apply-key key this-element)))))
1568 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1569 ;;; the predicate.
1570 (sb!xc:defmacro list-remove-macro (pred reverse?)
1571 `(let* ((sequence ,(if reverse?
1572 '(reverse (the list sequence))
1573 'sequence))
1574 (%start ,(if reverse? '(- length end) 'start))
1575 (%end ,(if reverse? '(- length start) 'end))
1576 (splice (list nil))
1577 (results (do ((index 0 (1+ index))
1578 (before-start splice))
1579 ((= index (the fixnum %start)) before-start)
1580 (declare (fixnum index))
1581 (setq splice
1582 (cdr (rplacd splice (list (pop sequence))))))))
1583 (do ((index %start (1+ index))
1584 (this-element)
1585 (number-zapped 0))
1586 ((or (= index (the fixnum %end)) (= number-zapped count))
1587 (do ((index index (1+ index)))
1588 ((null sequence)
1589 ,(if reverse?
1590 '(nreverse (the list (cdr results)))
1591 '(cdr results)))
1592 (declare (fixnum index))
1593 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1594 (declare (fixnum index number-zapped))
1595 (setq this-element (pop sequence))
1596 (if ,pred
1597 (setq number-zapped (1+ number-zapped))
1598 (setq splice (cdr (rplacd splice (list this-element))))))))
1600 (sb!xc:defmacro list-remove (pred)
1601 `(list-remove-macro ,pred nil))
1603 (sb!xc:defmacro list-remove-from-end (pred)
1604 `(list-remove-macro ,pred t))
1606 (sb!xc:defmacro normal-list-remove ()
1607 `(list-remove
1608 (if test-not
1609 (not (funcall test-not item (apply-key key this-element)))
1610 (funcall test item (apply-key key this-element)))))
1612 (sb!xc:defmacro normal-list-remove-from-end ()
1613 `(list-remove-from-end
1614 (if test-not
1615 (not (funcall test-not item (apply-key key this-element)))
1616 (funcall test item (apply-key key this-element)))))
1618 (sb!xc:defmacro if-list-remove ()
1619 `(list-remove
1620 (funcall predicate (apply-key key this-element))))
1622 (sb!xc:defmacro if-list-remove-from-end ()
1623 `(list-remove-from-end
1624 (funcall predicate (apply-key key this-element))))
1626 (sb!xc:defmacro if-not-list-remove ()
1627 `(list-remove
1628 (not (funcall predicate (apply-key key this-element)))))
1630 (sb!xc:defmacro if-not-list-remove-from-end ()
1631 `(list-remove-from-end
1632 (not (funcall predicate (apply-key key this-element)))))
1634 ) ; EVAL-WHEN
1636 (define-sequence-traverser remove
1637 (item sequence &rest args &key from-end test test-not start
1638 end count key)
1639 #!+sb-doc
1640 "Return a copy of SEQUENCE with elements satisfying the test (default is
1641 EQL) with ITEM removed."
1642 (declare (type fixnum start)
1643 (truly-dynamic-extent args))
1644 (seq-dispatch sequence
1645 (let ((end (or end length)))
1646 (declare (type index end))
1647 (if from-end
1648 (normal-list-remove-from-end)
1649 (normal-list-remove)))
1650 (let ((end (or end length)))
1651 (declare (type index end))
1652 (if from-end
1653 (normal-mumble-remove-from-end)
1654 (normal-mumble-remove)))
1655 (apply #'sb!sequence:remove item sequence args)))
1657 (define-sequence-traverser remove-if
1658 (predicate sequence &rest args &key from-end start end count key)
1659 #!+sb-doc
1660 "Return a copy of sequence with elements satisfying PREDICATE removed."
1661 (declare (type fixnum start)
1662 (truly-dynamic-extent args))
1663 (seq-dispatch sequence
1664 (let ((end (or end length)))
1665 (declare (type index end))
1666 (if from-end
1667 (if-list-remove-from-end)
1668 (if-list-remove)))
1669 (let ((end (or end length)))
1670 (declare (type index end))
1671 (if from-end
1672 (if-mumble-remove-from-end)
1673 (if-mumble-remove)))
1674 (apply #'sb!sequence:remove-if predicate sequence args)))
1676 (define-sequence-traverser remove-if-not
1677 (predicate sequence &rest args &key from-end start end count key)
1678 #!+sb-doc
1679 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1680 (declare (type fixnum start)
1681 (truly-dynamic-extent args))
1682 (seq-dispatch sequence
1683 (let ((end (or end length)))
1684 (declare (type index end))
1685 (if from-end
1686 (if-not-list-remove-from-end)
1687 (if-not-list-remove)))
1688 (let ((end (or end length)))
1689 (declare (type index end))
1690 (if from-end
1691 (if-not-mumble-remove-from-end)
1692 (if-not-mumble-remove)))
1693 (apply #'sb!sequence:remove-if-not predicate sequence args)))
1695 ;;;; REMOVE-DUPLICATES
1697 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1698 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1699 ;;; if we look into the already copied structure (from after :start) and see
1700 ;;; the item. If we check from beginning we check into the rest of the
1701 ;;; original list up to the :end marker (this we have to do by running a
1702 ;;; do loop down the list that far and using our test.
1703 (defun list-remove-duplicates* (list test test-not start end key from-end)
1704 (declare (fixnum start))
1705 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1706 (splice result)
1707 (current list)
1708 (end (or end (length list)))
1709 (hash (and (> (- end start) 20)
1710 test
1711 (not key)
1712 (not test-not)
1713 (or (eql test #'eql)
1714 (eql test #'eq)
1715 (eql test #'equal)
1716 (eql test #'equalp))
1717 (make-hash-table :test test :size (- end start)))))
1718 (do ((index 0 (1+ index)))
1719 ((= index start))
1720 (declare (fixnum index))
1721 (setq splice (cdr (rplacd splice (list (car current)))))
1722 (setq current (cdr current)))
1723 (if hash
1724 (do ((index start (1+ index)))
1725 ((or (and end (= index (the fixnum end)))
1726 (atom current)))
1727 (declare (fixnum index))
1728 ;; The hash table contains links from values that are
1729 ;; already in result to the cons cell *preceding* theirs
1730 ;; in the list. That is, for each value v in the list,
1731 ;; v and (cadr (gethash v hash)) are equal under TEST.
1732 (let ((prev (gethash (car current) hash)))
1733 (cond
1734 ((not prev)
1735 (setf (gethash (car current) hash) splice)
1736 (setq splice (cdr (rplacd splice (list (car current))))))
1737 ((not from-end)
1738 (let* ((old (cdr prev))
1739 (next (cdr old)))
1740 (if next
1741 (let ((next-val (car next)))
1742 ;; (assert (eq (gethash next-val hash) old))
1743 (setf (cdr prev) next
1744 (gethash next-val hash) prev
1745 (gethash (car current) hash) splice
1746 splice (cdr (rplacd splice (list (car current))))))
1747 (setf (car old) (car current)))))))
1748 (setq current (cdr current)))
1749 (do ((index start (1+ index)))
1750 ((or (and end (= index (the fixnum end)))
1751 (atom current)))
1752 (declare (fixnum index))
1753 (if (or (and from-end
1754 (not (if test-not
1755 (member (apply-key key (car current))
1756 (nthcdr (1+ start) result)
1757 :test-not test-not
1758 :key key)
1759 (member (apply-key key (car current))
1760 (nthcdr (1+ start) result)
1761 :test test
1762 :key key))))
1763 (and (not from-end)
1764 (not (do ((it (apply-key key (car current)))
1765 (l (cdr current) (cdr l))
1766 (i (1+ index) (1+ i)))
1767 ((or (atom l) (and end (= i (the fixnum end))))
1769 (declare (fixnum i))
1770 (if (if test-not
1771 (not (funcall test-not
1773 (apply-key key (car l))))
1774 (funcall test it (apply-key key (car l))))
1775 (return t))))))
1776 (setq splice (cdr (rplacd splice (list (car current))))))
1777 (setq current (cdr current))))
1778 (do ()
1779 ((atom current))
1780 (setq splice (cdr (rplacd splice (list (car current)))))
1781 (setq current (cdr current)))
1782 (cdr result)))
1784 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1785 &optional (length (length vector)))
1786 (declare (vector vector) (fixnum start length))
1787 (when (null end) (setf end (length vector)))
1788 (let ((result (%make-sequence-like vector length))
1789 (index 0)
1790 (jndex start))
1791 (declare (fixnum index jndex))
1792 (do ()
1793 ((= index start))
1794 (setf (aref result index) (aref vector index))
1795 (setq index (1+ index)))
1796 (do ((elt))
1797 ((= index end))
1798 (setq elt (aref vector index))
1799 (unless (or (and from-end
1800 (if test-not
1801 (position (apply-key key elt) result
1802 :start start :end jndex
1803 :test-not test-not :key key)
1804 (position (apply-key key elt) result
1805 :start start :end jndex
1806 :test test :key key)))
1807 (and (not from-end)
1808 (if test-not
1809 (position (apply-key key elt) vector
1810 :start (1+ index) :end end
1811 :test-not test-not :key key)
1812 (position (apply-key key elt) vector
1813 :start (1+ index) :end end
1814 :test test :key key))))
1815 (setf (aref result jndex) elt)
1816 (setq jndex (1+ jndex)))
1817 (setq index (1+ index)))
1818 (do ()
1819 ((= index length))
1820 (setf (aref result jndex) (aref vector index))
1821 (setq index (1+ index))
1822 (setq jndex (1+ jndex)))
1823 (%shrink-vector result jndex)))
1825 (define-sequence-traverser remove-duplicates
1826 (sequence &rest args &key test test-not start end from-end key)
1827 #!+sb-doc
1828 "The elements of SEQUENCE are compared pairwise, and if any two match,
1829 the one occurring earlier is discarded, unless FROM-END is true, in
1830 which case the one later in the sequence is discarded. The resulting
1831 sequence is returned.
1833 The :TEST-NOT argument is deprecated."
1834 (declare (fixnum start)
1835 (truly-dynamic-extent args))
1836 (seq-dispatch sequence
1837 (if sequence
1838 (list-remove-duplicates* sequence test test-not
1839 start end key from-end))
1840 (vector-remove-duplicates* sequence test test-not start end key from-end)
1841 (apply #'sb!sequence:remove-duplicates sequence args)))
1843 ;;;; DELETE-DUPLICATES
1845 (defun list-delete-duplicates* (list test test-not key from-end start end)
1846 (declare (fixnum start))
1847 (let ((handle (cons nil list)))
1848 (declare (truly-dynamic-extent handle))
1849 (do ((current (nthcdr start list) (cdr current))
1850 (previous (nthcdr start handle))
1851 (index start (1+ index)))
1852 ((or (and end (= index (the fixnum end))) (null current))
1853 (cdr handle))
1854 (declare (fixnum index))
1855 (if (do ((x (if from-end
1856 (nthcdr (1+ start) handle)
1857 (cdr current))
1858 (cdr x))
1859 (i (1+ index) (1+ i)))
1860 ((or (null x)
1861 (and (not from-end) end (= i (the fixnum end)))
1862 (eq x current))
1863 nil)
1864 (declare (fixnum i))
1865 (if (if test-not
1866 (not (funcall test-not
1867 (apply-key key (car current))
1868 (apply-key key (car x))))
1869 (funcall test
1870 (apply-key key (car current))
1871 (apply-key key (car x))))
1872 (return t)))
1873 (rplacd previous (cdr current))
1874 (setq previous (cdr previous))))))
1876 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1877 &optional (length (length vector)))
1878 (declare (vector vector) (fixnum start length))
1879 (when (null end) (setf end (length vector)))
1880 (do ((index start (1+ index))
1881 (jndex start))
1882 ((= index end)
1883 (do ((index index (1+ index)) ; copy the rest of the vector
1884 (jndex jndex (1+ jndex)))
1885 ((= index length)
1886 (shrink-vector vector jndex))
1887 (setf (aref vector jndex) (aref vector index))))
1888 (declare (fixnum index jndex))
1889 (setf (aref vector jndex) (aref vector index))
1890 (unless (if test-not
1891 (position (apply-key key (aref vector index)) vector :key key
1892 :start (if from-end start (1+ index))
1893 :end (if from-end jndex end)
1894 :test-not test-not)
1895 (position (apply-key key (aref vector index)) vector :key key
1896 :start (if from-end start (1+ index))
1897 :end (if from-end jndex end)
1898 :test test))
1899 (setq jndex (1+ jndex)))))
1901 (define-sequence-traverser delete-duplicates
1902 (sequence &rest args &key test test-not start end from-end key)
1903 #!+sb-doc
1904 "The elements of SEQUENCE are examined, and if any two match, one is
1905 discarded. The resulting sequence, which may be formed by destroying the
1906 given sequence, is returned.
1908 The :TEST-NOT argument is deprecated."
1909 (declare (truly-dynamic-extent args))
1910 (seq-dispatch sequence
1911 (when sequence
1912 (list-delete-duplicates* sequence test test-not
1913 key from-end start end))
1914 (vector-delete-duplicates* sequence test test-not key from-end start end)
1915 (apply #'sb!sequence:delete-duplicates sequence args)))
1917 ;;;; SUBSTITUTE
1919 (defun list-substitute* (pred new list start end count key test test-not old)
1920 (declare (fixnum start end count))
1921 (let* ((result (list nil))
1923 (splice result)
1924 (list list)) ; Get a local list for a stepper.
1925 (do ((index 0 (1+ index)))
1926 ((= index start))
1927 (declare (fixnum index))
1928 (setq splice (cdr (rplacd splice (list (car list)))))
1929 (setq list (cdr list)))
1930 (do ((index start (1+ index)))
1931 ((or (= index end) (null list) (= count 0)))
1932 (declare (fixnum index))
1933 (setq elt (car list))
1934 (setq splice
1935 (cdr (rplacd splice
1936 (list
1937 (cond
1938 ((case pred
1939 (normal
1940 (if test-not
1941 (not
1942 (funcall test-not old (apply-key key elt)))
1943 (funcall test old (apply-key key elt))))
1944 (if (funcall test (apply-key key elt)))
1945 (if-not (not (funcall test (apply-key key elt)))))
1946 (decf count)
1947 new)
1948 (t elt))))))
1949 (setq list (cdr list)))
1950 (do ()
1951 ((null list))
1952 (setq splice (cdr (rplacd splice (list (car list)))))
1953 (setq list (cdr list)))
1954 (cdr result)))
1956 ;;; Replace old with new in sequence moving from left to right by incrementer
1957 ;;; on each pass through the loop. Called by all three substitute functions.
1958 (defun vector-substitute* (pred new sequence incrementer left right length
1959 start end count key test test-not old)
1960 (declare (fixnum start count end incrementer right))
1961 (let ((result (%make-sequence-like sequence length))
1962 (index left))
1963 (declare (fixnum index))
1964 (do ()
1965 ((= index start))
1966 (setf (aref result index) (aref sequence index))
1967 (setq index (+ index incrementer)))
1968 (do ((elt))
1969 ((or (= index end) (= count 0)))
1970 (setq elt (aref sequence index))
1971 (setf (aref result index)
1972 (cond ((case pred
1973 (normal
1974 (if test-not
1975 (not (funcall test-not old (apply-key key elt)))
1976 (funcall test old (apply-key key elt))))
1977 (if (funcall test (apply-key key elt)))
1978 (if-not (not (funcall test (apply-key key elt)))))
1979 (setq count (1- count))
1980 new)
1981 (t elt)))
1982 (setq index (+ index incrementer)))
1983 (do ()
1984 ((= index right))
1985 (setf (aref result index) (aref sequence index))
1986 (setq index (+ index incrementer)))
1987 result))
1989 (eval-when (:compile-toplevel :execute)
1991 (sb!xc:defmacro subst-dispatch (pred)
1992 `(seq-dispatch sequence
1993 (let ((end (or end length)))
1994 (declare (type index end))
1995 (if from-end
1996 (nreverse (list-substitute* ,pred
1998 (reverse sequence)
1999 (- (the fixnum length)
2000 (the fixnum end))
2001 (- (the fixnum length)
2002 (the fixnum start))
2003 count key test test-not old))
2004 (list-substitute* ,pred
2005 new sequence start end count key test test-not
2006 old)))
2008 (let ((end (or end length)))
2009 (declare (type index end))
2010 (if from-end
2011 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2012 -1 length (1- (the fixnum end))
2013 (1- (the fixnum start))
2014 count key test test-not old)
2015 (vector-substitute* ,pred new sequence 1 0 length length
2016 start end count key test test-not old)))
2018 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2019 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2020 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2021 ;; dispatch once per element on PRED's run-time identity.
2022 ,(ecase (cadr pred)
2023 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2024 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2025 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2026 ) ; EVAL-WHEN
2028 (define-sequence-traverser substitute
2029 (new old sequence &rest args &key from-end test test-not
2030 start count end key)
2031 #!+sb-doc
2032 "Return a sequence of the same kind as SEQUENCE with the same elements,
2033 except that all elements equal to OLD are replaced with NEW."
2034 (declare (type fixnum start)
2035 (truly-dynamic-extent args))
2036 (subst-dispatch 'normal))
2038 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2040 (define-sequence-traverser substitute-if
2041 (new predicate sequence &rest args &key from-end start end count key)
2042 #!+sb-doc
2043 "Return a sequence of the same kind as SEQUENCE with the same elements
2044 except that all elements satisfying the PRED are replaced with NEW."
2045 (declare (type fixnum start)
2046 (truly-dynamic-extent args))
2047 (let ((test predicate)
2048 (test-not nil)
2049 old)
2050 (subst-dispatch 'if)))
2052 (define-sequence-traverser substitute-if-not
2053 (new predicate sequence &rest args &key from-end start end count key)
2054 #!+sb-doc
2055 "Return a sequence of the same kind as SEQUENCE with the same elements
2056 except that all elements not satisfying the PRED are replaced with NEW."
2057 (declare (type fixnum start)
2058 (truly-dynamic-extent args))
2059 (let ((test predicate)
2060 (test-not nil)
2061 old)
2062 (subst-dispatch 'if-not)))
2064 ;;;; NSUBSTITUTE
2066 (define-sequence-traverser nsubstitute
2067 (new old sequence &rest args &key from-end test test-not
2068 end count key start)
2069 #!+sb-doc
2070 "Return a sequence of the same kind as SEQUENCE with the same elements
2071 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2072 may be destructively modified."
2073 (declare (type fixnum start)
2074 (truly-dynamic-extent args))
2075 (seq-dispatch sequence
2076 (let ((end (or end length)))
2077 (declare (type index end))
2078 (if from-end
2079 (nreverse (nlist-substitute*
2080 new old (nreverse (the list sequence))
2081 test test-not (- length end) (- length start)
2082 count key))
2083 (nlist-substitute* new old sequence
2084 test test-not start end count key)))
2085 (let ((end (or end length)))
2086 (declare (type index end))
2087 (if from-end
2088 (nvector-substitute* new old sequence -1
2089 test test-not (1- end) (1- start) count key)
2090 (nvector-substitute* new old sequence 1
2091 test test-not start end count key)))
2092 (apply #'sb!sequence:nsubstitute new old sequence args)))
2094 (defun nlist-substitute* (new old sequence test test-not start end count key)
2095 (declare (fixnum start count end))
2096 (do ((list (nthcdr start sequence) (cdr list))
2097 (index start (1+ index)))
2098 ((or (= index end) (null list) (= count 0)) sequence)
2099 (declare (fixnum index))
2100 (when (if test-not
2101 (not (funcall test-not old (apply-key key (car list))))
2102 (funcall test old (apply-key key (car list))))
2103 (rplaca list new)
2104 (setq count (1- count)))))
2106 (defun nvector-substitute* (new old sequence incrementer
2107 test test-not start end count key)
2108 (declare (fixnum start incrementer count end))
2109 (do ((index start (+ index incrementer)))
2110 ((or (= index end) (= count 0)) sequence)
2111 (declare (fixnum index))
2112 (when (if test-not
2113 (not (funcall test-not
2115 (apply-key key (aref sequence index))))
2116 (funcall test old (apply-key key (aref sequence index))))
2117 (setf (aref sequence index) new)
2118 (setq count (1- count)))))
2120 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2122 (define-sequence-traverser nsubstitute-if
2123 (new predicate sequence &rest args &key from-end start end count key)
2124 #!+sb-doc
2125 "Return a sequence of the same kind as SEQUENCE with the same elements
2126 except that all elements satisfying PREDICATE are replaced with NEW.
2127 SEQUENCE may be destructively modified."
2128 (declare (type fixnum start)
2129 (truly-dynamic-extent args))
2130 (seq-dispatch sequence
2131 (let ((end (or end length)))
2132 (declare (type index end))
2133 (if from-end
2134 (nreverse (nlist-substitute-if*
2135 new predicate (nreverse (the list sequence))
2136 (- length end) (- length start) count key))
2137 (nlist-substitute-if* new predicate sequence
2138 start end count key)))
2139 (let ((end (or end length)))
2140 (declare (type index end))
2141 (if from-end
2142 (nvector-substitute-if* new predicate sequence -1
2143 (1- end) (1- start) count key)
2144 (nvector-substitute-if* new predicate sequence 1
2145 start end count key)))
2146 (apply #'sb!sequence:nsubstitute-if new predicate sequence args)))
2148 (defun nlist-substitute-if* (new test sequence start end count key)
2149 (declare (type fixnum end)
2150 (type function test)) ; coercion is done by caller
2151 (do ((list (nthcdr start sequence) (cdr list))
2152 (index start (1+ index)))
2153 ((or (= index end) (null list) (= count 0)) sequence)
2154 (when (funcall test (apply-key key (car list)))
2155 (rplaca list new)
2156 (setq count (1- count)))))
2158 (defun nvector-substitute-if* (new test sequence incrementer
2159 start end count key)
2160 (declare (type fixnum end)
2161 (type function test)) ; coercion is done by caller
2162 (do ((index start (+ index incrementer)))
2163 ((or (= index end) (= count 0)) sequence)
2164 (when (funcall test (apply-key key (aref sequence index)))
2165 (setf (aref sequence index) new)
2166 (setq count (1- count)))))
2168 (define-sequence-traverser nsubstitute-if-not
2169 (new predicate sequence &rest args &key from-end start end count key)
2170 #!+sb-doc
2171 "Return a sequence of the same kind as SEQUENCE with the same elements
2172 except that all elements not satisfying PREDICATE are replaced with NEW.
2173 SEQUENCE may be destructively modified."
2174 (declare (type fixnum start)
2175 (truly-dynamic-extent args))
2176 (seq-dispatch sequence
2177 (let ((end (or end length)))
2178 (declare (fixnum end))
2179 (if from-end
2180 (nreverse (nlist-substitute-if-not*
2181 new predicate (nreverse (the list sequence))
2182 (- length end) (- length start) count key))
2183 (nlist-substitute-if-not* new predicate sequence
2184 start end count key)))
2185 (let ((end (or end length)))
2186 (declare (fixnum end))
2187 (if from-end
2188 (nvector-substitute-if-not* new predicate sequence -1
2189 (1- end) (1- start) count key)
2190 (nvector-substitute-if-not* new predicate sequence 1
2191 start end count key)))
2192 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args)))
2194 (defun nlist-substitute-if-not* (new test sequence start end count key)
2195 (declare (type fixnum end)
2196 (type function test)) ; coercion is done by caller
2197 (do ((list (nthcdr start sequence) (cdr list))
2198 (index start (1+ index)))
2199 ((or (= index end) (null list) (= count 0)) sequence)
2200 (when (not (funcall test (apply-key key (car list))))
2201 (rplaca list new)
2202 (decf count))))
2204 (defun nvector-substitute-if-not* (new test sequence incrementer
2205 start end count key)
2206 (declare (type fixnum end)
2207 (type function test)) ; coercion is done by caller
2208 (do ((index start (+ index incrementer)))
2209 ((or (= index end) (= count 0)) sequence)
2210 (when (not (funcall test (apply-key key (aref sequence index))))
2211 (setf (aref sequence index) new)
2212 (decf count))))
2214 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2216 (defun effective-find-position-test (test test-not)
2217 (effective-find-position-test test test-not))
2218 (defun effective-find-position-key (key)
2219 (effective-find-position-key key))
2221 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2222 (macrolet (;; shared logic for defining %FIND-POSITION and
2223 ;; %FIND-POSITION-IF in terms of various inlineable cases
2224 ;; of the expression defined in FROB and VECTOR*-FROB
2225 (frobs (&optional bit-frob)
2226 `(seq-dispatch sequence-arg
2227 (frob sequence-arg from-end)
2228 (with-array-data ((sequence sequence-arg :offset-var offset)
2229 (start start)
2230 (end end)
2231 :check-fill-pointer t)
2232 (multiple-value-bind (f p)
2233 (macrolet ((frob2 () `(if from-end
2234 (frob sequence t)
2235 (frob sequence nil))))
2236 (typecase sequence
2237 #!+sb-unicode
2238 ((simple-array character (*)) (frob2))
2239 ((simple-array base-char (*)) (frob2))
2240 ,@(when bit-frob
2241 `((simple-bit-vector
2242 (if (and (typep item 'bit)
2243 (eq #'identity key)
2244 (or (eq #'eq test)
2245 (eq #'eql test)
2246 (eq #'equal test)))
2247 (let ((p (%bit-position item sequence
2248 from-end start end)))
2249 (if p
2250 (values item p)
2251 (values nil nil)))
2252 (vector*-frob sequence)))))
2254 (vector*-frob sequence))))
2255 (declare (type (or index null) p))
2256 (values f (and p (the index (- p offset)))))))))
2257 (defun %find-position (item sequence-arg from-end start end key test)
2258 (macrolet ((frob (sequence from-end)
2259 `(%find-position item ,sequence
2260 ,from-end start end key test))
2261 (vector*-frob (sequence)
2262 `(%find-position-vector-macro item ,sequence
2263 from-end start end key test)))
2264 (frobs t)))
2265 (defun %find-position-if (predicate sequence-arg from-end start end key)
2266 (macrolet ((frob (sequence from-end)
2267 `(%find-position-if predicate ,sequence
2268 ,from-end start end key))
2269 (vector*-frob (sequence)
2270 `(%find-position-if-vector-macro predicate ,sequence
2271 from-end start end key)))
2272 (frobs)))
2273 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2274 (macrolet ((frob (sequence from-end)
2275 `(%find-position-if-not predicate ,sequence
2276 ,from-end start end key))
2277 (vector*-frob (sequence)
2278 `(%find-position-if-not-vector-macro predicate ,sequence
2279 from-end start end key)))
2280 (frobs))))
2282 (defun find
2283 (item sequence &rest args &key from-end (start 0) end key test test-not)
2284 (declare (truly-dynamic-extent args))
2285 (seq-dispatch sequence
2286 (nth-value 0 (%find-position
2287 item sequence from-end start end
2288 (effective-find-position-key key)
2289 (effective-find-position-test test test-not)))
2290 (nth-value 0 (%find-position
2291 item sequence from-end start end
2292 (effective-find-position-key key)
2293 (effective-find-position-test test test-not)))
2294 (apply #'sb!sequence:find item sequence args)))
2295 (defun position
2296 (item sequence &rest args &key from-end (start 0) end key test test-not)
2297 (declare (truly-dynamic-extent args))
2298 (seq-dispatch sequence
2299 (nth-value 1 (%find-position
2300 item sequence from-end start end
2301 (effective-find-position-key key)
2302 (effective-find-position-test test test-not)))
2303 (nth-value 1 (%find-position
2304 item sequence from-end start end
2305 (effective-find-position-key key)
2306 (effective-find-position-test test test-not)))
2307 (apply #'sb!sequence:position item sequence args)))
2309 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2310 (declare (truly-dynamic-extent args))
2311 (seq-dispatch sequence
2312 (nth-value 0 (%find-position-if
2313 (%coerce-callable-to-fun predicate)
2314 sequence from-end start end
2315 (effective-find-position-key key)))
2316 (nth-value 0 (%find-position-if
2317 (%coerce-callable-to-fun predicate)
2318 sequence from-end start end
2319 (effective-find-position-key key)))
2320 (apply #'sb!sequence:find-if predicate sequence args)))
2321 (defun position-if
2322 (predicate sequence &rest args &key from-end (start 0) end key)
2323 (declare (truly-dynamic-extent args))
2324 (seq-dispatch sequence
2325 (nth-value 1 (%find-position-if
2326 (%coerce-callable-to-fun predicate)
2327 sequence from-end start end
2328 (effective-find-position-key key)))
2329 (nth-value 1 (%find-position-if
2330 (%coerce-callable-to-fun predicate)
2331 sequence from-end start end
2332 (effective-find-position-key key)))
2333 (apply #'sb!sequence:position-if predicate sequence args)))
2335 (defun find-if-not
2336 (predicate sequence &rest args &key from-end (start 0) end key)
2337 (declare (truly-dynamic-extent args))
2338 (seq-dispatch sequence
2339 (nth-value 0 (%find-position-if-not
2340 (%coerce-callable-to-fun predicate)
2341 sequence from-end start end
2342 (effective-find-position-key key)))
2343 (nth-value 0 (%find-position-if-not
2344 (%coerce-callable-to-fun predicate)
2345 sequence from-end start end
2346 (effective-find-position-key key)))
2347 (apply #'sb!sequence:find-if-not predicate sequence args)))
2348 (defun position-if-not
2349 (predicate sequence &rest args &key from-end (start 0) end key)
2350 (declare (truly-dynamic-extent args))
2351 (seq-dispatch sequence
2352 (nth-value 1 (%find-position-if-not
2353 (%coerce-callable-to-fun predicate)
2354 sequence from-end start end
2355 (effective-find-position-key key)))
2356 (nth-value 1 (%find-position-if-not
2357 (%coerce-callable-to-fun predicate)
2358 sequence from-end start end
2359 (effective-find-position-key key)))
2360 (apply #'sb!sequence:position-if-not predicate sequence args)))
2362 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2364 (eval-when (:compile-toplevel :execute)
2366 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2367 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2368 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2369 `(let ((%start ,(if from-end-p '(1- end) 'start))
2370 (%end ,(if from-end-p '(1- start) 'end)))
2371 (do ((index %start ,next-index)
2372 (count 0))
2373 ((= index (the fixnum %end)) count)
2374 (declare (fixnum index count))
2375 (,(if notp 'unless 'when) ,pred
2376 (setq count (1+ count)))))))
2378 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2379 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2380 `(let ((%start ,(if from-end-p '(- length end) 'start))
2381 (%end ,(if from-end-p '(- length start) 'end))
2382 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2383 (do ((sequence (nthcdr %start ,sequence))
2384 (index %start (1+ index))
2385 (count 0))
2386 ((or (= index (the fixnum %end)) (null sequence)) count)
2387 (declare (fixnum index count))
2388 (,(if notp 'unless 'when) ,pred
2389 (setq count (1+ count)))))))
2392 ) ; EVAL-WHEN
2394 (define-sequence-traverser count-if
2395 (pred sequence &rest args &key from-end start end key)
2396 #!+sb-doc
2397 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2398 (declare (type fixnum start)
2399 (truly-dynamic-extent args))
2400 (let ((pred (%coerce-callable-to-fun pred)))
2401 (seq-dispatch sequence
2402 (let ((end (or end length)))
2403 (declare (type index end))
2404 (if from-end
2405 (list-count-if nil t pred sequence)
2406 (list-count-if nil nil pred sequence)))
2407 (let ((end (or end length)))
2408 (declare (type index end))
2409 (if from-end
2410 (vector-count-if nil t pred sequence)
2411 (vector-count-if nil nil pred sequence)))
2412 (apply #'sb!sequence:count-if pred sequence args))))
2414 (define-sequence-traverser count-if-not
2415 (pred sequence &rest args &key from-end start end key)
2416 #!+sb-doc
2417 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2418 (declare (type fixnum start)
2419 (truly-dynamic-extent args))
2420 (let ((pred (%coerce-callable-to-fun pred)))
2421 (seq-dispatch sequence
2422 (let ((end (or end length)))
2423 (declare (type index end))
2424 (if from-end
2425 (list-count-if t t pred sequence)
2426 (list-count-if t nil pred sequence)))
2427 (let ((end (or end length)))
2428 (declare (type index end))
2429 (if from-end
2430 (vector-count-if t t pred sequence)
2431 (vector-count-if t nil pred sequence)))
2432 (apply #'sb!sequence:count-if-not pred sequence args))))
2434 (define-sequence-traverser count
2435 (item sequence &rest args &key from-end start end
2436 key (test #'eql test-p) (test-not nil test-not-p))
2437 #!+sb-doc
2438 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2439 which defaults to EQL."
2440 (declare (type fixnum start)
2441 (truly-dynamic-extent args))
2442 (when (and test-p test-not-p)
2443 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2444 ;; (CLHS 17.2.1)
2445 (error ":TEST and :TEST-NOT are both present."))
2446 (let ((%test (if test-not-p
2447 (lambda (x)
2448 (not (funcall test-not item x)))
2449 (lambda (x)
2450 (funcall test item x)))))
2451 (seq-dispatch sequence
2452 (let ((end (or end length)))
2453 (declare (type index end))
2454 (if from-end
2455 (list-count-if nil t %test sequence)
2456 (list-count-if nil nil %test sequence)))
2457 (let ((end (or end length)))
2458 (declare (type index end))
2459 (if from-end
2460 (vector-count-if nil t %test sequence)
2461 (vector-count-if nil nil %test sequence)))
2462 (apply #'sb!sequence:count item sequence args))))
2464 ;;;; MISMATCH
2466 (eval-when (:compile-toplevel :execute)
2468 (sb!xc:defmacro match-vars (&rest body)
2469 `(let ((inc (if from-end -1 1))
2470 (start1 (if from-end (1- (the fixnum end1)) start1))
2471 (start2 (if from-end (1- (the fixnum end2)) start2))
2472 (end1 (if from-end (1- (the fixnum start1)) end1))
2473 (end2 (if from-end (1- (the fixnum start2)) end2)))
2474 (declare (fixnum inc start1 start2 end1 end2))
2475 ,@body))
2477 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2478 (declare (ignore end)) ;; ### Should END be used below?
2479 `(let ((,sequence (if from-end
2480 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2481 (reverse (the list ,sequence)))
2482 (nthcdr ,start ,sequence))))
2483 (declare (type list ,sequence))
2484 ,@body))
2486 ) ; EVAL-WHEN
2488 (eval-when (:compile-toplevel :execute)
2490 (sb!xc:defmacro if-mismatch (elt1 elt2)
2491 `(cond ((= (the fixnum index1) (the fixnum end1))
2492 (return (if (= (the fixnum index2) (the fixnum end2))
2494 (if from-end
2495 (1+ (the fixnum index1))
2496 (the fixnum index1)))))
2497 ((= (the fixnum index2) (the fixnum end2))
2498 (return (if from-end (1+ (the fixnum index1)) index1)))
2499 (test-not
2500 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2501 (return (if from-end (1+ (the fixnum index1)) index1))))
2502 (t (if (not (funcall test (apply-key key ,elt1)
2503 (apply-key key ,elt2)))
2504 (return (if from-end (1+ (the fixnum index1)) index1))))))
2506 (sb!xc:defmacro mumble-mumble-mismatch ()
2507 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2508 (index2 start2 (+ index2 (the fixnum inc))))
2509 (())
2510 (declare (fixnum index1 index2))
2511 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2513 (sb!xc:defmacro mumble-list-mismatch ()
2514 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2515 (index2 start2 (+ index2 (the fixnum inc))))
2516 (())
2517 (declare (fixnum index1 index2))
2518 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2520 (sb!xc:defmacro list-mumble-mismatch ()
2521 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2522 (index2 start2 (+ index2 (the fixnum inc))))
2523 (())
2524 (declare (fixnum index1 index2))
2525 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2527 (sb!xc:defmacro list-list-mismatch ()
2528 `(do ((sequence1 sequence1)
2529 (sequence2 sequence2)
2530 (index1 start1 (+ index1 (the fixnum inc)))
2531 (index2 start2 (+ index2 (the fixnum inc))))
2532 (())
2533 (declare (fixnum index1 index2))
2534 (if-mismatch (pop sequence1) (pop sequence2))))
2536 ) ; EVAL-WHEN
2538 (define-sequence-traverser mismatch
2539 (sequence1 sequence2 &rest args &key from-end test test-not
2540 start1 end1 start2 end2 key)
2541 #!+sb-doc
2542 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2543 element-wise. If they are of equal length and match in every element, the
2544 result is NIL. Otherwise, the result is a non-negative integer, the index
2545 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2546 if one is shorter than and a matching prefix of the other, the index within
2547 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2548 :FROM-END argument is given, then one plus the index of the rightmost
2549 position in which the sequences differ is returned."
2550 (declare (type fixnum start1 start2))
2551 (declare (truly-dynamic-extent args))
2552 (seq-dispatch sequence1
2553 (seq-dispatch sequence2
2554 (let ((end1 (or end1 length1))
2555 (end2 (or end2 length2)))
2556 (declare (type index end1 end2))
2557 (match-vars
2558 (matchify-list (sequence1 start1 length1 end1)
2559 (matchify-list (sequence2 start2 length2 end2)
2560 (list-list-mismatch)))))
2561 (let ((end1 (or end1 length1))
2562 (end2 (or end2 length2)))
2563 (declare (type index end1 end2))
2564 (match-vars
2565 (matchify-list (sequence1 start1 length1 end1)
2566 (list-mumble-mismatch))))
2567 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2568 (seq-dispatch sequence2
2569 (let ((end1 (or end1 length1))
2570 (end2 (or end2 length2)))
2571 (declare (type index end1 end2))
2572 (match-vars
2573 (matchify-list (sequence2 start2 length2 end2)
2574 (mumble-list-mismatch))))
2575 (let ((end1 (or end1 length1))
2576 (end2 (or end2 length2)))
2577 (declare (type index end1 end2))
2578 (match-vars
2579 (mumble-mumble-mismatch)))
2580 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2581 (apply #'sb!sequence:mismatch sequence1 sequence2 args)))
2584 ;;; search comparison functions
2586 (eval-when (:compile-toplevel :execute)
2588 ;;; Compare two elements and return if they don't match.
2589 (sb!xc:defmacro compare-elements (elt1 elt2)
2590 `(if test-not
2591 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2592 (return nil)
2594 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2595 (return nil)
2596 t)))
2598 (sb!xc:defmacro search-compare-list-list (main sub)
2599 `(do ((main ,main (cdr main))
2600 (jndex start1 (1+ jndex))
2601 (sub (nthcdr start1 ,sub) (cdr sub)))
2602 ((or (endp main) (endp sub) (<= end1 jndex))
2604 (declare (type (integer 0) jndex))
2605 (compare-elements (car sub) (car main))))
2607 (sb!xc:defmacro search-compare-list-vector (main sub)
2608 `(do ((main ,main (cdr main))
2609 (index start1 (1+ index)))
2610 ((or (endp main) (= index end1)) t)
2611 (compare-elements (aref ,sub index) (car main))))
2613 (sb!xc:defmacro search-compare-vector-list (main sub index)
2614 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2615 (jndex start1 (1+ jndex))
2616 (index ,index (1+ index)))
2617 ((or (<= end1 jndex) (endp sub)) t)
2618 (declare (type (integer 0) jndex))
2619 (compare-elements (car sub) (aref ,main index))))
2621 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2622 `(do ((index ,index (1+ index))
2623 (sub-index start1 (1+ sub-index)))
2624 ((= sub-index end1) t)
2625 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2627 (sb!xc:defmacro search-compare (main-type main sub index)
2628 (if (eq main-type 'list)
2629 `(seq-dispatch ,sub
2630 (search-compare-list-list ,main ,sub)
2631 (search-compare-list-vector ,main ,sub)
2632 ;; KLUDGE: just hack it together so that it works
2633 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2634 `(seq-dispatch ,sub
2635 (search-compare-vector-list ,main ,sub ,index)
2636 (search-compare-vector-vector ,main ,sub ,index)
2637 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2639 ) ; EVAL-WHEN
2641 ;;;; SEARCH
2643 (eval-when (:compile-toplevel :execute)
2645 (sb!xc:defmacro list-search (main sub)
2646 `(do ((main (nthcdr start2 ,main) (cdr main))
2647 (index2 start2 (1+ index2))
2648 (terminus (- end2 (the (integer 0) (- end1 start1))))
2649 (last-match ()))
2650 ((> index2 terminus) last-match)
2651 (declare (type (integer 0) index2))
2652 (if (search-compare list main ,sub index2)
2653 (if from-end
2654 (setq last-match index2)
2655 (return index2)))))
2657 (sb!xc:defmacro vector-search (main sub)
2658 `(do ((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 vector ,main ,sub index2)
2664 (if from-end
2665 (setq last-match index2)
2666 (return index2)))))
2668 ) ; EVAL-WHEN
2670 (define-sequence-traverser search
2671 (sequence1 sequence2 &rest args &key
2672 from-end test test-not start1 end1 start2 end2 key)
2673 (declare (type fixnum start1 start2)
2674 (truly-dynamic-extent args))
2675 (seq-dispatch sequence2
2676 (let ((end1 (or end1 length1))
2677 (end2 (or end2 length2)))
2678 (declare (type index end1 end2))
2679 (list-search sequence2 sequence1))
2680 (let ((end1 (or end1 length1))
2681 (end2 (or end2 length2)))
2682 (declare (type index end1 end2))
2683 (vector-search sequence2 sequence1))
2684 (apply #'sb!sequence:search sequence1 sequence2 args)))
2686 ;;; FIXME: this was originally in array.lisp; it might be better to
2687 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2688 ;;; a new early-seq.lisp file.
2689 (defun fill-data-vector (vector dimensions initial-contents)
2690 (let ((index 0))
2691 (labels ((frob (axis dims contents)
2692 (cond ((null dims)
2693 (setf (aref vector index) contents)
2694 (incf index))
2696 (unless (typep contents 'sequence)
2697 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2698 sequence, but ~W more layer~:P needed."
2699 contents
2700 (- (length dimensions) axis)))
2701 (unless (= (length contents) (car dims))
2702 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2703 axis ~W is ~W, but ~S is ~W long."
2704 axis (car dims) contents (length contents)))
2705 (sb!sequence:dosequence (content contents)
2706 (frob (1+ axis) (cdr dims) content))))))
2707 (frob 0 dimensions initial-contents))))