1.0.12.12: sequence optimizations: SUBSEQ, part 2
[sbcl/simd.git] / src / code / seq.lisp
bloba5173aa1b77039f712916de0e7cdbb66446ec304
1 ;;;; generic SEQUENCEs
2 ;;;;
3 ;;;; KLUDGE: comment from original CMU CL source:
4 ;;;; Be careful when modifying code. A lot of the structure of the
5 ;;;; code is affected by the fact that compiler transforms use the
6 ;;;; lower level support functions. If transforms are written for
7 ;;;; some sequence operation, note how the END argument is handled
8 ;;;; in other operations with transforms.
10 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; more information.
12 ;;;;
13 ;;;; This software is derived from the CMU CL system, which was
14 ;;;; written at Carnegie Mellon University and released into the
15 ;;;; public domain. The software is in the public domain and is
16 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
17 ;;;; files for more information.
19 (in-package "SB!IMPL")
21 ;;;; utilities
23 (eval-when (:compile-toplevel :load-toplevel :execute)
25 (defparameter *sequence-keyword-info*
26 ;; (name default supplied-p adjustment new-type)
27 `((count nil
28 nil
29 (etypecase count
30 (null (1- most-positive-fixnum))
31 (fixnum (max 0 count))
32 (integer (if (minusp count)
34 (1- most-positive-fixnum))))
35 (mod #.sb!xc:most-positive-fixnum))
36 ,@(mapcan (lambda (names)
37 (destructuring-bind (start end length sequence) names
38 (list
39 `(,start
41 nil
42 (if (<= 0 ,start ,length)
43 ,start
44 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
45 index)
46 `(,end
47 nil
48 nil
49 (if (or (null ,end) (<= ,start ,end ,length))
50 ;; Defaulting of NIL is done inside the
51 ;; bodies, for ease of sharing with compiler
52 ;; transforms.
54 ;; FIXME: defend against non-number non-NIL
55 ;; stuff?
56 ,end
57 (sequence-bounding-indices-bad-error ,sequence ,start ,end))
58 (or null index)))))
59 '((start end length sequence)
60 (start1 end1 length1 sequence1)
61 (start2 end2 length2 sequence2)))
62 (key nil
63 nil
64 (and key (%coerce-callable-to-fun key))
65 (or null function))
66 (test #'eql
67 nil
68 (%coerce-callable-to-fun test)
69 function)
70 (test-not nil
71 nil
72 (and test-not (%coerce-callable-to-fun test-not))
73 (or null function))
76 (sb!xc:defmacro define-sequence-traverser (name args &body body)
77 (multiple-value-bind (body declarations docstring)
78 (parse-body body :doc-string-allowed t)
79 (collect ((new-args) (new-declarations) (adjustments))
80 (dolist (arg args)
81 (case arg
82 ;; FIXME: make this robust. And clean.
83 ((sequence)
84 (new-args arg)
85 (adjustments '(length (length sequence)))
86 (new-declarations '(type index length)))
87 ((sequence1)
88 (new-args arg)
89 (adjustments '(length1 (length sequence1)))
90 (new-declarations '(type index length1)))
91 ((sequence2)
92 (new-args arg)
93 (adjustments '(length2 (length sequence2)))
94 (new-declarations '(type index length2)))
95 ((function predicate)
96 (new-args arg)
97 (adjustments `(,arg (%coerce-callable-to-fun ,arg))))
98 (t (let ((info (cdr (assoc arg *sequence-keyword-info*))))
99 (cond (info
100 (destructuring-bind (default supplied-p adjuster type) info
101 (new-args `(,arg ,default ,@(when supplied-p (list supplied-p))))
102 (adjustments `(,arg ,adjuster))
103 (new-declarations `(type ,type ,arg))))
104 (t (new-args arg)))))))
105 `(defun ,name ,(new-args)
106 ,@(when docstring (list docstring))
107 ,@declarations
108 (let* (,@(adjustments))
109 (declare ,@(new-declarations))
110 ,@body)))))
112 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
114 ;;; FIXME: It might be worth making three cases here, LIST,
115 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
116 ;;; It tends to make code run faster but be bigger; some benchmarking
117 ;;; is needed to decide.
118 (sb!xc:defmacro seq-dispatch
119 (sequence list-form array-form &optional other-form)
120 `(if (listp ,sequence)
121 (let ((,sequence (truly-the list ,sequence)))
122 (declare (ignorable ,sequence))
123 ,list-form)
124 ,@(if other-form
125 `((if (arrayp ,sequence)
126 (let ((,sequence (truly-the vector ,sequence)))
127 (declare (ignorable ,sequence))
128 ,array-form)
129 ,other-form))
130 `((let ((,sequence (truly-the vector ,sequence)))
131 (declare (ignorable ,sequence))
132 ,array-form)))))
134 (sb!xc:defmacro %make-sequence-like (sequence length)
135 #!+sb-doc
136 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
137 `(seq-dispatch ,sequence
138 (make-list ,length)
139 (make-array ,length :element-type (array-element-type ,sequence))
140 (sb!sequence:make-sequence-like ,sequence ,length)))
142 (sb!xc:defmacro bad-sequence-type-error (type-spec)
143 `(error 'simple-type-error
144 :datum ,type-spec
145 :expected-type '(satisfies is-a-valid-sequence-type-specifier-p)
146 :format-control "~S is a bad type specifier for sequences."
147 :format-arguments (list ,type-spec)))
149 (sb!xc:defmacro sequence-type-length-mismatch-error (type length)
150 `(error 'simple-type-error
151 :datum ,length
152 :expected-type (cond ((array-type-p ,type)
153 `(eql ,(car (array-type-dimensions ,type))))
154 ((type= ,type (specifier-type 'null))
155 '(eql 0))
156 ((cons-type-p ,type)
157 '(integer 1))
158 (t (bug "weird type in S-T-L-M-ERROR")))
159 ;; FIXME: this format control causes ugly printing. There's
160 ;; probably some ~<~@:_~> incantation that would make it
161 ;; nicer. -- CSR, 2002-10-18
162 :format-control "The length requested (~S) does not match the type restriction in ~S."
163 :format-arguments (list ,length (type-specifier ,type))))
165 (sb!xc:defmacro sequence-type-too-hairy (type-spec)
166 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
167 ;; words that give some but not total support to this position in
168 ;; ANSI. Essentially, we are justified in throwing this on
169 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
170 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
172 ;; On the other hand, I'm not sure it deserves to be a type-error,
173 ;; either. -- bem, 2005-08-10
174 `(error 'simple-program-error
175 :format-control "~S is too hairy for sequence functions."
176 :format-arguments (list ,type-spec)))
177 ) ; EVAL-WHEN
179 (defun is-a-valid-sequence-type-specifier-p (type)
180 (let ((type (specifier-type type)))
181 (or (csubtypep type (specifier-type 'list))
182 (csubtypep type (specifier-type 'vector)))))
184 ;;; It's possible with some sequence operations to declare the length
185 ;;; of a result vector, and to be safe, we really ought to verify that
186 ;;; the actual result has the declared length.
187 (defun vector-of-checked-length-given-length (vector declared-length)
188 (declare (type vector vector))
189 (declare (type index declared-length))
190 (let ((actual-length (length vector)))
191 (unless (= actual-length declared-length)
192 (error 'simple-type-error
193 :datum vector
194 :expected-type `(vector ,declared-length)
195 :format-control
196 "Vector length (~W) doesn't match declared length (~W)."
197 :format-arguments (list actual-length declared-length))))
198 vector)
199 (defun sequence-of-checked-length-given-type (sequence result-type)
200 (let ((ctype (specifier-type result-type)))
201 (if (not (array-type-p ctype))
202 sequence
203 (let ((declared-length (first (array-type-dimensions ctype))))
204 (if (eq declared-length '*)
205 sequence
206 (vector-of-checked-length-given-length sequence
207 declared-length))))))
209 (declaim (ftype (function (sequence index) nil) signal-index-too-large-error))
210 (defun signal-index-too-large-error (sequence index)
211 (let* ((length (length sequence))
212 (max-index (and (plusp length)
213 (1- length))))
214 (error 'index-too-large-error
215 :datum index
216 :expected-type (if max-index
217 `(integer 0 ,max-index)
218 ;; This seems silly, is there something better?
219 '(integer 0 (0))))))
221 (defun sequence-bounding-indices-bad-error (sequence start end)
222 (let ((size (length sequence)))
223 (error 'bounding-indices-bad-error
224 :datum (cons start end)
225 :expected-type `(cons (integer 0 ,size)
226 (integer ,start ,size))
227 :object sequence)))
229 (defun array-bounding-indices-bad-error (array start end)
230 (let ((size (array-total-size array)))
231 (error 'bounding-indices-bad-error
232 :datum (cons start end)
233 :expected-type `(cons (integer 0 ,size)
234 (integer ,start ,size))
235 :object array)))
237 (defun elt (sequence index)
238 #!+sb-doc "Return the element of SEQUENCE specified by INDEX."
239 (seq-dispatch sequence
240 (do ((count index (1- count))
241 (list sequence (cdr list)))
242 ((= count 0)
243 (if (endp list)
244 (signal-index-too-large-error sequence index)
245 (car list)))
246 (declare (type (integer 0) count)))
247 (progn
248 (when (>= index (length sequence))
249 (signal-index-too-large-error sequence index))
250 (aref sequence index))
251 (sb!sequence:elt sequence index)))
253 (defun %setelt (sequence index newval)
254 #!+sb-doc "Store NEWVAL as the component of SEQUENCE specified by INDEX."
255 (seq-dispatch sequence
256 (do ((count index (1- count))
257 (seq sequence))
258 ((= count 0) (rplaca seq newval) newval)
259 (declare (fixnum count))
260 (if (atom (cdr seq))
261 (signal-index-too-large-error sequence index)
262 (setq seq (cdr seq))))
263 (progn
264 (when (>= index (length sequence))
265 (signal-index-too-large-error sequence index))
266 (setf (aref sequence index) newval))
267 (setf (sb!sequence:elt sequence index) newval)))
269 (defun length (sequence)
270 #!+sb-doc "Return an integer that is the length of SEQUENCE."
271 (seq-dispatch sequence
272 (length sequence)
273 (length sequence)
274 (sb!sequence:length sequence)))
276 (defun make-sequence (type length &key (initial-element nil iep))
277 #!+sb-doc
278 "Return a sequence of the given TYPE and LENGTH, with elements initialized
279 to INITIAL-ELEMENT."
280 (declare (fixnum length))
281 (let* ((adjusted-type
282 (typecase type
283 (atom (cond
284 ((eq type 'string) '(vector character))
285 ((eq type 'simple-string) '(simple-array character (*)))
286 (t type)))
287 (cons (cond
288 ((eq (car type) 'string) `(vector character ,@(cdr type)))
289 ((eq (car type) 'simple-string)
290 `(simple-array character ,(if (cdr type)
291 (cdr type)
292 '(*))))
293 (t type)))
294 (t type)))
295 (type (specifier-type adjusted-type)))
296 (cond ((csubtypep type (specifier-type 'list))
297 (cond
298 ((type= type (specifier-type 'list))
299 (make-list length :initial-element initial-element))
300 ((eq type *empty-type*)
301 (bad-sequence-type-error nil))
302 ((type= type (specifier-type 'null))
303 (if (= length 0)
304 'nil
305 (sequence-type-length-mismatch-error type length)))
306 ((cons-type-p type)
307 (multiple-value-bind (min exactp)
308 (sb!kernel::cons-type-length-info type)
309 (if exactp
310 (unless (= length min)
311 (sequence-type-length-mismatch-error type length))
312 (unless (>= length min)
313 (sequence-type-length-mismatch-error type length)))
314 (make-list length :initial-element initial-element)))
315 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
316 ;; which may seem strange and non-ideal, but then I'd say
317 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
318 (t (sequence-type-too-hairy (type-specifier type)))))
319 ((csubtypep type (specifier-type 'vector))
320 (cond
321 (;; is it immediately obvious what the result type is?
322 (typep type 'array-type)
323 (progn
324 (aver (= (length (array-type-dimensions type)) 1))
325 (let* ((etype (type-specifier
326 (array-type-specialized-element-type type)))
327 (etype (if (eq etype '*) t etype))
328 (type-length (car (array-type-dimensions type))))
329 (unless (or (eq type-length '*)
330 (= type-length length))
331 (sequence-type-length-mismatch-error type length))
332 ;; FIXME: These calls to MAKE-ARRAY can't be
333 ;; open-coded, as the :ELEMENT-TYPE argument isn't
334 ;; constant. Probably we ought to write a
335 ;; DEFTRANSFORM for MAKE-SEQUENCE. -- CSR,
336 ;; 2002-07-22
337 (if iep
338 (make-array length :element-type etype
339 :initial-element initial-element)
340 (make-array length :element-type etype)))))
341 (t (sequence-type-too-hairy (type-specifier type)))))
342 ((and (csubtypep type (specifier-type 'sequence))
343 (find-class adjusted-type nil))
344 (let* ((class (find-class adjusted-type nil)))
345 (unless (sb!mop:class-finalized-p class)
346 (sb!mop:finalize-inheritance class))
347 (if iep
348 (sb!sequence:make-sequence-like
349 (sb!mop:class-prototype class) length
350 :initial-element initial-element)
351 (sb!sequence:make-sequence-like
352 (sb!mop:class-prototype class) length))))
353 (t (bad-sequence-type-error (type-specifier type))))))
355 ;;;; SUBSEQ
356 ;;;;
357 ;;;; The support routines for SUBSEQ are used by compiler transforms,
358 ;;;; so we worry about dealing with END being supplied or defaulting
359 ;;;; to NIL at this level.
361 (defun string-subseq* (sequence start end)
362 (with-array-data ((data sequence)
363 (start start)
364 (end end)
365 :force-inline t
366 :check-fill-pointer t)
367 (declare (optimize (speed 3) (safety 0)))
368 (string-dispatch ((simple-array character (*))
369 (simple-array base-char (*))
370 (vector nil))
371 data
372 (subseq data start end))))
374 (defun vector-subseq* (sequence start end)
375 (declare (type vector sequence))
376 (declare (type index start)
377 (type (or null index) end))
378 (with-array-data ((data sequence)
379 (start start)
380 (end end)
381 :check-fill-pointer t
382 :force-inline t)
383 (let ((copy (%make-sequence-like sequence (- end start))))
384 (declare (optimize (speed 3) (safety 0)))
385 (do ((old-index start (1+ old-index))
386 (new-index 0 (1+ new-index)))
387 ((= old-index end) copy)
388 (declare (index old-index new-index))
389 (setf (aref copy new-index)
390 (aref data old-index))))))
392 (defun list-subseq* (sequence start end)
393 (declare (type list sequence)
394 (type unsigned-byte start)
395 (type (or null unsigned-byte) end))
396 (flet ((oops ()
397 (sequence-bounding-indices-bad-error sequence start end)))
398 (let ((pointer sequence))
399 (unless (zerop start)
400 ;; If START > 0 the list cannot be empty. So CDR down to
401 ;; it START-1 times, check that we still have something, then
402 ;; CDR the final time.
404 ;; If START was zero, the list may be empty if END is NIL or
405 ;; also zero.
406 (when (> start 1)
407 (setf pointer (nthcdr (1- start) pointer)))
408 (if pointer
409 (pop pointer)
410 (oops)))
411 (if end
412 (let ((n (- end start)))
413 (declare (integer n))
414 (when (minusp n)
415 (oops))
416 (when (plusp n)
417 (let* ((head (list nil))
418 (tail head))
419 (macrolet ((pop-one ()
420 `(let ((tmp (list (pop pointer))))
421 (setf (cdr tail) tmp
422 tail tmp))))
423 ;; Bignum case
424 (loop until (fixnump n)
425 do (pop-one)
426 (decf n))
427 ;; Fixnum case, but leave last element, so we should
428 ;; still have something left in the sequence.
429 (let ((m (1- n)))
430 (declare (fixnum m))
431 (loop repeat m
432 do (pop-one)))
433 (unless pointer
434 (oops))
435 ;; OK, pop the last one.
436 (pop-one)
437 (cdr head)))))
438 (loop while pointer
439 collect (pop pointer))))))
441 (defun subseq (sequence start &optional end)
442 #!+sb-doc
443 "Return a copy of a subsequence of SEQUENCE starting with element number
444 START and continuing to the end of SEQUENCE or the optional END."
445 (seq-dispatch sequence
446 (list-subseq* sequence start end)
447 (vector-subseq* sequence start end)
448 (sb!sequence:subseq sequence start end)))
450 ;;;; COPY-SEQ
452 (eval-when (:compile-toplevel :execute)
454 (sb!xc:defmacro vector-copy-seq (sequence)
455 `(let ((length (length (the vector ,sequence))))
456 (declare (fixnum length))
457 (do ((index 0 (1+ index))
458 (copy (%make-sequence-like ,sequence length)))
459 ((= index length) copy)
460 (declare (fixnum index))
461 (setf (aref copy index) (aref ,sequence index)))))
463 (sb!xc:defmacro list-copy-seq (list)
464 `(if (atom ,list) '()
465 (let ((result (cons (car ,list) '()) ))
466 (do ((x (cdr ,list) (cdr x))
467 (splice result
468 (cdr (rplacd splice (cons (car x) '() ))) ))
469 ((atom x) (unless (null x)
470 (rplacd splice x))
471 result)))))
473 ) ; EVAL-WHEN
475 (defun copy-seq (sequence)
476 #!+sb-doc "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
477 (seq-dispatch sequence
478 (list-copy-seq* sequence)
479 (vector-copy-seq* sequence)
480 (sb!sequence:copy-seq sequence)))
482 ;;; internal frobs
484 (defun list-copy-seq* (sequence)
485 (list-copy-seq sequence))
487 (defun vector-copy-seq* (sequence)
488 (declare (type vector sequence))
489 (vector-copy-seq sequence))
491 ;;;; FILL
493 (eval-when (:compile-toplevel :execute)
495 (sb!xc:defmacro vector-fill (sequence item start end)
496 `(do ((index ,start (1+ index)))
497 ((= index (the fixnum ,end)) ,sequence)
498 (declare (fixnum index))
499 (setf (aref ,sequence index) ,item)))
501 (sb!xc:defmacro list-fill (sequence item start end)
502 `(do ((current (nthcdr ,start ,sequence) (cdr current))
503 (index ,start (1+ index)))
504 ((or (atom current) (and end (= index (the fixnum ,end))))
505 sequence)
506 (declare (fixnum index))
507 (rplaca current ,item)))
509 ) ; EVAL-WHEN
511 ;;; The support routines for FILL are used by compiler transforms, so we
512 ;;; worry about dealing with END being supplied or defaulting to NIL
513 ;;; at this level.
515 (defun list-fill* (sequence item start end)
516 (declare (list sequence))
517 (list-fill sequence item start end))
519 (defun vector-fill* (sequence item start end)
520 (declare (vector sequence))
521 (when (null end) (setq end (length sequence)))
522 (vector-fill sequence item start end))
524 (define-sequence-traverser fill (sequence item &rest args &key start end)
525 #!+sb-doc "Replace the specified elements of SEQUENCE with ITEM."
526 (seq-dispatch sequence
527 (list-fill* sequence item start end)
528 (vector-fill* sequence item start end)
529 (apply #'sb!sequence:fill sequence item args)))
531 ;;;; REPLACE
533 (eval-when (:compile-toplevel :execute)
535 ;;; If we are copying around in the same vector, be careful not to copy the
536 ;;; same elements over repeatedly. We do this by copying backwards.
537 (sb!xc:defmacro mumble-replace-from-mumble ()
538 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
539 (let ((nelts (min (- target-end target-start)
540 (- source-end source-start))))
541 (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
542 (1- target-index))
543 (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
544 (1- source-index)))
545 ((= target-index (the fixnum (1- target-start))) target-sequence)
546 (declare (fixnum target-index source-index))
547 ;; disable bounds checking
548 (declare (optimize (safety 0)))
549 (setf (aref target-sequence target-index)
550 (aref source-sequence source-index))))
551 (do ((target-index target-start (1+ target-index))
552 (source-index source-start (1+ source-index)))
553 ((or (= target-index (the fixnum target-end))
554 (= source-index (the fixnum source-end)))
555 target-sequence)
556 (declare (fixnum target-index source-index))
557 ;; disable bounds checking
558 (declare (optimize (safety 0)))
559 (setf (aref target-sequence target-index)
560 (aref source-sequence source-index)))))
562 (sb!xc:defmacro list-replace-from-list ()
563 `(if (and (eq target-sequence source-sequence) (> target-start source-start))
564 (let ((new-elts (subseq source-sequence source-start
565 (+ (the fixnum source-start)
566 (the fixnum
567 (min (- (the fixnum target-end)
568 (the fixnum target-start))
569 (- (the fixnum source-end)
570 (the fixnum source-start))))))))
571 (do ((n new-elts (cdr n))
572 (o (nthcdr target-start target-sequence) (cdr o)))
573 ((null n) target-sequence)
574 (rplaca o (car n))))
575 (do ((target-index target-start (1+ target-index))
576 (source-index source-start (1+ source-index))
577 (target-sequence-ref (nthcdr target-start target-sequence)
578 (cdr target-sequence-ref))
579 (source-sequence-ref (nthcdr source-start source-sequence)
580 (cdr source-sequence-ref)))
581 ((or (= target-index (the fixnum target-end))
582 (= source-index (the fixnum source-end))
583 (null target-sequence-ref) (null source-sequence-ref))
584 target-sequence)
585 (declare (fixnum target-index source-index))
586 (rplaca target-sequence-ref (car source-sequence-ref)))))
588 (sb!xc:defmacro list-replace-from-mumble ()
589 `(do ((target-index target-start (1+ target-index))
590 (source-index source-start (1+ source-index))
591 (target-sequence-ref (nthcdr target-start target-sequence)
592 (cdr target-sequence-ref)))
593 ((or (= target-index (the fixnum target-end))
594 (= source-index (the fixnum source-end))
595 (null target-sequence-ref))
596 target-sequence)
597 (declare (fixnum source-index target-index))
598 (rplaca target-sequence-ref (aref source-sequence source-index))))
600 (sb!xc:defmacro mumble-replace-from-list ()
601 `(do ((target-index target-start (1+ target-index))
602 (source-index source-start (1+ source-index))
603 (source-sequence (nthcdr source-start source-sequence)
604 (cdr source-sequence)))
605 ((or (= target-index (the fixnum target-end))
606 (= source-index (the fixnum source-end))
607 (null source-sequence))
608 target-sequence)
609 (declare (fixnum target-index source-index))
610 (setf (aref target-sequence target-index) (car source-sequence))))
612 ) ; EVAL-WHEN
614 ;;;; The support routines for REPLACE are used by compiler transforms, so we
615 ;;;; worry about dealing with END being supplied or defaulting to NIL
616 ;;;; at this level.
618 (defun list-replace-from-list* (target-sequence source-sequence target-start
619 target-end source-start source-end)
620 (when (null target-end) (setq target-end (length target-sequence)))
621 (when (null source-end) (setq source-end (length source-sequence)))
622 (list-replace-from-list))
624 (defun list-replace-from-vector* (target-sequence source-sequence target-start
625 target-end source-start source-end)
626 (when (null target-end) (setq target-end (length target-sequence)))
627 (when (null source-end) (setq source-end (length source-sequence)))
628 (list-replace-from-mumble))
630 (defun vector-replace-from-list* (target-sequence source-sequence target-start
631 target-end source-start source-end)
632 (when (null target-end) (setq target-end (length target-sequence)))
633 (when (null source-end) (setq source-end (length source-sequence)))
634 (mumble-replace-from-list))
636 (defun vector-replace-from-vector* (target-sequence source-sequence
637 target-start target-end source-start
638 source-end)
639 (when (null target-end) (setq target-end (length target-sequence)))
640 (when (null source-end) (setq source-end (length source-sequence)))
641 (mumble-replace-from-mumble))
643 #!+sb-unicode
644 (defun simple-character-string-replace-from-simple-character-string*
645 (target-sequence source-sequence
646 target-start target-end source-start source-end)
647 (declare (type (simple-array character (*)) target-sequence source-sequence))
648 (when (null target-end) (setq target-end (length target-sequence)))
649 (when (null source-end) (setq source-end (length source-sequence)))
650 (mumble-replace-from-mumble))
652 (define-sequence-traverser replace
653 (sequence1 sequence2 &rest args &key start1 end1 start2 end2)
654 #!+sb-doc
655 "The target sequence is destructively modified by copying successive
656 elements into it from the source sequence."
657 (declare (dynamic-extent args))
658 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
659 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
660 ;; these things here so that legacy code gets the names it's
661 ;; expecting. We could use &AUX instead :-/.
662 (target-sequence sequence1)
663 (source-sequence sequence2)
664 (target-start start1)
665 (source-start start2)
666 (target-end (or end1 length1))
667 (source-end (or end2 length2)))
668 (seq-dispatch target-sequence
669 (seq-dispatch source-sequence
670 (list-replace-from-list)
671 (list-replace-from-mumble)
672 (apply #'sb!sequence:replace sequence1 sequence2 args))
673 (seq-dispatch source-sequence
674 (mumble-replace-from-list)
675 (mumble-replace-from-mumble)
676 (apply #'sb!sequence:replace sequence1 sequence2 args))
677 (apply #'sb!sequence:replace sequence1 sequence2 args))))
679 ;;;; REVERSE
681 (eval-when (:compile-toplevel :execute)
683 (sb!xc:defmacro vector-reverse (sequence)
684 `(let ((length (length ,sequence)))
685 (declare (fixnum length))
686 (do ((forward-index 0 (1+ forward-index))
687 (backward-index (1- length) (1- backward-index))
688 (new-sequence (%make-sequence-like sequence length)))
689 ((= forward-index length) new-sequence)
690 (declare (fixnum forward-index backward-index))
691 (setf (aref new-sequence forward-index)
692 (aref ,sequence backward-index)))))
694 (sb!xc:defmacro list-reverse-macro (sequence)
695 `(do ((new-list ()))
696 ((endp ,sequence) new-list)
697 (push (pop ,sequence) new-list)))
699 ) ; EVAL-WHEN
701 (defun reverse (sequence)
702 #!+sb-doc
703 "Return a new sequence containing the same elements but in reverse order."
704 (seq-dispatch sequence
705 (list-reverse* sequence)
706 (vector-reverse* sequence)
707 (sb!sequence:reverse sequence)))
709 ;;; internal frobs
711 (defun list-reverse* (sequence)
712 (list-reverse-macro sequence))
714 (defun vector-reverse* (sequence)
715 (vector-reverse sequence))
717 ;;;; NREVERSE
719 (eval-when (:compile-toplevel :execute)
721 (sb!xc:defmacro vector-nreverse (sequence)
722 `(let ((length (length (the vector ,sequence))))
723 (when (>= length 2)
724 (do ((left-index 0 (1+ left-index))
725 (right-index (1- length) (1- right-index)))
726 ((<= right-index left-index))
727 (declare (type index left-index right-index))
728 (rotatef (aref ,sequence left-index)
729 (aref ,sequence right-index))))
730 ,sequence))
732 (sb!xc:defmacro list-nreverse-macro (list)
733 `(do ((1st (cdr ,list) (if (endp 1st) 1st (cdr 1st)))
734 (2nd ,list 1st)
735 (3rd '() 2nd))
736 ((atom 2nd) 3rd)
737 (rplacd 2nd 3rd)))
739 ) ; EVAL-WHEN
741 (defun list-nreverse* (sequence)
742 (list-nreverse-macro sequence))
744 (defun vector-nreverse* (sequence)
745 (vector-nreverse sequence))
747 (defun nreverse (sequence)
748 #!+sb-doc
749 "Return a sequence of the same elements in reverse order; the argument
750 is destroyed."
751 (seq-dispatch sequence
752 (list-nreverse* sequence)
753 (vector-nreverse* sequence)
754 (sb!sequence:nreverse sequence)))
756 ;;;; CONCATENATE
758 (defmacro sb!sequence:dosequence ((e sequence &optional return) &body body)
759 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
760 (let ((s sequence)
761 (sequence (gensym "SEQUENCE")))
762 `(block nil
763 (let ((,sequence ,s))
764 (seq-dispatch ,sequence
765 (dolist (,e ,sequence ,return) ,@body)
766 (dovector (,e ,sequence ,return) ,@body)
767 (multiple-value-bind (state limit from-end step endp elt)
768 (sb!sequence:make-sequence-iterator ,sequence)
769 (do ((state state (funcall step ,sequence state from-end)))
770 ((funcall endp ,sequence state limit from-end)
771 (let ((,e nil))
772 ,@(filter-dolist-declarations decls)
774 ,return))
775 (let ((,e (funcall elt ,sequence state)))
776 ,@decls
777 (tagbody
778 ,@forms))))))))))
780 (eval-when (:compile-toplevel :execute)
782 (sb!xc:defmacro concatenate-to-list (sequences)
783 `(let ((result (list nil)))
784 (do ((sequences ,sequences (cdr sequences))
785 (splice result))
786 ((null sequences) (cdr result))
787 (let ((sequence (car sequences)))
788 (sb!sequence:dosequence (e sequence)
789 (setq splice (cdr (rplacd splice (list e)))))))))
791 (sb!xc:defmacro concatenate-to-mumble (output-type-spec sequences)
792 `(do ((seqs ,sequences (cdr seqs))
793 (total-length 0)
794 (lengths ()))
795 ((null seqs)
796 (do ((sequences ,sequences (cdr sequences))
797 (lengths lengths (cdr lengths))
798 (index 0)
799 (result (make-sequence ,output-type-spec total-length)))
800 ((= index total-length) result)
801 (declare (fixnum index))
802 (let ((sequence (car sequences)))
803 (sb!sequence:dosequence (e sequence)
804 (setf (aref result index) e)
805 (incf index)))))
806 (let ((length (length (car seqs))))
807 (declare (fixnum length))
808 (setq lengths (nconc lengths (list length)))
809 (setq total-length (+ total-length length)))))
811 ) ; EVAL-WHEN
813 (defun concatenate (output-type-spec &rest sequences)
814 #!+sb-doc
815 "Return a new sequence of all the argument sequences concatenated together
816 which shares no structure with the original argument sequences of the
817 specified OUTPUT-TYPE-SPEC."
818 (let ((type (specifier-type output-type-spec)))
819 (cond
820 ((csubtypep type (specifier-type 'list))
821 (cond
822 ((type= type (specifier-type 'list))
823 (apply #'concat-to-list* sequences))
824 ((eq type *empty-type*)
825 (bad-sequence-type-error nil))
826 ((type= type (specifier-type 'null))
827 (if (every (lambda (x) (or (null x)
828 (and (vectorp x) (= (length x) 0))))
829 sequences)
830 'nil
831 (sequence-type-length-mismatch-error
832 type
833 ;; FIXME: circular list issues.
834 (reduce #'+ sequences :key #'length))))
835 ((cons-type-p type)
836 (multiple-value-bind (min exactp)
837 (sb!kernel::cons-type-length-info type)
838 (let ((length (reduce #'+ sequences :key #'length)))
839 (if exactp
840 (unless (= length min)
841 (sequence-type-length-mismatch-error type length))
842 (unless (>= length min)
843 (sequence-type-length-mismatch-error type length)))
844 (apply #'concat-to-list* sequences))))
845 (t (sequence-type-too-hairy (type-specifier type)))))
846 ((csubtypep type (specifier-type 'vector))
847 (apply #'concat-to-simple* output-type-spec sequences))
848 ((and (csubtypep type (specifier-type 'sequence))
849 (find-class output-type-spec nil))
850 (coerce (apply #'concat-to-simple* 'vector sequences) output-type-spec))
852 (bad-sequence-type-error output-type-spec)))))
854 ;;; internal frobs
855 ;;; FIXME: These are weird. They're never called anywhere except in
856 ;;; CONCATENATE. It seems to me that the macros ought to just
857 ;;; be expanded directly in CONCATENATE, or in CONCATENATE-STRING
858 ;;; and CONCATENATE-LIST variants. Failing that, these ought to be local
859 ;;; functions (FLET).
860 (defun concat-to-list* (&rest sequences)
861 (concatenate-to-list sequences))
862 (defun concat-to-simple* (type &rest sequences)
863 (concatenate-to-mumble type sequences))
865 ;;;; MAP and MAP-INTO
867 ;;; helper functions to handle arity-1 subcases of MAP
868 (declaim (ftype (function (function sequence) list) %map-list-arity-1))
869 (declaim (ftype (function (function sequence) simple-vector)
870 %map-simple-vector-arity-1))
871 (defun %map-to-list-arity-1 (fun sequence)
872 (let ((reversed-result nil)
873 (really-fun (%coerce-callable-to-fun fun)))
874 (sb!sequence:dosequence (element sequence)
875 (push (funcall really-fun element)
876 reversed-result))
877 (nreverse reversed-result)))
878 (defun %map-to-simple-vector-arity-1 (fun sequence)
879 (let ((result (make-array (length sequence)))
880 (index 0)
881 (really-fun (%coerce-callable-to-fun fun)))
882 (declare (type index index))
883 (sb!sequence:dosequence (element sequence)
884 (setf (aref result index)
885 (funcall really-fun element))
886 (incf index))
887 result))
888 (defun %map-for-effect-arity-1 (fun sequence)
889 (let ((really-fun (%coerce-callable-to-fun fun)))
890 (sb!sequence:dosequence (element sequence)
891 (funcall really-fun element)))
892 nil)
894 (declaim (maybe-inline %map-for-effect))
895 (defun %map-for-effect (fun sequences)
896 (declare (type function fun) (type list sequences))
897 (let ((%sequences sequences)
898 (%iters (mapcar (lambda (s)
899 (seq-dispatch s
902 (multiple-value-list
903 (sb!sequence:make-sequence-iterator s))))
904 sequences))
905 (%apply-args (make-list (length sequences))))
906 ;; this is almost efficient (except in the general case where we
907 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
908 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
909 (declare (type list %sequences %iters %apply-args))
910 (loop
911 (do ((in-sequences %sequences (cdr in-sequences))
912 (in-iters %iters (cdr in-iters))
913 (in-apply-args %apply-args (cdr in-apply-args)))
914 ((null in-sequences) (apply fun %apply-args))
915 (let ((i (car in-iters)))
916 (declare (type (or list index) i))
917 (cond
918 ((listp (car in-sequences))
919 (if (null i)
920 (return-from %map-for-effect nil)
921 (setf (car in-apply-args) (car i)
922 (car in-iters) (cdr i))))
923 ((typep i 'index)
924 (let ((v (the vector (car in-sequences))))
925 (if (>= i (length v))
926 (return-from %map-for-effect nil)
927 (setf (car in-apply-args) (aref v i)
928 (car in-iters) (1+ i)))))
930 (destructuring-bind (state limit from-end step endp elt &rest ignore)
932 (declare (type function step endp elt)
933 (ignore ignore))
934 (let ((s (car in-sequences)))
935 (if (funcall endp s state limit from-end)
936 (return-from %map-for-effect nil)
937 (progn
938 (setf (car in-apply-args) (funcall elt s state))
939 (setf (caar in-iters) (funcall step s state from-end)))))))))))))
940 (defun %map-to-list (fun sequences)
941 (declare (type function fun)
942 (type list sequences))
943 (let ((result nil))
944 (flet ((f (&rest args)
945 (declare (dynamic-extent args))
946 (push (apply fun args) result)))
947 (declare (dynamic-extent #'f))
948 (%map-for-effect #'f sequences))
949 (nreverse result)))
950 (defun %map-to-vector (output-type-spec fun sequences)
951 (declare (type function fun)
952 (type list sequences))
953 (let ((min-len 0))
954 (flet ((f (&rest args)
955 (declare (dynamic-extent args))
956 (declare (ignore args))
957 (incf min-len)))
958 (declare (dynamic-extent #'f))
959 (%map-for-effect #'f sequences))
960 (let ((result (make-sequence output-type-spec min-len))
961 (i 0))
962 (declare (type (simple-array * (*)) result))
963 (flet ((f (&rest args)
964 (declare (dynamic-extent args))
965 (setf (aref result i) (apply fun args))
966 (incf i)))
967 (declare (dynamic-extent #'f))
968 (%map-for-effect #'f sequences))
969 result)))
970 (defun %map-to-sequence (result-type fun sequences)
971 (declare (type function fun)
972 (type list sequences))
973 (let ((min-len 0))
974 (flet ((f (&rest args)
975 (declare (dynamic-extent args))
976 (declare (ignore args))
977 (incf min-len)))
978 (declare (dynamic-extent #'f))
979 (%map-for-effect #'f sequences))
980 (let ((result (make-sequence result-type min-len)))
981 (multiple-value-bind (state limit from-end step endp elt setelt)
982 (sb!sequence:make-sequence-iterator result)
983 (declare (ignore limit endp elt))
984 (flet ((f (&rest args)
985 (declare (dynamic-extent args))
986 (funcall setelt (apply fun args) result state)
987 (setq state (funcall step result state from-end))))
988 (declare (dynamic-extent #'f))
989 (%map-for-effect #'f sequences)))
990 result)))
992 ;;; %MAP is just MAP without the final just-to-be-sure check that
993 ;;; length of the output sequence matches any length specified
994 ;;; in RESULT-TYPE.
995 (defun %map (result-type function first-sequence &rest more-sequences)
996 (let ((really-fun (%coerce-callable-to-fun function))
997 (type (specifier-type result-type)))
998 ;; Handle one-argument MAP NIL specially, using ETYPECASE to turn
999 ;; it into something which can be DEFTRANSFORMed away. (It's
1000 ;; fairly important to handle this case efficiently, since
1001 ;; quantifiers like SOME are transformed into this case, and since
1002 ;; there's no consing overhead to dwarf our inefficiency.)
1003 (if (and (null more-sequences)
1004 (null result-type))
1005 (%map-for-effect-arity-1 really-fun first-sequence)
1006 ;; Otherwise, use the industrial-strength full-generality
1007 ;; approach, consing O(N-ARGS) temporary storage (which can have
1008 ;; DYNAMIC-EXTENT), then using O(N-ARGS * RESULT-LENGTH) time.
1009 (let ((sequences (cons first-sequence more-sequences)))
1010 (cond
1011 ((eq type *empty-type*) (%map-for-effect really-fun sequences))
1012 ((csubtypep type (specifier-type 'list))
1013 (%map-to-list really-fun sequences))
1014 ((csubtypep type (specifier-type 'vector))
1015 (%map-to-vector result-type really-fun sequences))
1016 ((and (csubtypep type (specifier-type 'sequence))
1017 (find-class result-type nil))
1018 (%map-to-sequence result-type really-fun sequences))
1020 (bad-sequence-type-error result-type)))))))
1022 (defun map (result-type function first-sequence &rest more-sequences)
1023 (apply #'%map
1024 result-type
1025 function
1026 first-sequence
1027 more-sequences))
1029 ;;; KLUDGE: MAP has been rewritten substantially since the fork from
1030 ;;; CMU CL in order to give reasonable performance, but this
1031 ;;; implementation of MAP-INTO still has the same problems as the old
1032 ;;; MAP code. Ideally, MAP-INTO should be rewritten to be efficient in
1033 ;;; the same way that the corresponding cases of MAP have been
1034 ;;; rewritten. Instead of doing it now, though, it's easier to wait
1035 ;;; until we have DYNAMIC-EXTENT, at which time it should become
1036 ;;; extremely easy to define a reasonably efficient MAP-INTO in terms
1037 ;;; of (MAP NIL ..). -- WHN 20000920
1038 (defun map-into (result-sequence function &rest sequences)
1039 (let* ((fp-result
1040 (and (arrayp result-sequence)
1041 (array-has-fill-pointer-p result-sequence)))
1042 (len (apply #'min
1043 (if fp-result
1044 (array-dimension result-sequence 0)
1045 (length result-sequence))
1046 (mapcar #'length sequences))))
1048 (when fp-result
1049 (setf (fill-pointer result-sequence) len))
1051 (let ((really-fun (%coerce-callable-to-fun function)))
1052 (dotimes (index len)
1053 (setf (elt result-sequence index)
1054 (apply really-fun
1055 (mapcar (lambda (seq) (elt seq index))
1056 sequences))))))
1057 result-sequence)
1059 ;;;; quantifiers
1061 ;;; We borrow the logic from (MAP NIL ..) to handle iteration over
1062 ;;; arbitrary sequence arguments, both in the full call case and in
1063 ;;; the open code case.
1064 (macrolet ((defquantifier (name found-test found-result
1065 &key doc (unfound-result (not found-result)))
1066 `(progn
1067 ;; KLUDGE: It would be really nice if we could simply
1068 ;; do something like this
1069 ;; (declaim (inline ,name))
1070 ;; (defun ,name (pred first-seq &rest more-seqs)
1071 ;; ,doc
1072 ;; (flet ((map-me (&rest rest)
1073 ;; (let ((pred-value (apply pred rest)))
1074 ;; (,found-test pred-value
1075 ;; (return-from ,name
1076 ;; ,found-result)))))
1077 ;; (declare (inline map-me))
1078 ;; (apply #'map nil #'map-me first-seq more-seqs)
1079 ;; ,unfound-result))
1080 ;; but Python doesn't seem to be smart enough about
1081 ;; inlining and APPLY to recognize that it can use
1082 ;; the DEFTRANSFORM for MAP in the resulting inline
1083 ;; expansion. I don't have any appetite for deep
1084 ;; compiler hacking right now, so I'll just work
1085 ;; around the apparent problem by using a compiler
1086 ;; macro instead. -- WHN 20000410
1087 (defun ,name (pred first-seq &rest more-seqs)
1088 #!+sb-doc ,doc
1089 (flet ((map-me (&rest rest)
1090 (let ((pred-value (apply pred rest)))
1091 (,found-test pred-value
1092 (return-from ,name
1093 ,found-result)))))
1094 (declare (inline map-me))
1095 (apply #'map nil #'map-me first-seq more-seqs)
1096 ,unfound-result))
1097 ;; KLUDGE: It would be more obviously correct -- but
1098 ;; also significantly messier -- for PRED-VALUE to be
1099 ;; a gensym. However, a private symbol really does
1100 ;; seem to be good enough; and anyway the really
1101 ;; obviously correct solution is to make Python smart
1102 ;; enough that we can use an inline function instead
1103 ;; of a compiler macro (as above). -- WHN 20000410
1105 ;; FIXME: The DEFINE-COMPILER-MACRO here can be
1106 ;; important for performance, and it'd be good to have
1107 ;; it be visible throughout the compilation of all the
1108 ;; target SBCL code. That could be done by defining
1109 ;; SB-XC:DEFINE-COMPILER-MACRO and using it here,
1110 ;; moving this DEFQUANTIFIER stuff (and perhaps other
1111 ;; inline definitions in seq.lisp as well) into a new
1112 ;; seq.lisp, and moving remaining target-only stuff
1113 ;; from the old seq.lisp into target-seq.lisp.
1114 (define-compiler-macro ,name (pred first-seq &rest more-seqs)
1115 (let ((elements (make-gensym-list (1+ (length more-seqs))))
1116 (blockname (gensym "BLOCK")))
1117 (once-only ((pred pred))
1118 `(block ,blockname
1119 (map nil
1120 (lambda (,@elements)
1121 (let ((pred-value (funcall ,pred ,@elements)))
1122 (,',found-test pred-value
1123 (return-from ,blockname
1124 ,',found-result))))
1125 ,first-seq
1126 ,@more-seqs)
1127 ,',unfound-result)))))))
1128 (defquantifier some when pred-value :unfound-result nil :doc
1129 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1130 possibly to those with index 1, and so on. Return the first
1131 non-NIL value encountered, or NIL if the end of any sequence is reached.")
1132 (defquantifier every unless nil :doc
1133 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1134 possibly to those with index 1, and so on. Return NIL as soon
1135 as any invocation of PREDICATE returns NIL, or T if every invocation
1136 is non-NIL.")
1137 (defquantifier notany when nil :doc
1138 "Apply PREDICATE to the 0-indexed elements of the sequences, then
1139 possibly to those with index 1, and so on. Return NIL as soon
1140 as any invocation of PREDICATE returns a non-NIL value, or T if the end
1141 of any sequence is reached.")
1142 (defquantifier notevery unless t :doc
1143 "Apply PREDICATE to 0-indexed elements of the sequences, then
1144 possibly to those with index 1, and so on. Return T as soon
1145 as any invocation of PREDICATE returns NIL, or NIL if every invocation
1146 is non-NIL."))
1148 ;;;; REDUCE
1150 (eval-when (:compile-toplevel :execute)
1152 (sb!xc:defmacro mumble-reduce (function
1153 sequence
1155 start
1157 initial-value
1158 ref)
1159 `(do ((index ,start (1+ index))
1160 (value ,initial-value))
1161 ((>= index ,end) value)
1162 (setq value (funcall ,function value
1163 (apply-key ,key (,ref ,sequence index))))))
1165 (sb!xc:defmacro mumble-reduce-from-end (function
1166 sequence
1168 start
1170 initial-value
1171 ref)
1172 `(do ((index (1- ,end) (1- index))
1173 (value ,initial-value)
1174 (terminus (1- ,start)))
1175 ((<= index terminus) value)
1176 (setq value (funcall ,function
1177 (apply-key ,key (,ref ,sequence index))
1178 value))))
1180 (sb!xc:defmacro list-reduce (function
1181 sequence
1183 start
1185 initial-value
1186 ivp)
1187 `(let ((sequence (nthcdr ,start ,sequence)))
1188 (do ((count (if ,ivp ,start (1+ ,start))
1189 (1+ count))
1190 (sequence (if ,ivp sequence (cdr sequence))
1191 (cdr sequence))
1192 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1193 (funcall ,function value (apply-key ,key (car sequence)))))
1194 ((>= count ,end) value))))
1196 (sb!xc:defmacro list-reduce-from-end (function
1197 sequence
1199 start
1201 initial-value
1202 ivp)
1203 `(let ((sequence (nthcdr (- (length ,sequence) ,end)
1204 (reverse ,sequence))))
1205 (do ((count (if ,ivp ,start (1+ ,start))
1206 (1+ count))
1207 (sequence (if ,ivp sequence (cdr sequence))
1208 (cdr sequence))
1209 (value (if ,ivp ,initial-value (apply-key ,key (car sequence)))
1210 (funcall ,function (apply-key ,key (car sequence)) value)))
1211 ((>= count ,end) value))))
1213 ) ; EVAL-WHEN
1215 (define-sequence-traverser reduce (function sequence &rest args &key key
1216 from-end start end (initial-value nil ivp))
1217 (declare (type index start))
1218 (declare (dynamic-extent args))
1219 (let ((start start)
1220 (end (or end length)))
1221 (declare (type index start end))
1222 (seq-dispatch sequence
1223 (if (= end start)
1224 (if ivp initial-value (funcall function))
1225 (if from-end
1226 (list-reduce-from-end function sequence key start end
1227 initial-value ivp)
1228 (list-reduce function sequence key start end
1229 initial-value ivp)))
1230 (if (= end start)
1231 (if ivp initial-value (funcall function))
1232 (if from-end
1233 (progn
1234 (when (not ivp)
1235 (setq end (1- (the fixnum end)))
1236 (setq initial-value (apply-key key (aref sequence end))))
1237 (mumble-reduce-from-end function sequence key start end
1238 initial-value aref))
1239 (progn
1240 (when (not ivp)
1241 (setq initial-value (apply-key key (aref sequence start)))
1242 (setq start (1+ start)))
1243 (mumble-reduce function sequence key start end
1244 initial-value aref))))
1245 (apply #'sb!sequence:reduce function sequence args))))
1247 ;;;; DELETE
1249 (eval-when (:compile-toplevel :execute)
1251 (sb!xc:defmacro mumble-delete (pred)
1252 `(do ((index start (1+ index))
1253 (jndex start)
1254 (number-zapped 0))
1255 ((or (= index (the fixnum end)) (= number-zapped count))
1256 (do ((index index (1+ index)) ; Copy the rest of the vector.
1257 (jndex jndex (1+ jndex)))
1258 ((= index (the fixnum length))
1259 (shrink-vector sequence jndex))
1260 (declare (fixnum index jndex))
1261 (setf (aref sequence jndex) (aref sequence index))))
1262 (declare (fixnum index jndex number-zapped))
1263 (setf (aref sequence jndex) (aref sequence index))
1264 (if ,pred
1265 (incf number-zapped)
1266 (incf jndex))))
1268 (sb!xc:defmacro mumble-delete-from-end (pred)
1269 `(do ((index (1- (the fixnum end)) (1- index)) ; Find the losers.
1270 (number-zapped 0)
1271 (losers ())
1272 this-element
1273 (terminus (1- start)))
1274 ((or (= index terminus) (= number-zapped count))
1275 (do ((losers losers) ; Delete the losers.
1276 (index start (1+ index))
1277 (jndex start))
1278 ((or (null losers) (= index (the fixnum end)))
1279 (do ((index index (1+ index)) ; Copy the rest of the vector.
1280 (jndex jndex (1+ jndex)))
1281 ((= index (the fixnum length))
1282 (shrink-vector sequence jndex))
1283 (declare (fixnum index jndex))
1284 (setf (aref sequence jndex) (aref sequence index))))
1285 (declare (fixnum index jndex))
1286 (setf (aref sequence jndex) (aref sequence index))
1287 (if (= index (the fixnum (car losers)))
1288 (pop losers)
1289 (incf jndex))))
1290 (declare (fixnum index number-zapped terminus))
1291 (setq this-element (aref sequence index))
1292 (when ,pred
1293 (incf number-zapped)
1294 (push index losers))))
1296 (sb!xc:defmacro normal-mumble-delete ()
1297 `(mumble-delete
1298 (if test-not
1299 (not (funcall test-not item (apply-key key (aref sequence index))))
1300 (funcall test item (apply-key key (aref sequence index))))))
1302 (sb!xc:defmacro normal-mumble-delete-from-end ()
1303 `(mumble-delete-from-end
1304 (if test-not
1305 (not (funcall test-not item (apply-key key this-element)))
1306 (funcall test item (apply-key key this-element)))))
1308 (sb!xc:defmacro list-delete (pred)
1309 `(let ((handle (cons nil sequence)))
1310 (do ((current (nthcdr start sequence) (cdr current))
1311 (previous (nthcdr start handle))
1312 (index start (1+ index))
1313 (number-zapped 0))
1314 ((or (= index (the fixnum end)) (= number-zapped count))
1315 (cdr handle))
1316 (declare (fixnum index number-zapped))
1317 (cond (,pred
1318 (rplacd previous (cdr current))
1319 (incf number-zapped))
1321 (setq previous (cdr previous)))))))
1323 (sb!xc:defmacro list-delete-from-end (pred)
1324 `(let* ((reverse (nreverse (the list sequence)))
1325 (handle (cons nil reverse)))
1326 (do ((current (nthcdr (- (the fixnum length) (the fixnum end)) reverse)
1327 (cdr current))
1328 (previous (nthcdr (- (the fixnum length) (the fixnum end)) handle))
1329 (index start (1+ index))
1330 (number-zapped 0))
1331 ((or (= index (the fixnum end)) (= number-zapped count))
1332 (nreverse (cdr handle)))
1333 (declare (fixnum index number-zapped))
1334 (cond (,pred
1335 (rplacd previous (cdr current))
1336 (incf number-zapped))
1338 (setq previous (cdr previous)))))))
1340 (sb!xc:defmacro normal-list-delete ()
1341 '(list-delete
1342 (if test-not
1343 (not (funcall test-not item (apply-key key (car current))))
1344 (funcall test item (apply-key key (car current))))))
1346 (sb!xc:defmacro normal-list-delete-from-end ()
1347 '(list-delete-from-end
1348 (if test-not
1349 (not (funcall test-not item (apply-key key (car current))))
1350 (funcall test item (apply-key key (car current))))))
1352 ) ; EVAL-WHEN
1354 (define-sequence-traverser delete
1355 (item sequence &rest args &key from-end test test-not start
1356 end count key)
1357 #!+sb-doc
1358 "Return a sequence formed by destructively removing the specified ITEM from
1359 the given SEQUENCE."
1360 (declare (fixnum start))
1361 (declare (dynamic-extent args))
1362 (let ((end (or end length)))
1363 (declare (type index end))
1364 (seq-dispatch sequence
1365 (if from-end
1366 (normal-list-delete-from-end)
1367 (normal-list-delete))
1368 (if from-end
1369 (normal-mumble-delete-from-end)
1370 (normal-mumble-delete))
1371 (apply #'sb!sequence:delete item sequence args))))
1373 (eval-when (:compile-toplevel :execute)
1375 (sb!xc:defmacro if-mumble-delete ()
1376 `(mumble-delete
1377 (funcall predicate (apply-key key (aref sequence index)))))
1379 (sb!xc:defmacro if-mumble-delete-from-end ()
1380 `(mumble-delete-from-end
1381 (funcall predicate (apply-key key this-element))))
1383 (sb!xc:defmacro if-list-delete ()
1384 '(list-delete
1385 (funcall predicate (apply-key key (car current)))))
1387 (sb!xc:defmacro if-list-delete-from-end ()
1388 '(list-delete-from-end
1389 (funcall predicate (apply-key key (car current)))))
1391 ) ; EVAL-WHEN
1393 (define-sequence-traverser delete-if
1394 (predicate sequence &rest args &key from-end start key end count)
1395 #!+sb-doc
1396 "Return a sequence formed by destructively removing the elements satisfying
1397 the specified PREDICATE from the given SEQUENCE."
1398 (declare (fixnum start))
1399 (declare (dynamic-extent args))
1400 (let ((end (or end length)))
1401 (declare (type index end))
1402 (seq-dispatch sequence
1403 (if from-end
1404 (if-list-delete-from-end)
1405 (if-list-delete))
1406 (if from-end
1407 (if-mumble-delete-from-end)
1408 (if-mumble-delete))
1409 (apply #'sb!sequence:delete-if predicate sequence args))))
1411 (eval-when (:compile-toplevel :execute)
1413 (sb!xc:defmacro if-not-mumble-delete ()
1414 `(mumble-delete
1415 (not (funcall predicate (apply-key key (aref sequence index))))))
1417 (sb!xc:defmacro if-not-mumble-delete-from-end ()
1418 `(mumble-delete-from-end
1419 (not (funcall predicate (apply-key key this-element)))))
1421 (sb!xc:defmacro if-not-list-delete ()
1422 '(list-delete
1423 (not (funcall predicate (apply-key key (car current))))))
1425 (sb!xc:defmacro if-not-list-delete-from-end ()
1426 '(list-delete-from-end
1427 (not (funcall predicate (apply-key key (car current))))))
1429 ) ; EVAL-WHEN
1431 (define-sequence-traverser delete-if-not
1432 (predicate sequence &rest args &key from-end start end key count)
1433 #!+sb-doc
1434 "Return a sequence formed by destructively removing the elements not
1435 satisfying the specified PREDICATE from the given SEQUENCE."
1436 (declare (fixnum start))
1437 (declare (dynamic-extent args))
1438 (let ((end (or end length)))
1439 (declare (type index end))
1440 (seq-dispatch sequence
1441 (if from-end
1442 (if-not-list-delete-from-end)
1443 (if-not-list-delete))
1444 (if from-end
1445 (if-not-mumble-delete-from-end)
1446 (if-not-mumble-delete))
1447 (apply #'sb!sequence:delete-if-not predicate sequence args))))
1449 ;;;; REMOVE
1451 (eval-when (:compile-toplevel :execute)
1453 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1454 ;;; satisfies the predicate.
1455 (sb!xc:defmacro mumble-remove-macro (bump left begin finish right pred)
1456 `(do ((index ,begin (,bump index))
1457 (result
1458 (do ((index ,left (,bump index))
1459 (result (%make-sequence-like sequence length)))
1460 ((= index (the fixnum ,begin)) result)
1461 (declare (fixnum index))
1462 (setf (aref result index) (aref sequence index))))
1463 (new-index ,begin)
1464 (number-zapped 0)
1465 (this-element))
1466 ((or (= index (the fixnum ,finish))
1467 (= number-zapped count))
1468 (do ((index index (,bump index))
1469 (new-index new-index (,bump new-index)))
1470 ((= index (the fixnum ,right)) (%shrink-vector result new-index))
1471 (declare (fixnum index new-index))
1472 (setf (aref result new-index) (aref sequence index))))
1473 (declare (fixnum index new-index number-zapped))
1474 (setq this-element (aref sequence index))
1475 (cond (,pred (incf number-zapped))
1476 (t (setf (aref result new-index) this-element)
1477 (setq new-index (,bump new-index))))))
1479 (sb!xc:defmacro mumble-remove (pred)
1480 `(mumble-remove-macro 1+ 0 start end length ,pred))
1482 (sb!xc:defmacro mumble-remove-from-end (pred)
1483 `(let ((sequence (copy-seq sequence)))
1484 (mumble-delete-from-end ,pred)))
1486 (sb!xc:defmacro normal-mumble-remove ()
1487 `(mumble-remove
1488 (if test-not
1489 (not (funcall test-not item (apply-key key this-element)))
1490 (funcall test item (apply-key key this-element)))))
1492 (sb!xc:defmacro normal-mumble-remove-from-end ()
1493 `(mumble-remove-from-end
1494 (if test-not
1495 (not (funcall test-not item (apply-key key this-element)))
1496 (funcall test item (apply-key key this-element)))))
1498 (sb!xc:defmacro if-mumble-remove ()
1499 `(mumble-remove (funcall predicate (apply-key key this-element))))
1501 (sb!xc:defmacro if-mumble-remove-from-end ()
1502 `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
1504 (sb!xc:defmacro if-not-mumble-remove ()
1505 `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
1507 (sb!xc:defmacro if-not-mumble-remove-from-end ()
1508 `(mumble-remove-from-end
1509 (not (funcall predicate (apply-key key this-element)))))
1511 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1512 ;;; the predicate.
1513 (sb!xc:defmacro list-remove-macro (pred reverse?)
1514 `(let* ((sequence ,(if reverse?
1515 '(reverse (the list sequence))
1516 'sequence))
1517 (%start ,(if reverse? '(- length end) 'start))
1518 (%end ,(if reverse? '(- length start) 'end))
1519 (splice (list nil))
1520 (results (do ((index 0 (1+ index))
1521 (before-start splice))
1522 ((= index (the fixnum %start)) before-start)
1523 (declare (fixnum index))
1524 (setq splice
1525 (cdr (rplacd splice (list (pop sequence))))))))
1526 (do ((index %start (1+ index))
1527 (this-element)
1528 (number-zapped 0))
1529 ((or (= index (the fixnum %end)) (= number-zapped count))
1530 (do ((index index (1+ index)))
1531 ((null sequence)
1532 ,(if reverse?
1533 '(nreverse (the list (cdr results)))
1534 '(cdr results)))
1535 (declare (fixnum index))
1536 (setq splice (cdr (rplacd splice (list (pop sequence)))))))
1537 (declare (fixnum index number-zapped))
1538 (setq this-element (pop sequence))
1539 (if ,pred
1540 (setq number-zapped (1+ number-zapped))
1541 (setq splice (cdr (rplacd splice (list this-element))))))))
1543 (sb!xc:defmacro list-remove (pred)
1544 `(list-remove-macro ,pred nil))
1546 (sb!xc:defmacro list-remove-from-end (pred)
1547 `(list-remove-macro ,pred t))
1549 (sb!xc:defmacro normal-list-remove ()
1550 `(list-remove
1551 (if test-not
1552 (not (funcall test-not item (apply-key key this-element)))
1553 (funcall test item (apply-key key this-element)))))
1555 (sb!xc:defmacro normal-list-remove-from-end ()
1556 `(list-remove-from-end
1557 (if test-not
1558 (not (funcall test-not item (apply-key key this-element)))
1559 (funcall test item (apply-key key this-element)))))
1561 (sb!xc:defmacro if-list-remove ()
1562 `(list-remove
1563 (funcall predicate (apply-key key this-element))))
1565 (sb!xc:defmacro if-list-remove-from-end ()
1566 `(list-remove-from-end
1567 (funcall predicate (apply-key key this-element))))
1569 (sb!xc:defmacro if-not-list-remove ()
1570 `(list-remove
1571 (not (funcall predicate (apply-key key this-element)))))
1573 (sb!xc:defmacro if-not-list-remove-from-end ()
1574 `(list-remove-from-end
1575 (not (funcall predicate (apply-key key this-element)))))
1577 ) ; EVAL-WHEN
1579 (define-sequence-traverser remove
1580 (item sequence &rest args &key from-end test test-not start
1581 end count key)
1582 #!+sb-doc
1583 "Return a copy of SEQUENCE with elements satisfying the test (default is
1584 EQL) with ITEM removed."
1585 (declare (fixnum start))
1586 (declare (dynamic-extent args))
1587 (let ((end (or end length)))
1588 (declare (type index end))
1589 (seq-dispatch sequence
1590 (if from-end
1591 (normal-list-remove-from-end)
1592 (normal-list-remove))
1593 (if from-end
1594 (normal-mumble-remove-from-end)
1595 (normal-mumble-remove))
1596 (apply #'sb!sequence:remove item sequence args))))
1598 (define-sequence-traverser remove-if
1599 (predicate sequence &rest args &key from-end start end count key)
1600 #!+sb-doc
1601 "Return a copy of sequence with elements satisfying PREDICATE removed."
1602 (declare (fixnum start))
1603 (declare (dynamic-extent args))
1604 (let ((end (or end length)))
1605 (declare (type index end))
1606 (seq-dispatch sequence
1607 (if from-end
1608 (if-list-remove-from-end)
1609 (if-list-remove))
1610 (if from-end
1611 (if-mumble-remove-from-end)
1612 (if-mumble-remove))
1613 (apply #'sb!sequence:remove-if predicate sequence args))))
1615 (define-sequence-traverser remove-if-not
1616 (predicate sequence &rest args &key from-end start end count key)
1617 #!+sb-doc
1618 "Return a copy of sequence with elements not satisfying PREDICATE removed."
1619 (declare (fixnum start))
1620 (declare (dynamic-extent args))
1621 (let ((end (or end length)))
1622 (declare (type index end))
1623 (seq-dispatch sequence
1624 (if from-end
1625 (if-not-list-remove-from-end)
1626 (if-not-list-remove))
1627 (if from-end
1628 (if-not-mumble-remove-from-end)
1629 (if-not-mumble-remove))
1630 (apply #'sb!sequence:remove-if-not predicate sequence args))))
1632 ;;;; REMOVE-DUPLICATES
1634 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
1635 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
1636 ;;; if we look into the already copied structure (from after :start) and see
1637 ;;; the item. If we check from beginning we check into the rest of the
1638 ;;; original list up to the :end marker (this we have to do by running a
1639 ;;; do loop down the list that far and using our test.
1640 (defun list-remove-duplicates* (list test test-not start end key from-end)
1641 (declare (fixnum start))
1642 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
1643 (splice result)
1644 (current list)
1645 (end (or end (length list)))
1646 (hash (and (> (- end start) 20)
1647 test
1648 (not key)
1649 (not test-not)
1650 (or (eql test #'eql)
1651 (eql test #'eq)
1652 (eql test #'equal)
1653 (eql test #'equalp))
1654 (make-hash-table :test test :size (- end start)))))
1655 (do ((index 0 (1+ index)))
1656 ((= index start))
1657 (declare (fixnum index))
1658 (setq splice (cdr (rplacd splice (list (car current)))))
1659 (setq current (cdr current)))
1660 (if hash
1661 (do ((index start (1+ index)))
1662 ((or (and end (= index (the fixnum end)))
1663 (atom current)))
1664 (declare (fixnum index))
1665 ;; The hash table contains links from values that are
1666 ;; already in result to the cons cell *preceding* theirs
1667 ;; in the list. That is, for each value v in the list,
1668 ;; v and (cadr (gethash v hash)) are equal under TEST.
1669 (let ((prev (gethash (car current) hash)))
1670 (cond
1671 ((not prev)
1672 (setf (gethash (car current) hash) splice)
1673 (setq splice (cdr (rplacd splice (list (car current))))))
1674 ((not from-end)
1675 (let* ((old (cdr prev))
1676 (next (cdr old)))
1677 (if next
1678 (let ((next-val (car next)))
1679 ;; (assert (eq (gethash next-val hash) old))
1680 (setf (cdr prev) next
1681 (gethash next-val hash) prev
1682 (gethash (car current) hash) splice
1683 splice (cdr (rplacd splice (list (car current))))))
1684 (setf (car old) (car current)))))))
1685 (setq current (cdr current)))
1686 (do ((index start (1+ index)))
1687 ((or (and end (= index (the fixnum end)))
1688 (atom current)))
1689 (declare (fixnum index))
1690 (if (or (and from-end
1691 (not (if test-not
1692 (member (apply-key key (car current))
1693 (nthcdr (1+ start) result)
1694 :test-not test-not
1695 :key key)
1696 (member (apply-key key (car current))
1697 (nthcdr (1+ start) result)
1698 :test test
1699 :key key))))
1700 (and (not from-end)
1701 (not (do ((it (apply-key key (car current)))
1702 (l (cdr current) (cdr l))
1703 (i (1+ index) (1+ i)))
1704 ((or (atom l) (and end (= i (the fixnum end))))
1706 (declare (fixnum i))
1707 (if (if test-not
1708 (not (funcall test-not
1710 (apply-key key (car l))))
1711 (funcall test it (apply-key key (car l))))
1712 (return t))))))
1713 (setq splice (cdr (rplacd splice (list (car current))))))
1714 (setq current (cdr current))))
1715 (do ()
1716 ((atom current))
1717 (setq splice (cdr (rplacd splice (list (car current)))))
1718 (setq current (cdr current)))
1719 (cdr result)))
1721 (defun vector-remove-duplicates* (vector test test-not start end key from-end
1722 &optional (length (length vector)))
1723 (declare (vector vector) (fixnum start length))
1724 (when (null end) (setf end (length vector)))
1725 (let ((result (%make-sequence-like vector length))
1726 (index 0)
1727 (jndex start))
1728 (declare (fixnum index jndex))
1729 (do ()
1730 ((= index start))
1731 (setf (aref result index) (aref vector index))
1732 (setq index (1+ index)))
1733 (do ((elt))
1734 ((= index end))
1735 (setq elt (aref vector index))
1736 (unless (or (and from-end
1737 (if test-not
1738 (position (apply-key key elt) result
1739 :start start :end jndex
1740 :test-not test-not :key key)
1741 (position (apply-key key elt) result
1742 :start start :end jndex
1743 :test test :key key)))
1744 (and (not from-end)
1745 (if test-not
1746 (position (apply-key key elt) vector
1747 :start (1+ index) :end end
1748 :test-not test-not :key key)
1749 (position (apply-key key elt) vector
1750 :start (1+ index) :end end
1751 :test test :key key))))
1752 (setf (aref result jndex) elt)
1753 (setq jndex (1+ jndex)))
1754 (setq index (1+ index)))
1755 (do ()
1756 ((= index length))
1757 (setf (aref result jndex) (aref vector index))
1758 (setq index (1+ index))
1759 (setq jndex (1+ jndex)))
1760 (%shrink-vector result jndex)))
1762 (define-sequence-traverser remove-duplicates
1763 (sequence &rest args &key test test-not start end from-end key)
1764 #!+sb-doc
1765 "The elements of SEQUENCE are compared pairwise, and if any two match,
1766 the one occurring earlier is discarded, unless FROM-END is true, in
1767 which case the one later in the sequence is discarded. The resulting
1768 sequence is returned.
1770 The :TEST-NOT argument is deprecated."
1771 (declare (fixnum start))
1772 (declare (dynamic-extent args))
1773 (seq-dispatch sequence
1774 (if sequence
1775 (list-remove-duplicates* sequence test test-not
1776 start end key from-end))
1777 (vector-remove-duplicates* sequence test test-not start end key from-end)
1778 (apply #'sb!sequence:remove-duplicates sequence args)))
1780 ;;;; DELETE-DUPLICATES
1782 (defun list-delete-duplicates* (list test test-not key from-end start end)
1783 (declare (fixnum start))
1784 (let ((handle (cons nil list)))
1785 (do ((current (nthcdr start list) (cdr current))
1786 (previous (nthcdr start handle))
1787 (index start (1+ index)))
1788 ((or (and end (= index (the fixnum end))) (null current))
1789 (cdr handle))
1790 (declare (fixnum index))
1791 (if (do ((x (if from-end
1792 (nthcdr (1+ start) handle)
1793 (cdr current))
1794 (cdr x))
1795 (i (1+ index) (1+ i)))
1796 ((or (null x)
1797 (and (not from-end) end (= i (the fixnum end)))
1798 (eq x current))
1799 nil)
1800 (declare (fixnum i))
1801 (if (if test-not
1802 (not (funcall test-not
1803 (apply-key key (car current))
1804 (apply-key key (car x))))
1805 (funcall test
1806 (apply-key key (car current))
1807 (apply-key key (car x))))
1808 (return t)))
1809 (rplacd previous (cdr current))
1810 (setq previous (cdr previous))))))
1812 (defun vector-delete-duplicates* (vector test test-not key from-end start end
1813 &optional (length (length vector)))
1814 (declare (vector vector) (fixnum start length))
1815 (when (null end) (setf end (length vector)))
1816 (do ((index start (1+ index))
1817 (jndex start))
1818 ((= index end)
1819 (do ((index index (1+ index)) ; copy the rest of the vector
1820 (jndex jndex (1+ jndex)))
1821 ((= index length)
1822 (shrink-vector vector jndex))
1823 (setf (aref vector jndex) (aref vector index))))
1824 (declare (fixnum index jndex))
1825 (setf (aref vector jndex) (aref vector index))
1826 (unless (if test-not
1827 (position (apply-key key (aref vector index)) vector :key key
1828 :start (if from-end start (1+ index))
1829 :end (if from-end jndex end)
1830 :test-not test-not)
1831 (position (apply-key key (aref vector index)) vector :key key
1832 :start (if from-end start (1+ index))
1833 :end (if from-end jndex end)
1834 :test test))
1835 (setq jndex (1+ jndex)))))
1837 (define-sequence-traverser delete-duplicates
1838 (sequence &rest args &key test test-not start end from-end key)
1839 #!+sb-doc
1840 "The elements of SEQUENCE are examined, and if any two match, one is
1841 discarded. The resulting sequence, which may be formed by destroying the
1842 given sequence, is returned.
1844 The :TEST-NOT argument is deprecated."
1845 (declare (dynamic-extent args))
1846 (seq-dispatch sequence
1847 (if sequence
1848 (list-delete-duplicates* sequence test test-not
1849 key from-end start end))
1850 (vector-delete-duplicates* sequence test test-not key from-end start end)
1851 (apply #'sb!sequence:delete-duplicates sequence args)))
1853 ;;;; SUBSTITUTE
1855 (defun list-substitute* (pred new list start end count key test test-not old)
1856 (declare (fixnum start end count))
1857 (let* ((result (list nil))
1859 (splice result)
1860 (list list)) ; Get a local list for a stepper.
1861 (do ((index 0 (1+ index)))
1862 ((= index start))
1863 (declare (fixnum index))
1864 (setq splice (cdr (rplacd splice (list (car list)))))
1865 (setq list (cdr list)))
1866 (do ((index start (1+ index)))
1867 ((or (= index end) (null list) (= count 0)))
1868 (declare (fixnum index))
1869 (setq elt (car list))
1870 (setq splice
1871 (cdr (rplacd splice
1872 (list
1873 (cond
1874 ((case pred
1875 (normal
1876 (if test-not
1877 (not
1878 (funcall test-not old (apply-key key elt)))
1879 (funcall test old (apply-key key elt))))
1880 (if (funcall test (apply-key key elt)))
1881 (if-not (not (funcall test (apply-key key elt)))))
1882 (decf count)
1883 new)
1884 (t elt))))))
1885 (setq list (cdr list)))
1886 (do ()
1887 ((null list))
1888 (setq splice (cdr (rplacd splice (list (car list)))))
1889 (setq list (cdr list)))
1890 (cdr result)))
1892 ;;; Replace old with new in sequence moving from left to right by incrementer
1893 ;;; on each pass through the loop. Called by all three substitute functions.
1894 (defun vector-substitute* (pred new sequence incrementer left right length
1895 start end count key test test-not old)
1896 (declare (fixnum start count end incrementer right))
1897 (let ((result (%make-sequence-like sequence length))
1898 (index left))
1899 (declare (fixnum index))
1900 (do ()
1901 ((= index start))
1902 (setf (aref result index) (aref sequence index))
1903 (setq index (+ index incrementer)))
1904 (do ((elt))
1905 ((or (= index end) (= count 0)))
1906 (setq elt (aref sequence index))
1907 (setf (aref result index)
1908 (cond ((case pred
1909 (normal
1910 (if test-not
1911 (not (funcall test-not old (apply-key key elt)))
1912 (funcall test old (apply-key key elt))))
1913 (if (funcall test (apply-key key elt)))
1914 (if-not (not (funcall test (apply-key key elt)))))
1915 (setq count (1- count))
1916 new)
1917 (t elt)))
1918 (setq index (+ index incrementer)))
1919 (do ()
1920 ((= index right))
1921 (setf (aref result index) (aref sequence index))
1922 (setq index (+ index incrementer)))
1923 result))
1925 (eval-when (:compile-toplevel :execute)
1927 (sb!xc:defmacro subst-dispatch (pred)
1928 `(seq-dispatch sequence
1929 (if from-end
1930 (nreverse (list-substitute* ,pred
1932 (reverse sequence)
1933 (- (the fixnum length)
1934 (the fixnum end))
1935 (- (the fixnum length)
1936 (the fixnum start))
1937 count key test test-not old))
1938 (list-substitute* ,pred
1939 new sequence start end count key test test-not
1940 old))
1941 (if from-end
1942 (vector-substitute* ,pred new sequence -1 (1- (the fixnum length))
1943 -1 length (1- (the fixnum end))
1944 (1- (the fixnum start))
1945 count key test test-not old)
1946 (vector-substitute* ,pred new sequence 1 0 length length
1947 start end count key test test-not old))
1948 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
1949 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
1950 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
1951 ;; dispatch once per element on PRED's run-time identity.
1952 ,(ecase (cadr pred)
1953 ((normal) `(apply #'sb!sequence:substitute new old sequence args))
1954 ((if) `(apply #'sb!sequence:substitute-if new predicate sequence args))
1955 ((if-not) `(apply #'sb!sequence:substitute-if-not new predicate sequence args)))))
1956 ) ; EVAL-WHEN
1958 (define-sequence-traverser substitute
1959 (new old sequence &rest args &key from-end test test-not
1960 start count end key)
1961 #!+sb-doc
1962 "Return a sequence of the same kind as SEQUENCE with the same elements,
1963 except that all elements equal to OLD are replaced with NEW."
1964 (declare (fixnum start))
1965 (declare (dynamic-extent args))
1966 (let ((end (or end length)))
1967 (declare (type index end))
1968 (subst-dispatch 'normal)))
1970 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
1972 (define-sequence-traverser substitute-if
1973 (new predicate sequence &rest args &key from-end start end count key)
1974 #!+sb-doc
1975 "Return a sequence of the same kind as SEQUENCE with the same elements
1976 except that all elements satisfying the PRED are replaced with NEW."
1977 (declare (dynamic-extent args))
1978 (declare (fixnum start))
1979 (let ((end (or end length))
1980 (test predicate)
1981 (test-not nil)
1982 old)
1983 (declare (type index length end))
1984 (subst-dispatch 'if)))
1986 (define-sequence-traverser substitute-if-not
1987 (new predicate sequence &rest args &key from-end start end count key)
1988 #!+sb-doc
1989 "Return a sequence of the same kind as SEQUENCE with the same elements
1990 except that all elements not satisfying the PRED are replaced with NEW."
1991 (declare (dynamic-extent args))
1992 (declare (fixnum start))
1993 (let ((end (or end length))
1994 (test predicate)
1995 (test-not nil)
1996 old)
1997 (declare (type index length end))
1998 (subst-dispatch 'if-not)))
2000 ;;;; NSUBSTITUTE
2002 (define-sequence-traverser nsubstitute
2003 (new old sequence &rest args &key from-end test test-not
2004 end count key start)
2005 #!+sb-doc
2006 "Return a sequence of the same kind as SEQUENCE with the same elements
2007 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2008 may be destructively modified."
2009 (declare (fixnum start))
2010 (declare (dynamic-extent args))
2011 (let ((end (or end length)))
2012 (seq-dispatch sequence
2013 (if from-end
2014 (let ((length (length sequence)))
2015 (nreverse (nlist-substitute*
2016 new old (nreverse (the list sequence))
2017 test test-not (- length end) (- length start)
2018 count key)))
2019 (nlist-substitute* new old sequence
2020 test test-not start end count key))
2021 (if from-end
2022 (nvector-substitute* new old sequence -1
2023 test test-not (1- end) (1- start) count key)
2024 (nvector-substitute* new old sequence 1
2025 test test-not start end count key))
2026 (apply #'sb!sequence:nsubstitute new old sequence args))))
2028 (defun nlist-substitute* (new old sequence test test-not start end count key)
2029 (declare (fixnum start count end))
2030 (do ((list (nthcdr start sequence) (cdr list))
2031 (index start (1+ index)))
2032 ((or (= index end) (null list) (= count 0)) sequence)
2033 (declare (fixnum index))
2034 (when (if test-not
2035 (not (funcall test-not old (apply-key key (car list))))
2036 (funcall test old (apply-key key (car list))))
2037 (rplaca list new)
2038 (setq count (1- count)))))
2040 (defun nvector-substitute* (new old sequence incrementer
2041 test test-not start end count key)
2042 (declare (fixnum start incrementer count end))
2043 (do ((index start (+ index incrementer)))
2044 ((or (= index end) (= count 0)) sequence)
2045 (declare (fixnum index))
2046 (when (if test-not
2047 (not (funcall test-not
2049 (apply-key key (aref sequence index))))
2050 (funcall test old (apply-key key (aref sequence index))))
2051 (setf (aref sequence index) new)
2052 (setq count (1- count)))))
2054 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2056 (define-sequence-traverser nsubstitute-if
2057 (new predicate sequence &rest args &key from-end start end count key)
2058 #!+sb-doc
2059 "Return a sequence of the same kind as SEQUENCE with the same elements
2060 except that all elements satisfying PREDICATE are replaced with NEW.
2061 SEQUENCE may be destructively modified."
2062 (declare (fixnum start))
2063 (declare (dynamic-extent args))
2064 (let ((end (or end length)))
2065 (declare (fixnum end))
2066 (seq-dispatch sequence
2067 (if from-end
2068 (let ((length (length sequence)))
2069 (nreverse (nlist-substitute-if*
2070 new predicate (nreverse (the list sequence))
2071 (- length end) (- length start) count key)))
2072 (nlist-substitute-if* new predicate sequence
2073 start end count key))
2074 (if from-end
2075 (nvector-substitute-if* new predicate sequence -1
2076 (1- end) (1- start) count key)
2077 (nvector-substitute-if* new predicate sequence 1
2078 start end count key))
2079 (apply #'sb!sequence:nsubstitute-if new predicate sequence args))))
2081 (defun nlist-substitute-if* (new test sequence start end count key)
2082 (declare (fixnum end))
2083 (do ((list (nthcdr start sequence) (cdr list))
2084 (index start (1+ index)))
2085 ((or (= index end) (null list) (= count 0)) sequence)
2086 (when (funcall test (apply-key key (car list)))
2087 (rplaca list new)
2088 (setq count (1- count)))))
2090 (defun nvector-substitute-if* (new test sequence incrementer
2091 start end count key)
2092 (do ((index start (+ index incrementer)))
2093 ((or (= index end) (= count 0)) sequence)
2094 (when (funcall test (apply-key key (aref sequence index)))
2095 (setf (aref sequence index) new)
2096 (setq count (1- count)))))
2098 (define-sequence-traverser nsubstitute-if-not
2099 (new predicate sequence &rest args &key from-end start end count key)
2100 #!+sb-doc
2101 "Return a sequence of the same kind as SEQUENCE with the same elements
2102 except that all elements not satisfying PREDICATE are replaced with NEW.
2103 SEQUENCE may be destructively modified."
2104 (declare (fixnum start))
2105 (declare (dynamic-extent args))
2106 (let ((end (or end length)))
2107 (declare (fixnum end))
2108 (seq-dispatch sequence
2109 (if from-end
2110 (let ((length (length sequence)))
2111 (nreverse (nlist-substitute-if-not*
2112 new predicate (nreverse (the list sequence))
2113 (- length end) (- length start) count key)))
2114 (nlist-substitute-if-not* new predicate sequence
2115 start end count key))
2116 (if from-end
2117 (nvector-substitute-if-not* new predicate sequence -1
2118 (1- end) (1- start) count key)
2119 (nvector-substitute-if-not* new predicate sequence 1
2120 start end count key))
2121 (apply #'sb!sequence:nsubstitute-if-not new predicate sequence args))))
2123 (defun nlist-substitute-if-not* (new test sequence start end count key)
2124 (declare (fixnum end))
2125 (do ((list (nthcdr start sequence) (cdr list))
2126 (index start (1+ index)))
2127 ((or (= index end) (null list) (= count 0)) sequence)
2128 (when (not (funcall test (apply-key key (car list))))
2129 (rplaca list new)
2130 (decf count))))
2132 (defun nvector-substitute-if-not* (new test sequence incrementer
2133 start end count key)
2134 (do ((index start (+ index incrementer)))
2135 ((or (= index end) (= count 0)) sequence)
2136 (when (not (funcall test (apply-key key (aref sequence index))))
2137 (setf (aref sequence index) new)
2138 (decf count))))
2140 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2142 (defun effective-find-position-test (test test-not)
2143 (effective-find-position-test test test-not))
2144 (defun effective-find-position-key (key)
2145 (effective-find-position-key key))
2147 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2148 (macrolet (;; shared logic for defining %FIND-POSITION and
2149 ;; %FIND-POSITION-IF in terms of various inlineable cases
2150 ;; of the expression defined in FROB and VECTOR*-FROB
2151 (frobs ()
2152 `(seq-dispatch sequence-arg
2153 (frob sequence-arg from-end)
2154 (with-array-data ((sequence sequence-arg :offset-var offset)
2155 (start start)
2156 (end end)
2157 :check-fill-pointer t)
2158 (multiple-value-bind (f p)
2159 (macrolet ((frob2 () '(if from-end
2160 (frob sequence t)
2161 (frob sequence nil))))
2162 (typecase sequence
2163 (simple-vector (frob2))
2164 (simple-base-string (frob2))
2165 (t (vector*-frob sequence))))
2166 (declare (type (or index null) p))
2167 (values f (and p (the index (- p offset)))))))))
2168 (defun %find-position (item sequence-arg from-end start end key test)
2169 (macrolet ((frob (sequence from-end)
2170 `(%find-position item ,sequence
2171 ,from-end start end key test))
2172 (vector*-frob (sequence)
2173 `(%find-position-vector-macro item ,sequence
2174 from-end start end key test)))
2175 (frobs)))
2176 (defun %find-position-if (predicate sequence-arg from-end start end key)
2177 (macrolet ((frob (sequence from-end)
2178 `(%find-position-if predicate ,sequence
2179 ,from-end start end key))
2180 (vector*-frob (sequence)
2181 `(%find-position-if-vector-macro predicate ,sequence
2182 from-end start end key)))
2183 (frobs)))
2184 (defun %find-position-if-not (predicate sequence-arg from-end start end key)
2185 (macrolet ((frob (sequence from-end)
2186 `(%find-position-if-not predicate ,sequence
2187 ,from-end start end key))
2188 (vector*-frob (sequence)
2189 `(%find-position-if-not-vector-macro predicate ,sequence
2190 from-end start end key)))
2191 (frobs))))
2193 (defun find
2194 (item sequence &rest args &key from-end (start 0) end key test test-not)
2195 (declare (dynamic-extent args))
2196 (seq-dispatch sequence
2197 (nth-value 0 (%find-position
2198 item sequence from-end start end
2199 (effective-find-position-key key)
2200 (effective-find-position-test test test-not)))
2201 (nth-value 0 (%find-position
2202 item sequence from-end start end
2203 (effective-find-position-key key)
2204 (effective-find-position-test test test-not)))
2205 (apply #'sb!sequence:find item sequence args)))
2206 (defun position
2207 (item sequence &rest args &key from-end (start 0) end key test test-not)
2208 (declare (dynamic-extent args))
2209 (seq-dispatch sequence
2210 (nth-value 1 (%find-position
2211 item sequence from-end start end
2212 (effective-find-position-key key)
2213 (effective-find-position-test test test-not)))
2214 (nth-value 1 (%find-position
2215 item sequence from-end start end
2216 (effective-find-position-key key)
2217 (effective-find-position-test test test-not)))
2218 (apply #'sb!sequence:position item sequence args)))
2220 (defun find-if (predicate sequence &rest args &key from-end (start 0) end key)
2221 (declare (dynamic-extent args))
2222 (seq-dispatch sequence
2223 (nth-value 0 (%find-position-if
2224 (%coerce-callable-to-fun predicate)
2225 sequence from-end start end
2226 (effective-find-position-key key)))
2227 (nth-value 0 (%find-position-if
2228 (%coerce-callable-to-fun predicate)
2229 sequence from-end start end
2230 (effective-find-position-key key)))
2231 (apply #'sb!sequence:find-if predicate sequence args)))
2232 (defun position-if
2233 (predicate sequence &rest args &key from-end (start 0) end key)
2234 (declare (dynamic-extent args))
2235 (seq-dispatch sequence
2236 (nth-value 1 (%find-position-if
2237 (%coerce-callable-to-fun predicate)
2238 sequence from-end start end
2239 (effective-find-position-key key)))
2240 (nth-value 1 (%find-position-if
2241 (%coerce-callable-to-fun predicate)
2242 sequence from-end start end
2243 (effective-find-position-key key)))
2244 (apply #'sb!sequence:position-if predicate sequence args)))
2246 (defun find-if-not
2247 (predicate sequence &rest args &key from-end (start 0) end key)
2248 (declare (dynamic-extent args))
2249 (seq-dispatch sequence
2250 (nth-value 0 (%find-position-if-not
2251 (%coerce-callable-to-fun predicate)
2252 sequence from-end start end
2253 (effective-find-position-key key)))
2254 (nth-value 0 (%find-position-if-not
2255 (%coerce-callable-to-fun predicate)
2256 sequence from-end start end
2257 (effective-find-position-key key)))
2258 (apply #'sb!sequence:find-if-not predicate sequence args)))
2259 (defun position-if-not
2260 (predicate sequence &rest args &key from-end (start 0) end key)
2261 (declare (dynamic-extent args))
2262 (seq-dispatch sequence
2263 (nth-value 1 (%find-position-if-not
2264 (%coerce-callable-to-fun predicate)
2265 sequence from-end start end
2266 (effective-find-position-key key)))
2267 (nth-value 1 (%find-position-if-not
2268 (%coerce-callable-to-fun predicate)
2269 sequence from-end start end
2270 (effective-find-position-key key)))
2271 (apply #'sb!sequence:position-if-not predicate sequence args)))
2273 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2275 (eval-when (:compile-toplevel :execute)
2277 (sb!xc:defmacro vector-count-if (notp from-end-p predicate sequence)
2278 (let ((next-index (if from-end-p '(1- index) '(1+ index)))
2279 (pred `(funcall ,predicate (apply-key key (aref ,sequence index)))))
2280 `(let ((%start ,(if from-end-p '(1- end) 'start))
2281 (%end ,(if from-end-p '(1- start) 'end)))
2282 (do ((index %start ,next-index)
2283 (count 0))
2284 ((= index (the fixnum %end)) count)
2285 (declare (fixnum index count))
2286 (,(if notp 'unless 'when) ,pred
2287 (setq count (1+ count)))))))
2289 (sb!xc:defmacro list-count-if (notp from-end-p predicate sequence)
2290 (let ((pred `(funcall ,predicate (apply-key key (pop sequence)))))
2291 `(let ((%start ,(if from-end-p '(- length end) 'start))
2292 (%end ,(if from-end-p '(- length start) 'end))
2293 (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
2294 (do ((sequence (nthcdr %start ,sequence))
2295 (index %start (1+ index))
2296 (count 0))
2297 ((or (= index (the fixnum %end)) (null sequence)) count)
2298 (declare (fixnum index count))
2299 (,(if notp 'unless 'when) ,pred
2300 (setq count (1+ count)))))))
2303 ) ; EVAL-WHEN
2305 (define-sequence-traverser count-if
2306 (pred sequence &rest args &key from-end start end key)
2307 #!+sb-doc
2308 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2309 (declare (fixnum start))
2310 (declare (dynamic-extent args))
2311 (let ((end (or end length))
2312 (pred (%coerce-callable-to-fun pred)))
2313 (declare (type index end))
2314 (seq-dispatch sequence
2315 (if from-end
2316 (list-count-if nil t pred sequence)
2317 (list-count-if nil nil pred sequence))
2318 (if from-end
2319 (vector-count-if nil t pred sequence)
2320 (vector-count-if nil nil pred sequence))
2321 (apply #'sb!sequence:count-if pred sequence args))))
2323 (define-sequence-traverser count-if-not
2324 (pred sequence &rest args &key from-end start end key)
2325 #!+sb-doc
2326 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2327 (declare (fixnum start))
2328 (declare (dynamic-extent args))
2329 (let ((end (or end length))
2330 (pred (%coerce-callable-to-fun pred)))
2331 (declare (type index end))
2332 (seq-dispatch sequence
2333 (if from-end
2334 (list-count-if t t pred sequence)
2335 (list-count-if t nil pred sequence))
2336 (if from-end
2337 (vector-count-if t t pred sequence)
2338 (vector-count-if t nil pred sequence))
2339 (apply #'sb!sequence:count-if-not pred sequence args))))
2341 (define-sequence-traverser count
2342 (item sequence &rest args &key from-end start end
2343 key (test #'eql test-p) (test-not nil test-not-p))
2344 #!+sb-doc
2345 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2346 which defaults to EQL."
2347 (declare (fixnum start))
2348 (declare (dynamic-extent args))
2349 (when (and test-p test-not-p)
2350 ;; ANSI Common Lisp has left the behavior in this situation unspecified.
2351 ;; (CLHS 17.2.1)
2352 (error ":TEST and :TEST-NOT are both present."))
2353 (let ((end (or end length)))
2354 (declare (type index end))
2355 (let ((%test (if test-not-p
2356 (lambda (x)
2357 (not (funcall test-not item x)))
2358 (lambda (x)
2359 (funcall test item x)))))
2360 (seq-dispatch sequence
2361 (if from-end
2362 (list-count-if nil t %test sequence)
2363 (list-count-if nil nil %test sequence))
2364 (if from-end
2365 (vector-count-if nil t %test sequence)
2366 (vector-count-if nil nil %test sequence))
2367 (apply #'sb!sequence:count item sequence args)))))
2369 ;;;; MISMATCH
2371 (eval-when (:compile-toplevel :execute)
2373 (sb!xc:defmacro match-vars (&rest body)
2374 `(let ((inc (if from-end -1 1))
2375 (start1 (if from-end (1- (the fixnum end1)) start1))
2376 (start2 (if from-end (1- (the fixnum end2)) start2))
2377 (end1 (if from-end (1- (the fixnum start1)) end1))
2378 (end2 (if from-end (1- (the fixnum start2)) end2)))
2379 (declare (fixnum inc start1 start2 end1 end2))
2380 ,@body))
2382 (sb!xc:defmacro matchify-list ((sequence start length end) &body body)
2383 (declare (ignore end)) ;; ### Should END be used below?
2384 `(let ((,sequence (if from-end
2385 (nthcdr (- (the fixnum ,length) (the fixnum ,start) 1)
2386 (reverse (the list ,sequence)))
2387 (nthcdr ,start ,sequence))))
2388 (declare (type list ,sequence))
2389 ,@body))
2391 ) ; EVAL-WHEN
2393 (eval-when (:compile-toplevel :execute)
2395 (sb!xc:defmacro if-mismatch (elt1 elt2)
2396 `(cond ((= (the fixnum index1) (the fixnum end1))
2397 (return (if (= (the fixnum index2) (the fixnum end2))
2399 (if from-end
2400 (1+ (the fixnum index1))
2401 (the fixnum index1)))))
2402 ((= (the fixnum index2) (the fixnum end2))
2403 (return (if from-end (1+ (the fixnum index1)) index1)))
2404 (test-not
2405 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2406 (return (if from-end (1+ (the fixnum index1)) index1))))
2407 (t (if (not (funcall test (apply-key key ,elt1)
2408 (apply-key key ,elt2)))
2409 (return (if from-end (1+ (the fixnum index1)) index1))))))
2411 (sb!xc:defmacro mumble-mumble-mismatch ()
2412 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2413 (index2 start2 (+ index2 (the fixnum inc))))
2414 (())
2415 (declare (fixnum index1 index2))
2416 (if-mismatch (aref sequence1 index1) (aref sequence2 index2))))
2418 (sb!xc:defmacro mumble-list-mismatch ()
2419 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2420 (index2 start2 (+ index2 (the fixnum inc))))
2421 (())
2422 (declare (fixnum index1 index2))
2423 (if-mismatch (aref sequence1 index1) (pop sequence2))))
2425 (sb!xc:defmacro list-mumble-mismatch ()
2426 `(do ((index1 start1 (+ index1 (the fixnum inc)))
2427 (index2 start2 (+ index2 (the fixnum inc))))
2428 (())
2429 (declare (fixnum index1 index2))
2430 (if-mismatch (pop sequence1) (aref sequence2 index2))))
2432 (sb!xc:defmacro list-list-mismatch ()
2433 `(do ((sequence1 sequence1)
2434 (sequence2 sequence2)
2435 (index1 start1 (+ index1 (the fixnum inc)))
2436 (index2 start2 (+ index2 (the fixnum inc))))
2437 (())
2438 (declare (fixnum index1 index2))
2439 (if-mismatch (pop sequence1) (pop sequence2))))
2441 ) ; EVAL-WHEN
2443 (define-sequence-traverser mismatch
2444 (sequence1 sequence2 &rest args &key from-end test test-not
2445 start1 end1 start2 end2 key)
2446 #!+sb-doc
2447 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
2448 element-wise. If they are of equal length and match in every element, the
2449 result is NIL. Otherwise, the result is a non-negative integer, the index
2450 within SEQUENCE1 of the leftmost position at which they fail to match; or,
2451 if one is shorter than and a matching prefix of the other, the index within
2452 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
2453 :FROM-END argument is given, then one plus the index of the rightmost
2454 position in which the sequences differ is returned."
2455 (declare (fixnum start1 start2))
2456 (declare (dynamic-extent args))
2457 (let* ((end1 (or end1 length1))
2458 (end2 (or end2 length2)))
2459 (declare (type index end1 end2))
2460 (match-vars
2461 (seq-dispatch sequence1
2462 (seq-dispatch sequence2
2463 (matchify-list (sequence1 start1 length1 end1)
2464 (matchify-list (sequence2 start2 length2 end2)
2465 (list-list-mismatch)))
2466 (matchify-list (sequence1 start1 length1 end1)
2467 (list-mumble-mismatch))
2468 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2469 (seq-dispatch sequence2
2470 (matchify-list (sequence2 start2 length2 end2)
2471 (mumble-list-mismatch))
2472 (mumble-mumble-mismatch)
2473 (apply #'sb!sequence:mismatch sequence1 sequence2 args))
2474 (apply #'sb!sequence:mismatch sequence1 sequence2 args)))))
2476 ;;; search comparison functions
2478 (eval-when (:compile-toplevel :execute)
2480 ;;; Compare two elements and return if they don't match.
2481 (sb!xc:defmacro compare-elements (elt1 elt2)
2482 `(if test-not
2483 (if (funcall test-not (apply-key key ,elt1) (apply-key key ,elt2))
2484 (return nil)
2486 (if (not (funcall test (apply-key key ,elt1) (apply-key key ,elt2)))
2487 (return nil)
2488 t)))
2490 (sb!xc:defmacro search-compare-list-list (main sub)
2491 `(do ((main ,main (cdr main))
2492 (jndex start1 (1+ jndex))
2493 (sub (nthcdr start1 ,sub) (cdr sub)))
2494 ((or (endp main) (endp sub) (<= end1 jndex))
2496 (declare (type (integer 0) jndex))
2497 (compare-elements (car sub) (car main))))
2499 (sb!xc:defmacro search-compare-list-vector (main sub)
2500 `(do ((main ,main (cdr main))
2501 (index start1 (1+ index)))
2502 ((or (endp main) (= index end1)) t)
2503 (compare-elements (aref ,sub index) (car main))))
2505 (sb!xc:defmacro search-compare-vector-list (main sub index)
2506 `(do ((sub (nthcdr start1 ,sub) (cdr sub))
2507 (jndex start1 (1+ jndex))
2508 (index ,index (1+ index)))
2509 ((or (<= end1 jndex) (endp sub)) t)
2510 (declare (type (integer 0) jndex))
2511 (compare-elements (car sub) (aref ,main index))))
2513 (sb!xc:defmacro search-compare-vector-vector (main sub index)
2514 `(do ((index ,index (1+ index))
2515 (sub-index start1 (1+ sub-index)))
2516 ((= sub-index end1) t)
2517 (compare-elements (aref ,sub sub-index) (aref ,main index))))
2519 (sb!xc:defmacro search-compare (main-type main sub index)
2520 (if (eq main-type 'list)
2521 `(seq-dispatch ,sub
2522 (search-compare-list-list ,main ,sub)
2523 (search-compare-list-vector ,main ,sub)
2524 ;; KLUDGE: just hack it together so that it works
2525 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))
2526 `(seq-dispatch ,sub
2527 (search-compare-vector-list ,main ,sub ,index)
2528 (search-compare-vector-vector ,main ,sub ,index)
2529 (return-from search (apply #'sb!sequence:search sequence1 sequence2 args)))))
2531 ) ; EVAL-WHEN
2533 ;;;; SEARCH
2535 (eval-when (:compile-toplevel :execute)
2537 (sb!xc:defmacro list-search (main sub)
2538 `(do ((main (nthcdr start2 ,main) (cdr main))
2539 (index2 start2 (1+ index2))
2540 (terminus (- end2 (the (integer 0) (- end1 start1))))
2541 (last-match ()))
2542 ((> index2 terminus) last-match)
2543 (declare (type (integer 0) index2))
2544 (if (search-compare list main ,sub index2)
2545 (if from-end
2546 (setq last-match index2)
2547 (return index2)))))
2549 (sb!xc:defmacro vector-search (main sub)
2550 `(do ((index2 start2 (1+ index2))
2551 (terminus (- end2 (the (integer 0) (- end1 start1))))
2552 (last-match ()))
2553 ((> index2 terminus) last-match)
2554 (declare (type (integer 0) index2))
2555 (if (search-compare vector ,main ,sub index2)
2556 (if from-end
2557 (setq last-match index2)
2558 (return index2)))))
2560 ) ; EVAL-WHEN
2562 (define-sequence-traverser search
2563 (sequence1 sequence2 &rest args &key
2564 from-end test test-not start1 end1 start2 end2 key)
2565 (declare (fixnum start1 start2))
2566 (declare (dynamic-extent args))
2567 (let ((end1 (or end1 length1))
2568 (end2 (or end2 length2)))
2569 (seq-dispatch sequence2
2570 (list-search sequence2 sequence1)
2571 (vector-search sequence2 sequence1)
2572 (apply #'sb!sequence:search sequence1 sequence2 args))))
2574 ;;; FIXME: this was originally in array.lisp; it might be better to
2575 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
2576 ;;; a new early-seq.lisp file.
2577 (defun fill-data-vector (vector dimensions initial-contents)
2578 (let ((index 0))
2579 (labels ((frob (axis dims contents)
2580 (cond ((null dims)
2581 (setf (aref vector index) contents)
2582 (incf index))
2584 (unless (typep contents 'sequence)
2585 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
2586 sequence, but ~W more layer~:P needed."
2587 contents
2588 (- (length dimensions) axis)))
2589 (unless (= (length contents) (car dims))
2590 (error "malformed :INITIAL-CONTENTS: Dimension of ~
2591 axis ~W is ~W, but ~S is ~W long."
2592 axis (car dims) contents (length contents)))
2593 (sb!sequence:dosequence (content contents)
2594 (frob (1+ axis) (cdr dims) content))))))
2595 (frob 0 dimensions initial-contents))))