Add a test that checks CL symbols for being bound/fbound, etc.
[sbcl.git] / src / code / seq.lisp
blobcd552470f8c6b457fb07af33fdf551bfda8e34b5
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) (parse-body body t)
88 (collect ((new-args)
89 (new-declarations)
90 ;; Things which are definitely used in any code path.
91 (rebindings/eager)
92 ;; Things which may be used/are only used in certain
93 ;; code paths (e.g. length).
94 (rebindings/lazy))
95 (dolist (arg args)
96 (case arg
97 ;; FIXME: make this robust. And clean.
98 ((sequence sequence1 sequence2)
99 (let* ((length-var (ecase arg
100 (sequence 'length)
101 (sequence1 'length1)
102 (sequence2 'length2)))
103 (cache-var (symbolicate length-var '#:-cache)))
104 (new-args arg)
105 (rebindings/eager `(,cache-var nil))
106 (rebindings/lazy
107 `(,length-var (truly-the
108 index
109 (or ,cache-var (setf ,cache-var (length ,arg))))))))
110 ((function predicate)
111 (new-args arg)
112 (rebindings/eager `(,arg (%coerce-callable-to-fun ,arg))))
114 (let ((info (cdr (assoc arg *sequence-keyword-info*))))
115 (cond (info
116 (destructuring-bind (default supplied-p adjuster type) info
117 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
118 (rebindings/eager `(,arg ,adjuster))
119 (new-declarations `(type ,type ,arg))))
120 (t (new-args arg)))))))
121 `(defun ,name ,(new-args)
122 ,@(when docstring (list docstring))
123 ,@declarations
124 (symbol-macrolet (,@(rebindings/lazy))
125 (let* (,@(rebindings/eager))
126 (declare ,@(new-declarations))
127 ,@body
128 ))))))
130 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
132 ;;; FIXME: It might be worth making three cases here, LIST,
133 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
134 ;;; It tends to make code run faster but be bigger; some benchmarking
135 ;;; is needed to decide.
136 (sb!xc:defmacro seq-dispatch
137 (sequence list-form array-form &optional other-form)
138 `(if (listp ,sequence)
139 (let ((,sequence (truly-the list ,sequence)))
140 (declare (ignorable ,sequence))
141 ,list-form)
142 ,@(if other-form
143 `((if (arrayp ,sequence)
144 (let ((,sequence (truly-the vector ,sequence)))
145 (declare (ignorable ,sequence))
146 ,array-form)
147 ,other-form))
148 `((let ((,sequence (truly-the vector ,sequence)))
149 (declare (ignorable ,sequence))
150 ,array-form)))))
152 ;; Same as above, but don't assume that ARRAYP implies VECTORP,
153 ;; and call SEQUENCEP only after checking for not LISTP and not VECTORP.
154 ;; This is for dispatching within sequence functions that have the
155 ;; EXPLICIT-CHECK attribute. [Because there is no way to have the compiler
156 ;; insert checks for a subset of arguments, it is inconvenient to declare
157 ;; things like DELETE-IF with explicit-check to suppress checking of the
158 ;; 'sequence' because then you have to manually check all other arguments.]
159 (sb!xc:defmacro seq-dispatch-checking
160 (sequence list-form array-form &optional other-form)
161 `(if (listp ,sequence)
162 (let ((,sequence (truly-the list ,sequence)))
163 (declare (ignorable ,sequence))
164 ,list-form)
165 ,@(if other-form
166 `((if (vectorp ,sequence)
167 (let ((,sequence (truly-the vector ,sequence)))
168 (declare (ignorable ,sequence))
169 ,array-form)
170 ;; This could assert (THE EXTENDED-SEQUENCE ,sequence)
171 ;; for a slight win, however the error string would be wrong.
172 ;; It needs to be "<x> is not a SEQUENCE", not "<x> is not
173 ;; an EXTENDED-SEQUENCE".
174 (let ((,sequence (the sequence ,sequence)))
175 ,other-form)))
176 `((let ((,sequence (the vector ,sequence)))
177 (declare (ignorable ,sequence))
178 ,array-form)))))
180 (sb!xc:defmacro %make-sequence-like (sequence length)
181 #!+sb-doc
182 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
183 `(seq-dispatch ,sequence
184 (make-list ,length)
185 (make-array ,length :element-type (array-element-type ,sequence))
186 (sb!sequence:make-sequence-like ,sequence ,length)))
188 (sb!xc:defmacro bad-sequence-type-error (type-spec)
189 `(error 'simple-type-error
190 :datum ,type-spec
191 :expected-type '(satisfies is-a-valid-sequence-type-specifier-p)
192 :format-control "~S is a bad type specifier for sequences."
193 :format-arguments (list ,type-spec)))
195 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
196 `(error 'simple-type-error
197 :datum ,length
198 :expected-type (cond ((array-type-p ,type)
199 `(eql ,(car (array-type-dimensions ,type))))
200 ((type= ,type (specifier-type 'null))
201 '(eql 0))
202 ((cons-type-p ,type)
203 '(integer 1))
204 (t (bug "weird type in S-T-L-M-ERROR")))
205 ;; FIXME: this format control causes ugly printing. There's
206 ;; probably some ~<~@:_~> incantation that would make it
207 ;; nicer. -- CSR, 2002-10-18
208 :format-control "The length requested (~S) does not match the type restriction in ~S."
209 :format-arguments (list ,length (type-specifier ,type))))
211 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
212 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
213 ;; words that give some but not total support to this position in
214 ;; ANSI. Essentially, we are justified in throwing this on
215 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
216 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
218 ;; On the other hand, I'm not sure it deserves to be a type-error,
219 ;; either. -- bem, 2005-08-10
220 `(error 'simple-program-error
221 :format-control "~S is too hairy for sequence functions."
222 :format-arguments (list ,type-spec)))
223 ) ; EVAL-WHEN
225 (defun is-a-valid-sequence-type-specifier-p (type)
226 (let ((type (specifier-type type)))
227 (or (csubtypep type (specifier-type 'list))
228 (csubtypep type (specifier-type 'vector)))))
230 ;;; It's possible with some sequence operations to declare the length
231 ;;; of a result vector, and to be safe, we really ought to verify that
232 ;;; the actual result has the declared length.
233 (defun vector-of-checked-length-given-length (vector declared-length)
234 (declare (type vector vector))
235 (declare (type index declared-length))
236 (let ((actual-length (length vector)))
237 (unless (= actual-length declared-length)
238 (error 'simple-type-error
239 :datum vector
240 :expected-type `(vector ,declared-length)
241 :format-control
242 "Vector length (~W) doesn't match declared length (~W)."
243 :format-arguments (list actual-length declared-length))))
244 vector)
246 (defun sequence-of-checked-length-given-type (sequence result-type)
247 (let ((ctype (specifier-type result-type)))
248 (if (not (array-type-p ctype))
249 sequence
250 (let ((declared-length (first (array-type-dimensions ctype))))
251 (if (eq declared-length '*)
252 sequence
253 (vector-of-checked-length-given-length sequence
254 declared-length))))))
256 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
257 (defun signal-index-too-large-error (sequence index)
258 (let* ((length (length sequence))
259 (max-index (and (plusp length)
260 (1- length))))
261 (error 'index-too-large-error
262 :datum index
263 :expected-type (if max-index
264 `(integer 0 ,max-index)
265 ;; This seems silly, is there something better?
266 '(integer 0 (0))))))
268 (declaim (ftype (function (t t t) nil) sequence-bounding-indices-bad-error))
269 (defun sequence-bounding-indices-bad-error (sequence start end)
270 (let ((size (length sequence)))
271 (error 'bounding-indices-bad-error
272 :datum (cons start end)
273 :expected-type `(cons (integer 0 ,size)
274 (integer ,start ,size))
275 :object sequence)))
277 (declaim (ftype (function (t t t) nil) array-bounding-indices-bad-error))
278 (defun array-bounding-indices-bad-error (array start end)
279 (let ((size (array-total-size array)))
280 (error 'bounding-indices-bad-error
281 :datum (cons start end)
282 :expected-type `(cons (integer 0 ,size)
283 (integer ,start ,size))
284 :object array)))
286 (declaim (ftype (function (t) nil) circular-list-error))
287 (defun circular-list-error (list)
288 (let ((*print-circle* t))
289 (error 'simple-type-error
290 :format-control "List is circular:~% ~S"
291 :format-arguments (list list)
292 :datum list
293 :type '(and list (satisfies list-length)))))
297 (defun emptyp (sequence)
298 #!+sb-doc
299 "Returns T if SEQUENCE is an empty sequence and NIL
300 otherwise. Signals an error if SEQUENCE is not a sequence."
301 (seq-dispatch sequence
302 (null sequence)
303 (zerop (length sequence))
304 (sb!sequence:emptyp sequence)))
306 (defun elt (sequence index)
307 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
308 (seq-dispatch sequence
309 (do ((count index (1- count))
310 (list sequence (cdr list)))
311 ((= count 0)
312 (if (endp list)
313 (signal-index-too-large-error sequence index)
314 (car list)))
315 (declare (type (integer 0) count)))
316 (progn
317 (when (>= index (length sequence))
318 (signal-index-too-large-error sequence index))
319 (aref sequence index))
320 (sb!sequence:elt sequence index)))
322 (defun %setelt (sequence index newval)
323 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
324 (seq-dispatch sequence
325 (do ((count index (1- count))
326 (seq sequence))
327 ((= count 0) (rplaca seq newval) newval)
328 (declare (fixnum count))
329 (if (atom (cdr seq))
330 (signal-index-too-large-error sequence index)
331 (setq seq (cdr seq))))
332 (progn
333 (when (>= index (length sequence))
334 (signal-index-too-large-error sequence index))
335 (setf (aref sequence index) newval))
336 (setf (sb!sequence:elt sequence index) newval)))
338 (defun length (sequence)
339 #!+sb-doc "Return an integer that is the length of SEQUENCE."
340 (declare (explicit-check))
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) (explicit-check))
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 (list-type (specifier-type 'list)))
369 (cond ((csubtypep type list-type)
370 (cond
371 ((type= type list-type)
372 (make-list length :initial-element initial-element))
373 ((eq type *empty-type*)
374 (bad-sequence-type-error nil))
375 ((type= type (specifier-type 'null))
376 (if (= length 0)
377 'nil
378 (sequence-type-length-mismatch-error type length)))
379 ((cons-type-p type)
380 (multiple-value-bind (min exactp)
381 (sb!kernel::cons-type-length-info type)
382 (if exactp
383 (unless (= length min)
384 (sequence-type-length-mismatch-error type length))
385 (unless (>= length min)
386 (sequence-type-length-mismatch-error type length)))
387 (make-list length :initial-element initial-element)))
388 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
389 ;; which may seem strange and non-ideal, but then I'd say
390 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
391 (t (sequence-type-too-hairy (type-specifier type)))))
392 ((csubtypep type (specifier-type 'vector))
393 (cond
394 (;; is it immediately obvious what the result type is?
395 (typep type 'array-type)
396 (progn
397 (aver (= (length (array-type-dimensions type)) 1))
398 (let* ((etype (type-specifier
399 (array-type-specialized-element-type type)))
400 (etype (if (eq etype '*) t etype))
401 (type-length (car (array-type-dimensions type))))
402 (unless (or (eq type-length '*)
403 (= type-length length))
404 (sequence-type-length-mismatch-error type length))
405 (if iep
406 (make-array length :element-type etype
407 :initial-element initial-element)
408 (make-array length :element-type etype)))))
409 (t (sequence-type-too-hairy (type-specifier type)))))
410 ((and (csubtypep type (specifier-type 'sequence))
411 (awhen (find-class adjusted-type nil)
412 (let ((prototype (sb!mop:class-prototype
413 (sb!pcl:ensure-class-finalized it))))
414 ;; This function has the EXPLICIT-CHECK declaration,
415 ;; so we manually assert that it returns a 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 (declare (explicit-check))
773 (seq-dispatch-checking sequence
774 (list-reverse* sequence)
775 (vector-reverse* sequence)
776 ;; The type deriver says that LIST => LIST and VECTOR => VECTOR
777 ;; but does not claim to know anything about extended-sequences.
778 ;; So this could theoretically return any subtype of SEQUENCE
779 ;; given an EXTENDED-SEQUENCE as input. But fndb says this returns
780 ;; a CONSED-SEQUENCE, which precludes non-simple vectors.
781 ;; But a CLOS sequence can apparently decide to return a LIST when
782 ;; reversed. [Is that too weird? Make this EXTENDED-SEQUENCE maybe?]
783 (the consed-sequence (values (sb!sequence:reverse sequence)))))
785 ;;; internal frobs
787 (defun list-reverse* (sequence)
788 (list-reverse-macro sequence))
790 (defun vector-reverse* (sequence)
791 (vector-reverse sequence))
793 ;;;; NREVERSE
795 (eval-when (:compile-toplevel :execute)
797 (sb!xc:defmacro vector-nreverse (sequence)
798 `(let ((length (length (the vector ,sequence))))
799 (when (>= length 2)
800 (do ((left-index 0 (1+ left-index))
801 (right-index (1- length) (1- right-index)))
802 ((<= right-index left-index))
803 (declare (type index left-index right-index))
804 (rotatef (aref ,sequence left-index)
805 (aref ,sequence right-index))))
806 ,sequence))
808 (sb!xc:defmacro list-nreverse-macro (list)
809 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
810 (2nd ,list 1st)
811 (3rd '() 2nd))
812 ((atom 2nd) 3rd)
813 (rplacd 2nd 3rd)))
815 ) ; EVAL-WHEN
817 (defun list-nreverse* (sequence)
818 (list-nreverse-macro sequence))
820 (defun vector-nreverse* (sequence)
821 (vector-nreverse sequence))
823 (defun nreverse (sequence)
824 #!+sb-doc
825 "Return a sequence of the same elements in reverse order; the argument
826 is destroyed."
827 (declare (explicit-check))
828 (seq-dispatch-checking sequence
829 (list-nreverse* sequence)
830 (vector-nreverse* sequence)
831 ;; The type deriver for this is 'result-type-first-arg',
832 ;; meaning it should return definitely an EXTENDED-SEQUENCE
833 ;; and not a list or vector.
834 (the extended-sequence (values (sb!sequence:nreverse sequence)))))
836 ;;;; CONCATENATE
838 (defmacro sb!sequence:dosequence ((element sequence &optional return) &body body)
839 #!+sb-doc
840 "Executes BODY with ELEMENT subsequently bound to each element of
841 SEQUENCE, then returns RETURN."
842 (multiple-value-bind (forms decls) (parse-body body nil)
843 (once-only ((sequence sequence))
844 (with-unique-names (state limit from-end step endp elt)
845 `(block nil
846 (seq-dispatch ,sequence
847 (dolist (,element ,sequence ,return) ,@body)
848 (do-vector-data (,element ,sequence ,return) ,@body)
849 (multiple-value-bind (,state ,limit ,from-end ,step ,endp ,elt)
850 (sb!sequence:make-sequence-iterator ,sequence)
851 (do ((,state ,state (funcall ,step ,sequence ,state ,from-end)))
852 ((funcall ,endp ,sequence ,state ,limit ,from-end)
853 (let ((,element nil))
854 ,@(filter-dolist-declarations decls)
855 (declare (ignorable ,element))
856 ,return))
857 (let ((,element (funcall ,elt ,sequence ,state)))
858 ,@decls
859 (tagbody
860 ,@forms))))))))))
863 (defun concatenate (output-type-spec &rest sequences)
864 #!+sb-doc
865 "Return a new sequence of all the argument sequences concatenated together
866 which shares no structure with the original argument sequences of the
867 specified OUTPUT-TYPE-SPEC."
868 (declare (explicit-check))
869 (flet ((concat-to-list* (sequences)
870 (let ((result (list nil)))
871 (do ((sequences sequences (cdr sequences))
872 (splice result))
873 ((null sequences) (cdr result))
874 (let ((sequence (car sequences)))
875 (sb!sequence:dosequence (e sequence)
876 (setq splice (cdr (rplacd splice (list e)))))))))
877 (concat-to-simple* (type-spec sequences)
878 (do ((seqs sequences (cdr seqs))
879 (total-length 0)
880 (lengths ()))
881 ((null seqs)
882 (do ((sequences sequences (cdr sequences))
883 (lengths lengths (cdr lengths))
884 (index 0)
885 (result (make-sequence type-spec total-length)))
886 ((= index total-length) result)
887 (declare (fixnum index))
888 (let ((sequence (car sequences)))
889 (sb!sequence:dosequence (e sequence)
890 (setf (aref result index) e)
891 (incf index)))))
892 (let ((length (length (car seqs))))
893 (declare (fixnum length))
894 (setq lengths (nconc lengths (list length)))
895 (setq total-length (+ total-length length))))))
896 (let ((type (specifier-type output-type-spec)))
897 (cond
898 ((csubtypep type (specifier-type 'list))
899 (cond
900 ((type= type (specifier-type 'list))
901 (concat-to-list* sequences))
902 ((eq type *empty-type*)
903 (bad-sequence-type-error nil))
904 ((type= type (specifier-type 'null))
905 (unless (every #'emptyp sequences)
906 (sequence-type-length-mismatch-error
907 type (reduce #'+ sequences :key #'length))) ; FIXME: circular list issues.
908 '())
909 ((cons-type-p type)
910 (multiple-value-bind (min exactp)
911 (sb!kernel::cons-type-length-info type)
912 (let ((length (reduce #'+ sequences :key #'length)))
913 (if exactp
914 (unless (= length min)
915 (sequence-type-length-mismatch-error type length))
916 (unless (>= length min)
917 (sequence-type-length-mismatch-error type length)))
918 (concat-to-list* sequences))))
919 (t (sequence-type-too-hairy (type-specifier type)))))
920 ((csubtypep type (specifier-type 'vector))
921 (concat-to-simple* output-type-spec sequences))
922 ((and (csubtypep type (specifier-type 'sequence))
923 (awhen (find-class output-type-spec nil)
924 ;; This function has the EXPLICIT-CHECK declaration,
925 ;; so we manually assert that it returns a SEQUENCE.
926 (the sequence
927 (apply #'sb!sequence:concatenate
928 (sb!mop:class-prototype
929 (sb!pcl:ensure-class-finalized it))
930 sequences)))))
932 (bad-sequence-type-error output-type-spec))))))
934 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
935 ;;; CONCATENATE 'STRING &co into these.
936 (macrolet ((def (name element-type)
937 `(defun ,name (&rest sequences)
938 (declare (dynamic-extent sequences)
939 (explicit-check)
940 (optimize speed)
941 (optimize (sb!c::insert-array-bounds-checks 0)))
942 (let* ((lengths (mapcar #'length sequences))
943 (result (make-array (the integer (apply #'+ lengths))
944 :element-type ',element-type))
945 (start 0))
946 (declare (index start))
947 (dolist (seq sequences)
948 (string-dispatch
949 ((simple-array character (*))
950 (simple-array base-char (*))
953 (replace result seq :start1 start))
954 (incf start (the index (pop lengths))))
955 result))))
956 (def %concatenate-to-string character)
957 (def %concatenate-to-base-string base-char))
960 ;;;; MAP
962 ;;; helper functions to handle arity-1 subcases of MAP
963 (defun %map-to-list-arity-1 (fun sequence)
964 (declare (explicit-check))
965 (let ((reversed-result nil)
966 (really-fun (%coerce-callable-to-fun fun)))
967 (sb!sequence:dosequence (element sequence)
968 (push (funcall really-fun element)
969 reversed-result))
970 (nreverse reversed-result)))
971 (defun %map-to-simple-vector-arity-1 (fun sequence)
972 (declare (explicit-check))
973 (let ((result (make-array (length sequence)))
974 (index 0)
975 (really-fun (%coerce-callable-to-fun fun)))
976 (declare (type index index))
977 (sb!sequence:dosequence (element sequence)
978 (setf (aref result index)
979 (funcall really-fun element))
980 (incf index))
981 result))
982 (defun %map-for-effect-arity-1 (fun sequence)
983 (declare (explicit-check))
984 (let ((really-fun (%coerce-callable-to-fun fun)))
985 (sb!sequence:dosequence (element sequence)
986 (funcall really-fun element)))
987 nil)
989 (declaim (maybe-inline %map-for-effect))
990 (defun %map-for-effect (fun sequences)
991 (declare (type function fun) (type list sequences))
992 (let ((%sequences sequences)
993 (%iters (mapcar (lambda (s)
994 (seq-dispatch s
997 (multiple-value-list
998 (sb!sequence:make-sequence-iterator s))))
999 sequences))
1000 (%apply-args (make-list (length sequences))))
1001 ;; this is almost efficient (except in the general case where we
1002 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
1003 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
1004 ;; TODO: on x86-64, we do have. Now see if the above remark is true.
1005 (declare (type list %sequences %iters %apply-args))
1006 (loop
1007 (do ((in-sequences %sequences (cdr in-sequences))
1008 (in-iters %iters (cdr in-iters))
1009 (in-apply-args %apply-args (cdr in-apply-args)))
1010 ((null in-sequences) (apply fun %apply-args))
1011 (let ((i (car in-iters)))
1012 (declare (type (or list index) i))
1013 (cond
1014 ((listp (car in-sequences))
1015 (if (null i)
1016 (return-from %map-for-effect nil)
1017 (setf (car in-apply-args) (car i)
1018 (car in-iters) (cdr i))))
1019 ((typep i 'index)
1020 (let ((v (the vector (car in-sequences))))
1021 (if (>= i (length v))
1022 (return-from %map-for-effect nil)
1023 (setf (car in-apply-args) (aref v i)
1024 (car in-iters) (1+ i)))))
1026 ;; While on one hand this could benefit from a zero-safety ds-bind,
1027 ;; on the other, why not coerce these tuples to vectors or structs?
1028 (destructuring-bind (state limit from-end step endp elt &rest ignore)
1030 (declare (type function step endp elt)
1031 (ignore ignore))
1032 (let ((s (car in-sequences)))
1033 (if (funcall endp s state limit from-end)
1034 (return-from %map-for-effect nil)
1035 (progn
1036 (setf (car in-apply-args) (funcall elt s state))
1037 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
1038 (defun %map-to-list (fun sequences)
1039 (declare (type function fun)
1040 (type list sequences))
1041 (let ((result nil))
1042 (flet ((f (&rest args)
1043 (declare (truly-dynamic-extent args))
1044 (push (apply fun args) result)))
1045 (declare (truly-dynamic-extent #'f))
1046 (%map-for-effect #'f sequences))
1047 (nreverse result)))
1048 (defun %map-to-vector (output-type-spec fun sequences)
1049 (declare (type function fun)
1050 (type list sequences))
1051 (let ((min-len 0))
1052 (flet ((f (&rest args)
1053 (declare (truly-dynamic-extent args))
1054 (declare (ignore args))
1055 (incf min-len)))
1056 (declare (truly-dynamic-extent #'f))
1057 (%map-for-effect #'f sequences))
1058 (let ((result (make-sequence output-type-spec min-len))
1059 (i 0))
1060 (declare (type (simple-array * (*)) result))
1061 (flet ((f (&rest args)
1062 (declare (truly-dynamic-extent args))
1063 (setf (aref result i) (apply fun args))
1064 (incf i)))
1065 (declare (truly-dynamic-extent #'f))
1066 (%map-for-effect #'f sequences))
1067 result)))
1069 ;;; %MAP is just MAP without the final just-to-be-sure check that
1070 ;;; length of the output sequence matches any length specified
1071 ;;; in RESULT-TYPE.
1072 (defun %map (result-type function &rest sequences)
1073 (declare (explicit-check))
1074 (declare (dynamic-extent sequences))
1075 ;; Everything that we end up calling uses %COERCE-TO-CALLABLE
1076 ;; on FUNCTION so we don't need to declare it of type CALLABLE here.
1077 ;; Additionally all the arity-1 mappers use SEQ-DISPATCH which asserts
1078 ;; that the input is a SEQUENCE. Despite SEQ-DISPATCH being "less safe"
1079 ;; than SEQ-DISPATCH-CHECKING, both are in fact equally safe, because
1080 ;; the ARRAY case (which assumes that all arrays are vectors) utilizes
1081 ;; %WITH-ARRAY-DATA/FP which asserts that its input is a vector.
1082 (labels ((slower-map (type)
1083 (let ((really-fun (%coerce-callable-to-fun function)))
1084 (cond
1085 ((eq type *empty-type*)
1086 (%map-for-effect really-fun sequences))
1087 ((csubtypep type (specifier-type 'list))
1088 (%map-to-list really-fun sequences))
1089 ((csubtypep type (specifier-type 'vector))
1090 (%map-to-vector result-type really-fun sequences))
1091 ((and (csubtypep type (specifier-type 'sequence))
1092 (awhen (find-class result-type nil)
1093 ;; This function has the EXPLICIT-CHECK declaration,
1094 ;; so we manually assert that it returns a SEQUENCE.
1095 (the sequence
1096 (apply #'sb!sequence:map
1097 (sb!mop:class-prototype
1098 (sb!pcl:ensure-class-finalized it))
1099 really-fun sequences)))))
1101 (bad-sequence-type-error result-type))))))
1102 ;; Handle some easy cases faster
1103 (if (/= (length sequences) 1)
1104 (slower-map (specifier-type result-type))
1105 (let ((first-sequence (fast-&rest-nth 0 sequences)))
1106 (case result-type
1107 ((nil)
1108 (%map-for-effect-arity-1 function first-sequence))
1109 ((list cons)
1110 (%map-to-list-arity-1 function first-sequence))
1111 ((vector simple-vector)
1112 (%map-to-simple-vector-arity-1 function first-sequence))
1114 (let ((type (specifier-type result-type)))
1115 (cond ((eq type *empty-type*)
1116 (%map-for-effect-arity-1 function first-sequence))
1117 ((csubtypep type (specifier-type 'list))
1118 (%map-to-list-arity-1 function first-sequence))
1119 ((csubtypep type (specifier-type '(vector t)))
1120 (%map-to-simple-vector-arity-1 function first-sequence))
1122 (slower-map type))))))))))
1124 (defun map (result-type function first-sequence &rest more-sequences)
1125 (declare (explicit-check))
1126 (let ((result
1127 (apply #'%map result-type function first-sequence more-sequences)))
1128 (if (or (eq result-type 'nil) (typep result result-type))
1129 result
1130 (error 'simple-type-error
1131 :format-control "MAP result ~S is not a sequence of type ~S"
1132 :datum result
1133 :expected-type result-type
1134 :format-arguments (list result result-type)))))
1136 ;;;; MAP-INTO
1138 (defmacro map-into-lambda (sequences params &body body)
1139 (check-type sequences symbol)
1140 `(flet ((f ,params ,@body))
1141 (declare (truly-dynamic-extent #'f))
1142 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1143 ;; hence the awkward flip between MAP and LOOP.
1144 (if ,sequences
1145 (apply #'%map nil #'f ,sequences)
1146 (loop (f)))))
1148 (define-array-dispatch vector-map-into (data start end fun sequences)
1149 (declare (type index start end)
1150 (type function fun)
1151 (type list sequences))
1152 (declare (explicit-check))
1153 (let ((index start))
1154 (declare (type index index))
1155 (block mapping
1156 (map-into-lambda sequences (&rest args)
1157 (declare (truly-dynamic-extent args))
1158 (when (eql index end)
1159 (return-from mapping))
1160 (setf (aref data index) (apply fun args))
1161 (incf index)))
1162 index))
1164 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1165 ;;; computing the length of the result sequence since we can detect
1166 ;;; the end during mapping (if MAP even gets that far).
1168 ;;; For each result type, define a mapping function which is
1169 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1170 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1171 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1173 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1174 ;;; Because we are manually doing bounds checking with known types,
1175 ;;; safety is turned off for vectors and lists but kept for generic
1176 ;;; sequences.
1177 (defun map-into (result-sequence function &rest sequences)
1178 (let ((really-fun (%coerce-callable-to-fun function)))
1179 (etypecase result-sequence
1180 (vector
1181 (with-array-data ((data result-sequence) (start) (end)
1182 ;; MAP-INTO ignores fill pointer when mapping
1183 :check-fill-pointer nil)
1184 (let ((new-end (vector-map-into data start end really-fun sequences)))
1185 (when (array-has-fill-pointer-p result-sequence)
1186 (setf (fill-pointer result-sequence) (- new-end start))))))
1187 (list
1188 (let ((node result-sequence))
1189 (declare (type list node))
1190 (map-into-lambda sequences (&rest args)
1191 (declare (truly-dynamic-extent args))
1192 (cond ((null node)
1193 (return-from map-into result-sequence))
1194 ((not (listp (cdr node)))
1195 (error 'simple-type-error
1196 :format-control "~a is not a proper list"
1197 :format-arguments (list result-sequence)
1198 :expected-type 'list
1199 :datum result-sequence)))
1200 (setf (car node) (apply really-fun args))
1201 (setf node (cdr node)))))
1202 (sequence
1203 (multiple-value-bind (iter limit from-end)
1204 (sb!sequence:make-sequence-iterator result-sequence)
1205 (map-into-lambda sequences (&rest args)
1206 (declare (truly-dynamic-extent args) (optimize speed))
1207 (when (sb!sequence:iterator-endp result-sequence
1208 iter limit from-end)
1209 (return-from map-into result-sequence))
1210 (setf (sb!sequence:iterator-element result-sequence iter)
1211 (apply really-fun args))
1212 (setf iter (sb!sequence:iterator-step result-sequence
1213 iter from-end)))))))
1214 result-sequence)
1216 ;;;; REDUCE
1218 (eval-when (:compile-toplevel :execute)
1220 (sb!xc:defmacro mumble-reduce (function
1221 sequence
1223 start
1225 initial-value
1226 ref)
1227 `(do ((index ,start (1+ index))
1228 (value ,initial-value))
1229 ((>= index ,end) value)
1230 (setq value (funcall ,function value
1231 (apply-key ,key (,ref ,sequence index))))))
1233 (sb!xc:defmacro mumble-reduce-from-end (function
1234 sequence
1236 start
1238 initial-value
1239 ref)
1240 `(do ((index (1- ,end) (1- index))
1241 (value ,initial-value)
1242 (terminus (1- ,start)))
1243 ((<= index terminus) value)
1244 (setq value (funcall ,function
1245 (apply-key ,key (,ref ,sequence index))
1246 value))))
1248 (sb!xc:defmacro list-reduce (function
1249 sequence
1251 start
1253 initial-value
1254 ivp)
1255 `(let ((sequence (nthcdr ,start ,sequence)))
1256 (do ((count (if ,ivp ,start (1+ ,start))
1257 (1+ count))
1258 (sequence (if ,ivp sequence (cdr sequence))
1259 (cdr sequence))
1260 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1261 (funcall ,function value (apply-key ,key (car sequence)))))
1262 ((>= count ,end) value))))
1264 (sb!xc:defmacro list-reduce-from-end (function
1265 sequence
1267 start
1269 initial-value
1270 ivp)
1271 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1272 (reverse ,sequence))))
1273 (do ((count (if ,ivp ,start (1+ ,start))
1274 (1+ count))
1275 (sequence (if ,ivp sequence (cdr sequence))
1276 (cdr sequence))
1277 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1278 (funcall ,function (apply-key ,key (car sequence)) value)))
1279 ((>= count ,end) value))))
1281 ) ; EVAL-WHEN
1283 (define-sequence-traverser reduce (function sequence &rest args &key key
1284 from-end start end (initial-value nil ivp))
1285 (declare (type index start)
1286 (truly-dynamic-extent args))
1287 (seq-dispatch sequence
1288 (let ((end (or end length)))
1289 (declare (type index end))
1290 (if (= end start)
1291 (if ivp initial-value (funcall function))
1292 (if from-end
1293 (list-reduce-from-end function sequence key start end
1294 initial-value ivp)
1295 (list-reduce function sequence key start end
1296 initial-value ivp))))
1297 (let ((end (or end length)))
1298 (declare (type index end))
1299 (if (= end start)
1300 (if ivp initial-value (funcall function))
1301 (if from-end
1302 (progn
1303 (when (not ivp)
1304 (setq end (1- (the fixnum end)))
1305 (setq initial-value (apply-key key (aref sequence end))))
1306 (mumble-reduce-from-end function sequence key start end
1307 initial-value aref))
1308 (progn
1309 (when (not ivp)
1310 (setq initial-value (apply-key key (aref sequence start)))
1311 (setq start (1+ start)))
1312 (mumble-reduce function sequence key start end
1313 initial-value aref)))))
1314 (apply #'sb!sequence:reduce function sequence args)))
1316 ;;;; DELETE
1318 (eval-when (:compile-toplevel :execute)
1320 (sb!xc:defmacro mumble-delete (pred)
1321 `(do ((index start (1+ index))
1322 (jndex start)
1323 (number-zapped 0))
1324 ((or (= index (the fixnum end)) (= number-zapped count))
1325 (do ((index index (1+ index)) ; Copy the rest of the vector.
1326 (jndex jndex (1+ jndex)))
1327 ((= index (the fixnum length))
1328 (shrink-vector sequence jndex))
1329 (declare (fixnum index jndex))
1330 (setf (aref sequence jndex) (aref sequence index))))
1331 (declare (fixnum index jndex number-zapped))
1332 (setf (aref sequence jndex) (aref sequence index))
1333 (if ,pred
1334 (incf number-zapped)
1335 (incf jndex))))
1337 (sb!xc:defmacro mumble-delete-from-end (pred)
1338 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1339 (number-zapped 0)
1340 (losers ())
1341 this-element
1342 (terminus (1- start)))
1343 ((or (= index terminus) (= number-zapped count))
1344 (do ((losers losers) ; Delete the losers.
1345 (index start (1+ index))
1346 (jndex start))
1347 ((or (null losers) (= index (the fixnum end)))
1348 (do ((index index (1+ index)) ; Copy the rest of the vector.
1349 (jndex jndex (1+ jndex)))
1350 ((= index (the fixnum length))
1351 (shrink-vector sequence jndex))
1352 (declare (fixnum index jndex))
1353 (setf (aref sequence jndex) (aref sequence index))))
1354 (declare (fixnum index jndex))
1355 (setf (aref sequence jndex) (aref sequence index))
1356 (if (= index (the fixnum (car losers)))
1357 (pop losers)
1358 (incf jndex))))
1359 (declare (fixnum index number-zapped terminus))
1360 (setq this-element (aref sequence index))
1361 (when ,pred
1362 (incf number-zapped)
1363 (push index losers))))
1365 (sb!xc:defmacro normal-mumble-delete ()
1366 `(mumble-delete
1367 (if test-not
1368 (not (funcall test-not item (apply-key key (aref sequence index))))
1369 (funcall test item (apply-key key (aref sequence index))))))
1371 (sb!xc:defmacro normal-mumble-delete-from-end ()
1372 `(mumble-delete-from-end
1373 (if test-not
1374 (not (funcall test-not item (apply-key key this-element)))
1375 (funcall test item (apply-key key this-element)))))
1377 (sb!xc:defmacro list-delete (pred)
1378 `(let ((handle (cons nil sequence)))
1379 (declare (truly-dynamic-extent handle))
1380 (do ((current (nthcdr start sequence) (cdr current))
1381 (previous (nthcdr start handle))
1382 (index start (1+ index))
1383 (number-zapped 0))
1384 ((or (= index (the fixnum end)) (= number-zapped count))
1385 (cdr handle))
1386 (declare (fixnum index number-zapped))
1387 (cond (,pred
1388 (rplacd previous (cdr current))
1389 (incf number-zapped))
1391 (setq previous (cdr previous)))))))
1393 (sb!xc:defmacro list-delete-from-end (pred)
1394 `(let* ((reverse (nreverse (the list sequence)))
1395 (handle (cons nil reverse)))
1396 (declare (truly-dynamic-extent handle))
1397 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1398 (cdr current))
1399 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1400 (index start (1+ index))
1401 (number-zapped 0))
1402 ((or (= index (the fixnum end)) (= number-zapped count))
1403 (nreverse (cdr handle)))
1404 (declare (fixnum index number-zapped))
1405 (cond (,pred
1406 (rplacd previous (cdr current))
1407 (incf number-zapped))
1409 (setq previous (cdr previous)))))))
1411 (sb!xc:defmacro normal-list-delete ()
1412 '(list-delete
1413 (if test-not
1414 (not (funcall test-not item (apply-key key (car current))))
1415 (funcall test item (apply-key key (car current))))))
1417 (sb!xc:defmacro normal-list-delete-from-end ()
1418 '(list-delete-from-end
1419 (if test-not
1420 (not (funcall test-not item (apply-key key (car current))))
1421 (funcall test item (apply-key key (car current))))))
1423 ) ; EVAL-WHEN
1425 (define-sequence-traverser delete
1426 (item sequence &rest args &key from-end test test-not start
1427 end count key)
1428 #!+sb-doc
1429 "Return a sequence formed by destructively removing the specified ITEM from
1430 the given SEQUENCE."
1431 (declare (type fixnum start)
1432 (truly-dynamic-extent args))
1433 (seq-dispatch sequence
1434 (let ((end (or end length)))
1435 (declare (type index end))
1436 (if from-end
1437 (normal-list-delete-from-end)
1438 (normal-list-delete)))
1439 (let ((end (or end length)))
1440 (declare (type index end))
1441 (if from-end
1442 (normal-mumble-delete-from-end)
1443 (normal-mumble-delete)))
1444 (apply #'sb!sequence:delete item sequence args)))
1446 (eval-when (:compile-toplevel :execute)
1448 (sb!xc:defmacro if-mumble-delete ()
1449 `(mumble-delete
1450 (funcall predicate (apply-key key (aref sequence index)))))
1452 (sb!xc:defmacro if-mumble-delete-from-end ()
1453 `(mumble-delete-from-end
1454 (funcall predicate (apply-key key this-element))))
1456 (sb!xc:defmacro if-list-delete ()
1457 '(list-delete
1458 (funcall predicate (apply-key key (car current)))))
1460 (sb!xc:defmacro if-list-delete-from-end ()
1461 '(list-delete-from-end
1462 (funcall predicate (apply-key key (car current)))))
1464 ) ; EVAL-WHEN
1466 (define-sequence-traverser delete-if
1467 (predicate sequence &rest args &key from-end start key end count)
1468 #!+sb-doc
1469 "Return a sequence formed by destructively removing the elements satisfying
1470 the specified PREDICATE from the given SEQUENCE."
1471 (declare (type fixnum start)
1472 (truly-dynamic-extent args))
1473 (seq-dispatch sequence
1474 (let ((end (or end length)))
1475 (declare (type index end))
1476 (if from-end
1477 (if-list-delete-from-end)
1478 (if-list-delete)))
1479 (let ((end (or end length)))
1480 (declare (type index end))
1481 (if from-end
1482 (if-mumble-delete-from-end)
1483 (if-mumble-delete)))
1484 (apply #'sb!sequence:delete-if predicate sequence args)))
1486 (eval-when (:compile-toplevel :execute)
1488 (sb!xc:defmacro if-not-mumble-delete ()
1489 `(mumble-delete
1490 (not (funcall predicate (apply-key key (aref sequence index))))))
1492 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1493 `(mumble-delete-from-end
1494 (not (funcall predicate (apply-key key this-element)))))
1496 (sb!xc:defmacro if-not-list-delete ()
1497 '(list-delete
1498 (not (funcall predicate (apply-key key (car current))))))
1500 (sb!xc:defmacro if-not-list-delete-from-end ()
1501 '(list-delete-from-end
1502 (not (funcall predicate (apply-key key (car current))))))
1504 ) ; EVAL-WHEN
1506 (define-sequence-traverser delete-if-not
1507 (predicate sequence &rest args &key from-end start end key count)
1508 #!+sb-doc
1509 "Return a sequence formed by destructively removing the elements not
1510 satisfying the specified PREDICATE from the given SEQUENCE."
1511 (declare (type fixnum start)
1512 (truly-dynamic-extent args))
1513 (seq-dispatch sequence
1514 (let ((end (or end length)))
1515 (declare (type index end))
1516 (if from-end
1517 (if-not-list-delete-from-end)
1518 (if-not-list-delete)))
1519 (let ((end (or end length)))
1520 (declare (type index end))
1521 (if from-end
1522 (if-not-mumble-delete-from-end)
1523 (if-not-mumble-delete)))
1524 (apply #'sb!sequence:delete-if-not predicate sequence args)))
1526 ;;;; REMOVE
1528 (eval-when (:compile-toplevel :execute)
1530 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1531 ;;; satisfies the predicate.
1532 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1533 `(do ((index ,begin (,bump index))
1534 (result
1535 (do ((index ,left (,bump index))
1536 (result (%make-sequence-like sequence length)))
1537 ((= index (the fixnum ,begin)) result)
1538 (declare (fixnum index))
1539 (setf (aref result index) (aref sequence index))))
1540 (new-index ,begin)
1541 (number-zapped 0)
1542 (this-element))
1543 ((or (= index (the fixnum ,finish))
1544 (= number-zapped count))
1545 (do ((index index (,bump index))
1546 (new-index new-index (,bump new-index)))
1547 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1548 (declare (fixnum index new-index))
1549 (setf (aref result new-index) (aref sequence index))))
1550 (declare (fixnum index new-index number-zapped))
1551 (setq this-element (aref sequence index))
1552 (cond (,pred (incf number-zapped))
1553 (t (setf (aref result new-index) this-element)
1554 (setq new-index (,bump new-index))))))
1556 (sb!xc:defmacro mumble-remove (pred)
1557 `(mumble-remove-macro 1+ 0 start end length ,pred))
1559 (sb!xc:defmacro mumble-remove-from-end (pred)
1560 `(let ((sequence (copy-seq sequence)))
1561 (mumble-delete-from-end ,pred)))
1563 (sb!xc:defmacro normal-mumble-remove ()
1564 `(mumble-remove
1565 (if test-not
1566 (not (funcall test-not item (apply-key key this-element)))
1567 (funcall test item (apply-key key this-element)))))
1569 (sb!xc:defmacro normal-mumble-remove-from-end ()
1570 `(mumble-remove-from-end
1571 (if test-not
1572 (not (funcall test-not item (apply-key key this-element)))
1573 (funcall test item (apply-key key this-element)))))
1575 (sb!xc:defmacro if-mumble-remove ()
1576 `(mumble-remove (funcall predicate (apply-key key this-element))))
1578 (sb!xc:defmacro if-mumble-remove-from-end ()
1579 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1581 (sb!xc:defmacro if-not-mumble-remove ()
1582 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1584 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1585 `(mumble-remove-from-end
1586 (not (funcall predicate (apply-key key this-element)))))
1588 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1589 ;;; the predicate.
1590 (sb!xc:defmacro list-remove-macro (pred reverse?)
1591 `(let* ((sequence ,(if reverse?
1592 '(reverse (the list sequence))
1593 'sequence))
1594 (%start ,(if reverse? '(- length end) 'start))
1595 (%end ,(if reverse? '(- length start) 'end))
1596 (splice (list nil))
1597 (results (do ((index 0 (1+ index))
1598 (before-start splice))
1599 ((= index (the fixnum %start)) before-start)
1600 (declare (fixnum index))
1601 (setq splice
1602 (cdr (rplacd splice (list (pop sequence))))))))
1603 (do ((index %start (1+ index))
1604 (this-element)
1605 (number-zapped 0))
1606 ((or (= index (the fixnum %end)) (= number-zapped count))
1607 (do ((index index (1+ index)))
1608 ((null sequence)
1609 ,(if reverse?
1610 '(nreverse (the list (cdr results)))
1611 '(cdr results)))
1612 (declare (fixnum index))
1613 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1614 (declare (fixnum index number-zapped))
1615 (setq this-element (pop sequence))
1616 (if ,pred
1617 (setq number-zapped (1+ number-zapped))
1618 (setq splice (cdr (rplacd splice (list this-element))))))))
1620 (sb!xc:defmacro list-remove (pred)
1621 `(list-remove-macro ,pred nil))
1623 (sb!xc:defmacro list-remove-from-end (pred)
1624 `(list-remove-macro ,pred t))
1626 (sb!xc:defmacro normal-list-remove ()
1627 `(list-remove
1628 (if test-not
1629 (not (funcall test-not item (apply-key key this-element)))
1630 (funcall test item (apply-key key this-element)))))
1632 (sb!xc:defmacro normal-list-remove-from-end ()
1633 `(list-remove-from-end
1634 (if test-not
1635 (not (funcall test-not item (apply-key key this-element)))
1636 (funcall test item (apply-key key this-element)))))
1638 (sb!xc:defmacro if-list-remove ()
1639 `(list-remove
1640 (funcall predicate (apply-key key this-element))))
1642 (sb!xc:defmacro if-list-remove-from-end ()
1643 `(list-remove-from-end
1644 (funcall predicate (apply-key key this-element))))
1646 (sb!xc:defmacro if-not-list-remove ()
1647 `(list-remove
1648 (not (funcall predicate (apply-key key this-element)))))
1650 (sb!xc:defmacro if-not-list-remove-from-end ()
1651 `(list-remove-from-end
1652 (not (funcall predicate (apply-key key this-element)))))
1654 ) ; EVAL-WHEN
1656 (define-sequence-traverser remove
1657 (item sequence &rest args &key from-end test test-not start
1658 end count key)
1659 #!+sb-doc
1660 "Return a copy of SEQUENCE with elements satisfying the test (default is
1661 EQL) with ITEM removed."
1662 (declare (type fixnum start)
1663 (truly-dynamic-extent args))
1664 (seq-dispatch sequence
1665 (let ((end (or end length)))
1666 (declare (type index end))
1667 (if from-end
1668 (normal-list-remove-from-end)
1669 (normal-list-remove)))
1670 (let ((end (or end length)))
1671 (declare (type index end))
1672 (if from-end
1673 (normal-mumble-remove-from-end)
1674 (normal-mumble-remove)))
1675 (apply #'sb!sequence:remove item sequence args)))
1677 (define-sequence-traverser remove-if
1678 (predicate sequence &rest args &key from-end start end count key)
1679 #!+sb-doc
1680 "Return a copy of sequence with elements satisfying PREDICATE removed."
1681 (declare (type fixnum start)
1682 (truly-dynamic-extent args))
1683 (seq-dispatch sequence
1684 (let ((end (or end length)))
1685 (declare (type index end))
1686 (if from-end
1687 (if-list-remove-from-end)
1688 (if-list-remove)))
1689 (let ((end (or end length)))
1690 (declare (type index end))
1691 (if from-end
1692 (if-mumble-remove-from-end)
1693 (if-mumble-remove)))
1694 (apply #'sb!sequence:remove-if predicate sequence args)))
1696 (define-sequence-traverser remove-if-not
1697 (predicate sequence &rest args &key from-end start end count key)
1698 #!+sb-doc
1699 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1700 (declare (type fixnum start)
1701 (truly-dynamic-extent args))
1702 (seq-dispatch sequence
1703 (let ((end (or end length)))
1704 (declare (type index end))
1705 (if from-end
1706 (if-not-list-remove-from-end)
1707 (if-not-list-remove)))
1708 (let ((end (or end length)))
1709 (declare (type index end))
1710 (if from-end
1711 (if-not-mumble-remove-from-end)
1712 (if-not-mumble-remove)))
1713 (apply #'sb!sequence:remove-if-not predicate sequence args)))
1715 ;;;; REMOVE-DUPLICATES
1717 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1718 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1719 ;;; if we look into the already copied structure (from after :start) and see
1720 ;;; the item. If we check from beginning we check into the rest of the
1721 ;;; original list up to the :end marker (this we have to do by running a
1722 ;;; do loop down the list that far and using our test.
1723 (defun list-remove-duplicates* (list test test-not start end key from-end)
1724 (declare (fixnum start))
1725 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1726 (splice result)
1727 (current list)
1728 (end (or end (length list)))
1729 (hash (and (> (- end start) 20)
1730 test
1731 (not key)
1732 (not test-not)
1733 (or (eql test #'eql)
1734 (eql test #'eq)
1735 (eql test #'equal)
1736 (eql test #'equalp))
1737 (make-hash-table :test test :size (- end start)))))
1738 (do ((index 0 (1+ index)))
1739 ((= index start))
1740 (declare (fixnum index))
1741 (setq splice (cdr (rplacd splice (list (car current)))))
1742 (setq current (cdr current)))
1743 (if hash
1744 (do ((index start (1+ index)))
1745 ((or (and end (= index (the fixnum end)))
1746 (atom current)))
1747 (declare (fixnum index))
1748 ;; The hash table contains links from values that are
1749 ;; already in result to the cons cell *preceding* theirs
1750 ;; in the list. That is, for each value v in the list,
1751 ;; v and (cadr (gethash v hash)) are equal under TEST.
1752 (let ((prev (gethash (car current) hash)))
1753 (cond
1754 ((not prev)
1755 (setf (gethash (car current) hash) splice)
1756 (setq splice (cdr (rplacd splice (list (car current))))))
1757 ((not from-end)
1758 (let* ((old (cdr prev))
1759 (next (cdr old)))
1760 (if next
1761 (let ((next-val (car next)))
1762 ;; (assert (eq (gethash next-val hash) old))
1763 (setf (cdr prev) next
1764 (gethash next-val hash) prev
1765 (gethash (car current) hash) splice
1766 splice (cdr (rplacd splice (list (car current))))))
1767 (setf (car old) (car current)))))))
1768 (setq current (cdr current)))
1769 (do ((index start (1+ index)))
1770 ((or (and end (= index (the fixnum end)))
1771 (atom current)))
1772 (declare (fixnum index))
1773 (if (or (and from-end
1774 (not (if test-not
1775 (member (apply-key key (car current))
1776 (nthcdr (1+ start) result)
1777 :test-not test-not
1778 :key key)
1779 (member (apply-key key (car current))
1780 (nthcdr (1+ start) result)
1781 :test test
1782 :key key))))
1783 (and (not from-end)
1784 (not (do ((it (apply-key key (car current)))
1785 (l (cdr current) (cdr l))
1786 (i (1+ index) (1+ i)))
1787 ((or (atom l) (and end (= i (the fixnum end))))
1789 (declare (fixnum i))
1790 (if (if test-not
1791 (not (funcall test-not
1793 (apply-key key (car l))))
1794 (funcall test it (apply-key key (car l))))
1795 (return t))))))
1796 (setq splice (cdr (rplacd splice (list (car current))))))
1797 (setq current (cdr current))))
1798 (do ()
1799 ((atom current))
1800 (setq splice (cdr (rplacd splice (list (car current)))))
1801 (setq current (cdr current)))
1802 (cdr result)))
1804 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1805 &optional (length (length vector)))
1806 (declare (vector vector) (fixnum start length))
1807 (when (null end) (setf end (length vector)))
1808 (let ((result (%make-sequence-like vector length))
1809 (index 0)
1810 (jndex start))
1811 (declare (fixnum index jndex))
1812 (do ()
1813 ((= index start))
1814 (setf (aref result index) (aref vector index))
1815 (setq index (1+ index)))
1816 (do ((elt))
1817 ((= index end))
1818 (setq elt (aref vector index))
1819 (unless (or (and from-end
1820 (if test-not
1821 (position (apply-key key elt) result
1822 :start start :end jndex
1823 :test-not test-not :key key)
1824 (position (apply-key key elt) result
1825 :start start :end jndex
1826 :test test :key key)))
1827 (and (not from-end)
1828 (if test-not
1829 (position (apply-key key elt) vector
1830 :start (1+ index) :end end
1831 :test-not test-not :key key)
1832 (position (apply-key key elt) vector
1833 :start (1+ index) :end end
1834 :test test :key key))))
1835 (setf (aref result jndex) elt)
1836 (setq jndex (1+ jndex)))
1837 (setq index (1+ index)))
1838 (do ()
1839 ((= index length))
1840 (setf (aref result jndex) (aref vector index))
1841 (setq index (1+ index))
1842 (setq jndex (1+ jndex)))
1843 (%shrink-vector result jndex)))
1845 (define-sequence-traverser remove-duplicates
1846 (sequence &rest args &key test test-not start end from-end key)
1847 #!+sb-doc
1848 "The elements of SEQUENCE are compared pairwise, and if any two match,
1849 the one occurring earlier is discarded, unless FROM-END is true, in
1850 which case the one later in the sequence is discarded. The resulting
1851 sequence is returned.
1853 The :TEST-NOT argument is deprecated."
1854 (declare (fixnum start)
1855 (truly-dynamic-extent args))
1856 (seq-dispatch sequence
1857 (if sequence
1858 (list-remove-duplicates* sequence test test-not
1859 start end key from-end))
1860 (vector-remove-duplicates* sequence test test-not start end key from-end)
1861 (apply #'sb!sequence:remove-duplicates sequence args)))
1863 ;;;; DELETE-DUPLICATES
1865 (defun list-delete-duplicates* (list test test-not key from-end start end)
1866 (declare (fixnum start))
1867 (let ((handle (cons nil list)))
1868 (declare (truly-dynamic-extent handle))
1869 (do ((current (nthcdr start list) (cdr current))
1870 (previous (nthcdr start handle))
1871 (index start (1+ index)))
1872 ((or (and end (= index (the fixnum end))) (null current))
1873 (cdr handle))
1874 (declare (fixnum index))
1875 (if (do ((x (if from-end
1876 (nthcdr (1+ start) handle)
1877 (cdr current))
1878 (cdr x))
1879 (i (1+ index) (1+ i)))
1880 ((or (null x)
1881 (and (not from-end) end (= i (the fixnum end)))
1882 (eq x current))
1883 nil)
1884 (declare (fixnum i))
1885 (if (if test-not
1886 (not (funcall test-not
1887 (apply-key key (car current))
1888 (apply-key key (car x))))
1889 (funcall test
1890 (apply-key key (car current))
1891 (apply-key key (car x))))
1892 (return t)))
1893 (rplacd previous (cdr current))
1894 (setq previous (cdr previous))))))
1896 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1897 &optional (length (length vector)))
1898 (declare (vector vector) (fixnum start length))
1899 (when (null end) (setf end (length vector)))
1900 (do ((index start (1+ index))
1901 (jndex start))
1902 ((= index end)
1903 (do ((index index (1+ index)) ; copy the rest of the vector
1904 (jndex jndex (1+ jndex)))
1905 ((= index length)
1906 (shrink-vector vector jndex))
1907 (setf (aref vector jndex) (aref vector index))))
1908 (declare (fixnum index jndex))
1909 (setf (aref vector jndex) (aref vector index))
1910 (unless (if test-not
1911 (position (apply-key key (aref vector index)) vector :key key
1912 :start (if from-end start (1+ index))
1913 :end (if from-end jndex end)
1914 :test-not test-not)
1915 (position (apply-key key (aref vector index)) vector :key key
1916 :start (if from-end start (1+ index))
1917 :end (if from-end jndex end)
1918 :test test))
1919 (setq jndex (1+ jndex)))))
1921 (define-sequence-traverser delete-duplicates
1922 (sequence &rest args &key test test-not start end from-end key)
1923 #!+sb-doc
1924 "The elements of SEQUENCE are examined, and if any two match, one is
1925 discarded. The resulting sequence, which may be formed by destroying the
1926 given sequence, is returned.
1928 The :TEST-NOT argument is deprecated."
1929 (declare (truly-dynamic-extent args))
1930 (seq-dispatch sequence
1931 (when sequence
1932 (list-delete-duplicates* sequence test test-not
1933 key from-end start end))
1934 (vector-delete-duplicates* sequence test test-not key from-end start end)
1935 (apply #'sb!sequence:delete-duplicates sequence args)))
1937 ;;;; SUBSTITUTE
1939 (defun list-substitute* (pred new list start end count key test test-not old)
1940 (declare (fixnum start end count))
1941 (let* ((result (list nil))
1943 (splice result)
1944 (list list)) ; Get a local list for a stepper.
1945 (do ((index 0 (1+ index)))
1946 ((= index start))
1947 (declare (fixnum index))
1948 (setq splice (cdr (rplacd splice (list (car list)))))
1949 (setq list (cdr list)))
1950 (do ((index start (1+ index)))
1951 ((or (= index end) (null list) (= count 0)))
1952 (declare (fixnum index))
1953 (setq elt (car list))
1954 (setq splice
1955 (cdr (rplacd splice
1956 (list
1957 (cond
1958 ((case pred
1959 (normal
1960 (if test-not
1961 (not
1962 (funcall test-not old (apply-key key elt)))
1963 (funcall test old (apply-key key elt))))
1964 (if (funcall test (apply-key key elt)))
1965 (if-not (not (funcall test (apply-key key elt)))))
1966 (decf count)
1967 new)
1968 (t elt))))))
1969 (setq list (cdr list)))
1970 (do ()
1971 ((null list))
1972 (setq splice (cdr (rplacd splice (list (car list)))))
1973 (setq list (cdr list)))
1974 (cdr result)))
1976 ;;; Replace old with new in sequence moving from left to right by incrementer
1977 ;;; on each pass through the loop. Called by all three substitute functions.
1978 (defun vector-substitute* (pred new sequence incrementer left right length
1979 start end count key test test-not old)
1980 (declare (fixnum start count end incrementer right))
1981 (let ((result (%make-sequence-like sequence length))
1982 (index left))
1983 (declare (fixnum index))
1984 (do ()
1985 ((= index start))
1986 (setf (aref result index) (aref sequence index))
1987 (setq index (+ index incrementer)))
1988 (do ((elt))
1989 ((or (= index end) (= count 0)))
1990 (setq elt (aref sequence index))
1991 (setf (aref result index)
1992 (cond ((case pred
1993 (normal
1994 (if test-not
1995 (not (funcall test-not old (apply-key key elt)))
1996 (funcall test old (apply-key key elt))))
1997 (if (funcall test (apply-key key elt)))
1998 (if-not (not (funcall test (apply-key key elt)))))
1999 (setq count (1- count))
2000 new)
2001 (t elt)))
2002 (setq index (+ index incrementer)))
2003 (do ()
2004 ((= index right))
2005 (setf (aref result index) (aref sequence index))
2006 (setq index (+ index incrementer)))
2007 result))
2009 (eval-when (:compile-toplevel :execute)
2011 (sb!xc:defmacro subst-dispatch (pred)
2012 `(seq-dispatch sequence
2013 (let ((end (or end length)))
2014 (declare (type index end))
2015 (if from-end
2016 (nreverse (list-substitute* ,pred
2018 (reverse sequence)
2019 (- (the fixnum length)
2020 (the fixnum end))
2021 (- (the fixnum length)
2022 (the fixnum start))
2023 count key test test-not old))
2024 (list-substitute* ,pred
2025 new sequence start end count key test test-not
2026 old)))
2028 (let ((end (or end length)))
2029 (declare (type index end))
2030 (if from-end
2031 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2032 -1 length (1- (the fixnum end))
2033 (1- (the fixnum start))
2034 count key test test-not old)
2035 (vector-substitute* ,pred new sequence 1 0 length length
2036 start end count key test test-not old)))
2038 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2039 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2040 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2041 ;; dispatch once per element on PRED's run-time identity.
2042 ,(ecase (cadr pred)
2043 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2044 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2045 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2046 ) ; EVAL-WHEN
2048 (define-sequence-traverser substitute
2049 (new old sequence &rest args &key from-end test test-not
2050 start count end key)
2051 #!+sb-doc
2052 "Return a sequence of the same kind as SEQUENCE with the same elements,
2053 except that all elements equal to OLD are replaced with NEW."
2054 (declare (type fixnum start)
2055 (truly-dynamic-extent args))
2056 (subst-dispatch 'normal))
2058 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2060 (define-sequence-traverser substitute-if
2061 (new predicate sequence &rest args &key from-end start end count key)
2062 #!+sb-doc
2063 "Return a sequence of the same kind as SEQUENCE with the same elements
2064 except that all elements satisfying the PRED are replaced with NEW."
2065 (declare (type fixnum start)
2066 (truly-dynamic-extent args))
2067 (let ((test predicate)
2068 (test-not nil)
2069 old)
2070 (subst-dispatch 'if)))
2072 (define-sequence-traverser substitute-if-not
2073 (new predicate sequence &rest args &key from-end start end count key)
2074 #!+sb-doc
2075 "Return a sequence of the same kind as SEQUENCE with the same elements
2076 except that all elements not satisfying the PRED are replaced with NEW."
2077 (declare (type fixnum start)
2078 (truly-dynamic-extent args))
2079 (let ((test predicate)
2080 (test-not nil)
2081 old)
2082 (subst-dispatch 'if-not)))
2084 ;;;; NSUBSTITUTE
2086 (define-sequence-traverser nsubstitute
2087 (new old sequence &rest args &key from-end test test-not
2088 end count key start)
2089 #!+sb-doc
2090 "Return a sequence of the same kind as SEQUENCE with the same elements
2091 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2092 may be destructively modified."
2093 (declare (type fixnum start)
2094 (truly-dynamic-extent args))
2095 (seq-dispatch sequence
2096 (let ((end (or end length)))
2097 (declare (type index end))
2098 (if from-end
2099 (nreverse (nlist-substitute*
2100 new old (nreverse (the list sequence))
2101 test test-not (- length end) (- length start)
2102 count key))
2103 (nlist-substitute* new old sequence
2104 test test-not start end count key)))
2105 (let ((end (or end length)))
2106 (declare (type index end))
2107 (if from-end
2108 (nvector-substitute* new old sequence -1
2109 test test-not (1- end) (1- start) count key)
2110 (nvector-substitute* new old sequence 1
2111 test test-not start end count key)))
2112 (apply #'sb!sequence:nsubstitute new old sequence args)))
2114 (defun nlist-substitute* (new old sequence test test-not start end count key)
2115 (declare (fixnum start count end))
2116 (do ((list (nthcdr start sequence) (cdr list))
2117 (index start (1+ index)))
2118 ((or (= index end) (null list) (= count 0)) sequence)
2119 (declare (fixnum index))
2120 (when (if test-not
2121 (not (funcall test-not old (apply-key key (car list))))
2122 (funcall test old (apply-key key (car list))))
2123 (rplaca list new)
2124 (setq count (1- count)))))
2126 (defun nvector-substitute* (new old sequence incrementer
2127 test test-not start end count key)
2128 (declare (fixnum start incrementer count end))
2129 (do ((index start (+ index incrementer)))
2130 ((or (= index end) (= count 0)) sequence)
2131 (declare (fixnum index))
2132 (when (if test-not
2133 (not (funcall test-not
2135 (apply-key key (aref sequence index))))
2136 (funcall test old (apply-key key (aref sequence index))))
2137 (setf (aref sequence index) new)
2138 (setq count (1- count)))))
2140 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2142 (define-sequence-traverser nsubstitute-if
2143 (new predicate sequence &rest args &key from-end start end count key)
2144 #!+sb-doc
2145 "Return a sequence of the same kind as SEQUENCE with the same elements
2146 except that all elements satisfying PREDICATE are replaced with NEW.
2147 SEQUENCE may be destructively modified."
2148 (declare (type fixnum start)
2149 (truly-dynamic-extent args))
2150 (seq-dispatch sequence
2151 (let ((end (or end length)))
2152 (declare (type index end))
2153 (if from-end
2154 (nreverse (nlist-substitute-if*
2155 new predicate (nreverse (the list sequence))
2156 (- length end) (- length start) count key))
2157 (nlist-substitute-if* new predicate sequence
2158 start end count key)))
2159 (let ((end (or end length)))
2160 (declare (type index end))
2161 (if from-end
2162 (nvector-substitute-if* new predicate sequence -1
2163 (1- end) (1- start) count key)
2164 (nvector-substitute-if* new predicate sequence 1
2165 start end count key)))
2166 (apply #'sb!sequence:nsubstitute-if new predicate sequence args)))
2168 (defun nlist-substitute-if* (new test sequence start end count key)
2169 (declare (type fixnum end)
2170 (type function test)) ; coercion is done by caller
2171 (do ((list (nthcdr start sequence) (cdr list))
2172 (index start (1+ index)))
2173 ((or (= index end) (null list) (= count 0)) sequence)
2174 (when (funcall test (apply-key key (car list)))
2175 (rplaca list new)
2176 (setq count (1- count)))))
2178 (defun nvector-substitute-if* (new test sequence incrementer
2179 start end count key)
2180 (declare (type fixnum end)
2181 (type function test)) ; coercion is done by caller
2182 (do ((index start (+ index incrementer)))
2183 ((or (= index end) (= count 0)) sequence)
2184 (when (funcall test (apply-key key (aref sequence index)))
2185 (setf (aref sequence index) new)
2186 (setq count (1- count)))))
2188 (define-sequence-traverser nsubstitute-if-not
2189 (new predicate sequence &rest args &key from-end start end count key)
2190 #!+sb-doc
2191 "Return a sequence of the same kind as SEQUENCE with the same elements
2192 except that all elements not satisfying PREDICATE are replaced with NEW.
2193 SEQUENCE may be destructively modified."
2194 (declare (type fixnum start)
2195 (truly-dynamic-extent args))
2196 (seq-dispatch sequence
2197 (let ((end (or end length)))
2198 (declare (fixnum end))
2199 (if from-end
2200 (nreverse (nlist-substitute-if-not*
2201 new predicate (nreverse (the list sequence))
2202 (- length end) (- length start) count key))
2203 (nlist-substitute-if-not* new predicate sequence
2204 start end count key)))
2205 (let ((end (or end length)))
2206 (declare (fixnum end))
2207 (if from-end
2208 (nvector-substitute-if-not* new predicate sequence -1
2209 (1- end) (1- start) count key)
2210 (nvector-substitute-if-not* new predicate sequence 1
2211 start end count key)))
2212 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args)))
2214 (defun nlist-substitute-if-not* (new test sequence start end count key)
2215 (declare (type fixnum end)
2216 (type function test)) ; coercion is done by caller
2217 (do ((list (nthcdr start sequence) (cdr list))
2218 (index start (1+ index)))
2219 ((or (= index end) (null list) (= count 0)) sequence)
2220 (when (not (funcall test (apply-key key (car list))))
2221 (rplaca list new)
2222 (decf count))))
2224 (defun nvector-substitute-if-not* (new test sequence incrementer
2225 start end count key)
2226 (declare (type fixnum end)
2227 (type function test)) ; coercion is done by caller
2228 (do ((index start (+ index incrementer)))
2229 ((or (= index end) (= count 0)) sequence)
2230 (when (not (funcall test (apply-key key (aref sequence index))))
2231 (setf (aref sequence index) new)
2232 (decf count))))
2234 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2236 (defun effective-find-position-test (test test-not)
2237 (effective-find-position-test test test-not))
2238 (defun effective-find-position-key (key)
2239 (effective-find-position-key key))
2241 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2242 (macrolet (;; shared logic for defining %FIND-POSITION and
2243 ;; %FIND-POSITION-IF in terms of various inlineable cases
2244 ;; of the expression defined in FROB and VECTOR*-FROB
2245 (frobs (&optional bit-frob)
2246 `(seq-dispatch sequence-arg
2247 (frob sequence-arg from-end)
2248 (with-array-data ((sequence sequence-arg :offset-var offset)
2249 (start start)
2250 (end end)
2251 :check-fill-pointer t)
2252 (multiple-value-bind (f p)
2253 (macrolet ((frob2 () `(if from-end
2254 (frob sequence t)
2255 (frob sequence nil))))
2256 (typecase sequence
2257 #!+sb-unicode
2258 ((simple-array character (*)) (frob2))
2259 ((simple-array base-char (*)) (frob2))
2260 ,@(when bit-frob
2261 `((simple-bit-vector
2262 (if (and (typep item 'bit)
2263 (eq #'identity key)
2264 (or (eq #'eq test)
2265 (eq #'eql test)
2266 (eq #'equal test)))
2267 (let ((p (%bit-position item sequence
2268 from-end start end)))
2269 (if p
2270 (values item p)
2271 (values nil nil)))
2272 (vector*-frob sequence)))))
2274 (vector*-frob sequence))))
2275 (declare (type (or index null) p))
2276 (values f (and p (the index (- p offset)))))))))
2277 (defun %find-position (item sequence-arg from-end start end key test)
2278 (macrolet ((frob (sequence from-end)
2279 `(%find-position item ,sequence
2280 ,from-end start end key test))
2281 (vector*-frob (sequence)
2282 `(%find-position-vector-macro item ,sequence
2283 from-end start end key test)))
2284 (frobs t)))
2285 (defun %find-position-if (predicate sequence-arg from-end start end key)
2286 (macrolet ((frob (sequence from-end)
2287 `(%find-position-if predicate ,sequence
2288 ,from-end start end key))
2289 (vector*-frob (sequence)
2290 `(%find-position-if-vector-macro predicate ,sequence
2291 from-end start end key)))
2292 (frobs)))
2293 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2294 (macrolet ((frob (sequence from-end)
2295 `(%find-position-if-not predicate ,sequence
2296 ,from-end start end key))
2297 (vector*-frob (sequence)
2298 `(%find-position-if-not-vector-macro predicate ,sequence
2299 from-end start end key)))
2300 (frobs))))
2302 (defun find
2303 (item sequence &rest args &key from-end (start 0) end key test test-not)
2304 (declare (truly-dynamic-extent args))
2305 (seq-dispatch sequence
2306 (nth-value 0 (%find-position
2307 item sequence from-end start end
2308 (effective-find-position-key key)
2309 (effective-find-position-test test test-not)))
2310 (nth-value 0 (%find-position
2311 item sequence from-end start end
2312 (effective-find-position-key key)
2313 (effective-find-position-test test test-not)))
2314 (apply #'sb!sequence:find item sequence args)))
2315 (defun position
2316 (item sequence &rest args &key from-end (start 0) end key test test-not)
2317 (declare (truly-dynamic-extent args))
2318 (seq-dispatch sequence
2319 (nth-value 1 (%find-position
2320 item sequence from-end start end
2321 (effective-find-position-key key)
2322 (effective-find-position-test test test-not)))
2323 (nth-value 1 (%find-position
2324 item sequence from-end start end
2325 (effective-find-position-key key)
2326 (effective-find-position-test test test-not)))
2327 (apply #'sb!sequence:position item sequence args)))
2329 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2330 (declare (truly-dynamic-extent args))
2331 (seq-dispatch sequence
2332 (nth-value 0 (%find-position-if
2333 (%coerce-callable-to-fun predicate)
2334 sequence from-end start end
2335 (effective-find-position-key key)))
2336 (nth-value 0 (%find-position-if
2337 (%coerce-callable-to-fun predicate)
2338 sequence from-end start end
2339 (effective-find-position-key key)))
2340 (apply #'sb!sequence:find-if predicate sequence args)))
2341 (defun position-if
2342 (predicate sequence &rest args &key from-end (start 0) end key)
2343 (declare (truly-dynamic-extent args))
2344 (seq-dispatch sequence
2345 (nth-value 1 (%find-position-if
2346 (%coerce-callable-to-fun predicate)
2347 sequence from-end start end
2348 (effective-find-position-key key)))
2349 (nth-value 1 (%find-position-if
2350 (%coerce-callable-to-fun predicate)
2351 sequence from-end start end
2352 (effective-find-position-key key)))
2353 (apply #'sb!sequence:position-if predicate sequence args)))
2355 (defun find-if-not
2356 (predicate sequence &rest args &key from-end (start 0) end key)
2357 (declare (truly-dynamic-extent args))
2358 (seq-dispatch sequence
2359 (nth-value 0 (%find-position-if-not
2360 (%coerce-callable-to-fun predicate)
2361 sequence from-end start end
2362 (effective-find-position-key key)))
2363 (nth-value 0 (%find-position-if-not
2364 (%coerce-callable-to-fun predicate)
2365 sequence from-end start end
2366 (effective-find-position-key key)))
2367 (apply #'sb!sequence:find-if-not predicate sequence args)))
2368 (defun position-if-not
2369 (predicate sequence &rest args &key from-end (start 0) end key)
2370 (declare (truly-dynamic-extent args))
2371 (seq-dispatch sequence
2372 (nth-value 1 (%find-position-if-not
2373 (%coerce-callable-to-fun predicate)
2374 sequence from-end start end
2375 (effective-find-position-key key)))
2376 (nth-value 1 (%find-position-if-not
2377 (%coerce-callable-to-fun predicate)
2378 sequence from-end start end
2379 (effective-find-position-key key)))
2380 (apply #'sb!sequence:position-if-not predicate sequence args)))
2382 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2384 (eval-when (:compile-toplevel :execute)
2386 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2387 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2388 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2389 `(let ((%start ,(if from-end-p '(1- end) 'start))
2390 (%end ,(if from-end-p '(1- start) 'end)))
2391 (do ((index %start ,next-index)
2392 (count 0))
2393 ((= index (the fixnum %end)) count)
2394 (declare (fixnum index count))
2395 (,(if notp 'unless 'when) ,pred
2396 (setq count (1+ count)))))))
2398 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2399 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2400 `(let ((%start ,(if from-end-p '(- length end) 'start))
2401 (%end ,(if from-end-p '(- length start) 'end))
2402 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2403 (do ((sequence (nthcdr %start ,sequence))
2404 (index %start (1+ index))
2405 (count 0))
2406 ((or (= index (the fixnum %end)) (null sequence)) count)
2407 (declare (fixnum index count))
2408 (,(if notp 'unless 'when) ,pred
2409 (setq count (1+ count)))))))
2412 ) ; EVAL-WHEN
2414 (define-sequence-traverser count-if
2415 (pred sequence &rest args &key from-end start end key)
2416 #!+sb-doc
2417 "Return the number of elements in SEQUENCE satisfying PRED(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 nil t pred sequence)
2426 (list-count-if nil nil pred sequence)))
2427 (let ((end (or end length)))
2428 (declare (type index end))
2429 (if from-end
2430 (vector-count-if nil t pred sequence)
2431 (vector-count-if nil nil pred sequence)))
2432 (apply #'sb!sequence:count-if pred sequence args))))
2434 (define-sequence-traverser count-if-not
2435 (pred sequence &rest args &key from-end start end key)
2436 #!+sb-doc
2437 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2438 (declare (type fixnum start)
2439 (truly-dynamic-extent args))
2440 (let ((pred (%coerce-callable-to-fun pred)))
2441 (seq-dispatch sequence
2442 (let ((end (or end length)))
2443 (declare (type index end))
2444 (if from-end
2445 (list-count-if t t pred sequence)
2446 (list-count-if t nil pred sequence)))
2447 (let ((end (or end length)))
2448 (declare (type index end))
2449 (if from-end
2450 (vector-count-if t t pred sequence)
2451 (vector-count-if t nil pred sequence)))
2452 (apply #'sb!sequence:count-if-not pred sequence args))))
2454 (define-sequence-traverser count
2455 (item sequence &rest args &key from-end start end
2456 key (test #'eql test-p) (test-not nil test-not-p))
2457 #!+sb-doc
2458 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2459 which defaults to EQL."
2460 (declare (type fixnum start)
2461 (truly-dynamic-extent args))
2462 (when (and test-p test-not-p)
2463 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2464 ;; (CLHS 17.2.1)
2465 (error ":TEST and :TEST-NOT are both present."))
2466 (let ((%test (if test-not-p
2467 (lambda (x)
2468 (not (funcall test-not item x)))
2469 (lambda (x)
2470 (funcall test item x)))))
2471 (seq-dispatch sequence
2472 (let ((end (or end length)))
2473 (declare (type index end))
2474 (if from-end
2475 (list-count-if nil t %test sequence)
2476 (list-count-if nil nil %test sequence)))
2477 (let ((end (or end length)))
2478 (declare (type index end))
2479 (if from-end
2480 (vector-count-if nil t %test sequence)
2481 (vector-count-if nil nil %test sequence)))
2482 (apply #'sb!sequence:count item sequence args))))
2484 ;;;; MISMATCH
2486 (eval-when (:compile-toplevel :execute)
2488 (sb!xc:defmacro match-vars (&rest body)
2489 `(let ((inc (if from-end -1 1))
2490 (start1 (if from-end (1- (the fixnum end1)) start1))
2491 (start2 (if from-end (1- (the fixnum end2)) start2))
2492 (end1 (if from-end (1- (the fixnum start1)) end1))
2493 (end2 (if from-end (1- (the fixnum start2)) end2)))
2494 (declare (fixnum inc start1 start2 end1 end2))
2495 ,@body))
2497 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2498 (declare (ignore end)) ;; ### Should END be used below?
2499 `(let ((,sequence (if from-end
2500 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2501 (reverse (the list ,sequence)))
2502 (nthcdr ,start ,sequence))))
2503 (declare (type list ,sequence))
2504 ,@body))
2506 ) ; EVAL-WHEN
2508 (eval-when (:compile-toplevel :execute)
2510 (sb!xc:defmacro if-mismatch (elt1 elt2)
2511 `(cond ((= (the fixnum index1) (the fixnum end1))
2512 (return (if (= (the fixnum index2) (the fixnum end2))
2514 (if from-end
2515 (1+ (the fixnum index1))
2516 (the fixnum index1)))))
2517 ((= (the fixnum index2) (the fixnum end2))
2518 (return (if from-end (1+ (the fixnum index1)) index1)))
2519 (test-not
2520 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2521 (return (if from-end (1+ (the fixnum index1)) index1))))
2522 (t (if (not (funcall test (apply-key key ,elt1)
2523 (apply-key key ,elt2)))
2524 (return (if from-end (1+ (the fixnum index1)) index1))))))
2526 (sb!xc:defmacro mumble-mumble-mismatch ()
2527 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2528 (index2 start2 (+ index2 (the fixnum inc))))
2529 (())
2530 (declare (fixnum index1 index2))
2531 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2533 (sb!xc:defmacro mumble-list-mismatch ()
2534 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2535 (index2 start2 (+ index2 (the fixnum inc))))
2536 (())
2537 (declare (fixnum index1 index2))
2538 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2540 (sb!xc:defmacro list-mumble-mismatch ()
2541 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2542 (index2 start2 (+ index2 (the fixnum inc))))
2543 (())
2544 (declare (fixnum index1 index2))
2545 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2547 (sb!xc:defmacro list-list-mismatch ()
2548 `(do ((sequence1 sequence1)
2549 (sequence2 sequence2)
2550 (index1 start1 (+ index1 (the fixnum inc)))
2551 (index2 start2 (+ index2 (the fixnum inc))))
2552 (())
2553 (declare (fixnum index1 index2))
2554 (if-mismatch (pop sequence1) (pop sequence2))))
2556 ) ; EVAL-WHEN
2558 (define-sequence-traverser mismatch
2559 (sequence1 sequence2 &rest args &key from-end test test-not
2560 start1 end1 start2 end2 key)
2561 #!+sb-doc
2562 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2563 element-wise. If they are of equal length and match in every element, the
2564 result is NIL. Otherwise, the result is a non-negative integer, the index
2565 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2566 if one is shorter than and a matching prefix of the other, the index within
2567 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2568 :FROM-END argument is given, then one plus the index of the rightmost
2569 position in which the sequences differ is returned."
2570 (declare (type fixnum start1 start2))
2571 (declare (truly-dynamic-extent args))
2572 (seq-dispatch sequence1
2573 (seq-dispatch sequence2
2574 (let ((end1 (or end1 length1))
2575 (end2 (or end2 length2)))
2576 (declare (type index end1 end2))
2577 (match-vars
2578 (matchify-list (sequence1 start1 length1 end1)
2579 (matchify-list (sequence2 start2 length2 end2)
2580 (list-list-mismatch)))))
2581 (let ((end1 (or end1 length1))
2582 (end2 (or end2 length2)))
2583 (declare (type index end1 end2))
2584 (match-vars
2585 (matchify-list (sequence1 start1 length1 end1)
2586 (list-mumble-mismatch))))
2587 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2588 (seq-dispatch sequence2
2589 (let ((end1 (or end1 length1))
2590 (end2 (or end2 length2)))
2591 (declare (type index end1 end2))
2592 (match-vars
2593 (matchify-list (sequence2 start2 length2 end2)
2594 (mumble-list-mismatch))))
2595 (let ((end1 (or end1 length1))
2596 (end2 (or end2 length2)))
2597 (declare (type index end1 end2))
2598 (match-vars
2599 (mumble-mumble-mismatch)))
2600 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2601 (apply #'sb!sequence:mismatch sequence1 sequence2 args)))
2604 ;;; search comparison functions
2606 (eval-when (:compile-toplevel :execute)
2608 ;;; Compare two elements and return if they don't match.
2609 (sb!xc:defmacro compare-elements (elt1 elt2)
2610 `(if test-not
2611 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2612 (return nil)
2614 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2615 (return nil)
2616 t)))
2618 (sb!xc:defmacro search-compare-list-list (main sub)
2619 `(do ((main ,main (cdr main))
2620 (jndex start1 (1+ jndex))
2621 (sub (nthcdr start1 ,sub) (cdr sub)))
2622 ((or (endp main) (endp sub) (<= end1 jndex))
2624 (declare (type (integer 0) jndex))
2625 (compare-elements (car sub) (car main))))
2627 (sb!xc:defmacro search-compare-list-vector (main sub)
2628 `(do ((main ,main (cdr main))
2629 (index start1 (1+ index)))
2630 ((or (endp main) (= index end1)) t)
2631 (compare-elements (aref ,sub index) (car main))))
2633 (sb!xc:defmacro search-compare-vector-list (main sub index)
2634 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2635 (jndex start1 (1+ jndex))
2636 (index ,index (1+ index)))
2637 ((or (<= end1 jndex) (endp sub)) t)
2638 (declare (type (integer 0) jndex))
2639 (compare-elements (car sub) (aref ,main index))))
2641 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2642 `(do ((index ,index (1+ index))
2643 (sub-index start1 (1+ sub-index)))
2644 ((= sub-index end1) t)
2645 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2647 (sb!xc:defmacro search-compare (main-type main sub index)
2648 (if (eq main-type 'list)
2649 `(seq-dispatch ,sub
2650 (search-compare-list-list ,main ,sub)
2651 (search-compare-list-vector ,main ,sub)
2652 ;; KLUDGE: just hack it together so that it works
2653 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2654 `(seq-dispatch ,sub
2655 (search-compare-vector-list ,main ,sub ,index)
2656 (search-compare-vector-vector ,main ,sub ,index)
2657 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2659 ) ; EVAL-WHEN
2661 ;;;; SEARCH
2663 (eval-when (:compile-toplevel :execute)
2665 (sb!xc:defmacro list-search (main sub)
2666 `(do ((main (nthcdr start2 ,main) (cdr main))
2667 (index2 start2 (1+ index2))
2668 (terminus (- end2 (the (integer 0) (- end1 start1))))
2669 (last-match ()))
2670 ((> index2 terminus) last-match)
2671 (declare (type (integer 0) index2))
2672 (if (search-compare list main ,sub index2)
2673 (if from-end
2674 (setq last-match index2)
2675 (return index2)))))
2677 (sb!xc:defmacro vector-search (main sub)
2678 `(do ((index2 start2 (1+ index2))
2679 (terminus (- end2 (the (integer 0) (- end1 start1))))
2680 (last-match ()))
2681 ((> index2 terminus) last-match)
2682 (declare (type (integer 0) index2))
2683 (if (search-compare vector ,main ,sub index2)
2684 (if from-end
2685 (setq last-match index2)
2686 (return index2)))))
2688 ) ; EVAL-WHEN
2690 (define-sequence-traverser search
2691 (sequence1 sequence2 &rest args &key
2692 from-end test test-not start1 end1 start2 end2 key)
2693 (declare (type fixnum start1 start2)
2694 (truly-dynamic-extent args))
2695 (seq-dispatch sequence2
2696 (let ((end1 (or end1 length1))
2697 (end2 (or end2 length2)))
2698 (declare (type index end1 end2))
2699 (list-search sequence2 sequence1))
2700 (let ((end1 (or end1 length1))
2701 (end2 (or end2 length2)))
2702 (declare (type index end1 end2))
2703 (vector-search sequence2 sequence1))
2704 (apply #'sb!sequence:search sequence1 sequence2 args)))
2706 ;;; FIXME: this was originally in array.lisp; it might be better to
2707 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2708 ;;; a new early-seq.lisp file.
2709 (defun fill-data-vector (vector dimensions initial-contents)
2710 (declare (explicit-check))
2711 (let ((index 0))
2712 (labels ((frob (axis dims contents)
2713 (cond ((null dims)
2714 (setf (aref vector index) contents)
2715 (incf index))
2717 (unless (typep contents 'sequence)
2718 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2719 sequence, but ~W more layer~:P needed."
2720 contents
2721 (- (length dimensions) axis)))
2722 (unless (= (length contents) (car dims))
2723 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2724 axis ~W is ~W, but ~S is ~W long."
2725 axis (car dims) contents (length contents)))
2726 (sb!sequence:dosequence (content contents)
2727 (frob (1+ axis) (cdr dims) content))))))
2728 (frob 0 dimensions initial-contents))))