Debian Codegraph @

src/lsp/arraylib.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 ;;;; arraylib.lsp
12 ;;;;
13 ;;;; array routines
14 (in-package "SYSTEM")
15
16 (defun make-array (dimensions
17 &key (element-type t)
18 (initial-element nil initial-element-supplied-p)
19 (initial-contents nil initial-contents-supplied-p)
20 adjustable fill-pointer
21 displaced-to (displaced-index-offset 0))
22 "Args: (dimensions &key (element-type t) initial-element (initial-contents nil)
23 (adjustable nil) (fill-pointer nil) (displaced-to nil)
24 (displaced-index-offset 0) (static nil))
25 Creates an array of the specified DIMENSIONS. DIMENSIONS is a list of
26 non-negative integers each representing the length of the corresponding
27 dimension. It may be an integer for vectors, i.e., one-dimensional arrays.
28 ELEMENT-TYPE specifies the type of array elements. INITIAL-ELEMENT specifies
29 the initial value for all elements. Its default value depends on ELEMENT-
30 TYPE. INITIAL-CONTENTS specifies each element in terms of sequences.
31 ADJUSTABLE specifies whether or not the array is adjustable (see ADJUST-
32 ARRAY). FILL-POINTER is meaningful only for vectors. It specifies whether
33 the vector has fill-pointer or not, and if it has, the initial value of the
34 fill-pointer. Possible values are NIL (no fill-pointer), T (the length of the
35 vector), or an integer. See VECTOR-PUSH and VECTOR-POP. DISPLACED-TO, if
36 non-NIL, must be an array and specifies that the new array is displaced to the
37 given array. DISPLACED-INDEX-OFFSET is meaningful only when DISPLACED-TO is
38 non-NIL and specifies that the reference to the I-th element of the new array
39 in raw-major indexing is actually the reference to the (I + DISPLACED-INDEX-
40 OFFSET)th element of the given array.If the STATIC argument is supplied
41 with a non-nil value, then the body of the array is allocated as a
42 contiguous block."
43 (let ((x (sys:make-pure-array element-type dimensions adjustable
44 fill-pointer displaced-to displaced-index-offset)))
45 (declare (array x))
46 (cond (initial-element-supplied-p
47 (when initial-contents-supplied-p
48 (error "MAKE-ARRAY: Cannot supply both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
49 (fill-array-with-elt x initial-element 0 nil))
50 (initial-contents-supplied-p
51 (fill-array-with-seq x initial-contents))
52 (t
53 x))))
54
55 (defun fill-array-with-seq (array initial-contents)
56 (declare (array array)
57 (sequence initial-contents)
58 (optimize (safety 0))
59 (si::c-local))
60 (labels ((iterate-over-contents (array contents dims written)
61 (declare (fixnum written)
62 (array array)
63 (optimize (safety 0)))
64 (when (/= (length contents) (first dims))
65 (error "In MAKE-ARRAY: the elements in :INITIAL-CONTENTS do not match the array dimensions"))
66 (if (= (length dims) 1)
67 (do* ((it (make-seq-iterator contents) (seq-iterator-next contents it)))
68 ((null it))
69 (sys:row-major-aset array written (seq-iterator-ref contents it))
70 (incf written))
71 (do* ((it (make-seq-iterator contents) (seq-iterator-next contents it)))
72 ((null it))
73 (setf written (iterate-over-contents array
74 (seq-iterator-ref contents it)
75 (rest dims)
76 written))))
77 written))
78 (let ((dims (array-dimensions array)))
79 (if dims
80 (iterate-over-contents array initial-contents dims 0)
81 (setf (aref array) initial-contents))))
82 array)
83
84
85 (defun vector (&rest objects)
86 "Args: (&rest objects)
87 Creates and returns a simple-vector, with the N-th OBJECT being the N-th
88 element."
89 (let ((a (si:make-vector t (length objects) nil nil nil 0)))
90 (fill-array-with-seq a objects)))
91
92 (defun array-dimensions (array)
93 "Args: (array)
94 Returns a list whose N-th element is the length of the N-th dimension of ARRAY."
95 (do ((i (array-rank array))
96 (d nil))
97 ((= i 0) d)
98 (declare (fixnum i)
99 (optimize (safety 0)))
100 (push (array-dimension array (decf i)) d)))
101
102
103 (defun array-in-bounds-p (array &rest indices)
104 "Args: (array &rest indexes)
105 Returns T if INDEXes are valid indexes of ARRAY; NIL otherwise. The number of
106 INDEXes must be equal to the rank of ARRAY."
107 (declare (type array array)
108 (optimize (safety 0))
109 (ext:check-arguments-type))
110 (do* ((indices indices (cons-cdr indices))
111 (r (array-rank array))
112 (i 0 (1+ i)))
113 ((>= i r) t)
114 (declare (type index r i))
115 (if indices
116 (let* ((index (cons-car indices)))
117 (when (or (not (si::fixnump index))
118 (minusp (truly-the fixnum index))
119 (>= (truly-the fixnum index) (array-dimension array i)))
120 (return nil)))
121 (error "The rank of the array is ~R,~%~
122 ~7@Tbut ~R ~:*~[indices are~;index is~:;indices are~] ~
123 supplied."
124 r i))))
125
126 (defun row-major-index-inner (array indices)
127 (declare (optimize (safety 0))
128 (array array)
129 (si::c-local))
130 (flet ((indexing-error (array indices)
131 (error "Not valid index or indices~%~A~%into array~%~A" indices array)))
132 (do* ((r (array-rank array))
133 (i 0 (1+ i))
134 (j 0)
135 (s indices (cons-cdr s)))
136 ((null s)
137 (when (< i r)
138 (indexing-error array indices))
139 j)
140 (declare (ext:array-index j)
141 (fixnum i r))
142 (let* ((d (array-dimension array i))
143 (o (cons-car s))
144 ndx)
145 (declare (ext:array-index ndx))
146 (unless (and (typep o 'fixnum)
147 (<= 0 (setf ndx o))
148 (< ndx (array-dimension array i)))
149 (indexing-error array indices))
150 (setf j (* j d)
151 j (+ j ndx))))))
152
153 (defun array-row-major-index (array &rest indices)
154 "Args: (array &rest indexes)
155 Returns the non-negative integer that represents the location of the element
156 of ARRAY specified by INDEXes, assuming all elements of ARRAY are aligned in
157 row-major order."
158 (declare (array array)
159 (ext:check-arguments-type))
160 (row-major-index-inner array indices))
161
162
163 (defun bit (bit-array &rest indices)
164 "Args: (bit-array &rest indexes)
165 Returns the bit of BIT-ARRAY specified by INDEXes."
166 (declare (array bit-array) ;; FIXME! Should be (simple-array bit)
167 (ext:check-arguments-type))
168 (row-major-aref bit-array (row-major-index-inner bit-array indices)))
169
170
171 (defun sbit (bit-array &rest indices)
172 "Args: (simple-bit-array &rest subscripts)
173 Returns the specified bit in SIMPLE-BIT-ARRAY."
174 (declare (array bit-array) ;; FIXME! Should be (simple-array bit)
175 (ext:check-arguments-type))
176 (row-major-aref bit-array (row-major-index-inner bit-array indices)))
177
178
179 (defun bit-and (bit-array1 bit-array2 &optional result-bit-array)
180 "Args: (bit-array1 bit-array2 &optional (result nil))
181 Returns the element-wise AND of BIT-ARRAY1 and BIT-ARRAY2. Puts the results
182 into a new bit-array if RESULT is NIL, into BIT-ARRAY1 if RESULT is T, or into
183 RESULT if RESULT is a bit-array."
184 (bit-array-op boole-and bit-array1 bit-array2 result-bit-array))
185
186
187 (defun bit-ior (bit-array1 bit-array2 &optional result-bit-array)
188 "Args: (bit-array1 bit-array2 &optional (result nil))
189 Returns the element-wise INCLUSIVE OR of BIT-ARRAY1 and BIT-ARRAY2. Puts the
190 results into a new bit-array if RESULT is NIL, into BIT-ARRAY1 if RESULT is T,
191 or into RESULT if RESULT is a bit-array."
192 (bit-array-op boole-ior bit-array1 bit-array2 result-bit-array))
193
194
195 (defun bit-xor (bit-array1 bit-array2 &optional result-bit-array)
196 "Args: (bit-array1 bit-array2 &optional (result nil))
197 Returns the element-wise EXCLUSIVE OR of BIT-ARRAY1 and BIT-ARRAY2. Puts the
198 results into a new bit-array if RESULT is NIL, into BIT-ARRAY1 if RESULT is T,
199 or into RESULT if RESULT is a bit-array."
200 (bit-array-op boole-xor bit-array1 bit-array2 result-bit-array))
201
202
203 (defun bit-eqv (bit-array1 bit-array2 &optional result-bit-array)
204 "Args: (bit-array1 bit-array2 &optional (result nil))
205 Returns the element-wise EQUIVALENCE of BIT-ARRAY1 and BIT-ARRAY2. Puts the
206 results into a new bit-array if RESULT is NIL, into BIT-ARRAY1 if RESULT is T,
207 or into RESULT if RESULT is a bit-array."
208 (bit-array-op boole-eqv bit-array1 bit-array2 result-bit-array))
209
210
211 (defun bit-nand (bit-array1 bit-array2 &optional result-bit-array)
212 "Args: (bit-array1 bit-array2 &optional (result nil))
213 Returns the element-wise NOT of {the element-wise AND of BIT-ARRAY1 and BIT-
214 ARRAY2}. Puts the results into a new bit-array if RESULT is NIL, into BIT-
215 ARRAY1 if RESULT is T, or into RESULT if RESULT is a bit-array."
216 (bit-array-op boole-nand bit-array1 bit-array2 result-bit-array))
217
218
219 (defun bit-nor (bit-array1 bit-array2 &optional result-bit-array)
220 "Args: (bit-array1 bit-array2 &optional (result nil))
221 Returns the element-wise NOT of {the element-wise INCLUSIVE OR of BIT-ARRAY1
222 and BIT-ARRAY2}. Puts the results into a new bit-array if RESULT is NIL, into
223 BIT-ARRAY1 if RESULT is T, or into RESULT if RESULT is a bit-array."
224 (bit-array-op boole-nor bit-array1 bit-array2 result-bit-array))
225
226
227 (defun bit-andc1 (bit-array1 bit-array2 &optional result-bit-array)
228 "Args: (bit-array1 bit-array2 &optional (result nil))
229 Returns the element-wise AND of {the element-wise NOT of BIT-ARRAY1} and BIT-
230 ARRAY2. Puts the results into a new bit-array if RESULT is NIL, into BIT-
231 ARRAY1 if RESULT is T, or into RESULT if RESULT is a bit-array."
232 (bit-array-op boole-andc1 bit-array1 bit-array2 result-bit-array))
233
234
235 (defun bit-andc2 (bit-array1 bit-array2 &optional result-bit-array)
236 "Args: (bit-array1 bit-array2 &optional (result nil))
237 Returns the element-wise AND of BIT-ARRAY1 and {the element-wise NOT of BIT-
238 ARRAY2}. Puts the results into a new bit-array if RESULT is NIL, into BIT-
239 ARRAY1 if RESULT is T, or into RESULT if RESULT is a bit-array."
240 (bit-array-op boole-andc2 bit-array1 bit-array2 result-bit-array))
241
242
243 (defun bit-orc1 (bit-array1 bit-array2 &optional result-bit-array)
244 "Args: (bit-array1 bit-array2 &optional (result nil))
245 Returns the element-wise INCLUSIVE OR of {the element-wise NOT of BIT-ARRAY1}
246 and BIT-ARRAY2. Puts the results into a new bit-array if RESULT is NIL, into
247 BIT-ARRAY1 if RESULT is T, or into RESULT if RESULT is a bit-array."
248 (bit-array-op boole-orc1 bit-array1 bit-array2 result-bit-array))
249
250
251 (defun bit-orc2 (bit-array1 bit-array2 &optional result-bit-array)
252 "Args: (bit-array1 bit-array2 &optional (result nil))
253 Returns the element-wise INCLUSIVE OR of BIT-ARRAY1 and {the element-wise NOT
254 of BIT-ARRAY2}. Puts the results into a new bit-array if RESULT is NIL, into
255 BIT-ARRAY1 if RESULT is T, or into RESULT if RESULT is a bit-array."
256 (bit-array-op boole-orc2 bit-array1 bit-array2 result-bit-array))
257
258
259 (defun bit-not (bit-array &optional result-bit-array)
260 "Args: (bit-array &optional (result nil))
261 Returns the element-wise NOT of BIT-ARRAY. Puts the results into a new bit-
262 array if RESULT is NIL, into BIT-ARRAY if RESULT is T, or into RESULT if
263 RESULT is a bit-array."
264 (bit-array-op boole-c1 bit-array bit-array result-bit-array))
265
266
267 (defun vector-pop (vector)
268 "Args: (vector)
269 Decrements the fill-pointer of VECTOR by one and returns the element pointed
270 to by the new fill-pointer. Signals an error if the old value of the fill-
271 pointer is 0 already."
272 ;; FILL-POINTER asserts vector is a vector and has fill pointer
273 (let* ((fp (fill-pointer vector))
274 (vector (truly-the vector vector)))
275 (declare (ext:array-index fp)
276 (optimize (safety 0)))
277 (when (zerop fp)
278 (error "The fill pointer of the vector ~S zero." vector))
279 (sys:fill-pointer-set vector (decf fp))
280 (aref vector fp)))
281
282 (defun copy-array-contents (dest orig)
283 (declare (si::c-local)
284 (array dest orig)
285 (optimize (safety 0)))
286 (labels
287 ((do-copy (dest orig dims1 dims2 start1 start2)
288 (declare (array dest orig)
289 (list dims1 dims2)
290 (ext:array-index start1 start2))
291 (let* ((d1 (pop dims1))
292 (d2 (pop dims2))
293 (l (min d1 d2))
294 (i1 start1)
295 (i2 start2))
296 (declare (ext:array-index d1 d2 l i1 i2))
297 (if (null dims1)
298 (copy-subarray dest i1 orig i2 l)
299 (let ((step1 (apply #'* dims1))
300 (step2 (apply #'* dims2)))
301 (declare (ext:array-index step1 step2))
302 (dotimes (i l)
303 (declare (ext:array-index i))
304 (do-copy dest orig dims1 dims2 i1 i2)
305 (incf i1 step1)
306 (incf i2 step2)))))))
307 ;; We have to lie to DO-COPY reshaping the zero-dimensional array
308 ;; as a one-dimensional array of one element.
309 (do-copy dest orig (or (array-dimensions dest) '(1))
310 (or (array-dimensions orig) '(1))
311 0 0)))
312
313 (defun adjust-array (array new-dimensions
314 &rest r
315 &key (element-type (array-element-type array))
316 initial-element
317 initial-contents
318 fill-pointer
319 displaced-to
320 displaced-index-offset)
321 "Args: (array dimensions
322 &key (element-type (array-element-type array))
323 initial-element (initial-contents nil) (fill-pointer nil)
324 (displaced-to nil) (displaced-index-offset 0))
325 Adjusts the dimensions of ARRAY to the given DIMENSIONS. ARRAY must be an
326 adjustable array."
327 (declare (ignore initial-element
328 displaced-index-offset))
329 (when (integerp new-dimensions)
330 (setq new-dimensions (list new-dimensions)))
331 ;; FILL-POINTER = NIL means use the old value of the fill pointer
332 ;; Cannot set a fill pointer for an array that does not have any.
333 (if (array-has-fill-pointer-p array)
334 (unless fill-pointer
335 (setf r (list* :fill-pointer (fill-pointer array) r)))
336 (when fill-pointer
337 (error 'simple-type-error
338 :datum array
339 :expected-type '(satisfies array-has-fill-pointer-p)
340 :format-control "You supplied a fill pointer for an array without it.")))
341 (let ((x (apply #'make-array new-dimensions :adjustable t :element-type element-type r)))
342 (declare (array x))
343 (unless (or displaced-to initial-contents)
344 (copy-array-contents x array))
345 (sys:replace-array array x)
346 ))
347
348 ;;; Copied from cmuci-compat.lisp of CLSQL by Kevin M. Rosenberg (LLGPL-licensed)
349 (defun shrink-vector (vec len)
350 "Shrinks a vector."
351 (cond ((adjustable-array-p vec)
352 (adjust-array vec len))
353 ((typep vec 'simple-array)
354 (let ((new-vec (make-array len :element-type (array-element-type vec))))
355 (copy-subarray new-vec 0 vec 0 len)))
356 ((typep vec 'vector)
357 (setf (fill-pointer vec) len)
358 vec)
359 (t
360 (error "Unable to shrink vector ~S which is type-of ~S" vec (type-of vec)))
361 ))