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 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
27 ;;; FIXME: It might be worth making three cases here, LIST,
28 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
29 ;;; It tend to make code run faster but be bigger; some benchmarking
30 ;;; is needed to decide.
31 (sb!xc:defmacro seq-dispatch (sequence list-form array-form)
32 `(if (listp ,sequence)
36 (sb!xc:defmacro make-sequence-like (sequence length)
38 "Returns a sequence of the same type as SEQUENCE and the given LENGTH."
39 `(make-sequence-of-type (type-of ,sequence) ,length))
41 (sb!xc:defmacro type-specifier-atom (type)
42 #!+sb-doc "Returns the broad class of which TYPE is a specific subclass."
43 `(if (atom ,type) ,type (car ,type)))
47 ;;; It's possible with some sequence operations to declare the length
48 ;;; of a result vector, and to be safe, we really ought to verify that
49 ;;; the actual result has the declared length.
50 (defun vector-of-checked-length-given-length (vector declared-length)
51 (declare (type vector vector))
52 (declare (type index declared-length))
53 (let ((actual-length (length vector)))
54 (unless (= actual-length declared-length)
55 (error 'simple-type-error
57 :expected-type `(vector ,declared-length)
59 "Vector length (~D) doesn't match declared length (~D)."
60 :format-arguments (list actual-length declared-length))))
62 (defun sequence-of-checked-length-given-type (sequence result-type)
63 (let ((ctype (specifier-type result-type)))
64 (if (not (array-type-p ctype))
66 (let ((declared-length (first (array-type-dimensions ctype))))
67 (if (eq declared-length '*)
69 (vector-of-checked-length-given-length sequence
72 ;;; Given an arbitrary type specifier, return a sane sequence type
73 ;;; specifier that we can directly match.
74 (defun result-type-or-lose (type &optional nil-ok)
75 (let ((type (specifier-type type)))
77 ((eq type *empty-type*)
80 (error 'simple-type-error
82 :expected-type '(or vector cons)
84 "A NIL output type is invalid for this sequence function."
85 :format-arguments ())))
86 ((dolist (seq-type '(list string simple-vector bit-vector))
87 (when (csubtypep type (specifier-type seq-type))
89 ((csubtypep type (specifier-type 'vector))
90 (type-specifier type))
92 (error 'simple-type-error
94 :expected-type 'sequence
96 "~S is not a legal type specifier for sequence functions."
97 :format-arguments (list type))))))
99 (defun signal-index-too-large-error (sequence index)
100 (let* ((length (length sequence))
101 (max-index (and (plusp length) (1- length))))
102 (error 'index-too-large-error
104 :expected-type (if max-index
105 `(integer 0 ,max-index)
106 ;; This seems silly, is there something better?
107 '(integer (0) (0))))))
109 (defun make-sequence-of-type (type length)
110 #!+sb-doc "Returns a sequence of the given TYPE and LENGTH."
111 (declare (fixnum length))
112 (case (type-specifier-atom type)
113 (list (make-list length))
114 ((bit-vector simple-bit-vector) (make-array length :element-type '(mod 2)))
115 ((string simple-string base-string simple-base-string)
116 (make-string length))
117 (simple-vector (make-array length))
118 ((array simple-array vector)
120 (make-array length :element-type (cadr type))
121 (make-array length)))
123 (make-sequence-of-type (result-type-or-lose type) length))))
125 (defun elt (sequence index)
126 #!+sb-doc "Returns the element of SEQUENCE specified by INDEX."
129 (do ((count index (1- count))
130 (list sequence (cdr list)))
133 (signal-index-too-large-error sequence index)
135 (declare (type (integer 0) count))))
137 (when (>= index (length sequence))
138 (signal-index-too-large-error sequence index))
139 (aref sequence index))))
141 (defun %setelt (sequence index newval)
142 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
145 (do ((count index (1- count))
147 ((= count 0) (rplaca seq newval) newval)
148 (declare (fixnum count))
150 (signal-index-too-large-error sequence index)
151 (setq seq (cdr seq)))))
153 (when (>= index (length sequence))
154 (signal-index-too-large-error sequence index))
155 (setf (aref sequence index) newval))))
157 (defun length (sequence)
158 #!+sb-doc "Return an integer that is the length of SEQUENCE."
160 (vector (length (truly-the vector sequence)))
161 (list (length (truly-the list sequence)))))
163 (defun make-sequence (type length &key (initial-element NIL iep))
165 "Return a sequence of the given TYPE and LENGTH, with elements initialized
166 to :INITIAL-ELEMENT."
167 (declare (fixnum length))
168 (let ((type (specifier-type type)))
169 (cond ((csubtypep type (specifier-type 'list))
170 (make-list length :initial-element initial-element))
171 ((csubtypep type (specifier-type 'string))
173 (make-string length :initial-element initial-element)
174 (make-string length)))
175 ((csubtypep type (specifier-type 'simple-vector))
176 (make-array length :initial-element initial-element))
177 ((csubtypep type (specifier-type 'bit-vector))
179 (make-array length :element-type '(mod 2)
180 :initial-element initial-element)
181 (make-array length :element-type '(mod 2))))
182 ((csubtypep type (specifier-type 'vector))
183 (if (typep type 'array-type)
184 (let ((etype (type-specifier
185 (array-type-specialized-element-type type)))
186 (vlen (car (array-type-dimensions type))))
187 (if (and (numberp vlen) (/= vlen length))
188 (error 'simple-type-error
189 ;; These two are under-specified by ANSI.
190 :datum (type-specifier type)
191 :expected-type (type-specifier type)
193 "The length of ~S does not match the specified ~
196 (list (type-specifier type) length)))
198 (make-array length :element-type etype
199 :initial-element initial-element)
200 (make-array length :element-type etype)))
201 (make-array length :initial-element initial-element)))
202 (t (error 'simple-type-error
204 :expected-type 'sequence
205 :format-control "~S is a bad type specifier for sequences."
206 :format-arguments (list type))))))
210 ;;;; The support routines for SUBSEQ are used by compiler transforms, so we
211 ;;;; worry about dealing with END being supplied or defaulting to NIL
214 (defun vector-subseq* (sequence start &optional end)
215 (declare (type vector sequence))
216 (declare (type fixnum start))
217 (declare (type (or null fixnum) end))
218 (when (null end) (setf end (length sequence)))
219 (do ((old-index start (1+ old-index))
220 (new-index 0 (1+ new-index))
221 (copy (make-sequence-like sequence (- end start))))
222 ((= old-index end) copy)
223 (declare (fixnum old-index new-index))
224 (setf (aref copy new-index) (aref sequence old-index))))
226 (defun list-subseq* (sequence start &optional end)
227 (declare (type list sequence))
228 (declare (type fixnum start))
229 (declare (type (or null fixnum) end))
230 (if (and end (>= start (the fixnum end)))
232 (let* ((groveled (nthcdr start sequence))
233 (result (list (car groveled))))
235 (do ((list (cdr groveled) (cdr list))
236 (splice result (cdr (rplacd splice (list (car list)))))
237 (index (1+ start) (1+ index)))
238 ((or (atom list) (and end (= index (the fixnum end))))
240 (declare (fixnum index)))
243 ;;; SUBSEQ cannot default end to the length of sequence since it is not
244 ;;; an error to supply nil for its value. We must test for end being nil
245 ;;; in the body of the function, and this is actually done in the support
246 ;;; routines for other reasons (see above).
247 (defun subseq (sequence start &optional end)
249 "Returns a copy of a subsequence of SEQUENCE starting with element number
250 START and continuing to the end of SEQUENCE or the optional END."
251 (seq-dispatch sequence
252 (list-subseq* sequence start end)
253 (vector-subseq* sequence start end)))
257 (eval-when (:compile-toplevel :execute)
259 (sb!xc:defmacro vector-copy-seq (sequence type)
260 `(let ((length (length (the vector ,sequence))))
261 (declare (fixnum length))
262 (do ((index 0 (1+ index))
263 (copy (make-sequence-of-type ,type length)))
264 ((= index length) copy)
265 (declare (fixnum index))
266 (setf (aref copy index) (aref ,sequence index)))))
268 (sb!xc:defmacro list-copy-seq (list)
269 `(if (atom ,list) '()
270 (let ((result (cons (car ,list) '()) ))
271 (do ((x (cdr ,list) (cdr x))
273 (cdr (rplacd splice (cons (car x) '() ))) ))
274 ((atom x) (unless (null x)
280 (defun copy-seq (sequence)
281 #!+sb-doc "Returns a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
282 (seq-dispatch sequence
283 (list-copy-seq* sequence)
284 (vector-copy-seq* sequence)))
288 (defun list-copy-seq* (sequence)
289 (list-copy-seq sequence))
291 (defun vector-copy-seq* (sequence)
292 (vector-copy-seq sequence (type-of sequence)))
296 (eval-when (:compile-toplevel :execute)
298 (sb!xc:defmacro vector-fill (sequence item start end)
299 `(do ((index ,start (1+ index)))
300 ((= index (the fixnum ,end)) ,sequence)
301 (declare (fixnum index))
302 (setf (aref ,sequence index) ,item)))
304 (sb!xc:defmacro list-fill (sequence item start end)
305 `(do ((current (nthcdr ,start ,sequence) (cdr current))
306 (index ,start (1+ index)))
307 ((or (atom current) (and end (= index (the fixnum ,end))))
309 (declare (fixnum index))
310 (rplaca current ,item)))
314 ;;; The support routines for FILL are used by compiler transforms, so we
315 ;;; worry about dealing with END being supplied or defaulting to NIL
318 (defun list-fill* (sequence item start end)
319 (declare (list sequence))
320 (list-fill sequence item start end))
322 (defun vector-fill* (sequence item start end)
323 (declare (vector sequence))
324 (when (null end) (setq end (length sequence)))
325 (vector-fill sequence item start end))
327 ;;; FILL cannot default end to the length of sequence since it is not
328 ;;; an error to supply nil for its value. We must test for end being nil
329 ;;; in the body of the function, and this is actually done in the support
330 ;;; routines for other reasons (see above).
331 (defun fill (sequence item &key (start 0) end)
332 #!+sb-doc "Replace the specified elements of SEQUENCE with ITEM."
333 (seq-dispatch sequence
334 (list-fill* sequence item start end)
335 (vector-fill* sequence item start end)))
339 (eval-when (:compile-toplevel :execute)
341 ;;; If we are copying around in the same vector, be careful not to copy the
342 ;;; same elements over repeatedly. We do this by copying backwards.
343 (sb!xc:defmacro mumble-replace-from-mumble ()
344 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
345 (let ((nelts (min (- target-end target-start)
346 (- source-end source-start))))
347 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
349 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
351 ((= target-index (the fixnum (1- target-start))) target-sequence)
352 (declare (fixnum target-index source-index))
353 (setf (aref target-sequence target-index)
354 (aref source-sequence source-index))))
355 (do ((target-index target-start (1+ target-index))
356 (source-index source-start (1+ source-index)))
357 ((or (= target-index (the fixnum target-end))
358 (= source-index (the fixnum source-end)))
360 (declare (fixnum target-index source-index))
361 (setf (aref target-sequence target-index)
362 (aref source-sequence source-index)))))
364 (sb!xc:defmacro list-replace-from-list ()
365 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
366 (let ((new-elts (subseq source-sequence source-start
367 (+ (the fixnum source-start)
369 (min (- (the fixnum target-end)
370 (the fixnum target-start))
371 (- (the fixnum source-end)
372 (the fixnum source-start))))))))
373 (do ((n new-elts (cdr n))
374 (o (nthcdr target-start target-sequence) (cdr o)))
375 ((null n) target-sequence)
377 (do ((target-index target-start (1+ target-index))
378 (source-index source-start (1+ source-index))
379 (target-sequence-ref (nthcdr target-start target-sequence)
380 (cdr target-sequence-ref))
381 (source-sequence-ref (nthcdr source-start source-sequence)
382 (cdr source-sequence-ref)))
383 ((or (= target-index (the fixnum target-end))
384 (= source-index (the fixnum source-end))
385 (null target-sequence-ref) (null source-sequence-ref))
387 (declare (fixnum target-index source-index))
388 (rplaca target-sequence-ref (car source-sequence-ref)))))
390 (sb!xc:defmacro list-replace-from-mumble ()
391 `(do ((target-index target-start (1+ target-index))
392 (source-index source-start (1+ source-index))
393 (target-sequence-ref (nthcdr target-start target-sequence)
394 (cdr target-sequence-ref)))
395 ((or (= target-index (the fixnum target-end))
396 (= source-index (the fixnum source-end))
397 (null target-sequence-ref))
399 (declare (fixnum source-index target-index))
400 (rplaca target-sequence-ref (aref source-sequence source-index))))
402 (sb!xc:defmacro mumble-replace-from-list ()
403 `(do ((target-index target-start (1+ target-index))
404 (source-index source-start (1+ source-index))
405 (source-sequence (nthcdr source-start source-sequence)
406 (cdr source-sequence)))
407 ((or (= target-index (the fixnum target-end))
408 (= source-index (the fixnum source-end))
409 (null source-sequence))
411 (declare (fixnum target-index source-index))
412 (setf (aref target-sequence target-index) (car source-sequence))))
416 ;;;; The support routines for REPLACE are used by compiler transforms, so we
417 ;;;; worry about dealing with END being supplied or defaulting to NIL
420 (defun list-replace-from-list* (target-sequence source-sequence target-start
421 target-end source-start source-end)
422 (when (null target-end) (setq target-end (length target-sequence)))
423 (when (null source-end) (setq source-end (length source-sequence)))
424 (list-replace-from-list))
426 (defun list-replace-from-vector* (target-sequence source-sequence target-start
427 target-end source-start source-end)
428 (when (null target-end) (setq target-end (length target-sequence)))
429 (when (null source-end) (setq source-end (length source-sequence)))
430 (list-replace-from-mumble))
432 (defun vector-replace-from-list* (target-sequence source-sequence target-start
433 target-end source-start source-end)
434 (when (null target-end) (setq target-end (length target-sequence)))
435 (when (null source-end) (setq source-end (length source-sequence)))
436 (mumble-replace-from-list))
438 (defun vector-replace-from-vector* (target-sequence source-sequence
439 target-start target-end source-start
441 (when (null target-end) (setq target-end (length target-sequence)))
442 (when (null source-end) (setq source-end (length source-sequence)))
443 (mumble-replace-from-mumble))
445 ;;; REPLACE cannot default END arguments to the length of SEQUENCE since it
446 ;;; is not an error to supply NIL for their values. We must test for ENDs
447 ;;; being NIL in the body of the function.
448 (defun replace (target-sequence source-sequence &key
449 ((:start1 target-start) 0)
451 ((:start2 source-start) 0)
452 ((:end2 source-end)))
454 "The target sequence is destructively modified by copying successive
455 elements into it from the source sequence."
456 (let ((target-end (or target-end (length target-sequence)))
457 (source-end (or source-end (length source-sequence))))
458 (seq-dispatch target-sequence
459 (seq-dispatch source-sequence
460 (list-replace-from-list)
461 (list-replace-from-mumble))
462 (seq-dispatch source-sequence
463 (mumble-replace-from-list)
464 (mumble-replace-from-mumble)))))
468 (eval-when (:compile-toplevel :execute)
470 (sb!xc:defmacro vector-reverse (sequence type)
471 `(let ((length (length ,sequence)))
472 (declare (fixnum length))
473 (do ((forward-index 0 (1+ forward-index))
474 (backward-index (1- length) (1- backward-index))
475 (new-sequence (make-sequence-of-type ,type length)))
476 ((= forward-index length) new-sequence)
477 (declare (fixnum forward-index backward-index))
478 (setf (aref new-sequence forward-index)
479 (aref ,sequence backward-index)))))
481 (sb!xc:defmacro list-reverse-macro (sequence)
483 ((atom ,sequence) new-list)
484 (push (pop ,sequence) new-list)))
488 (defun reverse (sequence)
490 "Returns a new sequence containing the same elements but in reverse order."
491 (seq-dispatch sequence
492 (list-reverse* sequence)
493 (vector-reverse* sequence)))
497 (defun list-reverse* (sequence)
498 (list-reverse-macro sequence))
500 (defun vector-reverse* (sequence)
501 (vector-reverse sequence (type-of sequence)))
505 (eval-when (:compile-toplevel :execute)
507 (sb!xc:defmacro vector-nreverse (sequence)
508 `(let ((length (length (the vector ,sequence))))
509 (declare (fixnum length))
510 (do ((left-index 0 (1+ left-index))
511 (right-index (1- length) (1- right-index))
512 (half-length (truncate length 2)))
513 ((= left-index half-length) ,sequence)
514 (declare (fixnum left-index right-index half-length))
515 (rotatef (aref ,sequence left-index)
516 (aref ,sequence right-index)))))
518 (sb!xc:defmacro list-nreverse-macro (list)
519 `(do ((1st (cdr ,list) (if (atom 1st) 1st (cdr 1st)))
527 (defun list-nreverse* (sequence)
528 (list-nreverse-macro sequence))
530 (defun vector-nreverse* (sequence)
531 (vector-nreverse sequence))
533 (defun nreverse (sequence)
535 "Returns a sequence of the same elements in reverse order; the argument
537 (seq-dispatch sequence
538 (list-nreverse* sequence)
539 (vector-nreverse* sequence)))
543 (eval-when (:compile-toplevel :execute)
545 (sb!xc:defmacro concatenate-to-list (sequences)
546 `(let ((result (list nil)))
547 (do ((sequences ,sequences (cdr sequences))
549 ((null sequences) (cdr result))
550 (let ((sequence (car sequences)))
551 ;; FIXME: It appears to me that this and CONCATENATE-TO-MUMBLE
552 ;; could benefit from a DO-SEQUENCE macro.
553 (seq-dispatch sequence
554 (do ((sequence sequence (cdr sequence)))
557 (cdr (rplacd splice (list (car sequence))))))
558 (do ((index 0 (1+ index))
559 (length (length sequence)))
561 (declare (fixnum index length))
564 (list (aref sequence index)))))))))))
566 (sb!xc:defmacro concatenate-to-mumble (output-type-spec sequences)
567 `(do ((seqs ,sequences (cdr seqs))
571 (do ((sequences ,sequences (cdr sequences))
572 (lengths lengths (cdr lengths))
574 (result (make-sequence-of-type ,output-type-spec total-length)))
575 ((= index total-length) result)
576 (declare (fixnum index))
577 (let ((sequence (car sequences)))
578 (seq-dispatch sequence
579 (do ((sequence sequence (cdr sequence)))
581 (setf (aref result index) (car sequence))
582 (setq index (1+ index)))
583 (do ((jndex 0 (1+ jndex))
584 (this-length (car lengths)))
585 ((= jndex this-length))
586 (declare (fixnum jndex this-length))
587 (setf (aref result index)
588 (aref sequence jndex))
589 (setq index (1+ index)))))))
590 (let ((length (length (car seqs))))
591 (declare (fixnum length))
592 (setq lengths (nconc lengths (list length)))
593 (setq total-length (+ total-length length)))))
597 ;;; FIXME: Make a compiler macro or transform for this which efficiently
598 ;;; handles the case of constant 'STRING first argument. (It's not just time
599 ;;; efficiency, but space efficiency..)
600 (defun concatenate (output-type-spec &rest sequences)
602 "Returns a new sequence of all the argument sequences concatenated together
603 which shares no structure with the original argument sequences of the
604 specified OUTPUT-TYPE-SPEC."
605 (case (type-specifier-atom output-type-spec)
606 ((simple-vector simple-string vector string array simple-array
607 bit-vector simple-bit-vector base-string
608 simple-base-string) ; FIXME: unifying principle here?
609 (let ((result (apply #'concat-to-simple* output-type-spec sequences)))
611 (check-type-var result output-type-spec)
613 (list (apply #'concat-to-list* sequences))
615 (apply #'concatenate (result-type-or-lose output-type-spec) sequences))))
618 ;;; FIXME: These are weird. They're never called anywhere except in
619 ;;; CONCATENATE. It seems to me that the macros ought to just
620 ;;; be expanded directly in CONCATENATE, or in CONCATENATE-STRING
621 ;;; and CONCATENATE-LIST variants. Failing that, these ought to be local
622 ;;; functions (FLET).
623 (defun concat-to-list* (&rest sequences)
624 (concatenate-to-list sequences))
625 (defun concat-to-simple* (type &rest sequences)
626 (concatenate-to-mumble type sequences))
628 ;;;; MAP and MAP-INTO
630 ;;; helper functions to handle arity-1 subcases of MAP
631 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
632 (declaim (ftype (function (function sequence) simple-vector)
633 %map-simple-vector-arity-1))
634 (macrolet ((dosequence ((i sequence) &body body)
635 (once-only ((sequence sequence))
636 `(etypecase ,sequence
637 (list (dolist (,i ,sequence) ,@body))
638 (simple-vector (dovector (,i sequence) ,@body))
639 (vector (dovector (,i sequence) ,@body))))))
640 (defun %map-to-list-arity-1 (fun sequence)
641 (let ((reversed-result nil)
642 (really-fun (%coerce-callable-to-function fun)))
643 (dosequence (element sequence)
644 (push (funcall really-fun element)
646 (nreverse reversed-result)))
647 (defun %map-to-simple-vector-arity-1 (fun sequence)
648 (let ((result (make-array (length sequence)))
650 (really-fun (%coerce-callable-to-function fun)))
651 (declare (type index index))
652 (dosequence (element sequence)
653 (setf (aref result index)
654 (funcall really-fun element))
657 (defun %map-for-effect-arity-1 (fun sequence)
658 (let ((really-fun (%coerce-callable-to-function fun)))
659 (dosequence (element sequence)
660 (funcall really-fun element)))
663 ;;; helper functions to handle arity-N subcases of MAP
665 ;;; KLUDGE: This is hairier, and larger, than need be, because we
666 ;;; don't have DYNAMIC-EXTENT. With DYNAMIC-EXTENT, we could define
667 ;;; %MAP-FOR-EFFECT, and then implement the
668 ;;; other %MAP-TO-FOO functions reasonably efficiently by passing closures to
669 ;;; %MAP-FOR-EFFECT. (DYNAMIC-EXTENT would help a little by avoiding
670 ;;; consing each closure, and would help a lot by allowing us to define
671 ;;; a closure (LAMBDA (&REST REST) <do something with (APPLY FUN REST)>)
672 ;;; with the REST list allocated with DYNAMIC-EXTENT. -- WHN 20000920
673 (macrolet (;; Execute BODY in a context where the machinery for
674 ;; UPDATED-MAP-APPLY-ARGS has been set up.
675 (with-map-state (sequences &body body)
676 `(let* ((%sequences ,sequences)
677 (%iters (mapcar (lambda (sequence)
682 (%apply-args (make-list (length %sequences))))
683 (declare (type list %sequences %iters %apply-args))
685 ;; Return a list of args to pass to APPLY for the next
686 ;; function call in the mapping, or NIL if no more function
687 ;; calls should be made (because we've reached the end of a
689 (updated-map-apply-args ()
690 '(do ((in-sequences %sequences (cdr in-sequences))
691 (in-iters %iters (cdr in-iters))
692 (in-apply-args %apply-args (cdr in-apply-args)))
695 (declare (type list in-sequences in-iters in-apply-args))
696 (let ((i (car in-iters)))
697 (declare (type (or list index) i))
699 (if (null i) ; if end of this sequence
701 (setf (car in-apply-args) (car i)
702 (car in-iters) (cdr i)))
703 (let ((v (the vector (car in-sequences))))
704 (if (>= i (length v)) ; if end of this sequence
706 (setf (car in-apply-args) (aref v i)
707 (car in-iters) (1+ i)))))))))
708 (defun %map-to-list (func sequences)
709 (declare (type function func))
710 (declare (type list sequences))
711 (with-map-state sequences
712 (loop with updated-map-apply-args
713 while (setf updated-map-apply-args (updated-map-apply-args))
714 collect (apply func updated-map-apply-args))))
715 (defun %map-to-vector (output-type-spec func sequences)
716 (declare (type function func))
717 (declare (type list sequences))
718 (let ((min-len (with-map-state sequences
719 (do ((counter 0 (1+ counter)))
720 ;; Note: Doing everything in
721 ;; UPDATED-MAP-APPLY-ARGS here is somewhat
722 ;; wasteful; we even do some extra consing.
723 ;; And stepping over every element of
724 ;; VECTORs, instead of just grabbing their
725 ;; LENGTH, is also wasteful. But it's easy
726 ;; and safe. (If you do rewrite it, please
727 ;; try to make sure that
728 ;; (MAP NIL #'F SOME-CIRCULAR-LIST #(1))
729 ;; does the right thing.)
730 ((not (updated-map-apply-args))
732 (declare (type index counter))))))
733 (declare (type index min-len))
734 (with-map-state sequences
735 (let ((result (make-sequence-of-type output-type-spec min-len))
737 (declare (type index index))
738 (loop with updated-map-apply-args
739 while (setf updated-map-apply-args (updated-map-apply-args))
741 (setf (aref result index)
742 (apply func updated-map-apply-args))
745 (defun %map-for-effect (func sequences)
746 (declare (type function func))
747 (declare (type list sequences))
748 (with-map-state sequences
749 (loop with updated-map-apply-args
750 while (setf updated-map-apply-args (updated-map-apply-args))
752 (apply func updated-map-apply-args))
755 "FUNCTION must take as many arguments as there are sequences provided.
756 The result is a sequence of type OUTPUT-TYPE-SPEC such that element I
757 is the result of applying FUNCTION to element I of each of the argument
760 ;;; %MAP is just MAP without the final just-to-be-sure check that
761 ;;; length of the output sequence matches any length specified
763 (defun %map (result-type function first-sequence &rest more-sequences)
764 (let ((really-function (%coerce-callable-to-function function)))
765 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
766 ;; it into something which can be DEFTRANSFORMed away. (It's
767 ;; fairly important to handle this case efficiently, since
768 ;; quantifiers like SOME are transformed into this case, and since
769 ;; there's no consing overhead to dwarf our inefficiency.)
770 (if (and (null more-sequences)
772 (%map-for-effect-arity-1 really-function first-sequence)
773 ;; Otherwise, use the industrial-strength full-generality
774 ;; approach, consing O(N-ARGS) temporary storage (which can have
775 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
776 (let ((sequences (cons first-sequence more-sequences)))
777 (case (type-specifier-atom result-type)
778 ((nil) (%map-for-effect really-function sequences))
779 (list (%map-to-list really-function sequences))
780 ((simple-vector simple-string vector string array simple-array
781 bit-vector simple-bit-vector base-string simple-base-string)
782 (%map-to-vector result-type really-function sequences))
785 (result-type-or-lose result-type t)
789 (defun map (result-type function first-sequence &rest more-sequences)
790 (sequence-of-checked-length-given-type (apply #'%map
795 ;; (The RESULT-TYPE isn't
796 ;; strictly the type of the
797 ;; result, because when
798 ;; RESULT-TYPE=NIL, the result
799 ;; actually has NULL type. But
800 ;; that special case doesn't
801 ;; matter here, since we only
802 ;; look closely at vector
803 ;; types; so we can just pass
804 ;; RESULT-TYPE straight through
805 ;; as a type specifier.)
808 ;;; KLUDGE: MAP has been rewritten substantially since the fork from
809 ;;; CMU CL in order to give reasonable performance, but this
810 ;;; implementation of MAP-INTO still has the same problems as the old
811 ;;; MAP code. Ideally, MAP-INTO should be rewritten to be efficient in
812 ;;; the same way that the corresponding cases of MAP have been
813 ;;; rewritten. Instead of doing it now, though, it's easier to wait
814 ;;; until we have DYNAMIC-EXTENT, at which time it should become
815 ;;; extremely easy to define a reasonably efficient MAP-INTO in terms
816 ;;; of (MAP NIL ..). -- WHN 20000920
817 (defun map-into (result-sequence function &rest sequences)
819 (and (arrayp result-sequence)
820 (array-has-fill-pointer-p result-sequence)))
823 (array-dimension result-sequence 0)
824 (length result-sequence))
825 (mapcar #'length sequences))))
828 (setf (fill-pointer result-sequence) len))
830 (let ((really-fun (%coerce-callable-to-function function)))
832 (setf (elt result-sequence index)
834 (mapcar #'(lambda (seq) (elt seq index))
840 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
841 ;;; arbitrary sequence arguments, both in the full call case and in
842 ;;; the open code case.
843 (macrolet ((defquantifier (name found-test found-result
844 &key doc (unfound-result (not found-result)))
846 ;; KLUDGE: It would be really nice if we could simply
847 ;; do something like this
848 ;; (declaim (inline ,name))
849 ;; (defun ,name (pred first-seq &rest more-seqs)
851 ;; (flet ((map-me (&rest rest)
852 ;; (let ((pred-value (apply pred rest)))
853 ;; (,found-test pred-value
854 ;; (return-from ,name
855 ;; ,found-result)))))
856 ;; (declare (inline map-me))
857 ;; (apply #'map nil #'map-me first-seq more-seqs)
859 ;; but Python doesn't seem to be smart enough about
860 ;; inlining and APPLY to recognize that it can use
861 ;; the DEFTRANSFORM for MAP in the resulting inline
862 ;; expansion. I don't have any appetite for deep
863 ;; compiler hacking right now, so I'll just work
864 ;; around the apparent problem by using a compiler
865 ;; macro instead. -- WHN 20000410
866 (defun ,name (pred first-seq &rest more-seqs)
868 (flet ((map-me (&rest rest)
869 (let ((pred-value (apply pred rest)))
870 (,found-test pred-value
873 (declare (inline map-me))
874 (apply #'map nil #'map-me first-seq more-seqs)
876 ;; KLUDGE: It would be more obviously correct -- but
877 ;; also significantly messier -- for PRED-VALUE to be
878 ;; a gensym. However, a private symbol really does
879 ;; seem to be good enough; and anyway the really
880 ;; obviously correct solution is to make Python smart
881 ;; enough that we can use an inline function instead
882 ;; of a compiler macro (as above). -- WHN 20000410
883 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
884 (let ((elements (make-gensym-list (1+ (length more-seqs))))
885 (blockname (gensym "BLOCK")))
886 (once-only ((pred pred))
890 (let ((pred-value (funcall ,pred ,@elements)))
891 (,',found-test pred-value
892 (return-from ,blockname
896 ,',unfound-result)))))))
897 (defquantifier some when pred-value :unfound-result nil :doc
898 "PREDICATE is applied to the elements with index 0 of the sequences, then
899 possibly to those with index 1, and so on. SOME returns the first
900 non-NIL value encountered, or NIL if the end of a sequence is reached.")
901 (defquantifier every unless nil :doc
902 "PREDICATE is applied to the elements with index 0 of the sequences, then
903 possibly to those with index 1, and so on. EVERY returns NIL as soon
904 as any invocation of PREDICATE returns NIL, or T if every invocation
906 (defquantifier notany when nil :doc
907 "PREDICATE is applied to the elements with index 0 of the sequences, then
908 possibly to those with index 1, and so on. NOTANY returns NIL as soon
909 as any invocation of PREDICATE returns a non-NIL value, or T if the end
910 of a sequence is reached.")
911 (defquantifier notevery unless t :doc
912 "PREDICATE is applied to the elements with index 0 of the sequences, then
913 possibly to those with index 1, and so on. NOTEVERY returns T as soon
914 as any invocation of PREDICATE returns NIL, or NIL if every invocation
919 (eval-when (:compile-toplevel :execute)
921 (sb!xc:defmacro mumble-reduce (function
928 `(do ((index ,start (1+ index))
929 (value ,initial-value))
930 ((= index (the fixnum ,end)) value)
931 (declare (fixnum index))
932 (setq value (funcall ,function value
933 (apply-key ,key (,ref ,sequence index))))))
935 (sb!xc:defmacro mumble-reduce-from-end (function
942 `(do ((index (1- ,end) (1- index))
943 (value ,initial-value)
944 (terminus (1- ,start)))
945 ((= index terminus) value)
946 (declare (fixnum index terminus))
947 (setq value (funcall ,function
948 (apply-key ,key (,ref ,sequence index))
951 (sb!xc:defmacro list-reduce (function
958 `(let ((sequence (nthcdr ,start ,sequence)))
959 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
961 (sequence (if ,ivp sequence (cdr sequence))
963 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
964 (funcall ,function value (apply-key ,key (car sequence)))))
965 ((= count (the fixnum ,end)) value)
966 (declare (fixnum count)))))
968 (sb!xc:defmacro list-reduce-from-end (function
975 `(let ((sequence (nthcdr (- (the fixnum (length ,sequence))
977 (reverse ,sequence))))
978 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
980 (sequence (if ,ivp sequence (cdr sequence))
982 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
983 (funcall ,function (apply-key ,key (car sequence)) value)))
984 ((= count (the fixnum ,end)) value)
985 (declare (fixnum count)))))
989 (defun reduce (function sequence &key key from-end (start 0)
990 end (initial-value nil ivp))
991 (declare (type index start))
993 (end (or end (length sequence))))
994 (declare (type index start end))
996 (if ivp initial-value (funcall function)))
999 (list-reduce-from-end function sequence key start end
1001 (list-reduce function sequence key start end
1002 initial-value ivp)))
1005 (setq end (1- (the fixnum end)))
1006 (setq initial-value (apply-key key (aref sequence end))))
1007 (mumble-reduce-from-end function sequence key start end
1008 initial-value aref))
1011 (setq initial-value (apply-key key (aref sequence start)))
1012 (setq start (1+ start)))
1013 (mumble-reduce function sequence key start end
1014 initial-value aref)))))
1018 (eval-when (:compile-toplevel :execute)
1020 (sb!xc:defmacro mumble-delete (pred)
1021 `(do ((index start (1+ index))
1024 ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1025 (do ((index index (1+ index)) ; Copy the rest of the vector.
1026 (jndex jndex (1+ jndex)))
1027 ((= index (the fixnum length))
1028 (shrink-vector sequence jndex))
1029 (declare (fixnum index jndex))
1030 (setf (aref sequence jndex) (aref sequence index))))
1031 (declare (fixnum index jndex number-zapped))
1032 (setf (aref sequence jndex) (aref sequence index))
1034 (setq number-zapped (1+ number-zapped))
1035 (setq jndex (1+ jndex)))))
1037 (sb!xc:defmacro mumble-delete-from-end (pred)
1038 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1042 (terminus (1- start)))
1043 ((or (= index terminus) (= number-zapped (the fixnum count)))
1044 (do ((losers losers) ; Delete the losers.
1045 (index start (1+ index))
1047 ((or (null losers) (= index (the fixnum end)))
1048 (do ((index index (1+ index)) ; Copy the rest of the vector.
1049 (jndex jndex (1+ jndex)))
1050 ((= index (the fixnum length))
1051 (shrink-vector sequence jndex))
1052 (declare (fixnum index jndex))
1053 (setf (aref sequence jndex) (aref sequence index))))
1054 (declare (fixnum index jndex))
1055 (setf (aref sequence jndex) (aref sequence index))
1056 (if (= index (the fixnum (car losers)))
1058 (setq jndex (1+ jndex)))))
1059 (declare (fixnum index number-zapped terminus))
1060 (setq this-element (aref sequence index))
1062 (setq number-zapped (1+ number-zapped))
1063 (push index losers))))
1065 (sb!xc:defmacro normal-mumble-delete ()
1068 (not (funcall test-not item (apply-key key (aref sequence index))))
1069 (funcall test item (apply-key key (aref sequence index))))))
1071 (sb!xc:defmacro normal-mumble-delete-from-end ()
1072 `(mumble-delete-from-end
1074 (not (funcall test-not item (apply-key key this-element)))
1075 (funcall test item (apply-key key this-element)))))
1077 (sb!xc:defmacro list-delete (pred)
1078 `(let ((handle (cons nil sequence)))
1079 (do ((current (nthcdr start sequence) (cdr current))
1080 (previous (nthcdr start handle))
1081 (index start (1+ index))
1083 ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1085 (declare (fixnum index number-zapped))
1087 (rplacd previous (cdr current))
1088 (setq number-zapped (1+ number-zapped)))
1090 (setq previous (cdr previous)))))))
1092 (sb!xc:defmacro list-delete-from-end (pred)
1093 `(let* ((reverse (nreverse (the list sequence)))
1094 (handle (cons nil reverse)))
1095 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1097 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1098 (index start (1+ index))
1100 ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1101 (nreverse (cdr handle)))
1102 (declare (fixnum index number-zapped))
1104 (rplacd previous (cdr current))
1105 (setq number-zapped (1+ number-zapped)))
1107 (setq previous (cdr previous)))))))
1109 (sb!xc:defmacro normal-list-delete ()
1112 (not (funcall test-not item (apply-key key (car current))))
1113 (funcall test item (apply-key key (car current))))))
1115 (sb!xc:defmacro normal-list-delete-from-end ()
1116 '(list-delete-from-end
1118 (not (funcall test-not item (apply-key key (car current))))
1119 (funcall test item (apply-key key (car current))))))
1123 (defun delete (item sequence &key from-end (test #'eql) test-not (start 0)
1126 "Returns a sequence formed by destructively removing the specified Item from
1127 the given Sequence."
1128 (declare (fixnum start))
1129 (let* ((length (length sequence))
1130 (end (or end length))
1131 (count (or count most-positive-fixnum)))
1132 (declare (type index length end)
1134 (seq-dispatch sequence
1136 (normal-list-delete-from-end)
1137 (normal-list-delete))
1139 (normal-mumble-delete-from-end)
1140 (normal-mumble-delete)))))
1142 (eval-when (:compile-toplevel :execute)
1144 (sb!xc:defmacro if-mumble-delete ()
1146 (funcall predicate (apply-key key (aref sequence index)))))
1148 (sb!xc:defmacro if-mumble-delete-from-end ()
1149 `(mumble-delete-from-end
1150 (funcall predicate (apply-key key this-element))))
1152 (sb!xc:defmacro if-list-delete ()
1154 (funcall predicate (apply-key key (car current)))))
1156 (sb!xc:defmacro if-list-delete-from-end ()
1157 '(list-delete-from-end
1158 (funcall predicate (apply-key key (car current)))))
1162 (defun delete-if (predicate sequence &key from-end (start 0) key end count)
1164 "Returns a sequence formed by destructively removing the elements satisfying
1165 the specified Predicate from the given Sequence."
1166 (declare (fixnum start))
1167 (let* ((length (length sequence))
1168 (end (or end length))
1169 (count (or count most-positive-fixnum)))
1170 (declare (type index length end)
1172 (seq-dispatch sequence
1174 (if-list-delete-from-end)
1177 (if-mumble-delete-from-end)
1178 (if-mumble-delete)))))
1180 (eval-when (:compile-toplevel :execute)
1182 (sb!xc:defmacro if-not-mumble-delete ()
1184 (not (funcall predicate (apply-key key (aref sequence index))))))
1186 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1187 `(mumble-delete-from-end
1188 (not (funcall predicate (apply-key key this-element)))))
1190 (sb!xc:defmacro if-not-list-delete ()
1192 (not (funcall predicate (apply-key key (car current))))))
1194 (sb!xc:defmacro if-not-list-delete-from-end ()
1195 '(list-delete-from-end
1196 (not (funcall predicate (apply-key key (car current))))))
1200 (defun delete-if-not (predicate sequence &key from-end (start 0) end key count)
1202 "Returns a sequence formed by destructively removing the elements not
1203 satisfying the specified Predicate from the given Sequence."
1204 (declare (fixnum start))
1205 (let* ((length (length sequence))
1206 (end (or end length))
1207 (count (or count most-positive-fixnum)))
1208 (declare (type index length end)
1210 (seq-dispatch sequence
1212 (if-not-list-delete-from-end)
1213 (if-not-list-delete))
1215 (if-not-mumble-delete-from-end)
1216 (if-not-mumble-delete)))))
1220 (eval-when (:compile-toplevel :execute)
1222 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1223 ;;; satisfies the predicate.
1224 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1225 `(do ((index ,begin (,bump index))
1227 (do ((index ,left (,bump index))
1228 (result (make-sequence-like sequence length)))
1229 ((= index (the fixnum ,begin)) result)
1230 (declare (fixnum index))
1231 (setf (aref result index) (aref sequence index))))
1235 ((or (= index (the fixnum ,finish))
1236 (= number-zapped (the fixnum count)))
1237 (do ((index index (,bump index))
1238 (new-index new-index (,bump new-index)))
1239 ((= index (the fixnum ,right)) (shrink-vector result new-index))
1240 (declare (fixnum index new-index))
1241 (setf (aref result new-index) (aref sequence index))))
1242 (declare (fixnum index new-index number-zapped))
1243 (setq this-element (aref sequence index))
1244 (cond (,pred (setq number-zapped (1+ number-zapped)))
1245 (t (setf (aref result new-index) this-element)
1246 (setq new-index (,bump new-index))))))
1248 (sb!xc:defmacro mumble-remove (pred)
1249 `(mumble-remove-macro 1+ 0 start end length ,pred))
1251 (sb!xc:defmacro mumble-remove-from-end (pred)
1252 `(let ((sequence (copy-seq sequence)))
1253 (mumble-delete-from-end ,pred)))
1255 (sb!xc:defmacro normal-mumble-remove ()
1258 (not (funcall test-not item (apply-key key this-element)))
1259 (funcall test item (apply-key key this-element)))))
1261 (sb!xc:defmacro normal-mumble-remove-from-end ()
1262 `(mumble-remove-from-end
1264 (not (funcall test-not item (apply-key key this-element)))
1265 (funcall test item (apply-key key this-element)))))
1267 (sb!xc:defmacro if-mumble-remove ()
1268 `(mumble-remove (funcall predicate (apply-key key this-element))))
1270 (sb!xc:defmacro if-mumble-remove-from-end ()
1271 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1273 (sb!xc:defmacro if-not-mumble-remove ()
1274 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1276 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1277 `(mumble-remove-from-end
1278 (not (funcall predicate (apply-key key this-element)))))
1280 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1282 (sb!xc:defmacro list-remove-macro (pred reverse?)
1283 `(let* ((sequence ,(if reverse?
1284 '(reverse (the list sequence))
1287 (results (do ((index 0 (1+ index))
1288 (before-start splice))
1289 ((= index (the fixnum start)) before-start)
1290 (declare (fixnum index))
1292 (cdr (rplacd splice (list (pop sequence))))))))
1293 (do ((index start (1+ index))
1296 ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1297 (do ((index index (1+ index)))
1300 '(nreverse (the list (cdr results)))
1302 (declare (fixnum index))
1303 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1304 (declare (fixnum index number-zapped))
1305 (setq this-element (pop sequence))
1307 (setq number-zapped (1+ number-zapped))
1308 (setq splice (cdr (rplacd splice (list this-element))))))))
1310 (sb!xc:defmacro list-remove (pred)
1311 `(list-remove-macro ,pred nil))
1313 (sb!xc:defmacro list-remove-from-end (pred)
1314 `(list-remove-macro ,pred t))
1316 (sb!xc:defmacro normal-list-remove ()
1319 (not (funcall test-not item (apply-key key this-element)))
1320 (funcall test item (apply-key key this-element)))))
1322 (sb!xc:defmacro normal-list-remove-from-end ()
1323 `(list-remove-from-end
1325 (not (funcall test-not item (apply-key key this-element)))
1326 (funcall test item (apply-key key this-element)))))
1328 (sb!xc:defmacro if-list-remove ()
1330 (funcall predicate (apply-key key this-element))))
1332 (sb!xc:defmacro if-list-remove-from-end ()
1333 `(list-remove-from-end
1334 (funcall predicate (apply-key key this-element))))
1336 (sb!xc:defmacro if-not-list-remove ()
1338 (not (funcall predicate (apply-key key this-element)))))
1340 (sb!xc:defmacro if-not-list-remove-from-end ()
1341 `(list-remove-from-end
1342 (not (funcall predicate (apply-key key this-element)))))
1346 (defun remove (item sequence &key from-end (test #'eql) test-not (start 0)
1349 "Returns a copy of SEQUENCE with elements satisfying the test (default is
1350 EQL) with ITEM removed."
1351 (declare (fixnum start))
1352 (let* ((length (length sequence))
1353 (end (or end length))
1354 (count (or count most-positive-fixnum)))
1355 (declare (type index length end)
1357 (seq-dispatch sequence
1359 (normal-list-remove-from-end)
1360 (normal-list-remove))
1362 (normal-mumble-remove-from-end)
1363 (normal-mumble-remove)))))
1365 (defun remove-if (predicate sequence &key from-end (start 0) end count key)
1367 "Returns a copy of sequence with elements such that predicate(element)
1368 is non-null are removed"
1369 (declare (fixnum start))
1370 (let* ((length (length sequence))
1371 (end (or end length))
1372 (count (or count most-positive-fixnum)))
1373 (declare (type index length end)
1375 (seq-dispatch sequence
1377 (if-list-remove-from-end)
1380 (if-mumble-remove-from-end)
1381 (if-mumble-remove)))))
1383 (defun remove-if-not (predicate sequence &key from-end (start 0) end count key)
1385 "Returns a copy of sequence with elements such that predicate(element)
1386 is null are removed"
1387 (declare (fixnum start))
1388 (let* ((length (length sequence))
1389 (end (or end length))
1390 (count (or count most-positive-fixnum)))
1391 (declare (type index length end)
1393 (seq-dispatch sequence
1395 (if-not-list-remove-from-end)
1396 (if-not-list-remove))
1398 (if-not-mumble-remove-from-end)
1399 (if-not-mumble-remove)))))
1401 ;;;; REMOVE-DUPLICATES
1403 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1404 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1405 ;;; if we look into the already copied structure (from after :start) and see
1406 ;;; the item. If we check from beginning we check into the rest of the
1407 ;;; original list up to the :end marker (this we have to do by running a
1408 ;;; do loop down the list that far and using our test.
1409 (defun list-remove-duplicates* (list test test-not start end key from-end)
1410 (declare (fixnum start))
1411 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1414 (do ((index 0 (1+ index)))
1416 (declare (fixnum index))
1417 (setq splice (cdr (rplacd splice (list (car current)))))
1418 (setq current (cdr current)))
1419 (do ((index 0 (1+ index)))
1420 ((or (and end (= index (the fixnum end)))
1422 (declare (fixnum index))
1423 (if (or (and from-end
1424 (not (member (apply-key key (car current))
1425 (nthcdr (1+ start) result)
1430 (not (do ((it (apply-key key (car current)))
1431 (l (cdr current) (cdr l))
1432 (i (1+ index) (1+ i)))
1433 ((or (atom l) (and end (= i (the fixnum end))))
1435 (declare (fixnum i))
1437 (not (funcall test-not it (apply-key key (car l))))
1438 (funcall test it (apply-key key (car l))))
1440 (setq splice (cdr (rplacd splice (list (car current))))))
1441 (setq current (cdr current)))
1444 (setq splice (cdr (rplacd splice (list (car current)))))
1445 (setq current (cdr current)))
1448 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1449 &optional (length (length vector)))
1450 (declare (vector vector) (fixnum start length))
1451 (when (null end) (setf end (length vector)))
1452 (let ((result (make-sequence-like vector length))
1455 (declare (fixnum index jndex))
1458 (setf (aref result index) (aref vector index))
1459 (setq index (1+ index)))
1462 (setq elt (aref vector index))
1463 (unless (or (and from-end
1464 (position (apply-key key elt) result :start start
1465 :end jndex :test test :test-not test-not :key key))
1467 (position (apply-key key elt) vector :start (1+ index)
1468 :end end :test test :test-not test-not :key key)))
1469 (setf (aref result jndex) elt)
1470 (setq jndex (1+ jndex)))
1471 (setq index (1+ index)))
1474 (setf (aref result jndex) (aref vector index))
1475 (setq index (1+ index))
1476 (setq jndex (1+ jndex)))
1477 (shrink-vector result jndex)))
1479 (defun remove-duplicates (sequence &key
1487 "The elements of Sequence are compared pairwise, and if any two match,
1488 the one occurring earlier is discarded, unless FROM-END is true, in
1489 which case the one later in the sequence is discarded. The resulting
1490 sequence is returned.
1492 The :TEST-NOT argument is depreciated."
1493 (declare (fixnum start))
1494 (seq-dispatch sequence
1496 (list-remove-duplicates* sequence test test-not
1497 start end key from-end))
1498 (vector-remove-duplicates* sequence test test-not
1499 start end key from-end)))
1501 ;;;; DELETE-DUPLICATES
1503 (defun list-delete-duplicates* (list test test-not key from-end start end)
1504 (declare (fixnum start))
1505 (let ((handle (cons nil list)))
1506 (do ((current (nthcdr start list) (cdr current))
1507 (previous (nthcdr start handle))
1508 (index start (1+ index)))
1509 ((or (and end (= index (the fixnum end))) (null current))
1511 (declare (fixnum index))
1512 (if (do ((x (if from-end
1513 (nthcdr (1+ start) handle)
1516 (i (1+ index) (1+ i)))
1518 (and (not from-end) end (= i (the fixnum end)))
1521 (declare (fixnum i))
1523 (not (funcall test-not
1524 (apply-key key (car current))
1525 (apply-key key (car x))))
1527 (apply-key key (car current))
1528 (apply-key key (car x))))
1530 (rplacd previous (cdr current))
1531 (setq previous (cdr previous))))))
1533 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1534 &optional (length (length vector)))
1535 (declare (vector vector) (fixnum start length))
1536 (when (null end) (setf end (length vector)))
1537 (do ((index start (1+ index))
1540 (do ((index index (1+ index)) ; copy the rest of the vector
1541 (jndex jndex (1+ jndex)))
1543 (shrink-vector vector jndex)
1545 (setf (aref vector jndex) (aref vector index))))
1546 (declare (fixnum index jndex))
1547 (setf (aref vector jndex) (aref vector index))
1548 (unless (position (apply-key key (aref vector index)) vector :key key
1549 :start (if from-end start (1+ index)) :test test
1550 :end (if from-end jndex end) :test-not test-not)
1551 (setq jndex (1+ jndex)))))
1553 (defun delete-duplicates (sequence &key
1561 "The elements of Sequence are examined, and if any two match, one is
1562 discarded. The resulting sequence, which may be formed by destroying the
1563 given sequence, is returned.
1565 The :TEST-NOT argument is depreciated."
1566 (seq-dispatch sequence
1568 (list-delete-duplicates* sequence test test-not key from-end start end))
1569 (vector-delete-duplicates* sequence test test-not key from-end start end)))
1573 (defun list-substitute* (pred new list start end count key test test-not old)
1574 (declare (fixnum start end count))
1575 (let* ((result (list nil))
1578 (list list)) ; Get a local list for a stepper.
1579 (do ((index 0 (1+ index)))
1581 (declare (fixnum index))
1582 (setq splice (cdr (rplacd splice (list (car list)))))
1583 (setq list (cdr list)))
1584 (do ((index start (1+ index)))
1585 ((or (= index end) (null list) (= count 0)))
1586 (declare (fixnum index))
1587 (setq elt (car list))
1596 (funcall test-not old (apply-key key elt)))
1597 (funcall test old (apply-key key elt))))
1598 (if (funcall test (apply-key key elt)))
1599 (if-not (not (funcall test (apply-key key elt)))))
1600 (setq count (1- count))
1603 (setq list (cdr list)))
1606 (setq splice (cdr (rplacd splice (list (car list)))))
1607 (setq list (cdr list)))
1610 ;;; Replace old with new in sequence moving from left to right by incrementer
1611 ;;; on each pass through the loop. Called by all three substitute functions.
1612 (defun vector-substitute* (pred new sequence incrementer left right length
1613 start end count key test test-not old)
1614 (declare (fixnum start count end incrementer right))
1615 (let ((result (make-sequence-like sequence length))
1617 (declare (fixnum index))
1620 (setf (aref result index) (aref sequence index))
1621 (setq index (+ index incrementer)))
1623 ((or (= index end) (= count 0)))
1624 (setq elt (aref sequence index))
1625 (setf (aref result index)
1629 (not (funcall test-not old (apply-key key elt)))
1630 (funcall test old (apply-key key elt))))
1631 (if (funcall test (apply-key key elt)))
1632 (if-not (not (funcall test (apply-key key elt)))))
1633 (setq count (1- count))
1636 (setq index (+ index incrementer)))
1639 (setf (aref result index) (aref sequence index))
1640 (setq index (+ index incrementer)))
1643 (eval-when (:compile-toplevel :execute)
1645 (sb!xc:defmacro subst-dispatch (pred)
1646 `(if (listp sequence)
1648 (nreverse (list-substitute* ,pred
1651 (- (the fixnum length)
1653 (- (the fixnum length)
1655 count key test test-not old))
1656 (list-substitute* ,pred
1657 new sequence start end count key test test-not
1660 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
1661 -1 length (1- (the fixnum end))
1662 (1- (the fixnum start))
1663 count key test test-not old)
1664 (vector-substitute* ,pred new sequence 1 0 length length
1665 start end count key test test-not old))))
1669 (defun substitute (new old sequence &key from-end (test #'eql) test-not
1670 (start 0) count end key)
1672 "Returns a sequence of the same kind as Sequence with the same elements
1673 except that all elements equal to Old are replaced with New. See manual
1675 (declare (fixnum start))
1676 (let* ((length (length sequence))
1677 (end (or end length))
1678 (count (or count most-positive-fixnum)))
1679 (declare (type index length end)
1681 (subst-dispatch 'normal)))
1683 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
1685 (defun substitute-if (new test sequence &key from-end (start 0) end count key)
1687 "Returns a sequence of the same kind as Sequence with the same elements
1688 except that all elements satisfying the Test are replaced with New. See
1689 manual for details."
1690 (declare (fixnum start))
1691 (let* ((length (length sequence))
1692 (end (or end length))
1693 (count (or count most-positive-fixnum))
1696 (declare (type index length end)
1698 (subst-dispatch 'if)))
1700 (defun substitute-if-not (new test sequence &key from-end (start 0)
1703 "Returns a sequence of the same kind as Sequence with the same elements
1704 except that all elements not satisfying the Test are replaced with New.
1705 See manual for details."
1706 (declare (fixnum start))
1707 (let* ((length (length sequence))
1708 (end (or end length))
1709 (count (or count most-positive-fixnum))
1712 (declare (type index length end)
1714 (subst-dispatch 'if-not)))
1718 (defun nsubstitute (new old sequence &key from-end (test #'eql) test-not
1719 end count key (start 0))
1721 "Returns a sequence of the same kind as Sequence with the same elements
1722 except that all elements equal to Old are replaced with New. The Sequence
1723 may be destroyed. See manual for details."
1724 (declare (fixnum start))
1725 (let ((end (or end (length sequence)))
1726 (count (or count most-positive-fixnum)))
1727 (declare (fixnum count))
1728 (if (listp sequence)
1730 (nreverse (nlist-substitute*
1731 new old (nreverse (the list sequence))
1732 test test-not start end count key))
1733 (nlist-substitute* new old sequence
1734 test test-not start end count key))
1736 (nvector-substitute* new old sequence -1
1737 test test-not (1- end) (1- start) count key)
1738 (nvector-substitute* new old sequence 1
1739 test test-not start end count key)))))
1741 (defun nlist-substitute* (new old sequence test test-not start end count key)
1742 (declare (fixnum start count end))
1743 (do ((list (nthcdr start sequence) (cdr list))
1744 (index start (1+ index)))
1745 ((or (= index end) (null list) (= count 0)) sequence)
1746 (declare (fixnum index))
1748 (not (funcall test-not old (apply-key key (car list))))
1749 (funcall test old (apply-key key (car list))))
1751 (setq count (1- count)))))
1753 (defun nvector-substitute* (new old sequence incrementer
1754 test test-not start end count key)
1755 (declare (fixnum start incrementer count end))
1756 (do ((index start (+ index incrementer)))
1757 ((or (= index end) (= count 0)) sequence)
1758 (declare (fixnum index))
1760 (not (funcall test-not
1762 (apply-key key (aref sequence index))))
1763 (funcall test old (apply-key key (aref sequence index))))
1764 (setf (aref sequence index) new)
1765 (setq count (1- count)))))
1767 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
1769 (defun nsubstitute-if (new test sequence &key from-end (start 0) end count key)
1771 "Returns a sequence of the same kind as Sequence with the same elements
1772 except that all elements satisfying the Test are replaced with New. The
1773 Sequence may be destroyed. See manual for details."
1774 (declare (fixnum start))
1775 (let ((end (or end (length sequence)))
1776 (count (or count most-positive-fixnum)))
1777 (declare (fixnum end count))
1778 (if (listp sequence)
1780 (nreverse (nlist-substitute-if*
1781 new test (nreverse (the list sequence))
1782 start end count key))
1783 (nlist-substitute-if* new test sequence
1784 start end count key))
1786 (nvector-substitute-if* new test sequence -1
1787 (1- end) (1- start) count key)
1788 (nvector-substitute-if* new test sequence 1
1789 start end count key)))))
1791 (defun nlist-substitute-if* (new test sequence start end count key)
1792 (declare (fixnum end))
1793 (do ((list (nthcdr start sequence) (cdr list))
1794 (index start (1+ index)))
1795 ((or (= index end) (null list) (= count 0)) sequence)
1796 (when (funcall test (apply-key key (car list)))
1798 (setq count (1- count)))))
1800 (defun nvector-substitute-if* (new test sequence incrementer
1801 start end count key)
1802 (do ((index start (+ index incrementer)))
1803 ((or (= index end) (= count 0)) sequence)
1804 (when (funcall test (apply-key key (aref sequence index)))
1805 (setf (aref sequence index) new)
1806 (setq count (1- count)))))
1808 (defun nsubstitute-if-not (new test sequence &key from-end (start 0)
1811 "Returns a sequence of the same kind as Sequence with the same elements
1812 except that all elements not satisfying the Test are replaced with New.
1813 The Sequence may be destroyed. See manual for details."
1814 (declare (fixnum start))
1815 (let ((end (or end (length sequence)))
1816 (count (or count most-positive-fixnum)))
1817 (declare (fixnum end count))
1818 (if (listp sequence)
1820 (nreverse (nlist-substitute-if-not*
1821 new test (nreverse (the list sequence))
1822 start end count key))
1823 (nlist-substitute-if-not* new test sequence
1824 start end count key))
1826 (nvector-substitute-if-not* new test sequence -1
1827 (1- end) (1- start) count key)
1828 (nvector-substitute-if-not* new test sequence 1
1829 start end count key)))))
1831 (defun nlist-substitute-if-not* (new test sequence start end count key)
1832 (declare (fixnum end))
1833 (do ((list (nthcdr start sequence) (cdr list))
1834 (index start (1+ index)))
1835 ((or (= index end) (null list) (= count 0)) sequence)
1836 (when (not (funcall test (apply-key key (car list))))
1838 (setq count (1- count)))))
1840 (defun nvector-substitute-if-not* (new test sequence incrementer
1841 start end count key)
1842 (do ((index start (+ index incrementer)))
1843 ((or (= index end) (= count 0)) sequence)
1844 (when (not (funcall test (apply-key key (aref sequence index))))
1845 (setf (aref sequence index) new)
1846 (setq count (1- count)))))
1848 ;;; locater macros used by FIND and POSITION
1850 (eval-when (:compile-toplevel :execute)
1852 (sb!xc:defmacro vector-locater-macro (sequence body-form return-type)
1853 `(let ((incrementer (if from-end -1 1))
1854 (start (if from-end (1- (the fixnum end)) start))
1855 (end (if from-end (1- (the fixnum start)) end)))
1856 (declare (fixnum start end incrementer))
1857 (do ((index start (+ index incrementer))
1858 ,@(case return-type (:position nil) (:element '(current))))
1860 (declare (fixnum index))
1863 (:element `((setf current (aref ,sequence index)))))
1866 (sb!xc:defmacro locater-test-not (item sequence seq-type return-type)
1867 (let ((seq-ref (case return-type
1870 (:vector `(aref ,sequence index))
1871 (:list `(pop ,sequence))))
1872 (:element 'current)))
1873 (return (case return-type
1875 (:element 'current))))
1877 (if (not (funcall test-not ,item (apply-key key ,seq-ref)))
1879 (if (funcall test ,item (apply-key key ,seq-ref))
1880 (return ,return)))))
1882 (sb!xc:defmacro vector-locater (item sequence return-type)
1883 `(vector-locater-macro ,sequence
1884 (locater-test-not ,item ,sequence :vector ,return-type)
1887 (sb!xc:defmacro locater-if-test (test sequence seq-type return-type sense)
1888 (let ((seq-ref (case return-type
1891 (:vector `(aref ,sequence index))
1892 (:list `(pop ,sequence))))
1893 (:element 'current)))
1894 (return (case return-type
1896 (:element 'current))))
1898 `(if (funcall ,test (apply-key key ,seq-ref))
1900 `(if (not (funcall ,test (apply-key key ,seq-ref)))
1901 (return ,return)))))
1903 (sb!xc:defmacro vector-locater-if-macro (test sequence return-type sense)
1904 `(vector-locater-macro ,sequence
1905 (locater-if-test ,test ,sequence :vector ,return-type ,sense)
1908 (sb!xc:defmacro vector-locater-if (test sequence return-type)
1909 `(vector-locater-if-macro ,test ,sequence ,return-type t))
1911 (sb!xc:defmacro vector-locater-if-not (test sequence return-type)
1912 `(vector-locater-if-macro ,test ,sequence ,return-type nil))
1914 (sb!xc:defmacro list-locater-macro (sequence body-form return-type)
1916 (do ((sequence (nthcdr (- (the fixnum (length sequence))
1918 (reverse (the list ,sequence))))
1919 (index (1- (the fixnum end)) (1- index))
1920 (terminus (1- (the fixnum start)))
1921 ,@(case return-type (:position nil) (:element '(current))))
1922 ((or (= index terminus) (null sequence)) ())
1923 (declare (fixnum index terminus))
1926 (:element `((setf current (pop ,sequence)))))
1928 (do ((sequence (nthcdr start ,sequence))
1929 (index start (1+ index))
1930 ,@(case return-type (:position nil) (:element '(current))))
1931 ((or (= index (the fixnum end)) (null sequence)) ())
1932 (declare (fixnum index))
1935 (:element `((setf current (pop ,sequence)))))
1938 (sb!xc:defmacro list-locater (item sequence return-type)
1939 `(list-locater-macro ,sequence
1940 (locater-test-not ,item ,sequence :list ,return-type)
1943 (sb!xc:defmacro list-locater-if-macro (test sequence return-type sense)
1944 `(list-locater-macro ,sequence
1945 (locater-if-test ,test ,sequence :list ,return-type ,sense)
1948 (sb!xc:defmacro list-locater-if (test sequence return-type)
1949 `(list-locater-if-macro ,test ,sequence ,return-type t))
1951 (sb!xc:defmacro list-locater-if-not (test sequence return-type)
1952 `(list-locater-if-macro ,test ,sequence ,return-type nil))
1958 (eval-when (:compile-toplevel :execute)
1960 (sb!xc:defmacro vector-position (item sequence)
1961 `(vector-locater ,item ,sequence :position))
1963 (sb!xc:defmacro list-position (item sequence)
1964 `(list-locater ,item ,sequence :position))
1968 ;;; POSITION cannot default end to the length of sequence since it is not
1969 ;;; an error to supply nil for its value. We must test for END being NIL
1970 ;;; in the body of the function, and this is actually done in the support
1971 ;;; routines for other reasons (see below).
1972 (defun position (item sequence &key from-end (test #'eql) test-not (start 0)
1975 "Returns the zero-origin index of the first element in SEQUENCE
1976 satisfying the test (default is EQL) with the given ITEM"
1977 (seq-dispatch sequence
1978 (list-position* item sequence from-end test test-not start end key)
1979 (vector-position* item sequence from-end test test-not start end key)))
1981 ;;; The support routines for SUBSEQ are used by compiler transforms, so we
1982 ;;; worry about dealing with END being supplied or defaulting to NIL
1985 (defun list-position* (item sequence from-end test test-not start end key)
1986 (declare (fixnum start))
1987 (when (null end) (setf end (length sequence)))
1988 (list-position item sequence))
1990 (defun vector-position* (item sequence from-end test test-not start end key)
1991 (declare (fixnum start))
1992 (when (null end) (setf end (length sequence)))
1993 (vector-position item sequence))
1997 (eval-when (:compile-toplevel :execute)
1999 (sb!xc:defmacro vector-position-if (test sequence)
2000 `(vector-locater-if ,test ,sequence :position))
2002 (sb!xc:defmacro list-position-if (test sequence)
2003 `(list-locater-if ,test ,sequence :position))
2007 (defun position-if (test sequence &key from-end (start 0) key end)
2009 "Returns the zero-origin index of the first element satisfying test(el)"
2010 (declare (fixnum start))
2011 (let ((end (or end (length sequence))))
2012 (declare (type index end))
2013 (seq-dispatch sequence
2014 (list-position-if test sequence)
2015 (vector-position-if test sequence))))
2017 ;;;; POSITION-IF-NOT
2019 (eval-when (:compile-toplevel :execute)
2021 (sb!xc:defmacro vector-position-if-not (test sequence)
2022 `(vector-locater-if-not ,test ,sequence :position))
2024 (sb!xc:defmacro list-position-if-not (test sequence)
2025 `(list-locater-if-not ,test ,sequence :position))
2029 (defun position-if-not (test sequence &key from-end (start 0) key end)
2031 "Returns the zero-origin index of the first element not satisfying test(el)"
2032 (declare (fixnum start))
2033 (let ((end (or end (length sequence))))
2034 (declare (type index end))
2035 (seq-dispatch sequence
2036 (list-position-if-not test sequence)
2037 (vector-position-if-not test sequence))))
2041 (eval-when (:compile-toplevel :execute)
2043 (sb!xc:defmacro vector-find (item sequence)
2044 `(vector-locater ,item ,sequence :element))
2046 (sb!xc:defmacro list-find (item sequence)
2047 `(list-locater ,item ,sequence :element))
2051 ;;; Note: FIND cannot default end to the length of sequence since it
2052 ;;; is not an error to supply NIL for its value. We must test for end
2053 ;;; being NIL in the body of the function, and this is actually done
2054 ;;; in the support routines for other reasons (see above).
2055 (defun find (item sequence &key from-end (test #'eql) test-not (start 0)
2058 "Returns the first element in SEQUENCE satisfying the test (default
2059 is EQL) with the given ITEM"
2060 (declare (fixnum start))
2061 (seq-dispatch sequence
2062 (list-find* item sequence from-end test test-not start end key)
2063 (vector-find* item sequence from-end test test-not start end key)))
2065 ;;; The support routines for FIND are used by compiler transforms, so we
2066 ;;; worry about dealing with END being supplied or defaulting to NIL
2069 (defun list-find* (item sequence from-end test test-not start end key)
2070 (when (null end) (setf end (length sequence)))
2071 (list-find item sequence))
2073 (defun vector-find* (item sequence from-end test test-not start end key)
2074 (when (null end) (setf end (length sequence)))
2075 (vector-find item sequence))
2077 ;;;; FIND-IF and FIND-IF-NOT
2079 (eval-when (:compile-toplevel :execute)
2081 (sb!xc:defmacro vector-find-if (test sequence)
2082 `(vector-locater-if ,test ,sequence :element))
2084 (sb!xc:defmacro list-find-if (test sequence)
2085 `(list-locater-if ,test ,sequence :element))
2089 (defun find-if (test sequence &key from-end (start 0) end key)
2091 "Returns the zero-origin index of the first element satisfying the test."
2092 (declare (fixnum start))
2093 (let ((end (or end (length sequence))))
2094 (declare (type index end))
2095 (seq-dispatch sequence
2096 (list-find-if test sequence)
2097 (vector-find-if test sequence))))
2099 (eval-when (:compile-toplevel :execute)
2101 (sb!xc:defmacro vector-find-if-not (test sequence)
2102 `(vector-locater-if-not ,test ,sequence :element))
2104 (sb!xc:defmacro list-find-if-not (test sequence)
2105 `(list-locater-if-not ,test ,sequence :element))
2109 (defun find-if-not (test sequence &key from-end (start 0) end key)
2111 "Returns the zero-origin index of the first element not satisfying the test."
2112 (declare (fixnum start))
2113 (let ((end (or end (length sequence))))
2114 (declare (type index end))
2115 (seq-dispatch sequence
2116 (list-find-if-not test sequence)
2117 (vector-find-if-not test sequence))))
2121 (eval-when (:compile-toplevel :execute)
2123 (sb!xc:defmacro vector-count (item sequence)
2124 `(do ((index start (1+ index))
2126 ((= index (the fixnum end)) count)
2127 (declare (fixnum index count))
2129 (unless (funcall test-not ,item
2130 (apply-key key (aref ,sequence index)))
2131 (setq count (1+ count)))
2132 (when (funcall test ,item (apply-key key (aref ,sequence index)))
2133 (setq count (1+ count))))))
2135 (sb!xc:defmacro list-count (item sequence)
2136 `(do ((sequence (nthcdr start ,sequence))
2137 (index start (1+ index))
2139 ((or (= index (the fixnum end)) (null sequence)) count)
2140 (declare (fixnum index count))
2142 (unless (funcall test-not ,item (apply-key key (pop sequence)))
2143 (setq count (1+ count)))
2144 (when (funcall test ,item (apply-key key (pop sequence)))
2145 (setq count (1+ count))))))
2149 (defun count (item sequence &key from-end (test #'eql) test-not (start 0)
2152 "Returns the number of elements in SEQUENCE satisfying a test with ITEM,
2153 which defaults to EQL."
2154 (declare (ignore from-end) (fixnum start))
2155 (let ((end (or end (length sequence))))
2156 (declare (type index end))
2157 (seq-dispatch sequence
2158 (list-count item sequence)
2159 (vector-count item sequence))))
2161 ;;;; COUNT-IF and COUNT-IF-NOT
2163 (eval-when (:compile-toplevel :execute)
2165 (sb!xc:defmacro vector-count-if (predicate sequence)
2166 `(do ((index start (1+ index))
2168 ((= index (the fixnum end)) count)
2169 (declare (fixnum index count))
2170 (if (funcall ,predicate (apply-key key (aref ,sequence index)))
2171 (setq count (1+ count)))))
2173 (sb!xc:defmacro list-count-if (predicate sequence)
2174 `(do ((sequence (nthcdr start ,sequence))
2175 (index start (1+ index))
2177 ((or (= index (the fixnum end)) (null sequence)) count)
2178 (declare (fixnum index count))
2179 (if (funcall ,predicate (apply-key key (pop sequence)))
2180 (setq count (1+ count)))))
2184 (defun count-if (test sequence &key from-end (start 0) end key)
2186 "Returns the number of elements in SEQUENCE satisfying TEST(el)."
2187 (declare (ignore from-end) (fixnum start))
2188 (let ((end (or end (length sequence))))
2189 (declare (type index end))
2190 (seq-dispatch sequence
2191 (list-count-if test sequence)
2192 (vector-count-if test sequence))))
2194 (eval-when (:compile-toplevel :execute)
2196 (sb!xc:defmacro vector-count-if-not (predicate sequence)
2197 `(do ((index start (1+ index))
2199 ((= index (the fixnum end)) count)
2200 (declare (fixnum index count))
2201 (if (not (funcall ,predicate (apply-key key (aref ,sequence index))))
2202 (setq count (1+ count)))))
2204 (sb!xc:defmacro list-count-if-not (predicate sequence)
2205 `(do ((sequence (nthcdr start ,sequence))
2206 (index start (1+ index))
2208 ((or (= index (the fixnum end)) (null sequence)) count)
2209 (declare (fixnum index count))
2210 (if (not (funcall ,predicate (apply-key key (pop sequence))))
2211 (setq count (1+ count)))))
2215 (defun count-if-not (test sequence &key from-end (start 0) end key)
2217 "Returns the number of elements in SEQUENCE not satisfying TEST(el)."
2218 (declare (ignore from-end) (fixnum start))
2219 (let ((end (or end (length sequence))))
2220 (declare (type index end))
2221 (seq-dispatch sequence
2222 (list-count-if-not test sequence)
2223 (vector-count-if-not test sequence))))
2227 (eval-when (:compile-toplevel :execute)
2229 (sb!xc:defmacro match-vars (&rest body)
2230 `(let ((inc (if from-end -1 1))
2231 (start1 (if from-end (1- (the fixnum end1)) start1))
2232 (start2 (if from-end (1- (the fixnum end2)) start2))
2233 (end1 (if from-end (1- (the fixnum start1)) end1))
2234 (end2 (if from-end (1- (the fixnum start2)) end2)))
2235 (declare (fixnum inc start1 start2 end1 end2))
2238 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2239 (declare (ignore end)) ;; ### Should END be used below?
2240 `(let ((,sequence (if from-end
2241 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2242 (reverse (the list ,sequence)))
2243 (nthcdr ,start ,sequence))))
2244 (declare (type list ,sequence))
2249 (eval-when (:compile-toplevel :execute)
2251 (sb!xc:defmacro if-mismatch (elt1 elt2)
2252 `(cond ((= (the fixnum index1) (the fixnum end1))
2253 (return (if (= (the fixnum index2) (the fixnum end2))
2256 (1+ (the fixnum index1))
2257 (the fixnum index1)))))
2258 ((= (the fixnum index2) (the fixnum end2))
2259 (return (if from-end (1+ (the fixnum index1)) index1)))
2261 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2262 (return (if from-end (1+ (the fixnum index1)) index1))))
2263 (t (if (not (funcall test (apply-key key ,elt1)
2264 (apply-key key ,elt2)))
2265 (return (if from-end (1+ (the fixnum index1)) index1))))))
2267 (sb!xc:defmacro mumble-mumble-mismatch ()
2268 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2269 (index2 start2 (+ index2 (the fixnum inc))))
2271 (declare (fixnum index1 index2))
2272 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2274 (sb!xc:defmacro mumble-list-mismatch ()
2275 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2276 (index2 start2 (+ index2 (the fixnum inc))))
2278 (declare (fixnum index1 index2))
2279 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2281 (sb!xc:defmacro list-mumble-mismatch ()
2282 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2283 (index2 start2 (+ index2 (the fixnum inc))))
2285 (declare (fixnum index1 index2))
2286 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2288 (sb!xc:defmacro list-list-mismatch ()
2289 `(do ((sequence1 sequence1)
2290 (sequence2 sequence2)
2291 (index1 start1 (+ index1 (the fixnum inc)))
2292 (index2 start2 (+ index2 (the fixnum inc))))
2294 (declare (fixnum index1 index2))
2295 (if-mismatch (pop sequence1) (pop sequence2))))
2299 (defun mismatch (sequence1 sequence2 &key from-end (test #'eql) test-not
2300 (start1 0) end1 (start2 0) end2 key)
2302 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2303 element-wise. If they are of equal length and match in every element, the
2304 result is Nil. Otherwise, the result is a non-negative integer, the index
2305 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2306 if one is shorter than and a matching prefix of the other, the index within
2307 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2308 :FROM-END argument is given, then one plus the index of the rightmost
2309 position in which the sequences differ is returned."
2310 (declare (fixnum start1 start2))
2311 (let* ((length1 (length sequence1))
2312 (end1 (or end1 length1))
2313 (length2 (length sequence2))
2314 (end2 (or end2 length2)))
2315 (declare (type index length1 end1 length2 end2))
2317 (seq-dispatch sequence1
2318 (matchify-list (sequence1 start1 length1 end1)
2319 (seq-dispatch sequence2
2320 (matchify-list (sequence2 start2 length2 end2)
2321 (list-list-mismatch))
2322 (list-mumble-mismatch)))
2323 (seq-dispatch sequence2
2324 (matchify-list (sequence2 start2 length2 end2)
2325 (mumble-list-mismatch))
2326 (mumble-mumble-mismatch))))))
2328 ;;; search comparison functions
2330 (eval-when (:compile-toplevel :execute)
2332 ;;; Compare two elements and return if they don't match.
2333 (sb!xc:defmacro compare-elements (elt1 elt2)
2335 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2338 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2342 (sb!xc:defmacro search-compare-list-list (main sub)
2343 `(do ((main ,main (cdr main))
2344 (jndex start1 (1+ jndex))
2345 (sub (nthcdr start1 ,sub) (cdr sub)))
2346 ((or (null main) (null sub) (= (the fixnum end1) jndex))
2348 (declare (fixnum jndex))
2349 (compare-elements (car main) (car sub))))
2351 (sb!xc:defmacro search-compare-list-vector (main sub)
2352 `(do ((main ,main (cdr main))
2353 (index start1 (1+ index)))
2354 ((or (null main) (= index (the fixnum end1))) t)
2355 (declare (fixnum index))
2356 (compare-elements (car main) (aref ,sub index))))
2358 (sb!xc:defmacro search-compare-vector-list (main sub index)
2359 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2360 (jndex start1 (1+ jndex))
2361 (index ,index (1+ index)))
2362 ((or (= (the fixnum end1) jndex) (null sub)) t)
2363 (declare (fixnum jndex index))
2364 (compare-elements (aref ,main index) (car sub))))
2366 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2367 `(do ((index ,index (1+ index))
2368 (sub-index start1 (1+ sub-index)))
2369 ((= sub-index (the fixnum end1)) t)
2370 (declare (fixnum sub-index index))
2371 (compare-elements (aref ,main index) (aref ,sub sub-index))))
2373 (sb!xc:defmacro search-compare (main-type main sub index)
2374 (if (eq main-type 'list)
2376 (search-compare-list-list ,main ,sub)
2377 (search-compare-list-vector ,main ,sub))
2379 (search-compare-vector-list ,main ,sub ,index)
2380 (search-compare-vector-vector ,main ,sub ,index))))
2386 (eval-when (:compile-toplevel :execute)
2388 (sb!xc:defmacro list-search (main sub)
2389 `(do ((main (nthcdr start2 ,main) (cdr main))
2390 (index2 start2 (1+ index2))
2391 (terminus (- (the fixnum end2)
2392 (the fixnum (- (the fixnum end1)
2393 (the fixnum start1)))))
2395 ((> index2 terminus) last-match)
2396 (declare (fixnum index2 terminus))
2397 (if (search-compare list main ,sub index2)
2399 (setq last-match index2)
2402 (sb!xc:defmacro vector-search (main sub)
2403 `(do ((index2 start2 (1+ index2))
2404 (terminus (- (the fixnum end2)
2405 (the fixnum (- (the fixnum end1)
2406 (the fixnum start1)))))
2408 ((> index2 terminus) last-match)
2409 (declare (fixnum index2 terminus))
2410 (if (search-compare vector ,main ,sub index2)
2412 (setq last-match index2)
2417 (defun search (sequence1 sequence2 &key from-end (test #'eql) test-not
2418 (start1 0) end1 (start2 0) end2 key)
2419 (declare (fixnum start1 start2))
2420 (let ((end1 (or end1 (length sequence1)))
2421 (end2 (or end2 (length sequence2))))
2422 (seq-dispatch sequence2
2423 (list-search sequence2 sequence1)
2424 (vector-search sequence2 sequence1))))