Next: , Previous: , Up: Packages   [Contents][Index]

5.4 Arrays


Next: , Previous: , Up: Arrays   [Contents][Index]

5.4.1 Conventional Arrays

The following syntax and procedures are SCM extensions to feature array in Arrays in SLIB.

Arrays read and write as a # followed by the rank (number of dimensions) followed by the character #\a or #\A and what appear as lists (of lists) of elements. The lists must be nested to the depth of the rank. For each depth, all lists must be the same length.

(make-array '#(ho) 4 3) ⇒
#2A((ho ho ho) (ho ho ho) (ho ho ho) (ho ho ho))

Unshared, conventional (not uniform) 0-based arrays of rank 1 are equivalent to (and can’t be distinguished from) scheme vectors.

(make-array '#(ho) 3) ⇒ #(ho ho ho)
Function: transpose-array array dim0 dim1 …

Returns an array sharing contents with array, but with dimensions arranged in a different order. There must be one dim argument for each dimension of array. dim0, dim1, … should be integers between 0 and the rank of the array to be returned. Each integer in that range must appear at least once in the argument list.

The values of dim0, dim1, … correspond to dimensions in the array to be returned, their positions in the argument list to dimensions of array. Several dims may have the same value, in which case the returned array will have smaller rank than array.

examples:

(transpose-array '#2A((a b) (c d)) 1 0) ⇒ #2A((a c) (b d))
(transpose-array '#2A((a b) (c d)) 0 0) ⇒ #1A(a d)
(transpose-array '#3A(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) ⇒
                #2A((a 4) (b 5) (c 6))
Function: enclose-array array dim0 dim1 …

dim0, dim1 … should be nonnegative integers less than the rank of array. enclose-array returns an array resembling an array of shared arrays. The dimensions of each shared array are the same as the dimth dimensions of the original array, the dimensions of the outer array are the same as those of the original array that did not match a dim.

An enclosed array is not a general Scheme array. Its elements may not be set using array-set!. Two references to the same element of an enclosed array will be equal? but will not in general be eq?. The value returned by array-prototype when given an enclosed array is unspecified.

examples:

(enclose-array '#3A(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1) ⇒
#<enclosed-array (#1A(a d) #1A(b e) #1A(c f)) (#1A(1 4) #1A(2 5) #1A(3 6))>

(enclose-array '#3A(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0) ⇒
#<enclosed-array #2A((a 1) (d 4)) #2A((b 2) (e 5)) #2A((c 3) (f 6))>
Function: array->list array

Returns a list consisting of all the elements, in order, of array. In the case of a rank-0 array, returns the single element.

Function: array-contents array
Function: array-contents array strict

If array may be unrolled into a one dimensional shared array without changing their order (last subscript changing fastest), then array-contents returns that shared array, otherwise it returns #f. All arrays made by make-array may be unrolled, some arrays made by make-shared-array may not be.

If the optional argument strict is provided, a shared array will be returned only if its elements are stored internally contiguous in memory.


Next: , Previous: , Up: Arrays   [Contents][Index]

5.4.2 Uniform Array

Uniform Arrays and vectors are arrays whose elements are all of the same type. Uniform vectors occupy less storage than conventional vectors. Uniform Array procedures also work on vectors, uniform-vectors, bit-vectors, and strings.

SLIB now supports uniform arrys. The primary array creation procedure is make-array, detailed in See Arrays in SLIB.

Unshared uniform character 0-based arrays of rank 1 (dimension) are equivalent to (and can’t be distinguished from) strings.

(make-array "" 3) ⇒ "$q2"

Unshared uniform boolean 0-based arrays of rank 1 (dimension) are equivalent to (and can’t be distinguished from) bit-vectors.

(make-array '#1at() 3) ⇒ #*000
≡
#1At(#f #f #f) ⇒ #*000

prototype arguments in the following procedures are interpreted according to the table:

prototype       type                              display prefix

()              conventional vector                     #A
+64i            complex (double precision)              #A:floC64b
64.0            double (double precision)               #A:floR64b
32.0            float (single precision)                #A:floR32b
32              unsigned integer (32-bit)               #A:fixN32b
-32             signed integer (32-bit)                 #A:fixZ32b
-16             signed integer (16-bit)                 #A:fixZ16b
#\a             char (string)                           #A:char
#t              boolean (bit-vector)                    #A:bool

Other uniform vectors are written in a form similar to that of general arrays, except that one or more modifying characters are put between the #\A character and the contents list. For example, '#1A:fixZ32b(3 5 9) returns a uniform vector of signed integers.

Function: array? obj prototype

Returns #t if the obj is an array of type corresponding to prototype, and #f if not.

Function: array-prototype array

Returns an object that would produce an array of the same type as array, if used as the prototype for list->uniform-array.

Function: list->uniform-array rank prot lst

Returns a uniform array of the type indicated by prototype prot with elements the same as those of lst. Elements must be of the appropriate type, no coercions are done.

In, for example, the case of a rank-2 array, lst must be a list of lists, all of the same length. The length of lst will be the first dimension of the result array, and the length of each element the second dimension.

If rank is zero, lst, which need not be a list, is the single element of the returned array.

Function: uniform-array-read! ura
Function: uniform-array-read! ura port

Attempts to read all elements of ura, in lexicographic order, as binary objects from port. If an end of file is encountered during uniform-array-read! the objects up to that point only are put into ura (starting at the beginning) and the remainder of the array is unchanged.

uniform-array-read! returns the number of objects read. port may be omitted, in which case it defaults to the value returned by (current-input-port).

Function: uniform-array-write ura
Function: uniform-array-write ura port

Writes all elements of ura as binary objects to port. The number of of objects actually written is returned. port may be omitted, in which case it defaults to the value returned by (current-output-port).

Function: logaref array index1 index2 …

If an index is provided for each dimension of array returns the index1, index2, …’th element of array. If one more index is provided, then the last index specifies bit position of the twos-complement representation of the array element indexed by the other indexs returning #t if the bit is 1, and #f if 0. It is an error if this element is not an exact integer.

(logaref '#(#b1101 #b0010) 0)       ⇒ #b1101
(logaref '#(#b1101 #b0010) 0 1)     ⇒ #f
(logaref '#2((#b1101 #b0010)) 0 0)  ⇒ #b1101
Function: logaset! array val index1 index2 …

If an index is provided for each dimension of array sets the index1, index2, …’th element of array to val. If one more index is provided, then the last index specifies bit position of the twos-complement representation of an exact integer array element, setting the bit to 1 if val is #t and to 0 if val is #f. In this case it is an error if the array element is not an exact integer or if val is not boolean.


Next: , Previous: , Up: Arrays   [Contents][Index]

5.4.3 Bit Vectors

Bit vectors can be written and read as a sequence of 0s and 1s prefixed by #*.

#1At(#f #f #f #t #f #t #f) ⇒ #*0001010

Some of these operations will eventually be generalized to other uniform-arrays.

Function: bit-count bool bv

Returns the number of occurrences of bool in bv.

Function: bit-position bool bv k

Returns the minimum index of an occurrence of bool in bv which is at least k. If no bool occurs within the specified range #f is returned.

Function: bit-invert! bv

Modifies bv by replacing each element with its negation.

Function: bit-set*! bv uve bool

If uve is a bit-vector, then bv and uve must be of the same length. If bool is #t, then uve is OR’ed into bv; If bool is #f, the inversion of uve is AND’ed into bv.

If uve is a unsigned integer vector, then all the elements of uve must be between 0 and the LENGTH of bv. The bits of bv corresponding to the indexes in uve are set to bool.

The return value is unspecified.

Function: bit-count* bv uve bool

Returns

(bit-count (bit-set*! (if bool bv (bit-invert! bv)) uve #t) #t).

bv is not modified.


Previous: , Up: Arrays   [Contents][Index]

5.4.4 Array Mapping

(require 'array-for-each)

SCM has some extra functions in feature array-for-each:

Function: array-fill! array fill

Stores fill in every element of array. The value returned is unspecified.

Function: serial-array:copy! destination source

Same as array:copy! but guaranteed to copy in row-major order.

Function: array-equal? array0 array1 …

Returns #t iff all arguments are arrays with the same shape, the same type, and have corresponding elements which are either equal? or array-equal?. This function differs from equal? in that a one dimensional shared array may be array-equal? but not equal? to a vector or uniform vector.

Function: array-map! array0 proc array1 …

If array1, … are arrays, they must have the same number of dimensions as array0 and have a range for each index which includes the range for the corresponding index in array0. If they are scalars, that is, not arrays, vectors, or strings, then they will be converted internally to arrays of the appropriate shape. proc is applied to each tuple of elements of array1 … and the result is stored as the corresponding element in array0. The value returned is unspecified. The order of application is unspecified.

Handling non-array arguments is a SCM extension of array-map! in SLIB

Function: serial-array-map! array0 proc array1 …

Same as array-map!, but guaranteed to apply proc in row-major order.

Function: array-map prototype proc array1 array2 …

array2, … must have the same number of dimensions as array1 and have a range for each index which includes the range for the corresponding index in array1. proc is applied to each tuple of elements of array1, array2, … and the result is stored as the corresponding element in a new array of type prototype. The new array is returned. The order of application is unspecified.

Function: scalar->array scalar array prototype
Function: scalar->array scalar array

Returns a uniform array of the same shape as array, having only one shared element, which is eqv? to scalar. If the optional argument prototype is supplied it will be used as the prototype for the returned array. Otherwise the returned array will be of the same type as array if that is possible, and a conventional array if it is not. This function is used internally by array-map! and friends to handle scalar arguments.


Next: , Previous: , Up: Packages   [Contents][Index]