Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / seq.lisp
blob2cab206dc10e47779c94a662ff569f41472ee644
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 (if iep
405 (make-array length :element-type etype
406 :initial-element initial-element)
407 (make-array length :element-type etype)))))
408 (t (sequence-type-too-hairy (type-specifier type)))))
409 ((and (csubtypep type (specifier-type 'sequence))
410 (awhen (find-class adjusted-type nil)
411 (let ((prototype (sb!mop:class-prototype
412 (sb!pcl:ensure-class-finalized it))))
413 (if iep
414 (sb!sequence:make-sequence-like
415 prototype length :initial-element initial-element)
416 (sb!sequence:make-sequence-like
417 prototype length))))))
418 (t (bad-sequence-type-error (type-specifier type))))))
420 ;;;; SUBSEQ
421 ;;;;
423 (define-array-dispatch vector-subseq-dispatch (array start end)
424 (declare (optimize speed (safety 0)))
425 (declare (type index start end))
426 (subseq array start end))
428 ;;;; The support routines for SUBSEQ are used by compiler transforms,
429 ;;;; so we worry about dealing with END being supplied or defaulting
430 ;;;; to NIL at this level.
432 (defun vector-subseq* (sequence start end)
433 (declare (type vector sequence))
434 (declare (type index start)
435 (type (or null index) end)
436 (optimize speed))
437 (with-array-data ((data sequence)
438 (start start)
439 (end end)
440 :check-fill-pointer t
441 :force-inline t)
442 (vector-subseq-dispatch data start end)))
444 (defun list-subseq* (sequence start end)
445 (declare (type list sequence)
446 (type unsigned-byte start)
447 (type (or null unsigned-byte) end))
448 (flet ((oops ()
449 (sequence-bounding-indices-bad-error sequence start end)))
450 (let ((pointer sequence))
451 (unless (zerop start)
452 ;; If START > 0 the list cannot be empty. So CDR down to
453 ;; it START-1 times, check that we still have something, then
454 ;; CDR the final time.
456 ;; If START was zero, the list may be empty if END is NIL or
457 ;; also zero.
458 (when (> start 1)
459 (setf pointer (nthcdr (1- start) pointer)))
460 (if pointer
461 (pop pointer)
462 (oops)))
463 (if end
464 (let ((n (- end start)))
465 (declare (integer n))
466 (when (minusp n)
467 (oops))
468 (when (plusp n)
469 (let* ((head (list nil))
470 (tail head))
471 (macrolet ((pop-one ()
472 `(let ((tmp (list (pop pointer))))
473 (setf (cdr tail) tmp
474 tail tmp))))
475 ;; Bignum case
476 (loop until (fixnump n)
477 do (pop-one)
478 (decf n))
479 ;; Fixnum case, but leave last element, so we should
480 ;; still have something left in the sequence.
481 (let ((m (1- n)))
482 (declare (fixnum m))
483 (loop repeat m
484 do (pop-one)))
485 (unless pointer
486 (oops))
487 ;; OK, pop the last one.
488 (pop-one)
489 (cdr head)))))
490 (loop while pointer
491 collect (pop pointer))))))
493 (defun subseq (sequence start &optional end)
494 #!+sb-doc
495 "Return a copy of a subsequence of SEQUENCE starting with element number
496 START and continuing to the end of SEQUENCE or the optional END."
497 (seq-dispatch sequence
498 (list-subseq* sequence start end)
499 (vector-subseq* sequence start end)
500 (sb!sequence:subseq sequence start end)))
502 ;;;; COPY-SEQ
504 (defun copy-seq (sequence)
505 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
506 (seq-dispatch sequence
507 (list-copy-seq* sequence)
508 (vector-subseq* sequence 0 nil)
509 (sb!sequence:copy-seq sequence)))
511 (defun list-copy-seq* (sequence)
512 (!copy-list-macro sequence :check-proper-list t))
514 ;;;; FILL
516 (defun list-fill* (sequence item start end)
517 (declare (type list sequence)
518 (type unsigned-byte start)
519 (type (or null unsigned-byte) end))
520 (flet ((oops ()
521 (sequence-bounding-indices-bad-error sequence start end)))
522 (let ((pointer sequence))
523 (unless (zerop start)
524 ;; If START > 0 the list cannot be empty. So CDR down to it
525 ;; START-1 times, check that we still have something, then CDR
526 ;; the final time.
528 ;; If START was zero, the list may be empty if END is NIL or
529 ;; also zero.
530 (unless (= start 1)
531 (setf pointer (nthcdr (1- start) pointer)))
532 (if pointer
533 (pop pointer)
534 (oops)))
535 (if end
536 (let ((n (- end start)))
537 (declare (integer n))
538 (when (minusp n)
539 (oops))
540 (when (plusp n)
541 (loop repeat n
542 do (setf pointer (cdr (rplaca pointer item))))))
543 (loop while pointer
544 do (setf pointer (cdr (rplaca pointer item)))))))
545 sequence)
547 (defun vector-fill* (sequence item start end)
548 (with-array-data ((data sequence)
549 (start start)
550 (end end)
551 :force-inline t
552 :check-fill-pointer t)
553 (let ((setter (!find-data-vector-setter data)))
554 (declare (optimize (speed 3) (safety 0)))
555 (do ((index start (1+ index)))
556 ((= index end) sequence)
557 (declare (index index))
558 (funcall setter data index item)))))
560 (defun string-fill* (sequence item start end)
561 (declare (string sequence))
562 (with-array-data ((data sequence)
563 (start start)
564 (end end)
565 :force-inline t
566 :check-fill-pointer t)
567 ;; DEFTRANSFORM for FILL will turn these into
568 ;; calls to UB*-BASH-FILL.
569 (etypecase data
570 #!+sb-unicode
571 ((simple-array character (*))
572 (let ((item (locally (declare (optimize (safety 3)))
573 (the character item))))
574 (fill data item :start start :end end)))
575 ((simple-array base-char (*))
576 (let ((item (locally (declare (optimize (safety 3)))
577 (the base-char item))))
578 (fill data item :start start :end end))))))
580 (defun fill (sequence item &key (start 0) end)
581 #!+sb-doc
582 "Replace the specified elements of SEQUENCE with ITEM."
583 (seq-dispatch sequence
584 (list-fill* sequence item start end)
585 (vector-fill* sequence item start end)
586 (sb!sequence:fill sequence item
587 :start start
588 :end (%check-generic-sequence-bounds sequence start end))))
590 ;;;; REPLACE
592 (eval-when (:compile-toplevel :execute)
594 ;;; If we are copying around in the same vector, be careful not to copy the
595 ;;; same elements over repeatedly. We do this by copying backwards.
596 (sb!xc:defmacro mumble-replace-from-mumble ()
597 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
598 (let ((nelts (min (- target-end target-start)
599 (- source-end source-start))))
600 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
601 (1- target-index))
602 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
603 (1- source-index)))
604 ((= target-index (the fixnum (1- target-start))) target-sequence)
605 (declare (fixnum target-index source-index))
606 ;; disable bounds checking
607 (declare (optimize (safety 0)))
608 (setf (aref target-sequence target-index)
609 (aref source-sequence source-index))))
610 (do ((target-index target-start (1+ target-index))
611 (source-index source-start (1+ source-index)))
612 ((or (= target-index (the fixnum target-end))
613 (= source-index (the fixnum source-end)))
614 target-sequence)
615 (declare (fixnum target-index source-index))
616 ;; disable bounds checking
617 (declare (optimize (safety 0)))
618 (setf (aref target-sequence target-index)
619 (aref source-sequence source-index)))))
621 (sb!xc:defmacro list-replace-from-list ()
622 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
623 (let ((new-elts (subseq source-sequence source-start
624 (+ (the fixnum source-start)
625 (the fixnum
626 (min (- (the fixnum target-end)
627 (the fixnum target-start))
628 (- (the fixnum source-end)
629 (the fixnum source-start))))))))
630 (do ((n new-elts (cdr n))
631 (o (nthcdr target-start target-sequence) (cdr o)))
632 ((null n) target-sequence)
633 (rplaca o (car n))))
634 (do ((target-index target-start (1+ target-index))
635 (source-index source-start (1+ source-index))
636 (target-sequence-ref (nthcdr target-start target-sequence)
637 (cdr target-sequence-ref))
638 (source-sequence-ref (nthcdr source-start source-sequence)
639 (cdr source-sequence-ref)))
640 ((or (= target-index (the fixnum target-end))
641 (= source-index (the fixnum source-end))
642 (null target-sequence-ref) (null source-sequence-ref))
643 target-sequence)
644 (declare (fixnum target-index source-index))
645 (rplaca target-sequence-ref (car source-sequence-ref)))))
647 (sb!xc:defmacro list-replace-from-mumble ()
648 `(do ((target-index target-start (1+ target-index))
649 (source-index source-start (1+ source-index))
650 (target-sequence-ref (nthcdr target-start target-sequence)
651 (cdr target-sequence-ref)))
652 ((or (= target-index (the fixnum target-end))
653 (= source-index (the fixnum source-end))
654 (null target-sequence-ref))
655 target-sequence)
656 (declare (fixnum source-index target-index))
657 (rplaca target-sequence-ref (aref source-sequence source-index))))
659 (sb!xc:defmacro mumble-replace-from-list ()
660 `(do ((target-index target-start (1+ target-index))
661 (source-index source-start (1+ source-index))
662 (source-sequence (nthcdr source-start source-sequence)
663 (cdr source-sequence)))
664 ((or (= target-index (the fixnum target-end))
665 (= source-index (the fixnum source-end))
666 (null source-sequence))
667 target-sequence)
668 (declare (fixnum target-index source-index))
669 (setf (aref target-sequence target-index) (car source-sequence))))
671 ) ; EVAL-WHEN
673 ;;;; The support routines for REPLACE are used by compiler transforms, so we
674 ;;;; worry about dealing with END being supplied or defaulting to NIL
675 ;;;; at this level.
677 (defun list-replace-from-list* (target-sequence source-sequence target-start
678 target-end source-start source-end)
679 (when (null target-end) (setq target-end (length target-sequence)))
680 (when (null source-end) (setq source-end (length source-sequence)))
681 (list-replace-from-list))
683 (defun list-replace-from-vector* (target-sequence source-sequence target-start
684 target-end source-start source-end)
685 (when (null target-end) (setq target-end (length target-sequence)))
686 (when (null source-end) (setq source-end (length source-sequence)))
687 (list-replace-from-mumble))
689 (defun vector-replace-from-list* (target-sequence source-sequence target-start
690 target-end source-start source-end)
691 (when (null target-end) (setq target-end (length target-sequence)))
692 (when (null source-end) (setq source-end (length source-sequence)))
693 (mumble-replace-from-list))
695 (defun vector-replace-from-vector* (target-sequence source-sequence
696 target-start target-end source-start
697 source-end)
698 (when (null target-end) (setq target-end (length target-sequence)))
699 (when (null source-end) (setq source-end (length source-sequence)))
700 (mumble-replace-from-mumble))
702 #!+sb-unicode
703 (defun simple-character-string-replace-from-simple-character-string*
704 (target-sequence source-sequence
705 target-start target-end source-start source-end)
706 (declare (type (simple-array character (*)) target-sequence source-sequence))
707 (when (null target-end) (setq target-end (length target-sequence)))
708 (when (null source-end) (setq source-end (length source-sequence)))
709 (mumble-replace-from-mumble))
711 (define-sequence-traverser replace
712 (sequence1 sequence2 &rest args &key start1 end1 start2 end2)
713 #!+sb-doc
714 "Destructively modifies SEQUENCE1 by copying successive elements
715 into it from the SEQUENCE2.
717 Elements are copied to the subseqeuence bounded by START1 and END1,
718 from the subsequence bounded by START2 and END2. If these subsequences
719 are not of the same length, then the shorter length determines how
720 many elements are copied."
721 (declare (truly-dynamic-extent args))
722 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
723 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
724 ;; these things here so that legacy code gets the names it's
725 ;; expecting. We could use &AUX instead :-/.
726 (target-sequence sequence1)
727 (source-sequence sequence2)
728 (target-start start1)
729 (source-start start2)
730 (target-end (or end1 length1))
731 (source-end (or end2 length2)))
732 (seq-dispatch target-sequence
733 (seq-dispatch source-sequence
734 (list-replace-from-list)
735 (list-replace-from-mumble)
736 (apply #'sb!sequence:replace sequence1 sequence2 args))
737 (seq-dispatch source-sequence
738 (mumble-replace-from-list)
739 (mumble-replace-from-mumble)
740 (apply #'sb!sequence:replace sequence1 sequence2 args))
741 (apply #'sb!sequence:replace sequence1 sequence2 args))))
743 ;;;; REVERSE
745 (eval-when (:compile-toplevel :execute)
747 (sb!xc:defmacro vector-reverse (sequence)
748 `(let ((length (length ,sequence)))
749 (declare (fixnum length))
750 (do ((forward-index 0 (1+ forward-index))
751 (backward-index (1- length) (1- backward-index))
752 (new-sequence (%make-sequence-like sequence length)))
753 ((= forward-index length) new-sequence)
754 (declare (fixnum forward-index backward-index))
755 (setf (aref new-sequence forward-index)
756 (aref ,sequence backward-index)))))
758 (sb!xc:defmacro list-reverse-macro (sequence)
759 `(do ((new-list ()))
760 ((endp ,sequence) new-list)
761 (push (pop ,sequence) new-list)))
763 ) ; EVAL-WHEN
765 (defun reverse (sequence)
766 #!+sb-doc
767 "Return a new sequence containing the same elements but in reverse order."
768 (seq-dispatch-checking sequence
769 (list-reverse* sequence)
770 (vector-reverse* sequence)
771 ;; The type deriver says that LIST => LIST and VECTOR => VECTOR
772 ;; but does not claim to know anything about extended-sequences.
773 ;; So this could theoretically return any subtype of SEQUENCE
774 ;; given an EXTENDED-SEQUENCE as input. But fndb says this returns
775 ;; a CONSED-SEQUENCE, which precludes non-simple vectors.
776 ;; But a CLOS sequence can apparently decide to return a LIST when
777 ;; reversed. [Is that too weird? Make this EXTENDED-SEQUENCE maybe?]
778 (the consed-sequence (values (sb!sequence:reverse sequence)))))
780 ;;; internal frobs
782 (defun list-reverse* (sequence)
783 (list-reverse-macro sequence))
785 (defun vector-reverse* (sequence)
786 (vector-reverse sequence))
788 ;;;; NREVERSE
790 (eval-when (:compile-toplevel :execute)
792 (sb!xc:defmacro vector-nreverse (sequence)
793 `(let ((length (length (the vector ,sequence))))
794 (when (>= length 2)
795 (do ((left-index 0 (1+ left-index))
796 (right-index (1- length) (1- right-index)))
797 ((<= right-index left-index))
798 (declare (type index left-index right-index))
799 (rotatef (aref ,sequence left-index)
800 (aref ,sequence right-index))))
801 ,sequence))
803 (sb!xc:defmacro list-nreverse-macro (list)
804 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
805 (2nd ,list 1st)
806 (3rd '() 2nd))
807 ((atom 2nd) 3rd)
808 (rplacd 2nd 3rd)))
810 ) ; EVAL-WHEN
812 (defun list-nreverse* (sequence)
813 (list-nreverse-macro sequence))
815 (defun vector-nreverse* (sequence)
816 (vector-nreverse sequence))
818 (defun nreverse (sequence)
819 #!+sb-doc
820 "Return a sequence of the same elements in reverse order; the argument
821 is destroyed."
822 (seq-dispatch-checking sequence
823 (list-nreverse* sequence)
824 (vector-nreverse* sequence)
825 ;; The type deriver for this is 'result-type-first-arg',
826 ;; meaning it should return definitely an EXTENDED-SEQUENCE
827 ;; and not a list or vector.
828 (the extended-sequence (values (sb!sequence:nreverse sequence)))))
830 ;;;; CONCATENATE
832 (defmacro sb!sequence:dosequence ((element sequence &optional return) &body body)
833 #!+sb-doc
834 "Executes BODY with ELEMENT subsequently bound to each element of
835 SEQUENCE, then returns RETURN."
836 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
837 (once-only ((sequence sequence))
838 (with-unique-names (state limit from-end step endp elt)
839 `(block nil
840 (seq-dispatch ,sequence
841 (dolist (,element ,sequence ,return) ,@body)
842 (do-vector-data (,element ,sequence ,return) ,@body)
843 (multiple-value-bind (,state ,limit ,from-end ,step ,endp ,elt)
844 (sb!sequence:make-sequence-iterator ,sequence)
845 (do ((,state ,state (funcall ,step ,sequence ,state ,from-end)))
846 ((funcall ,endp ,sequence ,state ,limit ,from-end)
847 (let ((,element nil))
848 ,@(filter-dolist-declarations decls)
849 (declare (ignorable ,element))
850 ,return))
851 (let ((,element (funcall ,elt ,sequence ,state)))
852 ,@decls
853 (tagbody
854 ,@forms))))))))))
857 (defun concatenate (output-type-spec &rest sequences)
858 #!+sb-doc
859 "Return a new sequence of all the argument sequences concatenated together
860 which shares no structure with the original argument sequences of the
861 specified OUTPUT-TYPE-SPEC."
862 (flet ((concat-to-list* (sequences)
863 (let ((result (list nil)))
864 (do ((sequences sequences (cdr sequences))
865 (splice result))
866 ((null sequences) (cdr result))
867 (let ((sequence (car sequences)))
868 (sb!sequence:dosequence (e sequence)
869 (setq splice (cdr (rplacd splice (list e)))))))))
870 (concat-to-simple* (type-spec sequences)
871 (do ((seqs sequences (cdr seqs))
872 (total-length 0)
873 (lengths ()))
874 ((null seqs)
875 (do ((sequences sequences (cdr sequences))
876 (lengths lengths (cdr lengths))
877 (index 0)
878 (result (make-sequence type-spec total-length)))
879 ((= index total-length) result)
880 (declare (fixnum index))
881 (let ((sequence (car sequences)))
882 (sb!sequence:dosequence (e sequence)
883 (setf (aref result index) e)
884 (incf index)))))
885 (let ((length (length (car seqs))))
886 (declare (fixnum length))
887 (setq lengths (nconc lengths (list length)))
888 (setq total-length (+ total-length length))))))
889 (let ((type (specifier-type output-type-spec)))
890 (cond
891 ((csubtypep type (specifier-type 'list))
892 (cond
893 ((type= type (specifier-type 'list))
894 (concat-to-list* sequences))
895 ((eq type *empty-type*)
896 (bad-sequence-type-error nil))
897 ((type= type (specifier-type 'null))
898 (unless (every #'emptyp sequences)
899 (sequence-type-length-mismatch-error
900 type (reduce #'+ sequences :key #'length))) ; FIXME: circular list issues.
901 '())
902 ((cons-type-p type)
903 (multiple-value-bind (min exactp)
904 (sb!kernel::cons-type-length-info type)
905 (let ((length (reduce #'+ sequences :key #'length)))
906 (if exactp
907 (unless (= length min)
908 (sequence-type-length-mismatch-error type length))
909 (unless (>= length min)
910 (sequence-type-length-mismatch-error type length)))
911 (concat-to-list* sequences))))
912 (t (sequence-type-too-hairy (type-specifier type)))))
913 ((csubtypep type (specifier-type 'vector))
914 (concat-to-simple* output-type-spec sequences))
915 ((and (csubtypep type (specifier-type 'sequence))
916 (awhen (find-class output-type-spec nil)
917 (apply #'sb!sequence:concatenate
918 (sb!mop:class-prototype
919 (sb!pcl:ensure-class-finalized it))
920 sequences))))
922 (bad-sequence-type-error output-type-spec))))))
924 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
925 ;;; CONCATENATE 'STRING &co into these.
926 (macrolet ((def (name element-type)
927 `(defun ,name (&rest sequences)
928 (declare (dynamic-extent sequences)
929 (optimize speed)
930 (optimize (sb!c::insert-array-bounds-checks 0)))
931 (let* ((lengths (mapcar #'length sequences))
932 (result (make-array (the integer (apply #'+ lengths))
933 :element-type ',element-type))
934 (start 0))
935 (declare (index start))
936 (dolist (seq sequences)
937 (string-dispatch
938 ((simple-array character (*))
939 (simple-array base-char (*))
942 (replace result seq :start1 start))
943 (incf start (the index (pop lengths))))
944 result))))
945 (def %concatenate-to-string character)
946 (def %concatenate-to-base-string base-char))
949 ;;;; MAP
951 ;;; helper functions to handle arity-1 subcases of MAP
952 (defun %map-to-list-arity-1 (fun sequence)
953 (let ((reversed-result nil)
954 (really-fun (%coerce-callable-to-fun fun)))
955 (sb!sequence:dosequence (element sequence)
956 (push (funcall really-fun element)
957 reversed-result))
958 (nreverse reversed-result)))
959 (defun %map-to-simple-vector-arity-1 (fun sequence)
960 (let ((result (make-array (length sequence)))
961 (index 0)
962 (really-fun (%coerce-callable-to-fun fun)))
963 (declare (type index index))
964 (sb!sequence:dosequence (element sequence)
965 (setf (aref result index)
966 (funcall really-fun element))
967 (incf index))
968 result))
969 (defun %map-for-effect-arity-1 (fun sequence)
970 (let ((really-fun (%coerce-callable-to-fun fun)))
971 (sb!sequence:dosequence (element sequence)
972 (funcall really-fun element)))
973 nil)
975 (declaim (maybe-inline %map-for-effect))
976 (defun %map-for-effect (fun sequences)
977 (declare (type function fun) (type list sequences))
978 (let ((%sequences sequences)
979 (%iters (mapcar (lambda (s)
980 (seq-dispatch s
983 (multiple-value-list
984 (sb!sequence:make-sequence-iterator s))))
985 sequences))
986 (%apply-args (make-list (length sequences))))
987 ;; this is almost efficient (except in the general case where we
988 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
989 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
990 (declare (type list %sequences %iters %apply-args))
991 (loop
992 (do ((in-sequences %sequences (cdr in-sequences))
993 (in-iters %iters (cdr in-iters))
994 (in-apply-args %apply-args (cdr in-apply-args)))
995 ((null in-sequences) (apply fun %apply-args))
996 (let ((i (car in-iters)))
997 (declare (type (or list index) i))
998 (cond
999 ((listp (car in-sequences))
1000 (if (null i)
1001 (return-from %map-for-effect nil)
1002 (setf (car in-apply-args) (car i)
1003 (car in-iters) (cdr i))))
1004 ((typep i 'index)
1005 (let ((v (the vector (car in-sequences))))
1006 (if (>= i (length v))
1007 (return-from %map-for-effect nil)
1008 (setf (car in-apply-args) (aref v i)
1009 (car in-iters) (1+ i)))))
1011 (destructuring-bind (state limit from-end step endp elt &rest ignore)
1013 (declare (type function step endp elt)
1014 (ignore ignore))
1015 (let ((s (car in-sequences)))
1016 (if (funcall endp s state limit from-end)
1017 (return-from %map-for-effect nil)
1018 (progn
1019 (setf (car in-apply-args) (funcall elt s state))
1020 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
1021 (defun %map-to-list (fun sequences)
1022 (declare (type function fun)
1023 (type list sequences))
1024 (let ((result nil))
1025 (flet ((f (&rest args)
1026 (declare (truly-dynamic-extent args))
1027 (push (apply fun args) result)))
1028 (declare (truly-dynamic-extent #'f))
1029 (%map-for-effect #'f sequences))
1030 (nreverse result)))
1031 (defun %map-to-vector (output-type-spec fun sequences)
1032 (declare (type function fun)
1033 (type list sequences))
1034 (let ((min-len 0))
1035 (flet ((f (&rest args)
1036 (declare (truly-dynamic-extent args))
1037 (declare (ignore args))
1038 (incf min-len)))
1039 (declare (truly-dynamic-extent #'f))
1040 (%map-for-effect #'f sequences))
1041 (let ((result (make-sequence output-type-spec min-len))
1042 (i 0))
1043 (declare (type (simple-array * (*)) result))
1044 (flet ((f (&rest args)
1045 (declare (truly-dynamic-extent args))
1046 (setf (aref result i) (apply fun args))
1047 (incf i)))
1048 (declare (truly-dynamic-extent #'f))
1049 (%map-for-effect #'f sequences))
1050 result)))
1052 ;;; %MAP is just MAP without the final just-to-be-sure check that
1053 ;;; length of the output sequence matches any length specified
1054 ;;; in RESULT-TYPE.
1055 (defun %map (result-type function first-sequence &rest more-sequences)
1056 (labels ((slower-map (type)
1057 (let ((really-fun (%coerce-callable-to-fun function))
1058 (sequences (cons first-sequence more-sequences)))
1059 (cond
1060 ((eq type *empty-type*)
1061 (%map-for-effect really-fun sequences))
1062 ((csubtypep type (specifier-type 'list))
1063 (%map-to-list really-fun sequences))
1064 ((csubtypep type (specifier-type 'vector))
1065 (%map-to-vector result-type really-fun sequences))
1066 ((and (csubtypep type (specifier-type 'sequence))
1067 (awhen (find-class result-type nil)
1068 (apply #'sb!sequence:map
1069 (sb!mop:class-prototype
1070 (sb!pcl:ensure-class-finalized it))
1071 really-fun sequences))))
1073 (bad-sequence-type-error result-type)))))
1074 (slow-map ()
1075 (let ((type (specifier-type result-type)))
1076 (cond
1077 (more-sequences
1078 (slower-map type))
1079 ((eq type *empty-type*)
1080 (%map-for-effect-arity-1 function first-sequence))
1081 ((csubtypep type (specifier-type 'list))
1082 (%map-to-list-arity-1 function first-sequence))
1083 ((or (csubtypep type (specifier-type 'simple-vector))
1084 (csubtypep type (specifier-type '(vector t))))
1085 (%map-to-simple-vector-arity-1 function first-sequence))
1087 (slower-map type))))))
1088 ;; Handle some easy cases faster
1089 (cond (more-sequences
1090 (slow-map))
1091 ((null result-type)
1092 (%map-for-effect-arity-1 function first-sequence))
1093 ((or (eq result-type 'list)
1094 (eq result-type 'cons))
1095 (%map-to-list-arity-1 function first-sequence))
1096 ((or (eq result-type 'vector)
1097 (eq result-type 'simple-vector))
1098 (%map-to-simple-vector-arity-1 function first-sequence))
1100 (slow-map)))))
1102 (defun map (result-type function first-sequence &rest more-sequences)
1103 (apply #'%map
1104 result-type
1105 function
1106 first-sequence
1107 more-sequences))
1109 ;;;; MAP-INTO
1111 (defmacro map-into-lambda (sequences params &body body)
1112 (check-type sequences symbol)
1113 `(flet ((f ,params ,@body))
1114 (declare (truly-dynamic-extent #'f))
1115 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1116 ;; hence the awkward flip between MAP and LOOP.
1117 (if ,sequences
1118 (apply #'map nil #'f ,sequences)
1119 (loop (f)))))
1121 (define-array-dispatch vector-map-into (data start end fun sequences)
1122 (declare (type index start end)
1123 (type function fun)
1124 (type list sequences))
1125 (let ((index start))
1126 (declare (type index index))
1127 (block mapping
1128 (map-into-lambda sequences (&rest args)
1129 (declare (truly-dynamic-extent args))
1130 (when (eql index end)
1131 (return-from mapping))
1132 (setf (aref data index) (apply fun args))
1133 (incf index)))
1134 index))
1136 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1137 ;;; computing the length of the result sequence since we can detect
1138 ;;; the end during mapping (if MAP even gets that far).
1140 ;;; For each result type, define a mapping function which is
1141 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1142 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1143 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1145 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1146 ;;; Because we are manually doing bounds checking with known types,
1147 ;;; safety is turned off for vectors and lists but kept for generic
1148 ;;; sequences.
1149 (defun map-into (result-sequence function &rest sequences)
1150 (let ((really-fun (%coerce-callable-to-fun function)))
1151 (etypecase result-sequence
1152 (vector
1153 (with-array-data ((data result-sequence) (start) (end)
1154 ;; MAP-INTO ignores fill pointer when mapping
1155 :check-fill-pointer nil)
1156 (let ((new-end (vector-map-into data start end really-fun sequences)))
1157 (when (array-has-fill-pointer-p result-sequence)
1158 (setf (fill-pointer result-sequence) (- new-end start))))))
1159 (list
1160 (let ((node result-sequence))
1161 (declare (type list node))
1162 (map-into-lambda sequences (&rest args)
1163 (declare (truly-dynamic-extent args))
1164 (cond ((null node)
1165 (return-from map-into result-sequence))
1166 ((not (listp (cdr node)))
1167 (error 'simple-type-error
1168 :format-control "~a is not a proper list"
1169 :format-arguments (list result-sequence)
1170 :expected-type 'list
1171 :datum result-sequence)))
1172 (setf (car node) (apply really-fun args))
1173 (setf node (cdr node)))))
1174 (sequence
1175 (multiple-value-bind (iter limit from-end)
1176 (sb!sequence:make-sequence-iterator result-sequence)
1177 (map-into-lambda sequences (&rest args)
1178 (declare (truly-dynamic-extent args) (optimize speed))
1179 (when (sb!sequence:iterator-endp result-sequence
1180 iter limit from-end)
1181 (return-from map-into result-sequence))
1182 (setf (sb!sequence:iterator-element result-sequence iter)
1183 (apply really-fun args))
1184 (setf iter (sb!sequence:iterator-step result-sequence
1185 iter from-end)))))))
1186 result-sequence)
1188 ;;;; quantifiers
1190 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
1191 ;;; arbitrary sequence arguments, both in the full call case and in
1192 ;;; the open code case.
1193 (macrolet ((defquantifier (name found-test found-result
1194 &key doc (unfound-result (not found-result)))
1195 `(progn
1196 ;; KLUDGE: It would be really nice if we could simply
1197 ;; do something like this
1198 ;; (declaim (inline ,name))
1199 ;; (defun ,name (pred first-seq &rest more-seqs)
1200 ;; ,doc
1201 ;; (flet ((map-me (&rest rest)
1202 ;; (let ((pred-value (apply pred rest)))
1203 ;; (,found-test pred-value
1204 ;; (return-from ,name
1205 ;; ,found-result)))))
1206 ;; (declare (inline map-me))
1207 ;; (apply #'map nil #'map-me first-seq more-seqs)
1208 ;; ,unfound-result))
1209 ;; but Python doesn't seem to be smart enough about
1210 ;; inlining and APPLY to recognize that it can use
1211 ;; the DEFTRANSFORM for MAP in the resulting inline
1212 ;; expansion. I don't have any appetite for deep
1213 ;; compiler hacking right now, so I'll just work
1214 ;; around the apparent problem by using a compiler
1215 ;; macro instead. -- WHN 20000410
1216 (defun ,name (pred first-seq &rest more-seqs)
1217 #!+sb-doc ,doc
1218 (flet ((map-me (&rest rest)
1219 (let ((pred-value (apply pred rest)))
1220 (,found-test pred-value
1221 (return-from ,name
1222 ,found-result)))))
1223 (declare (inline map-me))
1224 (apply #'map nil #'map-me first-seq more-seqs)
1225 ,unfound-result))
1226 ;; KLUDGE: It would be more obviously correct -- but
1227 ;; also significantly messier -- for PRED-VALUE to be
1228 ;; a gensym. However, a private symbol really does
1229 ;; seem to be good enough; and anyway the really
1230 ;; obviously correct solution is to make Python smart
1231 ;; enough that we can use an inline function instead
1232 ;; of a compiler macro (as above). -- WHN 20000410
1234 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
1235 ;; important for performance, and it'd be good to have
1236 ;; it be visible throughout the compilation of all the
1237 ;; target SBCL code. That could be done by defining
1238 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
1239 ;; moving this DEFQUANTIFIER stuff (and perhaps other
1240 ;; inline definitions in seq.lisp as well) into a new
1241 ;; seq.lisp, and moving remaining target-only stuff
1242 ;; from the old seq.lisp into target-seq.lisp.
1243 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
1244 (binding* ((elements
1245 (make-gensym-list (1+ (length more-seqs))))
1246 (blockname (sb!xc:gensym "BLOCK"))
1247 (wrapper (sb!xc:gensym "WRAPPER"))
1248 ((bind call)
1249 (funarg-bind/call-forms pred elements)))
1250 `(let ,bind
1251 (block ,blockname
1252 (flet ((,wrapper (,@elements)
1253 (declare (optimize (sb!c::check-tag-existence 0)))
1254 (let ((pred-value ,call))
1255 (,',found-test pred-value
1256 (return-from ,blockname ,',found-result)))))
1257 (declare (inline ,wrapper)
1258 (dynamic-extent #',wrapper))
1259 (map nil #',wrapper ,first-seq
1260 ,@more-seqs))
1261 ,',unfound-result)))))))
1262 (defquantifier some when pred-value :unfound-result nil :doc
1263 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1264 possibly to those with index 1, and so on. Return the first
1265 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1266 (defquantifier every unless nil :doc
1267 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1268 possibly to those with index 1, and so on. Return NIL as soon
1269 as any invocation of PREDICATE returns NIL, or T if every invocation
1270 is non-NIL.")
1271 (defquantifier notany when nil :doc
1272 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1273 possibly to those with index 1, and so on. Return NIL as soon
1274 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1275 of any sequence is reached.")
1276 (defquantifier notevery unless t :doc
1277 "Apply PREDICATE to 0-indexed elements of the sequences, then
1278 possibly to those with index 1, and so on. Return T as soon
1279 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1280 is non-NIL."))
1282 ;;;; REDUCE
1284 (eval-when (:compile-toplevel :execute)
1286 (sb!xc:defmacro mumble-reduce (function
1287 sequence
1289 start
1291 initial-value
1292 ref)
1293 `(do ((index ,start (1+ index))
1294 (value ,initial-value))
1295 ((>= index ,end) value)
1296 (setq value (funcall ,function value
1297 (apply-key ,key (,ref ,sequence index))))))
1299 (sb!xc:defmacro mumble-reduce-from-end (function
1300 sequence
1302 start
1304 initial-value
1305 ref)
1306 `(do ((index (1- ,end) (1- index))
1307 (value ,initial-value)
1308 (terminus (1- ,start)))
1309 ((<= index terminus) value)
1310 (setq value (funcall ,function
1311 (apply-key ,key (,ref ,sequence index))
1312 value))))
1314 (sb!xc:defmacro list-reduce (function
1315 sequence
1317 start
1319 initial-value
1320 ivp)
1321 `(let ((sequence (nthcdr ,start ,sequence)))
1322 (do ((count (if ,ivp ,start (1+ ,start))
1323 (1+ count))
1324 (sequence (if ,ivp sequence (cdr sequence))
1325 (cdr sequence))
1326 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1327 (funcall ,function value (apply-key ,key (car sequence)))))
1328 ((>= count ,end) value))))
1330 (sb!xc:defmacro list-reduce-from-end (function
1331 sequence
1333 start
1335 initial-value
1336 ivp)
1337 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1338 (reverse ,sequence))))
1339 (do ((count (if ,ivp ,start (1+ ,start))
1340 (1+ count))
1341 (sequence (if ,ivp sequence (cdr sequence))
1342 (cdr sequence))
1343 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1344 (funcall ,function (apply-key ,key (car sequence)) value)))
1345 ((>= count ,end) value))))
1347 ) ; EVAL-WHEN
1349 (define-sequence-traverser reduce (function sequence &rest args &key key
1350 from-end start end (initial-value nil ivp))
1351 (declare (type index start)
1352 (truly-dynamic-extent args))
1353 (seq-dispatch sequence
1354 (let ((end (or end length)))
1355 (declare (type index end))
1356 (if (= end start)
1357 (if ivp initial-value (funcall function))
1358 (if from-end
1359 (list-reduce-from-end function sequence key start end
1360 initial-value ivp)
1361 (list-reduce function sequence key start end
1362 initial-value ivp))))
1363 (let ((end (or end length)))
1364 (declare (type index end))
1365 (if (= end start)
1366 (if ivp initial-value (funcall function))
1367 (if from-end
1368 (progn
1369 (when (not ivp)
1370 (setq end (1- (the fixnum end)))
1371 (setq initial-value (apply-key key (aref sequence end))))
1372 (mumble-reduce-from-end function sequence key start end
1373 initial-value aref))
1374 (progn
1375 (when (not ivp)
1376 (setq initial-value (apply-key key (aref sequence start)))
1377 (setq start (1+ start)))
1378 (mumble-reduce function sequence key start end
1379 initial-value aref)))))
1380 (apply #'sb!sequence:reduce function sequence args)))
1382 ;;;; DELETE
1384 (eval-when (:compile-toplevel :execute)
1386 (sb!xc:defmacro mumble-delete (pred)
1387 `(do ((index start (1+ index))
1388 (jndex start)
1389 (number-zapped 0))
1390 ((or (= index (the fixnum end)) (= number-zapped count))
1391 (do ((index index (1+ index)) ; Copy the rest of the vector.
1392 (jndex jndex (1+ jndex)))
1393 ((= index (the fixnum length))
1394 (shrink-vector sequence jndex))
1395 (declare (fixnum index jndex))
1396 (setf (aref sequence jndex) (aref sequence index))))
1397 (declare (fixnum index jndex number-zapped))
1398 (setf (aref sequence jndex) (aref sequence index))
1399 (if ,pred
1400 (incf number-zapped)
1401 (incf jndex))))
1403 (sb!xc:defmacro mumble-delete-from-end (pred)
1404 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1405 (number-zapped 0)
1406 (losers ())
1407 this-element
1408 (terminus (1- start)))
1409 ((or (= index terminus) (= number-zapped count))
1410 (do ((losers losers) ; Delete the losers.
1411 (index start (1+ index))
1412 (jndex start))
1413 ((or (null losers) (= index (the fixnum end)))
1414 (do ((index index (1+ index)) ; Copy the rest of the vector.
1415 (jndex jndex (1+ jndex)))
1416 ((= index (the fixnum length))
1417 (shrink-vector sequence jndex))
1418 (declare (fixnum index jndex))
1419 (setf (aref sequence jndex) (aref sequence index))))
1420 (declare (fixnum index jndex))
1421 (setf (aref sequence jndex) (aref sequence index))
1422 (if (= index (the fixnum (car losers)))
1423 (pop losers)
1424 (incf jndex))))
1425 (declare (fixnum index number-zapped terminus))
1426 (setq this-element (aref sequence index))
1427 (when ,pred
1428 (incf number-zapped)
1429 (push index losers))))
1431 (sb!xc:defmacro normal-mumble-delete ()
1432 `(mumble-delete
1433 (if test-not
1434 (not (funcall test-not item (apply-key key (aref sequence index))))
1435 (funcall test item (apply-key key (aref sequence index))))))
1437 (sb!xc:defmacro normal-mumble-delete-from-end ()
1438 `(mumble-delete-from-end
1439 (if test-not
1440 (not (funcall test-not item (apply-key key this-element)))
1441 (funcall test item (apply-key key this-element)))))
1443 (sb!xc:defmacro list-delete (pred)
1444 `(let ((handle (cons nil sequence)))
1445 (declare (truly-dynamic-extent handle))
1446 (do ((current (nthcdr start sequence) (cdr current))
1447 (previous (nthcdr start handle))
1448 (index start (1+ index))
1449 (number-zapped 0))
1450 ((or (= index (the fixnum end)) (= number-zapped count))
1451 (cdr handle))
1452 (declare (fixnum index number-zapped))
1453 (cond (,pred
1454 (rplacd previous (cdr current))
1455 (incf number-zapped))
1457 (setq previous (cdr previous)))))))
1459 (sb!xc:defmacro list-delete-from-end (pred)
1460 `(let* ((reverse (nreverse (the list sequence)))
1461 (handle (cons nil reverse)))
1462 (declare (truly-dynamic-extent handle))
1463 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1464 (cdr current))
1465 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1466 (index start (1+ index))
1467 (number-zapped 0))
1468 ((or (= index (the fixnum end)) (= number-zapped count))
1469 (nreverse (cdr handle)))
1470 (declare (fixnum index number-zapped))
1471 (cond (,pred
1472 (rplacd previous (cdr current))
1473 (incf number-zapped))
1475 (setq previous (cdr previous)))))))
1477 (sb!xc:defmacro normal-list-delete ()
1478 '(list-delete
1479 (if test-not
1480 (not (funcall test-not item (apply-key key (car current))))
1481 (funcall test item (apply-key key (car current))))))
1483 (sb!xc:defmacro normal-list-delete-from-end ()
1484 '(list-delete-from-end
1485 (if test-not
1486 (not (funcall test-not item (apply-key key (car current))))
1487 (funcall test item (apply-key key (car current))))))
1489 ) ; EVAL-WHEN
1491 (define-sequence-traverser delete
1492 (item sequence &rest args &key from-end test test-not start
1493 end count key)
1494 #!+sb-doc
1495 "Return a sequence formed by destructively removing the specified ITEM from
1496 the given SEQUENCE."
1497 (declare (type fixnum start)
1498 (truly-dynamic-extent args))
1499 (seq-dispatch sequence
1500 (let ((end (or end length)))
1501 (declare (type index end))
1502 (if from-end
1503 (normal-list-delete-from-end)
1504 (normal-list-delete)))
1505 (let ((end (or end length)))
1506 (declare (type index end))
1507 (if from-end
1508 (normal-mumble-delete-from-end)
1509 (normal-mumble-delete)))
1510 (apply #'sb!sequence:delete item sequence args)))
1512 (eval-when (:compile-toplevel :execute)
1514 (sb!xc:defmacro if-mumble-delete ()
1515 `(mumble-delete
1516 (funcall predicate (apply-key key (aref sequence index)))))
1518 (sb!xc:defmacro if-mumble-delete-from-end ()
1519 `(mumble-delete-from-end
1520 (funcall predicate (apply-key key this-element))))
1522 (sb!xc:defmacro if-list-delete ()
1523 '(list-delete
1524 (funcall predicate (apply-key key (car current)))))
1526 (sb!xc:defmacro if-list-delete-from-end ()
1527 '(list-delete-from-end
1528 (funcall predicate (apply-key key (car current)))))
1530 ) ; EVAL-WHEN
1532 (define-sequence-traverser delete-if
1533 (predicate sequence &rest args &key from-end start key end count)
1534 #!+sb-doc
1535 "Return a sequence formed by destructively removing the elements satisfying
1536 the specified PREDICATE from the given SEQUENCE."
1537 (declare (type fixnum start)
1538 (truly-dynamic-extent args))
1539 (seq-dispatch sequence
1540 (let ((end (or end length)))
1541 (declare (type index end))
1542 (if from-end
1543 (if-list-delete-from-end)
1544 (if-list-delete)))
1545 (let ((end (or end length)))
1546 (declare (type index end))
1547 (if from-end
1548 (if-mumble-delete-from-end)
1549 (if-mumble-delete)))
1550 (apply #'sb!sequence:delete-if predicate sequence args)))
1552 (eval-when (:compile-toplevel :execute)
1554 (sb!xc:defmacro if-not-mumble-delete ()
1555 `(mumble-delete
1556 (not (funcall predicate (apply-key key (aref sequence index))))))
1558 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1559 `(mumble-delete-from-end
1560 (not (funcall predicate (apply-key key this-element)))))
1562 (sb!xc:defmacro if-not-list-delete ()
1563 '(list-delete
1564 (not (funcall predicate (apply-key key (car current))))))
1566 (sb!xc:defmacro if-not-list-delete-from-end ()
1567 '(list-delete-from-end
1568 (not (funcall predicate (apply-key key (car current))))))
1570 ) ; EVAL-WHEN
1572 (define-sequence-traverser delete-if-not
1573 (predicate sequence &rest args &key from-end start end key count)
1574 #!+sb-doc
1575 "Return a sequence formed by destructively removing the elements not
1576 satisfying the specified PREDICATE from the given SEQUENCE."
1577 (declare (type fixnum start)
1578 (truly-dynamic-extent args))
1579 (seq-dispatch sequence
1580 (let ((end (or end length)))
1581 (declare (type index end))
1582 (if from-end
1583 (if-not-list-delete-from-end)
1584 (if-not-list-delete)))
1585 (let ((end (or end length)))
1586 (declare (type index end))
1587 (if from-end
1588 (if-not-mumble-delete-from-end)
1589 (if-not-mumble-delete)))
1590 (apply #'sb!sequence:delete-if-not predicate sequence args)))
1592 ;;;; REMOVE
1594 (eval-when (:compile-toplevel :execute)
1596 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1597 ;;; satisfies the predicate.
1598 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1599 `(do ((index ,begin (,bump index))
1600 (result
1601 (do ((index ,left (,bump index))
1602 (result (%make-sequence-like sequence length)))
1603 ((= index (the fixnum ,begin)) result)
1604 (declare (fixnum index))
1605 (setf (aref result index) (aref sequence index))))
1606 (new-index ,begin)
1607 (number-zapped 0)
1608 (this-element))
1609 ((or (= index (the fixnum ,finish))
1610 (= number-zapped count))
1611 (do ((index index (,bump index))
1612 (new-index new-index (,bump new-index)))
1613 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1614 (declare (fixnum index new-index))
1615 (setf (aref result new-index) (aref sequence index))))
1616 (declare (fixnum index new-index number-zapped))
1617 (setq this-element (aref sequence index))
1618 (cond (,pred (incf number-zapped))
1619 (t (setf (aref result new-index) this-element)
1620 (setq new-index (,bump new-index))))))
1622 (sb!xc:defmacro mumble-remove (pred)
1623 `(mumble-remove-macro 1+ 0 start end length ,pred))
1625 (sb!xc:defmacro mumble-remove-from-end (pred)
1626 `(let ((sequence (copy-seq sequence)))
1627 (mumble-delete-from-end ,pred)))
1629 (sb!xc:defmacro normal-mumble-remove ()
1630 `(mumble-remove
1631 (if test-not
1632 (not (funcall test-not item (apply-key key this-element)))
1633 (funcall test item (apply-key key this-element)))))
1635 (sb!xc:defmacro normal-mumble-remove-from-end ()
1636 `(mumble-remove-from-end
1637 (if test-not
1638 (not (funcall test-not item (apply-key key this-element)))
1639 (funcall test item (apply-key key this-element)))))
1641 (sb!xc:defmacro if-mumble-remove ()
1642 `(mumble-remove (funcall predicate (apply-key key this-element))))
1644 (sb!xc:defmacro if-mumble-remove-from-end ()
1645 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1647 (sb!xc:defmacro if-not-mumble-remove ()
1648 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1650 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1651 `(mumble-remove-from-end
1652 (not (funcall predicate (apply-key key this-element)))))
1654 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1655 ;;; the predicate.
1656 (sb!xc:defmacro list-remove-macro (pred reverse?)
1657 `(let* ((sequence ,(if reverse?
1658 '(reverse (the list sequence))
1659 'sequence))
1660 (%start ,(if reverse? '(- length end) 'start))
1661 (%end ,(if reverse? '(- length start) 'end))
1662 (splice (list nil))
1663 (results (do ((index 0 (1+ index))
1664 (before-start splice))
1665 ((= index (the fixnum %start)) before-start)
1666 (declare (fixnum index))
1667 (setq splice
1668 (cdr (rplacd splice (list (pop sequence))))))))
1669 (do ((index %start (1+ index))
1670 (this-element)
1671 (number-zapped 0))
1672 ((or (= index (the fixnum %end)) (= number-zapped count))
1673 (do ((index index (1+ index)))
1674 ((null sequence)
1675 ,(if reverse?
1676 '(nreverse (the list (cdr results)))
1677 '(cdr results)))
1678 (declare (fixnum index))
1679 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1680 (declare (fixnum index number-zapped))
1681 (setq this-element (pop sequence))
1682 (if ,pred
1683 (setq number-zapped (1+ number-zapped))
1684 (setq splice (cdr (rplacd splice (list this-element))))))))
1686 (sb!xc:defmacro list-remove (pred)
1687 `(list-remove-macro ,pred nil))
1689 (sb!xc:defmacro list-remove-from-end (pred)
1690 `(list-remove-macro ,pred t))
1692 (sb!xc:defmacro normal-list-remove ()
1693 `(list-remove
1694 (if test-not
1695 (not (funcall test-not item (apply-key key this-element)))
1696 (funcall test item (apply-key key this-element)))))
1698 (sb!xc:defmacro normal-list-remove-from-end ()
1699 `(list-remove-from-end
1700 (if test-not
1701 (not (funcall test-not item (apply-key key this-element)))
1702 (funcall test item (apply-key key this-element)))))
1704 (sb!xc:defmacro if-list-remove ()
1705 `(list-remove
1706 (funcall predicate (apply-key key this-element))))
1708 (sb!xc:defmacro if-list-remove-from-end ()
1709 `(list-remove-from-end
1710 (funcall predicate (apply-key key this-element))))
1712 (sb!xc:defmacro if-not-list-remove ()
1713 `(list-remove
1714 (not (funcall predicate (apply-key key this-element)))))
1716 (sb!xc:defmacro if-not-list-remove-from-end ()
1717 `(list-remove-from-end
1718 (not (funcall predicate (apply-key key this-element)))))
1720 ) ; EVAL-WHEN
1722 (define-sequence-traverser remove
1723 (item sequence &rest args &key from-end test test-not start
1724 end count key)
1725 #!+sb-doc
1726 "Return a copy of SEQUENCE with elements satisfying the test (default is
1727 EQL) with ITEM removed."
1728 (declare (type fixnum start)
1729 (truly-dynamic-extent args))
1730 (seq-dispatch sequence
1731 (let ((end (or end length)))
1732 (declare (type index end))
1733 (if from-end
1734 (normal-list-remove-from-end)
1735 (normal-list-remove)))
1736 (let ((end (or end length)))
1737 (declare (type index end))
1738 (if from-end
1739 (normal-mumble-remove-from-end)
1740 (normal-mumble-remove)))
1741 (apply #'sb!sequence:remove item sequence args)))
1743 (define-sequence-traverser remove-if
1744 (predicate sequence &rest args &key from-end start end count key)
1745 #!+sb-doc
1746 "Return a copy of sequence with elements satisfying PREDICATE removed."
1747 (declare (type fixnum start)
1748 (truly-dynamic-extent args))
1749 (seq-dispatch sequence
1750 (let ((end (or end length)))
1751 (declare (type index end))
1752 (if from-end
1753 (if-list-remove-from-end)
1754 (if-list-remove)))
1755 (let ((end (or end length)))
1756 (declare (type index end))
1757 (if from-end
1758 (if-mumble-remove-from-end)
1759 (if-mumble-remove)))
1760 (apply #'sb!sequence:remove-if predicate sequence args)))
1762 (define-sequence-traverser remove-if-not
1763 (predicate sequence &rest args &key from-end start end count key)
1764 #!+sb-doc
1765 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1766 (declare (type fixnum start)
1767 (truly-dynamic-extent args))
1768 (seq-dispatch sequence
1769 (let ((end (or end length)))
1770 (declare (type index end))
1771 (if from-end
1772 (if-not-list-remove-from-end)
1773 (if-not-list-remove)))
1774 (let ((end (or end length)))
1775 (declare (type index end))
1776 (if from-end
1777 (if-not-mumble-remove-from-end)
1778 (if-not-mumble-remove)))
1779 (apply #'sb!sequence:remove-if-not predicate sequence args)))
1781 ;;;; REMOVE-DUPLICATES
1783 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1784 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1785 ;;; if we look into the already copied structure (from after :start) and see
1786 ;;; the item. If we check from beginning we check into the rest of the
1787 ;;; original list up to the :end marker (this we have to do by running a
1788 ;;; do loop down the list that far and using our test.
1789 (defun list-remove-duplicates* (list test test-not start end key from-end)
1790 (declare (fixnum start))
1791 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1792 (splice result)
1793 (current list)
1794 (end (or end (length list)))
1795 (hash (and (> (- end start) 20)
1796 test
1797 (not key)
1798 (not test-not)
1799 (or (eql test #'eql)
1800 (eql test #'eq)
1801 (eql test #'equal)
1802 (eql test #'equalp))
1803 (make-hash-table :test test :size (- end start)))))
1804 (do ((index 0 (1+ index)))
1805 ((= index start))
1806 (declare (fixnum index))
1807 (setq splice (cdr (rplacd splice (list (car current)))))
1808 (setq current (cdr current)))
1809 (if hash
1810 (do ((index start (1+ index)))
1811 ((or (and end (= index (the fixnum end)))
1812 (atom current)))
1813 (declare (fixnum index))
1814 ;; The hash table contains links from values that are
1815 ;; already in result to the cons cell *preceding* theirs
1816 ;; in the list. That is, for each value v in the list,
1817 ;; v and (cadr (gethash v hash)) are equal under TEST.
1818 (let ((prev (gethash (car current) hash)))
1819 (cond
1820 ((not prev)
1821 (setf (gethash (car current) hash) splice)
1822 (setq splice (cdr (rplacd splice (list (car current))))))
1823 ((not from-end)
1824 (let* ((old (cdr prev))
1825 (next (cdr old)))
1826 (if next
1827 (let ((next-val (car next)))
1828 ;; (assert (eq (gethash next-val hash) old))
1829 (setf (cdr prev) next
1830 (gethash next-val hash) prev
1831 (gethash (car current) hash) splice
1832 splice (cdr (rplacd splice (list (car current))))))
1833 (setf (car old) (car current)))))))
1834 (setq current (cdr current)))
1835 (do ((index start (1+ index)))
1836 ((or (and end (= index (the fixnum end)))
1837 (atom current)))
1838 (declare (fixnum index))
1839 (if (or (and from-end
1840 (not (if test-not
1841 (member (apply-key key (car current))
1842 (nthcdr (1+ start) result)
1843 :test-not test-not
1844 :key key)
1845 (member (apply-key key (car current))
1846 (nthcdr (1+ start) result)
1847 :test test
1848 :key key))))
1849 (and (not from-end)
1850 (not (do ((it (apply-key key (car current)))
1851 (l (cdr current) (cdr l))
1852 (i (1+ index) (1+ i)))
1853 ((or (atom l) (and end (= i (the fixnum end))))
1855 (declare (fixnum i))
1856 (if (if test-not
1857 (not (funcall test-not
1859 (apply-key key (car l))))
1860 (funcall test it (apply-key key (car l))))
1861 (return t))))))
1862 (setq splice (cdr (rplacd splice (list (car current))))))
1863 (setq current (cdr current))))
1864 (do ()
1865 ((atom current))
1866 (setq splice (cdr (rplacd splice (list (car current)))))
1867 (setq current (cdr current)))
1868 (cdr result)))
1870 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1871 &optional (length (length vector)))
1872 (declare (vector vector) (fixnum start length))
1873 (when (null end) (setf end (length vector)))
1874 (let ((result (%make-sequence-like vector length))
1875 (index 0)
1876 (jndex start))
1877 (declare (fixnum index jndex))
1878 (do ()
1879 ((= index start))
1880 (setf (aref result index) (aref vector index))
1881 (setq index (1+ index)))
1882 (do ((elt))
1883 ((= index end))
1884 (setq elt (aref vector index))
1885 (unless (or (and from-end
1886 (if test-not
1887 (position (apply-key key elt) result
1888 :start start :end jndex
1889 :test-not test-not :key key)
1890 (position (apply-key key elt) result
1891 :start start :end jndex
1892 :test test :key key)))
1893 (and (not from-end)
1894 (if test-not
1895 (position (apply-key key elt) vector
1896 :start (1+ index) :end end
1897 :test-not test-not :key key)
1898 (position (apply-key key elt) vector
1899 :start (1+ index) :end end
1900 :test test :key key))))
1901 (setf (aref result jndex) elt)
1902 (setq jndex (1+ jndex)))
1903 (setq index (1+ index)))
1904 (do ()
1905 ((= index length))
1906 (setf (aref result jndex) (aref vector index))
1907 (setq index (1+ index))
1908 (setq jndex (1+ jndex)))
1909 (%shrink-vector result jndex)))
1911 (define-sequence-traverser remove-duplicates
1912 (sequence &rest args &key test test-not start end from-end key)
1913 #!+sb-doc
1914 "The elements of SEQUENCE are compared pairwise, and if any two match,
1915 the one occurring earlier is discarded, unless FROM-END is true, in
1916 which case the one later in the sequence is discarded. The resulting
1917 sequence is returned.
1919 The :TEST-NOT argument is deprecated."
1920 (declare (fixnum start)
1921 (truly-dynamic-extent args))
1922 (seq-dispatch sequence
1923 (if sequence
1924 (list-remove-duplicates* sequence test test-not
1925 start end key from-end))
1926 (vector-remove-duplicates* sequence test test-not start end key from-end)
1927 (apply #'sb!sequence:remove-duplicates sequence args)))
1929 ;;;; DELETE-DUPLICATES
1931 (defun list-delete-duplicates* (list test test-not key from-end start end)
1932 (declare (fixnum start))
1933 (let ((handle (cons nil list)))
1934 (declare (truly-dynamic-extent handle))
1935 (do ((current (nthcdr start list) (cdr current))
1936 (previous (nthcdr start handle))
1937 (index start (1+ index)))
1938 ((or (and end (= index (the fixnum end))) (null current))
1939 (cdr handle))
1940 (declare (fixnum index))
1941 (if (do ((x (if from-end
1942 (nthcdr (1+ start) handle)
1943 (cdr current))
1944 (cdr x))
1945 (i (1+ index) (1+ i)))
1946 ((or (null x)
1947 (and (not from-end) end (= i (the fixnum end)))
1948 (eq x current))
1949 nil)
1950 (declare (fixnum i))
1951 (if (if test-not
1952 (not (funcall test-not
1953 (apply-key key (car current))
1954 (apply-key key (car x))))
1955 (funcall test
1956 (apply-key key (car current))
1957 (apply-key key (car x))))
1958 (return t)))
1959 (rplacd previous (cdr current))
1960 (setq previous (cdr previous))))))
1962 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1963 &optional (length (length vector)))
1964 (declare (vector vector) (fixnum start length))
1965 (when (null end) (setf end (length vector)))
1966 (do ((index start (1+ index))
1967 (jndex start))
1968 ((= index end)
1969 (do ((index index (1+ index)) ; copy the rest of the vector
1970 (jndex jndex (1+ jndex)))
1971 ((= index length)
1972 (shrink-vector vector jndex))
1973 (setf (aref vector jndex) (aref vector index))))
1974 (declare (fixnum index jndex))
1975 (setf (aref vector jndex) (aref vector index))
1976 (unless (if 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-not test-not)
1981 (position (apply-key key (aref vector index)) vector :key key
1982 :start (if from-end start (1+ index))
1983 :end (if from-end jndex end)
1984 :test test))
1985 (setq jndex (1+ jndex)))))
1987 (define-sequence-traverser delete-duplicates
1988 (sequence &rest args &key test test-not start end from-end key)
1989 #!+sb-doc
1990 "The elements of SEQUENCE are examined, and if any two match, one is
1991 discarded. The resulting sequence, which may be formed by destroying the
1992 given sequence, is returned.
1994 The :TEST-NOT argument is deprecated."
1995 (declare (truly-dynamic-extent args))
1996 (seq-dispatch sequence
1997 (when sequence
1998 (list-delete-duplicates* sequence test test-not
1999 key from-end start end))
2000 (vector-delete-duplicates* sequence test test-not key from-end start end)
2001 (apply #'sb!sequence:delete-duplicates sequence args)))
2003 ;;;; SUBSTITUTE
2005 (defun list-substitute* (pred new list start end count key test test-not old)
2006 (declare (fixnum start end count))
2007 (let* ((result (list nil))
2009 (splice result)
2010 (list list)) ; Get a local list for a stepper.
2011 (do ((index 0 (1+ index)))
2012 ((= index start))
2013 (declare (fixnum index))
2014 (setq splice (cdr (rplacd splice (list (car list)))))
2015 (setq list (cdr list)))
2016 (do ((index start (1+ index)))
2017 ((or (= index end) (null list) (= count 0)))
2018 (declare (fixnum index))
2019 (setq elt (car list))
2020 (setq splice
2021 (cdr (rplacd splice
2022 (list
2023 (cond
2024 ((case pred
2025 (normal
2026 (if test-not
2027 (not
2028 (funcall test-not old (apply-key key elt)))
2029 (funcall test old (apply-key key elt))))
2030 (if (funcall test (apply-key key elt)))
2031 (if-not (not (funcall test (apply-key key elt)))))
2032 (decf count)
2033 new)
2034 (t elt))))))
2035 (setq list (cdr list)))
2036 (do ()
2037 ((null list))
2038 (setq splice (cdr (rplacd splice (list (car list)))))
2039 (setq list (cdr list)))
2040 (cdr result)))
2042 ;;; Replace old with new in sequence moving from left to right by incrementer
2043 ;;; on each pass through the loop. Called by all three substitute functions.
2044 (defun vector-substitute* (pred new sequence incrementer left right length
2045 start end count key test test-not old)
2046 (declare (fixnum start count end incrementer right))
2047 (let ((result (%make-sequence-like sequence length))
2048 (index left))
2049 (declare (fixnum index))
2050 (do ()
2051 ((= index start))
2052 (setf (aref result index) (aref sequence index))
2053 (setq index (+ index incrementer)))
2054 (do ((elt))
2055 ((or (= index end) (= count 0)))
2056 (setq elt (aref sequence index))
2057 (setf (aref result index)
2058 (cond ((case pred
2059 (normal
2060 (if test-not
2061 (not (funcall test-not old (apply-key key elt)))
2062 (funcall test old (apply-key key elt))))
2063 (if (funcall test (apply-key key elt)))
2064 (if-not (not (funcall test (apply-key key elt)))))
2065 (setq count (1- count))
2066 new)
2067 (t elt)))
2068 (setq index (+ index incrementer)))
2069 (do ()
2070 ((= index right))
2071 (setf (aref result index) (aref sequence index))
2072 (setq index (+ index incrementer)))
2073 result))
2075 (eval-when (:compile-toplevel :execute)
2077 (sb!xc:defmacro subst-dispatch (pred)
2078 `(seq-dispatch sequence
2079 (let ((end (or end length)))
2080 (declare (type index end))
2081 (if from-end
2082 (nreverse (list-substitute* ,pred
2084 (reverse sequence)
2085 (- (the fixnum length)
2086 (the fixnum end))
2087 (- (the fixnum length)
2088 (the fixnum start))
2089 count key test test-not old))
2090 (list-substitute* ,pred
2091 new sequence start end count key test test-not
2092 old)))
2094 (let ((end (or end length)))
2095 (declare (type index end))
2096 (if from-end
2097 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
2098 -1 length (1- (the fixnum end))
2099 (1- (the fixnum start))
2100 count key test test-not old)
2101 (vector-substitute* ,pred new sequence 1 0 length length
2102 start end count key test test-not old)))
2104 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2105 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2106 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2107 ;; dispatch once per element on PRED's run-time identity.
2108 ,(ecase (cadr pred)
2109 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
2110 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
2111 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
2112 ) ; EVAL-WHEN
2114 (define-sequence-traverser substitute
2115 (new old sequence &rest args &key from-end test test-not
2116 start count end key)
2117 #!+sb-doc
2118 "Return a sequence of the same kind as SEQUENCE with the same elements,
2119 except that all elements equal to OLD are replaced with NEW."
2120 (declare (type fixnum start)
2121 (truly-dynamic-extent args))
2122 (subst-dispatch 'normal))
2124 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2126 (define-sequence-traverser substitute-if
2127 (new predicate sequence &rest args &key from-end start end count key)
2128 #!+sb-doc
2129 "Return a sequence of the same kind as SEQUENCE with the same elements
2130 except that all elements satisfying the PRED are replaced with NEW."
2131 (declare (type fixnum start)
2132 (truly-dynamic-extent args))
2133 (let ((test predicate)
2134 (test-not nil)
2135 old)
2136 (subst-dispatch 'if)))
2138 (define-sequence-traverser substitute-if-not
2139 (new predicate sequence &rest args &key from-end start end count key)
2140 #!+sb-doc
2141 "Return a sequence of the same kind as SEQUENCE with the same elements
2142 except that all elements not satisfying the PRED are replaced with NEW."
2143 (declare (type fixnum start)
2144 (truly-dynamic-extent args))
2145 (let ((test predicate)
2146 (test-not nil)
2147 old)
2148 (subst-dispatch 'if-not)))
2150 ;;;; NSUBSTITUTE
2152 (define-sequence-traverser nsubstitute
2153 (new old sequence &rest args &key from-end test test-not
2154 end count key start)
2155 #!+sb-doc
2156 "Return a sequence of the same kind as SEQUENCE with the same elements
2157 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2158 may be destructively modified."
2159 (declare (type fixnum start)
2160 (truly-dynamic-extent args))
2161 (seq-dispatch sequence
2162 (let ((end (or end length)))
2163 (declare (type index end))
2164 (if from-end
2165 (nreverse (nlist-substitute*
2166 new old (nreverse (the list sequence))
2167 test test-not (- length end) (- length start)
2168 count key))
2169 (nlist-substitute* new old sequence
2170 test test-not start end count key)))
2171 (let ((end (or end length)))
2172 (declare (type index end))
2173 (if from-end
2174 (nvector-substitute* new old sequence -1
2175 test test-not (1- end) (1- start) count key)
2176 (nvector-substitute* new old sequence 1
2177 test test-not start end count key)))
2178 (apply #'sb!sequence:nsubstitute new old sequence args)))
2180 (defun nlist-substitute* (new old sequence test test-not start end count key)
2181 (declare (fixnum start count end))
2182 (do ((list (nthcdr start sequence) (cdr list))
2183 (index start (1+ index)))
2184 ((or (= index end) (null list) (= count 0)) sequence)
2185 (declare (fixnum index))
2186 (when (if test-not
2187 (not (funcall test-not old (apply-key key (car list))))
2188 (funcall test old (apply-key key (car list))))
2189 (rplaca list new)
2190 (setq count (1- count)))))
2192 (defun nvector-substitute* (new old sequence incrementer
2193 test test-not start end count key)
2194 (declare (fixnum start incrementer count end))
2195 (do ((index start (+ index incrementer)))
2196 ((or (= index end) (= count 0)) sequence)
2197 (declare (fixnum index))
2198 (when (if test-not
2199 (not (funcall test-not
2201 (apply-key key (aref sequence index))))
2202 (funcall test old (apply-key key (aref sequence index))))
2203 (setf (aref sequence index) new)
2204 (setq count (1- count)))))
2206 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2208 (define-sequence-traverser nsubstitute-if
2209 (new predicate sequence &rest args &key from-end start end count key)
2210 #!+sb-doc
2211 "Return a sequence of the same kind as SEQUENCE with the same elements
2212 except that all elements satisfying PREDICATE are replaced with NEW.
2213 SEQUENCE may be destructively modified."
2214 (declare (type fixnum start)
2215 (truly-dynamic-extent args))
2216 (seq-dispatch sequence
2217 (let ((end (or end length)))
2218 (declare (type index end))
2219 (if from-end
2220 (nreverse (nlist-substitute-if*
2221 new predicate (nreverse (the list sequence))
2222 (- length end) (- length start) count key))
2223 (nlist-substitute-if* new predicate sequence
2224 start end count key)))
2225 (let ((end (or end length)))
2226 (declare (type index end))
2227 (if from-end
2228 (nvector-substitute-if* new predicate sequence -1
2229 (1- end) (1- start) count key)
2230 (nvector-substitute-if* new predicate sequence 1
2231 start end count key)))
2232 (apply #'sb!sequence:nsubstitute-if new predicate sequence args)))
2234 (defun nlist-substitute-if* (new test sequence start end count key)
2235 (declare (type fixnum end)
2236 (type function test)) ; coercion is done by caller
2237 (do ((list (nthcdr start sequence) (cdr list))
2238 (index start (1+ index)))
2239 ((or (= index end) (null list) (= count 0)) sequence)
2240 (when (funcall test (apply-key key (car list)))
2241 (rplaca list new)
2242 (setq count (1- count)))))
2244 (defun nvector-substitute-if* (new test sequence incrementer
2245 start end count key)
2246 (declare (type fixnum end)
2247 (type function test)) ; coercion is done by caller
2248 (do ((index start (+ index incrementer)))
2249 ((or (= index end) (= count 0)) sequence)
2250 (when (funcall test (apply-key key (aref sequence index)))
2251 (setf (aref sequence index) new)
2252 (setq count (1- count)))))
2254 (define-sequence-traverser nsubstitute-if-not
2255 (new predicate sequence &rest args &key from-end start end count key)
2256 #!+sb-doc
2257 "Return a sequence of the same kind as SEQUENCE with the same elements
2258 except that all elements not satisfying PREDICATE are replaced with NEW.
2259 SEQUENCE may be destructively modified."
2260 (declare (type fixnum start)
2261 (truly-dynamic-extent args))
2262 (seq-dispatch sequence
2263 (let ((end (or end length)))
2264 (declare (fixnum end))
2265 (if from-end
2266 (nreverse (nlist-substitute-if-not*
2267 new predicate (nreverse (the list sequence))
2268 (- length end) (- length start) count key))
2269 (nlist-substitute-if-not* new predicate sequence
2270 start end count key)))
2271 (let ((end (or end length)))
2272 (declare (fixnum end))
2273 (if from-end
2274 (nvector-substitute-if-not* new predicate sequence -1
2275 (1- end) (1- start) count key)
2276 (nvector-substitute-if-not* new predicate sequence 1
2277 start end count key)))
2278 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args)))
2280 (defun nlist-substitute-if-not* (new test sequence start end count key)
2281 (declare (type fixnum end)
2282 (type function test)) ; coercion is done by caller
2283 (do ((list (nthcdr start sequence) (cdr list))
2284 (index start (1+ index)))
2285 ((or (= index end) (null list) (= count 0)) sequence)
2286 (when (not (funcall test (apply-key key (car list))))
2287 (rplaca list new)
2288 (decf count))))
2290 (defun nvector-substitute-if-not* (new test sequence incrementer
2291 start end count key)
2292 (declare (type fixnum end)
2293 (type function test)) ; coercion is done by caller
2294 (do ((index start (+ index incrementer)))
2295 ((or (= index end) (= count 0)) sequence)
2296 (when (not (funcall test (apply-key key (aref sequence index))))
2297 (setf (aref sequence index) new)
2298 (decf count))))
2300 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2302 (defun effective-find-position-test (test test-not)
2303 (effective-find-position-test test test-not))
2304 (defun effective-find-position-key (key)
2305 (effective-find-position-key key))
2307 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2308 (macrolet (;; shared logic for defining %FIND-POSITION and
2309 ;; %FIND-POSITION-IF in terms of various inlineable cases
2310 ;; of the expression defined in FROB and VECTOR*-FROB
2311 (frobs (&optional bit-frob)
2312 `(seq-dispatch sequence-arg
2313 (frob sequence-arg from-end)
2314 (with-array-data ((sequence sequence-arg :offset-var offset)
2315 (start start)
2316 (end end)
2317 :check-fill-pointer t)
2318 (multiple-value-bind (f p)
2319 (macrolet ((frob2 () `(if from-end
2320 (frob sequence t)
2321 (frob sequence nil))))
2322 (typecase sequence
2323 #!+sb-unicode
2324 ((simple-array character (*)) (frob2))
2325 ((simple-array base-char (*)) (frob2))
2326 ,@(when bit-frob
2327 `((simple-bit-vector
2328 (if (and (typep item 'bit)
2329 (eq #'identity key)
2330 (or (eq #'eq test)
2331 (eq #'eql test)
2332 (eq #'equal test)))
2333 (let ((p (%bit-position item sequence
2334 from-end start end)))
2335 (if p
2336 (values item p)
2337 (values nil nil)))
2338 (vector*-frob sequence)))))
2340 (vector*-frob sequence))))
2341 (declare (type (or index null) p))
2342 (values f (and p (the index (- p offset)))))))))
2343 (defun %find-position (item sequence-arg from-end start end key test)
2344 (macrolet ((frob (sequence from-end)
2345 `(%find-position item ,sequence
2346 ,from-end start end key test))
2347 (vector*-frob (sequence)
2348 `(%find-position-vector-macro item ,sequence
2349 from-end start end key test)))
2350 (frobs t)))
2351 (defun %find-position-if (predicate sequence-arg from-end start end key)
2352 (macrolet ((frob (sequence from-end)
2353 `(%find-position-if predicate ,sequence
2354 ,from-end start end key))
2355 (vector*-frob (sequence)
2356 `(%find-position-if-vector-macro predicate ,sequence
2357 from-end start end key)))
2358 (frobs)))
2359 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2360 (macrolet ((frob (sequence from-end)
2361 `(%find-position-if-not predicate ,sequence
2362 ,from-end start end key))
2363 (vector*-frob (sequence)
2364 `(%find-position-if-not-vector-macro predicate ,sequence
2365 from-end start end key)))
2366 (frobs))))
2368 (defun find
2369 (item sequence &rest args &key from-end (start 0) end key test test-not)
2370 (declare (truly-dynamic-extent args))
2371 (seq-dispatch sequence
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 (nth-value 0 (%find-position
2377 item sequence from-end start end
2378 (effective-find-position-key key)
2379 (effective-find-position-test test test-not)))
2380 (apply #'sb!sequence:find item sequence args)))
2381 (defun position
2382 (item sequence &rest args &key from-end (start 0) end key test test-not)
2383 (declare (truly-dynamic-extent args))
2384 (seq-dispatch sequence
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 (nth-value 1 (%find-position
2390 item sequence from-end start end
2391 (effective-find-position-key key)
2392 (effective-find-position-test test test-not)))
2393 (apply #'sb!sequence:position item sequence args)))
2395 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2396 (declare (truly-dynamic-extent args))
2397 (seq-dispatch sequence
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 (nth-value 0 (%find-position-if
2403 (%coerce-callable-to-fun predicate)
2404 sequence from-end start end
2405 (effective-find-position-key key)))
2406 (apply #'sb!sequence:find-if predicate sequence args)))
2407 (defun position-if
2408 (predicate sequence &rest args &key from-end (start 0) end key)
2409 (declare (truly-dynamic-extent args))
2410 (seq-dispatch sequence
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 (nth-value 1 (%find-position-if
2416 (%coerce-callable-to-fun predicate)
2417 sequence from-end start end
2418 (effective-find-position-key key)))
2419 (apply #'sb!sequence:position-if predicate sequence args)))
2421 (defun find-if-not
2422 (predicate sequence &rest args &key from-end (start 0) end key)
2423 (declare (truly-dynamic-extent args))
2424 (seq-dispatch sequence
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 (nth-value 0 (%find-position-if-not
2430 (%coerce-callable-to-fun predicate)
2431 sequence from-end start end
2432 (effective-find-position-key key)))
2433 (apply #'sb!sequence:find-if-not predicate sequence args)))
2434 (defun position-if-not
2435 (predicate sequence &rest args &key from-end (start 0) end key)
2436 (declare (truly-dynamic-extent args))
2437 (seq-dispatch sequence
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 (nth-value 1 (%find-position-if-not
2443 (%coerce-callable-to-fun predicate)
2444 sequence from-end start end
2445 (effective-find-position-key key)))
2446 (apply #'sb!sequence:position-if-not predicate sequence args)))
2448 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2450 (eval-when (:compile-toplevel :execute)
2452 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2453 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2454 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2455 `(let ((%start ,(if from-end-p '(1- end) 'start))
2456 (%end ,(if from-end-p '(1- start) 'end)))
2457 (do ((index %start ,next-index)
2458 (count 0))
2459 ((= index (the fixnum %end)) count)
2460 (declare (fixnum index count))
2461 (,(if notp 'unless 'when) ,pred
2462 (setq count (1+ count)))))))
2464 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2465 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2466 `(let ((%start ,(if from-end-p '(- length end) 'start))
2467 (%end ,(if from-end-p '(- length start) 'end))
2468 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2469 (do ((sequence (nthcdr %start ,sequence))
2470 (index %start (1+ index))
2471 (count 0))
2472 ((or (= index (the fixnum %end)) (null sequence)) count)
2473 (declare (fixnum index count))
2474 (,(if notp 'unless 'when) ,pred
2475 (setq count (1+ count)))))))
2478 ) ; EVAL-WHEN
2480 (define-sequence-traverser count-if
2481 (pred sequence &rest args &key from-end start end key)
2482 #!+sb-doc
2483 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2484 (declare (type fixnum start)
2485 (truly-dynamic-extent args))
2486 (let ((pred (%coerce-callable-to-fun pred)))
2487 (seq-dispatch sequence
2488 (let ((end (or end length)))
2489 (declare (type index end))
2490 (if from-end
2491 (list-count-if nil t pred sequence)
2492 (list-count-if nil nil pred sequence)))
2493 (let ((end (or end length)))
2494 (declare (type index end))
2495 (if from-end
2496 (vector-count-if nil t pred sequence)
2497 (vector-count-if nil nil pred sequence)))
2498 (apply #'sb!sequence:count-if pred sequence args))))
2500 (define-sequence-traverser count-if-not
2501 (pred sequence &rest args &key from-end start end key)
2502 #!+sb-doc
2503 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2504 (declare (type fixnum start)
2505 (truly-dynamic-extent args))
2506 (let ((pred (%coerce-callable-to-fun pred)))
2507 (seq-dispatch sequence
2508 (let ((end (or end length)))
2509 (declare (type index end))
2510 (if from-end
2511 (list-count-if t t pred sequence)
2512 (list-count-if t nil pred sequence)))
2513 (let ((end (or end length)))
2514 (declare (type index end))
2515 (if from-end
2516 (vector-count-if t t pred sequence)
2517 (vector-count-if t nil pred sequence)))
2518 (apply #'sb!sequence:count-if-not pred sequence args))))
2520 (define-sequence-traverser count
2521 (item sequence &rest args &key from-end start end
2522 key (test #'eql test-p) (test-not nil test-not-p))
2523 #!+sb-doc
2524 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2525 which defaults to EQL."
2526 (declare (type fixnum start)
2527 (truly-dynamic-extent args))
2528 (when (and test-p test-not-p)
2529 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2530 ;; (CLHS 17.2.1)
2531 (error ":TEST and :TEST-NOT are both present."))
2532 (let ((%test (if test-not-p
2533 (lambda (x)
2534 (not (funcall test-not item x)))
2535 (lambda (x)
2536 (funcall test item x)))))
2537 (seq-dispatch sequence
2538 (let ((end (or end length)))
2539 (declare (type index end))
2540 (if from-end
2541 (list-count-if nil t %test sequence)
2542 (list-count-if nil nil %test sequence)))
2543 (let ((end (or end length)))
2544 (declare (type index end))
2545 (if from-end
2546 (vector-count-if nil t %test sequence)
2547 (vector-count-if nil nil %test sequence)))
2548 (apply #'sb!sequence:count item sequence args))))
2550 ;;;; MISMATCH
2552 (eval-when (:compile-toplevel :execute)
2554 (sb!xc:defmacro match-vars (&rest body)
2555 `(let ((inc (if from-end -1 1))
2556 (start1 (if from-end (1- (the fixnum end1)) start1))
2557 (start2 (if from-end (1- (the fixnum end2)) start2))
2558 (end1 (if from-end (1- (the fixnum start1)) end1))
2559 (end2 (if from-end (1- (the fixnum start2)) end2)))
2560 (declare (fixnum inc start1 start2 end1 end2))
2561 ,@body))
2563 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2564 (declare (ignore end)) ;; ### Should END be used below?
2565 `(let ((,sequence (if from-end
2566 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2567 (reverse (the list ,sequence)))
2568 (nthcdr ,start ,sequence))))
2569 (declare (type list ,sequence))
2570 ,@body))
2572 ) ; EVAL-WHEN
2574 (eval-when (:compile-toplevel :execute)
2576 (sb!xc:defmacro if-mismatch (elt1 elt2)
2577 `(cond ((= (the fixnum index1) (the fixnum end1))
2578 (return (if (= (the fixnum index2) (the fixnum end2))
2580 (if from-end
2581 (1+ (the fixnum index1))
2582 (the fixnum index1)))))
2583 ((= (the fixnum index2) (the fixnum end2))
2584 (return (if from-end (1+ (the fixnum index1)) index1)))
2585 (test-not
2586 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2587 (return (if from-end (1+ (the fixnum index1)) index1))))
2588 (t (if (not (funcall test (apply-key key ,elt1)
2589 (apply-key key ,elt2)))
2590 (return (if from-end (1+ (the fixnum index1)) index1))))))
2592 (sb!xc:defmacro mumble-mumble-mismatch ()
2593 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2594 (index2 start2 (+ index2 (the fixnum inc))))
2595 (())
2596 (declare (fixnum index1 index2))
2597 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2599 (sb!xc:defmacro mumble-list-mismatch ()
2600 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2601 (index2 start2 (+ index2 (the fixnum inc))))
2602 (())
2603 (declare (fixnum index1 index2))
2604 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2606 (sb!xc:defmacro list-mumble-mismatch ()
2607 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2608 (index2 start2 (+ index2 (the fixnum inc))))
2609 (())
2610 (declare (fixnum index1 index2))
2611 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2613 (sb!xc:defmacro list-list-mismatch ()
2614 `(do ((sequence1 sequence1)
2615 (sequence2 sequence2)
2616 (index1 start1 (+ index1 (the fixnum inc)))
2617 (index2 start2 (+ index2 (the fixnum inc))))
2618 (())
2619 (declare (fixnum index1 index2))
2620 (if-mismatch (pop sequence1) (pop sequence2))))
2622 ) ; EVAL-WHEN
2624 (define-sequence-traverser mismatch
2625 (sequence1 sequence2 &rest args &key from-end test test-not
2626 start1 end1 start2 end2 key)
2627 #!+sb-doc
2628 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2629 element-wise. If they are of equal length and match in every element, the
2630 result is NIL. Otherwise, the result is a non-negative integer, the index
2631 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2632 if one is shorter than and a matching prefix of the other, the index within
2633 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2634 :FROM-END argument is given, then one plus the index of the rightmost
2635 position in which the sequences differ is returned."
2636 (declare (type fixnum start1 start2))
2637 (declare (truly-dynamic-extent args))
2638 (seq-dispatch sequence1
2639 (seq-dispatch sequence2
2640 (let ((end1 (or end1 length1))
2641 (end2 (or end2 length2)))
2642 (declare (type index end1 end2))
2643 (match-vars
2644 (matchify-list (sequence1 start1 length1 end1)
2645 (matchify-list (sequence2 start2 length2 end2)
2646 (list-list-mismatch)))))
2647 (let ((end1 (or end1 length1))
2648 (end2 (or end2 length2)))
2649 (declare (type index end1 end2))
2650 (match-vars
2651 (matchify-list (sequence1 start1 length1 end1)
2652 (list-mumble-mismatch))))
2653 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2654 (seq-dispatch sequence2
2655 (let ((end1 (or end1 length1))
2656 (end2 (or end2 length2)))
2657 (declare (type index end1 end2))
2658 (match-vars
2659 (matchify-list (sequence2 start2 length2 end2)
2660 (mumble-list-mismatch))))
2661 (let ((end1 (or end1 length1))
2662 (end2 (or end2 length2)))
2663 (declare (type index end1 end2))
2664 (match-vars
2665 (mumble-mumble-mismatch)))
2666 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2667 (apply #'sb!sequence:mismatch sequence1 sequence2 args)))
2670 ;;; search comparison functions
2672 (eval-when (:compile-toplevel :execute)
2674 ;;; Compare two elements and return if they don't match.
2675 (sb!xc:defmacro compare-elements (elt1 elt2)
2676 `(if test-not
2677 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2678 (return nil)
2680 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2681 (return nil)
2682 t)))
2684 (sb!xc:defmacro search-compare-list-list (main sub)
2685 `(do ((main ,main (cdr main))
2686 (jndex start1 (1+ jndex))
2687 (sub (nthcdr start1 ,sub) (cdr sub)))
2688 ((or (endp main) (endp sub) (<= end1 jndex))
2690 (declare (type (integer 0) jndex))
2691 (compare-elements (car sub) (car main))))
2693 (sb!xc:defmacro search-compare-list-vector (main sub)
2694 `(do ((main ,main (cdr main))
2695 (index start1 (1+ index)))
2696 ((or (endp main) (= index end1)) t)
2697 (compare-elements (aref ,sub index) (car main))))
2699 (sb!xc:defmacro search-compare-vector-list (main sub index)
2700 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2701 (jndex start1 (1+ jndex))
2702 (index ,index (1+ index)))
2703 ((or (<= end1 jndex) (endp sub)) t)
2704 (declare (type (integer 0) jndex))
2705 (compare-elements (car sub) (aref ,main index))))
2707 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2708 `(do ((index ,index (1+ index))
2709 (sub-index start1 (1+ sub-index)))
2710 ((= sub-index end1) t)
2711 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2713 (sb!xc:defmacro search-compare (main-type main sub index)
2714 (if (eq main-type 'list)
2715 `(seq-dispatch ,sub
2716 (search-compare-list-list ,main ,sub)
2717 (search-compare-list-vector ,main ,sub)
2718 ;; KLUDGE: just hack it together so that it works
2719 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2720 `(seq-dispatch ,sub
2721 (search-compare-vector-list ,main ,sub ,index)
2722 (search-compare-vector-vector ,main ,sub ,index)
2723 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2725 ) ; EVAL-WHEN
2727 ;;;; SEARCH
2729 (eval-when (:compile-toplevel :execute)
2731 (sb!xc:defmacro list-search (main sub)
2732 `(do ((main (nthcdr start2 ,main) (cdr main))
2733 (index2 start2 (1+ index2))
2734 (terminus (- end2 (the (integer 0) (- end1 start1))))
2735 (last-match ()))
2736 ((> index2 terminus) last-match)
2737 (declare (type (integer 0) index2))
2738 (if (search-compare list main ,sub index2)
2739 (if from-end
2740 (setq last-match index2)
2741 (return index2)))))
2743 (sb!xc:defmacro vector-search (main sub)
2744 `(do ((index2 start2 (1+ index2))
2745 (terminus (- end2 (the (integer 0) (- end1 start1))))
2746 (last-match ()))
2747 ((> index2 terminus) last-match)
2748 (declare (type (integer 0) index2))
2749 (if (search-compare vector ,main ,sub index2)
2750 (if from-end
2751 (setq last-match index2)
2752 (return index2)))))
2754 ) ; EVAL-WHEN
2756 (define-sequence-traverser search
2757 (sequence1 sequence2 &rest args &key
2758 from-end test test-not start1 end1 start2 end2 key)
2759 (declare (type fixnum start1 start2)
2760 (truly-dynamic-extent args))
2761 (seq-dispatch sequence2
2762 (let ((end1 (or end1 length1))
2763 (end2 (or end2 length2)))
2764 (declare (type index end1 end2))
2765 (list-search sequence2 sequence1))
2766 (let ((end1 (or end1 length1))
2767 (end2 (or end2 length2)))
2768 (declare (type index end1 end2))
2769 (vector-search sequence2 sequence1))
2770 (apply #'sb!sequence:search sequence1 sequence2 args)))
2772 ;;; FIXME: this was originally in array.lisp; it might be better to
2773 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2774 ;;; a new early-seq.lisp file.
2775 (defun fill-data-vector (vector dimensions initial-contents)
2776 (let ((index 0))
2777 (labels ((frob (axis dims contents)
2778 (cond ((null dims)
2779 (setf (aref vector index) contents)
2780 (incf index))
2782 (unless (typep contents 'sequence)
2783 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2784 sequence, but ~W more layer~:P needed."
2785 contents
2786 (- (length dimensions) axis)))
2787 (unless (= (length contents) (car dims))
2788 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2789 axis ~W is ~W, but ~S is ~W long."
2790 axis (car dims) contents (length contents)))
2791 (sb!sequence:dosequence (content contents)
2792 (frob (1+ axis) (cdr dims) content))))))
2793 (frob 0 dimensions initial-contents))))