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.
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")
23 (eval-when (:compile-toplevel)
25 (defparameter *sequence-keyword-info*
26 ;; (name default supplied-p adjustment new-type)
30 (null (1- most-positive-fixnum))
31 (fixnum (max 0 count))
32 (integer (if (minusp count)
34 (1- most-positive-fixnum))))
35 (mod #.sb!xc:most-positive-fixnum))
36 ,@(mapcan (lambda (names)
37 (destructuring-bind (start end length sequence) names
42 (if (<= 0 ,start ,length)
44 (signal-bounding-indices-bad-error ,sequence
50 (if (or (null ,end) (<= ,start ,end ,length))
51 ;; Defaulting of NIL is done inside the
52 ;; bodies, for ease of sharing with compiler
55 ;; FIXME: defend against non-number non-NIL
58 (signal-bounding-indices-bad-error ,sequence
61 '((start end length sequence)
62 (start1 end1 length1 sequence1)
63 (start2 end2 length2 sequence2)))
66 (and key (%coerce-callable-to-fun key))
70 (%coerce-callable-to-fun test)
74 (and test-not (%coerce-callable-to-fun test-not))
78 (sb!xc:defmacro define-sequence-traverser (name args &body body)
79 (multiple-value-bind (body declarations docstring)
80 (parse-body body :doc-string-allowed t)
81 (collect ((new-args) (new-declarations) (adjustments))
84 ;; FIXME: make this robust. And clean.
87 (adjustments '(length (etypecase sequence
88 (list (length sequence))
89 (vector (length sequence)))))
90 (new-declarations '(type index length)))
93 (adjustments '(length1 (etypecase sequence1
94 (list (length sequence1))
95 (vector (length sequence1)))))
96 (new-declarations '(type index length1)))
99 (adjustments '(length2 (etypecase sequence2
100 (list (length sequence2))
101 (vector (length sequence2)))))
102 (new-declarations '(type index length2)))
103 ((function predicate)
105 (adjustments `(,arg (%coerce-callable-to-fun ,arg))))
106 (t (let ((info (cdr (assoc arg *sequence-keyword-info*))))
108 (destructuring-bind (default supplied-p adjuster type) info
109 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
110 (adjustments `(,arg ,adjuster))
111 (new-declarations `(type ,type ,arg))))
112 (t (new-args arg)))))))
113 `(defun ,name ,(new-args)
114 ,@(when docstring (list docstring))
116 (let* (,@(adjustments))
117 (declare ,@(new-declarations))
120 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
122 ;;; FIXME: It might be worth making three cases here, LIST,
123 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
124 ;;; It tends to make code run faster but be bigger; some benchmarking
125 ;;; is needed to decide.
126 (sb!xc:defmacro seq-dispatch (sequence list-form array-form)
127 `(if (listp ,sequence)
131 (sb!xc:defmacro make-sequence-like (sequence length)
133 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
134 `(if (typep ,sequence 'list)
137 ;; This is only called from places which have already deduced
138 ;; that the SEQUENCE argument is actually a sequence. So
139 ;; this would be a candidate place for (AVER (TYPEP ,SEQUENCE
140 ;; 'VECTOR)), except that this seems to be a performance
143 :element-type (array-element-type ,sequence)))))
145 (sb!xc:defmacro bad-sequence-type-error (type-spec)
146 `(error 'simple-type-error
148 :expected-type '(satisfies is-a-valid-sequence-type-specifier-p)
149 :format-control "~S is a bad type specifier for sequences."
150 :format-arguments (list ,type-spec)))
152 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
153 `(error 'simple-type-error
155 :expected-type (cond ((array-type-p ,type)
156 `(eql ,(car (array-type-dimensions ,type))))
157 ((type= ,type (specifier-type 'null))
161 (t (bug "weird type in S-T-L-M-ERROR")))
162 ;; FIXME: this format control causes ugly printing. There's
163 ;; probably some ~<~@:_~> incantation that would make it
164 ;; nicer. -- CSR, 2002-10-18
165 :format-control "The length requested (~S) does not match the type restriction in ~S."
166 :format-arguments (list ,length (type-specifier ,type))))
168 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
169 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
170 ;; words that give some but not total support to this position in
171 ;; ANSI. Essentially, we are justified in throwing this on
172 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
173 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
175 ;; On the other hand, I'm not sure it deserves to be a type-error,
176 ;; either. -- bem, 2005-08-10
177 `(error 'simple-program-error
178 :format-control "~S is too hairy for sequence functions."
179 :format-arguments (list ,type-spec)))
182 (defun is-a-valid-sequence-type-specifier-p (type)
183 (let ((type (specifier-type type)))
184 (or (csubtypep type (specifier-type 'list))
185 (csubtypep type (specifier-type 'vector)))))
187 ;;; It's possible with some sequence operations to declare the length
188 ;;; of a result vector, and to be safe, we really ought to verify that
189 ;;; the actual result has the declared length.
190 (defun vector-of-checked-length-given-length (vector declared-length)
191 (declare (type vector vector))
192 (declare (type index declared-length))
193 (let ((actual-length (length vector)))
194 (unless (= actual-length declared-length)
195 (error 'simple-type-error
197 :expected-type `(vector ,declared-length)
199 "Vector length (~W) doesn't match declared length (~W)."
200 :format-arguments (list actual-length declared-length))))
202 (defun sequence-of-checked-length-given-type (sequence result-type)
203 (let ((ctype (specifier-type result-type)))
204 (if (not (array-type-p ctype))
206 (let ((declared-length (first (array-type-dimensions ctype))))
207 (if (eq declared-length '*)
209 (vector-of-checked-length-given-length sequence
210 declared-length))))))
212 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
213 (defun signal-index-too-large-error (sequence index)
214 (let* ((length (length sequence))
215 (max-index (and (plusp length)
217 (error 'index-too-large-error
219 :expected-type (if max-index
220 `(integer 0 ,max-index)
221 ;; This seems silly, is there something better?
224 (defun signal-bounding-indices-bad-error (sequence start end)
225 (let ((length (length sequence)))
226 (error 'bounding-indices-bad-error
227 :datum (cons start end)
228 :expected-type `(cons (integer 0 ,length)
229 (or null (integer ,start ,length)))
232 (defun elt (sequence index)
233 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
236 (do ((count index (1- count))
237 (list sequence (cdr list)))
240 (signal-index-too-large-error sequence index)
242 (declare (type (integer 0) count))))
244 (when (>= index (length sequence))
245 (signal-index-too-large-error sequence index))
246 (aref sequence index))))
248 (defun %setelt (sequence index newval)
249 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
252 (do ((count index (1- count))
254 ((= count 0) (rplaca seq newval) newval)
255 (declare (fixnum count))
257 (signal-index-too-large-error sequence index)
258 (setq seq (cdr seq)))))
260 (when (>= index (length sequence))
261 (signal-index-too-large-error sequence index))
262 (setf (aref sequence index) newval))))
264 (defun length (sequence)
265 #!+sb-doc "Return an integer that is the length of SEQUENCE."
267 (vector (length (truly-the vector sequence)))
268 (list (length (truly-the list sequence)))))
270 (defun make-sequence (type length &key (initial-element nil iep))
272 "Return a sequence of the given TYPE and LENGTH, with elements initialized
274 (declare (fixnum length))
275 (let* ((adjusted-type
278 ((eq type 'string) '(vector character))
279 ((eq type 'simple-string) '(simple-array character (*)))
282 ((eq (car type) 'string) `(vector character ,@(cdr type)))
283 ((eq (car type) 'simple-string)
284 `(simple-array character ,(if (cdr type)
289 (type (specifier-type adjusted-type)))
290 (cond ((csubtypep type (specifier-type 'list))
292 ((type= type (specifier-type 'list))
293 (make-list length :initial-element initial-element))
294 ((eq type *empty-type*)
295 (bad-sequence-type-error nil))
296 ((type= type (specifier-type 'null))
299 (sequence-type-length-mismatch-error type length)))
301 (multiple-value-bind (min exactp)
302 (sb!kernel::cons-type-length-info type)
304 (unless (= length min)
305 (sequence-type-length-mismatch-error type length))
306 (unless (>= length min)
307 (sequence-type-length-mismatch-error type length)))
308 (make-list length :initial-element initial-element)))
309 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
310 ;; which may seem strange and non-ideal, but then I'd say
311 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
312 (t (sequence-type-too-hairy (type-specifier type)))))
313 ((csubtypep type (specifier-type 'vector))
315 (;; is it immediately obvious what the result type is?
316 (typep type 'array-type)
318 (aver (= (length (array-type-dimensions type)) 1))
319 (let* ((etype (type-specifier
320 (array-type-specialized-element-type type)))
321 (etype (if (eq etype '*) t etype))
322 (type-length (car (array-type-dimensions type))))
323 (unless (or (eq type-length '*)
324 (= type-length length))
325 (sequence-type-length-mismatch-error type length))
326 ;; FIXME: These calls to MAKE-ARRAY can't be
327 ;; open-coded, as the :ELEMENT-TYPE argument isn't
328 ;; constant. Probably we ought to write a
329 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
332 (make-array length :element-type etype
333 :initial-element initial-element)
334 (make-array length :element-type etype)))))
335 (t (sequence-type-too-hairy (type-specifier type)))))
336 (t (bad-sequence-type-error (type-specifier type))))))
340 ;;;; The support routines for SUBSEQ are used by compiler transforms,
341 ;;;; so we worry about dealing with END being supplied or defaulting
342 ;;;; to NIL at this level.
344 (defun vector-subseq* (sequence start &optional end)
345 (declare (type vector sequence))
346 (declare (type index start))
347 (declare (type (or null index) end))
349 (setf end (length sequence)))
350 (unless (<= 0 start end (length sequence))
351 (signal-bounding-indices-bad-error sequence start end))
352 (do ((old-index start (1+ old-index))
353 (new-index 0 (1+ new-index))
354 (copy (make-sequence-like sequence (- end start))))
355 ((= old-index end) copy)
356 (declare (fixnum old-index new-index))
357 (setf (aref copy new-index)
358 (aref sequence old-index))))
360 (defun list-subseq* (sequence start &optional end)
361 (declare (type list sequence))
362 ;; the INDEX declaration isn't actually mandatory, but it's true for
363 ;; all practical purposes.
364 (declare (type index start))
365 (declare (type (or null index) end))
366 (do ((list sequence (cdr list))
371 ((null list) (if (or (and end (> end index))
373 (signal-bounding-indices-bad-error sequence start end)
374 (return (nreverse result))))
375 ((< index start) nil)
376 ((and end (= index end)) (return (nreverse result)))
377 (t (push (car list) result)))))
379 (defun subseq (sequence start &optional end)
381 "Return a copy of a subsequence of SEQUENCE starting with element number
382 START and continuing to the end of SEQUENCE or the optional END."
383 (seq-dispatch sequence
384 (list-subseq* sequence start end)
385 (vector-subseq* sequence start end)))
389 (eval-when (:compile-toplevel :execute)
391 (sb!xc:defmacro vector-copy-seq (sequence)
392 `(let ((length (length (the vector ,sequence))))
393 (declare (fixnum length))
394 (do ((index 0 (1+ index))
395 (copy (make-sequence-like ,sequence length)))
396 ((= index length) copy)
397 (declare (fixnum index))
398 (setf (aref copy index) (aref ,sequence index)))))
400 (sb!xc:defmacro list-copy-seq (list)
401 `(if (atom ,list) '()
402 (let ((result (cons (car ,list) '()) ))
403 (do ((x (cdr ,list) (cdr x))
405 (cdr (rplacd splice (cons (car x) '() ))) ))
406 ((atom x) (unless (null x)
412 (defun copy-seq (sequence)
413 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
414 (seq-dispatch sequence
415 (list-copy-seq* sequence)
416 (vector-copy-seq* sequence)))
420 (defun list-copy-seq* (sequence)
421 (list-copy-seq sequence))
423 (defun vector-copy-seq* (sequence)
424 (declare (type vector sequence))
425 (vector-copy-seq sequence))
429 (eval-when (:compile-toplevel :execute)
431 (sb!xc:defmacro vector-fill (sequence item start end)
432 `(do ((index ,start (1+ index)))
433 ((= index (the fixnum ,end)) ,sequence)
434 (declare (fixnum index))
435 (setf (aref ,sequence index) ,item)))
437 (sb!xc:defmacro list-fill (sequence item start end)
438 `(do ((current (nthcdr ,start ,sequence) (cdr current))
439 (index ,start (1+ index)))
440 ((or (atom current) (and end (= index (the fixnum ,end))))
442 (declare (fixnum index))
443 (rplaca current ,item)))
447 ;;; The support routines for FILL are used by compiler transforms, so we
448 ;;; worry about dealing with END being supplied or defaulting to NIL
451 (defun list-fill* (sequence item start end)
452 (declare (list sequence))
453 (list-fill sequence item start end))
455 (defun vector-fill* (sequence item start end)
456 (declare (vector sequence))
457 (when (null end) (setq end (length sequence)))
458 (vector-fill sequence item start end))
460 (define-sequence-traverser fill (sequence item &key start end)
461 #!+sb-doc "Replace the specified elements of SEQUENCE with ITEM."
462 (seq-dispatch sequence
463 (list-fill* sequence item start end)
464 (vector-fill* sequence item start end)))
468 (eval-when (:compile-toplevel :execute)
470 ;;; If we are copying around in the same vector, be careful not to copy the
471 ;;; same elements over repeatedly. We do this by copying backwards.
472 (sb!xc:defmacro mumble-replace-from-mumble ()
473 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
474 (let ((nelts (min (- target-end target-start)
475 (- source-end source-start))))
476 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
478 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
480 ((= target-index (the fixnum (1- target-start))) target-sequence)
481 (declare (fixnum target-index source-index))
482 ;; disable bounds checking
483 (declare (optimize (safety 0)))
484 (setf (aref target-sequence target-index)
485 (aref source-sequence source-index))))
486 (do ((target-index target-start (1+ target-index))
487 (source-index source-start (1+ source-index)))
488 ((or (= target-index (the fixnum target-end))
489 (= source-index (the fixnum source-end)))
491 (declare (fixnum target-index source-index))
492 ;; disable bounds checking
493 (declare (optimize (safety 0)))
494 (setf (aref target-sequence target-index)
495 (aref source-sequence source-index)))))
497 (sb!xc:defmacro list-replace-from-list ()
498 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
499 (let ((new-elts (subseq source-sequence source-start
500 (+ (the fixnum source-start)
502 (min (- (the fixnum target-end)
503 (the fixnum target-start))
504 (- (the fixnum source-end)
505 (the fixnum source-start))))))))
506 (do ((n new-elts (cdr n))
507 (o (nthcdr target-start target-sequence) (cdr o)))
508 ((null n) target-sequence)
510 (do ((target-index target-start (1+ target-index))
511 (source-index source-start (1+ source-index))
512 (target-sequence-ref (nthcdr target-start target-sequence)
513 (cdr target-sequence-ref))
514 (source-sequence-ref (nthcdr source-start source-sequence)
515 (cdr source-sequence-ref)))
516 ((or (= target-index (the fixnum target-end))
517 (= source-index (the fixnum source-end))
518 (null target-sequence-ref) (null source-sequence-ref))
520 (declare (fixnum target-index source-index))
521 (rplaca target-sequence-ref (car source-sequence-ref)))))
523 (sb!xc:defmacro list-replace-from-mumble ()
524 `(do ((target-index target-start (1+ target-index))
525 (source-index source-start (1+ source-index))
526 (target-sequence-ref (nthcdr target-start target-sequence)
527 (cdr target-sequence-ref)))
528 ((or (= target-index (the fixnum target-end))
529 (= source-index (the fixnum source-end))
530 (null target-sequence-ref))
532 (declare (fixnum source-index target-index))
533 (rplaca target-sequence-ref (aref source-sequence source-index))))
535 (sb!xc:defmacro mumble-replace-from-list ()
536 `(do ((target-index target-start (1+ target-index))
537 (source-index source-start (1+ source-index))
538 (source-sequence (nthcdr source-start source-sequence)
539 (cdr source-sequence)))
540 ((or (= target-index (the fixnum target-end))
541 (= source-index (the fixnum source-end))
542 (null source-sequence))
544 (declare (fixnum target-index source-index))
545 (setf (aref target-sequence target-index) (car source-sequence))))
549 ;;;; The support routines for REPLACE are used by compiler transforms, so we
550 ;;;; worry about dealing with END being supplied or defaulting to NIL
553 (defun list-replace-from-list* (target-sequence source-sequence target-start
554 target-end source-start source-end)
555 (when (null target-end) (setq target-end (length target-sequence)))
556 (when (null source-end) (setq source-end (length source-sequence)))
557 (list-replace-from-list))
559 (defun list-replace-from-vector* (target-sequence source-sequence target-start
560 target-end source-start source-end)
561 (when (null target-end) (setq target-end (length target-sequence)))
562 (when (null source-end) (setq source-end (length source-sequence)))
563 (list-replace-from-mumble))
565 (defun vector-replace-from-list* (target-sequence source-sequence target-start
566 target-end source-start source-end)
567 (when (null target-end) (setq target-end (length target-sequence)))
568 (when (null source-end) (setq source-end (length source-sequence)))
569 (mumble-replace-from-list))
571 (defun vector-replace-from-vector* (target-sequence source-sequence
572 target-start target-end source-start
574 (when (null target-end) (setq target-end (length target-sequence)))
575 (when (null source-end) (setq source-end (length source-sequence)))
576 (mumble-replace-from-mumble))
579 (defun simple-character-string-replace-from-simple-character-string*
580 (target-sequence source-sequence
581 target-start target-end source-start source-end)
582 (declare (type (simple-array character (*)) target-sequence source-sequence))
583 (when (null target-end) (setq target-end (length target-sequence)))
584 (when (null source-end) (setq source-end (length source-sequence)))
585 (mumble-replace-from-mumble))
587 (define-sequence-traverser replace
588 (sequence1 sequence2 &key start1 end1 start2 end2)
590 "The target sequence is destructively modified by copying successive
591 elements into it from the source sequence."
592 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
593 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
594 ;; these things here so that legacy code gets the names it's
595 ;; expecting. We could use &AUX instead :-/.
596 (target-sequence sequence1)
597 (source-sequence sequence2)
598 (target-start start1)
599 (source-start start2)
600 (target-end (or end1 length1))
601 (source-end (or end2 length2)))
602 (seq-dispatch target-sequence
603 (seq-dispatch source-sequence
604 (list-replace-from-list)
605 (list-replace-from-mumble))
606 (seq-dispatch source-sequence
607 (mumble-replace-from-list)
608 (mumble-replace-from-mumble)))))
612 (eval-when (:compile-toplevel :execute)
614 (sb!xc:defmacro vector-reverse (sequence)
615 `(let ((length (length ,sequence)))
616 (declare (fixnum length))
617 (do ((forward-index 0 (1+ forward-index))
618 (backward-index (1- length) (1- backward-index))
619 (new-sequence (make-sequence-like sequence length)))
620 ((= forward-index length) new-sequence)
621 (declare (fixnum forward-index backward-index))
622 (setf (aref new-sequence forward-index)
623 (aref ,sequence backward-index)))))
625 (sb!xc:defmacro list-reverse-macro (sequence)
627 ((endp ,sequence) new-list)
628 (push (pop ,sequence) new-list)))
632 (defun reverse (sequence)
634 "Return a new sequence containing the same elements but in reverse order."
635 (seq-dispatch sequence
636 (list-reverse* sequence)
637 (vector-reverse* sequence)))
641 (defun list-reverse* (sequence)
642 (list-reverse-macro sequence))
644 (defun vector-reverse* (sequence)
645 (vector-reverse sequence))
649 (eval-when (:compile-toplevel :execute)
651 (sb!xc:defmacro vector-nreverse (sequence)
652 `(let ((length (length (the vector ,sequence))))
654 (do ((left-index 0 (1+ left-index))
655 (right-index (1- length) (1- right-index)))
656 ((<= right-index left-index))
657 (declare (type index left-index right-index))
658 (rotatef (aref ,sequence left-index)
659 (aref ,sequence right-index))))
662 (sb!xc:defmacro list-nreverse-macro (list)
663 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
671 (defun list-nreverse* (sequence)
672 (list-nreverse-macro sequence))
674 (defun vector-nreverse* (sequence)
675 (vector-nreverse sequence))
677 (defun nreverse (sequence)
679 "Return a sequence of the same elements in reverse order; the argument
681 (seq-dispatch sequence
682 (list-nreverse* sequence)
683 (vector-nreverse* sequence)))
687 (eval-when (:compile-toplevel :execute)
689 (sb!xc:defmacro concatenate-to-list (sequences)
690 `(let ((result (list nil)))
691 (do ((sequences ,sequences (cdr sequences))
693 ((null sequences) (cdr result))
694 (let ((sequence (car sequences)))
695 ;; FIXME: It appears to me that this and CONCATENATE-TO-MUMBLE
696 ;; could benefit from a DO-SEQUENCE macro.
697 (seq-dispatch sequence
698 (do ((sequence sequence (cdr sequence)))
701 (cdr (rplacd splice (list (car sequence))))))
702 (do ((index 0 (1+ index))
703 (length (length sequence)))
705 (declare (fixnum index length))
708 (list (aref sequence index)))))))))))
710 (sb!xc:defmacro concatenate-to-mumble (output-type-spec sequences)
711 `(do ((seqs ,sequences (cdr seqs))
715 (do ((sequences ,sequences (cdr sequences))
716 (lengths lengths (cdr lengths))
718 (result (make-sequence ,output-type-spec total-length)))
719 ((= index total-length) result)
720 (declare (fixnum index))
721 (let ((sequence (car sequences)))
722 (seq-dispatch sequence
723 (do ((sequence sequence (cdr sequence)))
725 (setf (aref result index) (car sequence))
726 (setq index (1+ index)))
727 (do ((jndex 0 (1+ jndex))
728 (this-length (car lengths)))
729 ((= jndex this-length))
730 (declare (fixnum jndex this-length))
731 (setf (aref result index)
732 (aref sequence jndex))
733 (setq index (1+ index)))))))
734 (let ((length (length (car seqs))))
735 (declare (fixnum length))
736 (setq lengths (nconc lengths (list length)))
737 (setq total-length (+ total-length length)))))
741 (defun concatenate (output-type-spec &rest sequences)
743 "Return a new sequence of all the argument sequences concatenated together
744 which shares no structure with the original argument sequences of the
745 specified OUTPUT-TYPE-SPEC."
746 (let ((type (specifier-type output-type-spec)))
748 ((csubtypep type (specifier-type 'list))
750 ((type= type (specifier-type 'list))
751 (apply #'concat-to-list* sequences))
752 ((eq type *empty-type*)
753 (bad-sequence-type-error nil))
754 ((type= type (specifier-type 'null))
755 (if (every (lambda (x) (or (null x)
756 (and (vectorp x) (= (length x) 0))))
759 (sequence-type-length-mismatch-error
761 ;; FIXME: circular list issues.
762 (reduce #'+ sequences :key #'length))))
764 (multiple-value-bind (min exactp)
765 (sb!kernel::cons-type-length-info type)
766 (let ((length (reduce #'+ sequences :key #'length)))
768 (unless (= length min)
769 (sequence-type-length-mismatch-error type length))
770 (unless (>= length min)
771 (sequence-type-length-mismatch-error type length)))
772 (apply #'concat-to-list* sequences))))
773 (t (sequence-type-too-hairy (type-specifier type)))))
774 ((csubtypep type (specifier-type 'vector))
775 (apply #'concat-to-simple* output-type-spec sequences))
777 (bad-sequence-type-error output-type-spec)))))
780 ;;; FIXME: These are weird. They're never called anywhere except in
781 ;;; CONCATENATE. It seems to me that the macros ought to just
782 ;;; be expanded directly in CONCATENATE, or in CONCATENATE-STRING
783 ;;; and CONCATENATE-LIST variants. Failing that, these ought to be local
784 ;;; functions (FLET).
785 (defun concat-to-list* (&rest sequences)
786 (concatenate-to-list sequences))
787 (defun concat-to-simple* (type &rest sequences)
788 (concatenate-to-mumble type sequences))
790 ;;;; MAP and MAP-INTO
792 ;;; helper functions to handle arity-1 subcases of MAP
793 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
794 (declaim (ftype (function (function sequence) simple-vector)
795 %map-simple-vector-arity-1))
796 (macrolet ((dosequence ((i sequence) &body body)
797 (once-only ((sequence sequence))
798 `(etypecase ,sequence
799 (list (dolist (,i ,sequence) ,@body))
800 (simple-vector (dovector (,i sequence) ,@body))
801 (vector (dovector (,i sequence) ,@body))))))
802 (defun %map-to-list-arity-1 (fun sequence)
803 (let ((reversed-result nil)
804 (really-fun (%coerce-callable-to-fun fun)))
805 (dosequence (element sequence)
806 (push (funcall really-fun element)
808 (nreverse reversed-result)))
809 (defun %map-to-simple-vector-arity-1 (fun sequence)
810 (let ((result (make-array (length sequence)))
812 (really-fun (%coerce-callable-to-fun fun)))
813 (declare (type index index))
814 (dosequence (element sequence)
815 (setf (aref result index)
816 (funcall really-fun element))
819 (defun %map-for-effect-arity-1 (fun sequence)
820 (let ((really-fun (%coerce-callable-to-fun fun)))
821 (dosequence (element sequence)
822 (funcall really-fun element)))
825 ;;; helper functions to handle arity-N subcases of MAP
827 ;;; KLUDGE: This is hairier, and larger, than need be, because we
828 ;;; don't have DYNAMIC-EXTENT. With DYNAMIC-EXTENT, we could define
829 ;;; %MAP-FOR-EFFECT, and then implement the
830 ;;; other %MAP-TO-FOO functions reasonably efficiently by passing closures to
831 ;;; %MAP-FOR-EFFECT. (DYNAMIC-EXTENT would help a little by avoiding
832 ;;; consing each closure, and would help a lot by allowing us to define
833 ;;; a closure (LAMBDA (&REST REST) <do something with (APPLY FUN REST)>)
834 ;;; with the REST list allocated with DYNAMIC-EXTENT. -- WHN 20000920
835 (macrolet (;; Execute BODY in a context where the machinery for
836 ;; UPDATED-MAP-APPLY-ARGS has been set up.
837 (with-map-state (sequences &body body)
838 `(let* ((%sequences ,sequences)
839 (%iters (mapcar (lambda (sequence)
844 (%apply-args (make-list (length %sequences))))
845 (declare (type list %sequences %iters %apply-args))
847 ;; Return a list of args to pass to APPLY for the next
848 ;; function call in the mapping, or NIL if no more function
849 ;; calls should be made (because we've reached the end of a
851 (updated-map-apply-args ()
852 '(do ((in-sequences %sequences (cdr in-sequences))
853 (in-iters %iters (cdr in-iters))
854 (in-apply-args %apply-args (cdr in-apply-args)))
857 (declare (type list in-sequences in-iters in-apply-args))
858 (let ((i (car in-iters)))
859 (declare (type (or list index) i))
861 (if (null i) ; if end of this sequence
863 (setf (car in-apply-args) (car i)
864 (car in-iters) (cdr i)))
865 (let ((v (the vector (car in-sequences))))
866 (if (>= i (length v)) ; if end of this sequence
868 (setf (car in-apply-args) (aref v i)
869 (car in-iters) (1+ i)))))))))
870 (defun %map-to-list (func sequences)
871 (declare (type function func))
872 (declare (type list sequences))
873 (with-map-state sequences
874 (loop with updated-map-apply-args
875 while (setf updated-map-apply-args (updated-map-apply-args))
876 collect (apply func updated-map-apply-args))))
877 (defun %map-to-vector (output-type-spec func sequences)
878 (declare (type function func))
879 (declare (type list sequences))
880 (let ((min-len (with-map-state sequences
881 (do ((counter 0 (1+ counter)))
882 ;; Note: Doing everything in
883 ;; UPDATED-MAP-APPLY-ARGS here is somewhat
884 ;; wasteful; we even do some extra consing.
885 ;; And stepping over every element of
886 ;; VECTORs, instead of just grabbing their
887 ;; LENGTH, is also wasteful. But it's easy
888 ;; and safe. (If you do rewrite it, please
889 ;; try to make sure that
890 ;; (MAP NIL #'F SOME-CIRCULAR-LIST #(1))
891 ;; does the right thing.)
892 ((not (updated-map-apply-args))
894 (declare (type index counter))))))
895 (declare (type index min-len))
896 (with-map-state sequences
897 (let ((result (make-sequence output-type-spec min-len))
899 (declare (type index index))
900 (loop with updated-map-apply-args
901 while (setf updated-map-apply-args (updated-map-apply-args))
903 (setf (aref result index)
904 (apply func updated-map-apply-args))
907 (defun %map-for-effect (func sequences)
908 (declare (type function func))
909 (declare (type list sequences))
910 (with-map-state sequences
911 (loop with updated-map-apply-args
912 while (setf updated-map-apply-args (updated-map-apply-args))
914 (apply func updated-map-apply-args))
917 "FUNCTION must take as many arguments as there are sequences provided.
918 The result is a sequence of type OUTPUT-TYPE-SPEC such that element I
919 is the result of applying FUNCTION to element I of each of the argument
922 ;;; %MAP is just MAP without the final just-to-be-sure check that
923 ;;; length of the output sequence matches any length specified
925 (defun %map (result-type function first-sequence &rest more-sequences)
926 (let ((really-fun (%coerce-callable-to-fun function))
927 (type (specifier-type result-type)))
928 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
929 ;; it into something which can be DEFTRANSFORMed away. (It's
930 ;; fairly important to handle this case efficiently, since
931 ;; quantifiers like SOME are transformed into this case, and since
932 ;; there's no consing overhead to dwarf our inefficiency.)
933 (if (and (null more-sequences)
935 (%map-for-effect-arity-1 really-fun first-sequence)
936 ;; Otherwise, use the industrial-strength full-generality
937 ;; approach, consing O(N-ARGS) temporary storage (which can have
938 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
939 (let ((sequences (cons first-sequence more-sequences)))
941 ((eq type *empty-type*) (%map-for-effect really-fun sequences))
942 ((csubtypep type (specifier-type 'list))
943 (%map-to-list really-fun sequences))
944 ((csubtypep type (specifier-type 'vector))
945 (%map-to-vector result-type really-fun sequences))
947 (bad-sequence-type-error result-type)))))))
949 (defun map (result-type function first-sequence &rest more-sequences)
956 ;;; KLUDGE: MAP has been rewritten substantially since the fork from
957 ;;; CMU CL in order to give reasonable performance, but this
958 ;;; implementation of MAP-INTO still has the same problems as the old
959 ;;; MAP code. Ideally, MAP-INTO should be rewritten to be efficient in
960 ;;; the same way that the corresponding cases of MAP have been
961 ;;; rewritten. Instead of doing it now, though, it's easier to wait
962 ;;; until we have DYNAMIC-EXTENT, at which time it should become
963 ;;; extremely easy to define a reasonably efficient MAP-INTO in terms
964 ;;; of (MAP NIL ..). -- WHN 20000920
965 (defun map-into (result-sequence function &rest sequences)
967 (and (arrayp result-sequence)
968 (array-has-fill-pointer-p result-sequence)))
971 (array-dimension result-sequence 0)
972 (length result-sequence))
973 (mapcar #'length sequences))))
976 (setf (fill-pointer result-sequence) len))
978 (let ((really-fun (%coerce-callable-to-fun function)))
980 (setf (elt result-sequence index)
982 (mapcar (lambda (seq) (elt seq index))
988 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
989 ;;; arbitrary sequence arguments, both in the full call case and in
990 ;;; the open code case.
991 (macrolet ((defquantifier (name found-test found-result
992 &key doc (unfound-result (not found-result)))
994 ;; KLUDGE: It would be really nice if we could simply
995 ;; do something like this
996 ;; (declaim (inline ,name))
997 ;; (defun ,name (pred first-seq &rest more-seqs)
999 ;; (flet ((map-me (&rest rest)
1000 ;; (let ((pred-value (apply pred rest)))
1001 ;; (,found-test pred-value
1002 ;; (return-from ,name
1003 ;; ,found-result)))))
1004 ;; (declare (inline map-me))
1005 ;; (apply #'map nil #'map-me first-seq more-seqs)
1006 ;; ,unfound-result))
1007 ;; but Python doesn't seem to be smart enough about
1008 ;; inlining and APPLY to recognize that it can use
1009 ;; the DEFTRANSFORM for MAP in the resulting inline
1010 ;; expansion. I don't have any appetite for deep
1011 ;; compiler hacking right now, so I'll just work
1012 ;; around the apparent problem by using a compiler
1013 ;; macro instead. -- WHN 20000410
1014 (defun ,name (pred first-seq &rest more-seqs)
1016 (flet ((map-me (&rest rest)
1017 (let ((pred-value (apply pred rest)))
1018 (,found-test pred-value
1021 (declare (inline map-me))
1022 (apply #'map nil #'map-me first-seq more-seqs)
1024 ;; KLUDGE: It would be more obviously correct -- but
1025 ;; also significantly messier -- for PRED-VALUE to be
1026 ;; a gensym. However, a private symbol really does
1027 ;; seem to be good enough; and anyway the really
1028 ;; obviously correct solution is to make Python smart
1029 ;; enough that we can use an inline function instead
1030 ;; of a compiler macro (as above). -- WHN 20000410
1032 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
1033 ;; important for performance, and it'd be good to have
1034 ;; it be visible throughout the compilation of all the
1035 ;; target SBCL code. That could be done by defining
1036 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
1037 ;; moving this DEFQUANTIFIER stuff (and perhaps other
1038 ;; inline definitions in seq.lisp as well) into a new
1039 ;; seq.lisp, and moving remaining target-only stuff
1040 ;; from the old seq.lisp into target-seq.lisp.
1041 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
1042 (let ((elements (make-gensym-list (1+ (length more-seqs))))
1043 (blockname (gensym "BLOCK")))
1044 (once-only ((pred pred))
1047 (lambda (,@elements)
1048 (let ((pred-value (funcall ,pred ,@elements)))
1049 (,',found-test pred-value
1050 (return-from ,blockname
1054 ,',unfound-result)))))))
1055 (defquantifier some when pred-value :unfound-result nil :doc
1056 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1057 possibly to those with index 1, and so on. Return the first
1058 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1059 (defquantifier every unless nil :doc
1060 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1061 possibly to those with index 1, and so on. Return NIL as soon
1062 as any invocation of PREDICATE returns NIL, or T if every invocation
1064 (defquantifier notany when nil :doc
1065 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1066 possibly to those with index 1, and so on. Return NIL as soon
1067 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1068 of any sequence is reached.")
1069 (defquantifier notevery unless t :doc
1070 "Apply PREDICATE to 0-indexed elements of the sequences, then
1071 possibly to those with index 1, and so on. Return T as soon
1072 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1077 (eval-when (:compile-toplevel :execute)
1079 (sb!xc:defmacro mumble-reduce (function
1086 `(do ((index ,start (1+ index))
1087 (value ,initial-value))
1088 ((>= index ,end) value)
1089 (setq value (funcall ,function value
1090 (apply-key ,key (,ref ,sequence index))))))
1092 (sb!xc:defmacro mumble-reduce-from-end (function
1099 `(do ((index (1- ,end) (1- index))
1100 (value ,initial-value)
1101 (terminus (1- ,start)))
1102 ((<= index terminus) value)
1103 (setq value (funcall ,function
1104 (apply-key ,key (,ref ,sequence index))
1107 (sb!xc:defmacro list-reduce (function
1114 `(let ((sequence (nthcdr ,start ,sequence)))
1115 (do ((count (if ,ivp ,start (1+ ,start))
1117 (sequence (if ,ivp sequence (cdr sequence))
1119 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1120 (funcall ,function value (apply-key ,key (car sequence)))))
1121 ((>= count ,end) value))))
1123 (sb!xc:defmacro list-reduce-from-end (function
1130 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1131 (reverse ,sequence))))
1132 (do ((count (if ,ivp ,start (1+ ,start))
1134 (sequence (if ,ivp sequence (cdr sequence))
1136 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1137 (funcall ,function (apply-key ,key (car sequence)) value)))
1138 ((>= count ,end) value))))
1142 (define-sequence-traverser reduce
1143 (function sequence &key key from-end start end (initial-value nil ivp))
1144 (declare (type index start))
1146 (end (or end length)))
1147 (declare (type index start end))
1148 (cond ((= end start)
1149 (if ivp initial-value (funcall function)))
1152 (list-reduce-from-end function sequence key start end
1154 (list-reduce function sequence key start end
1155 initial-value ivp)))
1158 (setq end (1- (the fixnum end)))
1159 (setq initial-value (apply-key key (aref sequence end))))
1160 (mumble-reduce-from-end function sequence key start end
1161 initial-value aref))
1164 (setq initial-value (apply-key key (aref sequence start)))
1165 (setq start (1+ start)))
1166 (mumble-reduce function sequence key start end
1167 initial-value aref)))))
1171 (eval-when (:compile-toplevel :execute)
1173 (sb!xc:defmacro mumble-delete (pred)
1174 `(do ((index start (1+ index))
1177 ((or (= index (the fixnum end)) (= number-zapped count))
1178 (do ((index index (1+ index)) ; Copy the rest of the vector.
1179 (jndex jndex (1+ jndex)))
1180 ((= index (the fixnum length))
1181 (shrink-vector sequence jndex))
1182 (declare (fixnum index jndex))
1183 (setf (aref sequence jndex) (aref sequence index))))
1184 (declare (fixnum index jndex number-zapped))
1185 (setf (aref sequence jndex) (aref sequence index))
1187 (incf number-zapped)
1190 (sb!xc:defmacro mumble-delete-from-end (pred)
1191 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1195 (terminus (1- start)))
1196 ((or (= index terminus) (= number-zapped count))
1197 (do ((losers losers) ; Delete the losers.
1198 (index start (1+ index))
1200 ((or (null losers) (= index (the fixnum end)))
1201 (do ((index index (1+ index)) ; Copy the rest of the vector.
1202 (jndex jndex (1+ jndex)))
1203 ((= index (the fixnum length))
1204 (shrink-vector sequence jndex))
1205 (declare (fixnum index jndex))
1206 (setf (aref sequence jndex) (aref sequence index))))
1207 (declare (fixnum index jndex))
1208 (setf (aref sequence jndex) (aref sequence index))
1209 (if (= index (the fixnum (car losers)))
1212 (declare (fixnum index number-zapped terminus))
1213 (setq this-element (aref sequence index))
1215 (incf number-zapped)
1216 (push index losers))))
1218 (sb!xc:defmacro normal-mumble-delete ()
1221 (not (funcall test-not item (apply-key key (aref sequence index))))
1222 (funcall test item (apply-key key (aref sequence index))))))
1224 (sb!xc:defmacro normal-mumble-delete-from-end ()
1225 `(mumble-delete-from-end
1227 (not (funcall test-not item (apply-key key this-element)))
1228 (funcall test item (apply-key key this-element)))))
1230 (sb!xc:defmacro list-delete (pred)
1231 `(let ((handle (cons nil sequence)))
1232 (do ((current (nthcdr start sequence) (cdr current))
1233 (previous (nthcdr start handle))
1234 (index start (1+ index))
1236 ((or (= index (the fixnum end)) (= number-zapped count))
1238 (declare (fixnum index number-zapped))
1240 (rplacd previous (cdr current))
1241 (incf number-zapped))
1243 (setq previous (cdr previous)))))))
1245 (sb!xc:defmacro list-delete-from-end (pred)
1246 `(let* ((reverse (nreverse (the list sequence)))
1247 (handle (cons nil reverse)))
1248 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1250 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1251 (index start (1+ index))
1253 ((or (= index (the fixnum end)) (= number-zapped count))
1254 (nreverse (cdr handle)))
1255 (declare (fixnum index number-zapped))
1257 (rplacd previous (cdr current))
1258 (incf number-zapped))
1260 (setq previous (cdr previous)))))))
1262 (sb!xc:defmacro normal-list-delete ()
1265 (not (funcall test-not item (apply-key key (car current))))
1266 (funcall test item (apply-key key (car current))))))
1268 (sb!xc:defmacro normal-list-delete-from-end ()
1269 '(list-delete-from-end
1271 (not (funcall test-not item (apply-key key (car current))))
1272 (funcall test item (apply-key key (car current))))))
1276 (define-sequence-traverser delete
1277 (item sequence &key from-end test test-not start
1280 "Return a sequence formed by destructively removing the specified ITEM from
1281 the given SEQUENCE."
1282 (declare (fixnum start))
1283 (let ((end (or end length)))
1284 (declare (type index end))
1285 (seq-dispatch sequence
1287 (normal-list-delete-from-end)
1288 (normal-list-delete))
1290 (normal-mumble-delete-from-end)
1291 (normal-mumble-delete)))))
1293 (eval-when (:compile-toplevel :execute)
1295 (sb!xc:defmacro if-mumble-delete ()
1297 (funcall predicate (apply-key key (aref sequence index)))))
1299 (sb!xc:defmacro if-mumble-delete-from-end ()
1300 `(mumble-delete-from-end
1301 (funcall predicate (apply-key key this-element))))
1303 (sb!xc:defmacro if-list-delete ()
1305 (funcall predicate (apply-key key (car current)))))
1307 (sb!xc:defmacro if-list-delete-from-end ()
1308 '(list-delete-from-end
1309 (funcall predicate (apply-key key (car current)))))
1313 (define-sequence-traverser delete-if
1314 (predicate sequence &key from-end start key end count)
1316 "Return a sequence formed by destructively removing the elements satisfying
1317 the specified PREDICATE from the given SEQUENCE."
1318 (declare (fixnum start))
1319 (let ((end (or end length)))
1320 (declare (type index end))
1321 (seq-dispatch sequence
1323 (if-list-delete-from-end)
1326 (if-mumble-delete-from-end)
1327 (if-mumble-delete)))))
1329 (eval-when (:compile-toplevel :execute)
1331 (sb!xc:defmacro if-not-mumble-delete ()
1333 (not (funcall predicate (apply-key key (aref sequence index))))))
1335 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1336 `(mumble-delete-from-end
1337 (not (funcall predicate (apply-key key this-element)))))
1339 (sb!xc:defmacro if-not-list-delete ()
1341 (not (funcall predicate (apply-key key (car current))))))
1343 (sb!xc:defmacro if-not-list-delete-from-end ()
1344 '(list-delete-from-end
1345 (not (funcall predicate (apply-key key (car current))))))
1349 (define-sequence-traverser delete-if-not
1350 (predicate sequence &key from-end start end key count)
1352 "Return a sequence formed by destructively removing the elements not
1353 satisfying the specified PREDICATE from the given SEQUENCE."
1354 (declare (fixnum start))
1355 (let ((end (or end length)))
1356 (declare (type index end))
1357 (seq-dispatch sequence
1359 (if-not-list-delete-from-end)
1360 (if-not-list-delete))
1362 (if-not-mumble-delete-from-end)
1363 (if-not-mumble-delete)))))
1367 (eval-when (:compile-toplevel :execute)
1369 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1370 ;;; satisfies the predicate.
1371 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1372 `(do ((index ,begin (,bump index))
1374 (do ((index ,left (,bump index))
1375 (result (make-sequence-like sequence length)))
1376 ((= index (the fixnum ,begin)) result)
1377 (declare (fixnum index))
1378 (setf (aref result index) (aref sequence index))))
1382 ((or (= index (the fixnum ,finish))
1383 (= number-zapped count))
1384 (do ((index index (,bump index))
1385 (new-index new-index (,bump new-index)))
1386 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1387 (declare (fixnum index new-index))
1388 (setf (aref result new-index) (aref sequence index))))
1389 (declare (fixnum index new-index number-zapped))
1390 (setq this-element (aref sequence index))
1391 (cond (,pred (incf number-zapped))
1392 (t (setf (aref result new-index) this-element)
1393 (setq new-index (,bump new-index))))))
1395 (sb!xc:defmacro mumble-remove (pred)
1396 `(mumble-remove-macro 1+ 0 start end length ,pred))
1398 (sb!xc:defmacro mumble-remove-from-end (pred)
1399 `(let ((sequence (copy-seq sequence)))
1400 (mumble-delete-from-end ,pred)))
1402 (sb!xc:defmacro normal-mumble-remove ()
1405 (not (funcall test-not item (apply-key key this-element)))
1406 (funcall test item (apply-key key this-element)))))
1408 (sb!xc:defmacro normal-mumble-remove-from-end ()
1409 `(mumble-remove-from-end
1411 (not (funcall test-not item (apply-key key this-element)))
1412 (funcall test item (apply-key key this-element)))))
1414 (sb!xc:defmacro if-mumble-remove ()
1415 `(mumble-remove (funcall predicate (apply-key key this-element))))
1417 (sb!xc:defmacro if-mumble-remove-from-end ()
1418 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1420 (sb!xc:defmacro if-not-mumble-remove ()
1421 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1423 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1424 `(mumble-remove-from-end
1425 (not (funcall predicate (apply-key key this-element)))))
1427 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1429 (sb!xc:defmacro list-remove-macro (pred reverse?)
1430 `(let* ((sequence ,(if reverse?
1431 '(reverse (the list sequence))
1433 (%start ,(if reverse? '(- length end) 'start))
1434 (%end ,(if reverse? '(- length start) 'end))
1436 (results (do ((index 0 (1+ index))
1437 (before-start splice))
1438 ((= index (the fixnum %start)) before-start)
1439 (declare (fixnum index))
1441 (cdr (rplacd splice (list (pop sequence))))))))
1442 (do ((index %start (1+ index))
1445 ((or (= index (the fixnum %end)) (= number-zapped count))
1446 (do ((index index (1+ index)))
1449 '(nreverse (the list (cdr results)))
1451 (declare (fixnum index))
1452 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1453 (declare (fixnum index number-zapped))
1454 (setq this-element (pop sequence))
1456 (setq number-zapped (1+ number-zapped))
1457 (setq splice (cdr (rplacd splice (list this-element))))))))
1459 (sb!xc:defmacro list-remove (pred)
1460 `(list-remove-macro ,pred nil))
1462 (sb!xc:defmacro list-remove-from-end (pred)
1463 `(list-remove-macro ,pred t))
1465 (sb!xc:defmacro normal-list-remove ()
1468 (not (funcall test-not item (apply-key key this-element)))
1469 (funcall test item (apply-key key this-element)))))
1471 (sb!xc:defmacro normal-list-remove-from-end ()
1472 `(list-remove-from-end
1474 (not (funcall test-not item (apply-key key this-element)))
1475 (funcall test item (apply-key key this-element)))))
1477 (sb!xc:defmacro if-list-remove ()
1479 (funcall predicate (apply-key key this-element))))
1481 (sb!xc:defmacro if-list-remove-from-end ()
1482 `(list-remove-from-end
1483 (funcall predicate (apply-key key this-element))))
1485 (sb!xc:defmacro if-not-list-remove ()
1487 (not (funcall predicate (apply-key key this-element)))))
1489 (sb!xc:defmacro if-not-list-remove-from-end ()
1490 `(list-remove-from-end
1491 (not (funcall predicate (apply-key key this-element)))))
1495 (define-sequence-traverser remove
1496 (item sequence &key from-end test test-not start
1499 "Return a copy of SEQUENCE with elements satisfying the test (default is
1500 EQL) with ITEM removed."
1501 (declare (fixnum start))
1502 (let ((end (or end length)))
1503 (declare (type index end))
1504 (seq-dispatch sequence
1506 (normal-list-remove-from-end)
1507 (normal-list-remove))
1509 (normal-mumble-remove-from-end)
1510 (normal-mumble-remove)))))
1512 (define-sequence-traverser remove-if
1513 (predicate sequence &key from-end start end count key)
1515 "Return a copy of sequence with elements satisfying PREDICATE removed."
1516 (declare (fixnum start))
1517 (let ((end (or end length)))
1518 (declare (type index end))
1519 (seq-dispatch sequence
1521 (if-list-remove-from-end)
1524 (if-mumble-remove-from-end)
1525 (if-mumble-remove)))))
1527 (define-sequence-traverser remove-if-not
1528 (predicate sequence &key from-end start end count key)
1530 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1531 (declare (fixnum start))
1532 (let ((end (or end length)))
1533 (declare (type index end))
1534 (seq-dispatch sequence
1536 (if-not-list-remove-from-end)
1537 (if-not-list-remove))
1539 (if-not-mumble-remove-from-end)
1540 (if-not-mumble-remove)))))
1542 ;;;; REMOVE-DUPLICATES
1544 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1545 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1546 ;;; if we look into the already copied structure (from after :start) and see
1547 ;;; the item. If we check from beginning we check into the rest of the
1548 ;;; original list up to the :end marker (this we have to do by running a
1549 ;;; do loop down the list that far and using our test.
1550 (defun list-remove-duplicates* (list test test-not start end key from-end)
1551 (declare (fixnum start))
1552 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1555 (end (or end (length list)))
1556 (hash (and (> (- end start) 20)
1560 (or (eql test #'eql)
1563 (eql test #'equalp))
1564 (make-hash-table :test test :size (- end start)))))
1565 (do ((index 0 (1+ index)))
1567 (declare (fixnum index))
1568 (setq splice (cdr (rplacd splice (list (car current)))))
1569 (setq current (cdr current)))
1571 (do ((index start (1+ index)))
1572 ((or (and end (= index (the fixnum end)))
1574 (declare (fixnum index))
1575 ;; The hash table contains links from values that are
1576 ;; already in result to the cons cell *preceding* theirs
1577 ;; in the list. That is, for each value v in the list,
1578 ;; v and (cadr (gethash v hash)) are equal under TEST.
1579 (let ((prev (gethash (car current) hash)))
1582 (setf (gethash (car current) hash) splice)
1583 (setq splice (cdr (rplacd splice (list (car current))))))
1585 (let* ((old (cdr prev))
1588 (let ((next-val (car next)))
1589 ;; (assert (eq (gethash next-val hash) old))
1590 (setf (cdr prev) next
1591 (gethash next-val hash) prev
1592 (gethash (car current) hash) splice
1593 splice (cdr (rplacd splice (list (car current))))))
1594 (setf (car old) (car current)))))))
1595 (setq current (cdr current)))
1596 (do ((index start (1+ index)))
1597 ((or (and end (= index (the fixnum end)))
1599 (declare (fixnum index))
1600 (if (or (and from-end
1602 (member (apply-key key (car current))
1603 (nthcdr (1+ start) result)
1606 (member (apply-key key (car current))
1607 (nthcdr (1+ start) result)
1611 (not (do ((it (apply-key key (car current)))
1612 (l (cdr current) (cdr l))
1613 (i (1+ index) (1+ i)))
1614 ((or (atom l) (and end (= i (the fixnum end))))
1616 (declare (fixnum i))
1618 (not (funcall test-not
1620 (apply-key key (car l))))
1621 (funcall test it (apply-key key (car l))))
1623 (setq splice (cdr (rplacd splice (list (car current))))))
1624 (setq current (cdr current))))
1627 (setq splice (cdr (rplacd splice (list (car current)))))
1628 (setq current (cdr current)))
1631 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1632 &optional (length (length vector)))
1633 (declare (vector vector) (fixnum start length))
1634 (when (null end) (setf end (length vector)))
1635 (let ((result (make-sequence-like vector length))
1638 (declare (fixnum index jndex))
1641 (setf (aref result index) (aref vector index))
1642 (setq index (1+ index)))
1645 (setq elt (aref vector index))
1646 ;; FIXME: Relying on POSITION allowing both :TEST and :TEST-NOT
1647 ;; arguments simultaneously is a little fragile, since ANSI says
1648 ;; we can't depend on it, so we need to remember to keep that
1649 ;; extension in our implementation. It'd probably be better to
1650 ;; rewrite this to avoid passing both (as
1651 ;; LIST-REMOVE-DUPLICATES* was rewritten ca. sbcl-0.7.12.18).
1652 (unless (or (and from-end
1653 (position (apply-key key elt) result
1654 :start start :end jndex
1655 :test test :test-not test-not :key key))
1657 (position (apply-key key elt) vector
1658 :start (1+ index) :end end
1659 :test test :test-not test-not :key key)))
1660 (setf (aref result jndex) elt)
1661 (setq jndex (1+ jndex)))
1662 (setq index (1+ index)))
1665 (setf (aref result jndex) (aref vector index))
1666 (setq index (1+ index))
1667 (setq jndex (1+ jndex)))
1668 (%shrink-vector result jndex)))
1670 (define-sequence-traverser remove-duplicates
1671 (sequence &key test test-not start end from-end key)
1673 "The elements of SEQUENCE are compared pairwise, and if any two match,
1674 the one occurring earlier is discarded, unless FROM-END is true, in
1675 which case the one later in the sequence is discarded. The resulting
1676 sequence is returned.
1678 The :TEST-NOT argument is deprecated."
1679 (declare (fixnum start))
1680 (seq-dispatch sequence
1682 (list-remove-duplicates* sequence test test-not
1683 start end key from-end))
1684 (vector-remove-duplicates* sequence test test-not
1685 start end key from-end)))
1687 ;;;; DELETE-DUPLICATES
1689 (defun list-delete-duplicates* (list test test-not key from-end start end)
1690 (declare (fixnum start))
1691 (let ((handle (cons nil list)))
1692 (do ((current (nthcdr start list) (cdr current))
1693 (previous (nthcdr start handle))
1694 (index start (1+ index)))
1695 ((or (and end (= index (the fixnum end))) (null current))
1697 (declare (fixnum index))
1698 (if (do ((x (if from-end
1699 (nthcdr (1+ start) handle)
1702 (i (1+ index) (1+ i)))
1704 (and (not from-end) end (= i (the fixnum end)))
1707 (declare (fixnum i))
1709 (not (funcall test-not
1710 (apply-key key (car current))
1711 (apply-key key (car x))))
1713 (apply-key key (car current))
1714 (apply-key key (car x))))
1716 (rplacd previous (cdr current))
1717 (setq previous (cdr previous))))))
1719 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1720 &optional (length (length vector)))
1721 (declare (vector vector) (fixnum start length))
1722 (when (null end) (setf end (length vector)))
1723 (do ((index start (1+ index))
1726 (do ((index index (1+ index)) ; copy the rest of the vector
1727 (jndex jndex (1+ jndex)))
1729 (shrink-vector vector jndex))
1730 (setf (aref vector jndex) (aref vector index))))
1731 (declare (fixnum index jndex))
1732 (setf (aref vector jndex) (aref vector index))
1733 (unless (position (apply-key key (aref vector index)) vector :key key
1734 :start (if from-end start (1+ index)) :test test
1735 :end (if from-end jndex end) :test-not test-not)
1736 (setq jndex (1+ jndex)))))
1738 (define-sequence-traverser delete-duplicates
1739 (sequence &key test test-not start end from-end key)
1741 "The elements of SEQUENCE are examined, and if any two match, one is
1742 discarded. The resulting sequence, which may be formed by destroying the
1743 given sequence, is returned.
1745 The :TEST-NOT argument is deprecated."
1746 (seq-dispatch sequence
1748 (list-delete-duplicates* sequence test test-not key from-end start end))
1749 (vector-delete-duplicates* sequence test test-not key from-end start end)))
1753 (defun list-substitute* (pred new list start end count key test test-not old)
1754 (declare (fixnum start end count))
1755 (let* ((result (list nil))
1758 (list list)) ; Get a local list for a stepper.
1759 (do ((index 0 (1+ index)))
1761 (declare (fixnum index))
1762 (setq splice (cdr (rplacd splice (list (car list)))))
1763 (setq list (cdr list)))
1764 (do ((index start (1+ index)))
1765 ((or (= index end) (null list) (= count 0)))
1766 (declare (fixnum index))
1767 (setq elt (car list))
1776 (funcall test-not old (apply-key key elt)))
1777 (funcall test old (apply-key key elt))))
1778 (if (funcall test (apply-key key elt)))
1779 (if-not (not (funcall test (apply-key key elt)))))
1783 (setq list (cdr list)))
1786 (setq splice (cdr (rplacd splice (list (car list)))))
1787 (setq list (cdr list)))
1790 ;;; Replace old with new in sequence moving from left to right by incrementer
1791 ;;; on each pass through the loop. Called by all three substitute functions.
1792 (defun vector-substitute* (pred new sequence incrementer left right length
1793 start end count key test test-not old)
1794 (declare (fixnum start count end incrementer right))
1795 (let ((result (make-sequence-like sequence length))
1797 (declare (fixnum index))
1800 (setf (aref result index) (aref sequence index))
1801 (setq index (+ index incrementer)))
1803 ((or (= index end) (= count 0)))
1804 (setq elt (aref sequence index))
1805 (setf (aref result index)
1809 (not (funcall test-not old (apply-key key elt)))
1810 (funcall test old (apply-key key elt))))
1811 (if (funcall test (apply-key key elt)))
1812 (if-not (not (funcall test (apply-key key elt)))))
1813 (setq count (1- count))
1816 (setq index (+ index incrementer)))
1819 (setf (aref result index) (aref sequence index))
1820 (setq index (+ index incrementer)))
1823 (eval-when (:compile-toplevel :execute)
1825 (sb!xc:defmacro subst-dispatch (pred)
1826 `(if (listp sequence)
1828 (nreverse (list-substitute* ,pred
1831 (- (the fixnum length)
1833 (- (the fixnum length)
1835 count key test test-not old))
1836 (list-substitute* ,pred
1837 new sequence start end count key test test-not
1840 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
1841 -1 length (1- (the fixnum end))
1842 (1- (the fixnum start))
1843 count key test test-not old)
1844 (vector-substitute* ,pred new sequence 1 0 length length
1845 start end count key test test-not old))))
1849 (define-sequence-traverser substitute
1850 (new old sequence &key from-end test test-not
1851 start count end key)
1853 "Return a sequence of the same kind as SEQUENCE with the same elements,
1854 except that all elements equal to OLD are replaced with NEW."
1855 (declare (fixnum start))
1856 (let ((end (or end length)))
1857 (declare (type index end))
1858 (subst-dispatch 'normal)))
1860 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
1862 (define-sequence-traverser substitute-if
1863 (new predicate sequence &key from-end start end count key)
1865 "Return a sequence of the same kind as SEQUENCE with the same elements
1866 except that all elements satisfying the PRED are replaced with NEW."
1867 (declare (fixnum start))
1868 (let ((end (or end length))
1872 (declare (type index length end))
1873 (subst-dispatch 'if)))
1875 (define-sequence-traverser substitute-if-not
1876 (new predicate sequence &key from-end start end count key)
1878 "Return a sequence of the same kind as SEQUENCE with the same elements
1879 except that all elements not satisfying the PRED are replaced with NEW."
1880 (declare (fixnum start))
1881 (let ((end (or end length))
1885 (declare (type index length end))
1886 (subst-dispatch 'if-not)))
1890 (define-sequence-traverser nsubstitute
1891 (new old sequence &key from-end test test-not
1892 end count key start)
1894 "Return a sequence of the same kind as SEQUENCE with the same elements
1895 except that all elements equal to OLD are replaced with NEW. SEQUENCE
1896 may be destructively modified."
1897 (declare (fixnum start))
1898 (let ((end (or end length)))
1899 (if (listp sequence)
1901 (let ((length (length sequence)))
1902 (nreverse (nlist-substitute*
1903 new old (nreverse (the list sequence))
1904 test test-not (- length end) (- length start)
1906 (nlist-substitute* new old sequence
1907 test test-not start end count key))
1909 (nvector-substitute* new old sequence -1
1910 test test-not (1- end) (1- start) count key)
1911 (nvector-substitute* new old sequence 1
1912 test test-not start end count key)))))
1914 (defun nlist-substitute* (new old sequence test test-not start end count key)
1915 (declare (fixnum start count end))
1916 (do ((list (nthcdr start sequence) (cdr list))
1917 (index start (1+ index)))
1918 ((or (= index end) (null list) (= count 0)) sequence)
1919 (declare (fixnum index))
1921 (not (funcall test-not old (apply-key key (car list))))
1922 (funcall test old (apply-key key (car list))))
1924 (setq count (1- count)))))
1926 (defun nvector-substitute* (new old sequence incrementer
1927 test test-not start end count key)
1928 (declare (fixnum start incrementer count end))
1929 (do ((index start (+ index incrementer)))
1930 ((or (= index end) (= count 0)) sequence)
1931 (declare (fixnum index))
1933 (not (funcall test-not
1935 (apply-key key (aref sequence index))))
1936 (funcall test old (apply-key key (aref sequence index))))
1937 (setf (aref sequence index) new)
1938 (setq count (1- count)))))
1940 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
1942 (define-sequence-traverser nsubstitute-if
1943 (new predicate sequence &key from-end start end count key)
1945 "Return a sequence of the same kind as SEQUENCE with the same elements
1946 except that all elements satisfying PREDICATE are replaced with NEW.
1947 SEQUENCE may be destructively modified."
1948 (declare (fixnum start))
1949 (let ((end (or end length)))
1950 (declare (fixnum end))
1951 (if (listp sequence)
1953 (let ((length (length sequence)))
1954 (nreverse (nlist-substitute-if*
1955 new predicate (nreverse (the list sequence))
1956 (- length end) (- length start) count key)))
1957 (nlist-substitute-if* new predicate sequence
1958 start end count key))
1960 (nvector-substitute-if* new predicate sequence -1
1961 (1- end) (1- start) count key)
1962 (nvector-substitute-if* new predicate sequence 1
1963 start end count key)))))
1965 (defun nlist-substitute-if* (new test sequence start end count key)
1966 (declare (fixnum end))
1967 (do ((list (nthcdr start sequence) (cdr list))
1968 (index start (1+ index)))
1969 ((or (= index end) (null list) (= count 0)) sequence)
1970 (when (funcall test (apply-key key (car list)))
1972 (setq count (1- count)))))
1974 (defun nvector-substitute-if* (new test sequence incrementer
1975 start end count key)
1976 (do ((index start (+ index incrementer)))
1977 ((or (= index end) (= count 0)) sequence)
1978 (when (funcall test (apply-key key (aref sequence index)))
1979 (setf (aref sequence index) new)
1980 (setq count (1- count)))))
1982 (define-sequence-traverser nsubstitute-if-not
1983 (new predicate sequence &key from-end start end count key)
1985 "Return a sequence of the same kind as SEQUENCE with the same elements
1986 except that all elements not satisfying PREDICATE are replaced with NEW.
1987 SEQUENCE may be destructively modified."
1988 (declare (fixnum start))
1989 (let ((end (or end length)))
1990 (declare (fixnum end))
1991 (if (listp sequence)
1993 (let ((length (length sequence)))
1994 (nreverse (nlist-substitute-if-not*
1995 new predicate (nreverse (the list sequence))
1996 (- length end) (- length start) count key)))
1997 (nlist-substitute-if-not* new predicate sequence
1998 start end count key))
2000 (nvector-substitute-if-not* new predicate sequence -1
2001 (1- end) (1- start) count key)
2002 (nvector-substitute-if-not* new predicate sequence 1
2003 start end count key)))))
2005 (defun nlist-substitute-if-not* (new test sequence start end count key)
2006 (declare (fixnum end))
2007 (do ((list (nthcdr start sequence) (cdr list))
2008 (index start (1+ index)))
2009 ((or (= index end) (null list) (= count 0)) sequence)
2010 (when (not (funcall test (apply-key key (car list))))
2014 (defun nvector-substitute-if-not* (new test sequence incrementer
2015 start end count key)
2016 (do ((index start (+ index incrementer)))
2017 ((or (= index end) (= count 0)) sequence)
2018 (when (not (funcall test (apply-key key (aref sequence index))))
2019 (setf (aref sequence index) new)
2022 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2024 (defun effective-find-position-test (test test-not)
2025 (effective-find-position-test test test-not))
2026 (defun effective-find-position-key (key)
2027 (effective-find-position-key key))
2029 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2030 (macrolet (;; shared logic for defining %FIND-POSITION and
2031 ;; %FIND-POSITION-IF in terms of various inlineable cases
2032 ;; of the expression defined in FROB and VECTOR*-FROB
2034 `(etypecase sequence-arg
2035 (list (frob sequence-arg from-end))
2037 (with-array-data ((sequence sequence-arg :offset-var offset)
2039 (end (%check-vector-sequence-bounds
2040 sequence-arg start end)))
2041 (multiple-value-bind (f p)
2042 (macrolet ((frob2 () '(if from-end
2044 (frob sequence nil))))
2046 (simple-vector (frob2))
2047 (simple-base-string (frob2))
2048 (t (vector*-frob sequence))))
2049 (declare (type (or index null) p))
2050 (values f (and p (the index (- p offset))))))))))
2051 (defun %find-position (item sequence-arg from-end start end key test)
2052 (macrolet ((frob (sequence from-end)
2053 `(%find-position item ,sequence
2054 ,from-end start end key test))
2055 (vector*-frob (sequence)
2056 `(%find-position-vector-macro item ,sequence
2057 from-end start end key test)))
2059 (defun %find-position-if (predicate sequence-arg from-end start end key)
2060 (macrolet ((frob (sequence from-end)
2061 `(%find-position-if predicate ,sequence
2062 ,from-end start end key))
2063 (vector*-frob (sequence)
2064 `(%find-position-if-vector-macro predicate ,sequence
2065 from-end start end key)))
2067 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2068 (macrolet ((frob (sequence from-end)
2069 `(%find-position-if-not predicate ,sequence
2070 ,from-end start end key))
2071 (vector*-frob (sequence)
2072 `(%find-position-if-not-vector-macro predicate ,sequence
2073 from-end start end key)))
2076 ;;; the user interface to FIND and POSITION: just interpreter stubs,
2078 (defun find (item sequence &key from-end (start 0) end key test test-not)
2079 ;; FIXME: this can't be the way to go, surely?
2080 (find item sequence :from-end from-end :start start :end end :key key
2081 :test test :test-not test-not))
2082 (defun position (item sequence &key from-end (start 0) end key test test-not)
2083 (position item sequence :from-end from-end :start start :end end :key key
2084 :test test :test-not test-not))
2086 ;;; the user interface to FIND-IF and POSITION-IF, entirely analogous
2087 ;;; to the interface to FIND and POSITION
2088 (defun find-if (predicate sequence &key from-end (start 0) end key)
2089 (find-if predicate sequence :from-end from-end :start start
2091 (defun position-if (predicate sequence &key from-end (start 0) end key)
2092 (position-if predicate sequence :from-end from-end :start start
2095 (defun find-if-not (predicate sequence &key from-end (start 0) end key)
2096 (find-if-not predicate sequence :from-end from-end :start start
2098 (defun position-if-not (predicate sequence &key from-end (start 0) end key)
2099 (position-if-not predicate sequence :from-end from-end :start start
2102 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2104 (eval-when (:compile-toplevel :execute)
2106 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2107 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2108 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2109 `(let ((%start ,(if from-end-p '(1- end) 'start))
2110 (%end ,(if from-end-p '(1- start) 'end)))
2111 (do ((index %start ,next-index)
2113 ((= index (the fixnum %end)) count)
2114 (declare (fixnum index count))
2115 (,(if notp 'unless 'when) ,pred
2116 (setq count (1+ count)))))))
2118 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2119 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2120 `(let ((%start ,(if from-end-p '(- length end) 'start))
2121 (%end ,(if from-end-p '(- length start) 'end))
2122 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2123 (do ((sequence (nthcdr %start ,sequence))
2124 (index %start (1+ index))
2126 ((or (= index (the fixnum %end)) (null sequence)) count)
2127 (declare (fixnum index count))
2128 (,(if notp 'unless 'when) ,pred
2129 (setq count (1+ count)))))))
2134 (define-sequence-traverser count-if (pred sequence &key from-end start end key)
2136 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2137 (declare (fixnum start))
2138 (let ((end (or end length))
2139 (pred (%coerce-callable-to-fun pred)))
2140 (declare (type index end))
2141 (seq-dispatch sequence
2143 (list-count-if nil t pred sequence)
2144 (list-count-if nil nil pred sequence))
2146 (vector-count-if nil t pred sequence)
2147 (vector-count-if nil nil pred sequence)))))
2149 (define-sequence-traverser count-if-not
2150 (pred sequence &key from-end start end key)
2152 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2153 (declare (fixnum start))
2154 (let ((end (or end length))
2155 (pred (%coerce-callable-to-fun pred)))
2156 (declare (type index end))
2157 (seq-dispatch sequence
2159 (list-count-if t t pred sequence)
2160 (list-count-if t nil pred sequence))
2162 (vector-count-if t t pred sequence)
2163 (vector-count-if t nil pred sequence)))))
2165 (define-sequence-traverser count
2166 (item sequence &key from-end start end
2167 key (test #'eql test-p) (test-not nil test-not-p))
2169 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2170 which defaults to EQL."
2171 (declare (fixnum start))
2172 (when (and test-p test-not-p)
2173 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2175 (error ":TEST and :TEST-NOT are both present."))
2176 (let ((end (or end length)))
2177 (declare (type index end))
2178 (let ((%test (if test-not-p
2180 (not (funcall test-not item x)))
2182 (funcall test item x)))))
2183 (seq-dispatch sequence
2185 (list-count-if nil t %test sequence)
2186 (list-count-if nil nil %test sequence))
2188 (vector-count-if nil t %test sequence)
2189 (vector-count-if nil nil %test sequence))))))
2195 (eval-when (:compile-toplevel :execute)
2197 (sb!xc:defmacro match-vars (&rest body)
2198 `(let ((inc (if from-end -1 1))
2199 (start1 (if from-end (1- (the fixnum end1)) start1))
2200 (start2 (if from-end (1- (the fixnum end2)) start2))
2201 (end1 (if from-end (1- (the fixnum start1)) end1))
2202 (end2 (if from-end (1- (the fixnum start2)) end2)))
2203 (declare (fixnum inc start1 start2 end1 end2))
2206 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2207 (declare (ignore end)) ;; ### Should END be used below?
2208 `(let ((,sequence (if from-end
2209 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2210 (reverse (the list ,sequence)))
2211 (nthcdr ,start ,sequence))))
2212 (declare (type list ,sequence))
2217 (eval-when (:compile-toplevel :execute)
2219 (sb!xc:defmacro if-mismatch (elt1 elt2)
2220 `(cond ((= (the fixnum index1) (the fixnum end1))
2221 (return (if (= (the fixnum index2) (the fixnum end2))
2224 (1+ (the fixnum index1))
2225 (the fixnum index1)))))
2226 ((= (the fixnum index2) (the fixnum end2))
2227 (return (if from-end (1+ (the fixnum index1)) index1)))
2229 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2230 (return (if from-end (1+ (the fixnum index1)) index1))))
2231 (t (if (not (funcall test (apply-key key ,elt1)
2232 (apply-key key ,elt2)))
2233 (return (if from-end (1+ (the fixnum index1)) index1))))))
2235 (sb!xc:defmacro mumble-mumble-mismatch ()
2236 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2237 (index2 start2 (+ index2 (the fixnum inc))))
2239 (declare (fixnum index1 index2))
2240 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2242 (sb!xc:defmacro mumble-list-mismatch ()
2243 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2244 (index2 start2 (+ index2 (the fixnum inc))))
2246 (declare (fixnum index1 index2))
2247 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2249 (sb!xc:defmacro list-mumble-mismatch ()
2250 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2251 (index2 start2 (+ index2 (the fixnum inc))))
2253 (declare (fixnum index1 index2))
2254 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2256 (sb!xc:defmacro list-list-mismatch ()
2257 `(do ((sequence1 sequence1)
2258 (sequence2 sequence2)
2259 (index1 start1 (+ index1 (the fixnum inc)))
2260 (index2 start2 (+ index2 (the fixnum inc))))
2262 (declare (fixnum index1 index2))
2263 (if-mismatch (pop sequence1) (pop sequence2))))
2267 (define-sequence-traverser mismatch
2268 (sequence1 sequence2
2269 &key from-end test test-not
2270 start1 end1 start2 end2 key)
2272 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2273 element-wise. If they are of equal length and match in every element, the
2274 result is NIL. Otherwise, the result is a non-negative integer, the index
2275 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2276 if one is shorter than and a matching prefix of the other, the index within
2277 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2278 :FROM-END argument is given, then one plus the index of the rightmost
2279 position in which the sequences differ is returned."
2280 (declare (fixnum start1 start2))
2281 (let* ((end1 (or end1 length1))
2282 (end2 (or end2 length2)))
2283 (declare (type index end1 end2))
2285 (seq-dispatch sequence1
2286 (matchify-list (sequence1 start1 length1 end1)
2287 (seq-dispatch sequence2
2288 (matchify-list (sequence2 start2 length2 end2)
2289 (list-list-mismatch))
2290 (list-mumble-mismatch)))
2291 (seq-dispatch sequence2
2292 (matchify-list (sequence2 start2 length2 end2)
2293 (mumble-list-mismatch))
2294 (mumble-mumble-mismatch))))))
2296 ;;; search comparison functions
2298 (eval-when (:compile-toplevel :execute)
2300 ;;; Compare two elements and return if they don't match.
2301 (sb!xc:defmacro compare-elements (elt1 elt2)
2303 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2306 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2310 (sb!xc:defmacro search-compare-list-list (main sub)
2311 `(do ((main ,main (cdr main))
2312 (jndex start1 (1+ jndex))
2313 (sub (nthcdr start1 ,sub) (cdr sub)))
2314 ((or (endp main) (endp sub) (<= end1 jndex))
2316 (declare (type (integer 0) jndex))
2317 (compare-elements (car sub) (car main))))
2319 (sb!xc:defmacro search-compare-list-vector (main sub)
2320 `(do ((main ,main (cdr main))
2321 (index start1 (1+ index)))
2322 ((or (endp main) (= index end1)) t)
2323 (compare-elements (aref ,sub index) (car main))))
2325 (sb!xc:defmacro search-compare-vector-list (main sub index)
2326 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2327 (jndex start1 (1+ jndex))
2328 (index ,index (1+ index)))
2329 ((or (<= end1 jndex) (endp sub)) t)
2330 (declare (type (integer 0) jndex))
2331 (compare-elements (car sub) (aref ,main index))))
2333 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2334 `(do ((index ,index (1+ index))
2335 (sub-index start1 (1+ sub-index)))
2336 ((= sub-index end1) t)
2337 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2339 (sb!xc:defmacro search-compare (main-type main sub index)
2340 (if (eq main-type 'list)
2342 (search-compare-list-list ,main ,sub)
2343 (search-compare-list-vector ,main ,sub))
2345 (search-compare-vector-list ,main ,sub ,index)
2346 (search-compare-vector-vector ,main ,sub ,index))))
2352 (eval-when (:compile-toplevel :execute)
2354 (sb!xc:defmacro list-search (main sub)
2355 `(do ((main (nthcdr start2 ,main) (cdr main))
2356 (index2 start2 (1+ index2))
2357 (terminus (- end2 (the (integer 0) (- end1 start1))))
2359 ((> index2 terminus) last-match)
2360 (declare (type (integer 0) index2))
2361 (if (search-compare list main ,sub index2)
2363 (setq last-match index2)
2366 (sb!xc:defmacro vector-search (main sub)
2367 `(do ((index2 start2 (1+ index2))
2368 (terminus (- end2 (the (integer 0) (- end1 start1))))
2370 ((> index2 terminus) last-match)
2371 (declare (type (integer 0) index2))
2372 (if (search-compare vector ,main ,sub index2)
2374 (setq last-match index2)
2379 (define-sequence-traverser search
2380 (sequence1 sequence2
2381 &key from-end test test-not
2382 start1 end1 start2 end2 key)
2383 (declare (fixnum start1 start2))
2384 (let ((end1 (or end1 length1))
2385 (end2 (or end2 length2)))
2386 (seq-dispatch sequence2
2387 (list-search sequence2 sequence1)
2388 (vector-search sequence2 sequence1))))
2390 (sb!xc:defmacro string-dispatch ((&rest types) var &body body)
2391 (let ((fun (gensym "STRING-DISPATCH-FUN-")))
2392 `(flet ((,fun (,var)
2394 (declare (inline ,fun))
2396 ,@(loop for type in types
2397 collect `(,type (,fun (the ,type ,var))))))))