Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

aref vs svref

107 views
Skip to first unread message

David Bakhash

unread,
Jan 25, 1999, 3:00:00 AM1/25/99
to
Hi,

Let's say I define a class, `class-A', and I have an array of objects of
type class-A. If class-A is a complex class, with lots of slots, etc,
is it better to define the (1-D) array of type T so I can use `svref',
or is it better to tell the compiler the truth, which is that the
:element-type is really class-A (in which case it may sometimes be
(or null class-A)). Sometimes it's not so clear if telling the compiler
things is really a good idea.

dave

Erik Naggum

unread,
Jan 25, 1999, 3:00:00 AM1/25/99
to
* David Bakhash <ca...@mit.edu>

I guess you're really asking whether a vector of class instances will be
a simple vector unless you have fill pointers, displacement, or make it
adjustable. the answer is yes. only a small set of types can be
expected to specialize the array type and all of them are built-in types.
the key is whether the object has to be referenced through a pointer or
not, or can reasonably be allocated a fixed-width slot in a vector. if
it cannot be allocated a fixed-width slot, it has to use a pointer.

you can probably find out by using UPGRADED-ARRAY-ELEMENT-TYPE on the
type and see whether it is T. if it is (and you do none of the other
stuff that requires an array header), you must get a simple vector.

of course, you know that you can find out whether a particular array is a
simple vector with SIMPLE-VECTOR-P.

#:Erik
--
SIGTHTBABW: a signal sent from Unix to its programmers at random
intervals to make them remember that There Has To Be A Better Way.

Lyman S. Taylor

unread,
Jan 25, 1999, 3:00:00 AM1/25/99
to
In article <wk7lubl...@mit.edu>, David Bakhash <ca...@mit.edu> wrote:
>Hi,

>
>Let's say I define a class, `class-A', and I have an array of objects of
>type class-A. If class-A is a complex class, with lots of slots, etc,
>is it better to define the (1-D) array of type T so I can use `svref',
>or is it better to tell the compiler the truth, which is that the
>:element-type is really class-A (in which case it may sometimes be
>(or null class-A)). Sometimes it's not so clear if telling the compiler
>things is really a good idea.

It is probably best to have the model that everything in lisp is a
reference. So an vector of T elements and a vector of CLASS-A
elements essentially both hold references. The type information
can be handy when Lisp wants to inference about what kind of
reference will be produced by a call to SVREF.

It doesn't change the "size" of a each vector "entry".
[ For immutable databstructures, (e.g., fixnums , floats , etc.)
the implementatoin may do this. However, that isn't required. ]

So why can't you use SVREF? ?

(defclass class-a ()
( ( a :accessor slot-a ) (b) (c) (d) ))

(defconstant my-arr (make-array 4 :element-type 'class-a
:initial-element (make-instance 'class-a)))


(defun take-two ( vect )
(declare (type (simple-vector 4) class-a ))
(let ( ( elem1 ( svref vect 1) )
( elem2 ( svref vect 2) ) )
(setf (slot-a elem1) 23 )
(slot-a elem2 )))


(defun run-demo ()
(take-two my-arr ))

RUN-DEMO should return 23.

If you do

(simple-vector-p my-arr) ==> T

You'll find that is it is a simple vector. Hence, SVREF should work on it.




--

Lyman S. Taylor "The Borg -- party poopers of the Galaxy. "
(ly...@cc.gatech.edu) EMH Doctor Star Trek Voyager.

0 new messages