1 ;;;; functions to implement arrays
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
15 (declaim (inline fill-pointer array-has-fill-pointer-p adjustable-array-p
18 ;;;; miscellaneous accessor functions
20 ;;; These functions are only needed by the interpreter, 'cause the
21 ;;; compiler inlines them.
22 (macrolet ((def (name)
26 (defun (setf ,name) (value array)
27 (setf (,name array) value)))))
28 (def %array-fill-pointer)
29 (def %array-fill-pointer-p)
30 (def %array-available-elements)
31 (def %array-data-vector)
32 (def %array-displacement)
33 (def %array-displaced-p))
35 (defun %array-rank (array)
38 (defun %array-dimension (array axis)
39 (%array-dimension array axis))
41 (defun %set-array-dimension (array axis value)
42 (%set-array-dimension array axis value))
44 (defun %check-bound (array bound index)
45 (declare (type index bound)
47 (%check-bound array bound index))
49 (defun %with-array-data (array start end)
50 (%with-array-data-macro array start end :fail-inline? t))
52 (defun %data-vector-and-index (array index)
53 (if (array-header-p array)
54 (multiple-value-bind (vector index)
55 (%with-array-data array index nil)
56 (values vector index))
57 (values array index)))
59 ;;; It'd waste space to expand copies of error handling in every
60 ;;; inline %WITH-ARRAY-DATA, so we have them call this function
61 ;;; instead. This is just a wrapper which is known never to return.
62 (defun failed-%with-array-data (array start end)
63 (declare (notinline %with-array-data))
64 (%with-array-data array start end)
65 (bug "called FAILED-%WITH-ARRAY-DATA with valid array parameters?"))
68 (eval-when (:compile-toplevel :execute)
69 (sb!xc:defmacro pick-vector-type (type &rest specs)
70 `(cond ,@(mapcar (lambda (spec)
71 `(,(if (eq (car spec) t)
73 `(subtypep ,type ',(car spec)))
77 ;;; These functions are used in the implementation of MAKE-ARRAY for
78 ;;; complex arrays. There are lots of transforms to simplify
79 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
80 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
81 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
82 ;;; making this somewhat efficient, at least not doing full calls to
83 ;;; SUBTYPEP in the easy cases.
84 (defun %vector-widetag-and-n-bits (type)
86 ;; Pick off some easy common cases.
88 ;; (Perhaps we should make a much more exhaustive table of easy
89 ;; common cases here. Or perhaps the effort would be better spent
90 ;; on smarter compiler transforms which do the calculation once
91 ;; and for all in any reasonable user programs.)
93 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
94 ((base-char standard-char #!-sb-unicode character)
95 (values #.sb!vm:simple-base-string-widetag #.sb!vm:n-byte-bits))
98 (values #.sb!vm:simple-character-string-widetag #.sb!vm:n-word-bits))
100 (values #.sb!vm:simple-bit-vector-widetag 1))
101 ;; OK, we have to wade into SUBTYPEPing after all.
103 #.`(pick-vector-type type
106 `(,(sb!vm:saetp-specifier saetp)
107 (values ,(sb!vm:saetp-typecode saetp)
108 ,(sb!vm:saetp-n-bits saetp))))
109 sb!vm:*specialized-array-element-type-properties*)))))
111 (defun %complex-vector-widetag (type)
113 ;; Pick off some easy common cases.
115 #.sb!vm:complex-vector-widetag)
116 ((base-char #!-sb-unicode character)
117 #.sb!vm:complex-base-string-widetag)
120 #.sb!vm:complex-character-string-widetag)
122 #.sb!vm:complex-vector-nil-widetag)
124 #.sb!vm:complex-bit-vector-widetag)
125 ;; OK, we have to wade into SUBTYPEPing after all.
127 (pick-vector-type type
128 (nil #.sb!vm:complex-vector-nil-widetag)
130 (character #.sb!vm:complex-base-string-widetag)
132 (base-char #.sb!vm:complex-base-string-widetag)
134 (character #.sb!vm:complex-character-string-widetag)
135 (bit #.sb!vm:complex-bit-vector-widetag)
136 (t #.sb!vm:complex-vector-widetag)))))
138 (defun make-array (dimensions &key
140 (initial-element nil initial-element-p)
141 (initial-contents nil initial-contents-p)
142 adjustable fill-pointer
143 displaced-to displaced-index-offset)
144 (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
145 (array-rank (length (the list dimensions)))
146 (simple (and (null fill-pointer)
148 (null displaced-to))))
149 (declare (fixnum array-rank))
150 (when (and displaced-index-offset (null displaced-to))
151 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
152 (when (and displaced-to
153 (arrayp displaced-to)
154 (not (equal (array-element-type displaced-to)
155 (upgraded-array-element-type element-type))))
156 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
157 (if (and simple (= array-rank 1))
158 ;; it's a (SIMPLE-ARRAY * (*))
159 (multiple-value-bind (type n-bits)
160 (%vector-widetag-and-n-bits element-type)
161 (declare (type (unsigned-byte 8) type)
162 (type (integer 0 256) n-bits))
163 (let* ((length (car dimensions))
164 (array (allocate-vector
168 (* (if (or (= type sb!vm:simple-base-string-widetag)
171 sb!vm:simple-character-string-widetag))
175 sb!vm:n-word-bits))))
176 (declare (type index length))
177 (when initial-element-p
178 (fill array initial-element))
179 (when initial-contents-p
180 (when initial-element-p
181 (error "can't specify both :INITIAL-ELEMENT and ~
183 (unless (= length (length initial-contents))
184 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
185 the vector length is ~W."
186 (length initial-contents)
188 (replace array initial-contents))
190 ;; it's either a complex array or a multidimensional array.
191 (let* ((total-size (reduce #'* dimensions))
192 (data (or displaced-to
193 (data-vector-from-inits
194 dimensions total-size element-type
195 initial-contents initial-contents-p
196 initial-element initial-element-p)))
197 (array (make-array-header
198 (cond ((= array-rank 1)
199 (%complex-vector-widetag element-type))
200 (simple sb!vm:simple-array-widetag)
201 (t sb!vm:complex-array-widetag))
204 (unless (= array-rank 1)
205 (error "Only vectors can have fill pointers."))
206 (let ((length (car dimensions)))
207 (declare (fixnum length))
208 (setf (%array-fill-pointer array)
209 (cond ((eq fill-pointer t)
212 (unless (and (fixnump fill-pointer)
214 (<= fill-pointer length))
215 ;; FIXME: should be TYPE-ERROR?
216 (error "invalid fill-pointer ~W"
219 (setf (%array-fill-pointer-p array) t))
221 (setf (%array-fill-pointer array) total-size)
222 (setf (%array-fill-pointer-p array) nil)))
223 (setf (%array-available-elements array) total-size)
224 (setf (%array-data-vector array) data)
226 (when (or initial-element-p initial-contents-p)
227 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
228 can be specified along with :DISPLACED-TO"))
229 (let ((offset (or displaced-index-offset 0)))
230 (when (> (+ offset total-size)
231 (array-total-size displaced-to))
232 (error "~S doesn't have enough elements." displaced-to))
233 (setf (%array-displacement array) offset)
234 (setf (%array-displaced-p array) t)))
236 (setf (%array-displaced-p array) nil)))
238 (dolist (dim dimensions)
239 (setf (%array-dimension array axis) dim)
243 (defun make-static-vector (length &key
244 (element-type '(unsigned-byte 8))
245 (initial-contents nil initial-contents-p)
246 (initial-element nil initial-element-p))
247 "Allocate vector of LENGTH elements in static space. Only allocation
248 of specialized arrays is supported."
249 ;; STEP 1: check inputs fully
251 ;; This way of doing explicit checks before the vector is allocated
252 ;; is expensive, but probably worth the trouble as once we've allocated
253 ;; the vector we have no way to get rid of it anymore...
254 (when (eq t (upgraded-array-element-type element-type))
255 (error "Static arrays of type ~S not supported."
257 (when initial-contents-p
258 (when initial-element-p
259 (error "can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
260 (unless (= length (length initial-contents))
261 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
262 vector length is ~W."
263 (length initial-contents)
265 (unless (every (lambda (x) (typep x element-type)) initial-contents)
266 (error ":INITIAL-CONTENTS contains elements not of type ~S."
268 (when initial-element-p
269 (unless (typep initial-element element-type)
270 (error ":INITIAL-ELEMENT ~S is not of type ~S."
271 initial-element element-type)))
274 ;; Allocate and possibly initialize the vector.
275 (multiple-value-bind (type n-bits)
276 (sb!impl::%vector-widetag-and-n-bits element-type)
278 (allocate-static-vector type length
279 (ceiling (* length n-bits)
280 sb!vm:n-word-bits))))
281 (cond (initial-element-p
282 (fill vector initial-element))
284 (replace vector initial-contents))
288 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
289 ;;; specified array characteristics. Dimensions is only used to pass
290 ;;; to FILL-DATA-VECTOR for error checking on the structure of
291 ;;; initial-contents.
292 (defun data-vector-from-inits (dimensions total-size element-type
293 initial-contents initial-contents-p
294 initial-element initial-element-p)
295 (when (and initial-contents-p initial-element-p)
296 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
297 either MAKE-ARRAY or ADJUST-ARRAY."))
298 (let ((data (if initial-element-p
299 (make-array total-size
300 :element-type element-type
301 :initial-element initial-element)
302 (make-array total-size
303 :element-type element-type))))
304 (cond (initial-element-p
305 (unless (simple-vector-p data)
306 (unless (typep initial-element element-type)
307 (error "~S cannot be used to initialize an array of type ~S."
308 initial-element element-type))
309 (fill (the vector data) initial-element)))
311 (fill-data-vector data dimensions initial-contents)))
314 (defun vector (&rest objects)
316 "Construct a SIMPLE-VECTOR from the given objects."
317 (coerce (the list objects) 'simple-vector))
319 ;;;; accessor/setter functions
320 (defun hairy-data-vector-ref (array index)
321 (with-array-data ((vector array) (index index) (end))
322 (declare (ignore end))
326 (let* ((type (sb!vm:saetp-specifier saetp))
327 (atype `(simple-array ,type (*))))
329 (data-vector-ref (the ,atype vector) index))))
332 sb!vm:*specialized-array-element-type-properties*)
333 #'> :key #'sb!vm:saetp-importance)))))
335 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
336 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
337 ;;; definition is needed for the compiler to use in constant folding.)
338 (defun data-vector-ref (array index)
339 (hairy-data-vector-ref array index))
341 (defun hairy-data-vector-set (array index new-value)
342 (with-array-data ((vector array) (index index) (end))
343 (declare (ignore end))
347 (let* ((type (sb!vm:saetp-specifier saetp))
348 (atype `(simple-array ,type (*))))
350 (data-vector-set (the ,atype vector) index
351 (the ,type new-value))
352 ;; For specialized arrays, the return from
353 ;; data-vector-set would have to be
354 ;; reboxed to be a (Lisp) return value;
355 ;; instead, we use the already-boxed value
360 sb!vm:*specialized-array-element-type-properties*)
361 #'> :key #'sb!vm:saetp-importance)))))
363 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
364 (defun %array-row-major-index (array subscripts
365 &optional (invalid-index-error-p t))
366 (declare (array array)
368 (let ((rank (array-rank array)))
369 (unless (= rank (length subscripts))
370 (error "wrong number of subscripts, ~W, for array of rank ~W"
371 (length subscripts) rank))
372 (if (array-header-p array)
373 (do ((subs (nreverse subscripts) (cdr subs))
374 (axis (1- (array-rank array)) (1- axis))
378 (declare (list subs) (fixnum axis chunk-size result))
379 (let ((index (car subs))
380 (dim (%array-dimension array axis)))
381 (declare (fixnum dim))
382 (unless (and (fixnump index) (< -1 index dim))
383 (if invalid-index-error-p
384 (error 'simple-type-error
385 :format-control "invalid index ~W~[~;~:; on axis ~:*~W~] in ~S"
386 :format-arguments (list index axis array)
388 :expected-type `(integer 0 (,dim)))
389 (return-from %array-row-major-index nil)))
390 (incf result (* chunk-size (the fixnum index)))
391 (setf chunk-size (* chunk-size dim))))
392 (let ((index (first subscripts))
393 (length (length (the (simple-array * (*)) array))))
394 (unless (and (fixnump index) (< -1 index length))
395 (if invalid-index-error-p
396 ;; FIXME: perhaps this should share a format-string
397 ;; with INVALID-ARRAY-INDEX-ERROR or
398 ;; INDEX-TOO-LARGE-ERROR?
399 (error 'simple-type-error
400 :format-control "invalid index ~W in ~S"
401 :format-arguments (list index array)
403 :expected-type `(integer 0 (,length)))
404 (return-from %array-row-major-index nil)))
407 (defun array-in-bounds-p (array &rest subscripts)
409 "Return T if the SUBSCIPTS are in bounds for the ARRAY, NIL otherwise."
410 (if (%array-row-major-index array subscripts nil)
413 (defun array-row-major-index (array &rest subscripts)
414 (declare (dynamic-extent subscripts))
415 (%array-row-major-index array subscripts))
417 (defun aref (array &rest subscripts)
419 "Return the element of the ARRAY specified by the SUBSCRIPTS."
420 (declare (dynamic-extent subscripts))
421 (row-major-aref array (%array-row-major-index array subscripts)))
423 (defun %aset (array &rest stuff)
424 (declare (dynamic-extent stuff))
425 (let ((subscripts (butlast stuff))
426 (new-value (car (last stuff))))
427 (setf (row-major-aref array (%array-row-major-index array subscripts))
430 ;;; FIXME: What's supposed to happen with functions
431 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
432 ;;; DEFSETF FOO is also defined? It seems as though the logical
433 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
434 ;;; and replace it with the (SETF FOO) function, issuing a warning,
435 ;;; just as for ordinary functions
436 ;;; * (LISP-IMPLEMENTATION-VERSION)
437 ;;; "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
438 ;;; * (DEFMACRO ZOO (X) `(+ ,X ,X))
440 ;;; * (DEFUN ZOO (X) (* 3 X))
441 ;;; Warning: ZOO previously defined as a macro.
443 ;;; But that doesn't seem to be what happens in CMU CL.
445 ;;; KLUDGE: this is probably because ANSI, in its wisdom (CLHS
446 ;;; 5.1.2.5) requires implementations to support
447 ;;; (SETF (APPLY #'AREF ...) ...)
448 ;;; [and also #'BIT and #'SBIT]. Yes, this is terrifying, and it's
449 ;;; also terrifying that this sequence of definitions causes it to
452 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
453 ;;; has a setf expansion and/or a setf function defined.
455 #!-sb-fluid (declaim (inline (setf aref)))
456 (defun (setf aref) (new-value array &rest subscripts)
457 (declare (dynamic-extent subscripts))
458 (declare (type array array))
459 (setf (row-major-aref array (%array-row-major-index array subscripts))
462 (defun row-major-aref (array index)
464 "Return the element of array corressponding to the row-major index. This is
466 (declare (optimize (safety 1)))
467 (row-major-aref array index))
469 (defun %set-row-major-aref (array index new-value)
470 (declare (optimize (safety 1)))
471 (setf (row-major-aref array index) new-value))
473 (defun svref (simple-vector index)
475 "Return the INDEX'th element of the given Simple-Vector."
476 (declare (optimize (safety 1)))
477 (aref simple-vector index))
479 (defun %svset (simple-vector index new)
480 (declare (optimize (safety 1)))
481 (setf (aref simple-vector index) new))
483 (defun bit (bit-array &rest subscripts)
485 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
486 (declare (type (array bit) bit-array) (optimize (safety 1)))
487 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
489 (defun %bitset (bit-array &rest stuff)
490 (declare (type (array bit) bit-array) (optimize (safety 1)))
491 (let ((subscripts (butlast stuff))
492 (new-value (car (last stuff))))
493 (setf (row-major-aref bit-array
494 (%array-row-major-index bit-array subscripts))
497 #!-sb-fluid (declaim (inline (setf bit)))
498 (defun (setf bit) (new-value bit-array &rest subscripts)
499 (declare (type (array bit) bit-array) (optimize (safety 1)))
500 (setf (row-major-aref bit-array
501 (%array-row-major-index bit-array subscripts))
504 (defun sbit (simple-bit-array &rest subscripts)
506 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
507 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
508 (row-major-aref simple-bit-array
509 (%array-row-major-index simple-bit-array subscripts)))
511 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
512 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
513 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
515 (defun %sbitset (simple-bit-array &rest stuff)
516 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
517 (let ((subscripts (butlast stuff))
518 (new-value (car (last stuff))))
519 (setf (row-major-aref simple-bit-array
520 (%array-row-major-index simple-bit-array subscripts))
523 #!-sb-fluid (declaim (inline (setf sbit)))
524 (defun (setf sbit) (new-value bit-array &rest subscripts)
525 (declare (type (simple-array bit) bit-array) (optimize (safety 1)))
526 (setf (row-major-aref bit-array
527 (%array-row-major-index bit-array subscripts))
530 ;;;; miscellaneous array properties
532 (defun array-element-type (array)
534 "Return the type of the elements of the array"
535 (let ((widetag (widetag-of array)))
536 (macrolet ((pick-element-type (&rest stuff)
537 `(cond ,@(mapcar (lambda (stuff)
539 (let ((item (car stuff)))
548 `(= widetag ,item))))
551 #.`(pick-element-type
554 `(,(if (sb!vm:saetp-complex-typecode saetp)
555 (list (sb!vm:saetp-typecode saetp)
556 (sb!vm:saetp-complex-typecode saetp))
557 (sb!vm:saetp-typecode saetp))
558 ',(sb!vm:saetp-specifier saetp)))
559 sb!vm:*specialized-array-element-type-properties*)
560 ((sb!vm:simple-array-widetag
561 sb!vm:complex-vector-widetag
562 sb!vm:complex-array-widetag)
563 (with-array-data ((array array) (start) (end))
564 (declare (ignore start end))
565 (array-element-type array)))
567 (error 'type-error :datum array :expected-type 'array))))))
569 (defun array-rank (array)
571 "Return the number of dimensions of ARRAY."
572 (if (array-header-p array)
576 (defun array-dimension (array axis-number)
578 "Return the length of dimension AXIS-NUMBER of ARRAY."
579 (declare (array array) (type index axis-number))
580 (cond ((not (array-header-p array))
581 (unless (= axis-number 0)
582 (error "Vector axis is not zero: ~S" axis-number))
583 (length (the (simple-array * (*)) array)))
584 ((>= axis-number (%array-rank array))
585 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
586 axis-number array (%array-rank array)))
588 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
590 ;; "If A is displaced to B, the consequences are
591 ;; unspecified if B is adjusted in such a way that it no
592 ;; longer has enough elements to satisfy A.
594 ;; In situations where this matters we should be doing a
595 ;; bounds-check, which in turn uses ARRAY-DIMENSION -- so
596 ;; this seems like a good place to signal an error.
597 (multiple-value-bind (target offset) (array-displacement array)
599 (> (array-total-size array)
600 (- (array-total-size target) offset)))
601 (error 'displaced-to-array-too-small-error
602 :format-control "~@<The displaced-to array is too small. ~S ~
603 elements after offset required, ~S available.~:@>"
604 :format-arguments (list (array-total-size array)
605 (- (array-total-size target) offset))))
606 (%array-dimension array axis-number)))))
608 (defun array-dimensions (array)
610 "Return a list whose elements are the dimensions of the array"
611 (declare (array array))
612 (if (array-header-p array)
613 (do ((results nil (cons (array-dimension array index) results))
614 (index (1- (array-rank array)) (1- index)))
615 ((minusp index) results))
616 (list (array-dimension array 0))))
618 (defun array-total-size (array)
620 "Return the total number of elements in the Array."
621 (declare (array array))
622 (if (array-header-p array)
623 (%array-available-elements array)
624 (length (the vector array))))
626 (defun array-displacement (array)
628 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
629 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
630 (declare (type array array))
631 (if (and (array-header-p array) ; if unsimple and
632 (%array-displaced-p array)) ; displaced
633 (values (%array-data-vector array) (%array-displacement array))
636 (defun adjustable-array-p (array)
638 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
639 to the argument, this happens for complex arrays."
640 (declare (array array))
641 ;; Note that this appears not to be a fundamental limitation.
642 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
643 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
644 ;; -- CSR, 2004-03-01.
645 (not (typep array 'simple-array)))
647 ;;;; fill pointer frobbing stuff
649 (defun array-has-fill-pointer-p (array)
651 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
652 (declare (array array))
653 (and (array-header-p array) (%array-fill-pointer-p array)))
655 (defun fill-pointer (vector)
657 "Return the FILL-POINTER of the given VECTOR."
658 (declare (vector vector))
659 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
660 (%array-fill-pointer vector)
661 (error 'simple-type-error
663 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
664 :format-control "~S is not an array with a fill pointer."
665 :format-arguments (list vector))))
667 (defun %set-fill-pointer (vector new)
668 (declare (vector vector) (fixnum new))
669 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
670 (if (> new (%array-available-elements vector))
672 "The new fill pointer, ~S, is larger than the length of the vector."
674 (setf (%array-fill-pointer vector) new))
675 (error 'simple-type-error
677 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
678 :format-control "~S is not an array with a fill pointer."
679 :format-arguments (list vector))))
681 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
682 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
683 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
684 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
686 (defun vector-push (new-el array)
688 "Attempt to set the element of ARRAY designated by its fill pointer
689 to NEW-EL, and increment the fill pointer by one. If the fill pointer is
690 too large, NIL is returned, otherwise the index of the pushed element is
692 (declare (vector array))
693 (let ((fill-pointer (fill-pointer array)))
694 (declare (fixnum fill-pointer))
695 (cond ((= fill-pointer (%array-available-elements array))
698 (setf (aref array fill-pointer) new-el)
699 (setf (%array-fill-pointer array) (1+ fill-pointer))
702 (defun vector-push-extend (new-element
705 (extension (1+ (length vector))))
706 (declare (vector vector) (fixnum extension))
707 (let ((fill-pointer (fill-pointer vector)))
708 (declare (fixnum fill-pointer))
709 (when (= fill-pointer (%array-available-elements vector))
710 (adjust-array vector (+ fill-pointer extension)))
711 ;; disable bounds checking
712 (locally (declare (optimize (safety 0)))
713 (setf (aref vector fill-pointer) new-element))
714 (setf (%array-fill-pointer vector) (1+ fill-pointer))
717 (defun vector-pop (array)
719 "Decrease the fill pointer by 1 and return the element pointed to by the
721 (declare (vector array))
722 (let ((fill-pointer (fill-pointer array)))
723 (declare (fixnum fill-pointer))
724 (if (zerop fill-pointer)
725 (error "There is nothing left to pop.")
726 ;; disable bounds checking (and any fixnum test)
727 (locally (declare (optimize (safety 0)))
729 (setf (%array-fill-pointer array)
730 (1- fill-pointer)))))))
735 (defun adjust-array (array dimensions &key
736 (element-type (array-element-type array))
737 (initial-element nil initial-element-p)
738 (initial-contents nil initial-contents-p)
740 displaced-to displaced-index-offset)
742 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
743 (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
744 (cond ((/= (the fixnum (length (the list dimensions)))
745 (the fixnum (array-rank array)))
746 (error "The number of dimensions not equal to rank of array."))
747 ((not (subtypep element-type (array-element-type array)))
748 (error "The new element type, ~S, is incompatible with old type."
750 ((and fill-pointer (not (array-has-fill-pointer-p array)))
753 :expected-type '(satisfies array-has-fill-pointer-p))))
754 (let ((array-rank (length (the list dimensions))))
755 (declare (fixnum array-rank))
756 (unless (= array-rank 1)
758 (error "Only vectors can have fill pointers.")))
759 (cond (initial-contents-p
760 ;; array former contents replaced by INITIAL-CONTENTS
761 (if (or initial-element-p displaced-to)
762 (error "INITIAL-CONTENTS may not be specified with ~
763 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
764 (let* ((array-size (apply #'* dimensions))
765 (array-data (data-vector-from-inits
766 dimensions array-size element-type
767 initial-contents initial-contents-p
768 initial-element initial-element-p)))
769 (if (adjustable-array-p array)
770 (set-array-header array array-data array-size
771 (get-new-fill-pointer array array-size
774 (if (array-header-p array)
775 ;; simple multidimensional or single dimensional array
776 (make-array dimensions
777 :element-type element-type
778 :initial-contents initial-contents)
781 ;; We already established that no INITIAL-CONTENTS was supplied.
782 (when initial-element
783 (error "The :INITIAL-ELEMENT option may not be specified ~
784 with :DISPLACED-TO."))
785 (unless (subtypep element-type (array-element-type displaced-to))
786 (error "can't displace an array of type ~S into another of ~
788 element-type (array-element-type displaced-to)))
789 (let ((displacement (or displaced-index-offset 0))
790 (array-size (apply #'* dimensions)))
791 (declare (fixnum displacement array-size))
792 (if (< (the fixnum (array-total-size displaced-to))
793 (the fixnum (+ displacement array-size)))
794 (error "The :DISPLACED-TO array is too small."))
795 (if (adjustable-array-p array)
796 ;; None of the original contents appear in adjusted array.
797 (set-array-header array displaced-to array-size
798 (get-new-fill-pointer array array-size
800 displacement dimensions t)
801 ;; simple multidimensional or single dimensional array
802 (make-array dimensions
803 :element-type element-type
804 :displaced-to displaced-to
805 :displaced-index-offset
806 displaced-index-offset))))
808 (let ((old-length (array-total-size array))
809 (new-length (car dimensions))
811 (declare (fixnum old-length new-length))
812 (with-array-data ((old-data array) (old-start)
813 (old-end old-length))
814 (cond ((or (and (array-header-p array)
815 (%array-displaced-p array))
816 (< old-length new-length))
818 (data-vector-from-inits
819 dimensions new-length element-type
820 initial-contents initial-contents-p
821 initial-element initial-element-p))
822 (replace new-data old-data
823 :start2 old-start :end2 old-end))
825 (shrink-vector old-data new-length))))
826 (if (adjustable-array-p array)
827 (set-array-header array new-data new-length
828 (get-new-fill-pointer array new-length
833 (let ((old-length (%array-available-elements array))
834 (new-length (apply #'* dimensions)))
835 (declare (fixnum old-length new-length))
836 (with-array-data ((old-data array) (old-start)
837 (old-end old-length))
838 (declare (ignore old-end))
839 (let ((new-data (if (or (and (array-header-p array)
840 (%array-displaced-p array))
841 (> new-length old-length))
842 (data-vector-from-inits
843 dimensions new-length
845 initial-element initial-element-p)
847 (if (or (zerop old-length) (zerop new-length))
848 (when initial-element-p (fill new-data initial-element))
849 (zap-array-data old-data (array-dimensions array)
851 new-data dimensions new-length
852 element-type initial-element
854 (if (adjustable-array-p array)
855 (set-array-header array new-data new-length
856 new-length 0 dimensions nil)
859 sb!vm:simple-array-widetag array-rank)))
860 (set-array-header new-array new-data new-length
861 new-length 0 dimensions nil)))))))))))
864 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
865 (cond ((not fill-pointer)
866 (when (array-has-fill-pointer-p old-array)
867 (when (> (%array-fill-pointer old-array) new-array-size)
868 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
869 smaller than its fill pointer (~S)"
870 old-array new-array-size (fill-pointer old-array)))
871 (%array-fill-pointer old-array)))
872 ((not (array-has-fill-pointer-p old-array))
873 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
874 in ADJUST-ARRAY unless the array (~S) was originally ~
875 created with a fill pointer"
878 ((numberp fill-pointer)
879 (when (> fill-pointer new-array-size)
880 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
881 than the new length of the vector (~S)"
882 fill-pointer new-array-size))
887 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
890 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
891 ;;; which must be less than or equal to its current length. This can
892 ;;; be called on vectors without a fill pointer but it is extremely
893 ;;; dangerous to do so: shrinking the size of an object (as viewed by
894 ;;; the gc) makes bounds checking unreliable in the face of interrupts
895 ;;; or multi-threading. Call it only on provably local vectors.
896 (defun %shrink-vector (vector new-length)
897 (declare (vector vector))
898 (unless (array-header-p vector)
899 (macrolet ((frob (name &rest things)
901 ((simple-array nil (*)) (error 'nil-array-accessed-error))
902 ,@(mapcar (lambda (thing)
903 (destructuring-bind (type-spec fill-value)
906 (fill (truly-the ,type-spec ,name)
908 :start new-length))))
910 ;; Set the 'tail' of the vector to the appropriate type of zero,
911 ;; "because in some cases we'll scavenge larger areas in one go,
912 ;; like groups of pages that had triggered the write barrier, or
913 ;; the whole static space" according to jsnell.
917 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
918 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
920 (eq (sb!vm:saetp-specifier saetp) 'base-char))
921 *default-init-char-form*
922 (sb!vm:saetp-initial-element-default saetp))))
924 #'sb!vm:saetp-specifier
925 sb!vm:*specialized-array-element-type-properties*)))))
926 ;; Only arrays have fill-pointers, but vectors have their length
927 ;; parameter in the same place.
928 (setf (%array-fill-pointer vector) new-length)
931 (defun shrink-vector (vector new-length)
932 (declare (vector vector))
934 ((eq (length vector) new-length)
936 ((array-has-fill-pointer-p vector)
937 (setf (%array-fill-pointer vector) new-length)
939 (t (subseq vector 0 new-length))))
941 ;;; Fill in array header with the provided information, and return the array.
942 (defun set-array-header (array data length fill-pointer displacement dimensions
943 &optional displacedp)
944 (setf (%array-data-vector array) data)
945 (setf (%array-available-elements array) length)
947 (setf (%array-fill-pointer array) fill-pointer)
948 (setf (%array-fill-pointer-p array) t))
950 (setf (%array-fill-pointer array) length)
951 (setf (%array-fill-pointer-p array) nil)))
952 (setf (%array-displacement array) displacement)
953 (if (listp dimensions)
954 (dotimes (axis (array-rank array))
955 (declare (type index axis))
956 (setf (%array-dimension array axis) (pop dimensions)))
957 (setf (%array-dimension array 0) dimensions))
958 (setf (%array-displaced-p array) displacedp)
961 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
963 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
964 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice. This is rebound
965 ;;; to length zero array in each new thread.
967 ;;; DX is probably a bad idea, because a with a big array it would
968 ;;; be fairly easy to blow the stack.
970 ;;; Rebound per thread.
971 (defvar *zap-array-data-temp* (make-array 1000 :initial-element t))
973 (defun zap-array-data-temp (length element-type initial-element
975 (declare (fixnum length))
976 (when (> length (the fixnum (length *zap-array-data-temp*)))
977 (setf *zap-array-data-temp*
978 (make-array length :initial-element t)))
979 (when initial-element-p
980 (unless (typep initial-element element-type)
981 (error "~S can't be used to initialize an array of type ~S."
982 initial-element element-type))
983 (fill (the simple-vector *zap-array-data-temp*) initial-element
985 *zap-array-data-temp*)
987 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
988 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
989 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
990 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
991 ;;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and INITIAL-ELEMENT-P
992 ;;; are used when OLD-DATA and NEW-DATA are EQ; in this case, a
993 ;;; temporary must be used and filled appropriately. When OLD-DATA and
994 ;;; NEW-DATA are not EQ, NEW-DATA has already been filled with any
995 ;;; specified initial-element.
996 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
997 element-type initial-element initial-element-p)
998 (declare (list old-dims new-dims))
999 (setq old-dims (nreverse old-dims))
1000 (setq new-dims (reverse new-dims))
1001 (if (eq old-data new-data)
1002 (let ((temp (zap-array-data-temp new-length element-type
1003 initial-element initial-element-p)))
1004 (zap-array-data-aux old-data old-dims offset temp new-dims)
1005 (dotimes (i new-length) (setf (aref new-data i) (aref temp i))))
1006 (zap-array-data-aux old-data old-dims offset new-data new-dims)))
1008 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1009 (declare (fixnum offset))
1010 (let ((limits (mapcar (lambda (x y)
1011 (declare (fixnum x y))
1012 (1- (the fixnum (min x y))))
1013 old-dims new-dims)))
1014 (macrolet ((bump-index-list (index limits)
1015 `(do ((subscripts ,index (cdr subscripts))
1016 (limits ,limits (cdr limits)))
1017 ((null subscripts) :eof)
1018 (cond ((< (the fixnum (car subscripts))
1019 (the fixnum (car limits)))
1021 (1+ (the fixnum (car subscripts))))
1023 (t (rplaca subscripts 0))))))
1024 (do ((index (make-list (length old-dims) :initial-element 0)
1025 (bump-index-list index limits)))
1027 (setf (aref new-data (row-major-index-from-dims index new-dims))
1029 (+ (the fixnum (row-major-index-from-dims index old-dims))
1032 ;;; Figure out the row-major-order index of an array reference from a
1033 ;;; list of subscripts and a list of dimensions. This is for internal
1034 ;;; calls only, and the subscripts and dim-list variables are assumed
1035 ;;; to be reversed from what the user supplied.
1036 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1037 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1038 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1041 ((null rev-dim-list) result)
1042 (declare (fixnum chunk-size result))
1043 (setq result (+ result
1044 (the fixnum (* (the fixnum (car rev-subscripts))
1046 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1050 (defun bit-array-same-dimensions-p (array1 array2)
1051 (declare (type (array bit) array1 array2))
1052 (and (= (array-rank array1)
1053 (array-rank array2))
1054 (dotimes (index (array-rank array1) t)
1055 (when (/= (array-dimension array1 index)
1056 (array-dimension array2 index))
1059 (defun pick-result-array (result-bit-array bit-array-1)
1060 (case result-bit-array
1062 ((nil) (make-array (array-dimensions bit-array-1)
1064 :initial-element 0))
1066 (unless (bit-array-same-dimensions-p bit-array-1
1068 (error "~S and ~S don't have the same dimensions."
1069 bit-array-1 result-bit-array))
1072 (defmacro def-bit-array-op (name function)
1073 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1076 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1077 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1078 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1079 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1080 All the arrays must have the same rank and dimensions."
1081 (symbol-name function))
1082 (declare (type (array bit) bit-array-1 bit-array-2)
1083 (type (or (array bit) (member t nil)) result-bit-array))
1084 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1085 (error "~S and ~S don't have the same dimensions."
1086 bit-array-1 bit-array-2))
1087 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1088 (if (and (simple-bit-vector-p bit-array-1)
1089 (simple-bit-vector-p bit-array-2)
1090 (simple-bit-vector-p result-bit-array))
1091 (locally (declare (optimize (speed 3) (safety 0)))
1092 (,name bit-array-1 bit-array-2 result-bit-array))
1093 (with-array-data ((data1 bit-array-1) (start1) (end1))
1094 (declare (ignore end1))
1095 (with-array-data ((data2 bit-array-2) (start2) (end2))
1096 (declare (ignore end2))
1097 (with-array-data ((data3 result-bit-array) (start3) (end3))
1098 (do ((index-1 start1 (1+ index-1))
1099 (index-2 start2 (1+ index-2))
1100 (index-3 start3 (1+ index-3)))
1101 ((>= index-3 end3) result-bit-array)
1102 (declare (type index index-1 index-2 index-3))
1103 (setf (sbit data3 index-3)
1104 (logand (,function (sbit data1 index-1)
1105 (sbit data2 index-2))
1108 (def-bit-array-op bit-and logand)
1109 (def-bit-array-op bit-ior logior)
1110 (def-bit-array-op bit-xor logxor)
1111 (def-bit-array-op bit-eqv logeqv)
1112 (def-bit-array-op bit-nand lognand)
1113 (def-bit-array-op bit-nor lognor)
1114 (def-bit-array-op bit-andc1 logandc1)
1115 (def-bit-array-op bit-andc2 logandc2)
1116 (def-bit-array-op bit-orc1 logorc1)
1117 (def-bit-array-op bit-orc2 logorc2)
1119 (defun bit-not (bit-array &optional result-bit-array)
1121 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1122 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1123 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1124 created. Both arrays must have the same rank and dimensions."
1125 (declare (type (array bit) bit-array)
1126 (type (or (array bit) (member t nil)) result-bit-array))
1127 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1128 (if (and (simple-bit-vector-p bit-array)
1129 (simple-bit-vector-p result-bit-array))
1130 (locally (declare (optimize (speed 3) (safety 0)))
1131 (bit-not bit-array result-bit-array))
1132 (with-array-data ((src bit-array) (src-start) (src-end))
1133 (declare (ignore src-end))
1134 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1135 (do ((src-index src-start (1+ src-index))
1136 (dst-index dst-start (1+ dst-index)))
1137 ((>= dst-index dst-end) result-bit-array)
1138 (declare (type index src-index dst-index))
1139 (setf (sbit dst dst-index)
1140 (logxor (sbit src src-index) 1))))))))