mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-07-05 09:10:18 +02:00
Tests & doc for array-from, array-from*, array-set-from!
* test-suite/tests/arrays.test: tests for array-from, array-from*, array-set-from! * doc/ref/api-compound.texi: document array-from, array-from*, array-set-from!.
This commit is contained in:
parent
7d7ada39d0
commit
ecb38d4268
2 changed files with 223 additions and 11 deletions
|
@ -1709,6 +1709,109 @@ base and stride for new array indices in @var{oldarray} data. A few
|
||||||
sample points are enough because @var{mapfunc} is linear.
|
sample points are enough because @var{mapfunc} is linear.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
|
|
||||||
|
@deffn {Scheme Procedure} array-ref array idx @dots{}
|
||||||
|
@deffnx {C Function} scm_array_ref (array, idxlist)
|
||||||
|
Return the element at @code{(idx @dots{})} in @var{array}.
|
||||||
|
@end deffn
|
||||||
|
|
||||||
|
@deffn {Scheme Procedure} array-from array idx @dots{}
|
||||||
|
@deffnx {C Function} scm_array_from (array, idxlist)
|
||||||
|
If the length of @var{idxlist} equals the rank @math{n} of
|
||||||
|
@var{array}, return the element at @code{(idx @dots{})}, just like
|
||||||
|
@code{(array-ref array idx @dots{})}. If, however, the length @math{k}
|
||||||
|
of @var{idxlist} is shorter than @math{n}, then return the shared
|
||||||
|
@math{(n-k)}-rank prefix cell of @var{array} given by @var{idxlist}.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
@example
|
||||||
|
@lisp
|
||||||
|
(array-from #2((a b) (c d)) 0) @result{} #(a b)
|
||||||
|
(array-from #2((a b) (c d)) 1) @result{} #(c d)
|
||||||
|
(array-from #2((a b) (c d)) 1 1) @result{} d
|
||||||
|
(array-from #2((a b) (c d))) @result{} #2((a b) (c d))
|
||||||
|
@end lisp
|
||||||
|
@end example
|
||||||
|
|
||||||
|
@code{(apply array-from array indices)} is equivalent to
|
||||||
|
|
||||||
|
@lisp
|
||||||
|
(let ((len (length indices)))
|
||||||
|
(if (= (array-rank a) len)
|
||||||
|
(apply array-ref a indices)
|
||||||
|
(apply make-shared-array a
|
||||||
|
(lambda t (append indices t))
|
||||||
|
(drop (array-dimensions a) len))))
|
||||||
|
@end lisp
|
||||||
|
|
||||||
|
The name `from' comes from the J language.
|
||||||
|
@end deffn
|
||||||
|
|
||||||
|
@deffn {Scheme Procedure} array-from* array idx @dots{}
|
||||||
|
@deffnx {C Function} scm_array_from_s (array, idxlist)
|
||||||
|
Like @code{(array-from array idx @dots{})}, but return a 0-rank shared
|
||||||
|
array if the length of @var{idxlist} matches the rank of
|
||||||
|
@var{array}. This can be useful when using @var{ARRAY} as destination
|
||||||
|
of copies.
|
||||||
|
|
||||||
|
Compare:
|
||||||
|
|
||||||
|
@example
|
||||||
|
@lisp
|
||||||
|
(array-from #2((a b) (c d)) 1 1) @result{} d
|
||||||
|
(array-from* #2((a b) (c d)) 1) @result{} #0(d)
|
||||||
|
(define a (make-array 'a 2 2))
|
||||||
|
(array-fill! (array-from* a 1 1) 'b)
|
||||||
|
a @result{} #2((a a) (a b)).
|
||||||
|
(array-fill! (array-from a 1 1) 'b) @result{} error: not an array
|
||||||
|
@end lisp
|
||||||
|
@end example
|
||||||
|
|
||||||
|
@code{(apply array-from* array indices)} is equivalent to
|
||||||
|
|
||||||
|
@lisp
|
||||||
|
(apply make-shared-array a
|
||||||
|
(lambda t (append indices t))
|
||||||
|
(drop (array-dimensions a) (length indices)))
|
||||||
|
@end lisp
|
||||||
|
@end deffn
|
||||||
|
|
||||||
|
|
||||||
|
@deffn {Scheme Procedure} array-set-from! array x idx @dots{}
|
||||||
|
@deffnx {C Function} scm_array_set_from_x (array, x, idxlist)
|
||||||
|
If the length of @var{idxlist} equals the rank @math{n} of
|
||||||
|
@var{array}, set the element at @code{(idx @dots{})} of @var{array} to
|
||||||
|
@var{x}, just like @code{(array-set! array x idx @dots{})}. If,
|
||||||
|
however, the length @math{k} of @var{idxlist} is shorter than
|
||||||
|
@math{n}, then copy the @math{(n-k)}-rank array @var{x}
|
||||||
|
into @math{(n-k)}-rank prefix cell of @var{array} given by
|
||||||
|
@var{idxlist}. In this case, the last @math{(n-k)} dimensions of
|
||||||
|
@var{array} and the dimensions of @var{x} must match exactly.
|
||||||
|
|
||||||
|
This function returns the modified @var{array}.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
@example
|
||||||
|
@lisp
|
||||||
|
(array-set-from! (make-array 'a 2 2) b 1 1) @result{} #2((a a) (a b))
|
||||||
|
(array-set-from! (make-array 'a 2 2) #(x y) 1) @result{} #2((a a) (x y))
|
||||||
|
@end lisp
|
||||||
|
@end example
|
||||||
|
|
||||||
|
@code{(apply array-set-from! array x indices)} is equivalent to
|
||||||
|
|
||||||
|
@lisp
|
||||||
|
(let ((len (length indices)))
|
||||||
|
(if (= (array-rank array) len)
|
||||||
|
(apply array-set! array x indices)
|
||||||
|
(array-copy! x (apply array-from array indices)))
|
||||||
|
array)
|
||||||
|
@end lisp
|
||||||
|
@end deffn
|
||||||
|
|
||||||
|
|
||||||
@deffn {Scheme Procedure} shared-array-increments array
|
@deffn {Scheme Procedure} shared-array-increments array
|
||||||
@deffnx {C Function} scm_shared_array_increments (array)
|
@deffnx {C Function} scm_shared_array_increments (array)
|
||||||
For each dimension, return the distance between elements in the root vector.
|
For each dimension, return the distance between elements in the root vector.
|
||||||
|
|
|
@ -290,6 +290,115 @@
|
||||||
(and (eqv? 5 (array-ref s2 1))
|
(and (eqv? 5 (array-ref s2 1))
|
||||||
(eqv? 8 (array-ref s2 2))))))
|
(eqv? 8 (array-ref s2 2))))))
|
||||||
|
|
||||||
|
|
||||||
|
;;;
|
||||||
|
;;; array-from*
|
||||||
|
;;;
|
||||||
|
|
||||||
|
(with-test-prefix/c&e "array-from*"
|
||||||
|
|
||||||
|
(pass-if "vector I"
|
||||||
|
(let ((v (vector 1 2 3)))
|
||||||
|
(array-fill! (array-from* v 1) 'a)
|
||||||
|
(array-equal? v #(1 a 3))))
|
||||||
|
|
||||||
|
(pass-if "vector II"
|
||||||
|
(let ((v (vector 1 2 3)))
|
||||||
|
(array-copy! #(a b c) (array-from* v))
|
||||||
|
(array-equal? v #(a b c))))
|
||||||
|
|
||||||
|
(pass-if "array I"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(array-fill! (array-from* a 1 1) 'a)
|
||||||
|
(array-equal? a #2((1 2 3) (4 a 6)))))
|
||||||
|
|
||||||
|
(pass-if "array II"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(array-copy! #(a b c) (array-from* a 1))
|
||||||
|
(array-equal? a #2((1 2 3) (a b c)))))
|
||||||
|
|
||||||
|
(pass-if "array III"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(array-copy! #2((a b c) (x y z)) (array-from* a))
|
||||||
|
(array-equal? a #2((a b c) (x y z)))))
|
||||||
|
|
||||||
|
(pass-if "rank 0 array"
|
||||||
|
(let ((a (make-array 77)))
|
||||||
|
(array-fill! (array-from* a) 'a)
|
||||||
|
(array-equal? a #0(a)))))
|
||||||
|
|
||||||
|
|
||||||
|
;;;
|
||||||
|
;;; array-from
|
||||||
|
;;;
|
||||||
|
|
||||||
|
(with-test-prefix/c&e "array-from"
|
||||||
|
|
||||||
|
(pass-if "vector I"
|
||||||
|
(let ((v (vector 1 2 3)))
|
||||||
|
(equal? 2 (array-from v 1))))
|
||||||
|
|
||||||
|
(pass-if "vector II"
|
||||||
|
(let ((v (vector 1 2 3)))
|
||||||
|
(array-copy! #(a b c) (array-from v))
|
||||||
|
(array-equal? v #(a b c))))
|
||||||
|
|
||||||
|
(pass-if "array I"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(equal? 5 (array-from a 1 1))))
|
||||||
|
|
||||||
|
(pass-if "array II"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(array-copy! #(a b c) (array-from a 1))
|
||||||
|
(array-equal? a #2((1 2 3) (a b c)))))
|
||||||
|
|
||||||
|
(pass-if "array III"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(array-copy! #2((a b c) (x y z)) (array-from a))
|
||||||
|
(array-equal? a #2((a b c) (x y z)))))
|
||||||
|
|
||||||
|
(pass-if "rank 0 array"
|
||||||
|
(let ((a (make-array 77)))
|
||||||
|
(equal? (array-from a) 77))))
|
||||||
|
|
||||||
|
|
||||||
|
;;;
|
||||||
|
;;; array-set-from!
|
||||||
|
;;;
|
||||||
|
|
||||||
|
(with-test-prefix/c&e "array-set-from!"
|
||||||
|
|
||||||
|
(pass-if "vector I"
|
||||||
|
(let ((v (vector 1 2 3)))
|
||||||
|
(and (eq? v (array-set-from! v 'x 1))
|
||||||
|
(array-equal? v #(1 x 3)))))
|
||||||
|
|
||||||
|
(pass-if "vector II"
|
||||||
|
(let ((v (vector 1 2 3)))
|
||||||
|
(and (eq? v (array-set-from! (array-from v) #(a b c)))
|
||||||
|
(array-equal? v #(a b c)))))
|
||||||
|
|
||||||
|
(pass-if "array I"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(and (eq? a (array-set-from! a 'x 1 1))
|
||||||
|
(array-equal? a #2((1 2 3) (4 x 6))))))
|
||||||
|
|
||||||
|
(pass-if "array II"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(and (eq? a (array-set-from! a #(a b c) 1))
|
||||||
|
(array-equal? a #2((1 2 3) (a b c))))))
|
||||||
|
|
||||||
|
(pass-if "array III"
|
||||||
|
(let ((a (list->array 2 '((1 2 3) (4 5 6)))))
|
||||||
|
(and (eq? a (array-set-from! a #2((a b c) (x y z))))
|
||||||
|
(array-equal? a #2((a b c) (x y z))))))
|
||||||
|
|
||||||
|
(pass-if "rank 0 array"
|
||||||
|
(let ((a (make-array 77)))
|
||||||
|
(and (eq? a (array-set-from! a 99))
|
||||||
|
(array-equal? a #0(99))))))
|
||||||
|
|
||||||
|
|
||||||
;;;
|
;;;
|
||||||
;;; array-contents
|
;;; array-contents
|
||||||
;;;
|
;;;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue