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 (%with-array-data array index nil)
55 (values array index)))
57 ;;; It'd waste space to expand copies of error handling in every
58 ;;; inline %WITH-ARRAY-DATA, so we have them call this function
59 ;;; instead. This is just a wrapper which is known never to return.
60 (defun failed-%with-array-data (array start end)
61 (declare (notinline %with-array-data))
62 (%with-array-data array start end)
63 (bug "called FAILED-%WITH-ARRAY-DATA with valid array parameters?"))
67 (eval-when (:compile-toplevel :execute)
68 (sb!xc:defmacro pick-vector-type (type &rest specs)
69 `(cond ,@(mapcar (lambda (spec)
70 `(,(if (eq (car spec) t)
72 `(subtypep ,type ',(car spec)))
76 ;;; These functions are used in the implementation of MAKE-ARRAY for
77 ;;; complex arrays. There are lots of transforms to simplify
78 ;;; MAKE-ARRAY for various easy cases, but not for all reasonable
79 ;;; cases, so e.g. as of sbcl-0.6.6 we still make full calls to
80 ;;; MAKE-ARRAY for any non-simple array. Thus, there's some value to
81 ;;; making this somewhat efficient, at least not doing full calls to
82 ;;; SUBTYPEP in the easy cases.
83 (defun %vector-widetag-and-n-bits (type)
85 ;; Pick off some easy common cases.
87 ;; (Perhaps we should make a much more exhaustive table of easy
88 ;; common cases here. Or perhaps the effort would be better spent
89 ;; on smarter compiler transforms which do the calculation once
90 ;; and for all in any reasonable user programs.)
92 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
93 ((character base-char standard-char)
94 (values #.sb!vm:simple-string-widetag #.sb!vm:n-byte-bits))
96 (values #.sb!vm:simple-bit-vector-widetag 1))
97 ;; OK, we have to wade into SUBTYPEPing after all.
99 ;; FIXME: The data here are redundant with
100 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPE-PROPERTIES*.
101 (pick-vector-type type
102 (base-char (values #.sb!vm:simple-string-widetag #.sb!vm:n-byte-bits))
103 (bit (values #.sb!vm:simple-bit-vector-widetag 1))
105 (values #.sb!vm:simple-array-unsigned-byte-2-widetag 2))
107 (values #.sb!vm:simple-array-unsigned-byte-4-widetag 4))
109 (values #.sb!vm:simple-array-unsigned-byte-8-widetag 8))
111 (values #.sb!vm:simple-array-unsigned-byte-16-widetag 16))
113 (values #.sb!vm:simple-array-unsigned-byte-32-widetag 32))
115 (values #.sb!vm:simple-array-signed-byte-8-widetag 8))
117 (values #.sb!vm:simple-array-signed-byte-16-widetag 16))
119 (values #.sb!vm:simple-array-signed-byte-30-widetag 32))
121 (values #.sb!vm:simple-array-signed-byte-32-widetag 32))
122 (single-float (values #.sb!vm:simple-array-single-float-widetag 32))
123 (double-float (values #.sb!vm:simple-array-double-float-widetag 64))
126 (values #.sb!vm:simple-array-long-float-widetag
127 #!+x86 96 #!+sparc 128))
128 ((complex single-float)
129 (values #.sb!vm:simple-array-complex-single-float-widetag 64))
130 ((complex double-float)
131 (values #.sb!vm:simple-array-complex-double-float-widetag 128))
133 ((complex long-float)
134 (values #.sb!vm:simple-array-complex-long-float-widetag
137 (t (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))))))
138 (defun %complex-vector-widetag (type)
140 ;; Pick off some easy common cases.
142 #.sb!vm:complex-vector-widetag)
143 ((character base-char)
144 #.sb!vm:complex-string-widetag)
146 #.sb!vm:complex-bit-vector-widetag)
147 ;; OK, we have to wade into SUBTYPEPing after all.
149 (pick-vector-type type
150 (base-char #.sb!vm:complex-string-widetag)
151 (bit #.sb!vm:complex-bit-vector-widetag)
152 (t #.sb!vm:complex-vector-widetag)))))
154 (defun make-array (dimensions &key
156 (initial-element nil initial-element-p)
157 initial-contents adjustable fill-pointer
158 displaced-to displaced-index-offset)
159 (let* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
160 (array-rank (length (the list dimensions)))
161 (simple (and (null fill-pointer)
163 (null displaced-to))))
164 (declare (fixnum array-rank))
165 (when (and displaced-index-offset (null displaced-to))
166 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
167 (if (and simple (= array-rank 1))
168 ;; Its a (simple-array * (*))
169 (multiple-value-bind (type n-bits)
170 (%vector-widetag-and-n-bits element-type)
171 (declare (type (unsigned-byte 8) type)
172 (type (integer 1 256) n-bits))
173 (let* ((length (car dimensions))
174 (array (allocate-vector
177 (ceiling (* (if (= type sb!vm:simple-string-widetag)
181 sb!vm:n-word-bits))))
182 (declare (type index length))
183 (when initial-element-p
184 (fill array initial-element))
185 (when initial-contents
186 (when initial-element
187 (error "can't specify both :INITIAL-ELEMENT and ~
189 (unless (= length (length initial-contents))
190 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
191 the vector length is ~W."
192 (length initial-contents)
194 (replace array initial-contents))
196 ;; It's either a complex array or a multidimensional array.
197 (let* ((total-size (reduce #'* dimensions))
198 (data (or displaced-to
199 (data-vector-from-inits
200 dimensions total-size element-type
201 initial-contents initial-element initial-element-p)))
202 (array (make-array-header
203 (cond ((= array-rank 1)
204 (%complex-vector-widetag element-type))
205 (simple sb!vm:simple-array-widetag)
206 (t sb!vm:complex-array-widetag))
209 (unless (= array-rank 1)
210 (error "Only vectors can have fill pointers."))
211 (let ((length (car dimensions)))
212 (declare (fixnum length))
213 (setf (%array-fill-pointer array)
214 (cond ((eq fill-pointer t)
217 (unless (and (fixnump fill-pointer)
219 (<= fill-pointer length))
220 ;; FIXME: should be TYPE-ERROR?
221 (error "invalid fill-pointer ~W"
224 (setf (%array-fill-pointer-p array) t))
226 (setf (%array-fill-pointer array) total-size)
227 (setf (%array-fill-pointer-p array) nil)))
228 (setf (%array-available-elements array) total-size)
229 (setf (%array-data-vector array) data)
231 (when (or initial-element-p initial-contents)
232 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
233 can be specified along with :DISPLACED-TO"))
234 (let ((offset (or displaced-index-offset 0)))
235 (when (> (+ offset total-size)
236 (array-total-size displaced-to))
237 (error "~S doesn't have enough elements." displaced-to))
238 (setf (%array-displacement array) offset)
239 (setf (%array-displaced-p array) t)))
241 (setf (%array-displaced-p array) nil)))
243 (dolist (dim dimensions)
244 (setf (%array-dimension array axis) dim)
248 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
249 ;;; specified array characteristics. Dimensions is only used to pass
250 ;;; to FILL-DATA-VECTOR for error checking on the structure of
251 ;;; initial-contents.
252 (defun data-vector-from-inits (dimensions total-size element-type
253 initial-contents initial-element
255 (when (and initial-contents initial-element-p)
256 (error "cannot supply both :INITIAL-CONTENTS and :INITIAL-ELEMENT to
257 either MAKE-ARRAY or ADJUST-ARRAY."))
258 (let ((data (if initial-element-p
259 (make-array total-size
260 :element-type element-type
261 :initial-element initial-element)
262 (make-array total-size
263 :element-type element-type))))
264 (cond (initial-element-p
265 (unless (simple-vector-p data)
266 (unless (typep initial-element element-type)
267 (error "~S cannot be used to initialize an array of type ~S."
268 initial-element element-type))
269 (fill (the vector data) initial-element)))
271 (fill-data-vector data dimensions initial-contents)))
274 (defun fill-data-vector (vector dimensions initial-contents)
276 (labels ((frob (axis dims contents)
278 (setf (aref vector index) contents)
281 (unless (typep contents 'sequence)
282 (error "malformed :INITIAL-CONTENTS: ~S is not a ~
283 sequence, but ~W more layer~:P needed."
285 (- (length dimensions) axis)))
286 (unless (= (length contents) (car dims))
287 (error "malformed :INITIAL-CONTENTS: Dimension of ~
288 axis ~W is ~W, but ~S is ~W long."
289 axis (car dims) contents (length contents)))
291 (dolist (content contents)
292 (frob (1+ axis) (cdr dims) content))
293 (dotimes (i (length contents))
294 (frob (1+ axis) (cdr dims) (aref contents i))))))))
295 (frob 0 dimensions initial-contents))))
297 (defun vector (&rest objects)
299 "Construct a SIMPLE-VECTOR from the given objects."
300 (coerce (the list objects) 'simple-vector))
302 ;;;; accessor/setter functions
304 (eval-when (:compile-toplevel :execute)
305 (defparameter *specialized-array-element-types*
320 #!+long-float long-float
321 (complex single-float)
322 (complex double-float)
323 #!+long-float (complex long-float))))
325 (defun hairy-data-vector-ref (array index)
326 (with-array-data ((vector array) (index index) (end))
327 (declare (ignore end))
329 #.(mapcar (lambda (type)
330 (let ((atype `(simple-array ,type (*))))
332 (data-vector-ref (the ,atype vector)
334 *specialized-array-element-types*))))
336 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
337 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
338 ;;; definition is needed for the compiler to use in constant folding.)
339 (defun data-vector-ref (array index)
340 (hairy-data-vector-ref array index))
342 (defun hairy-data-vector-set (array index new-value)
343 (with-array-data ((vector array) (index index) (end))
344 (declare (ignore end))
346 #.(mapcar (lambda (type)
347 (let ((atype `(simple-array ,type (*))))
349 (data-vector-set (the ,atype vector)
353 ;; For specialized arrays, the return
354 ;; from data-vector-set would have to
355 ;; be reboxed to be a (Lisp) return
356 ;; value; instead, we use the
357 ;; already-boxed value as the return.
359 *specialized-array-element-types*))))
361 (defun %array-row-major-index (array subscripts
362 &optional (invalid-index-error-p t))
363 (declare (array array)
365 (let ((rank (array-rank array)))
366 (unless (= rank (length subscripts))
367 (error "wrong number of subscripts, ~W, for array of rank ~W"
368 (length subscripts) rank))
369 (if (array-header-p array)
370 (do ((subs (nreverse subscripts) (cdr subs))
371 (axis (1- (array-rank array)) (1- axis))
375 (declare (list subs) (fixnum axis chunk-size result))
376 (let ((index (car subs))
377 (dim (%array-dimension array axis)))
378 (declare (fixnum index dim))
379 (unless (< -1 index dim)
380 (if invalid-index-error-p
381 (error "invalid index ~W~[~;~:; on axis ~:*~W~] in ~S"
383 (return-from %array-row-major-index nil)))
384 (incf result (* chunk-size index))
385 (setf chunk-size (* chunk-size dim))))
386 (let ((index (first subscripts)))
387 (unless (< -1 index (length (the (simple-array * (*)) array)))
388 (if invalid-index-error-p
389 (error "invalid index ~W in ~S" index array)
390 (return-from %array-row-major-index nil)))
393 (defun array-in-bounds-p (array &rest subscripts)
395 "Return T if the Subscipts are in bounds for the Array, Nil otherwise."
396 (if (%array-row-major-index array subscripts nil)
399 (defun array-row-major-index (array &rest subscripts)
400 (%array-row-major-index array subscripts))
402 (defun aref (array &rest subscripts)
404 "Return the element of the Array specified by the Subscripts."
405 (row-major-aref array (%array-row-major-index array subscripts)))
407 (defun %aset (array &rest stuff)
408 (let ((subscripts (butlast stuff))
409 (new-value (car (last stuff))))
410 (setf (row-major-aref array (%array-row-major-index array subscripts))
413 ;;; FIXME: What's supposed to happen with functions
414 ;;; like AREF when we (DEFUN (SETF FOO) ..) when
415 ;;; DEFSETF FOO is also defined? It seems as though the logical
416 ;;; thing to do would be to nuke the macro definition for (SETF FOO)
417 ;;; and replace it with the (SETF FOO) function, issuing a warning,
418 ;;; just as for ordinary functions
419 ;;; * (LISP-IMPLEMENTATION-VERSION)
420 ;;; "18a+ release x86-linux 2.4.7 6 November 1998 cvs"
421 ;;; * (DEFMACRO ZOO (X) `(+ ,X ,X))
423 ;;; * (DEFUN ZOO (X) (* 3 X))
424 ;;; Warning: ZOO previously defined as a macro.
426 ;;; But that doesn't seem to be what happens in CMU CL.
428 ;;; Also, it would be nice to make DESCRIBE FOO tell whether a symbol
429 ;;; has a setf expansion and/or a setf function defined.
431 #!-sb-fluid (declaim (inline (setf aref)))
432 (defun (setf aref) (new-value array &rest subscripts)
433 (declare (type array array))
434 (setf (row-major-aref array (%array-row-major-index array subscripts))
437 (defun row-major-aref (array index)
439 "Return the element of array corressponding to the row-major index. This is
441 (declare (optimize (safety 1)))
442 (row-major-aref array index))
444 (defun %set-row-major-aref (array index new-value)
445 (declare (optimize (safety 1)))
446 (setf (row-major-aref array index) new-value))
448 (defun svref (simple-vector index)
450 "Return the INDEX'th element of the given Simple-Vector."
451 (declare (optimize (safety 1)))
452 (aref simple-vector index))
454 (defun %svset (simple-vector index new)
455 (declare (optimize (safety 1)))
456 (setf (aref simple-vector index) new))
458 (defun bit (bit-array &rest subscripts)
460 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
461 (declare (type (array bit) bit-array) (optimize (safety 1)))
462 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
464 (defun %bitset (bit-array &rest stuff)
465 (declare (type (array bit) bit-array) (optimize (safety 1)))
466 (let ((subscripts (butlast stuff))
467 (new-value (car (last stuff))))
468 (setf (row-major-aref bit-array
469 (%array-row-major-index bit-array subscripts))
472 #!-sb-fluid (declaim (inline (setf bit)))
473 (defun (setf bit) (new-value bit-array &rest subscripts)
474 (declare (type (array bit) bit-array) (optimize (safety 1)))
475 (setf (row-major-aref bit-array
476 (%array-row-major-index bit-array subscripts))
479 (defun sbit (simple-bit-array &rest subscripts)
481 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
482 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
483 (row-major-aref simple-bit-array
484 (%array-row-major-index simple-bit-array subscripts)))
486 ;;; KLUDGE: Not all these things (%SET-ROW-MAJOR-AREF, %SET-FILL-POINTER,
487 ;;; %SET-FDEFINITION, %SCHARSET, %SBITSET..) seem to deserve separate names.
488 ;;; Could we just DEFUN (SETF SBIT) etc. and get rid of the non-ANSI names?
490 (defun %sbitset (simple-bit-array &rest stuff)
491 (declare (type (simple-array bit) simple-bit-array) (optimize (safety 1)))
492 (let ((subscripts (butlast stuff))
493 (new-value (car (last stuff))))
494 (setf (row-major-aref simple-bit-array
495 (%array-row-major-index simple-bit-array subscripts))
498 #!-sb-fluid (declaim (inline (setf sbit)))
499 (defun (setf sbit) (new-value bit-array &rest subscripts)
500 (declare (type (simple-array bit) bit-array) (optimize (safety 1)))
501 (setf (row-major-aref bit-array
502 (%array-row-major-index bit-array subscripts))
505 ;;;; miscellaneous array properties
507 (defun array-element-type (array)
509 "Return the type of the elements of the array"
510 (let ((widetag (widetag-of array)))
511 (macrolet ((pick-element-type (&rest stuff)
512 `(cond ,@(mapcar (lambda (stuff)
514 (let ((item (car stuff)))
523 `(= widetag ,item))))
526 ;; FIXME: The data here are redundant with
527 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPE-PROPERTIES*.
529 ((sb!vm:simple-string-widetag sb!vm:complex-string-widetag) 'base-char)
530 ((sb!vm:simple-bit-vector-widetag
531 sb!vm:complex-bit-vector-widetag) 'bit)
532 (sb!vm:simple-vector-widetag t)
533 (sb!vm:simple-array-unsigned-byte-2-widetag '(unsigned-byte 2))
534 (sb!vm:simple-array-unsigned-byte-4-widetag '(unsigned-byte 4))
535 (sb!vm:simple-array-unsigned-byte-8-widetag '(unsigned-byte 8))
536 (sb!vm:simple-array-unsigned-byte-16-widetag '(unsigned-byte 16))
537 (sb!vm:simple-array-unsigned-byte-32-widetag '(unsigned-byte 32))
538 (sb!vm:simple-array-signed-byte-8-widetag '(signed-byte 8))
539 (sb!vm:simple-array-signed-byte-16-widetag '(signed-byte 16))
540 (sb!vm:simple-array-signed-byte-30-widetag '(signed-byte 30))
541 (sb!vm:simple-array-signed-byte-32-widetag '(signed-byte 32))
542 (sb!vm:simple-array-single-float-widetag 'single-float)
543 (sb!vm:simple-array-double-float-widetag 'double-float)
545 (sb!vm:simple-array-long-float-widetag 'long-float)
546 (sb!vm:simple-array-complex-single-float-widetag
547 '(complex single-float))
548 (sb!vm:simple-array-complex-double-float-widetag
549 '(complex double-float))
551 (sb!vm:simple-array-complex-long-float-widetag '(complex long-float))
552 ((sb!vm:simple-array-widetag
553 sb!vm:complex-vector-widetag
554 sb!vm:complex-array-widetag)
555 (with-array-data ((array array) (start) (end))
556 (declare (ignore start end))
557 (array-element-type array)))
559 (error 'type-error :datum array :expected-type 'array))))))
561 (defun array-rank (array)
563 "Return the number of dimensions of ARRAY."
564 (if (array-header-p array)
568 (defun array-dimension (array axis-number)
570 "Return the length of dimension AXIS-NUMBER of ARRAY."
571 (declare (array array) (type index axis-number))
572 (cond ((not (array-header-p array))
573 (unless (= axis-number 0)
574 (error "Vector axis is not zero: ~S" axis-number))
575 (length (the (simple-array * (*)) array)))
576 ((>= axis-number (%array-rank array))
577 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
578 axis-number array (%array-rank array)))
580 (%array-dimension array axis-number))))
582 (defun array-dimensions (array)
584 "Return a list whose elements are the dimensions of the array"
585 (declare (array array))
586 (if (array-header-p array)
587 (do ((results nil (cons (array-dimension array index) results))
588 (index (1- (array-rank array)) (1- index)))
589 ((minusp index) results))
590 (list (array-dimension array 0))))
592 (defun array-total-size (array)
594 "Return the total number of elements in the Array."
595 (declare (array array))
596 (if (array-header-p array)
597 (%array-available-elements array)
598 (length (the vector array))))
600 (defun array-displacement (array)
602 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
603 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
604 (declare (type array array))
605 (if (and (array-header-p array) ; if unsimple and
606 (%array-displaced-p array)) ; displaced
607 (values (%array-data-vector array) (%array-displacement array))
610 (defun adjustable-array-p (array)
612 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
613 to the argument, this happens for complex arrays."
614 (declare (array array))
615 (not (typep array 'simple-array)))
617 ;;;; fill pointer frobbing stuff
619 (defun array-has-fill-pointer-p (array)
621 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
622 (declare (array array))
623 (and (array-header-p array) (%array-fill-pointer-p array)))
625 (defun fill-pointer (vector)
627 "Return the FILL-POINTER of the given VECTOR."
628 (declare (vector vector))
629 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
630 (%array-fill-pointer vector)
631 (error 'simple-type-error
633 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
634 :format-control "~S is not an array with a fill pointer."
635 :format-arguments (list vector))))
637 (defun %set-fill-pointer (vector new)
638 (declare (vector vector) (fixnum new))
639 (if (and (array-header-p vector) (%array-fill-pointer-p vector))
640 (if (> new (%array-available-elements vector))
642 "The new fill pointer, ~S, is larger than the length of the vector."
644 (setf (%array-fill-pointer vector) new))
645 (error 'simple-type-error
647 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
648 :format-control "~S is not an array with a fill pointer."
649 :format-arguments (list vector))))
651 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
652 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
653 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
654 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
656 (defun vector-push (new-el array)
658 "Attempt to set the element of ARRAY designated by its fill pointer
659 to NEW-EL, and increment the fill pointer by one. If the fill pointer is
660 too large, NIL is returned, otherwise the index of the pushed element is
662 (declare (vector array))
663 (let ((fill-pointer (fill-pointer array)))
664 (declare (fixnum fill-pointer))
665 (cond ((= fill-pointer (%array-available-elements array))
668 (setf (aref array fill-pointer) new-el)
669 (setf (%array-fill-pointer array) (1+ fill-pointer))
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 "Decrease the fill pointer by 1 and return the element pointed to by the
689 (declare (vector array))
690 (let ((fill-pointer (fill-pointer array)))
691 (declare (fixnum fill-pointer))
692 (if (zerop fill-pointer)
693 (error "There is nothing left to pop.")
695 (setf (%array-fill-pointer array)
696 (1- fill-pointer))))))
700 (defun adjust-array (array dimensions &key
701 (element-type (array-element-type array))
702 (initial-element nil initial-element-p)
703 initial-contents fill-pointer
704 displaced-to displaced-index-offset)
706 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
707 (let ((dimensions (if (listp dimensions) dimensions (list dimensions))))
708 (cond ((/= (the fixnum (length (the list dimensions)))
709 (the fixnum (array-rank array)))
710 (error "The number of dimensions not equal to rank of array."))
711 ((not (subtypep element-type (array-element-type array)))
712 (error "The new element type, ~S, is incompatible with old type."
714 (let ((array-rank (length (the list dimensions))))
715 (declare (fixnum array-rank))
716 (when (and fill-pointer (> array-rank 1))
717 (error "Multidimensional arrays can't have fill pointers."))
718 (cond (initial-contents
719 ;; array former contents replaced by INITIAL-CONTENTS
720 (if (or initial-element-p displaced-to)
721 (error "INITIAL-CONTENTS may not be specified with ~
722 the :INITIAL-ELEMENT or :DISPLACED-TO option."))
723 (let* ((array-size (apply #'* dimensions))
724 (array-data (data-vector-from-inits
725 dimensions array-size element-type
726 initial-contents initial-element
728 (if (adjustable-array-p array)
729 (set-array-header array array-data array-size
730 (get-new-fill-pointer array array-size
733 (if (array-header-p array)
734 ;; simple multidimensional or single dimensional array
735 (make-array dimensions
736 :element-type element-type
737 :initial-contents initial-contents)
740 ;; We already established that no INITIAL-CONTENTS was supplied.
741 (when initial-element
742 (error "The :INITIAL-ELEMENT option may not be specified ~
743 with :DISPLACED-TO."))
744 (unless (subtypep element-type (array-element-type displaced-to))
745 (error "can't displace an array of type ~S into another of ~
747 element-type (array-element-type displaced-to)))
748 (let ((displacement (or displaced-index-offset 0))
749 (array-size (apply #'* dimensions)))
750 (declare (fixnum displacement array-size))
751 (if (< (the fixnum (array-total-size displaced-to))
752 (the fixnum (+ displacement array-size)))
753 (error "The :DISPLACED-TO array is too small."))
754 (if (adjustable-array-p array)
755 ;; None of the original contents appear in adjusted array.
756 (set-array-header array displaced-to array-size
757 (get-new-fill-pointer array array-size
759 displacement dimensions t)
760 ;; simple multidimensional or single dimensional array
761 (make-array dimensions
762 :element-type element-type
763 :displaced-to displaced-to
764 :displaced-index-offset
765 displaced-index-offset))))
767 (let ((old-length (array-total-size array))
768 (new-length (car dimensions))
770 (declare (fixnum old-length new-length))
771 (with-array-data ((old-data array) (old-start)
772 (old-end old-length))
773 (cond ((or (%array-displaced-p array)
774 (< old-length new-length))
776 (data-vector-from-inits
777 dimensions new-length element-type
778 initial-contents initial-element
780 (replace new-data old-data
781 :start2 old-start :end2 old-end))
783 (shrink-vector old-data new-length))))
784 (if (adjustable-array-p array)
785 (set-array-header array new-data new-length
786 (get-new-fill-pointer array new-length
791 (let ((old-length (%array-available-elements array))
792 (new-length (apply #'* dimensions)))
793 (declare (fixnum old-length new-length))
794 (with-array-data ((old-data array) (old-start)
795 (old-end old-length))
796 (declare (ignore old-end))
797 (let ((new-data (if (or (%array-displaced-p array)
798 (> new-length old-length))
799 (data-vector-from-inits
800 dimensions new-length
801 element-type () initial-element
804 (if (or (zerop old-length) (zerop new-length))
805 (when initial-element-p (fill new-data initial-element))
806 (zap-array-data old-data (array-dimensions array)
808 new-data dimensions new-length
809 element-type initial-element
811 (set-array-header array new-data new-length
812 new-length 0 dimensions nil)))))))))
814 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
815 (cond ((not fill-pointer)
816 (when (array-has-fill-pointer-p old-array)
817 (when (> (%array-fill-pointer old-array) new-array-size)
818 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
819 smaller than its fill pointer (~S)"
820 old-array new-array-size (fill-pointer old-array)))
821 (%array-fill-pointer old-array)))
822 ((not (array-has-fill-pointer-p old-array))
823 (error "cannot supply a non-NIL value (~S) for :FILL-POINTER ~
824 in ADJUST-ARRAY unless the array (~S) was originally ~
825 created with a fill pointer"
828 ((numberp fill-pointer)
829 (when (> fill-pointer new-array-size)
830 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
831 than the new length of the vector (~S)"
832 fill-pointer new-array-size))
837 (error "bogus value for :FILL-POINTER in ADJUST-ARRAY: ~S"
840 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
841 ;;; which must be less than or equal to its current length.
842 (defun shrink-vector (vector new-length)
843 (declare (vector vector))
844 (unless (array-header-p vector)
845 (macrolet ((frob (name &rest things)
847 ,@(mapcar (lambda (thing)
848 (destructuring-bind (type-spec fill-value)
851 (fill (truly-the ,type-spec ,name)
853 :start new-length))))
855 ;; FIXME: The associations between vector types and initial
856 ;; values here are redundant with
857 ;; *SPECIALIZED-ARRAY-ELEMENT-TYPE-PROPERTIES*.
860 (simple-base-string #.*default-init-char-form*)
861 (simple-bit-vector 0)
862 ((simple-array (unsigned-byte 2) (*)) 0)
863 ((simple-array (unsigned-byte 4) (*)) 0)
864 ((simple-array (unsigned-byte 8) (*)) 0)
865 ((simple-array (unsigned-byte 16) (*)) 0)
866 ((simple-array (unsigned-byte 32) (*)) 0)
867 ((simple-array (signed-byte 8) (*)) 0)
868 ((simple-array (signed-byte 16) (*)) 0)
869 ((simple-array (signed-byte 30) (*)) 0)
870 ((simple-array (signed-byte 32) (*)) 0)
871 ((simple-array single-float (*)) (coerce 0 'single-float))
872 ((simple-array double-float (*)) (coerce 0 'double-float))
874 ((simple-array long-float (*)) (coerce 0 'long-float))
875 ((simple-array (complex single-float) (*))
876 (coerce 0 '(complex single-float)))
877 ((simple-array (complex double-float) (*))
878 (coerce 0 '(complex double-float)))
880 ((simple-array (complex long-float) (*))
881 (coerce 0 '(complex long-float))))))
882 ;; Only arrays have fill-pointers, but vectors have their length
883 ;; parameter in the same place.
884 (setf (%array-fill-pointer vector) new-length)
887 ;;; Fill in array header with the provided information, and return the array.
888 (defun set-array-header (array data length fill-pointer displacement dimensions
889 &optional displacedp)
890 (setf (%array-data-vector array) data)
891 (setf (%array-available-elements array) length)
893 (setf (%array-fill-pointer array) fill-pointer)
894 (setf (%array-fill-pointer-p array) t))
896 (setf (%array-fill-pointer array) length)
897 (setf (%array-fill-pointer-p array) nil)))
898 (setf (%array-displacement array) displacement)
899 (if (listp dimensions)
900 (dotimes (axis (array-rank array))
901 (declare (type index axis))
902 (setf (%array-dimension array axis) (pop dimensions)))
903 (setf (%array-dimension array 0) dimensions))
904 (setf (%array-displaced-p array) displacedp)
907 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
909 ;;; a temporary to be used when OLD-DATA and NEW-DATA are EQ.
910 ;;; KLUDGE: Boy, DYNAMIC-EXTENT would be nice.
911 (defvar *zap-array-data-temp* (make-array 1000 :initial-element t))
913 (defun zap-array-data-temp (length element-type initial-element
915 (declare (fixnum length))
916 (when (> length (the fixnum (length *zap-array-data-temp*)))
917 (setf *zap-array-data-temp*
918 (make-array length :initial-element t)))
919 (when initial-element-p
920 (unless (typep initial-element element-type)
921 (error "~S can't be used to initialize an array of type ~S."
922 initial-element element-type))
923 (fill (the simple-vector *zap-array-data-temp*) initial-element
925 *zap-array-data-temp*)
927 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
928 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
929 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
930 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
931 ;;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and INITIAL-ELEMENT-P
932 ;;; are used when OLD-DATA and NEW-DATA are EQ; in this case, a
933 ;;; temporary must be used and filled appropriately. When OLD-DATA and
934 ;;; NEW-DATA are not EQ, NEW-DATA has already been filled with any
935 ;;; specified initial-element.
936 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
937 element-type initial-element initial-element-p)
938 (declare (list old-dims new-dims))
939 (setq old-dims (nreverse old-dims))
940 (setq new-dims (reverse new-dims))
941 (if (eq old-data new-data)
942 (let ((temp (zap-array-data-temp new-length element-type
943 initial-element initial-element-p)))
944 (zap-array-data-aux old-data old-dims offset temp new-dims)
945 (dotimes (i new-length) (setf (aref new-data i) (aref temp i))))
946 (zap-array-data-aux old-data old-dims offset new-data new-dims)))
948 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
949 (declare (fixnum offset))
950 (let ((limits (mapcar (lambda (x y)
951 (declare (fixnum x y))
952 (1- (the fixnum (min x y))))
954 (macrolet ((bump-index-list (index limits)
955 `(do ((subscripts ,index (cdr subscripts))
956 (limits ,limits (cdr limits)))
957 ((null subscripts) nil)
958 (cond ((< (the fixnum (car subscripts))
959 (the fixnum (car limits)))
961 (1+ (the fixnum (car subscripts))))
963 (t (rplaca subscripts 0))))))
964 (do ((index (make-list (length old-dims) :initial-element 0)
965 (bump-index-list index limits)))
967 (setf (aref new-data (row-major-index-from-dims index new-dims))
969 (+ (the fixnum (row-major-index-from-dims index old-dims))
972 ;;; Figure out the row-major-order index of an array reference from a
973 ;;; list of subscripts and a list of dimensions. This is for internal
974 ;;; calls only, and the subscripts and dim-list variables are assumed
975 ;;; to be reversed from what the user supplied.
976 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
977 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
978 (rev-dim-list rev-dim-list (cdr rev-dim-list))
981 ((null rev-dim-list) result)
982 (declare (fixnum chunk-size result))
983 (setq result (+ result
984 (the fixnum (* (the fixnum (car rev-subscripts))
986 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
990 (defun bit-array-same-dimensions-p (array1 array2)
991 (declare (type (array bit) array1 array2))
992 (and (= (array-rank array1)
994 (dotimes (index (array-rank array1) t)
995 (when (/= (array-dimension array1 index)
996 (array-dimension array2 index))
999 (defun pick-result-array (result-bit-array bit-array-1)
1000 (case result-bit-array
1002 ((nil) (make-array (array-dimensions bit-array-1)
1004 :initial-element 0))
1006 (unless (bit-array-same-dimensions-p bit-array-1
1008 (error "~S and ~S don't have the same dimensions."
1009 bit-array-1 result-bit-array))
1012 (defmacro def-bit-array-op (name function)
1013 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1015 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1016 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1017 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1018 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1019 All the arrays must have the same rank and dimensions."
1020 (symbol-name function))
1021 (declare (type (array bit) bit-array-1 bit-array-2)
1022 (type (or (array bit) (member t nil)) result-bit-array))
1023 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1024 (error "~S and ~S don't have the same dimensions."
1025 bit-array-1 bit-array-2))
1026 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1027 (if (and (simple-bit-vector-p bit-array-1)
1028 (simple-bit-vector-p bit-array-2)
1029 (simple-bit-vector-p result-bit-array))
1030 (locally (declare (optimize (speed 3) (safety 0)))
1031 (,name bit-array-1 bit-array-2 result-bit-array))
1032 (with-array-data ((data1 bit-array-1) (start1) (end1))
1033 (declare (ignore end1))
1034 (with-array-data ((data2 bit-array-2) (start2) (end2))
1035 (declare (ignore end2))
1036 (with-array-data ((data3 result-bit-array) (start3) (end3))
1037 (do ((index-1 start1 (1+ index-1))
1038 (index-2 start2 (1+ index-2))
1039 (index-3 start3 (1+ index-3)))
1040 ((>= index-3 end3) result-bit-array)
1041 (declare (type index index-1 index-2 index-3))
1042 (setf (sbit data3 index-3)
1043 (logand (,function (sbit data1 index-1)
1044 (sbit data2 index-2))
1047 (def-bit-array-op bit-and logand)
1048 (def-bit-array-op bit-ior logior)
1049 (def-bit-array-op bit-xor logxor)
1050 (def-bit-array-op bit-eqv logeqv)
1051 (def-bit-array-op bit-nand lognand)
1052 (def-bit-array-op bit-nor lognor)
1053 (def-bit-array-op bit-andc1 logandc1)
1054 (def-bit-array-op bit-andc2 logandc2)
1055 (def-bit-array-op bit-orc1 logorc1)
1056 (def-bit-array-op bit-orc2 logorc2)
1058 (defun bit-not (bit-array &optional result-bit-array)
1060 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1061 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1062 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1063 created. Both arrays must have the same rank and dimensions."
1064 (declare (type (array bit) bit-array)
1065 (type (or (array bit) (member t nil)) result-bit-array))
1066 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1067 (if (and (simple-bit-vector-p bit-array)
1068 (simple-bit-vector-p result-bit-array))
1069 (locally (declare (optimize (speed 3) (safety 0)))
1070 (bit-not bit-array result-bit-array))
1071 (with-array-data ((src bit-array) (src-start) (src-end))
1072 (declare (ignore src-end))
1073 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1074 (do ((src-index src-start (1+ src-index))
1075 (dst-index dst-start (1+ dst-index)))
1076 ((>= dst-index dst-end) result-bit-array)
1077 (declare (type index src-index dst-index))
1078 (setf (sbit dst dst-index)
1079 (logxor (sbit src src-index) 1))))))))