3 ;;;; KLUDGE: comment from original CMU CL source:
4 ;;;; Be careful when modifying code. A lot of the structure of the
5 ;;;; code is affected by the fact that compiler transforms use the
6 ;;;; lower level support functions. If transforms are written for
7 ;;;; some sequence operation, note how the END argument is handled
8 ;;;; in other operations with transforms.
10 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; more information.
13 ;;;; This software is derived from the CMU CL system, which was
14 ;;;; written at Carnegie Mellon University and released into the
15 ;;;; public domain. The software is in the public domain and is
16 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
17 ;;;; files for more information.
19 (in-package "SB-IMPL")
23 (defun %check-generic-sequence-bounds
(seq start end
)
24 (let ((length (sb-sequence:length seq
)))
25 (if (<= 0 start
(or end length
) length
)
27 (sequence-bounding-indices-bad-error seq start end
))))
29 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
30 (defparameter *sequence-keyword-info
*
31 ;; (name default supplied-p adjustment new-type)
35 (null (1- most-positive-fixnum
))
36 (fixnum (max 0 count
))
37 (integer (if (minusp count
)
39 (1- most-positive-fixnum
))))
40 (mod #.most-positive-fixnum
))
41 ;; Entries for {start,end}{,1,2}
42 ,@(mapcan (lambda (names)
43 (destructuring-bind (start end length sequence
) names
48 ;; Only evaluate LENGTH (which may be expensive)
49 ;; if START is non-NIL.
50 (if (or (zerop ,start
) (<= 0 ,start
,length
))
52 (sequence-bounding-indices-bad-error ,sequence
,start
,end
))
57 ;; Only evaluate LENGTH (which may be expensive)
59 (if (or (null ,end
) (<= ,start
,end
,length
))
60 ;; Defaulting of NIL is done inside the
61 ;; bodies, for ease of sharing with compiler
64 ;; FIXME: defend against non-number non-NIL
67 (sequence-bounding-indices-bad-error ,sequence
,start
,end
))
69 '((start end length sequence
)
70 (start1 end1 length1 sequence1
)
71 (start2 end2 length2 sequence2
)))
74 (and key
(%coerce-callable-to-fun key
))
78 (%coerce-callable-to-fun test
)
82 (and test-not
(%coerce-callable-to-fun test-not
))
83 (or null function
)))))
85 (defmacro define-sequence-traverser
(name args
&body body
)
86 (multiple-value-bind (body declarations docstring
) (parse-body body t
)
89 ;; Things which are functions
91 ;; Things which are definitely used in any code path.
93 ;; Things which may be used/are only used in certain
94 ;; code paths (e.g. length).
98 (let ((sym (if (listp arg
) (car arg
) arg
)))
99 (when (member sym
'(function predicate key test test-not
))
103 (search "SEQUENCE" (string arg
)))
104 (let* ((length-var (ecase (char (string arg
) (1- (length (string arg
))))
108 ;; For *sequence-keyword-info*
109 (substs (cons arg
'sequence1
))
112 (substs (cons arg
'sequence2
))
114 (cache-var (symbolicate length-var
"-CACHE")))
116 (rebindings/eager
`(,cache-var nil
))
118 `(,length-var
(truly-the
120 (or ,cache-var
(setf ,cache-var
(length ,arg
))))))))
121 ((memq arg
'(function predicate
))
123 (rebindings/eager
`(,arg
(%coerce-callable-to-fun
,arg
))))
125 (let ((info (cdr (assoc arg
*sequence-keyword-info
*))))
127 (destructuring-bind (default supplied-p adjuster type
) info
128 (new-args `(,arg
,default
,@(when supplied-p
(list supplied-p
))))
129 (loop for
(new . old
) in
(substs)
130 do
(setf adjuster
(subst new old adjuster
)))
131 (rebindings/eager
`(,arg
,adjuster
))
132 (new-declarations `(type ,type
,arg
))))
133 (t (new-args arg
)))))))
134 `(defun ,name
,(new-args)
135 ,@(when docstring
(list docstring
))
137 ;; All sequence traversers' funargs are downward funargs
138 (declare (dynamic-extent ,@(funargs)))
139 (symbol-macrolet (,@(rebindings/lazy
))
140 (let* (,@(rebindings/eager
))
141 (declare ,@(new-declarations))
144 ;;; SEQ-DISPATCH does an efficient type-dispatch on the given SEQUENCE.
146 ;;; FIXME: It might be worth making three cases here, LIST,
147 ;;; SIMPLE-VECTOR, and VECTOR, instead of the current LIST and VECTOR.
148 ;;; It tends to make code run faster but be bigger; some benchmarking
149 ;;; is needed to decide.
150 (defmacro seq-dispatch
(sequence list-form array-form
&optional other-form
)
151 `(if (listp ,sequence
)
152 (let ((,sequence
(truly-the list
,sequence
)))
153 (declare (ignorable ,sequence
))
156 `((if (arrayp ,sequence
)
157 (let ((,sequence
(truly-the vector
,sequence
)))
158 (declare (ignorable ,sequence
))
161 `((let ((,sequence
(truly-the vector
,sequence
)))
162 (declare (ignorable ,sequence
))
165 ;; Same as above, but don't assume that ARRAYP implies VECTORP,
166 ;; and test EXTENDED-SEQUENCE-P if three cases are allowed.
167 ;; Signal a type error for non-sequences.
168 ;; This is for dispatching within sequence functions that have
169 ;; the EXPLICIT-CHECK attribute on at least their sequence arg(s).
170 (defmacro seq-dispatch-checking
171 (sequence list-form vector-form
&optional
(other-form nil other-form-p
))
172 `(cond ((listp ,sequence
)
173 (let ((,sequence
(truly-the list
,sequence
)))
174 (declare (ignorable ,sequence
))
177 (let ((,sequence
(truly-the vector
,sequence
)))
178 (declare (ignorable ,sequence
))
180 ,@(cond ((not other-form-p
)
182 (sb-c::%type-check-error
,sequence
'(or list vector
)
185 `(((extended-sequence-p ,sequence
)
186 (let ((,sequence
(truly-the extended-sequence
,sequence
)))
187 (declare (ignorable ,sequence
))
190 (sb-c::%type-check-error
/c
191 ,sequence
'sb-kernel
::object-not-sequence-error nil
)))))))
193 (defmacro make-vector-like
(vector length poison
)
194 (declare (ignorable poison
))
195 `(let ((type (array-underlying-widetag ,vector
)))
196 (sb-vm::allocate-vector-with-widetag
197 #+ubsan
,poison type
,length
(aref sb-vm
::%%simple-array-n-bits-shifts%% type
))))
199 ;;; Like SEQ-DISPATCH-CHECKING, but also assert that OTHER-FORM produces
200 ;;; a sequence. This assumes that the containing function declares its
201 ;;; result to be explicitly checked,
202 ;;; and that the LIST and VECTOR cases never fail to return a sequence.
203 (defmacro seq-dispatch-checking
=>seq
204 (sequence list-form vector-form other-form
)
205 `(seq-dispatch-checking ,sequence
,list-form
,vector-form
206 (the sequence
(values ,other-form
))))
208 (defmacro %make-sequence-like
(sequence length
)
209 "Return a sequence of the same type as SEQUENCE and the given LENGTH."
210 `(seq-dispatch ,sequence
212 (make-vector-like ,sequence
,length t
)
213 (sb-sequence:make-sequence-like
,sequence
,length
)))
215 (define-error-wrapper bad-sequence-type-error
(type-spec)
216 (error 'simple-type-error
218 :expected-type
'(satisfies is-a-valid-sequence-type-specifier-p
)
219 :format-control
"~S is a bad type specifier for sequences."
220 :format-arguments
(list type-spec
)))
222 (define-error-wrapper sequence-type-length-mismatch-error
(type length
)
223 (error 'simple-type-error
225 :expected-type
(cond ((array-type-p type
)
226 `(eql ,(car (array-type-dimensions type
))))
227 ((type= type
(specifier-type 'null
))
231 (t (bug "weird type in S-T-L-M-ERROR")))
232 ;; FIXME: this format control causes ugly printing. There's
233 ;; probably some ~<~@:_~> incantation that would make it
234 ;; nicer. -- CSR, 2002-10-18
235 :format-control
"The length requested (~S) does not match the type restriction in ~S."
236 :format-arguments
(list length
(type-specifier type
))))
238 (define-error-wrapper sequence-type-too-hairy
(type-spec)
239 ;; FIXME: Should this be a BUG? I'm inclined to think not; there are
240 ;; words that give some but not total support to this position in
241 ;; ANSI. Essentially, we are justified in throwing this on
242 ;; e.g. '(OR SIMPLE-VECTOR (VECTOR FIXNUM)), but maybe not (by ANSI)
243 ;; on '(CONS * (CONS * NULL)) -- CSR, 2002-10-18
245 ;; On the other hand, I'm not sure it deserves to be a type-error,
246 ;; either. -- bem, 2005-08-10
247 (%program-error
"~S is too hairy for sequence functions." type-spec
))
249 (defmacro when-extended-sequence-type
250 ((type-specifier type
253 (expanded (gensym "EXPANDED"))
254 (class (gensym "CLASS"))
255 (prototype (gensym "PROTOTYPE") prototypep
))
257 (once-only ((type-specifier type-specifier
) (type type
))
258 `(when (csubtypep ,type
(specifier-type 'sequence
))
259 (binding* ((,expanded
,(if expandedp
261 `(typexpand ,type-specifier
)))
262 (,class
(if (typep ,expanded
'class
)
264 (find-class ,expanded nil
))
266 (,prototype
(sb-mop:class-prototype
267 (sb-pcl:ensure-class-finalized
,class
))))
268 ,@(unless prototypep
`((ignore ,prototype
)))
271 (defun is-a-valid-sequence-type-specifier-p (type)
272 (let ((type (specifier-type type
)))
273 (or (csubtypep type
(specifier-type 'list
))
274 (csubtypep type
(specifier-type 'vector
)))))
276 (declaim (ftype (function (sequence index
) nil
) signal-index-too-large-error
))
277 (define-error-wrapper signal-index-too-large-error
(sequence index
)
278 (let* ((length (length sequence
))
279 (max-index (and (plusp length
)
281 (error 'index-too-large-error
284 :expected-type
(if max-index
285 `(integer 0 ,max-index
)
286 ;; This seems silly, is there something better?
289 (declaim (ftype (function (t t t
) nil
) sequence-bounding-indices-bad-error
))
290 (define-error-wrapper sequence-bounding-indices-bad-error
(sequence start end
)
291 (let ((size (length sequence
)))
292 (error 'bounding-indices-bad-error
293 :datum
(cons start end
)
294 :expected-type
`(cons (integer 0 ,size
)
295 (integer ,start
,size
))
298 (declaim (ftype (function (t t t
) nil
) array-bounding-indices-bad-error
))
299 (define-error-wrapper array-bounding-indices-bad-error
(array start end
)
300 (let ((size (array-total-size array
)))
301 (error 'bounding-indices-bad-error
302 :datum
(cons start end
)
303 :expected-type
`(cons (integer 0 ,size
)
304 (integer ,start
,size
))
307 (declaim (ftype (function (t) nil
) circular-list-error
))
308 (define-error-wrapper circular-list-error
(list)
309 (error 'simple-type-error
310 :format-control
"List is circular:~% ~S"
311 :format-arguments
(list list
)
313 :type
'(and list
(satisfies list-length
))))
317 (defun emptyp (sequence)
318 "Returns T if SEQUENCE is an empty sequence and NIL
319 otherwise. Signals an error if SEQUENCE is not a sequence."
320 (declare (explicit-check sequence
))
321 (seq-dispatch-checking sequence
323 (zerop (length sequence
))
324 (sb-sequence:emptyp sequence
)))
326 (defun elt (sequence index
)
327 "Return the element of SEQUENCE specified by INDEX."
328 (declare (explicit-check sequence
))
329 (seq-dispatch-checking sequence
330 (do ((count index
(1- count
))
331 (list sequence
(cdr list
)))
334 (signal-index-too-large-error sequence index
)
336 (declare (type index count
)))
338 (declare (optimize (sb-c:insert-array-bounds-checks
0)))
339 (when (>= index
(length sequence
))
340 (signal-index-too-large-error sequence index
))
341 (aref sequence index
))
342 (sb-sequence:elt sequence index
)))
344 (defun %setelt
(sequence index newval
)
345 "Store NEWVAL as the component of SEQUENCE specified by INDEX."
346 (declare (explicit-check sequence
))
347 (seq-dispatch-checking sequence
348 (do ((count index
(1- count
))
350 ((= count
0) (rplaca seq newval
) newval
)
351 (declare (fixnum count
))
352 (let ((cdr (cdr seq
)))
354 (signal-index-too-large-error sequence index
)
356 (if (>= index
(length sequence
))
357 (signal-index-too-large-error sequence index
)
359 (declare (optimize (sb-c:insert-array-bounds-checks
0)))
360 (setf (aref sequence index
) newval
)))
361 (setf (sb-sequence:elt sequence index
) newval
)))
363 (defun length (sequence)
364 "Return an integer that is the length of SEQUENCE."
365 (declare (explicit-check))
366 (seq-dispatch-checking sequence
369 (sb-sequence:length sequence
)))
371 (defun make-sequence (result-type length
&key
(initial-element nil iep
))
372 "Return a sequence of the given RESULT-TYPE and LENGTH, with
373 elements initialized to INITIAL-ELEMENT."
374 (declare (index length
) (explicit-check))
375 (let* ((expanded-type (typexpand result-type
))
377 (typecase expanded-type
379 ((eq expanded-type
'string
) '(vector character
))
380 ((eq expanded-type
'simple-string
)
381 '(simple-array character
(*)))
384 ((eq (car expanded-type
) 'string
)
385 `(vector character
,@(cdr expanded-type
)))
386 ((eq (car expanded-type
) 'simple-string
)
387 `(simple-array character
,(if (cdr expanded-type
)
390 (t expanded-type
)))))
391 (type (specifier-type adjusted-type
))
392 (list-type (specifier-type 'list
)))
393 (cond ((csubtypep type list-type
)
395 ((type= type list-type
)
396 (make-list length
:initial-element initial-element
))
397 ((eq type
*empty-type
*)
398 (bad-sequence-type-error nil
))
399 ((type= type
(specifier-type 'null
))
402 (sequence-type-length-mismatch-error type length
)))
404 (multiple-value-bind (min exactp
)
405 (sb-kernel::cons-type-length-info type
)
407 (unless (= length min
)
408 (sequence-type-length-mismatch-error type length
))
409 (unless (>= length min
)
410 (sequence-type-length-mismatch-error type length
)))
411 (make-list length
:initial-element initial-element
)))
412 ;; We'll get here for e.g. (OR NULL (CONS INTEGER *)),
413 ;; which may seem strange and non-ideal, but then I'd say
414 ;; it was stranger to feed that type in to MAKE-SEQUENCE.
415 (t (sequence-type-too-hairy (type-specifier type
)))))
416 ((csubtypep type
(specifier-type 'vector
))
418 (;; is it immediately obvious what the result type is?
419 (typep type
'array-type
)
420 (aver (= (length (array-type-dimensions type
)) 1))
421 (let* ((etype (type-specifier
422 (array-type-specialized-element-type type
)))
423 (etype (if (eq etype
'*) t etype
))
424 (type-length (car (array-type-dimensions type
))))
425 (unless (or (eq type-length
'*)
426 (= type-length length
))
427 (sequence-type-length-mismatch-error type length
))
429 (make-array length
:element-type etype
430 :initial-element initial-element
)
431 (make-array length
:element-type etype
))))
432 (t (sequence-type-too-hairy (type-specifier type
)))))
433 ((when-extended-sequence-type
434 (expanded-type type
:expandedp t
:prototype prototype
)
435 ;; This function has the EXPLICIT-CHECK declaration, so
436 ;; we manually assert that it returns a SEQUENCE.
437 (the extended-sequence
439 (sb-sequence:make-sequence-like
440 prototype length
:initial-element initial-element
)
441 (sb-sequence:make-sequence-like
442 prototype length
)))))
443 (t (bad-sequence-type-error (type-specifier type
))))))
448 ;;; FIXME: surely we can emit tighter code by not having 16 copies of the
449 ;;; vector allocator buried inside this (and similar) functions.
450 ;;; Instead there can be a size calculation up front, then ALLOCATE-VECTOR
451 ;;; with the underlying widetag, then perform an N-way dispatch for the copy.
452 ;;; Also, there should only be as many different ways to copy
453 ;;; as there are different element sizes.
454 (!define-array-dispatch
:jump-table vector-subseq-dispatch
(array start end
)
455 ((declare (ignore array
))
456 (make-array (- end start
) :element-type nil
))
457 (declare (optimize speed
(safety 0)))
458 (declare (type index start end
))
459 (subseq array start end
))
461 ;;;; The support routines for SUBSEQ are used by compiler transforms,
462 ;;;; so we worry about dealing with END being supplied or defaulting
463 ;;;; to NIL at this level.
465 (defun vector-subseq* (sequence start end
)
466 (declare (type vector sequence
))
467 (declare (type index start
)
468 (type (or null index
) end
)
470 ;; This seems suboptimal in that type checking is performed in the XEP
471 ;; but we also have fallback cases in the dispatch table for catching
472 ;; all invalid widetags. Otoh, WITH-ARRAY-DATA needs to dtrt too.
473 ;; So maybe the dispatch table could dispatch to the specialization
474 ;; that handles everything needed for each widetag.
475 ;; This "outer" use of WITH-ARRAY-DATA would be removed.
476 (with-array-data ((data sequence
)
479 :check-fill-pointer t
481 (vector-subseq-dispatch data start end
)))
483 (defun list-subseq* (sequence start end
)
484 (declare (type list sequence
)
485 (type unsigned-byte start
)
486 (type (or null unsigned-byte
) end
))
488 (sequence-bounding-indices-bad-error sequence start end
)))
489 (let ((pointer sequence
))
490 (unless (zerop start
)
491 ;; If START > 0 the list cannot be empty. So CDR down to
492 ;; it START-1 times, check that we still have something, then
493 ;; CDR the final time.
495 ;; If START was zero, the list may be empty if END is NIL or
498 (setf pointer
(nthcdr (1- start
) pointer
)))
503 (let ((n (- end start
)))
507 (let* ((head (list nil
))
509 (declare (dynamic-extent head
))
510 (macrolet ((pop-one ()
511 `(let ((tmp (list (pop pointer
))))
515 (loop until
(fixnump n
)
518 ;; Fixnum case, but leave last element, so we should
519 ;; still have something left in the sequence.
526 ;; OK, pop the last one.
530 collect
(pop pointer
))))))
532 (defun subseq (sequence start
&optional end
)
533 "Return a copy of a subsequence of SEQUENCE starting with element number
534 START and continuing to the end of SEQUENCE or the optional END."
535 (declare (explicit-check sequence
:result
))
536 (seq-dispatch-checking=>seq sequence
537 (list-subseq* sequence start end
)
538 (vector-subseq* sequence start end
)
539 (sb-sequence:subseq sequence start end
)))
543 (defun copy-seq (sequence)
544 "Return a copy of SEQUENCE which is EQUAL to SEQUENCE but not EQ."
545 (declare (explicit-check sequence
:result
))
546 (seq-dispatch-checking sequence
547 (list-copy-seq* sequence
)
548 (vector-subseq* sequence
0 nil
)
549 ;; Copying an extended sequence has to return an extended-sequence
550 ;; and not just any SEQUENCE.
551 (the extended-sequence
(values (sb-sequence:copy-seq sequence
)))))
553 (defun list-copy-seq* (sequence)
554 (copy-list-macro sequence
:check-proper-list t
))
558 (defun list-fill* (sequence item start end
)
559 (declare (type list sequence
)
561 (type (or null index
) end
))
563 (sequence-bounding-indices-bad-error sequence start end
)))
564 (let ((pointer sequence
))
565 (unless (zerop start
)
566 ;; If START > 0 the list cannot be empty. So CDR down to it
567 ;; START-1 times, check that we still have something, then CDR
570 ;; If START was zero, the list may be empty if END is NIL or
573 (setf pointer
(nthcdr (1- start
) pointer
)))
578 (let ((n (- end start
)))
579 (declare (integer n
))
584 do
(setf pointer
(cdr (rplaca pointer item
))))))
586 do
(setf pointer
(cdr (rplaca pointer item
)))))))
589 (define-load-time-global %%fill-bashers%%
(make-array (1+ sb-vm
:widetag-mask
)
591 (macrolet ((init-fill-bashers ()
593 ,@(loop for saetp across sb-vm
:*specialized-array-element-type-properties
*
594 for et
= (sb-vm:saetp-specifier saetp
)
596 (sb-vm:valid-bit-bash-saetp-p saetp
))
598 (multiple-value-bind (basher value-transform
)
600 (sb-c::find-basher saetp
)
601 '(lambda (item vector start length
)
602 (declare (ignore item start length
))
603 (data-nil-vector-ref (truly-the (simple-array nil
(*)) vector
) 0)))
605 (aref %%fill-bashers%%
,(sb-vm:saetp-typecode saetp
))
608 `(lambda (sb-c::item
)
609 (declare (type ,et sb-c
::item
))
613 ;; vector-fill* depends on this assertion
614 (assert (member et
'(t (complex double-float
)
615 #-
64-bit
(complex single-float
)
616 #-
64-bit double-float
)
620 (defun vector-fill* (vector item start end
)
621 (declare (type index start
) (type (or index null
) end
)
623 (with-array-data ((vector vector
)
627 :check-fill-pointer t
)
628 (if (simple-vector-p vector
)
630 (declare (optimize (speed 3) (safety 0))) ; transform will kick in
631 (cond #+soft-card-marks
632 ((sb-c::unless-vop-existsp
(:named sb-kernel
:vector-fill
/t
)
633 (typep item
'(or fixnum boolean
)))
634 ;; No gc-card mark for these types
635 ;; Omit character and single-float for better
636 ;; type-checking, they are likely to go a
637 ;; specialized array.
638 (fill (truly-the simple-vector vector
) item
639 :start start
:end end
))
641 (fill (truly-the simple-vector vector
) item
642 :start start
:end end
))))
643 (let* ((widetag (%other-pointer-widetag vector
))
644 (bashers (svref %%fill-bashers%% widetag
)))
645 (macrolet ((fill-float (type)
647 (declare (optimize (speed 3) (safety 0))
649 (type (simple-array ,type
(*))
651 (do ((index start
(1+ index
)))
653 (declare (index index
))
654 (setf (aref vector index
) item
)))))
655 (cond ((neq bashers
0)
656 (funcall (truly-the function
(car (truly-the cons bashers
)))
657 (funcall (truly-the function
(cdr bashers
)) item
)
658 vector start
(- end start
)))
660 ((eq widetag sb-vm
:simple-array-double-float-widetag
)
661 (fill-float double-float
))
663 ((eq widetag sb-vm
:simple-array-complex-single-float-widetag
)
664 (fill-float (complex single-float
)))
666 (fill-float (complex double-float
))))))))
669 (defun string-fill* (sequence item start end
)
670 (declare (string sequence
))
671 (with-array-data ((data sequence
)
675 :check-fill-pointer t
)
676 ;; DEFTRANSFORM for FILL will turn these into
677 ;; calls to UB*-BASH-FILL.
680 ((simple-array character
(*))
681 (let ((item (locally (declare (optimize (safety 3)))
682 (the character item
))))
683 (fill data item
:start start
:end end
)))
684 ((simple-array base-char
(*))
685 (let ((item (locally (declare (optimize (safety 3)))
686 (the base-char item
))))
687 (fill data item
:start start
:end end
))))))
689 (defun fill (sequence item
&key
(start 0) end
)
690 "Replace the specified elements of SEQUENCE with ITEM."
691 (declare (explicit-check sequence
:result
))
692 (seq-dispatch-checking=>seq sequence
693 (list-fill* sequence item start end
)
694 (vector-fill* sequence item start end
)
695 (sb-sequence:fill sequence item
697 :end
(%check-generic-sequence-bounds sequence start end
))))
700 (defmacro word-specialized-vector-tag-p
(tag)
702 ,@(loop for saetp across sb-vm
:*specialized-array-element-type-properties
*
703 when
(and (eq (sb-vm:saetp-n-bits saetp
) sb-vm
:n-word-bits
)
704 (not (eq (sb-vm:saetp-specifier saetp
) t
)))
705 collect
`(eq ,tag
,(sb-vm:saetp-typecode saetp
)))))
709 ;;; If we are copying around in the same vector, be careful not to copy the
710 ;;; same elements over repeatedly. We do this by copying backwards.
711 ;;; Bounding indices were checked for validity by DEFINE-SEQUENCE-TRAVERSER.
712 (defmacro vector-replace-from-vector
()
714 (declare (optimize (safety 0)))
715 (let ((nelts (min (- target-end target-start
)
716 (- source-end source-start
))))
718 (with-array-data ((data1 target-sequence
) (start1 target-start
) (end1))
720 (with-array-data ((data2 source-sequence
) (start2 source-start
) (end2))
722 (let ((tag1 (%other-pointer-widetag data1
))
723 (tag2 (%other-pointer-widetag data2
)))
726 (when (= tag1 sb-vm
:simple-vector-widetag
) ; rely on the transform
727 (replace (truly-the simple-vector data1
)
728 (truly-the simple-vector data2
)
729 :start1 start1
:end1
(truly-the index
(+ start1 nelts
))
730 :start2 start2
:end2
(truly-the index
(+ start2 nelts
)))
731 (return-from replace
))
732 (let ((copier (sb-vm::blt-copier-for-widetag tag1
)))
734 ;; these copiers figure out which direction to step.
735 ;; arg order is FROM, TO which is the opposite of REPLACE.
736 (funcall (truly-the function copier
) data2 start2 data1 start1 nelts
)
737 (return-from replace
))))
738 ;; General case is just like the code emitted by TRANSFORM-REPLACE
739 ;; but using the getter and setter.
740 (let ((getter (the function
(svref %%data-vector-reffers%% tag2
)))
741 (setter (the function
(svref %%data-vector-setters%% tag1
))))
742 (cond ((and (eq data1 data2
) (> start1 start2
))
743 (do ((i (the (or (eql -
1) index
) (+ start1 nelts -
1)) (1- i
))
744 (j (the (or (eql -
1) index
) (+ start2 nelts -
1)) (1- j
)))
746 (declare (index i j
))
747 (funcall setter data1 i
(funcall getter data2 j
))))
749 (do ((i start1
(1+ i
))
751 (end (the index
(+ start1 nelts
))))
753 (declare (index i j
))
754 (funcall setter data1 i
(funcall getter data2 j
))))))))))))
757 (defmacro list-replace-from-list
()
758 `(if (and (eq target-sequence source-sequence
) (> target-start source-start
))
759 (let ((new-elts (subseq source-sequence source-start
760 (+ (the fixnum source-start
)
762 (min (- (the fixnum target-end
)
763 (the fixnum target-start
))
764 (- (the fixnum source-end
)
765 (the fixnum source-start
))))))))
766 (do ((n new-elts
(cdr n
))
767 (o (nthcdr target-start target-sequence
) (cdr o
)))
768 ((null n
) target-sequence
)
770 (do ((target-index target-start
(1+ target-index
))
771 (source-index source-start
(1+ source-index
))
772 (target-sequence-ref (nthcdr target-start target-sequence
)
773 (cdr target-sequence-ref
))
774 (source-sequence-ref (nthcdr source-start source-sequence
)
775 (cdr source-sequence-ref
)))
776 ((or (= target-index
(the fixnum target-end
))
777 (= source-index
(the fixnum source-end
))
778 (null target-sequence-ref
) (null source-sequence-ref
))
780 (declare (fixnum target-index source-index
))
781 (rplaca target-sequence-ref
(car source-sequence-ref
)))))
783 (defmacro list-replace-from-vector
()
784 `(do ((target-index target-start
(1+ target-index
))
785 (source-index source-start
(1+ source-index
))
786 (target-sequence-ref (nthcdr target-start target-sequence
)
787 (cdr target-sequence-ref
)))
788 ((or (= target-index
(the fixnum target-end
))
789 (= source-index
(the fixnum source-end
))
790 (null target-sequence-ref
))
792 (declare (fixnum source-index target-index
))
793 (rplaca target-sequence-ref
(aref source-sequence source-index
))))
795 (defmacro vector-replace-from-list
()
796 `(do ((target-index target-start
(1+ target-index
))
797 (source-index source-start
(1+ source-index
))
798 (source-sequence (nthcdr source-start source-sequence
)
799 (cdr source-sequence
)))
800 ((or (= target-index
(the fixnum target-end
))
801 (= source-index
(the fixnum source-end
))
802 (null source-sequence
))
804 (declare (fixnum target-index source-index
))
805 (setf (aref target-sequence target-index
) (car source-sequence
))))
807 (define-sequence-traverser replace
808 (target-sequence1 source-sequence2
&rest args
&key start1 end1 start2 end2
)
809 "Destructively modifies SEQUENCE1 by copying successive elements
810 into it from the SEQUENCE2.
812 Elements are copied to the subsequence bounded by START1 and END1,
813 from the subsequence bounded by START2 and END2. If these subsequences
814 are not of the same length, then the shorter length determines how
815 many elements are copied."
816 (declare (dynamic-extent args
))
817 (declare (explicit-check target-sequence1 source-sequence2
:result
))
818 (let* (;; KLUDGE: absent either rewriting FOO-REPLACE-FROM-BAR, or
819 ;; excessively polluting DEFINE-SEQUENCE-TRAVERSER, we rebind
820 ;; these things here so that legacy code gets the names it's
821 ;; expecting. We could use &AUX instead :-/.
822 (target-sequence target-sequence1
)
823 (source-sequence source-sequence2
)
824 (target-start start1
)
825 (source-start start2
)
826 (target-end (or end1 length1
))
827 (source-end (or end2 length2
)))
828 (seq-dispatch-checking target-sequence
829 (seq-dispatch-checking source-sequence
830 (return-from replace
(list-replace-from-list))
831 (return-from replace
(list-replace-from-vector))
833 (seq-dispatch-checking source-sequence
834 (return-from replace
(vector-replace-from-list))
835 (return-from replace
(vector-replace-from-vector))
838 ;; If sequence1 is an extended-sequence, we know nothing about sequence2.
839 ;; If sequence1 was a list or vector, then sequence2 is an extended-sequence
840 ;; or not a sequence. Either way, check it.
842 (values (apply #'sb-sequence
:replace target-sequence1
843 (the sequence source-sequence2
) args
)))))
846 (defun reverse (sequence)
847 "Return a new sequence containing the same elements but in reverse order."
848 (declare (explicit-check))
849 (seq-dispatch-checking sequence
850 (list-reverse sequence
)
851 (vector-reverse sequence
)
852 ;; The type deriver says that LIST => LIST and VECTOR => VECTOR
853 ;; but does not claim to know anything about extended-sequences.
854 ;; So this could theoretically return any subtype of SEQUENCE
855 ;; given an EXTENDED-SEQUENCE as input. But fndb says this returns
856 ;; a CONSED-SEQUENCE, which precludes non-simple vectors.
857 ;; But a CLOS sequence can apparently decide to return a LIST when
858 ;; reversed. [Is that too weird? Make this EXTENDED-SEQUENCE maybe?]
859 (the consed-sequence
(values (sb-sequence:reverse sequence
)))))
861 (defun list-reverse (list)
863 ((endp list
) new-list
)
864 (push (pop list
) new-list
)))
866 (defun list-reverse-into-vector (list)
867 (declare (explicit-check))
869 (let* ((length (length (the list list
)))
870 (vector (make-array length
))
872 (loop for i from
(1- length
) downto
0
874 (setf (aref vector i
) (pop list
)))
878 (defun reverse-word-specialized-vector (from to end
)
879 (declare (vector from
))
880 (do ((length (length to
))
881 (left-index 0 (1+ left-index
))
883 ((= left-index length
))
884 (declare (type index left-index right-index
))
886 (setf (%vector-raw-bits to left-index
)
887 (%vector-raw-bits from right-index
)))
890 (defun vector-reverse (vector)
891 (declare (vector vector
))
892 (let ((length (length vector
)))
893 (with-array-data ((vector vector
) (start) (end)
894 :check-fill-pointer t
)
895 (declare (ignorable start
))
896 (let* ((tag (%other-pointer-widetag vector
))
897 (new-vector (sb-vm::allocate-vector-with-widetag
898 #+ubsan nil tag length
899 (aref sb-vm
::%%simple-array-n-bits-shifts%% tag
))))
900 (cond ((= tag sb-vm
:simple-vector-widetag
)
901 (do ((left-index 0 (1+ left-index
))
903 ((= left-index length
))
904 (declare (type index left-index right-index
))
906 (setf (svref new-vector left-index
)
907 (svref vector right-index
))))
908 ((word-specialized-vector-tag-p tag
)
909 (reverse-word-specialized-vector vector new-vector end
))
911 ((typep vector
'(or (simple-array base-char
(*))
912 (simple-array (signed-byte 8) (*))
913 (simple-array (unsigned-byte 8) (*))))
914 (sb-vm::simd-reverse8 new-vector vector start length
))
916 ((typep vector
'(or #+sb-unicode
917 (simple-array character
(*))
918 (simple-array (signed-byte 32) (*))
919 (simple-array (unsigned-byte 32) (*))
920 (simple-array single-float
(*))))
921 (sb-vm::simd-reverse32 new-vector vector start length
))
923 (let ((getter (the function
(svref %%data-vector-reffers%% tag
)))
924 (setter (the function
(svref %%data-vector-setters%% tag
))))
925 (declare (fixnum length
))
926 (do ((forward-index 0 (1+ forward-index
))
927 (backward-index (1- end
) (1- backward-index
)))
928 ((= forward-index length
))
929 (declare (fixnum forward-index backward-index
))
930 (funcall setter new-vector forward-index
931 (funcall getter vector backward-index
))))))
936 (defun list-nreverse (list)
937 (do ((1st (cdr (truly-the list list
)) (cdr 1st
))
943 (declaim (inline nreverse-word-specialized-vector
))
944 (defun nreverse-word-specialized-vector (vector start end
)
945 (do ((left-index start
(1+ left-index
))
946 (right-index (1- end
) (1- right-index
)))
947 ((<= right-index left-index
))
948 (declare (type index left-index right-index
))
949 (let ((left (%vector-raw-bits vector left-index
))
950 (right (%vector-raw-bits vector right-index
)))
951 (setf (%vector-raw-bits vector left-index
) right
952 (%vector-raw-bits vector right-index
) left
)))
955 (defun vector-nreverse (original-vector)
956 (declare (vector original-vector
))
957 (when (> (length original-vector
) 1)
958 (with-array-data ((vector original-vector
) (start) (end)
959 :check-fill-pointer t
961 (let ((tag (%other-pointer-widetag vector
)))
962 (cond ((= tag sb-vm
:simple-vector-widetag
)
963 (do ((left-index start
(1+ left-index
))
964 (right-index (1- end
) (1- right-index
)))
965 ((<= right-index left-index
))
966 (declare (type index left-index right-index
))
967 (let ((left (svref vector left-index
))
968 (right (svref vector right-index
)))
969 (setf (svref vector left-index
) right
970 (svref vector right-index
) left
))))
971 ((word-specialized-vector-tag-p tag
)
972 (nreverse-word-specialized-vector vector start end
))
974 ((typep vector
'(or (simple-array base-char
(*))
975 (simple-array (signed-byte 8) (*))
976 (simple-array (unsigned-byte 8) (*))))
977 (return-from vector-nreverse
978 (sb-vm::simd-nreverse8 original-vector vector start end
)))
980 ((typep vector
'(or #+sb-unicode
981 (simple-array character
(*))
982 (simple-array (signed-byte 32) (*))
983 (simple-array (unsigned-byte 32) (*))
984 (simple-array single-float
(*))))
985 (return-from vector-nreverse
986 (sb-vm::simd-nreverse32 original-vector vector start end
)))
988 (let* ((getter (the function
(svref %%data-vector-reffers%% tag
)))
989 (setter (the function
(svref %%data-vector-setters%% tag
))))
990 (do ((left-index start
(1+ left-index
))
991 (right-index (1- end
) (1- right-index
)))
992 ((<= right-index left-index
))
993 (declare (type index left-index right-index
))
994 (let ((left (funcall getter vector left-index
))
995 (right (funcall getter vector right-index
)))
996 (funcall setter vector left-index right
)
997 (funcall setter vector right-index left
)))))))))
1000 (defun nreverse (sequence)
1001 "Return a sequence of the same elements in reverse order; the argument
1003 (declare (explicit-check))
1004 (seq-dispatch-checking sequence
1005 (list-nreverse sequence
)
1006 (vector-nreverse sequence
)
1007 ;; The type deriver for this is 'result-type-first-arg',
1008 ;; meaning it should return definitely an EXTENDED-SEQUENCE
1009 ;; and not a list or vector.
1010 (the extended-sequence
(values (sb-sequence:nreverse sequence
)))))
1013 (defmacro sb-sequence
:dosequence
((element sequence
&optional return
) &body body
)
1014 "Executes BODY with ELEMENT subsequently bound to each element of
1015 SEQUENCE, then returns RETURN."
1016 (multiple-value-bind (forms decls
) (parse-body body nil
)
1017 (once-only ((sequence sequence
))
1018 (with-unique-names (state limit from-end step endp elt
)
1020 (seq-dispatch ,sequence
1021 (dolist (,element
,sequence
,return
) ,@body
)
1022 (do-vector-data (,element
,sequence
,return
) ,@body
)
1023 (multiple-value-bind (,state
,limit
,from-end
,step
,endp
,elt
)
1024 (sb-sequence:make-sequence-iterator
,sequence
)
1025 (declare (function ,step
,endp
,elt
))
1026 (do ((,state
,state
(funcall ,step
,sequence
,state
,from-end
)))
1027 ((funcall ,endp
,sequence
,state
,limit
,from-end
)
1028 (let ((,element nil
))
1029 ,@(filter-dolist-declarations decls
)
1030 (declare (ignorable ,element
))
1032 (let ((,element
(funcall ,elt
,sequence
,state
)))
1037 (defmacro do-vector-subseq
((elt vector start end
) &body body
)
1038 (multiple-value-bind (forms decls
) (parse-body body nil
)
1039 (with-unique-names (index vec start-s end-s ref
)
1040 `(with-array-data ((,vec
,vector
)
1043 :check-fill-pointer t
)
1044 (let ((,ref
(sb-vm::%find-data-vector-reffer
,vec
)))
1045 (declare (function ,ref
))
1046 (do ((,index
,start-s
(1+ ,index
)))
1049 ,@(sb-impl::filter-dolist-declarations decls
)
1051 (let ((,elt
(funcall ,ref
,vec
,index
)))
1053 (tagbody ,@forms
))))))))
1055 (defmacro do-subsequence
((element sequence start end
) &body body
)
1056 "Executes BODY with ELEMENT subsequently bound to each element of
1057 SEQUENCE, then returns RETURN."
1058 (multiple-value-bind (forms decls
) (parse-body body nil
)
1059 (once-only ((sequence sequence
)
1062 (with-unique-names (state limit from-end step endp elt subsequence
)
1064 (seq-dispatch ,sequence
1065 (let ((,subsequence
,sequence
))
1066 (loop for i below
,start
1068 (unless ,subsequence
1069 (sequence-bounding-indices-bad-error ,sequence
,start
,end
))
1072 (loop for i from
,start below
,end
1074 (unless ,subsequence
1075 (sequence-bounding-indices-bad-error ,sequence
,start
,end
))
1076 (let ((,element
(pop ,subsequence
)))
1078 (loop for
,element in
,subsequence
1079 do
(progn ,@body
))))
1080 (do-vector-subseq (,element
,sequence
,start
,end
) ,@body
)
1081 (multiple-value-bind (,state
,limit
,from-end
,step
,endp
,elt
)
1082 (sb-sequence:make-sequence-iterator
,sequence
:start
,start
:end
,end
)
1083 (declare (function ,step
,endp
,elt
))
1084 (do ((,state
,state
(funcall ,step
,sequence
,state
,from-end
)))
1085 ((funcall ,endp
,sequence
,state
,limit
,from-end
)
1086 (let ((,element nil
))
1087 ,@(filter-dolist-declarations decls
)
1088 (declare (ignorable ,element
))))
1089 (let ((,element
(funcall ,elt
,sequence
,state
)))
1097 (defun concatenate (result-type &rest sequences
)
1098 "Return a new sequence of all the argument sequences concatenated together
1099 which shares no structure with the original argument sequences of the
1100 specified RESULT-TYPE."
1101 (declare (explicit-check)
1102 (dynamic-extent sequences
))
1103 (flet ((concat-to-simple* (type-spec sequences
)
1104 (do ((seqs sequences
(cdr seqs
))
1108 (do ((sequences sequences
(cdr sequences
))
1109 (lengths lengths
(cdr lengths
))
1111 (result (make-sequence type-spec total-length
)))
1112 ((= index total-length
) result
)
1113 (declare (fixnum index
))
1114 (let ((sequence (car sequences
)))
1115 (sb-sequence:dosequence
(e sequence
)
1116 (setf (aref result index
) e
)
1118 (let ((length (length (car seqs
))))
1119 (declare (fixnum length
))
1120 (setq lengths
(nconc lengths
(list length
)))
1121 (setq total-length
(+ total-length length
))))))
1123 ;; Pick up some common cases first
1125 (apply #'%concatenate-to-list sequences
))
1126 ((vector simple-vector
)
1127 (apply #'%concatenate-to-simple-vector sequences
))
1129 ((string simple-string
)
1130 (apply #'%concatenate-to-string sequences
))
1131 ((simple-base-string #-sb-unicode string
#-sb-unicode simple-string
)
1132 (apply #'%concatenate-to-base-string sequences
))
1134 (let ((type (specifier-type result-type
)))
1136 ((csubtypep type
(specifier-type 'list
))
1138 ((type= type
(specifier-type 'list
))
1139 (apply #'%concatenate-to-list sequences
))
1140 ((eq type
*empty-type
*)
1141 (bad-sequence-type-error nil
))
1142 ((type= type
(specifier-type 'null
))
1143 (unless (every #'emptyp sequences
)
1144 (sequence-type-length-mismatch-error
1145 type
(reduce #'+ sequences
:key
#'length
))) ; FIXME: circular list issues.
1148 (multiple-value-bind (min exactp
)
1149 (sb-kernel::cons-type-length-info type
)
1150 (let ((length (reduce #'+ sequences
:key
#'length
)))
1152 (unless (= length min
)
1153 (sequence-type-length-mismatch-error type length
))
1154 (unless (>= length min
)
1155 (sequence-type-length-mismatch-error type length
)))
1156 (apply #'%concatenate-to-list sequences
))))
1157 (t (sequence-type-too-hairy (type-specifier type
)))))
1158 ((csubtypep type
(specifier-type 'vector
))
1159 (concat-to-simple* result-type sequences
))
1160 ((when-extended-sequence-type
1161 (result-type type
:expandedp nil
:prototype prototype
)
1162 ;; This function has the EXPLICIT-CHECK declaration,
1163 ;; so we manually assert that it returns a SEQUENCE.
1164 (the extended-sequence
1165 (apply #'sb-sequence
:concatenate prototype sequences
))))
1167 (bad-sequence-type-error result-type
))))))))
1169 ;;; Efficient out-of-line concatenate for strings. Compiler transforms
1170 ;;; CONCATENATE 'STRING &co into these.
1171 (macrolet ((def (name element-type
&rest dispatch
)
1173 (defun ,name
(&rest sequences
)
1174 (declare (explicit-check)
1175 (optimize (sb-c:insert-array-bounds-checks
0)))
1177 (declare (index length
))
1178 (do-rest-arg ((seq) sequences
)
1179 (incf length
(length seq
)))
1180 (let ((result (make-array length
:element-type
',element-type
))
1182 (declare (index start
))
1183 (do-rest-arg ((seq) sequences
)
1184 (string-dispatch (,@dispatch t
)
1186 (let ((length (length seq
)))
1187 (replace result seq
:start1 start
)
1188 (incf start length
))))
1190 (defun ,(symbolicate name
"-SUBSEQ") (&rest sequences
)
1191 (declare (explicit-check)
1192 (optimize (sb-c:insert-array-bounds-checks
0)))
1194 (declare (index length
))
1195 (do-rest-arg ((arg index
) sequences
)
1196 (cond ((eq arg
'%subseq
)
1197 (let* ((seq (fast-&rest-nth
(incf index
) sequences
))
1198 (start (the index
(fast-&rest-nth
(incf index
) sequences
)))
1199 (end (the (or null index
) (fast-&rest-nth
(incf index
) sequences
)))
1200 (end (or end
(length seq
))))
1202 (sequence-bounding-indices-bad-error seq start end
))
1203 (incf length
(- end
(truly-the index start
)))))
1205 (incf length
(length arg
)))))
1206 (let ((result (make-array length
:element-type
',element-type
))
1208 (declare (index start
))
1209 (do-rest-arg ((arg index
) sequences
)
1210 (multiple-value-bind (seq start2 end2 length
)
1211 (cond ((eq arg
'%subseq
)
1212 (let* ((seq (fast-&rest-nth
(incf index
) sequences
))
1213 (start (truly-the index
(fast-&rest-nth
(incf index
) sequences
)))
1214 (end (truly-the (or null index
) (fast-&rest-nth
(incf index
) sequences
)))
1215 (end (or end
(length seq
))))
1216 (values seq start end
(- end start
))))
1219 (values (truly-the sequence arg
) 0 nil
(length arg
))))
1220 (string-dispatch (,@dispatch t
) seq
1221 (replace result seq
:start1 start
:start2 start2
:end2 end2
)
1222 (incf start length
))))
1225 (def %concatenate-to-string character
1226 (simple-array character
(*)) (simple-array base-char
(*)))
1227 (def %concatenate-to-base-string base-char
1228 (simple-array base-char
(*)) #+sb-unicode
(simple-array character
(*)))
1229 (def %concatenate-to-simple-vector t simple-vector
))
1231 (defun %concatenate-to-list
(&rest sequences
)
1232 (declare (explicit-check))
1233 (let* ((result (list nil
))
1235 (do-rest-arg ((sequence) sequences
)
1236 (sb-sequence:dosequence
(e sequence
)
1237 (setf splice
(cdr (rplacd splice
(list e
))))))
1240 (defun %concatenate-to-vector
(widetag &rest sequences
)
1241 (declare (explicit-check))
1243 (declare (index length
))
1244 (do-rest-arg ((seq) sequences
)
1245 (incf length
(length seq
)))
1246 (let* ((n-bits-shift (aref sb-vm
::%%simple-array-n-bits-shifts%% widetag
))
1247 (result (sb-vm::allocate-vector-with-widetag
1248 #+ubsan nil widetag length n-bits-shift
))
1249 (setter (the function
(svref %%data-vector-setters%% widetag
)))
1251 (declare (index index
))
1252 (do-rest-arg ((seq) sequences
)
1253 (sb-sequence:dosequence
(e seq
)
1254 (funcall setter result index e
)
1258 (defun %concatenate-to-list-subseq
(&rest sequences
)
1259 (declare (explicit-check))
1260 (let* ((result (list nil
))
1262 (do-rest-arg ((arg index
) sequences
)
1263 (multiple-value-bind (seq start2 end2
)
1264 (cond ((eq arg
'%subseq
)
1265 (let* ((seq (fast-&rest-nth
(incf index
) sequences
))
1266 (start (the index
(fast-&rest-nth
(incf index
) sequences
)))
1267 (end (the (or null index
) (fast-&rest-nth
(incf index
) sequences
))))
1268 (values seq start end
)))
1270 (values arg
0 nil
)))
1271 (do-subsequence (e seq start2 end2
)
1272 (setf splice
(cdr (rplacd splice
(list e
)))))))
1275 (defun %concatenate-to-vector-subseq
(widetag &rest sequences
)
1276 (declare (explicit-check))
1278 (declare (index length
))
1279 (do-rest-arg ((arg index
) sequences
)
1280 (cond ((eq arg
'%subseq
)
1281 (let* ((seq (fast-&rest-nth
(incf index
) sequences
))
1282 (start (the index
(fast-&rest-nth
(incf index
) sequences
)))
1283 (end (the (or null index
) (fast-&rest-nth
(incf index
) sequences
)))
1284 (end (or end
(length seq
))))
1286 (sequence-bounding-indices-bad-error seq start end
))
1287 (incf length
(- end
(truly-the index start
)))))
1289 (incf length
(length arg
)))))
1290 (let* ((n-bits-shift (aref sb-vm
::%%simple-array-n-bits-shifts%% widetag
))
1291 (result (sb-vm::allocate-vector-with-widetag
1292 #+ubsan nil widetag length n-bits-shift
))
1293 (setter (the function
(svref %%data-vector-setters%% widetag
)))
1295 (declare (index index
))
1296 (do-rest-arg ((arg rest-index
) sequences
)
1297 (multiple-value-bind (seq start2 end2
)
1298 (cond ((eq arg
'%subseq
)
1299 (let* ((seq (truly-the sequence
(fast-&rest-nth
(incf rest-index
) sequences
)))
1300 (start (truly-the index
(fast-&rest-nth
(incf rest-index
) sequences
)))
1301 (end (truly-the (or null index
) (fast-&rest-nth
(incf rest-index
) sequences
)))
1302 (end (or end
(length seq
))))
1303 (values seq start end
(- end start
))))
1305 (values arg
0 nil
)))
1306 (do-subsequence (e seq start2 end2
)
1307 (funcall setter result index e
)
1313 ;;; helper functions to handle arity-1 subcases of MAP
1314 (defun %map-to-list-arity-1
(fun sequence
)
1315 (declare (explicit-check))
1316 (declare (dynamic-extent fun
))
1317 (let ((reversed-result nil
)
1318 (really-fun (%coerce-callable-to-fun fun
)))
1319 (sb-sequence:dosequence
(element sequence
)
1320 (push (funcall really-fun element
)
1322 (nreverse reversed-result
)))
1323 (defun %map-to-simple-vector-arity-1
(fun sequence
)
1324 (declare (explicit-check))
1325 (declare (dynamic-extent fun
))
1326 (let ((result (make-array (length sequence
)))
1328 (really-fun (%coerce-callable-to-fun fun
)))
1329 (declare (type index index
))
1330 (sb-sequence:dosequence
(element sequence
)
1331 (setf (aref result index
)
1332 (funcall really-fun element
))
1335 (defun %map-for-effect-arity-1
(fun sequence
)
1336 (declare (explicit-check))
1337 (declare (dynamic-extent fun
))
1338 (let ((really-fun (%coerce-callable-to-fun fun
)))
1339 (sb-sequence:dosequence
(element sequence
)
1340 (funcall really-fun element
)))
1343 (declaim (maybe-inline %map-for-effect
))
1344 (defun %map-for-effect
(fun sequences
)
1345 (declare (type function fun
) (type list sequences
))
1346 (declare (dynamic-extent fun
))
1347 (let ((%sequences sequences
)
1348 (%iters
(mapcar (lambda (s)
1352 (multiple-value-list
1353 (sb-sequence:make-sequence-iterator s
))))
1355 (%apply-args
(make-list (length sequences
))))
1356 ;; this is almost efficient (except in the general case where we
1357 ;; trampoline to MAKE-SEQUENCE-ITERATOR; if we had DX allocation
1358 ;; of MAKE-LIST, the whole of %MAP would be cons-free.
1359 ;; TODO: on x86-64, we do have. Now see if the above remark is true.
1360 (declare (type list %sequences %iters %apply-args
))
1362 (do ((in-sequences %sequences
(cdr in-sequences
))
1363 (in-iters %iters
(cdr in-iters
))
1364 (in-apply-args %apply-args
(cdr in-apply-args
)))
1365 ((null in-sequences
) (apply fun %apply-args
))
1366 (let ((i (car in-iters
)))
1367 (declare (type (or list index
) i
))
1369 ((listp (car in-sequences
))
1371 (return-from %map-for-effect nil
)
1372 (setf (car in-apply-args
) (car i
)
1373 (car in-iters
) (cdr i
))))
1375 (let ((v (the vector
(car in-sequences
))))
1376 (if (>= i
(length v
))
1377 (return-from %map-for-effect nil
)
1378 (setf (car in-apply-args
) (aref v i
)
1379 (car in-iters
) (1+ i
)))))
1381 ;; While on one hand this could benefit from a zero-safety ds-bind,
1382 ;; on the other, why not coerce these tuples to vectors or structs?
1383 (destructuring-bind (state limit from-end step endp elt
&rest ignore
)
1385 (declare (type function step endp elt
)
1387 (let ((s (car in-sequences
)))
1388 (if (funcall endp s state limit from-end
)
1389 (return-from %map-for-effect nil
)
1391 (setf (car in-apply-args
) (funcall elt s state
))
1392 (setf (caar in-iters
) (funcall step s state from-end
)))))))))))))
1394 (declaim (start-block map %map
))
1396 (defun %map-to-list
(fun sequences
)
1397 (declare (type function fun
)
1398 (type list sequences
))
1399 (declare (dynamic-extent fun
))
1401 (flet ((f (&rest args
)
1402 (declare (dynamic-extent args
))
1403 (push (apply fun args
) result
)))
1404 (declare (dynamic-extent #'f
))
1405 (%map-for-effect
#'f sequences
))
1407 (defun %map-to-vector
(output-type-spec fun sequences
)
1408 (declare (type function fun
)
1409 (type list sequences
))
1410 (declare (dynamic-extent fun
))
1412 (flet ((f (&rest args
)
1413 (declare (dynamic-extent args
))
1414 (declare (ignore args
))
1416 (declare (dynamic-extent #'f
))
1417 (%map-for-effect
#'f sequences
))
1418 (let ((result (make-sequence output-type-spec min-len
))
1420 (declare (type (simple-array * (*)) result
))
1421 (flet ((f (&rest args
)
1422 (declare (dynamic-extent args
))
1423 (setf (aref result i
) (apply fun args
))
1425 (declare (dynamic-extent #'f
))
1426 (%map-for-effect
#'f sequences
))
1429 ;;; %MAP is just MAP without the final just-to-be-sure check that
1430 ;;; length of the output sequence matches any length specified
1432 (defun %map
(result-type function
&rest sequences
)
1433 (declare (explicit-check))
1434 (declare (dynamic-extent function sequences
))
1435 ;; Everything that we end up calling uses %COERCE-TO-CALLABLE
1436 ;; on FUNCTION so we don't need to declare it of type CALLABLE here.
1437 ;; Additionally all the arity-1 mappers use SEQ-DISPATCH which asserts
1438 ;; that the input is a SEQUENCE. Despite SEQ-DISPATCH being "less safe"
1439 ;; than SEQ-DISPATCH-CHECKING, both are in fact equally safe, because
1440 ;; the ARRAY case (which assumes that all arrays are vectors) utilizes
1441 ;; %WITH-ARRAY-DATA/FP which asserts that its input is a vector.
1442 (labels ((slower-map (type)
1443 (let ((really-fun (%coerce-callable-to-fun function
)))
1445 ((eq type
*empty-type
*)
1446 (%map-for-effect really-fun sequences
))
1447 ((csubtypep type
(specifier-type 'list
))
1448 (%map-to-list really-fun sequences
))
1449 ((csubtypep type
(specifier-type 'vector
))
1450 (%map-to-vector result-type really-fun sequences
))
1451 ((when-extended-sequence-type
1452 (result-type type
:expandedp nil
:prototype prototype
)
1453 ;; This function has the EXPLICIT-CHECK
1454 ;; declaration, so we manually assert that it
1455 ;; returns a SEQUENCE.
1456 (the extended-sequence
1457 (apply #'sb-sequence
:map
1458 prototype really-fun sequences
))))
1460 (bad-sequence-type-error result-type
))))))
1461 ;; Handle some easy cases faster
1462 (if (/= (length sequences
) 1)
1463 (slower-map (specifier-type result-type
))
1464 (let ((first-sequence (fast-&rest-nth
0 sequences
)))
1467 (%map-for-effect-arity-1 function first-sequence
))
1469 (%map-to-list-arity-1 function first-sequence
))
1470 ((vector simple-vector
)
1471 (%map-to-simple-vector-arity-1 function first-sequence
))
1473 (let ((type (specifier-type result-type
)))
1474 (cond ((eq type
*empty-type
*)
1475 (%map-for-effect-arity-1 function first-sequence
))
1476 ((csubtypep type
(specifier-type 'list
))
1477 (%map-to-list-arity-1 function first-sequence
))
1478 ((csubtypep type
(specifier-type '(vector t
)))
1479 (%map-to-simple-vector-arity-1 function first-sequence
))
1481 (slower-map type
))))))))))
1483 (defun map (result-type function first-sequence
&rest more-sequences
)
1484 (declare (explicit-check))
1485 (declare (dynamic-extent function
))
1487 (apply #'%map result-type function first-sequence more-sequences
)))
1488 (if (or (eq result-type
'nil
) (typep result result-type
))
1490 (sb-c::%type-check-error result result-type
'map
))))
1492 (declaim (end-block))
1496 (defmacro map-into-lambda
(sequences params
&body body
)
1497 (check-type sequences symbol
)
1498 `(flet ((f ,params
,@body
))
1499 (declare (dynamic-extent #'f
))
1500 ;; Note (MAP-INTO SEQ (LAMBDA () ...)) is a different animal,
1501 ;; hence the awkward flip between MAP and LOOP.
1503 (apply #'%map nil
#'f
,sequences
)
1506 ;;; seqtran can generate code which accesses the array of specialized
1507 ;;; functions, so we need the array for this, not a jump table.
1508 (!define-array-dispatch
:call vector-map-into
(data start end fun
&rest sequences
)
1509 ((declare (ignore fun sequences
))
1510 (unless (zerop (- end start
))
1511 (sb-c::%type-check-error
/c data
'nil-array-accessed-error nil
))
1513 (declare (type index start end
)
1515 (type list sequences
))
1516 (declare (explicit-check))
1517 (declare (dynamic-extent fun
))
1518 (let ((index start
))
1519 (declare (type index index
))
1521 (map-into-lambda sequences
(&rest args
)
1522 (declare (dynamic-extent args
))
1523 (when (eql index end
)
1524 (return-from mapping
))
1525 (setf (aref data index
) (apply fun args
))
1528 ;;; Uses the machinery of (MAP NIL ...). For non-vectors we avoid
1529 ;;; computing the length of the result sequence since we can detect
1530 ;;; the end during mapping (if MAP even gets that far).
1532 ;;; For each result type, define a mapping function which is
1533 ;;; responsible for replacing RESULT-SEQUENCE elements and for
1534 ;;; terminating itself if the end of RESULT-SEQUENCE is reached.
1535 ;;; The mapping function is defined with MAP-INTO-LAMBDA.
1537 ;;; MAP-INTO-LAMBDAs are optimized since they are the inner loops.
1538 ;;; Because we are manually doing bounds checking with known types,
1539 ;;; safety is turned off for vectors and lists but kept for generic
1541 (defun map-into (result-sequence function
&rest sequences
)
1542 (declare (dynamic-extent function
)
1544 (let ((really-fun (%coerce-callable-to-fun function
)))
1546 (etypecase result-sequence
1548 (with-array-data ((data result-sequence
) (start) (end)
1549 ;; MAP-INTO ignores fill pointer when mapping
1550 :check-fill-pointer nil
)
1551 (let ((new-end (truly-the index
(vector-map-into data start end really-fun sequences
))))
1552 (when (array-has-fill-pointer-p result-sequence
)
1553 (setf (%array-fill-pointer result-sequence
)
1554 (truly-the index
(- new-end start
)))))))
1556 (let ((node result-sequence
))
1557 (flet ((not-proper ()
1558 (error 'simple-type-error
1559 :format-control
"~a is not a proper list"
1560 :format-arguments
(list result-sequence
)
1561 :expected-type
'list
1562 :datum result-sequence
)))
1563 (if (and (= (length sequences
) 1)
1564 (eq result-sequence
(car sequences
)))
1565 (let ((list result-sequence
))
1566 (loop (typecase list
1571 (funcall really-fun
(car list
))))
1575 (map-into-lambda sequences
(&rest args
)
1576 (declare (dynamic-extent args
))
1581 (setf (car node
) (apply really-fun args
))
1582 (setf node
(cdr node
)))))))
1584 (multiple-value-bind (iter limit from-end step endp elt set
)
1585 (sb-sequence:make-sequence-iterator result-sequence
)
1586 (declare (ignore elt
) (type function step endp set
))
1587 (map-into-lambda sequences
(&rest args
)
1588 (declare (dynamic-extent args
) (optimize speed
))
1589 (when (funcall endp result-sequence iter limit from-end
)
1591 (funcall set
(apply really-fun args
) result-sequence iter
)
1592 (setf iter
(funcall step result-sequence iter from-end
))))))))
1597 (defmacro mumble-reduce
(function
1604 `(do ((index ,start
(1+ index
))
1605 (value ,initial-value
))
1606 ((>= index
,end
) value
)
1607 (setq value
(funcall ,function value
1608 (apply-key ,key
(,ref
,sequence index
))))))
1610 (defmacro mumble-reduce-from-end
(function
1617 `(do ((index (1- ,end
) (1- index
))
1618 (value ,initial-value
)
1619 (terminus (1- ,start
)))
1620 ((<= index terminus
) value
)
1621 (setq value
(funcall ,function
1622 (apply-key ,key
(,ref
,sequence index
))
1625 (defmacro list-reduce
(function
1632 `(let ((sequence (nthcdr ,start
,sequence
)))
1633 (do ((count (if ,ivp
,start
(1+ ,start
))
1635 (sequence (if ,ivp sequence
(cdr sequence
))
1637 (value (if ,ivp
,initial-value
(apply-key ,key
(car sequence
)))
1638 (funcall ,function value
(apply-key ,key
(car sequence
)))))
1639 ((>= count
,end
) value
))))
1641 (defmacro list-reduce-from-end
(function
1648 `(let ((sequence (nthcdr (- (length ,sequence
) ,end
)
1649 (reverse ,sequence
))))
1650 (do ((count (if ,ivp
,start
(1+ ,start
))
1652 (sequence (if ,ivp sequence
(cdr sequence
))
1654 (value (if ,ivp
,initial-value
(apply-key ,key
(car sequence
)))
1655 (funcall ,function
(apply-key ,key
(car sequence
)) value
)))
1656 ((>= count
,end
) value
))))
1658 (define-sequence-traverser reduce
(function sequence
&rest args
&key key
1659 from-end start end
(initial-value nil ivp
))
1660 (declare (type index start
)
1661 (dynamic-extent args
))
1662 (declare (explicit-check sequence
))
1663 (seq-dispatch-checking sequence
1664 (let ((end (or end length
)))
1665 (declare (type index end
))
1667 (if ivp initial-value
(funcall function
))
1669 (list-reduce-from-end function sequence key start end
1671 (list-reduce function sequence key start end
1672 initial-value ivp
))))
1673 (let ((end (or end length
)))
1674 (declare (type index end
))
1676 (if ivp initial-value
(funcall function
))
1680 (setq end
(1- (the fixnum end
)))
1681 (setq initial-value
(apply-key key
(aref sequence end
))))
1682 (mumble-reduce-from-end function sequence key start end
1683 initial-value aref
))
1686 (setq initial-value
(apply-key key
(aref sequence start
)))
1687 (setq start
(1+ start
)))
1688 (mumble-reduce function sequence key start end
1689 initial-value aref
)))))
1690 (apply #'sb-sequence
:reduce function sequence args
)))
1694 (defmacro mumble-delete
(pred)
1695 `(do ((index start
(1+ index
))
1698 ((or (= index
(the fixnum end
)) (= number-zapped count
))
1699 (do ((index index
(1+ index
)) ; Copy the rest of the vector.
1700 (jndex jndex
(1+ jndex
)))
1701 ((= index
(the fixnum length
))
1702 (shrink-vector sequence jndex
))
1703 (declare (fixnum index jndex
))
1704 (setf (aref sequence jndex
) (aref sequence index
))))
1705 (declare (fixnum index jndex number-zapped
))
1706 (setf (aref sequence jndex
) (aref sequence index
))
1708 (incf number-zapped
)
1711 (defmacro mumble-delete-from-end
(pred)
1712 `(do ((index (1- (the fixnum end
)) (1- index
)) ; Find the losers.
1716 (terminus (1- start
)))
1717 ((or (= index terminus
) (= number-zapped count
))
1718 (do ((losers losers
) ; Delete the losers.
1719 (index start
(1+ index
))
1721 ((or (null losers
) (= index
(the fixnum end
)))
1722 (do ((index index
(1+ index
)) ; Copy the rest of the vector.
1723 (jndex jndex
(1+ jndex
)))
1724 ((= index
(the fixnum length
))
1725 (shrink-vector sequence jndex
))
1726 (declare (fixnum index jndex
))
1727 (setf (aref sequence jndex
) (aref sequence index
))))
1728 (declare (fixnum index jndex
))
1729 (setf (aref sequence jndex
) (aref sequence index
))
1730 (if (= index
(the fixnum
(car losers
)))
1733 (declare (fixnum index number-zapped terminus
))
1734 (setq this-element
(aref sequence index
))
1736 (incf number-zapped
)
1737 (push index losers
))))
1739 (defmacro normal-mumble-delete
()
1742 (not (funcall test-not item
(apply-key key
(aref sequence index
))))
1743 (funcall test item
(apply-key key
(aref sequence index
))))))
1745 (defmacro normal-mumble-delete-from-end
()
1746 `(mumble-delete-from-end
1748 (not (funcall test-not item
(apply-key key this-element
)))
1749 (funcall test item
(apply-key key this-element
)))))
1751 (defmacro list-delete
(pred)
1752 `(let ((handle (cons nil sequence
)))
1753 (declare (dynamic-extent handle
))
1754 (do* ((previous (nthcdr start handle
))
1755 (current (cdr previous
) (cdr current
))
1756 (index start
(1+ index
))
1758 ((or (= index end
) (= number-zapped count
))
1760 (declare (index index number-zapped
))
1762 (rplacd previous
(cdr current
))
1763 (incf number-zapped
))
1767 (defmacro list-delete-from-end
(pred)
1768 `(let* ((reverse (nreverse sequence
))
1769 (handle (cons nil reverse
)))
1770 (declare (dynamic-extent handle
))
1771 (do* ((previous (nthcdr (- length end
) handle
))
1772 (current (cdr previous
) (cdr current
))
1773 (index start
(1+ index
))
1775 ((or (= index end
) (= number-zapped count
))
1776 (nreverse (cdr handle
)))
1777 (declare (index index number-zapped
))
1779 (rplacd previous
(cdr current
))
1780 (incf number-zapped
))
1784 (defmacro normal-list-delete
()
1787 (not (funcall test-not item
(apply-key key
(car current
))))
1788 (funcall test item
(apply-key key
(car current
))))))
1790 (defmacro normal-list-delete-from-end
()
1791 '(list-delete-from-end
1793 (not (funcall test-not item
(apply-key key
(car current
))))
1794 (funcall test item
(apply-key key
(car current
))))))
1796 (define-sequence-traverser delete
1797 (item sequence
&rest args
&key from-end test test-not start
1799 "Return a sequence formed by destructively removing the specified ITEM from
1800 the given SEQUENCE."
1801 (declare (type fixnum start
)
1802 (dynamic-extent args
))
1803 (declare (explicit-check sequence
:result
))
1804 (seq-dispatch-checking=>seq sequence
1805 (let ((end (or end length
)))
1806 (declare (type index end
))
1808 (normal-list-delete-from-end)
1809 (normal-list-delete)))
1810 (let ((end (or end length
)))
1811 (declare (type index end
))
1813 (normal-mumble-delete-from-end)
1814 (normal-mumble-delete)))
1815 (apply #'sb-sequence
:delete item sequence args
)))
1817 (defmacro if-mumble-delete
()
1819 (funcall predicate
(apply-key key
(aref sequence index
)))))
1821 (defmacro if-mumble-delete-from-end
()
1822 `(mumble-delete-from-end
1823 (funcall predicate
(apply-key key this-element
))))
1825 (defmacro if-list-delete
()
1827 (funcall predicate
(apply-key key
(car current
)))))
1829 (defmacro if-list-delete-from-end
()
1830 '(list-delete-from-end
1831 (funcall predicate
(apply-key key
(car current
)))))
1833 (define-sequence-traverser delete-if
1834 (predicate sequence
&rest args
&key from-end start key end count
)
1835 "Return a sequence formed by destructively removing the elements satisfying
1836 the specified PREDICATE from the given SEQUENCE."
1837 (declare (type fixnum start
)
1838 (dynamic-extent args
))
1839 (declare (explicit-check sequence
:result
))
1840 (seq-dispatch-checking=>seq sequence
1841 (let ((end (or end length
)))
1842 (declare (type index end
))
1844 (if-list-delete-from-end)
1846 (let ((end (or end length
)))
1847 (declare (type index end
))
1849 (if-mumble-delete-from-end)
1850 (if-mumble-delete)))
1851 (apply #'sb-sequence
:delete-if predicate sequence args
)))
1853 (defmacro if-not-mumble-delete
()
1855 (not (funcall predicate
(apply-key key
(aref sequence index
))))))
1857 (defmacro if-not-mumble-delete-from-end
()
1858 `(mumble-delete-from-end
1859 (not (funcall predicate
(apply-key key this-element
)))))
1861 (defmacro if-not-list-delete
()
1863 (not (funcall predicate
(apply-key key
(car current
))))))
1865 (defmacro if-not-list-delete-from-end
()
1866 '(list-delete-from-end
1867 (not (funcall predicate
(apply-key key
(car current
))))))
1869 (define-sequence-traverser delete-if-not
1870 (predicate sequence
&rest args
&key from-end start end key count
)
1871 "Return a sequence formed by destructively removing the elements not
1872 satisfying the specified PREDICATE from the given SEQUENCE."
1873 (declare (type fixnum start
)
1874 (dynamic-extent args
))
1875 (declare (explicit-check sequence
:result
))
1876 (seq-dispatch-checking=>seq sequence
1877 (let ((end (or end length
)))
1878 (declare (type index end
))
1880 (if-not-list-delete-from-end)
1881 (if-not-list-delete)))
1882 (let ((end (or end length
)))
1883 (declare (type index end
))
1885 (if-not-mumble-delete-from-end)
1886 (if-not-mumble-delete)))
1887 (apply #'sb-sequence
:delete-if-not predicate sequence args
)))
1891 ;;; MUMBLE-REMOVE-MACRO does not include (removes) each element that
1892 ;;; satisfies the predicate.
1893 (defmacro mumble-remove-macro
(bump left begin finish right pred
)
1894 `(do ((index ,begin
(,bump index
))
1896 (do ((index ,left
(,bump index
))
1897 (result (%make-sequence-like sequence length
)))
1898 ((= index
(the fixnum
,begin
)) result
)
1899 (declare (fixnum index
))
1900 (setf (aref result index
) (aref sequence index
))))
1904 ((or (= index
(the fixnum
,finish
))
1905 (= number-zapped count
))
1906 (do ((index index
(,bump index
))
1907 (new-index new-index
(,bump new-index
)))
1908 ((= index
(the fixnum
,right
)) (%shrink-vector result new-index
))
1909 (declare (fixnum index new-index
))
1910 (setf (aref result new-index
) (aref sequence index
))))
1911 (declare (fixnum index new-index number-zapped
))
1912 (setq this-element
(aref sequence index
))
1913 (cond (,pred
(incf number-zapped
))
1914 (t (setf (aref result new-index
) this-element
)
1915 (setq new-index
(,bump new-index
))))))
1917 (defmacro mumble-remove
(pred)
1918 `(mumble-remove-macro 1+ 0 start end length
,pred
))
1920 (defmacro mumble-remove-from-end
(pred)
1921 `(let ((sequence (copy-seq sequence
)))
1922 (mumble-delete-from-end ,pred
)))
1924 (defmacro normal-mumble-remove
()
1927 (not (funcall test-not item
(apply-key key this-element
)))
1928 (funcall test item
(apply-key key this-element
)))))
1930 (defmacro normal-mumble-remove-from-end
()
1931 `(mumble-remove-from-end
1933 (not (funcall test-not item
(apply-key key this-element
)))
1934 (funcall test item
(apply-key key this-element
)))))
1936 (defmacro if-mumble-remove
()
1937 `(mumble-remove (funcall predicate
(apply-key key this-element
))))
1939 (defmacro if-mumble-remove-from-end
()
1940 `(mumble-remove-from-end (funcall predicate
(apply-key key this-element
))))
1942 (defmacro if-not-mumble-remove
()
1943 `(mumble-remove (not (funcall predicate
(apply-key key this-element
)))))
1945 (defmacro if-not-mumble-remove-from-end
()
1946 `(mumble-remove-from-end
1947 (not (funcall predicate
(apply-key key this-element
)))))
1949 ;;; LIST-REMOVE-MACRO does not include (removes) each element that satisfies
1951 (defmacro list-remove-macro
(pred reverse?
)
1952 `(let* ((sequence ,(if reverse?
1953 '(reverse (the list sequence
))
1955 (%start
,(if reverse?
'(- length end
) 'start
))
1956 (%end
,(if reverse?
'(- length start
) 'end
))
1958 (tail (and (/= %end length
)
1959 (nthcdr %end sequence
)))
1960 (results ,(if reverse?
1961 ;; It's already copied by REVERSE, so it can
1964 (let* ((tail (nthcdr (1- %start
) sequence
))
1965 (remaining (cdr tail
)))
1966 (setf (cdr tail
) nil
)
1968 (rplacd splice sequence
)
1970 sequence remaining
)))
1972 `(do ((index 0 (1+ index
))
1973 (before-start splice
))
1974 ((= index
(the fixnum %start
)) before-start
)
1975 (declare (fixnum index
))
1977 (cdr (rplacd splice
(list (pop sequence
)))))))))
1978 (declare (dynamic-extent splice
))
1981 ((cond ((eq tail sequence
)
1982 (rplacd splice tail
)
1984 ((= number-zapped count
)
1985 (rplacd splice sequence
)
1988 '(nreverse (the list
(cdr results
)))
1990 (declare (index number-zapped
))
1991 (setf this-element
(pop sequence
))
1993 (incf number-zapped
)
1994 (setf splice
(cdr (rplacd splice
(list this-element
))))))))
1996 (defmacro list-remove
(pred)
1997 `(list-remove-macro ,pred nil
))
1999 (defmacro list-remove-from-end
(pred)
2000 `(list-remove-macro ,pred t
))
2002 (defmacro normal-list-remove
()
2005 (not (funcall test-not item
(apply-key key this-element
)))
2006 (funcall test item
(apply-key key this-element
)))))
2008 (defmacro normal-list-remove-from-end
()
2009 `(list-remove-from-end
2011 (not (funcall test-not item
(apply-key key this-element
)))
2012 (funcall test item
(apply-key key this-element
)))))
2014 (defmacro if-list-remove
()
2016 (funcall predicate
(apply-key key this-element
))))
2018 (defmacro if-list-remove-from-end
()
2019 `(list-remove-from-end
2020 (funcall predicate
(apply-key key this-element
))))
2022 (defmacro if-not-list-remove
()
2024 (not (funcall predicate
(apply-key key this-element
)))))
2026 (defmacro if-not-list-remove-from-end
()
2027 `(list-remove-from-end
2028 (not (funcall predicate
(apply-key key this-element
)))))
2030 (define-sequence-traverser remove
2031 (item sequence
&rest args
&key from-end test test-not start
2033 "Return a copy of SEQUENCE with elements satisfying the test (default is
2034 EQL) with ITEM removed."
2035 (declare (type fixnum start
)
2036 (dynamic-extent args
))
2037 (declare (explicit-check sequence
:result
))
2038 (seq-dispatch-checking=>seq sequence
2039 (let ((end (or end length
)))
2040 (declare (type index end
))
2042 (normal-list-remove-from-end)
2043 (normal-list-remove)))
2044 (let ((end (or end length
)))
2045 (declare (type index end
))
2047 (normal-mumble-remove-from-end)
2048 (normal-mumble-remove)))
2049 (apply #'sb-sequence
:remove item sequence args
)))
2051 (define-sequence-traverser remove-if
2052 (predicate sequence
&rest args
&key from-end start end count key
)
2053 "Return a copy of sequence with elements satisfying PREDICATE removed."
2054 (declare (type fixnum start
)
2055 (dynamic-extent args
))
2056 (declare (explicit-check sequence
:result
))
2057 (seq-dispatch-checking=>seq sequence
2058 (let ((end (or end length
)))
2059 (declare (type index end
))
2061 (if-list-remove-from-end)
2063 (let ((end (or end length
)))
2064 (declare (type index end
))
2066 (if-mumble-remove-from-end)
2067 (if-mumble-remove)))
2068 (apply #'sb-sequence
:remove-if predicate sequence args
)))
2070 (define-sequence-traverser remove-if-not
2071 (predicate sequence
&rest args
&key from-end start end count key
)
2072 "Return a copy of sequence with elements not satisfying PREDICATE removed."
2073 (declare (type fixnum start
)
2074 (dynamic-extent args
))
2075 (declare (explicit-check sequence
:result
))
2076 (seq-dispatch-checking=>seq sequence
2077 (let ((end (or end length
)))
2078 (declare (type index end
))
2080 (if-not-list-remove-from-end)
2081 (if-not-list-remove)))
2082 (let ((end (or end length
)))
2083 (declare (type index end
))
2085 (if-not-mumble-remove-from-end)
2086 (if-not-mumble-remove)))
2087 (apply #'sb-sequence
:remove-if-not predicate sequence args
)))
2089 ;;;; REMOVE-DUPLICATES
2091 (defun hash-table-test-p (fun)
2101 ;;; Remove duplicates from a list. If from-end, remove the later duplicates,
2102 ;;; not the earlier ones. Thus if we check from-end we don't copy an item
2103 ;;; if we look into the already copied structure (from after :start) and see
2104 ;;; the item. If we check from beginning we check into the rest of the
2105 ;;; original list up to the :end marker (this we have to do by running a
2106 ;;; do loop down the list that far and using our test.
2107 (defun list-remove-duplicates* (list test test-not start end key from-end
)
2108 (declare (fixnum start
)
2110 (let* ((result (list ())) ; Put a marker on the beginning to splice with.
2113 (length (length list
))
2114 (end (or end length
))
2115 (whole (= end length
))
2116 (hash (and (> (- end start
) 20)
2119 (hash-table-test-p test
)
2120 (make-hash-table :test test
:size
(- end start
))))
2121 (tail (and (not whole
)
2122 (nthcdr end list
))))
2123 (declare (dynamic-extent result
))
2124 (do ((index 0 (1+ index
)))
2126 (declare (fixnum index
))
2127 (setq splice
(cdr (rplacd splice
(list (car current
)))))
2128 (setq current
(cdr current
)))
2132 ;; The hash table contains links from values that are
2133 ;; already in result to the cons cell *preceding* theirs
2134 ;; in the list. That is, for each value v in the list,
2135 ;; v and (cadr (gethash v hash)) are equal under TEST.
2136 (let ((prev (gethash (car current
) hash
)))
2139 (setf (gethash (car current
) hash
) splice
)
2140 (setq splice
(cdr (rplacd splice
(list (car current
))))))
2142 (let* ((old (cdr prev
))
2145 (let ((next-val (car next
)))
2146 ;; (assert (eq (gethash next-val hash) old))
2147 (setf (cdr prev
) next
2148 (gethash next-val hash
) prev
2149 (gethash (car current
) hash
) splice
2150 splice
(cdr (rplacd splice
(list (car current
))))))
2151 (setf (car old
) (car current
)))))))
2152 (setq current
(cdr current
)))
2153 (let ((testp test
) ;; for with-member-test
2155 (with-member-test (member-test
2156 ((and (not from-end
)
2160 (lambda (x y key test
)
2161 (not (funcall (truly-the function test
) x
2162 (funcall (truly-the function key
) y
))))
2163 (lambda (x y key test
)
2164 (declare (ignore key
))
2165 (not (funcall (truly-the function test
) x y
))))
2167 (lambda (x y key test
)
2168 (funcall (truly-the function test
) x
2169 (funcall (truly-the function key
) y
)))
2170 (lambda (x y key test
)
2171 (declare (ignore key
))
2172 (funcall (truly-the function test
) x y
))))))
2173 (do ((copied (nthcdr start result
)))
2175 (let ((elt (car current
)))
2176 (when (cond (from-end
2177 (not (funcall member-test elt
(cdr copied
) key test
)))
2179 (not (funcall member-test elt
(cdr current
) key test
)))
2181 (do ((it (apply-key key elt
))
2182 (l (cdr current
) (cdr l
)))
2185 (when (funcall member-test it
(car l
) key test
)
2187 (setf splice
(cdr (rplacd splice
(list elt
))))))
2189 (rplacd splice tail
)
2192 (defun vector-remove-duplicates* (vector test test-not start end key from-end
2193 &optional
(length (length vector
)))
2194 (declare (vector vector
) (fixnum start length
))
2195 (when (null end
) (setf end
(length vector
)))
2196 (let ((result (%make-sequence-like vector length
))
2199 (declare (fixnum index jndex
))
2202 (setf (aref result index
) (aref vector index
))
2203 (setq index
(1+ index
)))
2206 (setq elt
(aref vector index
))
2207 (unless (or (and from-end
2209 (position (apply-key key elt
) result
2210 :start start
:end jndex
2211 :test-not test-not
:key key
)
2212 (position (apply-key key elt
) result
2213 :start start
:end jndex
2214 :test test
:key key
)))
2217 (position (apply-key key elt
) vector
2218 :start
(1+ index
) :end end
2219 :test-not test-not
:key key
)
2220 (position (apply-key key elt
) vector
2221 :start
(1+ index
) :end end
2222 :test test
:key key
))))
2223 (setf (aref result jndex
) elt
)
2224 (setq jndex
(1+ jndex
)))
2225 (setq index
(1+ index
)))
2228 (setf (aref result jndex
) (aref vector index
))
2229 (setq index
(1+ index
))
2230 (setq jndex
(1+ jndex
)))
2231 (%shrink-vector result jndex
)))
2233 (define-sequence-traverser remove-duplicates
2234 (sequence &rest args
&key test test-not start end from-end key
)
2235 "The elements of SEQUENCE are compared pairwise, and if any two match,
2236 the one occurring earlier is discarded, unless FROM-END is true, in
2237 which case the one later in the sequence is discarded. The resulting
2238 sequence is returned.
2240 The :TEST-NOT argument is deprecated."
2241 (declare (fixnum start
)
2242 (dynamic-extent args
))
2243 (declare (explicit-check sequence
:result
))
2244 (seq-dispatch-checking=>seq sequence
2246 (list-remove-duplicates* sequence test test-not
2247 start end key from-end
))
2248 (vector-remove-duplicates* sequence test test-not start end key from-end
)
2249 (apply #'sb-sequence
:remove-duplicates sequence args
)))
2251 ;;;; DELETE-DUPLICATES
2252 (defun list-delete-duplicates* (list test test-not key from-end start end
)
2253 (declare (index start
)
2255 (let* ((handle (cons nil list
))
2256 (from-end-start (and from-end
2257 (nthcdr (1+ start
) handle
)))
2258 (length (length list
))
2259 (end (or end length
))
2260 (tail (and (/= length
(truly-the fixnum end
))
2261 (nthcdr end list
))))
2262 (declare (dynamic-extent handle
))
2263 (do* ((previous (nthcdr start handle
))
2264 (current (cdr previous
) (cdr current
)))
2267 (if (do ((end (if from-end
2276 (not (funcall (truly-the function test-not
)
2277 (apply-key-function key
(car current
))
2278 (apply-key-function key
(car x
))))
2279 (funcall (truly-the function test
)
2280 (apply-key-function key
(car current
))
2281 (apply-key-function key
(car x
))))
2283 (rplacd previous
(cdr current
))
2286 (defun vector-delete-duplicates* (vector test test-not key from-end start end
2287 &optional
(length (length vector
)))
2288 (declare (vector vector
) (fixnum start length
))
2289 (when (null end
) (setf end
(length vector
)))
2290 (do ((index start
(1+ index
))
2293 (do ((index index
(1+ index
)) ; copy the rest of the vector
2294 (jndex jndex
(1+ jndex
)))
2296 (shrink-vector vector jndex
))
2297 (setf (aref vector jndex
) (aref vector index
))))
2298 (declare (fixnum index jndex
))
2299 (setf (aref vector jndex
) (aref vector index
))
2300 (unless (if test-not
2301 (position (apply-key key
(aref vector index
)) vector
:key key
2302 :start
(if from-end start
(1+ index
))
2303 :end
(if from-end jndex end
)
2305 (position (apply-key key
(aref vector index
)) vector
:key key
2306 :start
(if from-end start
(1+ index
))
2307 :end
(if from-end jndex end
)
2309 (setq jndex
(1+ jndex
)))))
2311 (define-sequence-traverser delete-duplicates
2312 (sequence &rest args
&key test test-not start end from-end key
)
2313 "The elements of SEQUENCE are examined, and if any two match, one is
2314 discarded. The resulting sequence, which may be formed by destroying the
2315 given sequence, is returned.
2317 The :TEST-NOT argument is deprecated."
2318 (declare (dynamic-extent args
))
2319 (declare (explicit-check sequence
:result
))
2320 (seq-dispatch-checking=>seq sequence
2322 (list-delete-duplicates* sequence test test-not
2323 key from-end start end
))
2324 (vector-delete-duplicates* sequence test test-not key from-end start end
)
2325 (apply #'sb-sequence
:delete-duplicates sequence args
)))
2329 (defun list-substitute* (pred new list start end count key test test-not old
)
2330 (declare (fixnum start end count
)
2331 (type (or null function
) key
)
2333 (let* ((result (list nil
))
2334 (test (or test-not test
))
2335 (test-not (or test-not
2339 (list list
)) ; Get a local list for a stepper.
2340 (declare (function test
))
2341 (do ((index 0 (1+ index
)))
2343 (declare (fixnum index
))
2344 (setf splice
(cdr (rplacd splice
(list (car list
))))
2346 (do ((index start
(1+ index
)))
2347 ((or (= index end
) (null list
) (= count
0)))
2348 (declare (fixnum index
))
2349 (setf elt
(car list
)
2353 (cond ((let* ((elt (apply-key key elt
))
2354 (value (if (eq pred
'normal
)
2355 (funcall test old elt
)
2356 (funcall test elt
))))
2366 (setf splice
(cdr (rplacd splice
(list (car list
))))
2370 ;;; Replace old with new in sequence moving from left to right by incrementer
2371 ;;; on each pass through the loop. Called by all three substitute functions.
2372 (defun vector-substitute* (pred new sequence incrementer left right length
2373 start end count key test test-not old
)
2374 (declare (fixnum start count end incrementer right
)
2375 (type (or null function
) key
))
2376 (let* ((result (make-vector-like sequence length nil
))
2377 (getter (the function
(svref %%data-vector-reffers%%
2378 (%other-pointer-widetag sequence
))))
2379 (setter (the function
(svref %%data-vector-setters%%
2380 (%other-pointer-widetag result
))))
2381 (test (or test-not test
))
2382 (test-not (or test-not
2385 (declare (fixnum index
)
2389 (funcall setter result index
2390 (funcall getter sequence index
))
2391 (incf index incrementer
))
2393 ((or (= index end
) (= count
0)))
2394 (setf elt
(funcall getter sequence index
))
2395 (funcall setter result index
2396 (cond ((let* ((elt (apply-key key elt
))
2397 (value (if (eq pred
'normal
)
2398 (funcall test old elt
)
2399 (funcall test elt
))))
2406 (incf index incrementer
))
2409 (funcall setter result index
2410 (funcall getter sequence index
))
2411 (incf index incrementer
))
2414 (defmacro subst-dispatch
(pred)
2415 `(seq-dispatch-checking=>seq sequence
2416 (let ((end (or end length
)))
2417 (declare (type index end
))
2419 (nreverse (list-substitute* ,pred
2422 (- (the fixnum length
)
2424 (- (the fixnum length
)
2426 count key test test-not old
))
2427 (list-substitute* ,pred
2428 new sequence start end count key test test-not
2431 (let ((end (or end length
)))
2432 (declare (type index end
))
2434 (vector-substitute* ,pred new sequence -
1 (1- (the fixnum length
))
2435 -
1 length
(1- (the fixnum end
))
2436 (1- (the fixnum start
))
2437 count key test test-not old
)
2438 (vector-substitute* ,pred new sequence
1 0 length length
2439 start end count key test test-not old
)))
2441 ;; FIXME: wow, this is an odd way to implement the dispatch. PRED
2442 ;; here is (QUOTE [NORMAL|IF|IF-NOT]). Not only is this pretty
2443 ;; pointless, but also LIST-SUBSTITUTE* and VECTOR-SUBSTITUTE*
2444 ;; dispatch once per element on PRED's run-time identity.
2446 ((normal) `(apply #'sb-sequence
:substitute new old sequence args
))
2447 ((if) `(apply #'sb-sequence
:substitute-if new predicate sequence args
))
2448 ((if-not) `(apply #'sb-sequence
:substitute-if-not new predicate sequence args
)))))
2450 (define-sequence-traverser substitute
2451 (new old sequence
&rest args
&key from-end test test-not
2452 start count end key
)
2453 "Return a sequence of the same kind as SEQUENCE with the same elements,
2454 except that all elements equal to OLD are replaced with NEW."
2455 (declare (type fixnum start
)
2456 (explicit-check sequence
:result
)
2457 (dynamic-extent args
))
2458 (subst-dispatch 'normal
))
2460 ;;;; SUBSTITUTE-IF, SUBSTITUTE-IF-NOT
2462 (define-sequence-traverser substitute-if
2463 (new predicate sequence
&rest args
&key from-end start end count key
)
2464 "Return a sequence of the same kind as SEQUENCE with the same elements
2465 except that all elements satisfying the PRED are replaced with NEW."
2466 (declare (type fixnum start
)
2467 (explicit-check sequence
:result
)
2468 (dynamic-extent args
))
2469 (let ((test predicate
)
2472 (subst-dispatch 'if
)))
2474 (define-sequence-traverser substitute-if-not
2475 (new predicate sequence
&rest args
&key from-end start end count key
)
2476 "Return a sequence of the same kind as SEQUENCE with the same elements
2477 except that all elements not satisfying the PRED are replaced with NEW."
2478 (declare (type fixnum start
)
2479 (explicit-check sequence
:result
)
2480 (dynamic-extent args
))
2481 (let ((test predicate
)
2484 (subst-dispatch 'if-not
)))
2488 (define-sequence-traverser nsubstitute
2489 (new old sequence
&rest args
&key from-end test test-not
2490 end count key start
)
2491 "Return a sequence of the same kind as SEQUENCE with the same elements
2492 except that all elements equal to OLD are replaced with NEW. SEQUENCE
2493 may be destructively modified."
2494 (declare (type fixnum start
)
2495 (dynamic-extent args
))
2496 (declare (explicit-check sequence
:result
))
2497 (seq-dispatch-checking=>seq sequence
2498 (let ((end (or end length
)))
2499 (declare (type index end
))
2501 (nreverse (nlist-substitute*
2502 new old
(nreverse (the list sequence
))
2503 test test-not
(- length end
) (- length start
)
2505 (nlist-substitute* new old sequence
2506 test test-not start end count key
)))
2507 (let ((end (or end length
)))
2508 (declare (type index end
))
2510 (nvector-substitute* new old sequence -
1
2511 test test-not
(1- end
) (1- start
) count key
)
2512 (nvector-substitute* new old sequence
1
2513 test test-not start end count key
)))
2514 (apply #'sb-sequence
:nsubstitute new old sequence args
)))
2516 (defun nlist-substitute* (new old sequence test test-not start end count key
)
2517 (declare (fixnum start count end
)
2518 (type (or null function
) key
))
2519 (do ((test (or test-not test
))
2520 (list (nthcdr start sequence
) (cdr list
))
2521 (index start
(1+ index
)))
2522 ((or (= index end
) (null list
) (= count
0)) sequence
)
2523 (declare (fixnum index
)
2525 (let ((value (funcall test old
(apply-key key
(car list
)))))
2532 (defun nvector-substitute* (new old sequence incrementer
2533 test test-not start end count key
)
2534 (declare (fixnum start count end
)
2535 (type (integer -
1 1) incrementer
)
2536 (type (or null function
) key
))
2537 (let* ((test (or test-not test
))
2538 (tag (%other-pointer-widetag sequence
))
2539 (getter (the function
(svref %%data-vector-reffers%% tag
)))
2540 (setter (the function
(svref %%data-vector-setters%% tag
))))
2541 (declare (function test
))
2542 (do ((index start
(+ index incrementer
)))
2543 ((or (= index end
) (= count
0)) sequence
)
2544 (declare (fixnum index
))
2545 (let* ((value (apply-key key
(funcall getter sequence index
)))
2546 (test (and (funcall test old value
) 0)))
2550 (funcall setter sequence index new
)
2553 ;;;; NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
2555 (define-sequence-traverser nsubstitute-if
2556 (new predicate sequence
&rest args
&key from-end start end count key
)
2557 "Return a sequence of the same kind as SEQUENCE with the same elements
2558 except that all elements satisfying PREDICATE are replaced with NEW.
2559 SEQUENCE may be destructively modified."
2560 (declare (type fixnum start
)
2561 (dynamic-extent args
))
2562 (declare (explicit-check sequence
:result
))
2563 (seq-dispatch-checking=>seq sequence
2564 (let ((end (or end length
)))
2565 (declare (type index end
))
2567 (nreverse (nlist-substitute-if*
2568 new predicate
(nreverse (the list sequence
))
2569 (- length end
) (- length start
) count key
))
2570 (nlist-substitute-if* new predicate sequence
2571 start end count key
)))
2572 (let ((end (or end length
)))
2573 (declare (type index end
))
2575 (nvector-substitute-if* new predicate sequence -
1
2576 (1- end
) (1- start
) count key
)
2577 (nvector-substitute-if* new predicate sequence
1
2578 start end count key
)))
2579 (apply #'sb-sequence
:nsubstitute-if new predicate sequence args
)))
2581 (defun nlist-substitute-if* (new test sequence start end count key
)
2582 (declare (type fixnum start end count
)
2583 (type (or null function
) key
)
2584 (type function test
)) ; coercion is done by caller
2585 (declare (dynamic-extent test key
))
2586 (do ((list (nthcdr start sequence
) (cdr list
))
2587 (index start
(1+ index
)))
2588 ((or (= index end
) (null list
) (= count
0)) sequence
)
2589 (declare (fixnum index
))
2590 (when (funcall test
(apply-key key
(car list
)))
2594 (defun nvector-substitute-if* (new test sequence incrementer
2595 start end count key
)
2596 (declare (type fixnum end count
)
2597 (type (integer -
1 1) incrementer
)
2598 (type (or null function
) key
)
2599 (type function test
)) ; coercion is done by caller
2600 (declare (dynamic-extent test key
))
2601 (let* ((tag (%other-pointer-widetag sequence
))
2602 (getter (the function
(svref %%data-vector-reffers%% tag
)))
2603 (setter (the function
(svref %%data-vector-setters%% tag
))))
2604 (do ((index start
(+ index incrementer
)))
2605 ((or (= index end
) (= count
0)) sequence
)
2606 (declare (fixnum index
))
2607 (when (funcall test
(apply-key key
(funcall getter sequence index
)))
2608 (funcall setter sequence index new
)
2611 (define-sequence-traverser nsubstitute-if-not
2612 (new predicate sequence
&rest args
&key from-end start end count key
)
2613 "Return a sequence of the same kind as SEQUENCE with the same elements
2614 except that all elements not satisfying PREDICATE are replaced with NEW.
2615 SEQUENCE may be destructively modified."
2616 (declare (type fixnum start
)
2617 (dynamic-extent args
))
2618 (declare (explicit-check sequence
:result
))
2619 (seq-dispatch-checking=>seq sequence
2620 (let ((end (or end length
)))
2621 (declare (fixnum end
))
2623 (nreverse (nlist-substitute-if-not*
2624 new predicate
(nreverse (the list sequence
))
2625 (- length end
) (- length start
) count key
))
2626 (nlist-substitute-if-not* new predicate sequence
2627 start end count key
)))
2628 (let ((end (or end length
)))
2629 (declare (fixnum end
))
2631 (nvector-substitute-if-not* new predicate sequence -
1
2632 (1- end
) (1- start
) count key
)
2633 (nvector-substitute-if-not* new predicate sequence
1
2634 start end count key
)))
2635 (apply #'sb-sequence
:nsubstitute-if-not new predicate sequence args
)))
2637 (defun nlist-substitute-if-not* (new test sequence start end count key
)
2638 (declare (type fixnum start end count
)
2639 (type (or null function
) key
)
2640 (type function test
)) ; coercion is done by caller
2641 (declare (dynamic-extent test key
))
2642 (do ((list (nthcdr start sequence
) (cdr list
))
2643 (index start
(1+ index
)))
2644 ((or (= index end
) (null list
) (= count
0)) sequence
)
2645 (declare (fixnum index
))
2646 (when (not (funcall test
(apply-key key
(car list
))))
2650 (defun nvector-substitute-if-not* (new test sequence incrementer
2651 start end count key
)
2652 (declare (type fixnum end count
)
2653 (type (integer -
1 1) incrementer
)
2654 (type (or null function
) key
)
2655 (type function test
)) ; coercion is done by caller
2656 (declare (dynamic-extent test key
))
2657 (let* ((tag (%other-pointer-widetag sequence
))
2658 (getter (the function
(svref %%data-vector-reffers%% tag
)))
2659 (setter (the function
(svref %%data-vector-setters%% tag
))))
2660 (do ((index start
(+ index incrementer
)))
2661 ((or (= index end
) (= count
0)) sequence
)
2662 (declare (fixnum index
))
2663 (when (not (funcall test
(apply-key key
(funcall getter sequence index
))))
2664 (funcall setter sequence index new
)
2667 ;;;; FIND, POSITION, and their -IF and -IF-NOT variants
2669 (defun effective-find-position-test (test test-not
)
2670 (effective-find-position-test test test-not
))
2671 (defun effective-find-position-key (key)
2672 (effective-find-position-key key
))
2674 ;;; shared guts of out-of-line FIND, POSITION, FIND-IF, and POSITION-IF
2675 (macrolet (;; shared logic for defining %FIND-POSITION and
2676 ;; %FIND-POSITION-IF in terms of various inlineable cases
2677 ;; of the expression defined in FROB and VECTOR*-FROB
2678 (frobs (&optional bit-frob
)
2679 `(seq-dispatch-checking sequence-arg
2680 (frob sequence-arg from-end
)
2681 (with-array-data ((sequence sequence-arg
:offset-var offset
)
2684 :check-fill-pointer t
)
2687 ((simple-array character
(*))
2688 (vector*-frob sequence
))
2689 ((simple-array base-char
(*))
2690 (vector*-frob sequence
))
2692 `((simple-bit-vector
2693 (if (and (typep item
'bit
)
2698 (let ((p (%bit-position item sequence
2699 from-end start end
)))
2701 (values item
(truly-the index
(- p offset
)))
2703 (vector*-frob sequence
)))))
2705 (vector*-frob sequence
))))
2706 ;; EXTENDED-SEQUENCE is not allowed.
2708 (defun %find-position
(item sequence-arg from-end start end key test
)
2709 (declare (explicit-check sequence-arg
))
2710 (declare (dynamic-extent test key
))
2711 (macrolet ((frob (sequence from-end
)
2712 `(%find-position item
,sequence
2713 ,from-end start end key test
))
2714 (vector*-frob
(sequence)
2715 `(%find-position-vector-macro item
,sequence
2716 from-end start end key test offset
)))
2718 (defun %find-position-if
(predicate sequence-arg from-end start end key
)
2719 (declare (explicit-check sequence-arg
))
2720 (declare (dynamic-extent predicate key
))
2721 (macrolet ((frob (sequence from-end
)
2722 `(%find-position-if predicate
,sequence
2723 ,from-end start end key
))
2724 (vector*-frob
(sequence)
2725 `(%find-position-if-vector-macro predicate
,sequence
2726 from-end start end key offset
)))
2728 (defun %find-position-if-not
(predicate sequence-arg from-end start end key
)
2729 (declare (explicit-check sequence-arg
))
2730 (declare (dynamic-extent predicate key
))
2731 (macrolet ((frob (sequence from-end
)
2732 `(%find-position-if-not predicate
,sequence
2733 ,from-end start end key
))
2734 (vector*-frob
(sequence)
2735 `(%find-position-if-not-vector-macro predicate
,sequence
2736 from-end start end key offset
)))
2740 (item sequence
&rest args
&key from-end
(start 0) end key test test-not
)
2741 (declare (dynamic-extent args
))
2742 (declare (dynamic-extent key test test-not
))
2743 (declare (explicit-check sequence
))
2744 (seq-dispatch-checking sequence
2745 (nth-value 0 (%find-position
2746 item sequence from-end start end
2747 (effective-find-position-key key
)
2748 (effective-find-position-test test test-not
)))
2749 (nth-value 0 (%find-position
2750 item sequence from-end start end
2751 (effective-find-position-key key
)
2752 (effective-find-position-test test test-not
)))
2753 (apply #'sb-sequence
:find item sequence args
)))
2755 (item sequence
&rest args
&key from-end
(start 0) end key test test-not
)
2756 (declare (dynamic-extent args
))
2757 (declare (dynamic-extent key test test-not
))
2758 (declare (explicit-check sequence
))
2759 (seq-dispatch-checking sequence
2760 (nth-value 1 (%find-position
2761 item sequence from-end start end
2762 (effective-find-position-key key
)
2763 (effective-find-position-test test test-not
)))
2764 (nth-value 1 (%find-position
2765 item sequence from-end start end
2766 (effective-find-position-key key
)
2767 (effective-find-position-test test test-not
)))
2768 (apply #'sb-sequence
:position item sequence args
)))
2770 (defun find-if (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2771 (declare (dynamic-extent args
))
2772 (declare (explicit-check sequence
))
2773 (declare (dynamic-extent predicate key
))
2774 (seq-dispatch-checking sequence
2775 (nth-value 0 (%find-position-if
2776 (%coerce-callable-to-fun predicate
)
2777 sequence from-end start end
2778 (effective-find-position-key key
)))
2779 (nth-value 0 (%find-position-if
2780 (%coerce-callable-to-fun predicate
)
2781 sequence from-end start end
2782 (effective-find-position-key key
)))
2783 (apply #'sb-sequence
:find-if predicate sequence args
)))
2785 (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2786 (declare (dynamic-extent args
))
2787 (declare (explicit-check sequence
))
2788 (declare (dynamic-extent predicate key
))
2789 (seq-dispatch-checking sequence
2790 (nth-value 1 (%find-position-if
2791 (%coerce-callable-to-fun predicate
)
2792 sequence from-end start end
2793 (effective-find-position-key key
)))
2794 (nth-value 1 (%find-position-if
2795 (%coerce-callable-to-fun predicate
)
2796 sequence from-end start end
2797 (effective-find-position-key key
)))
2798 (apply #'sb-sequence
:position-if predicate sequence args
)))
2801 (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2802 (declare (dynamic-extent args
))
2803 (declare (explicit-check sequence
))
2804 (declare (dynamic-extent predicate key
))
2805 (seq-dispatch-checking sequence
2806 (nth-value 0 (%find-position-if-not
2807 (%coerce-callable-to-fun predicate
)
2808 sequence from-end start end
2809 (effective-find-position-key key
)))
2810 (nth-value 0 (%find-position-if-not
2811 (%coerce-callable-to-fun predicate
)
2812 sequence from-end start end
2813 (effective-find-position-key key
)))
2814 (apply #'sb-sequence
:find-if-not predicate sequence args
)))
2815 (defun position-if-not
2816 (predicate sequence
&rest args
&key from-end
(start 0) end key
)
2817 (declare (dynamic-extent args
))
2818 (declare (explicit-check sequence
))
2819 (declare (dynamic-extent predicate key
))
2820 (seq-dispatch-checking sequence
2821 (nth-value 1 (%find-position-if-not
2822 (%coerce-callable-to-fun predicate
)
2823 sequence from-end start end
2824 (effective-find-position-key key
)))
2825 (nth-value 1 (%find-position-if-not
2826 (%coerce-callable-to-fun predicate
)
2827 sequence from-end start end
2828 (effective-find-position-key key
)))
2829 (apply #'sb-sequence
:position-if-not predicate sequence args
)))
2831 ;;;; COUNT-IF, COUNT-IF-NOT, and COUNT
2833 (defmacro vector-count-if
(notp from-end-p predicate sequence
2834 &key two-arg-predicate
)
2835 (let ((next-index (if from-end-p
'(1- index
) '(1+ index
)))
2836 (pred (if two-arg-predicate
2837 `(funcall ,predicate
,two-arg-predicate
(apply-key key
(aref ,sequence index
)))
2838 `(funcall ,predicate
(apply-key key
(aref ,sequence index
))))))
2839 `(let ((%start
,(if from-end-p
'(1- end
) 'start
))
2840 (%end
,(if from-end-p
'(1- start
) 'end
)))
2841 (do ((index %start
,next-index
)
2843 ((= index
(the fixnum %end
)) count
)
2844 (declare (fixnum index count
))
2845 ,(if two-arg-predicate
2849 (setq count
(1+ count
)))
2850 `(,(if notp
'unless
'when
) ,pred
2851 (setq count
(1+ count
))))))))
2853 (defmacro list-count-if
(notp from-end-p predicate sequence
2854 &key two-arg-predicate
)
2855 (let ((pred (if two-arg-predicate
2856 `(funcall ,predicate
,two-arg-predicate
(apply-key key
(pop sequence
)))
2857 `(funcall ,predicate
(apply-key key
(pop sequence
))))))
2858 `(let ((%start
,(if from-end-p
'(- length end
) 'start
))
2859 (%end
,(if from-end-p
'(- length start
) 'end
))
2860 (sequence ,(if from-end-p
'(reverse sequence
) 'sequence
)))
2861 (do ((sequence (nthcdr %start
,sequence
))
2862 (index %start
(1+ index
))
2864 ((or (= index
(the fixnum %end
)) (null sequence
)) count
)
2865 (declare (fixnum index count
))
2866 ,(if two-arg-predicate
2870 (setq count
(1+ count
)))
2871 `(,(if notp
'unless
'when
) ,pred
2872 (setq count
(1+ count
))))))))
2874 (define-sequence-traverser count-if
2875 (predicate sequence
&rest args
&key from-end start end key
)
2876 "Return the number of elements in SEQUENCE satisfying PRED(el)."
2877 (declare (type fixnum start
)
2878 (dynamic-extent args
))
2879 (declare (explicit-check sequence
))
2880 (seq-dispatch-checking sequence
2881 (let ((end (or end length
)))
2882 (declare (type index end
))
2884 (list-count-if nil t predicate sequence
)
2885 (list-count-if nil nil predicate sequence
)))
2886 (let ((end (or end length
)))
2887 (declare (type index end
))
2889 (vector-count-if nil t predicate sequence
)
2890 (vector-count-if nil nil predicate sequence
)))
2891 (apply #'sb-sequence
:count-if predicate sequence args
)))
2893 (define-sequence-traverser count-if-not
2894 (predicate sequence
&rest args
&key from-end start end key
)
2895 "Return the number of elements in SEQUENCE not satisfying TEST(el)."
2896 (declare (type fixnum start
)
2897 (dynamic-extent args
))
2898 (declare (explicit-check sequence
))
2899 (seq-dispatch-checking sequence
2900 (let ((end (or end length
)))
2901 (declare (type index end
))
2903 (list-count-if t t predicate sequence
)
2904 (list-count-if t nil predicate sequence
)))
2905 (let ((end (or end length
)))
2906 (declare (type index end
))
2908 (vector-count-if t t predicate sequence
)
2909 (vector-count-if t nil predicate sequence
)))
2910 (apply #'sb-sequence
:count-if-not predicate sequence args
)))
2912 (define-sequence-traverser count
2913 (item sequence
&rest args
&key from-end start end
2914 ;; FIXME: TEST and TEST-NOT are not eagerly coerced to functions
2915 ;; because DEFINE-SEQUENCE-TRAVERSER does not see the arg name-
2916 ;; it expects only symbols as args.
2917 key
(test #'eql test-p
) (test-not nil test-not-p
))
2918 "Return the number of elements in SEQUENCE satisfying a test with ITEM,
2919 which defaults to EQL."
2920 (declare (type fixnum start
)
2921 (dynamic-extent args
))
2922 (declare (explicit-check sequence
))
2923 (when (and test-p test-not-p
)
2924 ;; Use the same wording as EFFECTIVE-FIND-POSITION-TEST
2925 (error "can't specify both :TEST and :TEST-NOT"))
2926 (let ((test (%coerce-callable-to-fun
(or test-not test
))))
2927 (seq-dispatch-checking sequence
2928 (let ((end (or end length
)))
2929 (declare (type index end
))
2931 (list-count-if test-not-p t test sequence
:two-arg-predicate item
)
2932 (list-count-if test-not-p nil test sequence
:two-arg-predicate item
)))
2933 (let ((end (or end length
)))
2934 (declare (type index end
))
2936 (vector-count-if test-not-p t test sequence
:two-arg-predicate item
)
2937 (vector-count-if test-not-p nil test sequence
:two-arg-predicate item
)))
2938 (apply #'sb-sequence
:count item sequence args
))))
2942 (defmacro match-vars
(&rest body
)
2943 `(let ((inc (if from-end -
1 1))
2944 (start1 (if from-end
(1- (the fixnum end1
)) start1
))
2945 (start2 (if from-end
(1- (the fixnum end2
)) start2
))
2946 (end1 (if from-end
(1- (the fixnum start1
)) end1
))
2947 (end2 (if from-end
(1- (the fixnum start2
)) end2
)))
2948 (declare (fixnum inc start1 start2 end1 end2
))
2951 (defmacro matchify-list
((sequence start length end
) &body body
)
2952 (declare (ignore end
)) ;; ### Should END be used below?
2953 `(let ((,sequence
(if from-end
2954 (nthcdr (- (the fixnum
,length
) (the fixnum
,start
) 1)
2955 (reverse (the list
,sequence
)))
2956 (nthcdr ,start
,sequence
))))
2957 (declare (type list
,sequence
))
2960 (defmacro if-mismatch
(elt1 elt2
)
2961 `(cond ((= (the fixnum index1
) (the fixnum end1
))
2962 (return (if (= (the fixnum index2
) (the fixnum end2
))
2965 (1+ (the fixnum index1
))
2966 (the fixnum index1
)))))
2967 ((= (the fixnum index2
) (the fixnum end2
))
2968 (return (if from-end
(1+ (the fixnum index1
)) index1
)))
2970 (if (funcall test-not
(apply-key key
,elt1
) (apply-key key
,elt2
))
2971 (return (if from-end
(1+ (the fixnum index1
)) index1
))))
2972 (t (if (not (funcall test
(apply-key key
,elt1
)
2973 (apply-key key
,elt2
)))
2974 (return (if from-end
(1+ (the fixnum index1
)) index1
))))))
2976 (defmacro mumble-mumble-mismatch
()
2977 `(do ((index1 start1
(+ index1
(the fixnum inc
)))
2978 (index2 start2
(+ index2
(the fixnum inc
))))
2980 (declare (fixnum index1 index2
))
2981 (if-mismatch (aref sequence1 index1
) (aref sequence2 index2
))))
2983 (defmacro mumble-list-mismatch
()
2984 `(do ((index1 start1
(+ index1
(the fixnum inc
)))
2985 (index2 start2
(+ index2
(the fixnum inc
))))
2987 (declare (fixnum index1 index2
))
2988 (if-mismatch (aref sequence1 index1
) (pop sequence2
))))
2990 (defmacro list-mumble-mismatch
()
2991 `(do ((index1 start1
(+ index1
(the fixnum inc
)))
2992 (index2 start2
(+ index2
(the fixnum inc
))))
2994 (declare (fixnum index1 index2
))
2995 (if-mismatch (pop sequence1
) (aref sequence2 index2
))))
2997 (defmacro list-list-mismatch
()
2998 `(do ((sequence1 sequence1
)
2999 (sequence2 sequence2
)
3000 (index1 start1
(+ index1
(the fixnum inc
)))
3001 (index2 start2
(+ index2
(the fixnum inc
))))
3003 (declare (fixnum index1 index2
))
3004 (if-mismatch (pop sequence1
) (pop sequence2
))))
3006 (define-sequence-traverser mismatch
3007 (sequence1 sequence2
&rest args
&key from-end test test-not
3008 start1 end1 start2 end2 key
)
3009 "The specified subsequences of SEQUENCE1 and SEQUENCE2 are compared
3010 element-wise. If they are of equal length and match in every element, the
3011 result is NIL. Otherwise, the result is a non-negative integer, the index
3012 within SEQUENCE1 of the leftmost position at which they fail to match; or,
3013 if one is shorter than and a matching prefix of the other, the index within
3014 SEQUENCE1 beyond the last position tested is returned. If a non-NIL
3015 :FROM-END argument is given, then one plus the index of the rightmost
3016 position in which the sequences differ is returned."
3017 (declare (type fixnum start1 start2
))
3018 (declare (dynamic-extent args
))
3019 (declare (explicit-check sequence1 sequence2
:result
))
3020 (seq-dispatch-checking sequence1
3021 (seq-dispatch-checking sequence2
3022 (return-from mismatch
3023 (let ((end1 (or end1 length1
))
3024 (end2 (or end2 length2
)))
3025 (declare (type index end1 end2
))
3027 (matchify-list (sequence1 start1 length1 end1
)
3028 (matchify-list (sequence2 start2 length2 end2
)
3029 (list-list-mismatch))))))
3030 (return-from mismatch
3031 (let ((end1 (or end1 length1
))
3032 (end2 (or end2 length2
)))
3033 (declare (type index end1 end2
))
3035 (matchify-list (sequence1 start1 length1 end1
)
3036 (list-mumble-mismatch)))))
3038 (seq-dispatch-checking sequence2
3039 (return-from mismatch
3040 (let ((end1 (or end1 length1
))
3041 (end2 (or end2 length2
)))
3042 (declare (type index end1 end2
))
3044 (matchify-list (sequence2 start2 length2 end2
)
3045 (mumble-list-mismatch)))))
3046 (return-from mismatch
3047 (let ((end1 (or end1 length1
))
3048 (end2 (or end2 length2
)))
3049 (declare (type index end1 end2
))
3051 (mumble-mumble-mismatch))))
3054 ;; If sequence1 is an extended-sequence, we know nothing about sequence2.
3055 ;; If sequence1 was a list or vector, then sequence2 is an extended-sequence
3056 ;; or not a sequence. Either way, check it.
3057 (the (or index null
)
3058 (values (apply #'sb-sequence
:mismatch sequence1
3059 (the sequence sequence2
) args
))))
3061 ;;; search comparison functions
3063 ;;; Compare two elements and return if they don't match.
3064 (defmacro compare-elements
(elt1 elt2
)
3066 (if (funcall test-not
(apply-key key
,elt1
) (apply-key key
,elt2
))
3069 (if (not (funcall test
(apply-key key
,elt1
) (apply-key key
,elt2
)))
3073 (defmacro search-compare-list-list
(main sub
)
3074 `(do ((main ,main
(cdr main
))
3075 (jndex start1
(1+ jndex
))
3076 (sub (nthcdr start1
,sub
) (cdr sub
)))
3077 ((or (endp main
) (endp sub
) (<= end1 jndex
))
3079 (declare (type (integer 0) jndex
))
3080 (compare-elements (car sub
) (car main
))))
3082 (defmacro search-compare-list-vector
(main sub
)
3083 `(do ((main ,main
(cdr main
))
3084 (index start1
(1+ index
)))
3085 ((or (endp main
) (= index end1
)) t
)
3086 (compare-elements (aref ,sub index
) (car main
))))
3088 (defmacro search-compare-vector-list
(main sub index
)
3089 `(do ((sub (nthcdr start1
,sub
) (cdr sub
))
3090 (jndex start1
(1+ jndex
))
3091 (index ,index
(1+ index
)))
3092 ((or (<= end1 jndex
) (endp sub
)) t
)
3093 (declare (type (integer 0) jndex
))
3094 (compare-elements (car sub
) (aref ,main index
))))
3096 (defmacro search-compare-vector-vector
(main sub index
)
3097 `(do ((index ,index
(1+ index
))
3098 (sub-index start1
(1+ sub-index
)))
3099 ((= sub-index end1
) t
)
3100 (compare-elements (aref ,sub sub-index
) (aref ,main index
))))
3102 (defmacro search-compare
(main-type main sub index
)
3103 (if (eq main-type
'list
)
3105 (search-compare-list-list ,main
,sub
)
3106 (search-compare-list-vector ,main
,sub
)
3107 ;; KLUDGE: just hack it together so that it works
3108 (return-from search
(apply #'sb-sequence
:search
,sub
,main args
)))
3110 (search-compare-vector-list ,main
,sub
,index
)
3111 (search-compare-vector-vector ,main
,sub
,index
)
3112 (return-from search
(apply #'sb-sequence
:search
,sub
,main args
)))))
3116 (defmacro list-search
(main sub
)
3117 `(do ((main (nthcdr start2
,main
) (cdr main
))
3118 (index2 start2
(1+ index2
))
3119 (terminus (- end2
(the (integer 0) (- end1 start1
))))
3121 ((> index2 terminus
) last-match
)
3122 (declare (type (integer 0) index2
))
3123 (if (search-compare list main
,sub index2
)
3125 (setq last-match index2
)
3128 (defmacro vector-search
(main sub
)
3129 `(do ((index2 start2
(1+ index2
))
3130 (terminus (- end2
(the (integer 0) (- end1 start1
))))
3132 ((> index2 terminus
) last-match
)
3133 (declare (type (integer 0) index2
))
3134 (if (search-compare vector
,main
,sub index2
)
3136 (setq last-match index2
)
3139 (define-sequence-traverser search
3140 (sub-sequence1 main-sequence2
&rest args
&key
3141 from-end test test-not start1 end1 start2 end2 key
)
3142 (declare (type fixnum start1 start2
)
3143 (dynamic-extent args
))
3144 (declare (explicit-check main-sequence2
))
3145 (seq-dispatch-checking main-sequence2
3146 (let ((end1 (or end1 length1
))
3147 (end2 (or end2 length2
)))
3148 (declare (type index end1 end2
))
3149 (list-search main-sequence2 sub-sequence1
))
3150 (let ((end1 (or end1 length1
))
3151 (end2 (or end2 length2
)))
3152 (declare (type index end1 end2
))
3153 (vector-search main-sequence2 sub-sequence1
))
3154 (apply #'sb-sequence
:search sub-sequence1 main-sequence2 args
)))
3156 ;;; FIXME: this was originally in array.lisp; it might be better to
3157 ;;; put it back there, and make DOSEQUENCE and SEQ-DISPATCH be in
3158 ;;; a new early-seq.lisp file.
3159 (macrolet ((body (lambda-list endp-test start-recursion next-layer
)
3161 (labels ((frob ,lambda-list
3163 (setf (aref vector index
) contents
)
3166 (unless (typep contents
'sequence
)
3167 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
3168 sequence, but ~W more layer~:P needed."
3170 (- (length dimensions
) axis
)))
3171 (let ((k this-dimension
)
3172 (l (length contents
)))
3174 (error "malformed :INITIAL-CONTENTS: Dimension of ~
3175 axis ~W is ~W, but ~S is ~W long."
3176 axis k contents l
)))
3177 (sb-sequence:dosequence
(content contents
)
3179 ,start-recursion
))))
3181 (defun fill-data-vector (vector dimensions initial-contents
)
3182 (declare (explicit-check))
3183 (symbol-macrolet ((this-dimension (car dims
)))
3184 (body (axis dims contents
) (null dims
)
3185 (frob 0 dimensions initial-contents
)
3186 (frob (1+ axis
) (cdr dims
) content
)))
3189 ;; Identical to FILL-DATA-VECTOR but avoid reference
3190 ;; to DIMENSIONS as a list except in case of error.
3191 (defun fill-array (initial-contents array
)
3192 (declare (explicit-check))
3193 (let ((rank (array-rank array
))
3194 (vector (%array-data array
)))
3195 (symbol-macrolet ((dimensions (array-dimensions array
))
3196 (this-dimension (%array-dimension array axis
)))
3197 (body (axis contents
) (= axis rank
)
3198 (frob 0 initial-contents
)
3199 (frob (1+ axis
) content
))))