Re: Comparison of SRFIs 122 and 164
Per Bothner 02 Dec 2019 04:28 UTC
On 12/1/19 5:55 AM, John Cowan wrote:
> However, there is nothing preventing adding support for SRFI 122 storage classes
> to SRFI 164. For example one could add the procedure
>
> (make-specialized-array SHAPE STORAGE-CLASS)
>
> to SRFI 164 fairly easily, I'm pretty sure.
>
>
> Indeed. Would you consider a revised SRFI or small extension incorporating storage classes (by reference, no need to copy them) and this procedure?
I wrote a simple pure-Kawa-Scheme implementation of make-specialized-array
and it seems to work. (It won't work in Kawa head because of a name lookup bug
relating to 'int' (type) vs 'getInt' (method in AbstractSequence. Haven't
yet decided on the best way to fix that.)
The key is a 'specialized-vector' class, which implements gvector
and delegates the various methods to a storage-class object.
(define (shape-size shape)
;; FIXME very inefficient
(array-size (array (->shape shape) 0)))
(define-simple-class storage-class ()
((*init* getter setter checker maker length default)
(set! (this):getter getter)
(set! (this):setter setter)
(set! (this):checker checker)
(set! (this):maker maker)
(set! (this):length length)
(set! (this):default default))
(getter ::procedure)
(setter ::procedure)
(checker ::procedure)
(maker ::procedure)
(length ::procedure)
(default))
(define (make-storage-class getter setter checker maker length default)
(storage-class getter setter checker maker length default))
(define-simple-class specialized-vector (gnu.lists.AbstractSequence
gnu.lists.GVector)
(sclass ::storage-class)
(sz ::int)
(vec ::gnu.lists.GVector)
((*init* size::int sclass::storage-class)
(set! (this):sclass sclass)
(set! sz size)
(set! vec (sclass:maker size sclass:default)))
((get i::int) (sclass:getter vec i))
((getRaw i::int) (sclass:getter vec i))
((setRaw i::int val)::void (sclass:setter vec i val))
((size)::int sz))
(define (make-specialized-array shape sclass::storage-class)
(set! shape (->shape shape))
(array-reshape (specialized-vector (shape-size shape) sclass) shape))
(define generic-storage-class
(make-storage-class vector-ref
vector-set!
(lambda (arg) #t)
make-vector
vector-length
#f))
--
--Per Bothner
xxxxxx@bothner.com http://per.bothner.com/