[Rd] ranges and contiguity checking

Duncan Murdoch murdoch.duncan at gmail.com
Wed May 12 20:34:54 CEST 2010


On 12/05/2010 2:18 PM, James Bullard wrote:
> Hi All,
>
> I am interfacing to some C libraries (hdf5) and I have methods defined for
> '[', these methods do hyperslab selection, however, currently I am
> limiting slab selection to contiguous blocks, i.e., things defined like:
> i:(i+k). I don't do any contiguity checking at this point, I just grab the
> max and min of the range and them potentially do an in-memory subselection
> which is what I am definitely trying to avoid. Besides using deparse, I
> can't see anyway to figure out that these things (i:(i+k) and c(i, i+1,
> ..., i+k)) are different.
>
> I have always liked how 1:10 was a valid expression in R (as opposed to
> python where it is not by itself.), however I'd somehow like to know that
> the thing was contiguous range without examining the un-evaluated
> expression or worse, all(diff(i:(i+k)) == 1)

You can implement all(diff(x) == 1) more efficiently in C, but I don't 
see how you could hope to do any better than that without putting very 
un-R-like restrictions on your code.  Do you really want to say that

A[i:(i+k)]

is legal, but

x <- i:(i+k)
A[x]

is not?  That will be very confusing for your users.  The problem is 
that objects don't remember where they came from, only arguments to 
functions do, and functions that make use of this fact mainly do it for 
decorating the output (nice labels in plots) or making error messages 
more intelligible. 

Duncan Murdoch



More information about the R-devel mailing list