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-frob (name)
26 (defun (setf ,name) (value array)
27 (setf (,name array) value)))))
28 (def-frob %array-fill-pointer)
29 (def-frob %array-fill-pointer-p)
30 (def-frob %array-available-elements)
31 (def-frob %array-data-vector)
32 (def-frob %array-displacement)
33 (def-frob %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 ;;; It'd waste space to expand copies of error handling in every
53 ;;; inline %WITH-ARRAY-DATA, so we have them call this function
54 ;;; instead. This is just a wrapper which is known never to return.
55 (defun failed-%with-array-data (array start end)
56 (declare (notinline %with-array-data))
57 (%with-array-data array start end)
58 (error "internal error: shouldn't be here with valid parameters"))
62 (eval-when (:compile-toplevel :execute)
63 (sb!xc:defmacro pick-type (type &rest specs)
64 `(cond ,@(mapcar #'(lambda (spec)
65 `(,(if (eq (car spec) t)
67 `(subtypep ,type ',(car spec)))
71 ;;; These functions are used in the implementation of MAKE-ARRAY for
72 ;;; complex arrays. There are lots of transforms to simplify
73 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
74 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
75 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
76 ;;; making this somewhat efficient, at least not doing full calls to
77 ;;; SUBTYPEP in the easy cases.
78 (defun %vector-type-code (type)
80 ;; Pick off some easy common cases.
82 ;; (Perhaps we should make a much more exhaustive table of easy
83 ;; common cases here. Or perhaps the effort would be better spent
84 ;; on smarter compiler transforms which do the calculation once
85 ;; and for all in any reasonable user programs.)
87 (values #.sb!vm:simple-vector-type #.sb!vm:word-bits))
88 ((character base-char)
89 (values #.sb!vm:simple-string-type #.sb!vm:byte-bits))
91 (values #.sb!vm:simple-bit-vector-type 1))
92 ;; OK, we have to wade into SUBTYPEPing after all.
95 (base-char (values #.sb!vm:simple-string-type #.sb!vm:byte-bits))
96 (bit (values #.sb!vm:simple-bit-vector-type 1))
98 (values #.sb!vm:simple-array-unsigned-byte-2-type 2))
100 (values #.sb!vm:simple-array-unsigned-byte-4-type 4))
102 (values #.sb!vm:simple-array-unsigned-byte-8-type 8))
104 (values #.sb!vm:simple-array-unsigned-byte-16-type 16))
106 (values #.sb!vm:simple-array-unsigned-byte-32-type 32))
108 (values #.sb!vm:simple-array-signed-byte-8-type 8))
110 (values #.sb!vm:simple-array-signed-byte-16-type 16))
112 (values #.sb!vm:simple-array-signed-byte-30-type 32))
114 (values #.sb!vm:simple-array-signed-byte-32-type 32))
115 (single-float (values #.sb!vm:simple-array-single-float-type 32))
116 (double-float (values #.sb!vm:simple-array-double-float-type 64))
119 (values #.sb!vm:simple-array-long-float-type #!+x86 96 #!+sparc 128))
120 ((complex single-float)
121 (values #.sb!vm:simple-array-complex-single-float-type 64))
122 ((complex double-float)
123 (values #.sb!vm:simple-array-complex-double-float-type 128))
125 ((complex long-float)
126 (values #.sb!vm:simple-array-complex-long-float-type
129 (t (values #.sb!vm:simple-vector-type #.sb!vm:word-bits))))))
130 (defun %complex-vector-type-code (type)
132 ;; Pick off some easy common cases.
134 #.sb!vm:complex-vector-type)
135 ((character base-char)
136 #.sb!vm:complex-string-type)
138 #.sb!vm:complex-bit-vector-type)
139 ;; OK, we have to wade into SUBTYPEPing after all.
142 (base-char #.sb!vm:complex-string-type)
143 (bit #.sb!vm:complex-bit-vector-type)
144 (t #.sb!vm:complex-vector-type)))))
146 (defun make-array (dimensions &key
148 (initial-element nil initial-element-p)
149 initial-contents adjustable fill-pointer
150 displaced-to displaced-index-offset)
151 (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
152 (array-rank (length (the list dimensions)))
153 (simple (and (null fill-pointer)
155 (null displaced-to))))
156 (declare (fixnum array-rank))
157 (when (and displaced-index-offset (null displaced-to))
158 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
159 (if (and simple (= array-rank 1))
160 ;; Its a (simple-array * (*))
161 (multiple-value-bind (type bits) (%vector-type-code element-type)
162 (declare (type (unsigned-byte 8) type)
163 (type (integer 1 256) bits))
164 (let* ((length (car dimensions))
165 (array (allocate-vector
168 (ceiling (* (if (= type sb!vm:simple-string-type)
173 (declare (type index length))
174 (when initial-element-p
175 (fill array initial-element))
176 (when initial-contents
177 (when initial-element
178 (error "can't specify both :INITIAL-ELEMENT and ~
180 (unless (= length (length initial-contents))
181 (error "There are ~D elements in the :INITIAL-CONTENTS, but ~
182 the vector length is ~D."
183 (length initial-contents)
185 (replace array initial-contents))
187 ;; It's either a complex array or a multidimensional array.
188 (let* ((total-size (reduce #'* dimensions))
189 (data (or displaced-to
190 (data-vector-from-inits
191 dimensions total-size element-type
192 initial-contents initial-element initial-element-p)))
193 (array (make-array-header
194 (cond ((= array-rank 1)
195 (%complex-vector-type-code element-type))
196 (simple sb!vm:simple-array-type)
197 (t sb!vm:complex-array-type))
200 (unless (= array-rank 1)
201 (error "Only vectors can have fill pointers."))
202 (let ((length (car dimensions)))
203 (declare (fixnum length))
204 (setf (%array-fill-pointer array)
205 (cond ((eq fill-pointer t)
208 (unless (and (fixnump fill-pointer)
210 (<= fill-pointer length))
211 (error "invalid fill-pointer ~D"
214 (setf (%array-fill-pointer-p array) t))
216 (setf (%array-fill-pointer array) total-size)
217 (setf (%array-fill-pointer-p array) nil)))
218 (setf (%array-available-elements array) total-size)
219 (setf (%array-data-vector array) data)
221 (when (or initial-element-p initial-contents)
222 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
223 can be specified along with :DISPLACED-TO"))
224 (let ((offset (or displaced-index-offset 0)))
225 (when (> (+ offset total-size)
226 (array-total-size displaced-to))
227 (error "~S doesn't have enough elements." displaced-to))
228 (setf (%array-displacement array) offset)
229 (setf (%array-displaced-p array) t)))
231 (setf (%array-displaced-p array) nil)))
233 (dolist (dim dimensions)
234 (setf (%array-dimension array axis) dim)
238 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
239 ;;; specified array characteristics. Dimensions is only used to pass
240 ;;; to FILL-DATA-VECTOR for error checking on the structure of
241 ;;; initial-contents.
242 (defun data-vector-from-inits (dimensions total-size element-type
243 initial-contents initial-element
245 (when (and initial-contents initial-element-p)
246 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
247 either MAKE-ARRAY or ADJUST-ARRAY."))
248 (let ((data (if initial-element-p
249 (make-array total-size
250 :element-type element-type
251 :initial-element initial-element)
252 (make-array total-size
253 :element-type element-type))))
254 (cond (initial-element-p
255 (unless (simple-vector-p data)
256 (unless (typep initial-element element-type)
257 (error "~S cannot be used to initialize an array of type ~S."
258 initial-element element-type))
259 (fill (the vector data) initial-element)))
261 (fill-data-vector data dimensions initial-contents)))
264 (defun fill-data-vector (vector dimensions initial-contents)
266 (labels ((frob (axis dims contents)
268 (setf (aref vector index) contents)
271 (unless (typep contents 'sequence)
272 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
273 sequence, but ~D more layer~:P needed."
275 (- (length dimensions) axis)))
276 (unless (= (length contents) (car dims))
277 (error "malformed :INITIAL-CONTENTS: Dimension of ~
278 axis ~D is ~D, but ~S is ~D long."
279 axis (car dims) contents (length contents)))
281 (dolist (content contents)
282 (frob (1+ axis) (cdr dims) content))
283 (dotimes (i (length contents))
284 (frob (1+ axis) (cdr dims) (aref contents i))))))))
285 (frob 0 dimensions initial-contents))))
287 (defun vector (&rest objects)
289 "Construct a SIMPLE-VECTOR from the given objects."
290 (coerce (the list objects) 'simple-vector))
292 ;;;; accessor/setter functions
294 (eval-when (:compile-toplevel :execute)
295 (defparameter *specialized-array-element-types*
310 #!+long-float long-float
311 (complex single-float)
312 (complex double-float)
313 #!+long-float (complex long-float))))
315 (defun hairy-data-vector-ref (array index)
316 (with-array-data ((vector array) (index index) (end))
317 (declare (ignore end) (optimize (safety 3)))
319 #.(mapcar (lambda (type)
320 (let ((atype `(simple-array ,type (*))))
322 (data-vector-ref (the ,atype vector)
324 *specialized-array-element-types*))))
326 (defun hairy-data-vector-set (array index new-value)
327 (with-array-data ((vector array) (index index) (end))
328 (declare (ignore end) (optimize (safety 3)))
330 #.(mapcar (lambda (type)
331 (let ((atype `(simple-array ,type (*))))
333 (data-vector-set (the ,atype vector)
337 *specialized-array-element-types*))))
339 (defun %array-row-major-index (array subscripts
340 &optional (invalid-index-error-p t))
341 (declare (array array)
343 (let ((rank (array-rank array)))
344 (unless (= rank (length subscripts))
345 (error "wrong number of subscripts, ~D, for array of rank ~D"
346 (length subscripts) rank))
347 (if (array-header-p array)
348 (do ((subs (nreverse subscripts) (cdr subs))
349 (axis (1- (array-rank array)) (1- axis))
353 (declare (list subs) (fixnum axis chunk-size result))
354 (let ((index (car subs))
355 (dim (%array-dimension array axis)))
356 (declare (fixnum index dim))
357 (unless (< -1 index dim)
358 (if invalid-index-error-p
359 (error "invalid index ~D~[~;~:; on axis ~:*~D~] in ~S"
361 (return-from %array-row-major-index nil)))
362 (incf result (* chunk-size index))
363 (setf chunk-size (* chunk-size dim))))
364 (let ((index (first subscripts)))
365 (unless (< -1 index (length (the (simple-array * (*)) array)))
366 (if invalid-index-error-p
367 (error "invalid index ~D in ~S" index array)
368 (return-from %array-row-major-index nil)))
371 (defun array-in-bounds-p (array &rest subscripts)
373 "Returns T if the Subscipts are in bounds for the Array, Nil otherwise."
374 (if (%array-row-major-index array subscripts nil)
377 (defun array-row-major-index (array &rest subscripts)
378 (%array-row-major-index array subscripts))
380 (defun aref (array &rest subscripts)
382 "Returns the element of the Array specified by the Subscripts."
383 (row-major-aref array (%array-row-major-index array subscripts)))
385 (defun %aset (array &rest stuff)
386 (let ((subscripts (butlast stuff))
387 (new-value (car (last stuff))))
388 (setf (row-major-aref array (%array-row-major-index array subscripts))
391 ;;; FIXME: What's supposed to happen with functions
392 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
393 ;;; DEFSETF FOO is also defined? It seems as though the logical
394 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
395 ;;; and replace it with the (SETF FOO) function, issuing a warning,
396 ;;; just as for ordinary functions
397 ;;; * (LISP-IMPLEMENTATION-VERSION)
398 ;;; "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
399 ;;; * (DEFMACRO ZOO (X) `(+ ,X ,X))
401 ;;; * (DEFUN ZOO (X) (* 3 X))
402 ;;; Warning: ZOO previously defined as a macro.
404 ;;; But that doesn't seem to be what happens in CMU CL.
406 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
407 ;;; has a setf expansion and/or a setf function defined.
409 #!-sb-fluid (declaim (inline (setf aref)))
410 (defun (setf aref) (new-value array &rest subscripts)
411 (declare (type array array))
412 (setf (row-major-aref array (%array-row-major-index array subscripts))
415 (defun row-major-aref (array index)
417 "Returns the element of array corressponding to the row-major index. This is
419 (declare (optimize (safety 1)))
420 (row-major-aref array index))
422 (defun %set-row-major-aref (array index new-value)
423 (declare (optimize (safety 1)))
424 (setf (row-major-aref array index) new-value))
426 (defun svref (simple-vector index)
428 "Returns the Index'th element of the given Simple-Vector."
429 (declare (optimize (safety 1)))
430 (aref simple-vector index))
432 (defun %svset (simple-vector index new)
433 (declare (optimize (safety 1)))
434 (setf (aref simple-vector index) new))
436 (defun bit (bit-array &rest subscripts)
438 "Returns the bit from the Bit-Array at the specified Subscripts."
439 (declare (type (array bit) bit-array) (optimize (safety 1)))
440 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
442 (defun %bitset (bit-array &rest stuff)
443 (declare (type (array bit) bit-array) (optimize (safety 1)))
444 (let ((subscripts (butlast stuff))
445 (new-value (car (last stuff))))
446 (setf (row-major-aref bit-array
447 (%array-row-major-index bit-array subscripts))
450 #!-sb-fluid (declaim (inline (setf bit)))
451 (defun (setf bit) (new-value bit-array &rest subscripts)
452 (declare (type (array bit) bit-array) (optimize (safety 1)))
453 (setf (row-major-aref bit-array
454 (%array-row-major-index bit-array subscripts))
457 (defun sbit (simple-bit-array &rest subscripts)
459 "Returns the bit from the Simple-Bit-Array at the specified Subscripts."
460 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
461 (row-major-aref simple-bit-array
462 (%array-row-major-index simple-bit-array subscripts)))
464 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
465 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
466 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
468 (defun %sbitset (simple-bit-array &rest stuff)
469 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
470 (let ((subscripts (butlast stuff))
471 (new-value (car (last stuff))))
472 (setf (row-major-aref simple-bit-array
473 (%array-row-major-index simple-bit-array subscripts))
476 #!-sb-fluid (declaim (inline (setf sbit)))
477 (defun (setf sbit) (new-value bit-array &rest subscripts)
478 (declare (type (simple-array bit) bit-array) (optimize (safety 1)))
479 (setf (row-major-aref bit-array
480 (%array-row-major-index bit-array subscripts))
483 ;;;; miscellaneous array properties
485 (defun array-element-type (array)
487 "Returns the type of the elements of the array"
488 (let ((type (get-type array)))
489 (macrolet ((pick-element-type (&rest stuff)
490 `(cond ,@(mapcar #'(lambda (stuff)
492 (let ((item (car stuff)))
497 (mapcar #'(lambda (x)
505 ((sb!vm:simple-string-type sb!vm:complex-string-type) 'base-char)
506 ((sb!vm:simple-bit-vector-type sb!vm:complex-bit-vector-type) 'bit)
507 (sb!vm:simple-vector-type t)
508 (sb!vm:simple-array-unsigned-byte-2-type '(unsigned-byte 2))
509 (sb!vm:simple-array-unsigned-byte-4-type '(unsigned-byte 4))
510 (sb!vm:simple-array-unsigned-byte-8-type '(unsigned-byte 8))
511 (sb!vm:simple-array-unsigned-byte-16-type '(unsigned-byte 16))
512 (sb!vm:simple-array-unsigned-byte-32-type '(unsigned-byte 32))
513 (sb!vm:simple-array-signed-byte-8-type '(signed-byte 8))
514 (sb!vm:simple-array-signed-byte-16-type '(signed-byte 16))
515 (sb!vm:simple-array-signed-byte-30-type '(signed-byte 30))
516 (sb!vm:simple-array-signed-byte-32-type '(signed-byte 32))
517 (sb!vm:simple-array-single-float-type 'single-float)
518 (sb!vm:simple-array-double-float-type 'double-float)
520 (sb!vm:simple-array-long-float-type 'long-float)
521 (sb!vm:simple-array-complex-single-float-type '(complex single-float))
522 (sb!vm:simple-array-complex-double-float-type '(complex double-float))
524 (sb!vm:simple-array-complex-long-float-type '(complex long-float))
525 ((sb!vm:simple-array-type sb!vm:complex-vector-type
526 sb!vm:complex-array-type)
527 (with-array-data ((array array) (start) (end))
528 (declare (ignore start end))
529 (array-element-type array)))
531 (error "~S is not an array." array))))))
533 (defun array-rank (array)
535 "Return the number of dimensions of ARRAY."
536 (if (array-header-p array)
540 (defun array-dimension (array axis-number)
542 "Returns the length of dimension AXIS-NUMBER of ARRAY."
543 (declare (array array) (type index axis-number))
544 (cond ((not (array-header-p array))
545 (unless (= axis-number 0)
546 (error "Vector axis is not zero: ~S" axis-number))
547 (length (the (simple-array * (*)) array)))
548 ((>= axis-number (%array-rank array))
549 (error "~D is too big; ~S only has ~D dimension~:P."
550 axis-number array (%array-rank array)))
552 (%array-dimension array axis-number))))
554 (defun array-dimensions (array)
556 "Return a list whose elements are the dimensions of the array"
557 (declare (array array))
558 (if (array-header-p array)
559 (do ((results nil (cons (array-dimension array index) results))
560 (index (1- (array-rank array)) (1- index)))
561 ((minusp index) results))
562 (list (array-dimension array 0))))
564 (defun array-total-size (array)
566 "Return the total number of elements in the Array."
567 (declare (array array))
568 (if (array-header-p array)
569 (%array-available-elements array)
570 (length (the vector array))))
572 (defun array-displacement (array)
574 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
575 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
576 (declare (type array array))
577 (if (and (array-header-p array) ; if unsimple and
578 (%array-displaced-p array)) ; displaced
579 (values (%array-data-vector array) (%array-displacement array))
582 (defun adjustable-array-p (array)
584 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
585 to the argument, this happens for complex arrays."
586 (declare (array array))
587 (not (typep array 'simple-array)))
589 ;;;; fill pointer frobbing stuff
591 (defun array-has-fill-pointer-p (array)
593 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
594 (declare (array array))
595 (and (array-header-p array) (%array-fill-pointer-p array)))
597 (defun fill-pointer (vector)
599 "Return the FILL-POINTER of the given VECTOR."
600 (declare (vector vector))
601 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
602 (%array-fill-pointer vector)
603 (error 'simple-type-error
605 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
606 :format-control "~S is not an array with a fill pointer."
607 :format-arguments (list vector))))
609 (defun %set-fill-pointer (vector new)
610 (declare (vector vector) (fixnum new))
611 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
612 (if (> new (%array-available-elements vector))
614 "The new fill pointer, ~S, is larger than the length of the vector."
616 (setf (%array-fill-pointer vector) new))
617 (error 'simple-type-error
619 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
620 :format-control "~S is not an array with a fill pointer."
621 :format-arguments (list vector))))
623 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
624 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
625 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
626 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
628 (defun vector-push (new-el array)
630 "Attempt to set the element of ARRAY designated by its fill pointer
631 to NEW-EL, and increment the fill pointer by one. If the fill pointer is
632 too large, NIL is returned, otherwise the index of the pushed element is
634 (declare (vector array))
635 (let ((fill-pointer (fill-pointer array)))
636 (declare (fixnum fill-pointer))
637 (cond ((= fill-pointer (%array-available-elements array))
640 (setf (aref array fill-pointer) new-el)
641 (setf (%array-fill-pointer array) (1+ fill-pointer))
644 (defun vector-push-extend (new-element
647 (extension nil extension-p))
649 "This is like VECTOR-PUSH except that if the fill pointer gets too
650 large, VECTOR is extended to allow the push to work."
651 (declare (type vector vector))
652 (let ((old-fill-pointer (fill-pointer vector)))
653 (declare (type index old-fill-pointer))
654 (when (= old-fill-pointer (%array-available-elements vector))
655 (adjust-array vector (+ old-fill-pointer
657 (the (integer 1 #.most-positive-fixnum)
659 (1+ old-fill-pointer)))))
660 (setf (%array-fill-pointer vector)
661 (1+ old-fill-pointer))
662 ;; Wrapping the type test and the AREF in the same WITH-ARRAY-DATA
664 (with-array-data ((v vector) (i old-fill-pointer) (end)
666 (declare (ignore end) (optimize (safety 0)))
667 (if (simple-vector-p v) ; if common special case
668 (setf (aref v i) new-element)
669 (setf (aref v i) new-element)))
672 (defun vector-push-extend (new-element
675 (extension (1+ (length vector))))
676 (declare (vector vector) (fixnum extension))
677 (let ((fill-pointer (fill-pointer vector)))
678 (declare (fixnum fill-pointer))
679 (when (= fill-pointer (%array-available-elements vector))
680 (adjust-array vector (+ fill-pointer extension)))
681 (setf (aref vector fill-pointer) new-element)
682 (setf (%array-fill-pointer vector) (1+ fill-pointer))
685 (defun vector-pop (array)
687 "Attempts to decrease the fill pointer by 1 and return the element
688 pointer to by the new fill pointer. If the original value of the fill
689 pointer is 0, an error occurs."
690 (declare (vector array))
691 (let ((fill-pointer (fill-pointer array)))
692 (declare (fixnum fill-pointer))
693 (if (zerop fill-pointer)
694 (error "There is nothing left to pop.")
696 (setf (%array-fill-pointer array)
697 (1- fill-pointer))))))
701 (defun adjust-array (array dimensions &key
702 (element-type (array-element-type array))
703 (initial-element nil initial-element-p)
704 initial-contents fill-pointer
705 displaced-to displaced-index-offset)
707 "Adjusts the Array's dimensions to the given Dimensions and stuff."
708 (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
709 (cond ((/= (the fixnum (length (the list dimensions)))
710 (the fixnum (array-rank array)))
711 (error "The number of dimensions not equal to rank of array."))
712 ((not (subtypep element-type (array-element-type array)))
713 (error "The new element type, ~S, is incompatible with old type."
715 (let ((array-rank (length (the list dimensions))))
716 (declare (fixnum array-rank))
717 (when (and fill-pointer (> array-rank 1))
718 (error "Multidimensional arrays can't have fill pointers."))
719 (cond (initial-contents
720 ;; array former contents replaced by INITIAL-CONTENTS
721 (if (or initial-element-p displaced-to)
722 (error "INITIAL-CONTENTS may not be specified with ~
723 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
724 (let* ((array-size (apply #'* dimensions))
725 (array-data (data-vector-from-inits
726 dimensions array-size element-type
727 initial-contents initial-element
729 (if (adjustable-array-p array)
730 (set-array-header array array-data array-size
731 (get-new-fill-pointer array array-size
734 (if (array-header-p array)
735 ;; simple multidimensional or single dimensional array
736 (make-array dimensions
737 :element-type element-type
738 :initial-contents initial-contents)
741 ;; We already established that no INITIAL-CONTENTS was supplied.
742 (when initial-element
743 (error "The :INITIAL-ELEMENT option may not be specified ~
744 with :DISPLACED-TO."))
745 (unless (subtypep element-type (array-element-type displaced-to))
746 (error "can't displace an array of type ~S into another of ~
748 element-type (array-element-type displaced-to)))
749 (let ((displacement (or displaced-index-offset 0))
750 (array-size (apply #'* dimensions)))
751 (declare (fixnum displacement array-size))
752 (if (< (the fixnum (array-total-size displaced-to))
753 (the fixnum (+ displacement array-size)))
754 (error "The :DISPLACED-TO array is too small."))
755 (if (adjustable-array-p array)
756 ;; None of the original contents appear in adjusted array.
757 (set-array-header array displaced-to array-size
758 (get-new-fill-pointer array array-size
760 displacement dimensions t)
761 ;; simple multidimensional or single dimensional array
762 (make-array dimensions
763 :element-type element-type
764 :displaced-to displaced-to
765 :displaced-index-offset
766 displaced-index-offset))))
768 (let ((old-length (array-total-size array))
769 (new-length (car dimensions))
771 (declare (fixnum old-length new-length))
772 (with-array-data ((old-data array) (old-start)
773 (old-end old-length))
774 (cond ((or (%array-displaced-p array)
775 (< old-length new-length))
777 (data-vector-from-inits
778 dimensions new-length element-type
779 initial-contents initial-element
781 (replace new-data old-data
782 :start2 old-start :end2 old-end))
784 (shrink-vector old-data new-length))))
785 (if (adjustable-array-p array)
786 (set-array-header array new-data new-length
787 (get-new-fill-pointer array new-length
792 (let ((old-length (%array-available-elements array))
793 (new-length (apply #'* dimensions)))
794 (declare (fixnum old-length new-length))
795 (with-array-data ((old-data array) (old-start)
796 (old-end old-length))
797 (declare (ignore old-end))
798 (let ((new-data (if (or (%array-displaced-p array)
799 (> new-length old-length))
800 (data-vector-from-inits
801 dimensions new-length
802 element-type () initial-element
805 (if (or (zerop old-length) (zerop new-length))
806 (when initial-element-p (fill new-data initial-element))
807 (zap-array-data old-data (array-dimensions array)
809 new-data dimensions new-length
810 element-type initial-element
812 (set-array-header array new-data new-length
813 new-length 0 dimensions nil)))))))))
815 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
816 (cond ((not fill-pointer)
817 (when (array-has-fill-pointer-p old-array)
818 (when (> (%array-fill-pointer old-array) new-array-size)
819 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
820 smaller than its fill pointer (~S)"
821 old-array new-array-size (fill-pointer old-array)))
822 (%array-fill-pointer old-array)))
823 ((not (array-has-fill-pointer-p old-array))
824 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
825 in ADJUST-ARRAY unless the array (~S) was originally ~
826 created with a fill pointer"
829 ((numberp fill-pointer)
830 (when (> fill-pointer new-array-size)
831 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
832 than the new length of the vector (~S)"
833 fill-pointer new-array-size))
838 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
841 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
842 ;;; which must be less than or equal to its current length.
843 (defun shrink-vector (vector new-length)
844 (declare (vector vector))
845 (unless (array-header-p vector)
846 (macrolet ((frob (name &rest things)
848 ,@(mapcar #'(lambda (thing)
850 (fill (truly-the ,(car thing) ,name)
856 (simple-base-string #.default-init-char)
857 (simple-bit-vector 0)
858 ((simple-array (unsigned-byte 2) (*)) 0)
859 ((simple-array (unsigned-byte 4) (*)) 0)
860 ((simple-array (unsigned-byte 8) (*)) 0)
861 ((simple-array (unsigned-byte 16) (*)) 0)
862 ((simple-array (unsigned-byte 32) (*)) 0)
863 ((simple-array (signed-byte 8) (*)) 0)
864 ((simple-array (signed-byte 16) (*)) 0)
865 ((simple-array (signed-byte 30) (*)) 0)
866 ((simple-array (signed-byte 32) (*)) 0)
867 ((simple-array single-float (*)) (coerce 0 'single-float))
868 ((simple-array double-float (*)) (coerce 0 'double-float))
870 ((simple-array long-float (*)) (coerce 0 'long-float))
871 ((simple-array (complex single-float) (*))
872 (coerce 0 '(complex single-float)))
873 ((simple-array (complex double-float) (*))
874 (coerce 0 '(complex double-float)))
876 ((simple-array (complex long-float) (*))
877 (coerce 0 '(complex long-float))))))
878 ;; Only arrays have fill-pointers, but vectors have their length
879 ;; parameter in the same place.
880 (setf (%array-fill-pointer vector) new-length)
883 ;;; Fill in array header with the provided information, and return the array.
884 (defun set-array-header (array data length fill-pointer displacement dimensions
885 &optional displacedp)
886 (setf (%array-data-vector array) data)
887 (setf (%array-available-elements array) length)
889 (setf (%array-fill-pointer array) fill-pointer)
890 (setf (%array-fill-pointer-p array) t))
892 (setf (%array-fill-pointer array) length)
893 (setf (%array-fill-pointer-p array) nil)))
894 (setf (%array-displacement array) displacement)
895 (if (listp dimensions)
896 (dotimes (axis (array-rank array))
897 (declare (type index axis))
898 (setf (%array-dimension array axis) (pop dimensions)))
899 (setf (%array-dimension array 0) dimensions))
900 (setf (%array-displaced-p array) displacedp)
903 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
905 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
906 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice.
907 (defvar *zap-array-data-temp* (make-array 1000 :initial-element t))
909 (defun zap-array-data-temp (length element-type initial-element
911 (declare (fixnum length))
912 (when (> length (the fixnum (length *zap-array-data-temp*)))
913 (setf *zap-array-data-temp*
914 (make-array length :initial-element t)))
915 (when initial-element-p
916 (unless (typep initial-element element-type)
917 (error "~S can't be used to initialize an array of type ~S."
918 initial-element element-type))
919 (fill (the simple-vector *zap-array-data-temp*) initial-element
921 *zap-array-data-temp*)
923 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
924 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
925 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
926 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
927 ;;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and INITIAL-ELEMENT-P
928 ;;; are used when OLD-DATA and NEW-DATA are EQ; in this case, a
929 ;;; temporary must be used and filled appropriately. When OLD-DATA and
930 ;;; NEW-DATA are not EQ, NEW-DATA has already been filled with any
931 ;;; specified initial-element.
932 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
933 element-type initial-element initial-element-p)
934 (declare (list old-dims new-dims))
935 (setq old-dims (nreverse old-dims))
936 (setq new-dims (reverse new-dims))
937 (if (eq old-data new-data)
938 (let ((temp (zap-array-data-temp new-length element-type
939 initial-element initial-element-p)))
940 (zap-array-data-aux old-data old-dims offset temp new-dims)
941 (dotimes (i new-length) (setf (aref new-data i) (aref temp i))))
942 (zap-array-data-aux old-data old-dims offset new-data new-dims)))
944 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
945 (declare (fixnum offset))
946 (let ((limits (mapcar #'(lambda (x y)
947 (declare (fixnum x y))
948 (1- (the fixnum (min x y))))
950 (macrolet ((bump-index-list (index limits)
951 `(do ((subscripts ,index (cdr subscripts))
952 (limits ,limits (cdr limits)))
953 ((null subscripts) nil)
954 (cond ((< (the fixnum (car subscripts))
955 (the fixnum (car limits)))
957 (1+ (the fixnum (car subscripts))))
959 (t (rplaca subscripts 0))))))
960 (do ((index (make-list (length old-dims) :initial-element 0)
961 (bump-index-list index limits)))
963 (setf (aref new-data (row-major-index-from-dims index new-dims))
965 (+ (the fixnum (row-major-index-from-dims index old-dims))
968 ;;; Figure out the row-major-order index of an array reference from a
969 ;;; list of subscripts and a list of dimensions. This is for internal
970 ;;; calls only, and the subscripts and dim-list variables are assumed
971 ;;; to be reversed from what the user supplied.
972 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
973 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
974 (rev-dim-list rev-dim-list (cdr rev-dim-list))
977 ((null rev-dim-list) result)
978 (declare (fixnum chunk-size result))
979 (setq result (+ result
980 (the fixnum (* (the fixnum (car rev-subscripts))
982 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
986 (defun bit-array-same-dimensions-p (array1 array2)
987 (declare (type (array bit) array1 array2))
988 (and (= (array-rank array1)
990 (dotimes (index (array-rank array1) t)
991 (when (/= (array-dimension array1 index)
992 (array-dimension array2 index))
995 (defun pick-result-array (result-bit-array bit-array-1)
996 (case result-bit-array
998 ((nil) (make-array (array-dimensions bit-array-1)
1000 :initial-element 0))
1002 (unless (bit-array-same-dimensions-p bit-array-1
1004 (error "~S and ~S don't have the same dimensions."
1005 bit-array-1 result-bit-array))
1008 (defmacro def-bit-array-op (name function)
1009 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1011 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1012 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1013 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1014 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1015 All the arrays must have the same rank and dimensions."
1016 (symbol-name function))
1017 (declare (type (array bit) bit-array-1 bit-array-2)
1018 (type (or (array bit) (member t nil)) result-bit-array))
1019 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1020 (error "~S and ~S don't have the same dimensions."
1021 bit-array-1 bit-array-2))
1022 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1023 (if (and (simple-bit-vector-p bit-array-1)
1024 (simple-bit-vector-p bit-array-2)
1025 (simple-bit-vector-p result-bit-array))
1026 (locally (declare (optimize (speed 3) (safety 0)))
1027 (,name bit-array-1 bit-array-2 result-bit-array))
1028 (with-array-data ((data1 bit-array-1) (start1) (end1))
1029 (declare (ignore end1))
1030 (with-array-data ((data2 bit-array-2) (start2) (end2))
1031 (declare (ignore end2))
1032 (with-array-data ((data3 result-bit-array) (start3) (end3))
1033 (do ((index-1 start1 (1+ index-1))
1034 (index-2 start2 (1+ index-2))
1035 (index-3 start3 (1+ index-3)))
1036 ((>= index-3 end3) result-bit-array)
1037 (declare (type index index-1 index-2 index-3))
1038 (setf (sbit data3 index-3)
1039 (logand (,function (sbit data1 index-1)
1040 (sbit data2 index-2))
1043 (def-bit-array-op bit-and logand)
1044 (def-bit-array-op bit-ior logior)
1045 (def-bit-array-op bit-xor logxor)
1046 (def-bit-array-op bit-eqv logeqv)
1047 (def-bit-array-op bit-nand lognand)
1048 (def-bit-array-op bit-nor lognor)
1049 (def-bit-array-op bit-andc1 logandc1)
1050 (def-bit-array-op bit-andc2 logandc2)
1051 (def-bit-array-op bit-orc1 logorc1)
1052 (def-bit-array-op bit-orc2 logorc2)
1054 (defun bit-not (bit-array &optional result-bit-array)
1056 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1057 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1058 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1059 created. Both arrays must have the same rank and dimensions."
1060 (declare (type (array bit) bit-array)
1061 (type (or (array bit) (member t nil)) result-bit-array))
1062 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1063 (if (and (simple-bit-vector-p bit-array)
1064 (simple-bit-vector-p result-bit-array))
1065 (locally (declare (optimize (speed 3) (safety 0)))
1066 (bit-not bit-array result-bit-array))
1067 (with-array-data ((src bit-array) (src-start) (src-end))
1068 (declare (ignore src-end))
1069 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1070 (do ((src-index src-start (1+ src-index))
1071 (dst-index dst-start (1+ dst-index)))
1072 ((>= dst-index dst-end) result-bit-array)
1073 (declare (type index src-index dst-index))
1074 (setf (sbit dst dst-index)
1075 (logxor (sbit src src-index) 1))))))))