Next: , Previous: Reader macro and function <code>grid</code>, Up: Creating a grid


4.2.2 The foreign-array class

The foreign-array class uses CFFI to allow the use of C arrays as grids.

Classes of vectors and matrices are named by appending the element type as hypenated words to "vector" or "matrix". The following table shows the classes available on a 64-bit platform:

Element type Vector class Matrix class #m prefix
double-float vector-double-float matrix-double-float 1 or empty
(complex double-float) vector-complex-double-float matrix-complex-double-float 2
single-float vector-single-float matrix-single-float 3
(complex single-float) vector-complex-single-float matrix-complex-single-float 4
(signed-byte 8) vector-signed-byte-8 matrix-signed-byte-8 7
(unsigned-byte 8) vector-unsigned-byte-8 matrix-unsigned-byte-8 8
(signed-byte 16) vector-signed-byte-16 matrix-signed-byte-16 15
(unsigned-byte 16) vector-unsigned-byte-16 matrix-unsigned-byte-16 16
(signed-byte 32) vector-signed-byte-32 matrix-signed-byte-32 31
(unsigned-byte 32) vector-unsigned-byte-32 matrix-unsigned-byte-32 32
(signed-byte 64) vector-signed-byte-64 matrix-signed-byte-64 63
(unsigned-byte 64) vector-unsigned-byte-64 matrix-unsigned-byte-64 64

A foreign array is especially useful when sending or receiving data from a foreign library. In that case, the generic function grid:foreign-pointer should be called on the foreign array in order to obtain the pointer that the foreign library needs. The identical contents as a CL array is available with the function cl-array under two conditions: the static-vectors system is installed and supported for the CL implementation used, and foreign array was not made with make-foreign-array-from-pointer. If it does exist, this array is the same as the foreign array (not a copy), so for example changing one changes the other:

     
     ANTIK-USER> (defparameter *my-fa* (make-foreign-array 'double-float :dimensions 3))
     ANTIK-USER> *my-fa*
     #m(0.000000000000000d0 1.000000000000000d0 2.000000000000000d0)
     ANTIK-USER> (cl-array *my-fa*)
     #(0.0 1.0 2.0)
     ANTIK-USER> (setf (grid:aref *my-fa* 1) 33.3d0)
     33.3
     ANTIK-USER>  *my-fa*
     #m(0.000000000000000d0 33.300000000000000d0 2.000000000000000d0)
     ANTIK-USER> (cl-array *my-fa*)
     #(0.0 33.3 2.0)
     ANTIK-USER> (setf (aref (cl-array *my-fa*) 1) 22.2d0)
     22.2
     ANTIK-USER> *my-fa*
     #m(0.000000000000000d0 22.200000000000000d0 2.000000000000000d0)