| 1 |
@node Arrays |
| 2 |
@section Arrays |
| 3 |
@cindex Arrays |
| 4 |
|
| 5 |
@menu |
| 6 |
* Arrays - Array limits:: |
| 7 |
* Arrays - Specializations:: |
| 8 |
* Arrays - C Reference:: |
| 9 |
@end menu |
| 10 |
|
| 11 |
@node Arrays - Array limits |
| 12 |
@subsection Array limits |
| 13 |
ECL arrays can have up to 64 dimensions. Common-Lisp constants related to arrays have the following values in ECL. |
| 14 |
@multitable @columnfractions .5 .5 |
| 15 |
@headitem Constant @tab Value |
| 16 |
@item array-rank-limit @tab 64 |
| 17 |
@item array-dimension-limit @tab most-positive-fixnum |
| 18 |
@item array-total-size-limit @tab array-dimension-limit |
| 19 |
@end multitable |
| 20 |
|
| 21 |
@node Arrays - Specializations |
| 22 |
@subsection Specializations |
| 23 |
When the elements of an array are declared to have some precise type, such as a small or large integer, a character or a floating point number, ECL has means to store those elements in a more compact form, known as a @emph{specialized array}. The list of types for which ECL specializes arrays is platform dependent, but is summarized in the following table, together with the C type which is used internally and the expected size. |
| 24 |
|
| 25 |
@multitable @columnfractions .33 .33 .33 |
| 26 |
@headitem Specialized type @tab Element C type @tab Size |
| 27 |
@item bit @tab - @tab 1 bit |
| 28 |
@item character @tab unsigned char or uint32_t @tab Depends on character range |
| 29 |
@item base-char @tab unsigned char @tab |
| 30 |
@item fixnum @tab cl_fixnum @tab Machine word (32 or 64 bits) |
| 31 |
@item ext:cl-index @tab cl_index @tab Machine word (32 or 64 bits) |
| 32 |
@item (signed-byte 8) @tab int8_t @tab 8 bits |
| 33 |
@item (unsigned-byte 8) @tab uint8_t @tab 8 bits |
| 34 |
@item (signed-byte 16) @tab int16_t @tab 16 bits |
| 35 |
@item (unsigned-byte 16) @tab uint16_t @tab 16 bits |
| 36 |
@item (signed-byte 32) @tab int32_t @tab 32 bits |
| 37 |
@item (unsigned-byte 32) @tab uint32_t @tab 32 bits |
| 38 |
@item (signed-byte 64) @tab int64_t @tab 64 bits |
| 39 |
@item (unsigned-byte 64) @tab uint64_t @tab 64 bits |
| 40 |
@item single-float or short-float @tab float @tab 32-bits IEEE float |
| 41 |
@item double-float @tab double @tab 64-bits IEEE float |
| 42 |
@item long-float @tab long double @tab Between 96 and 128 bits |
| 43 |
@item (complex single-float) @tab float _Complex @tab 64 bits |
| 44 |
@item (complex double-float) @tab double _Complex @tab 128 bits |
| 45 |
@item (complex long-float) @tab long double _Complex @tab Between 192 and 256 bits |
| 46 |
@item t @tab cl_object @tab Size of a pointer. |
| 47 |
@end multitable |
| 48 |
|
| 49 |
Let us remark that some of these specialized types might not exist in your platform. This is detected using conditional reading and features (@xref{Numbers}). |
| 50 |
|
| 51 |
|
| 52 |
@node Arrays - C Reference |
| 53 |
@subsection C Reference |
| 54 |
@subsubsection Types and constants |
| 55 |
C types, limits and enumerations |
| 56 |
|
| 57 |
@subsubheading Constants and types |
| 58 |
@cppdef ECL_ARRAY_RANK_LIMIT |
| 59 |
@cppdef ECL_ARRAY_DIMENSION_LIMIT |
| 60 |
@cppdef ECL_ARRAY_TOTAL_LIMIT |
| 61 |
@cppdef cl_elttype |
| 62 |
@defvr Constant ECL_ARRAY_RANK_LIMIT |
| 63 |
@defvrx Constant ECL_ARRAY_DIMENSION_LIMIT |
| 64 |
@defvrx Constant ECL_ARRAY_TOTAL_LIMIT |
| 65 |
@end defvr |
| 66 |
@deftp enum cl_elttype @{ecl_aet_object, ...@} |
| 67 |
|
| 68 |
@multitable @columnfractions .25 .25 .25 .25 |
| 69 |
@headitem Lisp or C type @tab Enumeration value @tab Lisp or C type @tab Enumeration value |
| 70 |
@item t @tab ecl_aet_object @tab (unsigned-byte 1) @tab ecl_aet_bit |
| 71 |
@item cl_fixnum @tab ecl_aet_fix @tab cl_index @tab ecl_aet_index |
| 72 |
@item (unsigned-byte 8) @tab ecl_aet_b8 @tab (signed-byte 8) @tab ecl_aet_i8 |
| 73 |
@item (unsigned-byte 16) @tab ecl_aet_b16 @tab (signed-byte 16) @tab ecl_aet_i16 |
| 74 |
@item (unsigned-byte 32) @tab ecl_aet_b32 @tab (signed-byte 32) @tab ecl_aet_i32 |
| 75 |
@item (unsigned-byte 64) @tab ecl_aet_b64 @tab (signed-byte 64) @tab ecl_aet_i64 |
| 76 |
@item ecl_character @tab ecl_aet_ch @tab ecl_base_char @tab ecl_aet_bc |
| 77 |
@item single-float @tab ecl_aet_sf @tab double-float @tab ecl_aet_df |
| 78 |
@item long-float @tab ecl_aet_lf @tab (complex long-float) @tab ecl_aet_clf |
| 79 |
@item (complex single-float) @tab ecl_aet_csf @tab (complex double-float) @tab ecl_aet_cdf |
| 80 |
@end multitable |
| 81 |
|
| 82 |
@paragraph Description |
| 83 |
This list contains the constants that limit the rank of an array |
| 84 |
(@coderef{ECL_ARRAY_RANK_LIMIT}), the maximum size of each dimension |
| 85 |
(@coderef{ECL_ARRAY_DIMENSION_LIMIT}) and the maximum number of elements |
| 86 |
in an array (@coderef{ECL_ARRAY_TOTAL_LIMIT}). |
| 87 |
|
| 88 |
ECL uses also internally a set of constants to describe the different |
| 89 |
specialized arrays. The constants form up the enumeration type |
| 90 |
@coderef{cl_elttype}. They are listed in the table above, which |
| 91 |
associates enumeration values with the corresponding Common Lisp element |
| 92 |
type. |
| 93 |
@end deftp |
| 94 |
|
| 95 |
@subsubsection ecl_aet_to_symbol, ecl_symbol_to_aet |
| 96 |
To and from element types |
| 97 |
|
| 98 |
@subsubheading Functions |
| 99 |
@cppdef ecl_aet_to_symbol |
| 100 |
@cppdef ecl_symbol_to_aet |
| 101 |
@deftypefun cl_object ecl_aet_to_symbol (cl_elttype param) |
| 102 |
@deftypefunx cl_elttype ecl_symbol_to_aet (cl_object type) |
| 103 |
|
| 104 |
@paragraph Description |
| 105 |
@coderef{ecl_aet_to_symbol} returns the Lisp type associated to the elements of that specialized array class. @coderef{ecl_symbol_to_aet} does the converse, computing the C constant that is associated to a Lisp element type. |
| 106 |
|
| 107 |
The functions may signal an error if any of the arguments is an invalid C or Lisp type. |
| 108 |
@end deftypefun |
| 109 |
|
| 110 |
@subsubsection Constructors |
| 111 |
Creating array and vectors |
| 112 |
|
| 113 |
@subsubheading Functions |
| 114 |
@cppdef ecl_alloc_simple_vector |
| 115 |
@cppdef si_make_vector |
| 116 |
@cppdef si_make_array |
| 117 |
@cppdef si_adjust_vector |
| 118 |
@deftypefun cl_object ecl_alloc_simple_vector (cl_index length, cl_elttype element_type); |
| 119 |
@deftypefunx cl_object si_make_vector (cl_object element_type, cl_object length, cl_object adjustablep, cl_object fill_pointerp, cl_object displaced_to, cl_object displacement); |
| 120 |
@deftypefunx cl_object si_make_array (cl_object element_type, cl_object dimensions, cl_object adjustablep, cl_object fill_pointerp, cl_object displaced_to, cl_object displacement); |
| 121 |
@deftypefunx cl_object si_adjust_vector (cl_object vector, cl_object length); |
| 122 |
|
| 123 |
@paragraph Description |
| 124 |
The function @coderef{ecl_alloc_simple_vector} is the simplest |
| 125 |
constructor, creating a simple vector (i.e. non-adjustable and without |
| 126 |
a fill pointer), of the given size, preallocating the memory for the |
| 127 |
array data. The first argument, @emph{element_type}, is a C constant |
| 128 |
that represents a valid array element type (See @coderef{cl_elttype}). |
| 129 |
|
| 130 |
The function @coderef{si_make_vector} does the same job but allows creating an array with fill pointer, which is adjustable or displaced to another array. |
| 131 |
@itemize |
| 132 |
@item element_type is now a Common Lisp type descriptor, which is a symbol or list denoting a valid element type |
| 133 |
@item dimension is a non-negative fixnum with the vector size. |
| 134 |
@item fill_pointerp is either ECL_NIL or a non-negative fixnum denoting the fill pointer value. |
| 135 |
@item displaced_to is either ECL_NIL or a valid array to which the new array is displaced. |
| 136 |
@item displacement is either ECL_NIL or a non-negative value with the array displacement. |
| 137 |
@end itemize |
| 138 |
|
| 139 |
Adjustable vector may be adjusted with the function @coderef{si_adjust_vector}. |
| 140 |
|
| 141 |
Finally, the function @coderef{si_make_array} does a similar job to |
| 142 |
@coderef{si_make_vector} but its second argument, @emph{dimension}, |
| 143 |
can be a list of dimensions, to create a multidimensional array. |
| 144 |
|
| 145 |
@paragraph Examples |
| 146 |
Create one-dimensional @code{base-string} with room for 11 characters: |
| 147 |
|
| 148 |
@example |
| 149 |
cl_object s = ecl_alloc_simple_vector(11, ecl_aet_bc); |
| 150 |
@end example |
| 151 |
|
| 152 |
Create a one-dimensional @code{array} with a fill pointer |
| 153 |
|
| 154 |
@example |
| 155 |
cl_object type = ecl_make_symbol("BYTE8","EXT"); |
| 156 |
cl_object a = si_make_vector(type, ecl_make_fixnum(16), ECL_NIL, /* adjustable */ |
| 157 |
ecl_make_fixnum(0) /* fill-pointer */, |
| 158 |
ECL_NIL /* displaced_to */, |
| 159 |
ECL_NIL /* displacement */); |
| 160 |
@end example |
| 161 |
|
| 162 |
An alternative formulation |
| 163 |
|
| 164 |
@example |
| 165 |
cl_object type = ecl_make_symbol("BYTE8","EXT"); |
| 166 |
cl_object a = si_make_array(type, ecl_make_fixnum(16), ECL_NIL, /* adjustable */ |
| 167 |
ecl_make_fixnum(0) /* fill-pointer */, |
| 168 |
ECL_NIL /* displaced_to */, |
| 169 |
ECL_NIL /* displacement */); |
| 170 |
@end example |
| 171 |
|
| 172 |
Create a 2-by-3 two-dimensional @code{array}, specialized for an integer type: |
| 173 |
|
| 174 |
@example |
| 175 |
cl_object dims = cl_list(2, ecl_make_fixnum(2), ecl_make_fixnum(3)); |
| 176 |
cl_object type = ecl_make_symbol("BYTE8","EXT"); |
| 177 |
cl_object a = si_make_array(dims, type, ECL_NIL, /* adjustable */ |
| 178 |
ECL_NIL /* fill-pointer */, |
| 179 |
ECL_NIL /* displaced_to */, |
| 180 |
ECL_NIL /* displacement */); |
| 181 |
@end example |
| 182 |
@end deftypefun |
| 183 |
|
| 184 |
@subsubsection Accessors |
| 185 |
@cppdef ecl_aref |
| 186 |
@cppdef ecl_aset |
| 187 |
@cppdef ecl_aref1 |
| 188 |
@cppdef ecl_aset1 |
| 189 |
Reading and writing array elements |
| 190 |
|
| 191 |
@subsubheading Functions |
| 192 |
@deftypefun cl_object ecl_aref (cl_object array, cl_index row_major_index); |
| 193 |
@deftypefunx cl_object ecl_aset (cl_object array, cl_index row_major_index, cl_object new_value); |
| 194 |
@deftypefunx cl_object ecl_aref1 (cl_object vector, cl_index row_major_index); |
| 195 |
@deftypefunx cl_object ecl_aset1 (cl_object vector, cl_index row_major_index, cl_object new_value); |
| 196 |
|
| 197 |
@paragraph Description |
| 198 |
@coderef{ecl_aref} accesses an array using the supplied @emph{row_major_index}, checking the array bounds and returning a Lisp object for the value at that position. @coderef{ecl_aset} does the converse, storing a Lisp value at the given @emph{row_major_index}. |
| 199 |
|
| 200 |
The first argument to @coderef{ecl_aref} or @coderef{ecl_aset} is an array of any number of dimensions. For an array of rank @code{N} and dimensions @code{d1, d2 ...} up to @code{dN}, the row major index associated to the indices (@code{i1,i2,...iN}) is computed using the formula @code{i1+d1*(i2+d3*(i3+...))}. |
| 201 |
|
| 202 |
@coderef{ecl_aref1} and @coderef{ecl_aset1} are specialized versions that only work with one-dimensional arrays or vectors. They verify that the first argument is indeed a vector. |
| 203 |
|
| 204 |
All functions above check that the index does not exceed the array bounds, that the values match the array element type and that the argument is an array (or a vector). If these conditions are not met, a @code{type-error} is signaled. |
| 205 |
@end deftypefun |
| 206 |
|
| 207 |
@subsubsection Array properties |
| 208 |
@cppdef ecl_array_elttype |
| 209 |
@cppdef ecl_array_rank |
| 210 |
@cppdef ecl_array_dimension |
| 211 |
Array size, fill pointer, etc. |
| 212 |
|
| 213 |
@subsubheading Functions |
| 214 |
@deftypefun cl_elttype ecl_array_elttype (cl_object array); |
| 215 |
@deftypefunx cl_index ecl_array_rank (cl_object array); |
| 216 |
@deftypefunx cl_index ecl_array_dimension (cl_object array, cl_index index); |
| 217 |
|
| 218 |
@paragraph Description |
| 219 |
These functions query various properties of the arrays. Some of them belong to the list of functions in the Common Lisp package, without any need for specialized versions. More precisely |
| 220 |
|
| 221 |
@itemize |
| 222 |
@item @coderef{ecl_array_elttype} returns the array element type, with the encoding found in the enumeration cl_elttype. |
| 223 |
@item @coderef{ecl_array_rank} returns the number of dimensions of the vector or array. |
| 224 |
@item @coderef{ecl_array_dimension} queries the dimension of an array, where index is a non-negative integer between 0 and @code{ecl_array_dimension(array)-1}. |
| 225 |
@end itemize |
| 226 |
@end deftypefun |
| 227 |
|
| 228 |
@subsubsection ANSI Dictionary |
| 229 |
Common Lisp and C equivalence |
| 230 |
|
| 231 |
@multitable @columnfractions .32 .68 |
| 232 |
@headitem Lisp symbol @tab C function |
| 233 |
@item @clhs{f_mk_ar.htm,make-array} @tab cl_object cl_make_array(cl_narg narg, cl_object dimension...) |
| 234 |
@item @clhs{f_adjust.htm,adjust-array} @tab cl_object cl_adjust_array(cl_narg narg, cl_object array, cl_object dimensions, ...) |
| 235 |
@item @clhs{f_adju_1.htm,adjustable-array-p} @tab cl_object cl_adjustable_array_p(cl_object array) |
| 236 |
@item @clhs{f_aref.htm,aref} @tab cl_object cl_aref(cl_narg narg, cl_object array, ...) |
| 237 |
@item @clhs{f_aref.htm,(setf aref)} @tab cl_object si_aset(cl_narg narg, cl_object array, ...) |
| 238 |
@item @clhs{f_ar_dim.htm,array-dimension} @tab cl_object cl_array_dimension(cl_object array, cl_object index) |
| 239 |
@item @clhs{f_ar_d_1.htm,array-dimensions} @tab cl_object cl_array_dimensions(cl_object array) |
| 240 |
@item @clhs{f_ar_ele.htm,array-element-type} @tab cl_object cl_array_element_type(cl_object array) |
| 241 |
@item @clhs{f_ar_has.htm,array-has-fill-pointer-p} @tab cl_object cl_array_has_fill_pointer_p(cl_object array) |
| 242 |
@item @clhs{f_ar_dis.htm,array-displacement} @tab cl_object cl_array_displacement(cl_object array) |
| 243 |
@item @clhs{f_ar_in_.htm,array-in-bounds-p} @tab cl_object cl_array_in_bounds_p(cl_narg narg, cl_object array, ...) |
| 244 |
@item @clhs{f_ar_ran.htm,array-rank} @tab cl_object cl_array_rank(cl_object array) |
| 245 |
@item @clhs{f_ar_row.htm,array-row-major-index} @tab cl_object cl_array_row_major_index(cl_narg narg, cl_object array, ...) |
| 246 |
@item @clhs{f_ar_tot.htm,array-total-size} @tab cl_object cl_array_total_size(cl_object array) |
| 247 |
@item @clhs{f_arrayp.htm,arrayp} @tab cl_object cl_arrayp(cl_object array) |
| 248 |
@item @clhs{f_fill_p.htm,fill-pointer} @tab cl_object cl_fill_pointer(cl_object array) |
| 249 |
@item @clhs{f_fill_p.htm,(setf fill-pointer)} @tab cl_object si_fill_pointer_set(cl_object array, cl_object fill_pointer) |
| 250 |
@item @clhs{f_row_ma.htm,row-major-aref} @tab cl_object cl_row_major_aref(cl_object array, cl_object index) |
| 251 |
@item @clhs{f_row_ma.htm,(setf row-major-aref)} @tab cl_object si_row_major_aset(cl_object array, cl_object index, cl_object value) |
| 252 |
@item @clhs{f_upgr_1.htm,upgraded-array-element-type} @tab cl_object cl_upgraded_array_element_type(cl_narg narg, cl_object typespec, ...) |
| 253 |
@item @clhs{f_smp_ve.htm,simple-vector-p} @tab cl_object cl_simple_vector_p(cl_object object) |
| 254 |
@item @clhs{f_svref.htm,svref} @tab cl_object cl_svref(cl_object simple_vector, cl_object index) |
| 255 |
@item @clhs{f_svref.htm,(setf svref)} @tab cl_object si_svset(cl_object simple_vector, cl_object index, cl_object value) |
| 256 |
@item @clhs{f_vector.htm,vector} @tab cl_object cl_vector(cl_narg narg, ...) |
| 257 |
@item @clhs{f_vec_po.htm,vector-pop} @tab cl_object cl_vector_pop(cl_object vector) |
| 258 |
@item @clhs{f_vec_ps.htm,vector-push} @tab cl_object cl_vector_push(cl_object new_element, cl_object vector) |
| 259 |
@item @clhs{f_vec_ps.htm,vector-push-extend} @tab cl_object cl_vector_push_extend(cl_narg narg, cl_object new_element, cl_object vector, ...) |
| 260 |
@item @clhs{f_vecp.htm,vectorp} @tab cl_object cl_vectorp(cl_object object) |
| 261 |
@item @clhs{f_bt_sb.htm,bit} @tab cl_object cl_bit(cl_narg narg, cl_object bit_array, ...) |
| 262 |
@item @clhs{f_bt_sb.htm,(setf bit)} @tab cl_object si_aset(cl_narg narg, cl_object array, ...) |
| 263 |
@item @clhs{f_bt_sb.htm,sbit} @tab cl_object cl_sbit(cl_narg narg, cl_object bit_array, ...) |
| 264 |
@item @clhs{f_bt_sb.htm,(setf sbit)} @tab cl_object si_aset(cl_narg narg, cl_object array, ...) |
| 265 |
@item @clhs{f_bt_and.htm,bit-and} @tab cl_object cl_bit_and(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 266 |
@item @clhs{f_bt_and.htm,bit-andc1} @tab cl_object cl_bit_andc1(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 267 |
@item @clhs{f_bt_and.htm,bit-andc2} @tab cl_object cl_bit_andc2(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 268 |
@item @clhs{f_bt_and.htm,bit-eqv} @tab cl_object cl_bit_eqv(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 269 |
@item @clhs{f_bt_and.htm,bit-ior} @tab cl_object cl_bit_ior(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 270 |
@item @clhs{f_bt_and.htm,bit-nand} @tab cl_object cl_bit_nand(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 271 |
@item @clhs{f_bt_and.htm,bit-nor} @tab cl_object cl_bit_nor(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 272 |
@item @clhs{f_bt_and.htm,bit-orc1} @tab cl_object cl_bit_orc1(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 273 |
@item @clhs{f_bt_and.htm,bit-orc2} @tab cl_object cl_bit_orc1(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 274 |
@item @clhs{f_bt_and.htm,bit-xor} @tab cl_object cl_bit_xor(cl_narg narg, cl_object array1, cl_object array2, ...) |
| 275 |
@item @clhs{f_bt_and.htm,bit-not} @tab cl_object cl_bit_not(cl_narg narg, cl_object array, ...) |
| 276 |
@item @clhs{f_bt_vec.htm,bit-vector-p} @tab cl_object cl_bit_vector_p(cl_object object) |
| 277 |
@item @clhs{f_smp_bt.htm,simple-bit-vector-p} @tab cl_object cl_simple_bit_vector_p(cl_object object) |
| 278 |
@end multitable |