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