mailto:srfi-63@srfi.schemers.org
.
See instructions
here to subscribe to the list. You can access previous messages via
the
archive of the mailing list.
SRFI-58 gives a read/write invariant syntax for the homogeneous and heterogeneous arrays described here.
Character arrays are can be supported based on strings; but they do not necessarily have access times comparable to other types of arrays.
array->vector
and vector->array
are
not inverses for rank-0 arrays.
In particular, implementations that use flonum representations must follow these rules: A flonum result must be represented with at least as much precision as is used to express any of the inexact arguments to that operation.And calculation with any exact number inputs blows the precision out to "the most precise flonum format available":
If, however, an exact number is operated upon so as to produce an inexact result (as by `sqrt'), and if the result is represented as a flonum, then the most precise flonum format available must be used; but if the result is represented in some other way then the representation must have at least as much precision as the most precise flonum format available.Scheme is not much hampered by lack of low-precision inexact numbers for scalar calculations. The extra computation incurred by gratuitous precision is usually small compared with the overhead of type-dispatch and boxed data manipulation.
Also significant is that the numerical data in most Scheme implementations has manifest type information encoded with it. Varying sizes of number objects means that the vectors hold pointers to some numbers, requiring data fetches from memory locations unlikely to be in the same CPU cache-line.
Arrays composed of elements all having the same size representations can eliminate these indirect accesses and the storage allocation associated with them. Homogeneous arrays of lower precision flonums can reduce by factors of 2 or 4 the storage they occupy; which can also speed execution because of the lower bandwidth to the memory necessary to supply the CPU data cache.
make-array
does not translate well to
Scheme because the array element type and the initial contents are
passed using named arguments.
Prototype arrays specify both the homogeneous array type (or lack of)
and the initial value or lack of it; allowing these purposes to be
satisfied by one argument to make-array
or other
procedures which create arrays.
Some have objected that restricting type specification to arrays is a half-measure. In vectorized programs, specifying the precision of scalar calculations will produce negligible performance improvements. But the performance improvements of homogeneous arrays can accrue to both interpreted and compiled Scheme implementations. By avoiding the morass of general type specification, SRFI-63 can be more easily accommodated by more Scheme implementations.
array-set!
argument order is that of Bawden's
package. SLIB
adopted "array.scm" in 1993. This form of array-set!
has
also been part of the
SCM Scheme
implementation since 1993.
array-set!
argument order is different from the
same-named procedure in
SRFI-25.
Type dispatch on the first argument to array-set!
could
support both SRFIs simultaneously.
make-array
arguments are different from the
same-named procedure in
SRFI-25.
Type dispatch on the first argument to make-array
could
support both SRFIs simultaneously.
(vector->array vect proto bound1 ...) (make-array proto bound1 ...) (make-shared-array array mapper bound1 ...) (array-set! array obj index1 ...) (array-in-bounds? array index1 ...) (array-ref array index1 ...)
The list->array is somewhat dissonant:
(list->array rank proto list)
prototype procedure | exactness | element type |
---|---|---|
vector | any | |
A:floC128b | inexact | 128.bit binary flonum complex |
A:floC64b | inexact | 64.bit binary flonum complex |
A:floC32b | inexact | 32.bit binary flonum complex |
A:floC16b | inexact | 16.bit binary flonum complex |
A:floR128b | inexact | 128.bit binary flonum real |
A:floR64b | inexact | 64.bit binary flonum real |
A:floR32b | inexact | 32.bit binary flonum real |
A:floR16b | inexact | 16.bit binary flonum real |
A:floQ128d | exact | 128.bit decimal flonum rational |
A:floQ64d | exact | 64.bit decimal flonum rational |
A:floQ32d | exact | 32.bit decimal flonum rational |
A:fixZ64b | exact | 64.bit binary fixnum |
A:fixZ32b | exact | 32.bit binary fixnum |
A:fixZ16b | exact | 16.bit binary fixnum |
A:fixZ8b | exact | 8.bit binary fixnum |
A:fixN64b | exact | 64.bit nonnegative binary fixnum |
A:fixN32b | exact | 32.bit nonnegative binary fixnum |
A:fixN16b | exact | 16.bit nonnegative binary fixnum |
A:fixN8b | exact | 8.bit nonnegative binary fixnum |
A:bool | boolean |
Decimal flonums are used for financial calculations so that fractional errors do not accumulate. They should be exact numbers.
real?
returns
#f
) to an A:floR128b, A:floR64b, A:floR32b, or
A:floR16b array is an error.
For inexact flonum complex arrays:
This arrangement has platforms which support uniform array types employing them, with less capable platforms using vectors; but all working compatibly from the same source code.
Make-shared-array
creates any view into an
array whose coordinates can be mapped by exact integer affine
functions. Shared arrays are quite useful. They can reverse indexes,
make subarrays, and facilitate straightforward implementations of
divide-and-conquer algorithms.
In Common-Lisp a displaced array can be created by calls to adjust-array. But displaced arrays are far less flexible than shared arrays, constrained to have the same rank as the original and allowing only index displacements (not reversals, skips, or shuffling).
Empty arrays having no elements can be of any positive rank. Empty
arrays can be returned from make-shared-array
.
Following Common-Lisp's lead, zero-rank arrays have a single element.
Array access time is O(R)+V, where R is the rank of the array and V is the vector access time.
Returns #t
if the obj is an array, and #f
if not.
Note: Arrays are not disjoint from other Scheme types.
Vectors and possibly strings also satisfy array?
.
A disjoint array predicate can be written:
(define (strict-array? obj) (and (array? obj) (not (string? obj)) (not (vector? obj))))
Returns #t
if obj1 and obj2 have the same rank and dimensions and the
corresponding elements of obj1 and obj2 are equal?
.
equal?
recursively compares the contents of pairs, vectors, strings, and
arrays, applying eqv?
on other objects such as numbers
and symbols. A rule of thumb is that objects are generally equal?
if
they print the same. equal?
may fail to terminate if its arguments are
circular data structures.
(equal? 'a 'a) => #t (equal? '(a) '(a)) => #t (equal? '(a (b) c) '(a (b) c)) => #t (equal? "abc" "abc") => #t (equal? 2 2) => #t (equal? (make-vector 5 'a) (make-vector 5 'a)) => #t (equal? (make-array (A:fixN32b 4) 5 3) (make-array (A:fixN32b 4) 5 3)) => #t (equal? (make-array '#(foo) 3 3) (make-array '#(foo) 3 3)) => #t (equal? (lambda (x) x) (lambda (y) y)) => unspecified
Returns the number of dimensions of obj. If obj is not an array, 0 is returned.
Returns a list of dimensions.
(array-dimensions (make-array '#() 3 5)) => (3 5)
Creates and returns an array of type prototype with dimensions k1, ... and filled with elements from prototype. prototype must be an array, vector, or string. The implementation-dependent type of the returned array will be the same as the type of prototype; except if that would be a vector or string with rank not equal to one, in which case some variety of array will be returned.
If the prototype has no elements, then the initial contents of the returned array are unspecified. Otherwise, the returned array will be filled with the element at the origin of prototype.
make-shared-array
can be used to create shared subarrays of other
arrays. The mapper is a function that translates coordinates in
the new array into coordinates in the old array. A mapper must be
linear, and its range must stay within the bounds of the old array, but
it can be otherwise arbitrary. A simple example:
(define fred (make-array '#(#f) 8 8)) (define freds-diagonal (make-shared-array fred (lambda (i) (list i i)) 8)) (array-set! freds-diagonal 'foo 3) (array-ref fred 3 3) => FOO (define freds-center (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2)) (array-ref freds-center 0 0) => FOO
list must be a rank-nested list consisting of all the elements, in row-major order, of the array to be created.
list->array
returns an array of rank rank and type proto consisting of all the
elements, in row-major order, of list. When rank is 0, list is the lone
array element; not necessarily a list.
(list->array 2 '#() '((1 2) (3 4))) => #2A((1 2) (3 4)) (list->array 0 '#() 3) => #0A 3
Returns a rank-nested list consisting of all the elements, in
row-major order, of array. In the case of a rank-0 array, array->list
returns
the single element.
(array->list #2A((ho ho ho) (ho oh oh))) => ((ho ho ho) (ho oh oh)) (array->list #0A ho) => ho
vect must be a vector of length equal to the product of exact nonnegative integers dim1, ....
vector->array
returns an array of type proto consisting of all the elements, in
row-major order, of vect. In the case of a rank-0 array, vect has a
single element.
(vector->array #(1 2 3 4) #() 2 2) => #2A((1 2) (3 4)) (vector->array '#(3) '#()) => #0A 3
Returns a new vector consisting of all the elements of array in
row-major order. In the case of a rank-0 array, array->vector
returns the
single element.
(array->vector #2A ((1 2)( 3 4))) => #(1 2 3 4) (array->vector #0A ho) => ho
Returns #t
if its arguments would be acceptable to
array-ref
.
Returns the (k1, ...) element of array.
Stores obj in the (k1, ...) element of array. The value returned
by array-set!
is unspecified.
These functions return a prototypical uniform-array enclosing the optional argument (which must be of the correct type). If the uniform-array type is supported by the implementation, then it is returned; defaulting to the next larger precision type; resorting finally to vector.
"array.scm
" redefines equal?
to handle
arrays.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.