From luke@stat.umn.edu  Fri Apr  6 13:06:33 2001
From: luke@stat.umn.edu (Luke Tierney)
Date: Fri, 6 Apr 2001 07:06:33 -0500 (CDT)
Subject: [Rd] Pointers to external objects
In-Reply-To: <Pine.GSO.4.21.0104061555280.8991-100000@sununx.comp.nus.edu.sg>
 from Nicholas Lewin-Koh at "Apr 6, 2001 04:13:57 pm"
Message-ID: <200104061206.f36C6XD75674@muskrat.stat.umn.edu>

Nicholas Lewin-Koh wrote:
> Hi,
> I am starting to work on some data structures (quad trees, bounding box
> trees etc.) I would like to build these structures in C, and essentially
> have a new primative data type accessible that is persistant through an R
> session and can be written and retrived from .RData .
> 
> I think this is possible from my reading of the documentation, but I am
> not exactly sure how to do it. In the R lang manual in section 2 objects
> there is a mention of typeof returning externalptr I think this is what i
> want. Let me try to be a bit more specific because i am getting confused
> too.
> 
> > kdtree<-build.tree(x,y,...) # two vectors of x and y coordinates 
>                               # kdtree is a list of class tree and
>                               # contains a pointer to the actual tree
>                               # stucture
> > objects.in.range(kdtree,xmin,xmax,ymin,ymax) # returns a list of 
>                                                # external pointers to the
>                                                # data with x,y in the
>                                                # given range 
> > q() # quit the R session, but if the user saves the session then kdtree
>       # is saved and can be recovered when the user restarts R
> 
> I know I could do this using lists, but I am trying to keep the memory
> overhead down.
> 

External pointers can be used to manage lifetimes of externally
allocated data within a single R session.  If an external pointer is
saved its pointer address is saved as NULL (or maybe it isn't saved at
all--I forget) but restoring an external pointer from a saved
workspace will produce an external pointer with NULL for its pointer
address.  The reason for this is that there is no automatic way to
save or restore the appropriate memory layout that the pointer refers
to.

Ideally to support this sort of thing we would need a customizable
serialization (or pickling/unpickling so use the Python terms)
mechanism, but that isn't available now.

For now, your best bet is probably to do somehting like

    build.tree(x,y,...) returns list(data=list(x,y,...), internal=<externptr>)

to save in your object enough R data to allow the internal structure
to be rebuilt, and in the C code that uses the external pointer make
sure the pointer address is valid with something like

	if (extenal pointer value is NULL) {
           ptr = make new tree from data
           store in external pointer with R_SetExternalPtrAddr 
        }

Saving your R object in a workspace will then save the data needed for
rebuilding the internal structure in a new session.

You could also use the Protected field in external pointer objects for
saving the data needed for reconstruction.

Hope that helps.

luke

-- 
Luke Tierney
University of Minnesota                      Phone:           612-625-7843
School of Statistics                         Fax:             612-624-8868
313 Ford Hall, 224 Church St. S.E.           email:      luke@stat.umn.edu
Minneapolis, MN 55455 USA                    WWW:  http://www.stat.umn.edu
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] Pointers to external objects
Luke Tierney