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 (defun upgraded-array-element-type (spec &optional environment)
70 "Return the element type that will actually be used to implement an array
71 with the specifier :ELEMENT-TYPE Spec."
72 (declare (ignore environment))
73 (if (unknown-type-p (specifier-type spec))
74 (error "undefined type: ~S" spec)
75 (type-specifier (array-type-specialized-element-type
76 (specifier-type `(array ,spec))))))
77 (eval-when (:compile-toplevel :execute)
78 (sb!xc:defmacro pick-vector-type (type &rest specs)
79 `(cond ,@(mapcar (lambda (spec)
80 `(,(if (eq (car spec) t)
82 `(subtypep ,type ',(car spec)))
86 ;;; These functions are used in the implementation of MAKE-ARRAY for
87 ;;; complex arrays. There are lots of transforms to simplify
88 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
89 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
90 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
91 ;;; making this somewhat efficient, at least not doing full calls to
92 ;;; SUBTYPEP in the easy cases.
93 (defun %vector-widetag-and-n-bits (type)
95 ;; Pick off some easy common cases.
97 ;; (Perhaps we should make a much more exhaustive table of easy
98 ;; common cases here. Or perhaps the effort would be better spent
99 ;; on smarter compiler transforms which do the calculation once
100 ;; and for all in any reasonable user programs.)
102 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
103 ((character base-char standard-char)
104 (values #.sb!vm:simple-string-widetag #.sb!vm:n-byte-bits))
106 (values #.sb!vm:simple-bit-vector-widetag 1))
107 ;; OK, we have to wade into SUBTYPEPing after all.
109 ;; FIXME: The data here are redundant with
110 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPE-PROPERTIES*.
111 (pick-vector-type type
112 (nil (values #.sb!vm:simple-array-nil-widetag 0))
113 (base-char (values #.sb!vm:simple-string-widetag #.sb!vm:n-byte-bits))
114 (bit (values #.sb!vm:simple-bit-vector-widetag 1))
116 (values #.sb!vm:simple-array-unsigned-byte-2-widetag 2))
118 (values #.sb!vm:simple-array-unsigned-byte-4-widetag 4))
120 (values #.sb!vm:simple-array-unsigned-byte-8-widetag 8))
122 (values #.sb!vm:simple-array-unsigned-byte-16-widetag 16))
124 (values #.sb!vm:simple-array-unsigned-byte-32-widetag 32))
126 (values #.sb!vm:simple-array-signed-byte-8-widetag 8))
128 (values #.sb!vm:simple-array-signed-byte-16-widetag 16))
130 (values #.sb!vm:simple-array-signed-byte-30-widetag 32))
132 (values #.sb!vm:simple-array-signed-byte-32-widetag 32))
133 (single-float (values #.sb!vm:simple-array-single-float-widetag 32))
134 (double-float (values #.sb!vm:simple-array-double-float-widetag 64))
137 (values #.sb!vm:simple-array-long-float-widetag
138 #!+x86 96 #!+sparc 128))
139 ((complex single-float)
140 (values #.sb!vm:simple-array-complex-single-float-widetag 64))
141 ((complex double-float)
142 (values #.sb!vm:simple-array-complex-double-float-widetag 128))
144 ((complex long-float)
145 (values #.sb!vm:simple-array-complex-long-float-widetag
148 (t (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))))))
149 (defun %complex-vector-widetag (type)
151 ;; Pick off some easy common cases.
153 #.sb!vm:complex-vector-widetag)
154 ((character base-char)
155 #.sb!vm:complex-string-widetag)
157 #.sb!vm:complex-bit-vector-widetag)
158 ;; OK, we have to wade into SUBTYPEPing after all.
160 (pick-vector-type type
161 (base-char #.sb!vm:complex-string-widetag)
162 (bit #.sb!vm:complex-bit-vector-widetag)
163 (t #.sb!vm:complex-vector-widetag)))))
165 (defun make-array (dimensions &key
167 (initial-element nil initial-element-p)
168 initial-contents adjustable fill-pointer
169 displaced-to displaced-index-offset)
170 (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
171 (array-rank (length (the list dimensions)))
172 (simple (and (null fill-pointer)
174 (null displaced-to))))
175 (declare (fixnum array-rank))
176 (when (and displaced-index-offset (null displaced-to))
177 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
178 (if (and simple (= array-rank 1))
179 ;; it's a (SIMPLE-ARRAY * (*))
180 (multiple-value-bind (type n-bits)
181 (%vector-widetag-and-n-bits element-type)
182 (declare (type (unsigned-byte 8) type)
183 (type (integer 0 256) n-bits))
184 (let* ((length (car dimensions))
185 (array (allocate-vector
188 (ceiling (* (if (= type sb!vm:simple-string-widetag)
192 sb!vm:n-word-bits))))
193 (declare (type index length))
194 (when initial-element-p
195 (fill array initial-element))
196 (when initial-contents
197 (when initial-element
198 (error "can't specify both :INITIAL-ELEMENT and ~
200 (unless (= length (length initial-contents))
201 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
202 the vector length is ~W."
203 (length initial-contents)
205 (replace array initial-contents))
207 ;; it's either a complex array or a multidimensional array.
208 (let* ((total-size (reduce #'* dimensions))
209 (data (or displaced-to
210 (data-vector-from-inits
211 dimensions total-size element-type
212 initial-contents initial-element initial-element-p)))
213 (array (make-array-header
214 (cond ((= array-rank 1)
215 (%complex-vector-widetag element-type))
216 (simple sb!vm:simple-array-widetag)
217 (t sb!vm:complex-array-widetag))
220 (unless (= array-rank 1)
221 (error "Only vectors can have fill pointers."))
222 (let ((length (car dimensions)))
223 (declare (fixnum length))
224 (setf (%array-fill-pointer array)
225 (cond ((eq fill-pointer t)
228 (unless (and (fixnump fill-pointer)
230 (<= fill-pointer length))
231 ;; FIXME: should be TYPE-ERROR?
232 (error "invalid fill-pointer ~W"
235 (setf (%array-fill-pointer-p array) t))
237 (setf (%array-fill-pointer array) total-size)
238 (setf (%array-fill-pointer-p array) nil)))
239 (setf (%array-available-elements array) total-size)
240 (setf (%array-data-vector array) data)
242 (when (or initial-element-p initial-contents)
243 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
244 can be specified along with :DISPLACED-TO"))
245 (let ((offset (or displaced-index-offset 0)))
246 (when (> (+ offset total-size)
247 (array-total-size displaced-to))
248 (error "~S doesn't have enough elements." displaced-to))
249 (setf (%array-displacement array) offset)
250 (setf (%array-displaced-p array) t)))
252 (setf (%array-displaced-p array) nil)))
254 (dolist (dim dimensions)
255 (setf (%array-dimension array axis) dim)
259 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
260 ;;; specified array characteristics. Dimensions is only used to pass
261 ;;; to FILL-DATA-VECTOR for error checking on the structure of
262 ;;; initial-contents.
263 (defun data-vector-from-inits (dimensions total-size element-type
264 initial-contents initial-element
266 (when (and initial-contents initial-element-p)
267 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
268 either MAKE-ARRAY or ADJUST-ARRAY."))
269 (let ((data (if initial-element-p
270 (make-array total-size
271 :element-type element-type
272 :initial-element initial-element)
273 (make-array total-size
274 :element-type element-type))))
275 (cond (initial-element-p
276 (unless (simple-vector-p data)
277 (unless (typep initial-element element-type)
278 (error "~S cannot be used to initialize an array of type ~S."
279 initial-element element-type))
280 (fill (the vector data) initial-element)))
282 (fill-data-vector data dimensions initial-contents)))
285 (defun fill-data-vector (vector dimensions initial-contents)
287 (labels ((frob (axis dims contents)
289 (setf (aref vector index) contents)
292 (unless (typep contents 'sequence)
293 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
294 sequence, but ~W more layer~:P needed."
296 (- (length dimensions) axis)))
297 (unless (= (length contents) (car dims))
298 (error "malformed :INITIAL-CONTENTS: Dimension of ~
299 axis ~W is ~W, but ~S is ~W long."
300 axis (car dims) contents (length contents)))
302 (dolist (content contents)
303 (frob (1+ axis) (cdr dims) content))
304 (dotimes (i (length contents))
305 (frob (1+ axis) (cdr dims) (aref contents i))))))))
306 (frob 0 dimensions initial-contents))))
308 (defun vector (&rest objects)
310 "Construct a SIMPLE-VECTOR from the given objects."
311 (coerce (the list objects) 'simple-vector))
313 ;;;; accessor/setter functions
315 (eval-when (:compile-toplevel :execute)
316 (defparameter *specialized-array-element-types*
331 #!+long-float long-float
332 (complex single-float)
333 (complex double-float)
334 #!+long-float (complex long-float)
337 (defun hairy-data-vector-ref (array index)
338 (with-array-data ((vector array) (index index) (end))
339 (declare (ignore end))
341 #.(mapcar (lambda (type)
342 (let ((atype `(simple-array ,type (*))))
344 (data-vector-ref (the ,atype vector)
346 *specialized-array-element-types*))))
348 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
349 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
350 ;;; definition is needed for the compiler to use in constant folding.)
351 (defun data-vector-ref (array index)
352 (hairy-data-vector-ref array index))
354 (defun hairy-data-vector-set (array index new-value)
355 (with-array-data ((vector array) (index index) (end))
356 (declare (ignore end))
358 #.(mapcar (lambda (type)
359 (let ((atype `(simple-array ,type (*))))
361 (data-vector-set (the ,atype vector)
365 ;; For specialized arrays, the return
366 ;; from data-vector-set would have to
367 ;; be reboxed to be a (Lisp) return
368 ;; value; instead, we use the
369 ;; already-boxed value as the return.
371 *specialized-array-element-types*))))
373 (defun %array-row-major-index (array subscripts
374 &optional (invalid-index-error-p t))
375 (declare (array array)
377 (let ((rank (array-rank array)))
378 (unless (= rank (length subscripts))
379 (error "wrong number of subscripts, ~W, for array of rank ~W"
380 (length subscripts) rank))
381 (if (array-header-p array)
382 (do ((subs (nreverse subscripts) (cdr subs))
383 (axis (1- (array-rank array)) (1- axis))
387 (declare (list subs) (fixnum axis chunk-size result))
388 (let ((index (car subs))
389 (dim (%array-dimension array axis)))
390 (declare (fixnum dim))
391 (unless (< -1 index dim)
392 (if invalid-index-error-p
393 (error 'simple-type-error
394 :format-control "invalid index ~W~[~;~:; on axis ~:*~W~] in ~S"
395 :format-arguments (list index axis array)
397 :expected-type `(integer 0 (,dim)))
398 (return-from %array-row-major-index nil)))
399 (incf result (* chunk-size (the fixnum index)))
400 (setf chunk-size (* chunk-size dim))))
401 (let ((index (first subscripts))
402 (length (length (the (simple-array * (*)) array))))
403 (unless (< -1 index length)
404 (if invalid-index-error-p
405 ;; FIXME: perhaps this should share a format-string
406 ;; with INVALID-ARRAY-INDEX-ERROR or
407 ;; INDEX-TOO-LARGE-ERROR?
408 (error 'simple-type-error
409 :format-control "invalid index ~W in ~S"
410 :format-arguments (list index array)
412 :expected-type `(integer 0 (,length)))
413 (return-from %array-row-major-index nil)))
416 (defun array-in-bounds-p (array &rest subscripts)
418 "Return T if the Subscipts are in bounds for the Array, Nil otherwise."
419 (if (%array-row-major-index array subscripts nil)
422 (defun array-row-major-index (array &rest subscripts)
423 (%array-row-major-index array subscripts))
425 (defun aref (array &rest subscripts)
427 "Return the element of the Array specified by the Subscripts."
428 (row-major-aref array (%array-row-major-index array subscripts)))
430 (defun %aset (array &rest stuff)
431 (let ((subscripts (butlast stuff))
432 (new-value (car (last stuff))))
433 (setf (row-major-aref array (%array-row-major-index array subscripts))
436 ;;; FIXME: What's supposed to happen with functions
437 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
438 ;;; DEFSETF FOO is also defined? It seems as though the logical
439 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
440 ;;; and replace it with the (SETF FOO) function, issuing a warning,
441 ;;; just as for ordinary functions
442 ;;; * (LISP-IMPLEMENTATION-VERSION)
443 ;;; "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
444 ;;; * (DEFMACRO ZOO (X) `(+ ,X ,X))
446 ;;; * (DEFUN ZOO (X) (* 3 X))
447 ;;; Warning: ZOO previously defined as a macro.
449 ;;; But that doesn't seem to be what happens in CMU CL.
451 ;;; KLUDGE: this is probably because ANSI, in its wisdom (CLHS
452 ;;; 5.1.2.5) requires implementations to support
453 ;;; (SETF (APPLY #'AREF ...) ...)
454 ;;; [and also #'BIT and #'SBIT]. Yes, this is terrifying, and it's
455 ;;; also terrifying that this sequence of definitions causes it to
458 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
459 ;;; has a setf expansion and/or a setf function defined.
461 #!-sb-fluid (declaim (inline (setf aref)))
462 (defun (setf aref) (new-value array &rest subscripts)
463 (declare (type array array))
464 (setf (row-major-aref array (%array-row-major-index array subscripts))
467 (defun row-major-aref (array index)
469 "Return the element of array corressponding to the row-major index. This is
471 (declare (optimize (safety 1)))
472 (row-major-aref array index))
474 (defun %set-row-major-aref (array index new-value)
475 (declare (optimize (safety 1)))
476 (setf (row-major-aref array index) new-value))
478 (defun svref (simple-vector index)
480 "Return the INDEX'th element of the given Simple-Vector."
481 (declare (optimize (safety 1)))
482 (aref simple-vector index))
484 (defun %svset (simple-vector index new)
485 (declare (optimize (safety 1)))
486 (setf (aref simple-vector index) new))
488 (defun bit (bit-array &rest subscripts)
490 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
491 (declare (type (array bit) bit-array) (optimize (safety 1)))
492 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
494 (defun %bitset (bit-array &rest stuff)
495 (declare (type (array bit) bit-array) (optimize (safety 1)))
496 (let ((subscripts (butlast stuff))
497 (new-value (car (last stuff))))
498 (setf (row-major-aref bit-array
499 (%array-row-major-index bit-array subscripts))
502 #!-sb-fluid (declaim (inline (setf bit)))
503 (defun (setf bit) (new-value bit-array &rest subscripts)
504 (declare (type (array bit) bit-array) (optimize (safety 1)))
505 (setf (row-major-aref bit-array
506 (%array-row-major-index bit-array subscripts))
509 (defun sbit (simple-bit-array &rest subscripts)
511 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
512 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
513 (row-major-aref simple-bit-array
514 (%array-row-major-index simple-bit-array subscripts)))
516 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
517 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
518 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
520 (defun %sbitset (simple-bit-array &rest stuff)
521 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
522 (let ((subscripts (butlast stuff))
523 (new-value (car (last stuff))))
524 (setf (row-major-aref simple-bit-array
525 (%array-row-major-index simple-bit-array subscripts))
528 #!-sb-fluid (declaim (inline (setf sbit)))
529 (defun (setf sbit) (new-value bit-array &rest subscripts)
530 (declare (type (simple-array bit) bit-array) (optimize (safety 1)))
531 (setf (row-major-aref bit-array
532 (%array-row-major-index bit-array subscripts))
535 ;;;; miscellaneous array properties
537 (defun array-element-type (array)
539 "Return the type of the elements of the array"
540 (let ((widetag (widetag-of array)))
541 (macrolet ((pick-element-type (&rest stuff)
542 `(cond ,@(mapcar (lambda (stuff)
544 (let ((item (car stuff)))
553 `(= widetag ,item))))
556 ;; FIXME: The data here are redundant with
557 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPE-PROPERTIES*.
559 (sb!vm:simple-array-nil-widetag nil)
560 ((sb!vm:simple-string-widetag sb!vm:complex-string-widetag) 'base-char)
561 ((sb!vm:simple-bit-vector-widetag
562 sb!vm:complex-bit-vector-widetag) 'bit)
563 (sb!vm:simple-vector-widetag t)
564 (sb!vm:simple-array-unsigned-byte-2-widetag '(unsigned-byte 2))
565 (sb!vm:simple-array-unsigned-byte-4-widetag '(unsigned-byte 4))
566 (sb!vm:simple-array-unsigned-byte-8-widetag '(unsigned-byte 8))
567 (sb!vm:simple-array-unsigned-byte-16-widetag '(unsigned-byte 16))
568 (sb!vm:simple-array-unsigned-byte-32-widetag '(unsigned-byte 32))
569 (sb!vm:simple-array-signed-byte-8-widetag '(signed-byte 8))
570 (sb!vm:simple-array-signed-byte-16-widetag '(signed-byte 16))
571 (sb!vm:simple-array-signed-byte-30-widetag '(signed-byte 30))
572 (sb!vm:simple-array-signed-byte-32-widetag '(signed-byte 32))
573 (sb!vm:simple-array-single-float-widetag 'single-float)
574 (sb!vm:simple-array-double-float-widetag 'double-float)
576 (sb!vm:simple-array-long-float-widetag 'long-float)
577 (sb!vm:simple-array-complex-single-float-widetag
578 '(complex single-float))
579 (sb!vm:simple-array-complex-double-float-widetag
580 '(complex double-float))
582 (sb!vm:simple-array-complex-long-float-widetag '(complex long-float))
583 ((sb!vm:simple-array-widetag
584 sb!vm:complex-vector-widetag
585 sb!vm:complex-array-widetag)
586 (with-array-data ((array array) (start) (end))
587 (declare (ignore start end))
588 (array-element-type array)))
590 (error 'type-error :datum array :expected-type 'array))))))
592 (defun array-rank (array)
594 "Return the number of dimensions of ARRAY."
595 (if (array-header-p array)
599 (defun array-dimension (array axis-number)
601 "Return the length of dimension AXIS-NUMBER of ARRAY."
602 (declare (array array) (type index axis-number))
603 (cond ((not (array-header-p array))
604 (unless (= axis-number 0)
605 (error "Vector axis is not zero: ~S" axis-number))
606 (length (the (simple-array * (*)) array)))
607 ((>= axis-number (%array-rank array))
608 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
609 axis-number array (%array-rank array)))
611 (%array-dimension array axis-number))))
613 (defun array-dimensions (array)
615 "Return a list whose elements are the dimensions of the array"
616 (declare (array array))
617 (if (array-header-p array)
618 (do ((results nil (cons (array-dimension array index) results))
619 (index (1- (array-rank array)) (1- index)))
620 ((minusp index) results))
621 (list (array-dimension array 0))))
623 (defun array-total-size (array)
625 "Return the total number of elements in the Array."
626 (declare (array array))
627 (if (array-header-p array)
628 (%array-available-elements array)
629 (length (the vector array))))
631 (defun array-displacement (array)
633 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
634 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
635 (declare (type array array))
636 (if (and (array-header-p array) ; if unsimple and
637 (%array-displaced-p array)) ; displaced
638 (values (%array-data-vector array) (%array-displacement array))
641 (defun adjustable-array-p (array)
643 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
644 to the argument, this happens for complex arrays."
645 (declare (array array))
646 (not (typep array 'simple-array)))
648 ;;;; fill pointer frobbing stuff
650 (defun array-has-fill-pointer-p (array)
652 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
653 (declare (array array))
654 (and (array-header-p array) (%array-fill-pointer-p array)))
656 (defun fill-pointer (vector)
658 "Return the FILL-POINTER of the given VECTOR."
659 (declare (vector vector))
660 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
661 (%array-fill-pointer vector)
662 (error 'simple-type-error
664 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
665 :format-control "~S is not an array with a fill pointer."
666 :format-arguments (list vector))))
668 (defun %set-fill-pointer (vector new)
669 (declare (vector vector) (fixnum new))
670 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
671 (if (> new (%array-available-elements vector))
673 "The new fill pointer, ~S, is larger than the length of the vector."
675 (setf (%array-fill-pointer vector) new))
676 (error 'simple-type-error
678 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
679 :format-control "~S is not an array with a fill pointer."
680 :format-arguments (list vector))))
682 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
683 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
684 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
685 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
687 (defun vector-push (new-el array)
689 "Attempt to set the element of ARRAY designated by its fill pointer
690 to NEW-EL, and increment the fill pointer by one. If the fill pointer is
691 too large, NIL is returned, otherwise the index of the pushed element is
693 (declare (vector array))
694 (let ((fill-pointer (fill-pointer array)))
695 (declare (fixnum fill-pointer))
696 (cond ((= fill-pointer (%array-available-elements array))
699 (setf (aref array fill-pointer) new-el)
700 (setf (%array-fill-pointer array) (1+ fill-pointer))
703 (defun vector-push-extend (new-element
706 (extension (1+ (length vector))))
707 (declare (vector vector) (fixnum extension))
708 (let ((fill-pointer (fill-pointer vector)))
709 (declare (fixnum fill-pointer))
710 (when (= fill-pointer (%array-available-elements vector))
711 (adjust-array vector (+ fill-pointer extension)))
712 (setf (aref vector fill-pointer) new-element)
713 (setf (%array-fill-pointer vector) (1+ fill-pointer))
716 (defun vector-pop (array)
718 "Decrease the fill pointer by 1 and return the element pointed to by the
720 (declare (vector array))
721 (let ((fill-pointer (fill-pointer array)))
722 (declare (fixnum fill-pointer))
723 (if (zerop fill-pointer)
724 (error "There is nothing left to pop.")
726 (setf (%array-fill-pointer array)
727 (1- fill-pointer))))))
731 (defun adjust-array (array dimensions &key
732 (element-type (array-element-type array))
733 (initial-element nil initial-element-p)
734 initial-contents fill-pointer
735 displaced-to displaced-index-offset)
737 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
738 (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
739 (cond ((/= (the fixnum (length (the list dimensions)))
740 (the fixnum (array-rank array)))
741 (error "The number of dimensions not equal to rank of array."))
742 ((not (subtypep element-type (array-element-type array)))
743 (error "The new element type, ~S, is incompatible with old type."
745 (let ((array-rank (length (the list dimensions))))
746 (declare (fixnum array-rank))
747 (when (and fill-pointer (> array-rank 1))
748 (error "Multidimensional arrays can't have fill pointers."))
749 (cond (initial-contents
750 ;; array former contents replaced by INITIAL-CONTENTS
751 (if (or initial-element-p displaced-to)
752 (error "INITIAL-CONTENTS may not be specified with ~
753 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
754 (let* ((array-size (apply #'* dimensions))
755 (array-data (data-vector-from-inits
756 dimensions array-size element-type
757 initial-contents initial-element
759 (if (adjustable-array-p array)
760 (set-array-header array array-data array-size
761 (get-new-fill-pointer array array-size
764 (if (array-header-p array)
765 ;; simple multidimensional or single dimensional array
766 (make-array dimensions
767 :element-type element-type
768 :initial-contents initial-contents)
771 ;; We already established that no INITIAL-CONTENTS was supplied.
772 (when initial-element
773 (error "The :INITIAL-ELEMENT option may not be specified ~
774 with :DISPLACED-TO."))
775 (unless (subtypep element-type (array-element-type displaced-to))
776 (error "can't displace an array of type ~S into another of ~
778 element-type (array-element-type displaced-to)))
779 (let ((displacement (or displaced-index-offset 0))
780 (array-size (apply #'* dimensions)))
781 (declare (fixnum displacement array-size))
782 (if (< (the fixnum (array-total-size displaced-to))
783 (the fixnum (+ displacement array-size)))
784 (error "The :DISPLACED-TO array is too small."))
785 (if (adjustable-array-p array)
786 ;; None of the original contents appear in adjusted array.
787 (set-array-header array displaced-to array-size
788 (get-new-fill-pointer array array-size
790 displacement dimensions t)
791 ;; simple multidimensional or single dimensional array
792 (make-array dimensions
793 :element-type element-type
794 :displaced-to displaced-to
795 :displaced-index-offset
796 displaced-index-offset))))
798 (let ((old-length (array-total-size array))
799 (new-length (car dimensions))
801 (declare (fixnum old-length new-length))
802 (with-array-data ((old-data array) (old-start)
803 (old-end old-length))
804 (cond ((or (%array-displaced-p array)
805 (< old-length new-length))
807 (data-vector-from-inits
808 dimensions new-length element-type
809 initial-contents initial-element
811 (replace new-data old-data
812 :start2 old-start :end2 old-end))
814 (shrink-vector old-data new-length))))
815 (if (adjustable-array-p array)
816 (set-array-header array new-data new-length
817 (get-new-fill-pointer array new-length
822 (let ((old-length (%array-available-elements array))
823 (new-length (apply #'* dimensions)))
824 (declare (fixnum old-length new-length))
825 (with-array-data ((old-data array) (old-start)
826 (old-end old-length))
827 (declare (ignore old-end))
828 (let ((new-data (if (or (%array-displaced-p array)
829 (> new-length old-length))
830 (data-vector-from-inits
831 dimensions new-length
832 element-type () initial-element
835 (if (or (zerop old-length) (zerop new-length))
836 (when initial-element-p (fill new-data initial-element))
837 (zap-array-data old-data (array-dimensions array)
839 new-data dimensions new-length
840 element-type initial-element
842 (set-array-header array new-data new-length
843 new-length 0 dimensions nil)))))))))
845 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
846 (cond ((not fill-pointer)
847 (when (array-has-fill-pointer-p old-array)
848 (when (> (%array-fill-pointer old-array) new-array-size)
849 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
850 smaller than its fill pointer (~S)"
851 old-array new-array-size (fill-pointer old-array)))
852 (%array-fill-pointer old-array)))
853 ((not (array-has-fill-pointer-p old-array))
854 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
855 in ADJUST-ARRAY unless the array (~S) was originally ~
856 created with a fill pointer"
859 ((numberp fill-pointer)
860 (when (> fill-pointer new-array-size)
861 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
862 than the new length of the vector (~S)"
863 fill-pointer new-array-size))
868 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
871 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
872 ;;; which must be less than or equal to its current length.
873 (defun shrink-vector (vector new-length)
874 (declare (vector vector))
875 (unless (array-header-p vector)
876 (macrolet ((frob (name &rest things)
878 ((simple-array nil (*)) (error 'cell-error
879 :name 'nil-array-element))
880 ,@(mapcar (lambda (thing)
881 (destructuring-bind (type-spec fill-value)
884 (fill (truly-the ,type-spec ,name)
886 :start new-length))))
888 ;; FIXME: The associations between vector types and initial
889 ;; values here are redundant with
890 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPE-PROPERTIES*.
893 (simple-base-string #.*default-init-char-form*)
894 (simple-bit-vector 0)
895 ((simple-array (unsigned-byte 2) (*)) 0)
896 ((simple-array (unsigned-byte 4) (*)) 0)
897 ((simple-array (unsigned-byte 8) (*)) 0)
898 ((simple-array (unsigned-byte 16) (*)) 0)
899 ((simple-array (unsigned-byte 32) (*)) 0)
900 ((simple-array (signed-byte 8) (*)) 0)
901 ((simple-array (signed-byte 16) (*)) 0)
902 ((simple-array (signed-byte 30) (*)) 0)
903 ((simple-array (signed-byte 32) (*)) 0)
904 ((simple-array single-float (*)) (coerce 0 'single-float))
905 ((simple-array double-float (*)) (coerce 0 'double-float))
907 ((simple-array long-float (*)) (coerce 0 'long-float))
908 ((simple-array (complex single-float) (*))
909 (coerce 0 '(complex single-float)))
910 ((simple-array (complex double-float) (*))
911 (coerce 0 '(complex double-float)))
913 ((simple-array (complex long-float) (*))
914 (coerce 0 '(complex long-float))))))
915 ;; Only arrays have fill-pointers, but vectors have their length
916 ;; parameter in the same place.
917 (setf (%array-fill-pointer vector) new-length)
920 ;;; Fill in array header with the provided information, and return the array.
921 (defun set-array-header (array data length fill-pointer displacement dimensions
922 &optional displacedp)
923 (setf (%array-data-vector array) data)
924 (setf (%array-available-elements array) length)
926 (setf (%array-fill-pointer array) fill-pointer)
927 (setf (%array-fill-pointer-p array) t))
929 (setf (%array-fill-pointer array) length)
930 (setf (%array-fill-pointer-p array) nil)))
931 (setf (%array-displacement array) displacement)
932 (if (listp dimensions)
933 (dotimes (axis (array-rank array))
934 (declare (type index axis))
935 (setf (%array-dimension array axis) (pop dimensions)))
936 (setf (%array-dimension array 0) dimensions))
937 (setf (%array-displaced-p array) displacedp)
940 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
942 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
943 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice.
944 (defvar *zap-array-data-temp* (make-array 1000 :initial-element t))
946 (defun zap-array-data-temp (length element-type initial-element
948 (declare (fixnum length))
949 (when (> length (the fixnum (length *zap-array-data-temp*)))
950 (setf *zap-array-data-temp*
951 (make-array length :initial-element t)))
952 (when initial-element-p
953 (unless (typep initial-element element-type)
954 (error "~S can't be used to initialize an array of type ~S."
955 initial-element element-type))
956 (fill (the simple-vector *zap-array-data-temp*) initial-element
958 *zap-array-data-temp*)
960 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
961 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
962 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
963 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
964 ;;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and INITIAL-ELEMENT-P
965 ;;; are used when OLD-DATA and NEW-DATA are EQ; in this case, a
966 ;;; temporary must be used and filled appropriately. When OLD-DATA and
967 ;;; NEW-DATA are not EQ, NEW-DATA has already been filled with any
968 ;;; specified initial-element.
969 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
970 element-type initial-element initial-element-p)
971 (declare (list old-dims new-dims))
972 (setq old-dims (nreverse old-dims))
973 (setq new-dims (reverse new-dims))
974 (if (eq old-data new-data)
975 (let ((temp (zap-array-data-temp new-length element-type
976 initial-element initial-element-p)))
977 (zap-array-data-aux old-data old-dims offset temp new-dims)
978 (dotimes (i new-length) (setf (aref new-data i) (aref temp i))))
979 (zap-array-data-aux old-data old-dims offset new-data new-dims)))
981 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
982 (declare (fixnum offset))
983 (let ((limits (mapcar (lambda (x y)
984 (declare (fixnum x y))
985 (1- (the fixnum (min x y))))
987 (macrolet ((bump-index-list (index limits)
988 `(do ((subscripts ,index (cdr subscripts))
989 (limits ,limits (cdr limits)))
990 ((null subscripts) nil)
991 (cond ((< (the fixnum (car subscripts))
992 (the fixnum (car limits)))
994 (1+ (the fixnum (car subscripts))))
996 (t (rplaca subscripts 0))))))
997 (do ((index (make-list (length old-dims) :initial-element 0)
998 (bump-index-list index limits)))
1000 (setf (aref new-data (row-major-index-from-dims index new-dims))
1002 (+ (the fixnum (row-major-index-from-dims index old-dims))
1005 ;;; Figure out the row-major-order index of an array reference from a
1006 ;;; list of subscripts and a list of dimensions. This is for internal
1007 ;;; calls only, and the subscripts and dim-list variables are assumed
1008 ;;; to be reversed from what the user supplied.
1009 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1010 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1011 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1014 ((null rev-dim-list) result)
1015 (declare (fixnum chunk-size result))
1016 (setq result (+ result
1017 (the fixnum (* (the fixnum (car rev-subscripts))
1019 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1023 (defun bit-array-same-dimensions-p (array1 array2)
1024 (declare (type (array bit) array1 array2))
1025 (and (= (array-rank array1)
1026 (array-rank array2))
1027 (dotimes (index (array-rank array1) t)
1028 (when (/= (array-dimension array1 index)
1029 (array-dimension array2 index))
1032 (defun pick-result-array (result-bit-array bit-array-1)
1033 (case result-bit-array
1035 ((nil) (make-array (array-dimensions bit-array-1)
1037 :initial-element 0))
1039 (unless (bit-array-same-dimensions-p bit-array-1
1041 (error "~S and ~S don't have the same dimensions."
1042 bit-array-1 result-bit-array))
1045 (defmacro def-bit-array-op (name function)
1046 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1048 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1049 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1050 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1051 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1052 All the arrays must have the same rank and dimensions."
1053 (symbol-name function))
1054 (declare (type (array bit) bit-array-1 bit-array-2)
1055 (type (or (array bit) (member t nil)) result-bit-array))
1056 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1057 (error "~S and ~S don't have the same dimensions."
1058 bit-array-1 bit-array-2))
1059 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1060 (if (and (simple-bit-vector-p bit-array-1)
1061 (simple-bit-vector-p bit-array-2)
1062 (simple-bit-vector-p result-bit-array))
1063 (locally (declare (optimize (speed 3) (safety 0)))
1064 (,name bit-array-1 bit-array-2 result-bit-array))
1065 (with-array-data ((data1 bit-array-1) (start1) (end1))
1066 (declare (ignore end1))
1067 (with-array-data ((data2 bit-array-2) (start2) (end2))
1068 (declare (ignore end2))
1069 (with-array-data ((data3 result-bit-array) (start3) (end3))
1070 (do ((index-1 start1 (1+ index-1))
1071 (index-2 start2 (1+ index-2))
1072 (index-3 start3 (1+ index-3)))
1073 ((>= index-3 end3) result-bit-array)
1074 (declare (type index index-1 index-2 index-3))
1075 (setf (sbit data3 index-3)
1076 (logand (,function (sbit data1 index-1)
1077 (sbit data2 index-2))
1080 (def-bit-array-op bit-and logand)
1081 (def-bit-array-op bit-ior logior)
1082 (def-bit-array-op bit-xor logxor)
1083 (def-bit-array-op bit-eqv logeqv)
1084 (def-bit-array-op bit-nand lognand)
1085 (def-bit-array-op bit-nor lognor)
1086 (def-bit-array-op bit-andc1 logandc1)
1087 (def-bit-array-op bit-andc2 logandc2)
1088 (def-bit-array-op bit-orc1 logorc1)
1089 (def-bit-array-op bit-orc2 logorc2)
1091 (defun bit-not (bit-array &optional result-bit-array)
1093 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1094 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1095 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1096 created. Both arrays must have the same rank and dimensions."
1097 (declare (type (array bit) bit-array)
1098 (type (or (array bit) (member t nil)) result-bit-array))
1099 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1100 (if (and (simple-bit-vector-p bit-array)
1101 (simple-bit-vector-p result-bit-array))
1102 (locally (declare (optimize (speed 3) (safety 0)))
1103 (bit-not bit-array result-bit-array))
1104 (with-array-data ((src bit-array) (src-start) (src-end))
1105 (declare (ignore src-end))
1106 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1107 (do ((src-index src-start (1+ src-index))
1108 (dst-index dst-start (1+ dst-index)))
1109 ((>= dst-index dst-end) result-bit-array)
1110 (declare (type index src-index dst-index))
1111 (setf (sbit dst dst-index)
1112 (logxor (sbit src src-index) 1))))))))