[R] hash or other quick lookup function?

Duncan Murdoch murdoch at stats.uwo.ca
Tue May 27 13:41:07 CEST 2008


Esmail Bonakdarian wrote:
> Hello all,
>
> I have a matrix of bit values.
>
> I compute certain values based on the bits in each row. There may be
> *duplicate* entries in the matrix, ie several rows may be identical.
> These rows change over time, and identical entries may not be next to
> each other.
>
> Computing these values is time consuming, so I would like a way to store a
> value once it's computed. That way I would be able to look up the value
> based on a given bitstring (row in the matrix) and avoid having to re-compute
> the same value.
>
> So, my thought is a hash function based on a bit string - does R have such
> a thing? Or is there an alternative approach that would do the same in R?
> The lookup should be quick, otherwise the gains from avoiding
> recomputing identical values would be lost to some extent.
>
> I wasn't able to find anything via searches, I am hoping someone can
> point me in the right direction.
>   

Environments can be hashed. To use this, you'd convert the bit string 
into a character string, and use that as the name of a variable in an 
environment.

For example,

cache <- new.env(hash=TRUE, parent=emptyenv())
 ...
bits <- '0101'
if (exists(bits, cache)) {
  return(get(bits, cache))
} else {
  value <- long.computation()
  assign(bits, value)
  return(value)
}


Duncan Murdoch



More information about the R-help mailing list