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 (defun %check-generic-sequence-bounds
(seq start end
)
24 (let ((length (sb!sequence
:length seq
)))
25 (if (<= 0 start
(or end length
) length
)
27 (sequence-bounding-indices-bad-error seq start end
))))
29 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
31 (defparameter *sequence-keyword-info
*
32 ;; (name default supplied-p adjustment new-type)
36 (null (1- most-positive-fixnum
))
37 (fixnum (max 0 count
))
38 (integer (if (minusp count
)
40 (1- most-positive-fixnum
))))
41 (mod #.sb
!xc
:most-positive-fixnum
))
42 ;; Entries for {start,end}{,1,2}
43 ,@(mapcan (lambda (names)
44 (destructuring-bind (start end length sequence
) names
49 ;; Only evaluate LENGTH (which may be expensive)
50 ;; if START is non-NIL.
51 (if (or (zerop ,start
) (<= 0 ,start
,length
))
53 (sequence-bounding-indices-bad-error ,sequence
,start
,end
))
58 ;; Only evaluate LENGTH (which may be expensive)
60 (if (or (null ,end
) (<= ,start
,end
,length
))
61 ;; Defaulting of NIL is done inside the
62 ;; bodies, for ease of sharing with compiler
65 ;; FIXME: defend against non-number non-NIL
68 (sequence-bounding-indices-bad-error ,sequence
,start
,end
))
70 '((start end length sequence
)
71 (start1 end1 length1 sequence1
)
72 (start2 end2 length2 sequence2
)))
75 (and key
(%coerce-callable-to-fun key
))
79 (%coerce-callable-to-fun test
)
83 (and test-not
(%coerce-callable-to-fun test-not
))
86 (sb!xc
:defmacro define-sequence-traverser
(name args
&body body
)
87 (multiple-value-bind (body declarations docstring
)
88 (parse-body body
:doc-string-allowed t
)
91 ;; Things which are definitely used in any code path.
93 ;; Things which may be used/are only used in certain
94 ;; code paths (e.g. length).
98 ;; FIXME: make this robust. And clean.
99 ((sequence sequence1 sequence2
)
100 (let* ((length-var (ecase arg
103 (sequence2 'length2
)))
104 (cache-var (symbolicate length-var
'#:-cache
)))
106 (rebindings/eager
`(,cache-var nil
))
108 `(,length-var
(truly-the
110 (or ,cache-var
(setf ,cache-var
(length ,arg
))))))))
111 ((function predicate
)
113 (rebindings/eager
`(,arg
(%coerce-callable-to-fun
,arg
))))
115 (let ((info (cdr (assoc arg
*sequence-keyword-info
*))))
117 (destructuring-bind (default supplied-p adjuster type
) info
118 (new-args `(,arg
,default
,@(when supplied-p
(list supplied-p
))))
119 (rebindings/eager
`(,arg
,adjuster
))
120 (new-declarations `(type ,type
,arg
))))
121 (t (new-args arg
)))))))
122 `(defun ,name
,(new-args)
123 ,@(when docstring
(list docstring
))
125 (symbol-macrolet (,@(rebindings/lazy
))
126 (let* (,@(rebindings/eager
))
127 (declare ,@(new-declarations))
131 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
133 ;;; FIXME: It might be worth making three cases here, LIST,
134 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
135 ;;; It tends to make code run faster but be bigger; some benchmarking
136 ;;; is needed to decide.
137 (sb!xc
:defmacro seq-dispatch
138 (sequence list-form array-form
&optional other-form
)
139 `(if (listp ,sequence
)
140 (let ((,sequence
(truly-the list
,sequence
)))
141 (declare (ignorable ,sequence
))
144 `((if (arrayp ,sequence
)
145 (let ((,sequence
(truly-the vector
,sequence
)))
146 (declare (ignorable ,sequence
))
149 `((let ((,sequence
(truly-the vector
,sequence
)))
150 (declare (ignorable ,sequence
))
153 ;; Same as above, but don't assume that ARRAYP implies VECTORP,
154 ;; and call SEQUENCEP only after checking for not LISTP and not VECTORP.
155 ;; This is for dispatching within sequence functions that have the
156 ;; EXPLICIT-CHECK attribute. [Because there is no way to have the compiler
157 ;; insert checks for a subset of arguments, it is inconvenient to declare
158 ;; things like DELETE-IF with explicit-check to suppress checking of the
159 ;; 'sequence' because then you have to manually check all other arguments.]
160 (sb!xc
:defmacro seq-dispatch-checking
161 (sequence list-form array-form
&optional other-form
)
162 `(if (listp ,sequence
)
163 (let ((,sequence
(truly-the list
,sequence
)))
164 (declare (ignorable ,sequence
))
167 `((if (vectorp ,sequence
)
168 (let ((,sequence
(truly-the vector
,sequence
)))
169 (declare (ignorable ,sequence
))
171 ;; This could assert (THE EXTENDED-SEQUENCE ,sequence)
172 ;; for a slight win, however the error string would be wrong.
173 ;; It needs to be "<x> is not a SEQUENCE", not "<x> is not
174 ;; an EXTENDED-SEQUENCE".
175 (let ((,sequence
(the sequence
,sequence
)))
177 `((let ((,sequence
(the vector
,sequence
)))
178 (declare (ignorable ,sequence
))
181 (sb!xc
:defmacro %make-sequence-like
(sequence length
)
183 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
184 `(seq-dispatch ,sequence
186 (make-array ,length
:element-type
(array-element-type ,sequence
))
187 (sb!sequence
:make-sequence-like
,sequence
,length
)))
189 (sb!xc
:defmacro bad-sequence-type-error
(type-spec)
190 `(error 'simple-type-error
192 :expected-type
'(satisfies is-a-valid-sequence-type-specifier-p
)
193 :format-control
"~S is a bad type specifier for sequences."
194 :format-arguments
(list ,type-spec
)))
196 (sb!xc
:defmacro sequence-type-length-mismatch-error
(type length
)
197 `(error 'simple-type-error
199 :expected-type
(cond ((array-type-p ,type
)
200 `(eql ,(car (array-type-dimensions ,type
))))
201 ((type= ,type
(specifier-type 'null
))
205 (t (bug "weird type in S-T-L-M-ERROR")))
206 ;; FIXME: this format control causes ugly printing. There's
207 ;; probably some ~<~@:_~> incantation that would make it
208 ;; nicer. -- CSR, 2002-10-18
209 :format-control
"The length requested (~S) does not match the type restriction in ~S."
210 :format-arguments
(list ,length
(type-specifier ,type
))))
212 (sb!xc
:defmacro sequence-type-too-hairy
(type-spec)
213 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
214 ;; words that give some but not total support to this position in
215 ;; ANSI. Essentially, we are justified in throwing this on
216 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
217 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
219 ;; On the other hand, I'm not sure it deserves to be a type-error,
220 ;; either. -- bem, 2005-08-10
221 `(error 'simple-program-error
222 :format-control
"~S is too hairy for sequence functions."
223 :format-arguments
(list ,type-spec
)))
226 (defun is-a-valid-sequence-type-specifier-p (type)
227 (let ((type (specifier-type type
)))
228 (or (csubtypep type
(specifier-type 'list
))
229 (csubtypep type
(specifier-type 'vector
)))))
231 ;;; It's possible with some sequence operations to declare the length
232 ;;; of a result vector, and to be safe, we really ought to verify that
233 ;;; the actual result has the declared length.
234 (defun vector-of-checked-length-given-length (vector declared-length
)
235 (declare (type vector vector
))
236 (declare (type index declared-length
))
237 (let ((actual-length (length vector
)))
238 (unless (= actual-length declared-length
)
239 (error 'simple-type-error
241 :expected-type
`(vector ,declared-length
)
243 "Vector length (~W) doesn't match declared length (~W)."
244 :format-arguments
(list actual-length declared-length
))))
247 (defun sequence-of-checked-length-given-type (sequence result-type
)
248 (let ((ctype (specifier-type result-type
)))
249 (if (not (array-type-p ctype
))
251 (let ((declared-length (first (array-type-dimensions ctype
))))
252 (if (eq declared-length
'*)
254 (vector-of-checked-length-given-length sequence
255 declared-length
))))))
257 (declaim (ftype (function (sequence index
) nil
) signal-index-too-large-error
))
258 (defun signal-index-too-large-error (sequence index
)
259 (let* ((length (length sequence
))
260 (max-index (and (plusp length
)
262 (error 'index-too-large-error
264 :expected-type
(if max-index
265 `(integer 0 ,max-index
)
266 ;; This seems silly, is there something better?
269 (declaim (ftype (function (t t t
) nil
) sequence-bounding-indices-bad-error
))
270 (defun sequence-bounding-indices-bad-error (sequence start end
)
271 (let ((size (length sequence
)))
272 (error 'bounding-indices-bad-error
273 :datum
(cons start end
)
274 :expected-type
`(cons (integer 0 ,size
)
275 (integer ,start
,size
))
278 (declaim (ftype (function (t t t
) nil
) array-bounding-indices-bad-error
))
279 (defun array-bounding-indices-bad-error (array start end
)
280 (let ((size (array-total-size array
)))
281 (error 'bounding-indices-bad-error
282 :datum
(cons start end
)
283 :expected-type
`(cons (integer 0 ,size
)
284 (integer ,start
,size
))
287 (declaim (ftype (function (t) nil
) circular-list-error
))
288 (defun circular-list-error (list)
289 (let ((*print-circle
* t
))
290 (error 'simple-type-error
291 :format-control
"List is circular:~% ~S"
292 :format-arguments
(list list
)
294 :type
'(and list
(satisfies list-length
)))))
298 (defun emptyp (sequence)
300 "Returns T if SEQUENCE is an empty sequence and NIL
301 otherwise. Signals an error if SEQUENCE is not a sequence."
302 (seq-dispatch sequence
304 (zerop (length sequence
))
305 (sb!sequence
:emptyp sequence
)))
307 (defun elt (sequence index
)
308 #!+sb-doc
"Return the element of SEQUENCE specified by INDEX."
309 (seq-dispatch sequence
310 (do ((count index
(1- count
))
311 (list sequence
(cdr list
)))
314 (signal-index-too-large-error sequence index
)
316 (declare (type (integer 0) count
)))
318 (when (>= index
(length sequence
))
319 (signal-index-too-large-error sequence index
))
320 (aref sequence index
))
321 (sb!sequence
:elt sequence index
)))
323 (defun %setelt
(sequence index newval
)
324 #!+sb-doc
"Store NEWVAL as the component of SEQUENCE specified by INDEX."
325 (seq-dispatch sequence
326 (do ((count index
(1- count
))
328 ((= count
0) (rplaca seq newval
) newval
)
329 (declare (fixnum count
))
331 (signal-index-too-large-error sequence index
)
332 (setq seq
(cdr seq
))))
334 (when (>= index
(length sequence
))
335 (signal-index-too-large-error sequence index
))
336 (setf (aref sequence index
) newval
))
337 (setf (sb!sequence
:elt sequence index
) newval
)))
339 (defun length (sequence)
340 #!+sb-doc
"Return an integer that is the length of SEQUENCE."
341 (seq-dispatch-checking sequence
344 (sb!sequence
:length sequence
)))
346 (defun make-sequence (type length
&key
(initial-element nil iep
))
348 "Return a sequence of the given TYPE and LENGTH, with elements initialized
350 (declare (fixnum length
))
351 (let* ((expanded-type (typexpand type
))
353 (typecase expanded-type
355 ((eq expanded-type
'string
) '(vector character
))
356 ((eq expanded-type
'simple-string
)
357 '(simple-array character
(*)))
360 ((eq (car expanded-type
) 'string
)
361 `(vector character
,@(cdr expanded-type
)))
362 ((eq (car expanded-type
) 'simple-string
)
363 `(simple-array character
,(if (cdr expanded-type
)
366 (t expanded-type
)))))
367 (type (specifier-type adjusted-type
)))
368 (cond ((csubtypep type
(specifier-type 'list
))
370 ((type= type
(specifier-type 'list
))
371 (make-list length
:initial-element initial-element
))
372 ((eq type
*empty-type
*)
373 (bad-sequence-type-error nil
))
374 ((type= type
(specifier-type 'null
))
377 (sequence-type-length-mismatch-error type length
)))
379 (multiple-value-bind (min exactp
)
380 (sb!kernel
::cons-type-length-info type
)
382 (unless (= length min
)
383 (sequence-type-length-mismatch-error type length
))
384 (unless (>= length min
)
385 (sequence-type-length-mismatch-error type length
)))
386 (make-list length
:initial-element initial-element
)))
387 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
388 ;; which may seem strange and non-ideal, but then I'd say
389 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
390 (t (sequence-type-too-hairy (type-specifier type
)))))
391 ((csubtypep type
(specifier-type 'vector
))
393 (;; is it immediately obvious what the result type is?
394 (typep type
'array-type
)
396 (aver (= (length (array-type-dimensions type
)) 1))
397 (let* ((etype (type-specifier
398 (array-type-specialized-element-type type
)))
399 (etype (if (eq etype
'*) t etype
))
400 (type-length (car (array-type-dimensions type
))))
401 (unless (or (eq type-length
'*)
402 (= type-length length
))
403 (sequence-type-length-mismatch-error type length
))
404 ;; FIXME: These calls to MAKE-ARRAY can't be
405 ;; open-coded, as the :ELEMENT-TYPE argument isn't
406 ;; constant. Probably we ought to write a
407 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
410 (make-array length
:element-type etype
411 :initial-element initial-element
)
412 (make-array length
:element-type etype
)))))
413 (t (sequence-type-too-hairy (type-specifier type
)))))
414 ((and (csubtypep type
(specifier-type 'sequence
))
415 (awhen (find-class adjusted-type nil
)
416 (let ((prototype (sb!mop
:class-prototype
417 (sb!pcl
:ensure-class-finalized it
))))
419 (sb!sequence
:make-sequence-like
420 prototype length
:initial-element initial-element
)
421 (sb!sequence
:make-sequence-like
422 prototype length
))))))
423 (t (bad-sequence-type-error (type-specifier type
))))))
428 (define-array-dispatch vector-subseq-dispatch
(array start end
)
429 (declare (optimize speed
(safety 0)))
430 (declare (type index start end
))
431 (subseq array start end
))
433 ;;;; The support routines for SUBSEQ are used by compiler transforms,
434 ;;;; so we worry about dealing with END being supplied or defaulting
435 ;;;; to NIL at this level.
437 (defun vector-subseq* (sequence start end
)
438 (declare (type vector sequence
))
439 (declare (type index start
)
440 (type (or null index
) end
)
442 (with-array-data ((data sequence
)
445 :check-fill-pointer t
447 (vector-subseq-dispatch data start end
)))
449 (defun list-subseq* (sequence start end
)
450 (declare (type list sequence
)
451 (type unsigned-byte start
)
452 (type (or null unsigned-byte
) end
))
454 (sequence-bounding-indices-bad-error sequence start end
)))
455 (let ((pointer sequence
))
456 (unless (zerop start
)
457 ;; If START > 0 the list cannot be empty. So CDR down to
458 ;; it START-1 times, check that we still have something, then
459 ;; CDR the final time.
461 ;; If START was zero, the list may be empty if END is NIL or
464 (setf pointer
(nthcdr (1- start
) pointer
)))
469 (let ((n (- end start
)))
470 (declare (integer n
))
474 (let* ((head (list nil
))
476 (macrolet ((pop-one ()
477 `(let ((tmp (list (pop pointer
))))
481 (loop until
(fixnump n
)
484 ;; Fixnum case, but leave last element, so we should
485 ;; still have something left in the sequence.
492 ;; OK, pop the last one.
496 collect
(pop pointer
))))))
498 (defun subseq (sequence start
&optional end
)
500 "Return a copy of a subsequence of SEQUENCE starting with element number
501 START and continuing to the end of SEQUENCE or the optional END."
502 (seq-dispatch sequence
503 (list-subseq* sequence start end
)
504 (vector-subseq* sequence start end
)
505 (sb!sequence
:subseq sequence start end
)))
509 (defun copy-seq (sequence)
510 #!+sb-doc
"Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
511 (seq-dispatch sequence
512 (list-copy-seq* sequence
)
513 (vector-subseq* sequence
0 nil
)
514 (sb!sequence
:copy-seq sequence
)))
516 (defun list-copy-seq* (sequence)
517 (!copy-list-macro sequence
:check-proper-list t
))
521 (defun list-fill* (sequence item start end
)
522 (declare (type list sequence
)
523 (type unsigned-byte start
)
524 (type (or null unsigned-byte
) end
))
526 (sequence-bounding-indices-bad-error sequence start end
)))
527 (let ((pointer sequence
))
528 (unless (zerop start
)
529 ;; If START > 0 the list cannot be empty. So CDR down to it
530 ;; START-1 times, check that we still have something, then CDR
533 ;; If START was zero, the list may be empty if END is NIL or
536 (setf pointer
(nthcdr (1- start
) pointer
)))
541 (let ((n (- end start
)))
542 (declare (integer n
))
547 do
(setf pointer
(cdr (rplaca pointer item
))))))
549 do
(setf pointer
(cdr (rplaca pointer item
)))))))
552 (defun vector-fill* (sequence item start end
)
553 (with-array-data ((data sequence
)
557 :check-fill-pointer t
)
558 (let ((setter (!find-data-vector-setter data
)))
559 (declare (optimize (speed 3) (safety 0)))
560 (do ((index start
(1+ index
)))
561 ((= index end
) sequence
)
562 (declare (index index
))
563 (funcall setter data index item
)))))
565 (defun string-fill* (sequence item start end
)
566 (declare (string sequence
))
567 (with-array-data ((data sequence
)
571 :check-fill-pointer t
)
572 ;; DEFTRANSFORM for FILL will turn these into
573 ;; calls to UB*-BASH-FILL.
576 ((simple-array character
(*))
577 (let ((item (locally (declare (optimize (safety 3)))
578 (the character item
))))
579 (fill data item
:start start
:end end
)))
580 ((simple-array base-char
(*))
581 (let ((item (locally (declare (optimize (safety 3)))
582 (the base-char item
))))
583 (fill data item
:start start
:end end
))))))
585 (defun fill (sequence item
&key
(start 0) end
)
587 "Replace the specified elements of SEQUENCE with ITEM."
588 (seq-dispatch sequence
589 (list-fill* sequence item start end
)
590 (vector-fill* sequence item start end
)
591 (sb!sequence
:fill sequence item
593 :end
(%check-generic-sequence-bounds sequence start end
))))
597 (eval-when (:compile-toplevel
:execute
)
599 ;;; If we are copying around in the same vector, be careful not to copy the
600 ;;; same elements over repeatedly. We do this by copying backwards.
601 (sb!xc
:defmacro mumble-replace-from-mumble
()
602 `(if (and (eq target-sequence source-sequence
) (> target-start source-start
))
603 (let ((nelts (min (- target-end target-start
)
604 (- source-end source-start
))))
605 (do ((target-index (+ (the fixnum target-start
) (the fixnum nelts
) -
1)
607 (source-index (+ (the fixnum source-start
) (the fixnum nelts
) -
1)
609 ((= target-index
(the fixnum
(1- target-start
))) target-sequence
)
610 (declare (fixnum target-index source-index
))
611 ;; disable bounds checking
612 (declare (optimize (safety 0)))
613 (setf (aref target-sequence target-index
)
614 (aref source-sequence source-index
))))
615 (do ((target-index target-start
(1+ target-index
))
616 (source-index source-start
(1+ source-index
)))
617 ((or (= target-index
(the fixnum target-end
))
618 (= source-index
(the fixnum source-end
)))
620 (declare (fixnum target-index source-index
))
621 ;; disable bounds checking
622 (declare (optimize (safety 0)))
623 (setf (aref target-sequence target-index
)
624 (aref source-sequence source-index
)))))
626 (sb!xc
:defmacro list-replace-from-list
()
627 `(if (and (eq target-sequence source-sequence
) (> target-start source-start
))
628 (let ((new-elts (subseq source-sequence source-start
629 (+ (the fixnum source-start
)
631 (min (- (the fixnum target-end
)
632 (the fixnum target-start
))
633 (- (the fixnum source-end
)
634 (the fixnum source-start
))))))))
635 (do ((n new-elts
(cdr n
))
636 (o (nthcdr target-start target-sequence
) (cdr o
)))
637 ((null n
) target-sequence
)
639 (do ((target-index target-start
(1+ target-index
))
640 (source-index source-start
(1+ source-index
))
641 (target-sequence-ref (nthcdr target-start target-sequence
)
642 (cdr target-sequence-ref
))
643 (source-sequence-ref (nthcdr source-start source-sequence
)
644 (cdr source-sequence-ref
)))
645 ((or (= target-index
(the fixnum target-end
))
646 (= source-index
(the fixnum source-end
))
647 (null target-sequence-ref
) (null source-sequence-ref
))
649 (declare (fixnum target-index source-index
))
650 (rplaca target-sequence-ref
(car source-sequence-ref
)))))
652 (sb!xc
:defmacro list-replace-from-mumble
()
653 `(do ((target-index target-start
(1+ target-index
))
654 (source-index source-start
(1+ source-index
))
655 (target-sequence-ref (nthcdr target-start target-sequence
)
656 (cdr target-sequence-ref
)))
657 ((or (= target-index
(the fixnum target-end
))
658 (= source-index
(the fixnum source-end
))
659 (null target-sequence-ref
))
661 (declare (fixnum source-index target-index
))
662 (rplaca target-sequence-ref
(aref source-sequence source-index
))))
664 (sb!xc
:defmacro mumble-replace-from-list
()
665 `(do ((target-index target-start
(1+ target-index
))
666 (source-index source-start
(1+ source-index
))
667 (source-sequence (nthcdr source-start source-sequence
)
668 (cdr source-sequence
)))
669 ((or (= target-index
(the fixnum target-end
))
670 (= source-index
(the fixnum source-end
))
671 (null source-sequence
))
673 (declare (fixnum target-index source-index
))
674 (setf (aref target-sequence target-index
) (car source-sequence
))))
678 ;;;; The support routines for REPLACE are used by compiler transforms, so we
679 ;;;; worry about dealing with END being supplied or defaulting to NIL
682 (defun list-replace-from-list* (target-sequence source-sequence target-start
683 target-end source-start source-end
)
684 (when (null target-end
) (setq target-end
(length target-sequence
)))
685 (when (null source-end
) (setq source-end
(length source-sequence
)))
686 (list-replace-from-list))
688 (defun list-replace-from-vector* (target-sequence source-sequence target-start
689 target-end source-start source-end
)
690 (when (null target-end
) (setq target-end
(length target-sequence
)))
691 (when (null source-end
) (setq source-end
(length source-sequence
)))
692 (list-replace-from-mumble))
694 (defun vector-replace-from-list* (target-sequence source-sequence target-start
695 target-end source-start source-end
)
696 (when (null target-end
) (setq target-end
(length target-sequence
)))
697 (when (null source-end
) (setq source-end
(length source-sequence
)))
698 (mumble-replace-from-list))
700 (defun vector-replace-from-vector* (target-sequence source-sequence
701 target-start target-end source-start
703 (when (null target-end
) (setq target-end
(length target-sequence
)))
704 (when (null source-end
) (setq source-end
(length source-sequence
)))
705 (mumble-replace-from-mumble))
708 (defun simple-character-string-replace-from-simple-character-string*
709 (target-sequence source-sequence
710 target-start target-end source-start source-end
)
711 (declare (type (simple-array character
(*)) target-sequence source-sequence
))
712 (when (null target-end
) (setq target-end
(length target-sequence
)))
713 (when (null source-end
) (setq source-end
(length source-sequence
)))
714 (mumble-replace-from-mumble))
716 (define-sequence-traverser replace
717 (sequence1 sequence2
&rest args
&key start1 end1 start2 end2
)
719 "Destructively modifies SEQUENCE1 by copying successive elements
720 into it from the SEQUENCE2.
722 Elements are copied to the subseqeuence bounded by START1 and END1,
723 from the subsequence bounded by START2 and END2. If these subsequences
724 are not of the same length, then the shorter length determines how
725 many elements are copied."
726 (declare (truly-dynamic-extent args
))
727 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
728 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
729 ;; these things here so that legacy code gets the names it's
730 ;; expecting. We could use &AUX instead :-/.
731 (target-sequence sequence1
)
732 (source-sequence sequence2
)
733 (target-start start1
)
734 (source-start start2
)
735 (target-end (or end1 length1
))
736 (source-end (or end2 length2
)))
737 (seq-dispatch target-sequence
738 (seq-dispatch source-sequence
739 (list-replace-from-list)
740 (list-replace-from-mumble)
741 (apply #'sb
!sequence
:replace sequence1 sequence2 args
))
742 (seq-dispatch source-sequence
743 (mumble-replace-from-list)
744 (mumble-replace-from-mumble)
745 (apply #'sb
!sequence
:replace sequence1 sequence2 args
))
746 (apply #'sb
!sequence
:replace sequence1 sequence2 args
))))
750 (eval-when (:compile-toplevel
:execute
)
752 (sb!xc
:defmacro vector-reverse
(sequence)
753 `(let ((length (length ,sequence
)))
754 (declare (fixnum length
))
755 (do ((forward-index 0 (1+ forward-index
))
756 (backward-index (1- length
) (1- backward-index
))
757 (new-sequence (%make-sequence-like sequence length
)))
758 ((= forward-index length
) new-sequence
)
759 (declare (fixnum forward-index backward-index
))
760 (setf (aref new-sequence forward-index
)
761 (aref ,sequence backward-index
)))))
763 (sb!xc
:defmacro list-reverse-macro
(sequence)
765 ((endp ,sequence
) new-list
)
766 (push (pop ,sequence
) new-list
)))
770 (defun reverse (sequence)
772 "Return a new sequence containing the same elements but in reverse order."
773 (seq-dispatch-checking sequence
774 (list-reverse* sequence
)
775 (vector-reverse* sequence
)
776 ;; The type deriver says that LIST => LIST and VECTOR => VECTOR
777 ;; but does not claim to know anything about extended-sequences.
778 ;; So this could theoretically return any subtype of SEQUENCE
779 ;; given an EXTENDED-SEQUENCE as input. But fndb says this returns
780 ;; a CONSED-SEQUENCE, which precludes non-simple vectors.
781 ;; But a CLOS sequence can apparently decide to return a LIST when
782 ;; reversed. [Is that too weird? Make this EXTENDED-SEQUENCE maybe?]
783 (the consed-sequence
(values (sb!sequence
:reverse sequence
)))))
787 (defun list-reverse* (sequence)
788 (list-reverse-macro sequence
))
790 (defun vector-reverse* (sequence)
791 (vector-reverse sequence
))
795 (eval-when (:compile-toplevel
:execute
)
797 (sb!xc
:defmacro vector-nreverse
(sequence)
798 `(let ((length (length (the vector
,sequence
))))
800 (do ((left-index 0 (1+ left-index
))
801 (right-index (1- length
) (1- right-index
)))
802 ((<= right-index left-index
))
803 (declare (type index left-index right-index
))
804 (rotatef (aref ,sequence left-index
)
805 (aref ,sequence right-index
))))
808 (sb!xc
:defmacro list-nreverse-macro
(list)
809 `(do ((1st (cdr ,list
) (if (endp 1st
) 1st
(cdr 1st
)))
817 (defun list-nreverse* (sequence)
818 (list-nreverse-macro sequence
))
820 (defun vector-nreverse* (sequence)
821 (vector-nreverse sequence
))
823 (defun nreverse (sequence)
825 "Return a sequence of the same elements in reverse order; the argument
827 (seq-dispatch-checking sequence
828 (list-nreverse* sequence
)
829 (vector-nreverse* sequence
)
830 ;; The type deriver for this is 'result-type-first-arg',
831 ;; meaning it should return definitely an EXTENDED-SEQUENCE
832 ;; and not a list or vector.
833 (the extended-sequence
(values (sb!sequence
:nreverse sequence
)))))
837 (defmacro sb
!sequence
:dosequence
((element sequence
&optional return
) &body body
)
839 "Executes BODY with ELEMENT subsequently bound to each element of
840 SEQUENCE, then returns RETURN."
841 (multiple-value-bind (forms decls
) (parse-body body
:doc-string-allowed nil
)
842 (once-only ((sequence sequence
))
843 (with-unique-names (state limit from-end step endp elt
)
845 (seq-dispatch ,sequence
846 (dolist (,element
,sequence
,return
) ,@body
)
847 (do-vector-data (,element
,sequence
,return
) ,@body
)
848 (multiple-value-bind (,state
,limit
,from-end
,step
,endp
,elt
)
849 (sb!sequence
:make-sequence-iterator
,sequence
)
850 (do ((,state
,state
(funcall ,step
,sequence
,state
,from-end
)))
851 ((funcall ,endp
,sequence
,state
,limit
,from-end
)
852 (let ((,element nil
))
853 ,@(filter-dolist-declarations decls
)
854 (declare (ignorable ,element
))
856 (let ((,element
(funcall ,elt
,sequence
,state
)))
862 (defun concatenate (output-type-spec &rest sequences
)
864 "Return a new sequence of all the argument sequences concatenated together
865 which shares no structure with the original argument sequences of the
866 specified OUTPUT-TYPE-SPEC."
867 (flet ((concat-to-list* (sequences)
868 (let ((result (list nil
)))
869 (do ((sequences sequences
(cdr sequences
))
871 ((null sequences
) (cdr result
))
872 (let ((sequence (car sequences
)))
873 (sb!sequence
:dosequence
(e sequence
)
874 (setq splice
(cdr (rplacd splice
(list e
)))))))))
875 (concat-to-simple* (type-spec sequences
)
876 (do ((seqs sequences
(cdr seqs
))
880 (do ((sequences sequences
(cdr sequences
))
881 (lengths lengths
(cdr lengths
))
883 (result (make-sequence type-spec total-length
)))
884 ((= index total-length
) result
)
885 (declare (fixnum index
))
886 (let ((sequence (car sequences
)))
887 (sb!sequence
:dosequence
(e sequence
)
888 (setf (aref result index
) e
)
890 (let ((length (length (car seqs
))))
891 (declare (fixnum length
))
892 (setq lengths
(nconc lengths
(list length
)))
893 (setq total-length
(+ total-length length
))))))
894 (let ((type (specifier-type output-type-spec
)))
896 ((csubtypep type
(specifier-type 'list
))
898 ((type= type
(specifier-type 'list
))
899 (concat-to-list* sequences
))
900 ((eq type
*empty-type
*)
901 (bad-sequence-type-error nil
))
902 ((type= type
(specifier-type 'null
))
903 (unless (every #'emptyp sequences
)
904 (sequence-type-length-mismatch-error
905 type
(reduce #'+ sequences
:key
#'length
))) ; FIXME: circular list issues.
908 (multiple-value-bind (min exactp
)
909 (sb!kernel
::cons-type-length-info type
)
910 (let ((length (reduce #'+ sequences
:key
#'length
)))
912 (unless (= length min
)
913 (sequence-type-length-mismatch-error type length
))
914 (unless (>= length min
)
915 (sequence-type-length-mismatch-error type length
)))
916 (concat-to-list* sequences
))))
917 (t (sequence-type-too-hairy (type-specifier type
)))))
918 ((csubtypep type
(specifier-type 'vector
))
919 (concat-to-simple* output-type-spec sequences
))
920 ((and (csubtypep type
(specifier-type 'sequence
))
921 (find-class output-type-spec nil
))
922 (coerce (concat-to-simple* 'vector sequences
) output-type-spec
))
924 (bad-sequence-type-error output-type-spec
))))))
926 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
927 ;;; CONCATENATE 'STRING &co into these.
928 (macrolet ((def (name element-type
)
929 `(defun ,name
(&rest sequences
)
930 (declare (dynamic-extent sequences
)
932 (optimize (sb!c
::insert-array-bounds-checks
0)))
933 (let* ((lengths (mapcar #'length sequences
))
934 (result (make-array (the integer
(apply #'+ lengths
))
935 :element-type
',element-type
))
937 (declare (index start
))
938 (dolist (seq sequences
)
940 ((simple-array character
(*))
941 (simple-array base-char
(*))
944 (replace result seq
:start1 start
))
945 (incf start
(the index
(pop lengths
))))
947 (def %concatenate-to-string character
)
948 (def %concatenate-to-base-string base-char
))
952 ;;; helper functions to handle arity-1 subcases of MAP
953 (defun %map-to-list-arity-1
(fun sequence
)
954 (let ((reversed-result nil
)
955 (really-fun (%coerce-callable-to-fun fun
)))
956 (sb!sequence
:dosequence
(element sequence
)
957 (push (funcall really-fun element
)
959 (nreverse reversed-result
)))
960 (defun %map-to-simple-vector-arity-1
(fun sequence
)
961 (let ((result (make-array (length sequence
)))
963 (really-fun (%coerce-callable-to-fun fun
)))
964 (declare (type index index
))
965 (sb!sequence
:dosequence
(element sequence
)
966 (setf (aref result index
)
967 (funcall really-fun element
))
970 (defun %map-for-effect-arity-1
(fun sequence
)
971 (let ((really-fun (%coerce-callable-to-fun fun
)))
972 (sb!sequence
:dosequence
(element sequence
)
973 (funcall really-fun element
)))
976 (declaim (maybe-inline %map-for-effect
))
977 (defun %map-for-effect
(fun sequences
)
978 (declare (type function fun
) (type list sequences
))
979 (let ((%sequences sequences
)
980 (%iters
(mapcar (lambda (s)
985 (sb!sequence
:make-sequence-iterator s
))))
987 (%apply-args
(make-list (length sequences
))))
988 ;; this is almost efficient (except in the general case where we
989 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
990 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
991 (declare (type list %sequences %iters %apply-args
))
993 (do ((in-sequences %sequences
(cdr in-sequences
))
994 (in-iters %iters
(cdr in-iters
))
995 (in-apply-args %apply-args
(cdr in-apply-args
)))
996 ((null in-sequences
) (apply fun %apply-args
))
997 (let ((i (car in-iters
)))
998 (declare (type (or list index
) i
))
1000 ((listp (car in-sequences
))
1002 (return-from %map-for-effect nil
)
1003 (setf (car in-apply-args
) (car i
)
1004 (car in-iters
) (cdr i
))))
1006 (let ((v (the vector
(car in-sequences
))))
1007 (if (>= i
(length v
))
1008 (return-from %map-for-effect nil
)
1009 (setf (car in-apply-args
) (aref v i
)
1010 (car in-iters
) (1+ i
)))))
1012 (destructuring-bind (state limit from-end step endp elt
&rest ignore
)
1014 (declare (type function step endp elt
)
1016 (let ((s (car in-sequences
)))
1017 (if (funcall endp s state limit from-end
)
1018 (return-from %map-for-effect nil
)
1020 (setf (car in-apply-args
) (funcall elt s state
))
1021 (setf (caar in-iters
) (funcall step s state from-end
)))))))))))))
1022 (defun %map-to-list
(fun sequences
)
1023 (declare (type function fun
)
1024 (type list sequences
))
1026 (flet ((f (&rest args
)
1027 (declare (truly-dynamic-extent args
))
1028 (push (apply fun args
) result
)))
1029 (declare (truly-dynamic-extent #'f
))
1030 (%map-for-effect
#'f sequences
))
1032 (defun %map-to-vector
(output-type-spec fun sequences
)
1033 (declare (type function fun
)
1034 (type list sequences
))
1036 (flet ((f (&rest args
)
1037 (declare (truly-dynamic-extent args
))
1038 (declare (ignore args
))
1040 (declare (truly-dynamic-extent #'f
))
1041 (%map-for-effect
#'f sequences
))
1042 (let ((result (make-sequence output-type-spec min-len
))
1044 (declare (type (simple-array * (*)) result
))
1045 (flet ((f (&rest args
)
1046 (declare (truly-dynamic-extent args
))
1047 (setf (aref result i
) (apply fun args
))
1049 (declare (truly-dynamic-extent #'f
))
1050 (%map-for-effect
#'f sequences
))
1052 (defun %map-to-sequence
(result-type fun sequences
)
1053 (declare (type function fun
)
1054 (type list sequences
))
1056 (flet ((f (&rest args
)
1057 (declare (truly-dynamic-extent args
))
1058 (declare (ignore args
))
1060 (declare (truly-dynamic-extent #'f
))
1061 (%map-for-effect
#'f sequences
))
1062 (let ((result (make-sequence result-type min-len
)))
1063 (multiple-value-bind (state limit from-end step endp elt setelt
)
1064 (sb!sequence
:make-sequence-iterator result
)
1065 (declare (ignore limit endp elt
))
1066 (flet ((f (&rest args
)
1067 (declare (truly-dynamic-extent args
))
1068 (funcall setelt
(apply fun args
) result state
)
1069 (setq state
(funcall step result state from-end
))))
1070 (declare (truly-dynamic-extent #'f
))
1071 (%map-for-effect
#'f sequences
)))
1074 ;;; %MAP is just MAP without the final just-to-be-sure check that
1075 ;;; length of the output sequence matches any length specified
1077 (defun %map
(result-type function first-sequence
&rest more-sequences
)
1078 (labels ((slower-map (type)
1079 (let ((really-fun (%coerce-callable-to-fun function
))
1080 (sequences (cons first-sequence more-sequences
)))
1082 ((eq type
*empty-type
*)
1083 (%map-for-effect really-fun sequences
))
1084 ((csubtypep type
(specifier-type 'list
))
1085 (%map-to-list really-fun sequences
))
1086 ((csubtypep type
(specifier-type 'vector
))
1087 (%map-to-vector result-type really-fun sequences
))
1088 ((and (csubtypep type
(specifier-type 'sequence
))
1089 (find-class result-type nil
))
1090 (%map-to-sequence result-type really-fun sequences
))
1092 (bad-sequence-type-error result-type
)))))
1094 (let ((type (specifier-type result-type
)))
1098 ((eq type
*empty-type
*)
1099 (%map-for-effect-arity-1 function first-sequence
))
1100 ((csubtypep type
(specifier-type 'list
))
1101 (%map-to-list-arity-1 function first-sequence
))
1102 ((or (csubtypep type
(specifier-type 'simple-vector
))
1103 (csubtypep type
(specifier-type '(vector t
))))
1104 (%map-to-simple-vector-arity-1 function first-sequence
))
1106 (slower-map type
))))))
1107 ;; Handle some easy cases faster
1108 (cond (more-sequences
1111 (%map-for-effect-arity-1 function first-sequence
))
1112 ((or (eq result-type
'list
)
1113 (eq result-type
'cons
))
1114 (%map-to-list-arity-1 function first-sequence
))
1115 ((or (eq result-type
'vector
)
1116 (eq result-type
'simple-vector
))
1117 (%map-to-simple-vector-arity-1 function first-sequence
))
1121 (defun map (result-type function first-sequence
&rest more-sequences
)
1130 (defmacro map-into-lambda
(sequences params
&body body
)
1131 (check-type sequences symbol
)
1132 `(flet ((f ,params
,@body
))
1133 (declare (truly-dynamic-extent #'f
))
1134 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1135 ;; hence the awkward flip between MAP and LOOP.
1137 (apply #'map nil
#'f
,sequences
)
1140 (define-array-dispatch vector-map-into
(data start end fun sequences
)
1141 (declare (optimize speed
(safety 0))
1142 (type index start end
)
1144 (type list sequences
))
1145 (let ((index start
))
1146 (declare (type index index
))
1148 (map-into-lambda sequences
(&rest args
)
1149 (declare (truly-dynamic-extent args
))
1150 (when (eql index end
)
1151 (return-from mapping
))
1152 (setf (aref data index
) (apply fun args
))
1156 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1157 ;;; computing the length of the result sequence since we can detect
1158 ;;; the end during mapping (if MAP even gets that far).
1160 ;;; For each result type, define a mapping function which is
1161 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1162 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1163 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1165 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1166 ;;; Because we are manually doing bounds checking with known types,
1167 ;;; safety is turned off for vectors and lists but kept for generic
1169 (defun map-into (result-sequence function
&rest sequences
)
1170 (let ((really-fun (%coerce-callable-to-fun function
)))
1171 (etypecase result-sequence
1173 (with-array-data ((data result-sequence
) (start) (end)
1174 ;; MAP-INTO ignores fill pointer when mapping
1175 :check-fill-pointer nil
)
1176 (let ((new-end (vector-map-into data start end really-fun sequences
)))
1177 (when (array-has-fill-pointer-p result-sequence
)
1178 (setf (fill-pointer result-sequence
) (- new-end start
))))))
1180 (let ((node result-sequence
))
1181 (declare (type list node
))
1182 (map-into-lambda sequences
(&rest args
)
1183 (declare (truly-dynamic-extent args
)
1184 (optimize speed
(safety 0)))
1186 (return-from map-into result-sequence
))
1187 (setf (car node
) (apply really-fun args
))
1188 (setf node
(cdr node
)))))
1190 (multiple-value-bind (iter limit from-end
)
1191 (sb!sequence
:make-sequence-iterator result-sequence
)
1192 (map-into-lambda sequences
(&rest args
)
1193 (declare (truly-dynamic-extent args
) (optimize speed
))
1194 (when (sb!sequence
:iterator-endp result-sequence
1195 iter limit from-end
)
1196 (return-from map-into result-sequence
))
1197 (setf (sb!sequence
:iterator-element result-sequence iter
)
1198 (apply really-fun args
))
1199 (setf iter
(sb!sequence
:iterator-step result-sequence
1200 iter from-end
)))))))
1205 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
1206 ;;; arbitrary sequence arguments, both in the full call case and in
1207 ;;; the open code case.
1208 (macrolet ((defquantifier (name found-test found-result
1209 &key doc
(unfound-result (not found-result
)))
1211 ;; KLUDGE: It would be really nice if we could simply
1212 ;; do something like this
1213 ;; (declaim (inline ,name))
1214 ;; (defun ,name (pred first-seq &rest more-seqs)
1216 ;; (flet ((map-me (&rest rest)
1217 ;; (let ((pred-value (apply pred rest)))
1218 ;; (,found-test pred-value
1219 ;; (return-from ,name
1220 ;; ,found-result)))))
1221 ;; (declare (inline map-me))
1222 ;; (apply #'map nil #'map-me first-seq more-seqs)
1223 ;; ,unfound-result))
1224 ;; but Python doesn't seem to be smart enough about
1225 ;; inlining and APPLY to recognize that it can use
1226 ;; the DEFTRANSFORM for MAP in the resulting inline
1227 ;; expansion. I don't have any appetite for deep
1228 ;; compiler hacking right now, so I'll just work
1229 ;; around the apparent problem by using a compiler
1230 ;; macro instead. -- WHN 20000410
1231 (defun ,name
(pred first-seq
&rest more-seqs
)
1233 (flet ((map-me (&rest rest
)
1234 (let ((pred-value (apply pred rest
)))
1235 (,found-test pred-value
1238 (declare (inline map-me
))
1239 (apply #'map nil
#'map-me first-seq more-seqs
)
1241 ;; KLUDGE: It would be more obviously correct -- but
1242 ;; also significantly messier -- for PRED-VALUE to be
1243 ;; a gensym. However, a private symbol really does
1244 ;; seem to be good enough; and anyway the really
1245 ;; obviously correct solution is to make Python smart
1246 ;; enough that we can use an inline function instead
1247 ;; of a compiler macro (as above). -- WHN 20000410
1249 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
1250 ;; important for performance, and it'd be good to have
1251 ;; it be visible throughout the compilation of all the
1252 ;; target SBCL code. That could be done by defining
1253 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
1254 ;; moving this DEFQUANTIFIER stuff (and perhaps other
1255 ;; inline definitions in seq.lisp as well) into a new
1256 ;; seq.lisp, and moving remaining target-only stuff
1257 ;; from the old seq.lisp into target-seq.lisp.
1258 (define-compiler-macro ,name
(pred first-seq
&rest more-seqs
)
1259 (let ((elements (make-gensym-list (1+ (length more-seqs
))))
1260 (blockname (sb!xc
:gensym
"BLOCK"))
1261 (wrapper (sb!xc
:gensym
"WRAPPER")))
1262 (once-only ((pred pred
))
1264 (flet ((,wrapper
(,@elements
)
1265 (declare (optimize (sb!c
::check-tag-existence
0)))
1266 (let ((pred-value (funcall ,pred
,@elements
)))
1267 (,',found-test pred-value
1268 (return-from ,blockname
1269 ,',found-result
)))))
1270 (declare (inline ,wrapper
)
1271 (dynamic-extent #',wrapper
))
1272 (map nil
#',wrapper
,first-seq
1274 ,',unfound-result
)))))))
1275 (defquantifier some when pred-value
:unfound-result nil
:doc
1276 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1277 possibly to those with index 1, and so on. Return the first
1278 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1279 (defquantifier every unless nil
:doc
1280 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1281 possibly to those with index 1, and so on. Return NIL as soon
1282 as any invocation of PREDICATE returns NIL, or T if every invocation
1284 (defquantifier notany when nil
:doc
1285 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1286 possibly to those with index 1, and so on. Return NIL as soon
1287 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1288 of any sequence is reached.")
1289 (defquantifier notevery unless t
:doc
1290 "Apply PREDICATE to 0-indexed elements of the sequences, then
1291 possibly to those with index 1, and so on. Return T as soon
1292 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1297 (eval-when (:compile-toplevel
:execute
)
1299 (sb!xc
:defmacro mumble-reduce
(function
1306 `(do ((index ,start
(1+ index
))
1307 (value ,initial-value
))
1308 ((>= index
,end
) value
)
1309 (setq value
(funcall ,function value
1310 (apply-key ,key
(,ref
,sequence index
))))))
1312 (sb!xc
:defmacro mumble-reduce-from-end
(function
1319 `(do ((index (1- ,end
) (1- index
))
1320 (value ,initial-value
)
1321 (terminus (1- ,start
)))
1322 ((<= index terminus
) value
)
1323 (setq value
(funcall ,function
1324 (apply-key ,key
(,ref
,sequence index
))
1327 (sb!xc
:defmacro list-reduce
(function
1334 `(let ((sequence (nthcdr ,start
,sequence
)))
1335 (do ((count (if ,ivp
,start
(1+ ,start
))
1337 (sequence (if ,ivp sequence
(cdr sequence
))
1339 (value (if ,ivp
,initial-value
(apply-key ,key
(car sequence
)))
1340 (funcall ,function value
(apply-key ,key
(car sequence
)))))
1341 ((>= count
,end
) value
))))
1343 (sb!xc
:defmacro list-reduce-from-end
(function
1350 `(let ((sequence (nthcdr (- (length ,sequence
) ,end
)
1351 (reverse ,sequence
))))
1352 (do ((count (if ,ivp
,start
(1+ ,start
))
1354 (sequence (if ,ivp sequence
(cdr sequence
))
1356 (value (if ,ivp
,initial-value
(apply-key ,key
(car sequence
)))
1357 (funcall ,function
(apply-key ,key
(car sequence
)) value
)))
1358 ((>= count
,end
) value
))))
1362 (define-sequence-traverser reduce
(function sequence
&rest args
&key key
1363 from-end start end
(initial-value nil ivp
))
1364 (declare (type index start
)
1365 (truly-dynamic-extent args
))
1366 (seq-dispatch sequence
1367 (let ((end (or end length
)))
1368 (declare (type index end
))
1370 (if ivp initial-value
(funcall function
))
1372 (list-reduce-from-end function sequence key start end
1374 (list-reduce function sequence key start end
1375 initial-value ivp
))))
1376 (let ((end (or end length
)))
1377 (declare (type index end
))
1379 (if ivp initial-value
(funcall function
))
1383 (setq end
(1- (the fixnum end
)))
1384 (setq initial-value
(apply-key key
(aref sequence end
))))
1385 (mumble-reduce-from-end function sequence key start end
1386 initial-value aref
))
1389 (setq initial-value
(apply-key key
(aref sequence start
)))
1390 (setq start
(1+ start
)))
1391 (mumble-reduce function sequence key start end
1392 initial-value aref
)))))
1393 (apply #'sb
!sequence
:reduce function sequence args
)))
1397 (eval-when (:compile-toplevel
:execute
)
1399 (sb!xc
:defmacro mumble-delete
(pred)
1400 `(do ((index start
(1+ index
))
1403 ((or (= index
(the fixnum end
)) (= number-zapped count
))
1404 (do ((index index
(1+ index
)) ; Copy the rest of the vector.
1405 (jndex jndex
(1+ jndex
)))
1406 ((= index
(the fixnum length
))
1407 (shrink-vector sequence jndex
))
1408 (declare (fixnum index jndex
))
1409 (setf (aref sequence jndex
) (aref sequence index
))))
1410 (declare (fixnum index jndex number-zapped
))
1411 (setf (aref sequence jndex
) (aref sequence index
))
1413 (incf number-zapped
)
1416 (sb!xc
:defmacro mumble-delete-from-end
(pred)
1417 `(do ((index (1- (the fixnum end
)) (1- index
)) ; Find the losers.
1421 (terminus (1- start
)))
1422 ((or (= index terminus
) (= number-zapped count
))
1423 (do ((losers losers
) ; Delete the losers.
1424 (index start
(1+ index
))
1426 ((or (null losers
) (= index
(the fixnum end
)))
1427 (do ((index index
(1+ index
)) ; Copy the rest of the vector.
1428 (jndex jndex
(1+ jndex
)))
1429 ((= index
(the fixnum length
))
1430 (shrink-vector sequence jndex
))
1431 (declare (fixnum index jndex
))
1432 (setf (aref sequence jndex
) (aref sequence index
))))
1433 (declare (fixnum index jndex
))
1434 (setf (aref sequence jndex
) (aref sequence index
))
1435 (if (= index
(the fixnum
(car losers
)))
1438 (declare (fixnum index number-zapped terminus
))
1439 (setq this-element
(aref sequence index
))
1441 (incf number-zapped
)
1442 (push index losers
))))
1444 (sb!xc
:defmacro normal-mumble-delete
()
1447 (not (funcall test-not item
(apply-key key
(aref sequence index
))))
1448 (funcall test item
(apply-key key
(aref sequence index
))))))
1450 (sb!xc
:defmacro normal-mumble-delete-from-end
()
1451 `(mumble-delete-from-end
1453 (not (funcall test-not item
(apply-key key this-element
)))
1454 (funcall test item
(apply-key key this-element
)))))
1456 (sb!xc
:defmacro list-delete
(pred)
1457 `(let ((handle (cons nil sequence
)))
1458 (do ((current (nthcdr start sequence
) (cdr current
))
1459 (previous (nthcdr start handle
))
1460 (index start
(1+ index
))
1462 ((or (= index
(the fixnum end
)) (= number-zapped count
))
1464 (declare (fixnum index number-zapped
))
1466 (rplacd previous
(cdr current
))
1467 (incf number-zapped
))
1469 (setq previous
(cdr previous
)))))))
1471 (sb!xc
:defmacro list-delete-from-end
(pred)
1472 `(let* ((reverse (nreverse (the list sequence
)))
1473 (handle (cons nil reverse
)))
1474 (do ((current (nthcdr (- (the fixnum length
) (the fixnum end
)) reverse
)
1476 (previous (nthcdr (- (the fixnum length
) (the fixnum end
)) handle
))
1477 (index start
(1+ index
))
1479 ((or (= index
(the fixnum end
)) (= number-zapped count
))
1480 (nreverse (cdr handle
)))
1481 (declare (fixnum index number-zapped
))
1483 (rplacd previous
(cdr current
))
1484 (incf number-zapped
))
1486 (setq previous
(cdr previous
)))))))
1488 (sb!xc
:defmacro normal-list-delete
()
1491 (not (funcall test-not item
(apply-key key
(car current
))))
1492 (funcall test item
(apply-key key
(car current
))))))
1494 (sb!xc
:defmacro normal-list-delete-from-end
()
1495 '(list-delete-from-end
1497 (not (funcall test-not item
(apply-key key
(car current
))))
1498 (funcall test item
(apply-key key
(car current
))))))
1502 (define-sequence-traverser delete
1503 (item sequence
&rest args
&key from-end test test-not start
1506 "Return a sequence formed by destructively removing the specified ITEM from
1507 the given SEQUENCE."
1508 (declare (type fixnum start
)
1509 (truly-dynamic-extent args
))
1510 (seq-dispatch sequence
1511 (let ((end (or end length
)))
1512 (declare (type index end
))
1514 (normal-list-delete-from-end)
1515 (normal-list-delete)))
1516 (let ((end (or end length
)))
1517 (declare (type index end
))
1519 (normal-mumble-delete-from-end)
1520 (normal-mumble-delete)))
1521 (apply #'sb
!sequence
:delete item sequence args
)))
1523 (eval-when (:compile-toplevel
:execute
)
1525 (sb!xc
:defmacro if-mumble-delete
()
1527 (funcall predicate
(apply-key key
(aref sequence index
)))))
1529 (sb!xc
:defmacro if-mumble-delete-from-end
()
1530 `(mumble-delete-from-end
1531 (funcall predicate
(apply-key key this-element
))))
1533 (sb!xc
:defmacro if-list-delete
()
1535 (funcall predicate
(apply-key key
(car current
)))))
1537 (sb!xc
:defmacro if-list-delete-from-end
()
1538 '(list-delete-from-end
1539 (funcall predicate
(apply-key key
(car current
)))))
1543 (define-sequence-traverser delete-if
1544 (predicate sequence
&rest args
&key from-end start key end count
)
1546 "Return a sequence formed by destructively removing the elements satisfying
1547 the specified PREDICATE from the given SEQUENCE."
1548 (declare (type fixnum start
)
1549 (truly-dynamic-extent args
))
1550 (seq-dispatch sequence
1551 (let ((end (or end length
)))
1552 (declare (type index end
))
1554 (if-list-delete-from-end)
1556 (let ((end (or end length
)))
1557 (declare (type index end
))
1559 (if-mumble-delete-from-end)
1560 (if-mumble-delete)))
1561 (apply #'sb
!sequence
:delete-if predicate sequence args
)))
1563 (eval-when (:compile-toplevel
:execute
)
1565 (sb!xc
:defmacro if-not-mumble-delete
()
1567 (not (funcall predicate
(apply-key key
(aref sequence index
))))))
1569 (sb!xc
:defmacro if-not-mumble-delete-from-end
()
1570 `(mumble-delete-from-end
1571 (not (funcall predicate
(apply-key key this-element
)))))
1573 (sb!xc
:defmacro if-not-list-delete
()
1575 (not (funcall predicate
(apply-key key
(car current
))))))
1577 (sb!xc
:defmacro if-not-list-delete-from-end
()
1578 '(list-delete-from-end
1579 (not (funcall predicate
(apply-key key
(car current
))))))
1583 (define-sequence-traverser delete-if-not
1584 (predicate sequence
&rest args
&key from-end start end key count
)
1586 "Return a sequence formed by destructively removing the elements not
1587 satisfying the specified PREDICATE from the given SEQUENCE."
1588 (declare (type fixnum start
)
1589 (truly-dynamic-extent args
))
1590 (seq-dispatch sequence
1591 (let ((end (or end length
)))
1592 (declare (type index end
))
1594 (if-not-list-delete-from-end)
1595 (if-not-list-delete)))
1596 (let ((end (or end length
)))
1597 (declare (type index end
))
1599 (if-not-mumble-delete-from-end)
1600 (if-not-mumble-delete)))
1601 (apply #'sb
!sequence
:delete-if-not predicate sequence args
)))
1605 (eval-when (:compile-toplevel
:execute
)
1607 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1608 ;;; satisfies the predicate.
1609 (sb!xc
:defmacro mumble-remove-macro
(bump left begin finish right pred
)
1610 `(do ((index ,begin
(,bump index
))
1612 (do ((index ,left
(,bump index
))
1613 (result (%make-sequence-like sequence length
)))
1614 ((= index
(the fixnum
,begin
)) result
)
1615 (declare (fixnum index
))
1616 (setf (aref result index
) (aref sequence index
))))
1620 ((or (= index
(the fixnum
,finish
))
1621 (= number-zapped count
))
1622 (do ((index index
(,bump index
))
1623 (new-index new-index
(,bump new-index
)))
1624 ((= index
(the fixnum
,right
)) (%shrink-vector result new-index
))
1625 (declare (fixnum index new-index
))
1626 (setf (aref result new-index
) (aref sequence index
))))
1627 (declare (fixnum index new-index number-zapped
))
1628 (setq this-element
(aref sequence index
))
1629 (cond (,pred
(incf number-zapped
))
1630 (t (setf (aref result new-index
) this-element
)
1631 (setq new-index
(,bump new-index
))))))
1633 (sb!xc
:defmacro mumble-remove
(pred)
1634 `(mumble-remove-macro 1+ 0 start end length
,pred
))
1636 (sb!xc
:defmacro mumble-remove-from-end
(pred)
1637 `(let ((sequence (copy-seq sequence
)))
1638 (mumble-delete-from-end ,pred
)))
1640 (sb!xc
:defmacro normal-mumble-remove
()
1643 (not (funcall test-not item
(apply-key key this-element
)))
1644 (funcall test item
(apply-key key this-element
)))))
1646 (sb!xc
:defmacro normal-mumble-remove-from-end
()
1647 `(mumble-remove-from-end
1649 (not (funcall test-not item
(apply-key key this-element
)))
1650 (funcall test item
(apply-key key this-element
)))))
1652 (sb!xc
:defmacro if-mumble-remove
()
1653 `(mumble-remove (funcall predicate
(apply-key key this-element
))))
1655 (sb!xc
:defmacro if-mumble-remove-from-end
()
1656 `(mumble-remove-from-end (funcall predicate
(apply-key key this-element
))))
1658 (sb!xc
:defmacro if-not-mumble-remove
()
1659 `(mumble-remove (not (funcall predicate
(apply-key key this-element
)))))
1661 (sb!xc
:defmacro if-not-mumble-remove-from-end
()
1662 `(mumble-remove-from-end
1663 (not (funcall predicate
(apply-key key this-element
)))))
1665 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1667 (sb!xc
:defmacro list-remove-macro
(pred reverse?
)
1668 `(let* ((sequence ,(if reverse?
1669 '(reverse (the list sequence
))
1671 (%start
,(if reverse?
'(- length end
) 'start
))
1672 (%end
,(if reverse?
'(- length start
) 'end
))
1674 (results (do ((index 0 (1+ index
))
1675 (before-start splice
))
1676 ((= index
(the fixnum %start
)) before-start
)
1677 (declare (fixnum index
))
1679 (cdr (rplacd splice
(list (pop sequence
))))))))
1680 (do ((index %start
(1+ index
))
1683 ((or (= index
(the fixnum %end
)) (= number-zapped count
))
1684 (do ((index index
(1+ index
)))
1687 '(nreverse (the list
(cdr results
)))
1689 (declare (fixnum index
))
1690 (setq splice
(cdr (rplacd splice
(list (pop sequence
)))))))
1691 (declare (fixnum index number-zapped
))
1692 (setq this-element
(pop sequence
))
1694 (setq number-zapped
(1+ number-zapped
))
1695 (setq splice
(cdr (rplacd splice
(list this-element
))))))))
1697 (sb!xc
:defmacro list-remove
(pred)
1698 `(list-remove-macro ,pred nil
))
1700 (sb!xc
:defmacro list-remove-from-end
(pred)
1701 `(list-remove-macro ,pred t
))
1703 (sb!xc
:defmacro normal-list-remove
()
1706 (not (funcall test-not item
(apply-key key this-element
)))
1707 (funcall test item
(apply-key key this-element
)))))
1709 (sb!xc
:defmacro normal-list-remove-from-end
()
1710 `(list-remove-from-end
1712 (not (funcall test-not item
(apply-key key this-element
)))
1713 (funcall test item
(apply-key key this-element
)))))
1715 (sb!xc
:defmacro if-list-remove
()
1717 (funcall predicate
(apply-key key this-element
))))
1719 (sb!xc
:defmacro if-list-remove-from-end
()
1720 `(list-remove-from-end
1721 (funcall predicate
(apply-key key this-element
))))
1723 (sb!xc
:defmacro if-not-list-remove
()
1725 (not (funcall predicate
(apply-key key this-element
)))))
1727 (sb!xc
:defmacro if-not-list-remove-from-end
()
1728 `(list-remove-from-end
1729 (not (funcall predicate
(apply-key key this-element
)))))
1733 (define-sequence-traverser remove
1734 (item sequence
&rest args
&key from-end test test-not start
1737 "Return a copy of SEQUENCE with elements satisfying the test (default is
1738 EQL) with ITEM removed."
1739 (declare (type fixnum start
)
1740 (truly-dynamic-extent args
))
1741 (seq-dispatch sequence
1742 (let ((end (or end length
)))
1743 (declare (type index end
))
1745 (normal-list-remove-from-end)
1746 (normal-list-remove)))
1747 (let ((end (or end length
)))
1748 (declare (type index end
))
1750 (normal-mumble-remove-from-end)
1751 (normal-mumble-remove)))
1752 (apply #'sb
!sequence
:remove item sequence args
)))
1754 (define-sequence-traverser remove-if
1755 (predicate sequence
&rest args
&key from-end start end count key
)
1757 "Return a copy of sequence with elements satisfying PREDICATE removed."
1758 (declare (type fixnum start
)
1759 (truly-dynamic-extent args
))
1760 (seq-dispatch sequence
1761 (let ((end (or end length
)))
1762 (declare (type index end
))
1764 (if-list-remove-from-end)
1766 (let ((end (or end length
)))
1767 (declare (type index end
))
1769 (if-mumble-remove-from-end)
1770 (if-mumble-remove)))
1771 (apply #'sb
!sequence
:remove-if predicate sequence args
)))
1773 (define-sequence-traverser remove-if-not
1774 (predicate sequence
&rest args
&key from-end start end count key
)
1776 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1777 (declare (type fixnum start
)
1778 (truly-dynamic-extent args
))
1779 (seq-dispatch sequence
1780 (let ((end (or end length
)))
1781 (declare (type index end
))
1783 (if-not-list-remove-from-end)
1784 (if-not-list-remove)))
1785 (let ((end (or end length
)))
1786 (declare (type index end
))
1788 (if-not-mumble-remove-from-end)
1789 (if-not-mumble-remove)))
1790 (apply #'sb
!sequence
:remove-if-not predicate sequence args
)))
1792 ;;;; REMOVE-DUPLICATES
1794 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1795 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1796 ;;; if we look into the already copied structure (from after :start) and see
1797 ;;; the item. If we check from beginning we check into the rest of the
1798 ;;; original list up to the :end marker (this we have to do by running a
1799 ;;; do loop down the list that far and using our test.
1800 (defun list-remove-duplicates* (list test test-not start end key from-end
)
1801 (declare (fixnum start
))
1802 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1805 (end (or end
(length list
)))
1806 (hash (and (> (- end start
) 20)
1810 (or (eql test
#'eql
)
1813 (eql test
#'equalp
))
1814 (make-hash-table :test test
:size
(- end start
)))))
1815 (do ((index 0 (1+ index
)))
1817 (declare (fixnum index
))
1818 (setq splice
(cdr (rplacd splice
(list (car current
)))))
1819 (setq current
(cdr current
)))
1821 (do ((index start
(1+ index
)))
1822 ((or (and end
(= index
(the fixnum end
)))
1824 (declare (fixnum index
))
1825 ;; The hash table contains links from values that are
1826 ;; already in result to the cons cell *preceding* theirs
1827 ;; in the list. That is, for each value v in the list,
1828 ;; v and (cadr (gethash v hash)) are equal under TEST.
1829 (let ((prev (gethash (car current
) hash
)))
1832 (setf (gethash (car current
) hash
) splice
)
1833 (setq splice
(cdr (rplacd splice
(list (car current
))))))
1835 (let* ((old (cdr prev
))
1838 (let ((next-val (car next
)))
1839 ;; (assert (eq (gethash next-val hash) old))
1840 (setf (cdr prev
) next
1841 (gethash next-val hash
) prev
1842 (gethash (car current
) hash
) splice
1843 splice
(cdr (rplacd splice
(list (car current
))))))
1844 (setf (car old
) (car current
)))))))
1845 (setq current
(cdr current
)))
1846 (do ((index start
(1+ index
)))
1847 ((or (and end
(= index
(the fixnum end
)))
1849 (declare (fixnum index
))
1850 (if (or (and from-end
1852 (member (apply-key key
(car current
))
1853 (nthcdr (1+ start
) result
)
1856 (member (apply-key key
(car current
))
1857 (nthcdr (1+ start
) result
)
1861 (not (do ((it (apply-key key
(car current
)))
1862 (l (cdr current
) (cdr l
))
1863 (i (1+ index
) (1+ i
)))
1864 ((or (atom l
) (and end
(= i
(the fixnum end
))))
1866 (declare (fixnum i
))
1868 (not (funcall test-not
1870 (apply-key key
(car l
))))
1871 (funcall test it
(apply-key key
(car l
))))
1873 (setq splice
(cdr (rplacd splice
(list (car current
))))))
1874 (setq current
(cdr current
))))
1877 (setq splice
(cdr (rplacd splice
(list (car current
)))))
1878 (setq current
(cdr current
)))
1881 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1882 &optional
(length (length vector
)))
1883 (declare (vector vector
) (fixnum start length
))
1884 (when (null end
) (setf end
(length vector
)))
1885 (let ((result (%make-sequence-like vector length
))
1888 (declare (fixnum index jndex
))
1891 (setf (aref result index
) (aref vector index
))
1892 (setq index
(1+ index
)))
1895 (setq elt
(aref vector index
))
1896 (unless (or (and from-end
1898 (position (apply-key key elt
) result
1899 :start start
:end jndex
1900 :test-not test-not
:key key
)
1901 (position (apply-key key elt
) result
1902 :start start
:end jndex
1903 :test test
:key key
)))
1906 (position (apply-key key elt
) vector
1907 :start
(1+ index
) :end end
1908 :test-not test-not
:key key
)
1909 (position (apply-key key elt
) vector
1910 :start
(1+ index
) :end end
1911 :test test
:key key
))))
1912 (setf (aref result jndex
) elt
)
1913 (setq jndex
(1+ jndex
)))
1914 (setq index
(1+ index
)))
1917 (setf (aref result jndex
) (aref vector index
))
1918 (setq index
(1+ index
))
1919 (setq jndex
(1+ jndex
)))
1920 (%shrink-vector result jndex
)))
1922 (define-sequence-traverser remove-duplicates
1923 (sequence &rest args
&key test test-not start end from-end key
)
1925 "The elements of SEQUENCE are compared pairwise, and if any two match,
1926 the one occurring earlier is discarded, unless FROM-END is true, in
1927 which case the one later in the sequence is discarded. The resulting
1928 sequence is returned.
1930 The :TEST-NOT argument is deprecated."
1931 (declare (fixnum start
)
1932 (truly-dynamic-extent args
))
1933 (seq-dispatch sequence
1935 (list-remove-duplicates* sequence test test-not
1936 start end key from-end
))
1937 (vector-remove-duplicates* sequence test test-not start end key from-end
)
1938 (apply #'sb
!sequence
:remove-duplicates sequence args
)))
1940 ;;;; DELETE-DUPLICATES
1942 (defun list-delete-duplicates* (list test test-not key from-end start end
)
1943 (declare (fixnum start
))
1944 (let ((handle (cons nil list
)))
1945 (do ((current (nthcdr start list
) (cdr current
))
1946 (previous (nthcdr start handle
))
1947 (index start
(1+ index
)))
1948 ((or (and end
(= index
(the fixnum end
))) (null current
))
1950 (declare (fixnum index
))
1951 (if (do ((x (if from-end
1952 (nthcdr (1+ start
) handle
)
1955 (i (1+ index
) (1+ i
)))
1957 (and (not from-end
) end
(= i
(the fixnum end
)))
1960 (declare (fixnum i
))
1962 (not (funcall test-not
1963 (apply-key key
(car current
))
1964 (apply-key key
(car x
))))
1966 (apply-key key
(car current
))
1967 (apply-key key
(car x
))))
1969 (rplacd previous
(cdr current
))
1970 (setq previous
(cdr previous
))))))
1972 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1973 &optional
(length (length vector
)))
1974 (declare (vector vector
) (fixnum start length
))
1975 (when (null end
) (setf end
(length vector
)))
1976 (do ((index start
(1+ index
))
1979 (do ((index index
(1+ index
)) ; copy the rest of the vector
1980 (jndex jndex
(1+ jndex
)))
1982 (shrink-vector vector jndex
))
1983 (setf (aref vector jndex
) (aref vector index
))))
1984 (declare (fixnum index jndex
))
1985 (setf (aref vector jndex
) (aref vector index
))
1986 (unless (if test-not
1987 (position (apply-key key
(aref vector index
)) vector
:key key
1988 :start
(if from-end start
(1+ index
))
1989 :end
(if from-end jndex end
)
1991 (position (apply-key key
(aref vector index
)) vector
:key key
1992 :start
(if from-end start
(1+ index
))
1993 :end
(if from-end jndex end
)
1995 (setq jndex
(1+ jndex
)))))
1997 (define-sequence-traverser delete-duplicates
1998 (sequence &rest args
&key test test-not start end from-end key
)
2000 "The elements of SEQUENCE are examined, and if any two match, one is
2001 discarded. The resulting sequence, which may be formed by destroying the
2002 given sequence, is returned.
2004 The :TEST-NOT argument is deprecated."
2005 (declare (truly-dynamic-extent args
))
2006 (seq-dispatch sequence
2008 (list-delete-duplicates* sequence test test-not
2009 key from-end start end
))
2010 (vector-delete-duplicates* sequence test test-not key from-end start end
)
2011 (apply #'sb
!sequence
:delete-duplicates sequence args
)))
2015 (defun list-substitute* (pred new list start end count key test test-not old
)
2016 (declare (fixnum start end count
))
2017 (let* ((result (list nil
))
2020 (list list
)) ; Get a local list for a stepper.
2021 (do ((index 0 (1+ index
)))
2023 (declare (fixnum index
))
2024 (setq splice
(cdr (rplacd splice
(list (car list
)))))
2025 (setq list
(cdr list
)))
2026 (do ((index start
(1+ index
)))
2027 ((or (= index end
) (null list
) (= count
0)))
2028 (declare (fixnum index
))
2029 (setq elt
(car list
))
2038 (funcall test-not old
(apply-key key elt
)))
2039 (funcall test old
(apply-key key elt
))))
2040 (if (funcall test
(apply-key key elt
)))
2041 (if-not (not (funcall test
(apply-key key elt
)))))
2045 (setq list
(cdr list
)))
2048 (setq splice
(cdr (rplacd splice
(list (car list
)))))
2049 (setq list
(cdr list
)))
2052 ;;; Replace old with new in sequence moving from left to right by incrementer
2053 ;;; on each pass through the loop. Called by all three substitute functions.
2054 (defun vector-substitute* (pred new sequence incrementer left right length
2055 start end count key test test-not old
)
2056 (declare (fixnum start count end incrementer right
))
2057 (let ((result (%make-sequence-like sequence length
))
2059 (declare (fixnum index
))
2062 (setf (aref result index
) (aref sequence index
))
2063 (setq index
(+ index incrementer
)))
2065 ((or (= index end
) (= count
0)))
2066 (setq elt
(aref sequence index
))
2067 (setf (aref result index
)
2071 (not (funcall test-not old
(apply-key key elt
)))
2072 (funcall test old
(apply-key key elt
))))
2073 (if (funcall test
(apply-key key elt
)))
2074 (if-not (not (funcall test
(apply-key key elt
)))))
2075 (setq count
(1- count
))
2078 (setq index
(+ index incrementer
)))
2081 (setf (aref result index
) (aref sequence index
))
2082 (setq index
(+ index incrementer
)))
2085 (eval-when (:compile-toplevel
:execute
)
2087 (sb!xc
:defmacro subst-dispatch
(pred)
2088 `(seq-dispatch sequence
2089 (let ((end (or end length
)))
2090 (declare (type index end
))
2092 (nreverse (list-substitute* ,pred
2095 (- (the fixnum length
)
2097 (- (the fixnum length
)
2099 count key test test-not old
))
2100 (list-substitute* ,pred
2101 new sequence start end count key test test-not
2104 (let ((end (or end length
)))
2105 (declare (type index end
))
2107 (vector-substitute* ,pred new sequence -
1 (1- (the fixnum length
))
2108 -
1 length
(1- (the fixnum end
))
2109 (1- (the fixnum start
))
2110 count key test test-not old
)
2111 (vector-substitute* ,pred new sequence
1 0 length length
2112 start end count key test test-not old
)))
2114 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2115 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2116 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2117 ;; dispatch once per element on PRED's run-time identity.
2119 ((normal) `(apply #'sb
!sequence
:substitute new old sequence args
))
2120 ((if) `(apply #'sb
!sequence
:substitute-if new predicate sequence args
))
2121 ((if-not) `(apply #'sb
!sequence
:substitute-if-not new predicate sequence args
)))))
2124 (define-sequence-traverser substitute
2125 (new old sequence
&rest args
&key from-end test test-not
2126 start count end key
)
2128 "Return a sequence of the same kind as SEQUENCE with the same elements,
2129 except that all elements equal to OLD are replaced with NEW."
2130 (declare (type fixnum start
)
2131 (truly-dynamic-extent args
))
2132 (subst-dispatch 'normal
))
2134 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2136 (define-sequence-traverser substitute-if
2137 (new predicate sequence
&rest args
&key from-end start end count key
)
2139 "Return a sequence of the same kind as SEQUENCE with the same elements
2140 except that all elements satisfying the PRED are replaced with NEW."
2141 (declare (type fixnum start
)
2142 (truly-dynamic-extent args
))
2143 (let ((test predicate
)
2146 (subst-dispatch 'if
)))
2148 (define-sequence-traverser substitute-if-not
2149 (new predicate sequence
&rest args
&key from-end start end count key
)
2151 "Return a sequence of the same kind as SEQUENCE with the same elements
2152 except that all elements not satisfying the PRED are replaced with NEW."
2153 (declare (type fixnum start
)
2154 (truly-dynamic-extent args
))
2155 (let ((test predicate
)
2158 (subst-dispatch 'if-not
)))
2162 (define-sequence-traverser nsubstitute
2163 (new old sequence
&rest args
&key from-end test test-not
2164 end count key start
)
2166 "Return a sequence of the same kind as SEQUENCE with the same elements
2167 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2168 may be destructively modified."
2169 (declare (type fixnum start
)
2170 (truly-dynamic-extent args
))
2171 (seq-dispatch sequence
2172 (let ((end (or end length
)))
2173 (declare (type index end
))
2175 (nreverse (nlist-substitute*
2176 new old
(nreverse (the list sequence
))
2177 test test-not
(- length end
) (- length start
)
2179 (nlist-substitute* new old sequence
2180 test test-not start end count key
)))
2181 (let ((end (or end length
)))
2182 (declare (type index end
))
2184 (nvector-substitute* new old sequence -
1
2185 test test-not
(1- end
) (1- start
) count key
)
2186 (nvector-substitute* new old sequence
1
2187 test test-not start end count key
)))
2188 (apply #'sb
!sequence
:nsubstitute new old sequence args
)))
2190 (defun nlist-substitute* (new old sequence test test-not start end count key
)
2191 (declare (fixnum start count end
))
2192 (do ((list (nthcdr start sequence
) (cdr list
))
2193 (index start
(1+ index
)))
2194 ((or (= index end
) (null list
) (= count
0)) sequence
)
2195 (declare (fixnum index
))
2197 (not (funcall test-not old
(apply-key key
(car list
))))
2198 (funcall test old
(apply-key key
(car list
))))
2200 (setq count
(1- count
)))))
2202 (defun nvector-substitute* (new old sequence incrementer
2203 test test-not start end count key
)
2204 (declare (fixnum start incrementer count end
))
2205 (do ((index start
(+ index incrementer
)))
2206 ((or (= index end
) (= count
0)) sequence
)
2207 (declare (fixnum index
))
2209 (not (funcall test-not
2211 (apply-key key
(aref sequence index
))))
2212 (funcall test old
(apply-key key
(aref sequence index
))))
2213 (setf (aref sequence index
) new
)
2214 (setq count
(1- count
)))))
2216 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2218 (define-sequence-traverser nsubstitute-if
2219 (new predicate sequence
&rest args
&key from-end start end count key
)
2221 "Return a sequence of the same kind as SEQUENCE with the same elements
2222 except that all elements satisfying PREDICATE are replaced with NEW.
2223 SEQUENCE may be destructively modified."
2224 (declare (type fixnum start
)
2225 (truly-dynamic-extent args
))
2226 (seq-dispatch sequence
2227 (let ((end (or end length
)))
2228 (declare (type index end
))
2230 (nreverse (nlist-substitute-if*
2231 new predicate
(nreverse (the list sequence
))
2232 (- length end
) (- length start
) count key
))
2233 (nlist-substitute-if* new predicate sequence
2234 start end count key
)))
2235 (let ((end (or end length
)))
2236 (declare (type index end
))
2238 (nvector-substitute-if* new predicate sequence -
1
2239 (1- end
) (1- start
) count key
)
2240 (nvector-substitute-if* new predicate sequence
1
2241 start end count key
)))
2242 (apply #'sb
!sequence
:nsubstitute-if new predicate sequence args
)))
2244 (defun nlist-substitute-if* (new test sequence start end count key
)
2245 (declare (type fixnum end
)
2246 (type function test
)) ; coercion is done by caller
2247 (do ((list (nthcdr start sequence
) (cdr list
))
2248 (index start
(1+ index
)))
2249 ((or (= index end
) (null list
) (= count
0)) sequence
)
2250 (when (funcall test
(apply-key key
(car list
)))
2252 (setq count
(1- count
)))))
2254 (defun nvector-substitute-if* (new test sequence incrementer
2255 start end count key
)
2256 (declare (type fixnum end
)
2257 (type function test
)) ; coercion is done by caller
2258 (do ((index start
(+ index incrementer
)))
2259 ((or (= index end
) (= count
0)) sequence
)
2260 (when (funcall test
(apply-key key
(aref sequence index
)))
2261 (setf (aref sequence index
) new
)
2262 (setq count
(1- count
)))))
2264 (define-sequence-traverser nsubstitute-if-not
2265 (new predicate sequence
&rest args
&key from-end start end count key
)
2267 "Return a sequence of the same kind as SEQUENCE with the same elements
2268 except that all elements not satisfying PREDICATE are replaced with NEW.
2269 SEQUENCE may be destructively modified."
2270 (declare (type fixnum start
)
2271 (truly-dynamic-extent args
))
2272 (seq-dispatch sequence
2273 (let ((end (or end length
)))
2274 (declare (fixnum end
))
2276 (nreverse (nlist-substitute-if-not*
2277 new predicate
(nreverse (the list sequence
))
2278 (- length end
) (- length start
) count key
))
2279 (nlist-substitute-if-not* new predicate sequence
2280 start end count key
)))
2281 (let ((end (or end length
)))
2282 (declare (fixnum end
))
2284 (nvector-substitute-if-not* new predicate sequence -
1
2285 (1- end
) (1- start
) count key
)
2286 (nvector-substitute-if-not* new predicate sequence
1
2287 start end count key
)))
2288 (apply #'sb
!sequence
:nsubstitute-if-not new predicate sequence args
)))
2290 (defun nlist-substitute-if-not* (new test sequence start end count key
)
2291 (declare (type fixnum end
)
2292 (type function test
)) ; coercion is done by caller
2293 (do ((list (nthcdr start sequence
) (cdr list
))
2294 (index start
(1+ index
)))
2295 ((or (= index end
) (null list
) (= count
0)) sequence
)
2296 (when (not (funcall test
(apply-key key
(car list
))))
2300 (defun nvector-substitute-if-not* (new test sequence incrementer
2301 start end count key
)
2302 (declare (type fixnum end
)
2303 (type function test
)) ; coercion is done by caller
2304 (do ((index start
(+ index incrementer
)))
2305 ((or (= index end
) (= count
0)) sequence
)
2306 (when (not (funcall test
(apply-key key
(aref sequence index
))))
2307 (setf (aref sequence index
) new
)
2310 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2312 (defun effective-find-position-test (test test-not
)
2313 (effective-find-position-test test test-not
))
2314 (defun effective-find-position-key (key)
2315 (effective-find-position-key key
))
2317 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2318 (macrolet (;; shared logic for defining %FIND-POSITION and
2319 ;; %FIND-POSITION-IF in terms of various inlineable cases
2320 ;; of the expression defined in FROB and VECTOR*-FROB
2321 (frobs (&optional bit-frob
)
2322 `(seq-dispatch sequence-arg
2323 (frob sequence-arg from-end
)
2324 (with-array-data ((sequence sequence-arg
:offset-var offset
)
2327 :check-fill-pointer t
)
2328 (multiple-value-bind (f p
)
2329 (macrolet ((frob2 () `(if from-end
2331 (frob sequence nil
))))
2334 ((simple-array character
(*)) (frob2))
2335 ((simple-array base-char
(*)) (frob2))
2337 `((simple-bit-vector
2338 (if (and (typep item
'bit
)
2343 (let ((p (%bit-position item sequence
2344 from-end start end
)))
2348 (vector*-frob sequence
)))))
2350 (vector*-frob sequence
))))
2351 (declare (type (or index null
) p
))
2352 (values f
(and p
(the index
(- p offset
)))))))))
2353 (defun %find-position
(item sequence-arg from-end start end key test
)
2354 (macrolet ((frob (sequence from-end
)
2355 `(%find-position item
,sequence
2356 ,from-end start end key test
))
2357 (vector*-frob
(sequence)
2358 `(%find-position-vector-macro item
,sequence
2359 from-end start end key test
)))
2361 (defun %find-position-if
(predicate sequence-arg from-end start end key
)
2362 (macrolet ((frob (sequence from-end
)
2363 `(%find-position-if predicate
,sequence
2364 ,from-end start end key
))
2365 (vector*-frob
(sequence)
2366 `(%find-position-if-vector-macro predicate
,sequence
2367 from-end start end key
)))
2369 (defun %find-position-if-not
(predicate sequence-arg from-end start end key
)
2370 (macrolet ((frob (sequence from-end
)
2371 `(%find-position-if-not predicate
,sequence
2372 ,from-end start end key
))
2373 (vector*-frob
(sequence)
2374 `(%find-position-if-not-vector-macro predicate
,sequence
2375 from-end start end key
)))
2379 (item sequence
&rest args
&key from-end
(start 0) end key test test-not
)
2380 (declare (truly-dynamic-extent args
))
2381 (seq-dispatch sequence
2382 (nth-value 0 (%find-position
2383 item sequence from-end start end
2384 (effective-find-position-key key
)
2385 (effective-find-position-test test test-not
)))
2386 (nth-value 0 (%find-position
2387 item sequence from-end start end
2388 (effective-find-position-key key
)
2389 (effective-find-position-test test test-not
)))
2390 (apply #'sb
!sequence
:find item sequence args
)))
2392 (item sequence
&rest args
&key from-end
(start 0) end key test test-not
)
2393 (declare (truly-dynamic-extent args
))
2394 (seq-dispatch sequence
2395 (nth-value 1 (%find-position
2396 item sequence from-end start end
2397 (effective-find-position-key key
)
2398 (effective-find-position-test test test-not
)))
2399 (nth-value 1 (%find-position
2400 item sequence from-end start end
2401 (effective-find-position-key key
)
2402 (effective-find-position-test test test-not
)))
2403 (apply #'sb
!sequence
:position item sequence args
)))
2405 (defun find-if (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2406 (declare (truly-dynamic-extent args
))
2407 (seq-dispatch sequence
2408 (nth-value 0 (%find-position-if
2409 (%coerce-callable-to-fun predicate
)
2410 sequence from-end start end
2411 (effective-find-position-key key
)))
2412 (nth-value 0 (%find-position-if
2413 (%coerce-callable-to-fun predicate
)
2414 sequence from-end start end
2415 (effective-find-position-key key
)))
2416 (apply #'sb
!sequence
:find-if predicate sequence args
)))
2418 (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2419 (declare (truly-dynamic-extent args
))
2420 (seq-dispatch sequence
2421 (nth-value 1 (%find-position-if
2422 (%coerce-callable-to-fun predicate
)
2423 sequence from-end start end
2424 (effective-find-position-key key
)))
2425 (nth-value 1 (%find-position-if
2426 (%coerce-callable-to-fun predicate
)
2427 sequence from-end start end
2428 (effective-find-position-key key
)))
2429 (apply #'sb
!sequence
:position-if predicate sequence args
)))
2432 (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2433 (declare (truly-dynamic-extent args
))
2434 (seq-dispatch sequence
2435 (nth-value 0 (%find-position-if-not
2436 (%coerce-callable-to-fun predicate
)
2437 sequence from-end start end
2438 (effective-find-position-key key
)))
2439 (nth-value 0 (%find-position-if-not
2440 (%coerce-callable-to-fun predicate
)
2441 sequence from-end start end
2442 (effective-find-position-key key
)))
2443 (apply #'sb
!sequence
:find-if-not predicate sequence args
)))
2444 (defun position-if-not
2445 (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2446 (declare (truly-dynamic-extent args
))
2447 (seq-dispatch sequence
2448 (nth-value 1 (%find-position-if-not
2449 (%coerce-callable-to-fun predicate
)
2450 sequence from-end start end
2451 (effective-find-position-key key
)))
2452 (nth-value 1 (%find-position-if-not
2453 (%coerce-callable-to-fun predicate
)
2454 sequence from-end start end
2455 (effective-find-position-key key
)))
2456 (apply #'sb
!sequence
:position-if-not predicate sequence args
)))
2458 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2460 (eval-when (:compile-toplevel
:execute
)
2462 (sb!xc
:defmacro vector-count-if
(notp from-end-p predicate sequence
)
2463 (let ((next-index (if from-end-p
'(1- index
) '(1+ index
)))
2464 (pred `(funcall ,predicate
(apply-key key
(aref ,sequence index
)))))
2465 `(let ((%start
,(if from-end-p
'(1- end
) 'start
))
2466 (%end
,(if from-end-p
'(1- start
) 'end
)))
2467 (do ((index %start
,next-index
)
2469 ((= index
(the fixnum %end
)) count
)
2470 (declare (fixnum index count
))
2471 (,(if notp
'unless
'when
) ,pred
2472 (setq count
(1+ count
)))))))
2474 (sb!xc
:defmacro list-count-if
(notp from-end-p predicate sequence
)
2475 (let ((pred `(funcall ,predicate
(apply-key key
(pop sequence
)))))
2476 `(let ((%start
,(if from-end-p
'(- length end
) 'start
))
2477 (%end
,(if from-end-p
'(- length start
) 'end
))
2478 (sequence ,(if from-end-p
'(reverse sequence
) 'sequence
)))
2479 (do ((sequence (nthcdr %start
,sequence
))
2480 (index %start
(1+ index
))
2482 ((or (= index
(the fixnum %end
)) (null sequence
)) count
)
2483 (declare (fixnum index count
))
2484 (,(if notp
'unless
'when
) ,pred
2485 (setq count
(1+ count
)))))))
2490 (define-sequence-traverser count-if
2491 (pred sequence
&rest args
&key from-end start end key
)
2493 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2494 (declare (type fixnum start
)
2495 (truly-dynamic-extent args
))
2496 (let ((pred (%coerce-callable-to-fun pred
)))
2497 (seq-dispatch sequence
2498 (let ((end (or end length
)))
2499 (declare (type index end
))
2501 (list-count-if nil t pred sequence
)
2502 (list-count-if nil nil pred sequence
)))
2503 (let ((end (or end length
)))
2504 (declare (type index end
))
2506 (vector-count-if nil t pred sequence
)
2507 (vector-count-if nil nil pred sequence
)))
2508 (apply #'sb
!sequence
:count-if pred sequence args
))))
2510 (define-sequence-traverser count-if-not
2511 (pred sequence
&rest args
&key from-end start end key
)
2513 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2514 (declare (type fixnum start
)
2515 (truly-dynamic-extent args
))
2516 (let ((pred (%coerce-callable-to-fun pred
)))
2517 (seq-dispatch sequence
2518 (let ((end (or end length
)))
2519 (declare (type index end
))
2521 (list-count-if t t pred sequence
)
2522 (list-count-if t nil pred sequence
)))
2523 (let ((end (or end length
)))
2524 (declare (type index end
))
2526 (vector-count-if t t pred sequence
)
2527 (vector-count-if t nil pred sequence
)))
2528 (apply #'sb
!sequence
:count-if-not pred sequence args
))))
2530 (define-sequence-traverser count
2531 (item sequence
&rest args
&key from-end start end
2532 key
(test #'eql test-p
) (test-not nil test-not-p
))
2534 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2535 which defaults to EQL."
2536 (declare (type fixnum start
)
2537 (truly-dynamic-extent args
))
2538 (when (and test-p test-not-p
)
2539 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2541 (error ":TEST and :TEST-NOT are both present."))
2542 (let ((%test
(if test-not-p
2544 (not (funcall test-not item x
)))
2546 (funcall test item x
)))))
2547 (seq-dispatch sequence
2548 (let ((end (or end length
)))
2549 (declare (type index end
))
2551 (list-count-if nil t %test sequence
)
2552 (list-count-if nil nil %test sequence
)))
2553 (let ((end (or end length
)))
2554 (declare (type index end
))
2556 (vector-count-if nil t %test sequence
)
2557 (vector-count-if nil nil %test sequence
)))
2558 (apply #'sb
!sequence
:count item sequence args
))))
2562 (eval-when (:compile-toplevel
:execute
)
2564 (sb!xc
:defmacro match-vars
(&rest body
)
2565 `(let ((inc (if from-end -
1 1))
2566 (start1 (if from-end
(1- (the fixnum end1
)) start1
))
2567 (start2 (if from-end
(1- (the fixnum end2
)) start2
))
2568 (end1 (if from-end
(1- (the fixnum start1
)) end1
))
2569 (end2 (if from-end
(1- (the fixnum start2
)) end2
)))
2570 (declare (fixnum inc start1 start2 end1 end2
))
2573 (sb!xc
:defmacro matchify-list
((sequence start length end
) &body body
)
2574 (declare (ignore end
)) ;; ### Should END be used below?
2575 `(let ((,sequence
(if from-end
2576 (nthcdr (- (the fixnum
,length
) (the fixnum
,start
) 1)
2577 (reverse (the list
,sequence
)))
2578 (nthcdr ,start
,sequence
))))
2579 (declare (type list
,sequence
))
2584 (eval-when (:compile-toplevel
:execute
)
2586 (sb!xc
:defmacro if-mismatch
(elt1 elt2
)
2587 `(cond ((= (the fixnum index1
) (the fixnum end1
))
2588 (return (if (= (the fixnum index2
) (the fixnum end2
))
2591 (1+ (the fixnum index1
))
2592 (the fixnum index1
)))))
2593 ((= (the fixnum index2
) (the fixnum end2
))
2594 (return (if from-end
(1+ (the fixnum index1
)) index1
)))
2596 (if (funcall test-not
(apply-key key
,elt1
) (apply-key key
,elt2
))
2597 (return (if from-end
(1+ (the fixnum index1
)) index1
))))
2598 (t (if (not (funcall test
(apply-key key
,elt1
)
2599 (apply-key key
,elt2
)))
2600 (return (if from-end
(1+ (the fixnum index1
)) index1
))))))
2602 (sb!xc
:defmacro mumble-mumble-mismatch
()
2603 `(do ((index1 start1
(+ index1
(the fixnum inc
)))
2604 (index2 start2
(+ index2
(the fixnum inc
))))
2606 (declare (fixnum index1 index2
))
2607 (if-mismatch (aref sequence1 index1
) (aref sequence2 index2
))))
2609 (sb!xc
:defmacro mumble-list-mismatch
()
2610 `(do ((index1 start1
(+ index1
(the fixnum inc
)))
2611 (index2 start2
(+ index2
(the fixnum inc
))))
2613 (declare (fixnum index1 index2
))
2614 (if-mismatch (aref sequence1 index1
) (pop sequence2
))))
2616 (sb!xc
:defmacro list-mumble-mismatch
()
2617 `(do ((index1 start1
(+ index1
(the fixnum inc
)))
2618 (index2 start2
(+ index2
(the fixnum inc
))))
2620 (declare (fixnum index1 index2
))
2621 (if-mismatch (pop sequence1
) (aref sequence2 index2
))))
2623 (sb!xc
:defmacro list-list-mismatch
()
2624 `(do ((sequence1 sequence1
)
2625 (sequence2 sequence2
)
2626 (index1 start1
(+ index1
(the fixnum inc
)))
2627 (index2 start2
(+ index2
(the fixnum inc
))))
2629 (declare (fixnum index1 index2
))
2630 (if-mismatch (pop sequence1
) (pop sequence2
))))
2634 (define-sequence-traverser mismatch
2635 (sequence1 sequence2
&rest args
&key from-end test test-not
2636 start1 end1 start2 end2 key
)
2638 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2639 element-wise. If they are of equal length and match in every element, the
2640 result is NIL. Otherwise, the result is a non-negative integer, the index
2641 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2642 if one is shorter than and a matching prefix of the other, the index within
2643 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2644 :FROM-END argument is given, then one plus the index of the rightmost
2645 position in which the sequences differ is returned."
2646 (declare (type fixnum start1 start2
))
2647 (declare (truly-dynamic-extent args
))
2648 (seq-dispatch sequence1
2649 (seq-dispatch sequence2
2650 (let ((end1 (or end1 length1
))
2651 (end2 (or end2 length2
)))
2652 (declare (type index end1 end2
))
2654 (matchify-list (sequence1 start1 length1 end1
)
2655 (matchify-list (sequence2 start2 length2 end2
)
2656 (list-list-mismatch)))))
2657 (let ((end1 (or end1 length1
))
2658 (end2 (or end2 length2
)))
2659 (declare (type index end1 end2
))
2661 (matchify-list (sequence1 start1 length1 end1
)
2662 (list-mumble-mismatch))))
2663 (apply #'sb
!sequence
:mismatch sequence1 sequence2 args
))
2664 (seq-dispatch sequence2
2665 (let ((end1 (or end1 length1
))
2666 (end2 (or end2 length2
)))
2667 (declare (type index end1 end2
))
2669 (matchify-list (sequence2 start2 length2 end2
)
2670 (mumble-list-mismatch))))
2671 (let ((end1 (or end1 length1
))
2672 (end2 (or end2 length2
)))
2673 (declare (type index end1 end2
))
2675 (mumble-mumble-mismatch)))
2676 (apply #'sb
!sequence
:mismatch sequence1 sequence2 args
))
2677 (apply #'sb
!sequence
:mismatch sequence1 sequence2 args
)))
2680 ;;; search comparison functions
2682 (eval-when (:compile-toplevel
:execute
)
2684 ;;; Compare two elements and return if they don't match.
2685 (sb!xc
:defmacro compare-elements
(elt1 elt2
)
2687 (if (funcall test-not
(apply-key key
,elt1
) (apply-key key
,elt2
))
2690 (if (not (funcall test
(apply-key key
,elt1
) (apply-key key
,elt2
)))
2694 (sb!xc
:defmacro search-compare-list-list
(main sub
)
2695 `(do ((main ,main
(cdr main
))
2696 (jndex start1
(1+ jndex
))
2697 (sub (nthcdr start1
,sub
) (cdr sub
)))
2698 ((or (endp main
) (endp sub
) (<= end1 jndex
))
2700 (declare (type (integer 0) jndex
))
2701 (compare-elements (car sub
) (car main
))))
2703 (sb!xc
:defmacro search-compare-list-vector
(main sub
)
2704 `(do ((main ,main
(cdr main
))
2705 (index start1
(1+ index
)))
2706 ((or (endp main
) (= index end1
)) t
)
2707 (compare-elements (aref ,sub index
) (car main
))))
2709 (sb!xc
:defmacro search-compare-vector-list
(main sub index
)
2710 `(do ((sub (nthcdr start1
,sub
) (cdr sub
))
2711 (jndex start1
(1+ jndex
))
2712 (index ,index
(1+ index
)))
2713 ((or (<= end1 jndex
) (endp sub
)) t
)
2714 (declare (type (integer 0) jndex
))
2715 (compare-elements (car sub
) (aref ,main index
))))
2717 (sb!xc
:defmacro search-compare-vector-vector
(main sub index
)
2718 `(do ((index ,index
(1+ index
))
2719 (sub-index start1
(1+ sub-index
)))
2720 ((= sub-index end1
) t
)
2721 (compare-elements (aref ,sub sub-index
) (aref ,main index
))))
2723 (sb!xc
:defmacro search-compare
(main-type main sub index
)
2724 (if (eq main-type
'list
)
2726 (search-compare-list-list ,main
,sub
)
2727 (search-compare-list-vector ,main
,sub
)
2728 ;; KLUDGE: just hack it together so that it works
2729 (return-from search
(apply #'sb
!sequence
:search sequence1 sequence2 args
)))
2731 (search-compare-vector-list ,main
,sub
,index
)
2732 (search-compare-vector-vector ,main
,sub
,index
)
2733 (return-from search
(apply #'sb
!sequence
:search sequence1 sequence2 args
)))))
2739 (eval-when (:compile-toplevel
:execute
)
2741 (sb!xc
:defmacro list-search
(main sub
)
2742 `(do ((main (nthcdr start2
,main
) (cdr main
))
2743 (index2 start2
(1+ index2
))
2744 (terminus (- end2
(the (integer 0) (- end1 start1
))))
2746 ((> index2 terminus
) last-match
)
2747 (declare (type (integer 0) index2
))
2748 (if (search-compare list main
,sub index2
)
2750 (setq last-match index2
)
2753 (sb!xc
:defmacro vector-search
(main sub
)
2754 `(do ((index2 start2
(1+ index2
))
2755 (terminus (- end2
(the (integer 0) (- end1 start1
))))
2757 ((> index2 terminus
) last-match
)
2758 (declare (type (integer 0) index2
))
2759 (if (search-compare vector
,main
,sub index2
)
2761 (setq last-match index2
)
2766 (define-sequence-traverser search
2767 (sequence1 sequence2
&rest args
&key
2768 from-end test test-not start1 end1 start2 end2 key
)
2769 (declare (type fixnum start1 start2
)
2770 (truly-dynamic-extent args
))
2771 (seq-dispatch sequence2
2772 (let ((end1 (or end1 length1
))
2773 (end2 (or end2 length2
)))
2774 (declare (type index end1 end2
))
2775 (list-search sequence2 sequence1
))
2776 (let ((end1 (or end1 length1
))
2777 (end2 (or end2 length2
)))
2778 (declare (type index end1 end2
))
2779 (vector-search sequence2 sequence1
))
2780 (apply #'sb
!sequence
:search sequence1 sequence2 args
)))
2782 ;;; FIXME: this was originally in array.lisp; it might be better to
2783 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2784 ;;; a new early-seq.lisp file.
2785 (defun fill-data-vector (vector dimensions initial-contents
)
2787 (labels ((frob (axis dims contents
)
2789 (setf (aref vector index
) contents
)
2792 (unless (typep contents
'sequence
)
2793 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2794 sequence, but ~W more layer~:P needed."
2796 (- (length dimensions
) axis
)))
2797 (unless (= (length contents
) (car dims
))
2798 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2799 axis ~W is ~W, but ~S is ~W long."
2800 axis
(car dims
) contents
(length contents
)))
2801 (sb!sequence
:dosequence
(content contents
)
2802 (frob (1+ axis
) (cdr dims
) content
))))))
2803 (frob 0 dimensions initial-contents
))))