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 tends 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 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
39 `(if (typep ,sequence 'list)
42 ;; This is only called from places which have already deduced
43 ;; that the SEQUENCE argument is actually a sequence. So
44 ;; this would be a candidate place for (AVER (TYPEP ,SEQUENCE
45 ;; 'VECTOR)), except that this seems to be a performance
48 :element-type (array-element-type ,sequence)))))
50 (sb!xc:defmacro bad-sequence-type-error (type-spec)
51 `(error 'simple-type-error
53 ;; FIXME: This is actually wrong, and should be something
54 ;; like (SATISFIES IS-A-VALID-SEQUENCE-TYPE-SPECIFIER-P).
55 :expected-type 'sequence
56 :format-control "~S is a bad type specifier for sequences."
57 :format-arguments (list ,type-spec)))
61 ;;; It's possible with some sequence operations to declare the length
62 ;;; of a result vector, and to be safe, we really ought to verify that
63 ;;; the actual result has the declared length.
64 (defun vector-of-checked-length-given-length (vector declared-length)
65 (declare (type vector vector))
66 (declare (type index declared-length))
67 (let ((actual-length (length vector)))
68 (unless (= actual-length declared-length)
69 (error 'simple-type-error
71 :expected-type `(vector ,declared-length)
73 "Vector length (~W) doesn't match declared length (~W)."
74 :format-arguments (list actual-length declared-length))))
76 (defun sequence-of-checked-length-given-type (sequence result-type)
77 (let ((ctype (specifier-type result-type)))
78 (if (not (array-type-p ctype))
80 (let ((declared-length (first (array-type-dimensions ctype))))
81 (if (eq declared-length '*)
83 (vector-of-checked-length-given-length sequence
86 (defun signal-index-too-large-error (sequence index)
87 (let* ((length (length sequence))
88 (max-index (and (plusp length)
90 (error 'index-too-large-error
92 :expected-type (if max-index
93 `(integer 0 ,max-index)
94 ;; This seems silly, is there something better?
95 '(integer (0) (0))))))
97 (defun signal-end-too-large-error (sequence end)
98 (let* ((length (length sequence))
99 (max-end (and (not (minusp length))
101 (error 'end-too-large-error
103 :expected-type (if max-end
104 `(integer 0 ,max-end)
105 ;; This seems silly, is there something better?
108 (declaim (inline adjust-count)
109 (ftype (function (sequence-count) index) adjust-count))
110 (defun adjust-count (count)
111 (cond ((not count) most-positive-fixnum)
116 (defun elt (sequence index)
117 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
120 (do ((count index (1- count))
121 (list sequence (cdr list)))
124 (signal-index-too-large-error sequence index)
126 (declare (type (integer 0) count))))
128 (when (>= index (length sequence))
129 (signal-index-too-large-error sequence index))
130 (aref sequence index))))
132 (defun %setelt (sequence index newval)
133 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
136 (do ((count index (1- count))
138 ((= count 0) (rplaca seq newval) newval)
139 (declare (fixnum count))
141 (signal-index-too-large-error sequence index)
142 (setq seq (cdr seq)))))
144 (when (>= index (length sequence))
145 (signal-index-too-large-error sequence index))
146 (setf (aref sequence index) newval))))
148 (defun length (sequence)
149 #!+sb-doc "Return an integer that is the length of SEQUENCE."
151 (vector (length (truly-the vector sequence)))
152 (list (length (truly-the list sequence)))))
154 (defun make-sequence (type length &key (initial-element NIL iep))
156 "Return a sequence of the given TYPE and LENGTH, with elements initialized
157 to :INITIAL-ELEMENT."
158 (declare (fixnum length))
159 (let ((type (specifier-type type)))
160 (cond ((csubtypep type (specifier-type 'list))
161 (make-list length :initial-element initial-element))
162 ((csubtypep type (specifier-type 'vector))
163 (if (typep type 'array-type)
164 ;; KLUDGE: the above test essentially asks "Do we know
165 ;; what the upgraded-array-element-type is?" [consider
166 ;; (OR STRING BIT-VECTOR)]
168 (aver (= (length (array-type-dimensions type)) 1))
169 (let ((etype (type-specifier
170 (array-type-specialized-element-type type)))
171 (type-length (car (array-type-dimensions type))))
172 (unless (or (eq type-length '*)
173 (= type-length length))
174 (error 'simple-type-error
176 :expected-type `(eql ,type-length)
177 :format-control "The length requested (~S) ~
178 does not match the length type restriction in ~S."
179 :format-arguments (list length
180 (type-specifier type))))
181 ;; FIXME: These calls to MAKE-ARRAY can't be
182 ;; open-coded, as the :ELEMENT-TYPE argument isn't
183 ;; constant. Probably we ought to write a
184 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
187 (make-array length :element-type etype
188 :initial-element initial-element)
189 (make-array length :element-type etype))))
190 ;; We have a subtype of VECTOR, but it isn't an array
191 ;; type. Maybe this should be a BUG instead?
192 (error 'simple-type-error
194 :expected-type 'sequence
195 :format-control "~S is too hairy for MAKE-SEQUENCE."
196 :format-arguments (list (type-specifier type)))))
197 (t (bad-sequence-type-error (type-specifier type))))))
201 ;;;; The support routines for SUBSEQ are used by compiler transforms,
202 ;;;; so we worry about dealing with END being supplied or defaulting
203 ;;;; to NIL at this level.
205 (defun vector-subseq* (sequence start &optional end)
206 (declare (type vector sequence))
207 (declare (type fixnum start))
208 (declare (type (or null fixnum) end))
210 (setf end (length sequence))
211 (unless (<= end (length sequence))
212 (signal-index-too-large-error sequence end)))
213 (do ((old-index start (1+ old-index))
214 (new-index 0 (1+ new-index))
215 (copy (make-sequence-like sequence (- end start))))
216 ((= old-index end) copy)
217 (declare (fixnum old-index new-index))
218 (setf (aref copy new-index)
219 (aref sequence old-index))))
221 (defun list-subseq* (sequence start &optional end)
222 (declare (type list sequence))
223 (declare (type fixnum start))
224 (declare (type (or null fixnum) end))
225 (if (and end (>= start (the fixnum end)))
227 (let* ((groveled (nthcdr start sequence))
228 (result (list (car groveled))))
230 (do ((list (cdr groveled) (cdr list))
231 (splice result (cdr (rplacd splice (list (car list)))))
232 (index (1+ start) (1+ index)))
233 ((or (atom list) (and end (= index (the fixnum end))))
235 (declare (fixnum index)))
238 ;;; SUBSEQ cannot default END to the length of sequence since it is
239 ;;; not an error to supply NIL for its value. We must test for END
240 ;;; being NIL in the body of the function, and this is actually done
241 ;;; in the support routines for other reasons. (See above.)
242 (defun subseq (sequence start &optional end)
244 "Return a copy of a subsequence of SEQUENCE starting with element number
245 START and continuing to the end of SEQUENCE or the optional END."
246 (seq-dispatch sequence
247 (list-subseq* sequence start end)
248 (vector-subseq* sequence start end)))
252 (eval-when (:compile-toplevel :execute)
254 (sb!xc:defmacro vector-copy-seq (sequence)
255 `(let ((length (length (the vector ,sequence))))
256 (declare (fixnum length))
257 (do ((index 0 (1+ index))
258 (copy (make-sequence-like ,sequence length)))
259 ((= index length) copy)
260 (declare (fixnum index))
261 (setf (aref copy index) (aref ,sequence index)))))
263 (sb!xc:defmacro list-copy-seq (list)
264 `(if (atom ,list) '()
265 (let ((result (cons (car ,list) '()) ))
266 (do ((x (cdr ,list) (cdr x))
268 (cdr (rplacd splice (cons (car x) '() ))) ))
269 ((atom x) (unless (null x)
275 (defun copy-seq (sequence)
276 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
277 (seq-dispatch sequence
278 (list-copy-seq* sequence)
279 (vector-copy-seq* sequence)))
283 (defun list-copy-seq* (sequence)
284 (list-copy-seq sequence))
286 (defun vector-copy-seq* (sequence)
287 (declare (type vector sequence))
288 (vector-copy-seq sequence))
292 (eval-when (:compile-toplevel :execute)
294 (sb!xc:defmacro vector-fill (sequence item start end)
295 `(do ((index ,start (1+ index)))
296 ((= index (the fixnum ,end)) ,sequence)
297 (declare (fixnum index))
298 (setf (aref ,sequence index) ,item)))
300 (sb!xc:defmacro list-fill (sequence item start end)
301 `(do ((current (nthcdr ,start ,sequence) (cdr current))
302 (index ,start (1+ index)))
303 ((or (atom current) (and end (= index (the fixnum ,end))))
305 (declare (fixnum index))
306 (rplaca current ,item)))
310 ;;; The support routines for FILL are used by compiler transforms, so we
311 ;;; worry about dealing with END being supplied or defaulting to NIL
314 (defun list-fill* (sequence item start end)
315 (declare (list sequence))
316 (list-fill sequence item start end))
318 (defun vector-fill* (sequence item start end)
319 (declare (vector sequence))
320 (when (null end) (setq end (length sequence)))
321 (vector-fill sequence item start end))
323 ;;; FILL cannot default end to the length of sequence since it is not
324 ;;; an error to supply nil for its value. We must test for end being nil
325 ;;; in the body of the function, and this is actually done in the support
326 ;;; routines for other reasons (see above).
327 (defun fill (sequence item &key (start 0) end)
328 #!+sb-doc "Replace the specified elements of SEQUENCE with ITEM."
329 (seq-dispatch sequence
330 (list-fill* sequence item start end)
331 (vector-fill* sequence item start end)))
335 (eval-when (:compile-toplevel :execute)
337 ;;; If we are copying around in the same vector, be careful not to copy the
338 ;;; same elements over repeatedly. We do this by copying backwards.
339 (sb!xc:defmacro mumble-replace-from-mumble ()
340 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
341 (let ((nelts (min (- target-end target-start)
342 (- source-end source-start))))
343 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
345 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
347 ((= target-index (the fixnum (1- target-start))) target-sequence)
348 (declare (fixnum target-index source-index))
349 (setf (aref target-sequence target-index)
350 (aref source-sequence source-index))))
351 (do ((target-index target-start (1+ target-index))
352 (source-index source-start (1+ source-index)))
353 ((or (= target-index (the fixnum target-end))
354 (= source-index (the fixnum source-end)))
356 (declare (fixnum target-index source-index))
357 (setf (aref target-sequence target-index)
358 (aref source-sequence source-index)))))
360 (sb!xc:defmacro list-replace-from-list ()
361 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
362 (let ((new-elts (subseq source-sequence source-start
363 (+ (the fixnum source-start)
365 (min (- (the fixnum target-end)
366 (the fixnum target-start))
367 (- (the fixnum source-end)
368 (the fixnum source-start))))))))
369 (do ((n new-elts (cdr n))
370 (o (nthcdr target-start target-sequence) (cdr o)))
371 ((null n) target-sequence)
373 (do ((target-index target-start (1+ target-index))
374 (source-index source-start (1+ source-index))
375 (target-sequence-ref (nthcdr target-start target-sequence)
376 (cdr target-sequence-ref))
377 (source-sequence-ref (nthcdr source-start source-sequence)
378 (cdr source-sequence-ref)))
379 ((or (= target-index (the fixnum target-end))
380 (= source-index (the fixnum source-end))
381 (null target-sequence-ref) (null source-sequence-ref))
383 (declare (fixnum target-index source-index))
384 (rplaca target-sequence-ref (car source-sequence-ref)))))
386 (sb!xc:defmacro list-replace-from-mumble ()
387 `(do ((target-index target-start (1+ target-index))
388 (source-index source-start (1+ source-index))
389 (target-sequence-ref (nthcdr target-start target-sequence)
390 (cdr target-sequence-ref)))
391 ((or (= target-index (the fixnum target-end))
392 (= source-index (the fixnum source-end))
393 (null target-sequence-ref))
395 (declare (fixnum source-index target-index))
396 (rplaca target-sequence-ref (aref source-sequence source-index))))
398 (sb!xc:defmacro mumble-replace-from-list ()
399 `(do ((target-index target-start (1+ target-index))
400 (source-index source-start (1+ source-index))
401 (source-sequence (nthcdr source-start source-sequence)
402 (cdr source-sequence)))
403 ((or (= target-index (the fixnum target-end))
404 (= source-index (the fixnum source-end))
405 (null source-sequence))
407 (declare (fixnum target-index source-index))
408 (setf (aref target-sequence target-index) (car source-sequence))))
412 ;;;; The support routines for REPLACE are used by compiler transforms, so we
413 ;;;; worry about dealing with END being supplied or defaulting to NIL
416 (defun list-replace-from-list* (target-sequence source-sequence target-start
417 target-end source-start source-end)
418 (when (null target-end) (setq target-end (length target-sequence)))
419 (when (null source-end) (setq source-end (length source-sequence)))
420 (list-replace-from-list))
422 (defun list-replace-from-vector* (target-sequence source-sequence target-start
423 target-end source-start source-end)
424 (when (null target-end) (setq target-end (length target-sequence)))
425 (when (null source-end) (setq source-end (length source-sequence)))
426 (list-replace-from-mumble))
428 (defun vector-replace-from-list* (target-sequence source-sequence target-start
429 target-end source-start source-end)
430 (when (null target-end) (setq target-end (length target-sequence)))
431 (when (null source-end) (setq source-end (length source-sequence)))
432 (mumble-replace-from-list))
434 (defun vector-replace-from-vector* (target-sequence source-sequence
435 target-start target-end source-start
437 (when (null target-end) (setq target-end (length target-sequence)))
438 (when (null source-end) (setq source-end (length source-sequence)))
439 (mumble-replace-from-mumble))
441 ;;; REPLACE cannot default END arguments to the length of SEQUENCE since it
442 ;;; is not an error to supply NIL for their values. We must test for ENDs
443 ;;; being NIL in the body of the function.
444 (defun replace (target-sequence source-sequence &key
445 ((:start1 target-start) 0)
447 ((:start2 source-start) 0)
448 ((:end2 source-end)))
450 "The target sequence is destructively modified by copying successive
451 elements into it from the source sequence."
452 (let ((target-end (or target-end (length target-sequence)))
453 (source-end (or source-end (length source-sequence))))
454 (seq-dispatch target-sequence
455 (seq-dispatch source-sequence
456 (list-replace-from-list)
457 (list-replace-from-mumble))
458 (seq-dispatch source-sequence
459 (mumble-replace-from-list)
460 (mumble-replace-from-mumble)))))
464 (eval-when (:compile-toplevel :execute)
466 (sb!xc:defmacro vector-reverse (sequence type)
467 `(let ((length (length ,sequence)))
468 (declare (fixnum length))
469 (do ((forward-index 0 (1+ forward-index))
470 (backward-index (1- length) (1- backward-index))
471 (new-sequence (make-sequence ,type length)))
472 ((= forward-index length) new-sequence)
473 (declare (fixnum forward-index backward-index))
474 (setf (aref new-sequence forward-index)
475 (aref ,sequence backward-index)))))
477 (sb!xc:defmacro list-reverse-macro (sequence)
479 ((atom ,sequence) new-list)
480 (push (pop ,sequence) new-list)))
484 (defun reverse (sequence)
486 "Return a new sequence containing the same elements but in reverse order."
487 (seq-dispatch sequence
488 (list-reverse* sequence)
489 (vector-reverse* sequence)))
493 (defun list-reverse* (sequence)
494 (list-reverse-macro sequence))
496 (defun vector-reverse* (sequence)
497 (vector-reverse sequence (type-of sequence)))
501 (eval-when (:compile-toplevel :execute)
503 (sb!xc:defmacro vector-nreverse (sequence)
504 `(let ((length (length (the vector ,sequence))))
505 (declare (fixnum length))
506 (do ((left-index 0 (1+ left-index))
507 (right-index (1- length) (1- right-index))
508 (half-length (truncate length 2)))
509 ((= left-index half-length) ,sequence)
510 (declare (fixnum left-index right-index half-length))
511 (rotatef (aref ,sequence left-index)
512 (aref ,sequence right-index)))))
514 (sb!xc:defmacro list-nreverse-macro (list)
515 `(do ((1st (cdr ,list) (if (atom 1st) 1st (cdr 1st)))
523 (defun list-nreverse* (sequence)
524 (list-nreverse-macro sequence))
526 (defun vector-nreverse* (sequence)
527 (vector-nreverse sequence))
529 (defun nreverse (sequence)
531 "Return a sequence of the same elements in reverse order; the argument
533 (seq-dispatch sequence
534 (list-nreverse* sequence)
535 (vector-nreverse* sequence)))
539 (eval-when (:compile-toplevel :execute)
541 (sb!xc:defmacro concatenate-to-list (sequences)
542 `(let ((result (list nil)))
543 (do ((sequences ,sequences (cdr sequences))
545 ((null sequences) (cdr result))
546 (let ((sequence (car sequences)))
547 ;; FIXME: It appears to me that this and CONCATENATE-TO-MUMBLE
548 ;; could benefit from a DO-SEQUENCE macro.
549 (seq-dispatch sequence
550 (do ((sequence sequence (cdr sequence)))
553 (cdr (rplacd splice (list (car sequence))))))
554 (do ((index 0 (1+ index))
555 (length (length sequence)))
557 (declare (fixnum index length))
560 (list (aref sequence index)))))))))))
562 (sb!xc:defmacro concatenate-to-mumble (output-type-spec sequences)
563 `(do ((seqs ,sequences (cdr seqs))
567 (do ((sequences ,sequences (cdr sequences))
568 (lengths lengths (cdr lengths))
570 (result (make-sequence ,output-type-spec total-length)))
571 ((= index total-length) result)
572 (declare (fixnum index))
573 (let ((sequence (car sequences)))
574 (seq-dispatch sequence
575 (do ((sequence sequence (cdr sequence)))
577 (setf (aref result index) (car sequence))
578 (setq index (1+ index)))
579 (do ((jndex 0 (1+ jndex))
580 (this-length (car lengths)))
581 ((= jndex this-length))
582 (declare (fixnum jndex this-length))
583 (setf (aref result index)
584 (aref sequence jndex))
585 (setq index (1+ index)))))))
586 (let ((length (length (car seqs))))
587 (declare (fixnum length))
588 (setq lengths (nconc lengths (list length)))
589 (setq total-length (+ total-length length)))))
593 (defun concatenate (output-type-spec &rest sequences)
595 "Return a new sequence of all the argument sequences concatenated together
596 which shares no structure with the original argument sequences of the
597 specified OUTPUT-TYPE-SPEC."
598 (let ((type (specifier-type output-type-spec)))
600 ((csubtypep type (specifier-type 'vector))
601 (apply #'concat-to-simple* output-type-spec sequences))
602 ((csubtypep type (specifier-type 'list))
603 (apply #'concat-to-list* sequences))
605 (bad-sequence-type-error output-type-spec)))))
608 ;;; FIXME: These are weird. They're never called anywhere except in
609 ;;; CONCATENATE. It seems to me that the macros ought to just
610 ;;; be expanded directly in CONCATENATE, or in CONCATENATE-STRING
611 ;;; and CONCATENATE-LIST variants. Failing that, these ought to be local
612 ;;; functions (FLET).
613 (defun concat-to-list* (&rest sequences)
614 (concatenate-to-list sequences))
615 (defun concat-to-simple* (type &rest sequences)
616 (concatenate-to-mumble type sequences))
618 ;;;; MAP and MAP-INTO
620 ;;; helper functions to handle arity-1 subcases of MAP
621 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
622 (declaim (ftype (function (function sequence) simple-vector)
623 %map-simple-vector-arity-1))
624 (macrolet ((dosequence ((i sequence) &body body)
625 (once-only ((sequence sequence))
626 `(etypecase ,sequence
627 (list (dolist (,i ,sequence) ,@body))
628 (simple-vector (dovector (,i sequence) ,@body))
629 (vector (dovector (,i sequence) ,@body))))))
630 (defun %map-to-list-arity-1 (fun sequence)
631 (let ((reversed-result nil)
632 (really-fun (%coerce-callable-to-fun fun)))
633 (dosequence (element sequence)
634 (push (funcall really-fun element)
636 (nreverse reversed-result)))
637 (defun %map-to-simple-vector-arity-1 (fun sequence)
638 (let ((result (make-array (length sequence)))
640 (really-fun (%coerce-callable-to-fun fun)))
641 (declare (type index index))
642 (dosequence (element sequence)
643 (setf (aref result index)
644 (funcall really-fun element))
647 (defun %map-for-effect-arity-1 (fun sequence)
648 (let ((really-fun (%coerce-callable-to-fun fun)))
649 (dosequence (element sequence)
650 (funcall really-fun element)))
653 ;;; helper functions to handle arity-N subcases of MAP
655 ;;; KLUDGE: This is hairier, and larger, than need be, because we
656 ;;; don't have DYNAMIC-EXTENT. With DYNAMIC-EXTENT, we could define
657 ;;; %MAP-FOR-EFFECT, and then implement the
658 ;;; other %MAP-TO-FOO functions reasonably efficiently by passing closures to
659 ;;; %MAP-FOR-EFFECT. (DYNAMIC-EXTENT would help a little by avoiding
660 ;;; consing each closure, and would help a lot by allowing us to define
661 ;;; a closure (LAMBDA (&REST REST) <do something with (APPLY FUN REST)>)
662 ;;; with the REST list allocated with DYNAMIC-EXTENT. -- WHN 20000920
663 (macrolet (;; Execute BODY in a context where the machinery for
664 ;; UPDATED-MAP-APPLY-ARGS has been set up.
665 (with-map-state (sequences &body body)
666 `(let* ((%sequences ,sequences)
667 (%iters (mapcar (lambda (sequence)
672 (%apply-args (make-list (length %sequences))))
673 (declare (type list %sequences %iters %apply-args))
675 ;; Return a list of args to pass to APPLY for the next
676 ;; function call in the mapping, or NIL if no more function
677 ;; calls should be made (because we've reached the end of a
679 (updated-map-apply-args ()
680 '(do ((in-sequences %sequences (cdr in-sequences))
681 (in-iters %iters (cdr in-iters))
682 (in-apply-args %apply-args (cdr in-apply-args)))
685 (declare (type list in-sequences in-iters in-apply-args))
686 (let ((i (car in-iters)))
687 (declare (type (or list index) i))
689 (if (null i) ; if end of this sequence
691 (setf (car in-apply-args) (car i)
692 (car in-iters) (cdr i)))
693 (let ((v (the vector (car in-sequences))))
694 (if (>= i (length v)) ; if end of this sequence
696 (setf (car in-apply-args) (aref v i)
697 (car in-iters) (1+ i)))))))))
698 (defun %map-to-list (func sequences)
699 (declare (type function func))
700 (declare (type list sequences))
701 (with-map-state sequences
702 (loop with updated-map-apply-args
703 while (setf updated-map-apply-args (updated-map-apply-args))
704 collect (apply func updated-map-apply-args))))
705 (defun %map-to-vector (output-type-spec func sequences)
706 (declare (type function func))
707 (declare (type list sequences))
708 (let ((min-len (with-map-state sequences
709 (do ((counter 0 (1+ counter)))
710 ;; Note: Doing everything in
711 ;; UPDATED-MAP-APPLY-ARGS here is somewhat
712 ;; wasteful; we even do some extra consing.
713 ;; And stepping over every element of
714 ;; VECTORs, instead of just grabbing their
715 ;; LENGTH, is also wasteful. But it's easy
716 ;; and safe. (If you do rewrite it, please
717 ;; try to make sure that
718 ;; (MAP NIL #'F SOME-CIRCULAR-LIST #(1))
719 ;; does the right thing.)
720 ((not (updated-map-apply-args))
722 (declare (type index counter))))))
723 (declare (type index min-len))
724 (with-map-state sequences
725 (let ((result (make-sequence output-type-spec min-len))
727 (declare (type index index))
728 (loop with updated-map-apply-args
729 while (setf updated-map-apply-args (updated-map-apply-args))
731 (setf (aref result index)
732 (apply func updated-map-apply-args))
735 (defun %map-for-effect (func sequences)
736 (declare (type function func))
737 (declare (type list sequences))
738 (with-map-state sequences
739 (loop with updated-map-apply-args
740 while (setf updated-map-apply-args (updated-map-apply-args))
742 (apply func updated-map-apply-args))
745 "FUNCTION must take as many arguments as there are sequences provided.
746 The result is a sequence of type OUTPUT-TYPE-SPEC such that element I
747 is the result of applying FUNCTION to element I of each of the argument
750 ;;; %MAP is just MAP without the final just-to-be-sure check that
751 ;;; length of the output sequence matches any length specified
753 (defun %map (result-type function first-sequence &rest more-sequences)
754 (let ((really-fun (%coerce-callable-to-fun function))
755 (type (specifier-type result-type)))
756 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
757 ;; it into something which can be DEFTRANSFORMed away. (It's
758 ;; fairly important to handle this case efficiently, since
759 ;; quantifiers like SOME are transformed into this case, and since
760 ;; there's no consing overhead to dwarf our inefficiency.)
761 (if (and (null more-sequences)
763 (%map-for-effect-arity-1 really-fun first-sequence)
764 ;; Otherwise, use the industrial-strength full-generality
765 ;; approach, consing O(N-ARGS) temporary storage (which can have
766 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
767 (let ((sequences (cons first-sequence more-sequences)))
769 ((eq type *empty-type*) (%map-for-effect really-fun sequences))
770 ((csubtypep type (specifier-type 'list))
771 (%map-to-list really-fun sequences))
772 ((csubtypep type (specifier-type 'vector))
773 (%map-to-vector result-type really-fun sequences))
775 (bad-sequence-type-error result-type)))))))
777 (defun map (result-type function first-sequence &rest more-sequences)
784 ;;; KLUDGE: MAP has been rewritten substantially since the fork from
785 ;;; CMU CL in order to give reasonable performance, but this
786 ;;; implementation of MAP-INTO still has the same problems as the old
787 ;;; MAP code. Ideally, MAP-INTO should be rewritten to be efficient in
788 ;;; the same way that the corresponding cases of MAP have been
789 ;;; rewritten. Instead of doing it now, though, it's easier to wait
790 ;;; until we have DYNAMIC-EXTENT, at which time it should become
791 ;;; extremely easy to define a reasonably efficient MAP-INTO in terms
792 ;;; of (MAP NIL ..). -- WHN 20000920
793 (defun map-into (result-sequence function &rest sequences)
795 (and (arrayp result-sequence)
796 (array-has-fill-pointer-p result-sequence)))
799 (array-dimension result-sequence 0)
800 (length result-sequence))
801 (mapcar #'length sequences))))
804 (setf (fill-pointer result-sequence) len))
806 (let ((really-fun (%coerce-callable-to-fun function)))
808 (setf (elt result-sequence index)
810 (mapcar (lambda (seq) (elt seq index))
816 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
817 ;;; arbitrary sequence arguments, both in the full call case and in
818 ;;; the open code case.
819 (macrolet ((defquantifier (name found-test found-result
820 &key doc (unfound-result (not found-result)))
822 ;; KLUDGE: It would be really nice if we could simply
823 ;; do something like this
824 ;; (declaim (inline ,name))
825 ;; (defun ,name (pred first-seq &rest more-seqs)
827 ;; (flet ((map-me (&rest rest)
828 ;; (let ((pred-value (apply pred rest)))
829 ;; (,found-test pred-value
830 ;; (return-from ,name
831 ;; ,found-result)))))
832 ;; (declare (inline map-me))
833 ;; (apply #'map nil #'map-me first-seq more-seqs)
835 ;; but Python doesn't seem to be smart enough about
836 ;; inlining and APPLY to recognize that it can use
837 ;; the DEFTRANSFORM for MAP in the resulting inline
838 ;; expansion. I don't have any appetite for deep
839 ;; compiler hacking right now, so I'll just work
840 ;; around the apparent problem by using a compiler
841 ;; macro instead. -- WHN 20000410
842 (defun ,name (pred first-seq &rest more-seqs)
844 (flet ((map-me (&rest rest)
845 (let ((pred-value (apply pred rest)))
846 (,found-test pred-value
849 (declare (inline map-me))
850 (apply #'map nil #'map-me first-seq more-seqs)
852 ;; KLUDGE: It would be more obviously correct -- but
853 ;; also significantly messier -- for PRED-VALUE to be
854 ;; a gensym. However, a private symbol really does
855 ;; seem to be good enough; and anyway the really
856 ;; obviously correct solution is to make Python smart
857 ;; enough that we can use an inline function instead
858 ;; of a compiler macro (as above). -- WHN 20000410
860 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
861 ;; important for performance, and it'd be good to have
862 ;; it be visible throughout the compilation of all the
863 ;; target SBCL code. That could be done by defining
864 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
865 ;; moving this DEFQUANTIFIER stuff (and perhaps other
866 ;; inline definitions in seq.lisp as well) into a new
867 ;; seq.lisp, and moving remaining target-only stuff
868 ;; from the old seq.lisp into target-seq.lisp.
869 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
870 (let ((elements (make-gensym-list (1+ (length more-seqs))))
871 (blockname (gensym "BLOCK")))
872 (once-only ((pred pred))
876 (let ((pred-value (funcall ,pred ,@elements)))
877 (,',found-test pred-value
878 (return-from ,blockname
882 ,',unfound-result)))))))
883 (defquantifier some when pred-value :unfound-result nil :doc
884 "Apply PREDICATE to the 0-indexed elements of the sequences, then
885 possibly to those with index 1, and so on. Return the first
886 non-NIL value encountered, or NIL if the end of any sequence is reached.")
887 (defquantifier every unless nil :doc
888 "Apply PREDICATE to the 0-indexed elements of the sequences, then
889 possibly to those with index 1, and so on. Return NIL as soon
890 as any invocation of PREDICATE returns NIL, or T if every invocation
892 (defquantifier notany when nil :doc
893 "Apply PREDICATE to the 0-indexed elements of the sequences, then
894 possibly to those with index 1, and so on. Return NIL as soon
895 as any invocation of PREDICATE returns a non-NIL value, or T if the end
896 of any sequence is reached.")
897 (defquantifier notevery unless t :doc
898 "Apply PREDICATE to 0-indexed elements of the sequences, then
899 possibly to those with index 1, and so on. Return T as soon
900 as any invocation of PREDICATE returns NIL, or NIL if every invocation
905 (eval-when (:compile-toplevel :execute)
907 (sb!xc:defmacro mumble-reduce (function
914 `(do ((index ,start (1+ index))
915 (value ,initial-value))
916 ((= index (the fixnum ,end)) value)
917 (declare (fixnum index))
918 (setq value (funcall ,function value
919 (apply-key ,key (,ref ,sequence index))))))
921 (sb!xc:defmacro mumble-reduce-from-end (function
928 `(do ((index (1- ,end) (1- index))
929 (value ,initial-value)
930 (terminus (1- ,start)))
931 ((= index terminus) value)
932 (declare (fixnum index terminus))
933 (setq value (funcall ,function
934 (apply-key ,key (,ref ,sequence index))
937 (sb!xc:defmacro list-reduce (function
944 `(let ((sequence (nthcdr ,start ,sequence)))
945 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
947 (sequence (if ,ivp sequence (cdr sequence))
949 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
950 (funcall ,function value (apply-key ,key (car sequence)))))
951 ((= count (the fixnum ,end)) value)
952 (declare (fixnum count)))))
954 (sb!xc:defmacro list-reduce-from-end (function
961 `(let ((sequence (nthcdr (- (the fixnum (length ,sequence))
963 (reverse ,sequence))))
964 (do ((count (if ,ivp ,start (1+ (the fixnum ,start)))
966 (sequence (if ,ivp sequence (cdr sequence))
968 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
969 (funcall ,function (apply-key ,key (car sequence)) value)))
970 ((= count (the fixnum ,end)) value)
971 (declare (fixnum count)))))
975 (defun reduce (function sequence &key key from-end (start 0)
976 end (initial-value nil ivp))
977 (declare (type index start))
979 (end (or end (length sequence))))
980 (declare (type index start end))
982 (if ivp initial-value (funcall function)))
985 (list-reduce-from-end function sequence key start end
987 (list-reduce function sequence key start end
991 (setq end (1- (the fixnum end)))
992 (setq initial-value (apply-key key (aref sequence end))))
993 (mumble-reduce-from-end function sequence key start end
997 (setq initial-value (apply-key key (aref sequence start)))
998 (setq start (1+ start)))
999 (mumble-reduce function sequence key start end
1000 initial-value aref)))))
1004 (eval-when (:compile-toplevel :execute)
1006 (sb!xc:defmacro mumble-delete (pred)
1007 `(do ((index start (1+ index))
1010 ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1011 (do ((index index (1+ index)) ; Copy the rest of the vector.
1012 (jndex jndex (1+ jndex)))
1013 ((= index (the fixnum length))
1014 (shrink-vector sequence jndex))
1015 (declare (fixnum index jndex))
1016 (setf (aref sequence jndex) (aref sequence index))))
1017 (declare (fixnum index jndex number-zapped))
1018 (setf (aref sequence jndex) (aref sequence index))
1020 (setq number-zapped (1+ number-zapped))
1021 (setq jndex (1+ jndex)))))
1023 (sb!xc:defmacro mumble-delete-from-end (pred)
1024 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1028 (terminus (1- start)))
1029 ((or (= index terminus) (= number-zapped (the fixnum count)))
1030 (do ((losers losers) ; Delete the losers.
1031 (index start (1+ index))
1033 ((or (null losers) (= index (the fixnum end)))
1034 (do ((index index (1+ index)) ; Copy the rest of the vector.
1035 (jndex jndex (1+ jndex)))
1036 ((= index (the fixnum length))
1037 (shrink-vector sequence jndex))
1038 (declare (fixnum index jndex))
1039 (setf (aref sequence jndex) (aref sequence index))))
1040 (declare (fixnum index jndex))
1041 (setf (aref sequence jndex) (aref sequence index))
1042 (if (= index (the fixnum (car losers)))
1044 (setq jndex (1+ jndex)))))
1045 (declare (fixnum index number-zapped terminus))
1046 (setq this-element (aref sequence index))
1048 (setq number-zapped (1+ number-zapped))
1049 (push index losers))))
1051 (sb!xc:defmacro normal-mumble-delete ()
1054 (not (funcall test-not item (apply-key key (aref sequence index))))
1055 (funcall test item (apply-key key (aref sequence index))))))
1057 (sb!xc:defmacro normal-mumble-delete-from-end ()
1058 `(mumble-delete-from-end
1060 (not (funcall test-not item (apply-key key this-element)))
1061 (funcall test item (apply-key key this-element)))))
1063 (sb!xc:defmacro list-delete (pred)
1064 `(let ((handle (cons nil sequence)))
1065 (do ((current (nthcdr start sequence) (cdr current))
1066 (previous (nthcdr start handle))
1067 (index start (1+ index))
1069 ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1071 (declare (fixnum index number-zapped))
1073 (rplacd previous (cdr current))
1074 (setq number-zapped (1+ number-zapped)))
1076 (setq previous (cdr previous)))))))
1078 (sb!xc:defmacro list-delete-from-end (pred)
1079 `(let* ((reverse (nreverse (the list sequence)))
1080 (handle (cons nil reverse)))
1081 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1083 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1084 (index start (1+ index))
1086 ((or (= index (the fixnum end)) (= number-zapped (the fixnum count)))
1087 (nreverse (cdr handle)))
1088 (declare (fixnum index number-zapped))
1090 (rplacd previous (cdr current))
1091 (setq number-zapped (1+ number-zapped)))
1093 (setq previous (cdr previous)))))))
1095 (sb!xc:defmacro normal-list-delete ()
1098 (not (funcall test-not item (apply-key key (car current))))
1099 (funcall test item (apply-key key (car current))))))
1101 (sb!xc:defmacro normal-list-delete-from-end ()
1102 '(list-delete-from-end
1104 (not (funcall test-not item (apply-key key (car current))))
1105 (funcall test item (apply-key key (car current))))))
1109 (defun delete (item sequence &key from-end (test #'eql) test-not (start 0)
1112 "Return a sequence formed by destructively removing the specified ITEM from
1113 the given SEQUENCE."
1114 (declare (fixnum start))
1115 (let* ((length (length sequence))
1116 (end (or end length))
1117 (count (adjust-count count)))
1118 (declare (type index length end)
1120 (seq-dispatch sequence
1122 (normal-list-delete-from-end)
1123 (normal-list-delete))
1125 (normal-mumble-delete-from-end)
1126 (normal-mumble-delete)))))
1128 (eval-when (:compile-toplevel :execute)
1130 (sb!xc:defmacro if-mumble-delete ()
1132 (funcall predicate (apply-key key (aref sequence index)))))
1134 (sb!xc:defmacro if-mumble-delete-from-end ()
1135 `(mumble-delete-from-end
1136 (funcall predicate (apply-key key this-element))))
1138 (sb!xc:defmacro if-list-delete ()
1140 (funcall predicate (apply-key key (car current)))))
1142 (sb!xc:defmacro if-list-delete-from-end ()
1143 '(list-delete-from-end
1144 (funcall predicate (apply-key key (car current)))))
1148 (defun delete-if (predicate sequence &key from-end (start 0) key end count)
1150 "Return a sequence formed by destructively removing the elements satisfying
1151 the specified PREDICATE from the given SEQUENCE."
1152 (declare (fixnum start))
1153 (let* ((length (length sequence))
1154 (end (or end length))
1155 (count (adjust-count count)))
1156 (declare (type index length end)
1158 (seq-dispatch sequence
1160 (if-list-delete-from-end)
1163 (if-mumble-delete-from-end)
1164 (if-mumble-delete)))))
1166 (eval-when (:compile-toplevel :execute)
1168 (sb!xc:defmacro if-not-mumble-delete ()
1170 (not (funcall predicate (apply-key key (aref sequence index))))))
1172 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1173 `(mumble-delete-from-end
1174 (not (funcall predicate (apply-key key this-element)))))
1176 (sb!xc:defmacro if-not-list-delete ()
1178 (not (funcall predicate (apply-key key (car current))))))
1180 (sb!xc:defmacro if-not-list-delete-from-end ()
1181 '(list-delete-from-end
1182 (not (funcall predicate (apply-key key (car current))))))
1186 (defun delete-if-not (predicate sequence &key from-end (start 0) end key count)
1188 "Return a sequence formed by destructively removing the elements not
1189 satisfying the specified PREDICATE from the given SEQUENCE."
1190 (declare (fixnum start))
1191 (let* ((length (length sequence))
1192 (end (or end length))
1193 (count (adjust-count count)))
1194 (declare (type index length end)
1196 (seq-dispatch sequence
1198 (if-not-list-delete-from-end)
1199 (if-not-list-delete))
1201 (if-not-mumble-delete-from-end)
1202 (if-not-mumble-delete)))))
1206 (eval-when (:compile-toplevel :execute)
1208 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1209 ;;; satisfies the predicate.
1210 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1211 `(do ((index ,begin (,bump index))
1213 (do ((index ,left (,bump index))
1214 (result (make-sequence-like sequence length)))
1215 ((= index (the fixnum ,begin)) result)
1216 (declare (fixnum index))
1217 (setf (aref result index) (aref sequence index))))
1221 ((or (= index (the fixnum ,finish))
1222 (= number-zapped (the fixnum count)))
1223 (do ((index index (,bump index))
1224 (new-index new-index (,bump new-index)))
1225 ((= index (the fixnum ,right)) (shrink-vector result new-index))
1226 (declare (fixnum index new-index))
1227 (setf (aref result new-index) (aref sequence index))))
1228 (declare (fixnum index new-index number-zapped))
1229 (setq this-element (aref sequence index))
1230 (cond (,pred (setq number-zapped (1+ number-zapped)))
1231 (t (setf (aref result new-index) this-element)
1232 (setq new-index (,bump new-index))))))
1234 (sb!xc:defmacro mumble-remove (pred)
1235 `(mumble-remove-macro 1+ 0 start end length ,pred))
1237 (sb!xc:defmacro mumble-remove-from-end (pred)
1238 `(let ((sequence (copy-seq sequence)))
1239 (mumble-delete-from-end ,pred)))
1241 (sb!xc:defmacro normal-mumble-remove ()
1244 (not (funcall test-not item (apply-key key this-element)))
1245 (funcall test item (apply-key key this-element)))))
1247 (sb!xc:defmacro normal-mumble-remove-from-end ()
1248 `(mumble-remove-from-end
1250 (not (funcall test-not item (apply-key key this-element)))
1251 (funcall test item (apply-key key this-element)))))
1253 (sb!xc:defmacro if-mumble-remove ()
1254 `(mumble-remove (funcall predicate (apply-key key this-element))))
1256 (sb!xc:defmacro if-mumble-remove-from-end ()
1257 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1259 (sb!xc:defmacro if-not-mumble-remove ()
1260 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1262 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1263 `(mumble-remove-from-end
1264 (not (funcall predicate (apply-key key this-element)))))
1266 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1268 (sb!xc:defmacro list-remove-macro (pred reverse?)
1269 `(let* ((sequence ,(if reverse?
1270 '(reverse (the list sequence))
1272 (%start ,(if reverse? '(- length end) 'start))
1273 (%end ,(if reverse? '(- length start) 'end))
1275 (results (do ((index 0 (1+ index))
1276 (before-start splice))
1277 ((= index (the fixnum %start)) before-start)
1278 (declare (fixnum index))
1280 (cdr (rplacd splice (list (pop sequence))))))))
1281 (do ((index %start (1+ index))
1284 ((or (= index (the fixnum %end)) (= number-zapped (the fixnum count)))
1285 (do ((index index (1+ index)))
1288 '(nreverse (the list (cdr results)))
1290 (declare (fixnum index))
1291 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1292 (declare (fixnum index number-zapped))
1293 (setq this-element (pop sequence))
1295 (setq number-zapped (1+ number-zapped))
1296 (setq splice (cdr (rplacd splice (list this-element))))))))
1298 (sb!xc:defmacro list-remove (pred)
1299 `(list-remove-macro ,pred nil))
1301 (sb!xc:defmacro list-remove-from-end (pred)
1302 `(list-remove-macro ,pred t))
1304 (sb!xc:defmacro normal-list-remove ()
1307 (not (funcall test-not item (apply-key key this-element)))
1308 (funcall test item (apply-key key this-element)))))
1310 (sb!xc:defmacro normal-list-remove-from-end ()
1311 `(list-remove-from-end
1313 (not (funcall test-not item (apply-key key this-element)))
1314 (funcall test item (apply-key key this-element)))))
1316 (sb!xc:defmacro if-list-remove ()
1318 (funcall predicate (apply-key key this-element))))
1320 (sb!xc:defmacro if-list-remove-from-end ()
1321 `(list-remove-from-end
1322 (funcall predicate (apply-key key this-element))))
1324 (sb!xc:defmacro if-not-list-remove ()
1326 (not (funcall predicate (apply-key key this-element)))))
1328 (sb!xc:defmacro if-not-list-remove-from-end ()
1329 `(list-remove-from-end
1330 (not (funcall predicate (apply-key key this-element)))))
1334 (defun remove (item sequence &key from-end (test #'eql) test-not (start 0)
1337 "Return a copy of SEQUENCE with elements satisfying the test (default is
1338 EQL) with ITEM removed."
1339 (declare (fixnum start))
1340 (let* ((length (length sequence))
1341 (end (or end length))
1342 (count (adjust-count count)))
1343 (declare (type index length end)
1345 (seq-dispatch sequence
1347 (normal-list-remove-from-end)
1348 (normal-list-remove))
1350 (normal-mumble-remove-from-end)
1351 (normal-mumble-remove)))))
1353 (defun remove-if (predicate sequence &key from-end (start 0) end count key)
1355 "Return a copy of sequence with elements such that predicate(element)
1356 is non-null removed"
1357 (declare (fixnum start))
1358 (let* ((length (length sequence))
1359 (end (or end length))
1360 (count (adjust-count count)))
1361 (declare (type index length end)
1363 (seq-dispatch sequence
1365 (if-list-remove-from-end)
1368 (if-mumble-remove-from-end)
1369 (if-mumble-remove)))))
1371 (defun remove-if-not (predicate sequence &key from-end (start 0) end count key)
1373 "Return a copy of sequence with elements such that predicate(element)
1375 (declare (fixnum start))
1376 (let* ((length (length sequence))
1377 (end (or end length))
1378 (count (adjust-count count)))
1379 (declare (type index length end)
1381 (seq-dispatch sequence
1383 (if-not-list-remove-from-end)
1384 (if-not-list-remove))
1386 (if-not-mumble-remove-from-end)
1387 (if-not-mumble-remove)))))
1389 ;;;; REMOVE-DUPLICATES
1391 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1392 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1393 ;;; if we look into the already copied structure (from after :start) and see
1394 ;;; the item. If we check from beginning we check into the rest of the
1395 ;;; original list up to the :end marker (this we have to do by running a
1396 ;;; do loop down the list that far and using our test.
1397 (defun list-remove-duplicates* (list test test-not start end key from-end)
1398 (declare (fixnum start))
1399 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1402 (do ((index 0 (1+ index)))
1404 (declare (fixnum index))
1405 (setq splice (cdr (rplacd splice (list (car current)))))
1406 (setq current (cdr current)))
1407 (do ((index 0 (1+ index)))
1408 ((or (and end (= index (the fixnum end)))
1410 (declare (fixnum index))
1411 (if (or (and from-end
1412 (not (member (apply-key key (car current))
1413 (nthcdr (1+ start) result)
1418 (not (do ((it (apply-key key (car current)))
1419 (l (cdr current) (cdr l))
1420 (i (1+ index) (1+ i)))
1421 ((or (atom l) (and end (= i (the fixnum end))))
1423 (declare (fixnum i))
1425 (not (funcall test-not it (apply-key key (car l))))
1426 (funcall test it (apply-key key (car l))))
1428 (setq splice (cdr (rplacd splice (list (car current))))))
1429 (setq current (cdr current)))
1432 (setq splice (cdr (rplacd splice (list (car current)))))
1433 (setq current (cdr current)))
1436 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1437 &optional (length (length vector)))
1438 (declare (vector vector) (fixnum start length))
1439 (when (null end) (setf end (length vector)))
1440 (let ((result (make-sequence-like vector length))
1443 (declare (fixnum index jndex))
1446 (setf (aref result index) (aref vector index))
1447 (setq index (1+ index)))
1450 (setq elt (aref vector index))
1451 (unless (or (and from-end
1452 (position (apply-key key elt) result :start start
1453 :end jndex :test test :test-not test-not :key key))
1455 (position (apply-key key elt) vector :start (1+ index)
1456 :end end :test test :test-not test-not :key key)))
1457 (setf (aref result jndex) elt)
1458 (setq jndex (1+ jndex)))
1459 (setq index (1+ index)))
1462 (setf (aref result jndex) (aref vector index))
1463 (setq index (1+ index))
1464 (setq jndex (1+ jndex)))
1465 (shrink-vector result jndex)))
1467 (defun remove-duplicates (sequence &key
1475 "The elements of Sequence are compared pairwise, and if any two match,
1476 the one occurring earlier is discarded, unless FROM-END is true, in
1477 which case the one later in the sequence is discarded. The resulting
1478 sequence is returned.
1480 The :TEST-NOT argument is depreciated."
1481 (declare (fixnum start))
1482 (seq-dispatch sequence
1484 (list-remove-duplicates* sequence test test-not
1485 start end key from-end))
1486 (vector-remove-duplicates* sequence test test-not
1487 start end key from-end)))
1489 ;;;; DELETE-DUPLICATES
1491 (defun list-delete-duplicates* (list test test-not key from-end start end)
1492 (declare (fixnum start))
1493 (let ((handle (cons nil list)))
1494 (do ((current (nthcdr start list) (cdr current))
1495 (previous (nthcdr start handle))
1496 (index start (1+ index)))
1497 ((or (and end (= index (the fixnum end))) (null current))
1499 (declare (fixnum index))
1500 (if (do ((x (if from-end
1501 (nthcdr (1+ start) handle)
1504 (i (1+ index) (1+ i)))
1506 (and (not from-end) end (= i (the fixnum end)))
1509 (declare (fixnum i))
1511 (not (funcall test-not
1512 (apply-key key (car current))
1513 (apply-key key (car x))))
1515 (apply-key key (car current))
1516 (apply-key key (car x))))
1518 (rplacd previous (cdr current))
1519 (setq previous (cdr previous))))))
1521 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1522 &optional (length (length vector)))
1523 (declare (vector vector) (fixnum start length))
1524 (when (null end) (setf end (length vector)))
1525 (do ((index start (1+ index))
1528 (do ((index index (1+ index)) ; copy the rest of the vector
1529 (jndex jndex (1+ jndex)))
1531 (shrink-vector vector jndex)
1533 (setf (aref vector jndex) (aref vector index))))
1534 (declare (fixnum index jndex))
1535 (setf (aref vector jndex) (aref vector index))
1536 (unless (position (apply-key key (aref vector index)) vector :key key
1537 :start (if from-end start (1+ index)) :test test
1538 :end (if from-end jndex end) :test-not test-not)
1539 (setq jndex (1+ jndex)))))
1541 (defun delete-duplicates (sequence &key
1549 "The elements of Sequence are examined, and if any two match, one is
1550 discarded. The resulting sequence, which may be formed by destroying the
1551 given sequence, is returned.
1553 The :TEST-NOT argument is depreciated."
1554 (seq-dispatch sequence
1556 (list-delete-duplicates* sequence test test-not key from-end start end))
1557 (vector-delete-duplicates* sequence test test-not key from-end start end)))
1561 (defun list-substitute* (pred new list start end count key test test-not old)
1562 (declare (fixnum start end count))
1563 (let* ((result (list nil))
1566 (list list)) ; Get a local list for a stepper.
1567 (do ((index 0 (1+ index)))
1569 (declare (fixnum index))
1570 (setq splice (cdr (rplacd splice (list (car list)))))
1571 (setq list (cdr list)))
1572 (do ((index start (1+ index)))
1573 ((or (= index end) (null list) (= count 0)))
1574 (declare (fixnum index))
1575 (setq elt (car list))
1584 (funcall test-not old (apply-key key elt)))
1585 (funcall test old (apply-key key elt))))
1586 (if (funcall test (apply-key key elt)))
1587 (if-not (not (funcall test (apply-key key elt)))))
1588 (setq count (1- count))
1591 (setq list (cdr list)))
1594 (setq splice (cdr (rplacd splice (list (car list)))))
1595 (setq list (cdr list)))
1598 ;;; Replace old with new in sequence moving from left to right by incrementer
1599 ;;; on each pass through the loop. Called by all three substitute functions.
1600 (defun vector-substitute* (pred new sequence incrementer left right length
1601 start end count key test test-not old)
1602 (declare (fixnum start count end incrementer right))
1603 (let ((result (make-sequence-like sequence length))
1605 (declare (fixnum index))
1608 (setf (aref result index) (aref sequence index))
1609 (setq index (+ index incrementer)))
1611 ((or (= index end) (= count 0)))
1612 (setq elt (aref sequence index))
1613 (setf (aref result index)
1617 (not (funcall test-not old (apply-key key elt)))
1618 (funcall test old (apply-key key elt))))
1619 (if (funcall test (apply-key key elt)))
1620 (if-not (not (funcall test (apply-key key elt)))))
1621 (setq count (1- count))
1624 (setq index (+ index incrementer)))
1627 (setf (aref result index) (aref sequence index))
1628 (setq index (+ index incrementer)))
1631 (eval-when (:compile-toplevel :execute)
1633 (sb!xc:defmacro subst-dispatch (pred)
1634 `(if (listp sequence)
1636 (nreverse (list-substitute* ,pred
1639 (- (the fixnum length)
1641 (- (the fixnum length)
1643 count key test test-not old))
1644 (list-substitute* ,pred
1645 new sequence start end count key test test-not
1648 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
1649 -1 length (1- (the fixnum end))
1650 (1- (the fixnum start))
1651 count key test test-not old)
1652 (vector-substitute* ,pred new sequence 1 0 length length
1653 start end count key test test-not old))))
1657 (defun substitute (new old sequence &key from-end (test #'eql) test-not
1658 (start 0) count end key)
1660 "Return a sequence of the same kind as SEQUENCE with the same elements,
1661 except that all elements equal to OLD are replaced with NEW. See manual
1663 (declare (fixnum start))
1664 (let* ((length (length sequence))
1665 (end (or end length))
1666 (count (adjust-count count)))
1667 (declare (type index length end)
1669 (subst-dispatch 'normal)))
1671 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
1673 (defun substitute-if (new test sequence &key from-end (start 0) end count key)
1675 "Return a sequence of the same kind as SEQUENCE with the same elements
1676 except that all elements satisfying the TEST are replaced with NEW. See
1677 manual for details."
1678 (declare (fixnum start))
1679 (let* ((length (length sequence))
1680 (end (or end length))
1681 (count (adjust-count count))
1684 (declare (type index length end)
1686 (subst-dispatch 'if)))
1688 (defun substitute-if-not (new test sequence &key from-end (start 0)
1691 "Return a sequence of the same kind as SEQUENCE with the same elements
1692 except that all elements not satisfying the TEST are replaced with NEW.
1693 See manual for details."
1694 (declare (fixnum start))
1695 (let* ((length (length sequence))
1696 (end (or end length))
1697 (count (adjust-count count))
1700 (declare (type index length end)
1702 (subst-dispatch 'if-not)))
1706 (defun nsubstitute (new old sequence &key from-end (test #'eql) test-not
1707 end count key (start 0))
1709 "Return a sequence of the same kind as SEQUENCE with the same elements
1710 except that all elements equal to OLD are replaced with NEW. The SEQUENCE
1711 may be destructively modified. See manual for details."
1712 (declare (fixnum start))
1713 (let ((end (or end (length sequence)))
1714 (count (adjust-count count)))
1715 (declare (fixnum count))
1716 (if (listp sequence)
1718 (nreverse (nlist-substitute*
1719 new old (nreverse (the list sequence))
1720 test test-not start end count key))
1721 (nlist-substitute* new old sequence
1722 test test-not start end count key))
1724 (nvector-substitute* new old sequence -1
1725 test test-not (1- end) (1- start) count key)
1726 (nvector-substitute* new old sequence 1
1727 test test-not start end count key)))))
1729 (defun nlist-substitute* (new old sequence test test-not start end count key)
1730 (declare (fixnum start count end))
1731 (do ((list (nthcdr start sequence) (cdr list))
1732 (index start (1+ index)))
1733 ((or (= index end) (null list) (= count 0)) sequence)
1734 (declare (fixnum index))
1736 (not (funcall test-not old (apply-key key (car list))))
1737 (funcall test old (apply-key key (car list))))
1739 (setq count (1- count)))))
1741 (defun nvector-substitute* (new old sequence incrementer
1742 test test-not start end count key)
1743 (declare (fixnum start incrementer count end))
1744 (do ((index start (+ index incrementer)))
1745 ((or (= index end) (= count 0)) sequence)
1746 (declare (fixnum index))
1748 (not (funcall test-not
1750 (apply-key key (aref sequence index))))
1751 (funcall test old (apply-key key (aref sequence index))))
1752 (setf (aref sequence index) new)
1753 (setq count (1- count)))))
1755 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
1757 (defun nsubstitute-if (new test sequence &key from-end (start 0) end count key)
1759 "Return a sequence of the same kind as SEQUENCE with the same elements
1760 except that all elements satisfying the TEST are replaced with NEW.
1761 SEQUENCE may be destructively modified. See manual for details."
1762 (declare (fixnum start))
1763 (let ((end (or end (length sequence)))
1764 (count (adjust-count count)))
1765 (declare (fixnum end count))
1766 (if (listp sequence)
1768 (nreverse (nlist-substitute-if*
1769 new test (nreverse (the list sequence))
1770 start end count key))
1771 (nlist-substitute-if* new test sequence
1772 start end count key))
1774 (nvector-substitute-if* new test sequence -1
1775 (1- end) (1- start) count key)
1776 (nvector-substitute-if* new test sequence 1
1777 start end count key)))))
1779 (defun nlist-substitute-if* (new test sequence start end count key)
1780 (declare (fixnum end))
1781 (do ((list (nthcdr start sequence) (cdr list))
1782 (index start (1+ index)))
1783 ((or (= index end) (null list) (= count 0)) sequence)
1784 (when (funcall test (apply-key key (car list)))
1786 (setq count (1- count)))))
1788 (defun nvector-substitute-if* (new test sequence incrementer
1789 start end count key)
1790 (do ((index start (+ index incrementer)))
1791 ((or (= index end) (= count 0)) sequence)
1792 (when (funcall test (apply-key key (aref sequence index)))
1793 (setf (aref sequence index) new)
1794 (setq count (1- count)))))
1796 (defun nsubstitute-if-not (new test sequence &key from-end (start 0)
1799 "Return a sequence of the same kind as SEQUENCE with the same elements
1800 except that all elements not satisfying the TEST are replaced with NEW.
1801 SEQUENCE may be destructively modified. See manual for details."
1802 (declare (fixnum start))
1803 (let ((end (or end (length sequence)))
1804 (count (adjust-count count)))
1805 (declare (fixnum end count))
1806 (if (listp sequence)
1808 (nreverse (nlist-substitute-if-not*
1809 new test (nreverse (the list sequence))
1810 start end count key))
1811 (nlist-substitute-if-not* new test sequence
1812 start end count key))
1814 (nvector-substitute-if-not* new test sequence -1
1815 (1- end) (1- start) count key)
1816 (nvector-substitute-if-not* new test sequence 1
1817 start end count key)))))
1819 (defun nlist-substitute-if-not* (new test sequence start end count key)
1820 (declare (fixnum end))
1821 (do ((list (nthcdr start sequence) (cdr list))
1822 (index start (1+ index)))
1823 ((or (= index end) (null list) (= count 0)) sequence)
1824 (when (not (funcall test (apply-key key (car list))))
1826 (setq count (1- count)))))
1828 (defun nvector-substitute-if-not* (new test sequence incrementer
1829 start end count key)
1830 (do ((index start (+ index incrementer)))
1831 ((or (= index end) (= count 0)) sequence)
1832 (when (not (funcall test (apply-key key (aref sequence index))))
1833 (setf (aref sequence index) new)
1834 (setq count (1- count)))))
1836 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
1838 ;;; logic to unravel :TEST, :TEST-NOT, and :KEY options in FIND,
1839 ;;; POSITION-IF, etc.
1840 (declaim (inline effective-find-position-test effective-find-position-key))
1841 (defun effective-find-position-test (test test-not)
1842 (cond ((and test test-not)
1843 (error "can't specify both :TEST and :TEST-NOT"))
1844 (test (%coerce-callable-to-fun test))
1846 ;; (Without DYNAMIC-EXTENT, this is potentially horribly
1847 ;; inefficient, but since the TEST-NOT option is deprecated
1848 ;; anyway, we don't care.)
1849 (complement (%coerce-callable-to-fun test-not)))
1851 (defun effective-find-position-key (key)
1853 (%coerce-callable-to-fun key)
1856 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
1857 (macrolet (;; shared logic for defining %FIND-POSITION and
1858 ;; %FIND-POSITION-IF in terms of various inlineable cases
1859 ;; of the expression defined in FROB and VECTOR*-FROB
1861 `(etypecase sequence-arg
1862 (list (frob sequence-arg from-end))
1864 (with-array-data ((sequence sequence-arg :offset-var offset)
1866 (end (or end (length sequence-arg))))
1867 (multiple-value-bind (f p)
1868 (macrolet ((frob2 () '(if from-end
1870 (frob sequence nil))))
1872 (simple-vector (frob2))
1873 (simple-string (frob2))
1874 (t (vector*-frob sequence))))
1875 (declare (type (or index null) p))
1876 (values f (and p (the index (+ p offset))))))))))
1877 (defun %find-position (item sequence-arg from-end start end key test)
1878 (macrolet ((frob (sequence from-end)
1879 `(%find-position item ,sequence
1880 ,from-end start end key test))
1881 (vector*-frob (sequence)
1882 `(%find-position-vector-macro item ,sequence
1883 from-end start end key test)))
1885 (defun %find-position-if (predicate sequence-arg from-end start end key)
1886 (macrolet ((frob (sequence from-end)
1887 `(%find-position-if predicate ,sequence
1888 ,from-end start end key))
1889 (vector*-frob (sequence)
1890 `(%find-position-if-vector-macro predicate ,sequence
1891 from-end start end key)))
1893 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
1894 (macrolet ((frob (sequence from-end)
1895 `(%find-position-if-not predicate ,sequence
1896 ,from-end start end key))
1897 (vector*-frob (sequence)
1898 `(%find-position-if-not-vector-macro predicate ,sequence
1899 from-end start end key)))
1902 ;;; the user interface to FIND and POSITION: Get all our ducks in a
1903 ;;; row, then call %FIND-POSITION.
1904 (declaim (inline find position))
1905 (macrolet ((def-find-position (fun-name values-index)
1906 `(defun ,fun-name (item
1917 (%find-position item
1922 (effective-find-position-key key)
1923 (effective-find-position-test test
1925 (def-find-position find 0)
1926 (def-find-position position 1))
1928 ;;; the user interface to FIND-IF and POSITION-IF, entirely analogous
1929 ;;; to the interface to FIND and POSITION
1930 (declaim (inline find-if position-if))
1931 (macrolet ((def-find-position-if (fun-name values-index)
1932 `(defun ,fun-name (predicate sequence
1933 &key from-end (start 0) end key)
1936 (%find-position-if (%coerce-callable-to-fun predicate)
1941 (effective-find-position-key key))))))
1943 (def-find-position-if find-if 0)
1944 (def-find-position-if position-if 1))
1946 ;;; the deprecated functions FIND-IF-NOT and POSITION-IF-NOT. We
1947 ;;; didn't bother to worry about optimizing them, except note that on
1948 ;;; Sat, Oct 06, 2001 at 04:22:38PM +0100, Christophe Rhodes wrote on
1951 ;;; My understanding is that while the :test-not argument is
1952 ;;; deprecated in favour of :test (complement #'foo) because of
1953 ;;; semantic difficulties (what happens if both :test and :test-not
1954 ;;; are supplied, etc) the -if-not variants, while officially
1955 ;;; deprecated, would be undeprecated were X3J13 actually to produce
1956 ;;; a revised standard, as there are perfectly legitimate idiomatic
1957 ;;; reasons for allowing the -if-not versions equal status,
1958 ;;; particularly remove-if-not (== filter).
1960 ;;; This is only an informal understanding, I grant you, but
1961 ;;; perhaps it's worth optimizing the -if-not versions in the same
1962 ;;; way as the others?
1964 ;;; That sounds reasonable, so if someone wants to submit patches to
1965 ;;; make the -IF-NOT functions compile as efficiently as the
1966 ;;; corresponding -IF variants do, go for it. -- WHN 2001-10-06)
1968 ;;; FIXME: Remove uses of these deprecated functions (and of :TEST-NOT
1969 ;;; too) within the implementation of SBCL.
1970 (declaim (inline find-if-not position-if-not))
1971 (macrolet ((def-find-position-if-not (fun-name values-index)
1972 `(defun ,fun-name (predicate sequence
1973 &key from-end (start 0) end key)
1976 (%find-position-if-not (%coerce-callable-to-fun predicate)
1981 (effective-find-position-key key))))))
1983 (def-find-position-if-not find-if-not 0)
1984 (def-find-position-if-not position-if-not 1))
1988 (eval-when (:compile-toplevel :execute)
1990 (sb!xc:defmacro vector-count (item sequence)
1991 `(do ((index start (1+ index))
1993 ((= index (the fixnum end)) count)
1994 (declare (fixnum index count))
1996 (unless (funcall test-not ,item
1997 (apply-key key (aref ,sequence index)))
1998 (setq count (1+ count)))
1999 (when (funcall test ,item (apply-key key (aref ,sequence index)))
2000 (setq count (1+ count))))))
2002 (sb!xc:defmacro list-count (item sequence)
2003 `(do ((sequence (nthcdr start ,sequence))
2004 (index start (1+ index))
2006 ((or (= index (the fixnum end)) (null sequence)) count)
2007 (declare (fixnum index count))
2009 (unless (funcall test-not ,item (apply-key key (pop sequence)))
2010 (setq count (1+ count)))
2011 (when (funcall test ,item (apply-key key (pop sequence)))
2012 (setq count (1+ count))))))
2016 (defun count (item sequence &key from-end (test #'eql) test-not (start 0)
2019 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2020 which defaults to EQL."
2021 (declare (ignore from-end) (fixnum start))
2022 (let ((end (or end (length sequence))))
2023 (declare (type index end))
2024 (seq-dispatch sequence
2025 (list-count item sequence)
2026 (vector-count item sequence))))
2028 ;;;; COUNT-IF and COUNT-IF-NOT
2030 (eval-when (:compile-toplevel :execute)
2032 (sb!xc:defmacro vector-count-if (predicate sequence)
2033 `(do ((index start (1+ index))
2035 ((= index (the fixnum end)) count)
2036 (declare (fixnum index count))
2037 (if (funcall ,predicate (apply-key key (aref ,sequence index)))
2038 (setq count (1+ count)))))
2040 (sb!xc:defmacro list-count-if (predicate sequence)
2041 `(do ((sequence (nthcdr start ,sequence))
2042 (index start (1+ index))
2044 ((or (= index (the fixnum end)) (null sequence)) count)
2045 (declare (fixnum index count))
2046 (if (funcall ,predicate (apply-key key (pop sequence)))
2047 (setq count (1+ count)))))
2051 (defun count-if (test sequence &key from-end (start 0) end key)
2053 "Return the number of elements in SEQUENCE satisfying TEST(el)."
2054 (declare (ignore from-end) (fixnum start))
2055 (let ((end (or end (length sequence))))
2056 (declare (type index end))
2057 (seq-dispatch sequence
2058 (list-count-if test sequence)
2059 (vector-count-if test sequence))))
2061 (eval-when (:compile-toplevel :execute)
2063 (sb!xc:defmacro vector-count-if-not (predicate sequence)
2064 `(do ((index start (1+ index))
2066 ((= index (the fixnum end)) count)
2067 (declare (fixnum index count))
2068 (if (not (funcall ,predicate (apply-key key (aref ,sequence index))))
2069 (setq count (1+ count)))))
2071 (sb!xc:defmacro list-count-if-not (predicate sequence)
2072 `(do ((sequence (nthcdr start ,sequence))
2073 (index start (1+ index))
2075 ((or (= index (the fixnum end)) (null sequence)) count)
2076 (declare (fixnum index count))
2077 (if (not (funcall ,predicate (apply-key key (pop sequence))))
2078 (setq count (1+ count)))))
2082 (defun count-if-not (test sequence &key from-end (start 0) end key)
2084 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2085 (declare (ignore from-end) (fixnum start))
2086 (let ((end (or end (length sequence))))
2087 (declare (type index end))
2088 (seq-dispatch sequence
2089 (list-count-if-not test sequence)
2090 (vector-count-if-not test sequence))))
2094 (eval-when (:compile-toplevel :execute)
2096 (sb!xc:defmacro match-vars (&rest body)
2097 `(let ((inc (if from-end -1 1))
2098 (start1 (if from-end (1- (the fixnum end1)) start1))
2099 (start2 (if from-end (1- (the fixnum end2)) start2))
2100 (end1 (if from-end (1- (the fixnum start1)) end1))
2101 (end2 (if from-end (1- (the fixnum start2)) end2)))
2102 (declare (fixnum inc start1 start2 end1 end2))
2105 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2106 (declare (ignore end)) ;; ### Should END be used below?
2107 `(let ((,sequence (if from-end
2108 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2109 (reverse (the list ,sequence)))
2110 (nthcdr ,start ,sequence))))
2111 (declare (type list ,sequence))
2116 (eval-when (:compile-toplevel :execute)
2118 (sb!xc:defmacro if-mismatch (elt1 elt2)
2119 `(cond ((= (the fixnum index1) (the fixnum end1))
2120 (return (if (= (the fixnum index2) (the fixnum end2))
2123 (1+ (the fixnum index1))
2124 (the fixnum index1)))))
2125 ((= (the fixnum index2) (the fixnum end2))
2126 (return (if from-end (1+ (the fixnum index1)) index1)))
2128 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2129 (return (if from-end (1+ (the fixnum index1)) index1))))
2130 (t (if (not (funcall test (apply-key key ,elt1)
2131 (apply-key key ,elt2)))
2132 (return (if from-end (1+ (the fixnum index1)) index1))))))
2134 (sb!xc:defmacro mumble-mumble-mismatch ()
2135 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2136 (index2 start2 (+ index2 (the fixnum inc))))
2138 (declare (fixnum index1 index2))
2139 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2141 (sb!xc:defmacro mumble-list-mismatch ()
2142 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2143 (index2 start2 (+ index2 (the fixnum inc))))
2145 (declare (fixnum index1 index2))
2146 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2148 (sb!xc:defmacro list-mumble-mismatch ()
2149 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2150 (index2 start2 (+ index2 (the fixnum inc))))
2152 (declare (fixnum index1 index2))
2153 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2155 (sb!xc:defmacro list-list-mismatch ()
2156 `(do ((sequence1 sequence1)
2157 (sequence2 sequence2)
2158 (index1 start1 (+ index1 (the fixnum inc)))
2159 (index2 start2 (+ index2 (the fixnum inc))))
2161 (declare (fixnum index1 index2))
2162 (if-mismatch (pop sequence1) (pop sequence2))))
2166 (defun mismatch (sequence1 sequence2 &key from-end (test #'eql) test-not
2167 (start1 0) end1 (start2 0) end2 key)
2169 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2170 element-wise. If they are of equal length and match in every element, the
2171 result is Nil. Otherwise, the result is a non-negative integer, the index
2172 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2173 if one is shorter than and a matching prefix of the other, the index within
2174 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2175 :FROM-END argument is given, then one plus the index of the rightmost
2176 position in which the sequences differ is returned."
2177 (declare (fixnum start1 start2))
2178 (let* ((length1 (length sequence1))
2179 (end1 (or end1 length1))
2180 (length2 (length sequence2))
2181 (end2 (or end2 length2)))
2182 (declare (type index length1 end1 length2 end2))
2184 (seq-dispatch sequence1
2185 (matchify-list (sequence1 start1 length1 end1)
2186 (seq-dispatch sequence2
2187 (matchify-list (sequence2 start2 length2 end2)
2188 (list-list-mismatch))
2189 (list-mumble-mismatch)))
2190 (seq-dispatch sequence2
2191 (matchify-list (sequence2 start2 length2 end2)
2192 (mumble-list-mismatch))
2193 (mumble-mumble-mismatch))))))
2195 ;;; search comparison functions
2197 (eval-when (:compile-toplevel :execute)
2199 ;;; Compare two elements and return if they don't match.
2200 (sb!xc:defmacro compare-elements (elt1 elt2)
2202 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2205 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2209 (sb!xc:defmacro search-compare-list-list (main sub)
2210 `(do ((main ,main (cdr main))
2211 (jndex start1 (1+ jndex))
2212 (sub (nthcdr start1 ,sub) (cdr sub)))
2213 ((or (null main) (null sub) (= (the fixnum end1) jndex))
2215 (declare (fixnum jndex))
2216 (compare-elements (car main) (car sub))))
2218 (sb!xc:defmacro search-compare-list-vector (main sub)
2219 `(do ((main ,main (cdr main))
2220 (index start1 (1+ index)))
2221 ((or (null main) (= index (the fixnum end1))) t)
2222 (declare (fixnum index))
2223 (compare-elements (car main) (aref ,sub index))))
2225 (sb!xc:defmacro search-compare-vector-list (main sub index)
2226 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2227 (jndex start1 (1+ jndex))
2228 (index ,index (1+ index)))
2229 ((or (= (the fixnum end1) jndex) (null sub)) t)
2230 (declare (fixnum jndex index))
2231 (compare-elements (aref ,main index) (car sub))))
2233 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2234 `(do ((index ,index (1+ index))
2235 (sub-index start1 (1+ sub-index)))
2236 ((= sub-index (the fixnum end1)) t)
2237 (declare (fixnum sub-index index))
2238 (compare-elements (aref ,main index) (aref ,sub sub-index))))
2240 (sb!xc:defmacro search-compare (main-type main sub index)
2241 (if (eq main-type 'list)
2243 (search-compare-list-list ,main ,sub)
2244 (search-compare-list-vector ,main ,sub))
2246 (search-compare-vector-list ,main ,sub ,index)
2247 (search-compare-vector-vector ,main ,sub ,index))))
2253 (eval-when (:compile-toplevel :execute)
2255 (sb!xc:defmacro list-search (main sub)
2256 `(do ((main (nthcdr start2 ,main) (cdr main))
2257 (index2 start2 (1+ index2))
2258 (terminus (- (the fixnum end2)
2259 (the fixnum (- (the fixnum end1)
2260 (the fixnum start1)))))
2262 ((> index2 terminus) last-match)
2263 (declare (fixnum index2 terminus))
2264 (if (search-compare list main ,sub index2)
2266 (setq last-match index2)
2269 (sb!xc:defmacro vector-search (main sub)
2270 `(do ((index2 start2 (1+ index2))
2271 (terminus (- (the fixnum end2)
2272 (the fixnum (- (the fixnum end1)
2273 (the fixnum start1)))))
2275 ((> index2 terminus) last-match)
2276 (declare (fixnum index2 terminus))
2277 (if (search-compare vector ,main ,sub index2)
2279 (setq last-match index2)
2284 (defun search (sequence1 sequence2 &key from-end (test #'eql) test-not
2285 (start1 0) end1 (start2 0) end2 key)
2286 (declare (fixnum start1 start2))
2287 (let ((end1 (or end1 (length sequence1)))
2288 (end2 (or end2 (length sequence2))))
2289 (seq-dispatch sequence2
2290 (list-search sequence2 sequence1)
2291 (vector-search sequence2 sequence1))))