chickadee » srfi-179 » specialized-array-share

specialized-array-share array new-domain new-domain->old-domainprocedure

Constructs a new specialized array that shares the body of the specialized array array. Returns an object that is behaviorally equivalent to a specialized array with the following fields:

domain:        new-domain
storage-class: (array-storage-class array)
body:          (array-body array)
indexer:       (lambda multi-index
                 (call-with-values
                     (lambda ()
                       (apply new-domain->old-domain
                              multi-index))
                   (array-indexer array)))

The resulting array inherits its safety and mutability from array.

Note: It is assumed that the affine structure of the composition of new-domain->old-domain and (array-indexer array) will be used to simplify:

  (lambda multi-index
    (call-with-values
        (lambda ()
          (apply new-domain->old-domain multi-index))
      (array-indexer array)))

It is an error if array is not a specialized array, or if new-domain is not an interval, or if new-domain->old-domain is not a one-to-one affine mapping from new-domain to (array-domain array).

Example: One can apply a "shearing" operation to an array as follows:

(define a
  (array-copy
   (make-array (make-interval '#(5 10))
               list)))
(define b
  (specialized-array-share
   a
   (make-interval '#(5 5))
   (lambda (i j)
     (values i (+ i j)))))
;; Print the "rows" of b
(array-for-each (lambda (row)
                  (pretty-print (array->list row)))
                (array-curry b 1))

;; which prints
;; ((0 0) (0 1) (0 2) (0 3) (0 4))
;; ((1 1) (1 2) (1 3) (1 4) (1 5))
;; ((2 2) (2 3) (2 4) (2 5) (2 6))
;; ((3 3) (3 4) (3 5) (3 6) (3 7))
;; ((4 4) (4 5) (4 6) (4 7) (4 8))

This "shearing" operation cannot be achieved by combining the procedures array-extract, array-translate, array-permute, array-translate, array-curry, array-reverse, and array-sample.