Comments (7)
I generally prefer to avoid serialization in R because it is usually a performance bottleneck.
from datastructures.
Hey, I totally agree, this should be supported. I was planning to add this already, but to be honest I don't know the C API well enough yet to implement this. Unfortunately there are also not too many sources except the R documentation.
Let me quickly explain my problem. So, as far as I know I need to predefine C++
data types, because R dispatches dynamically. For instance, for a heap of int->double
I need to predefine the following :
typedef binomial_heap<int, double> binomial_heap_id;
Now suppose I want a heap of int -> list
. A list might look like this in R:
> list(a=1, b="a")
$a
[1] 1
$b
[1] "a"
If I look at the SEXP
object on the backend, it has the following structure:
pryr::inspect(list(a=1, b="a"))
<VECSXP 0x7f90fc23b950>
<REALSXP 0x7f90fc04baf8>
<STRSXP 0x7f90fc04ba98>
<CHARSXP 0x7f90fa814798>
attributes:
<LISTSXP 0x7f90fbcbf440>
tag:
<SYMSXP 0x7f90fb80a940>
car:
<STRSXP 0x7f90fc23b8e0>
[CHARSXP 0x7f90fa814798]
<CHARSXP 0x7f90fb30d3f8>
cdr:
NULL
I haven't checked, how I could implement this for different data types. If I can just
typedef binomial_heap<int, SEXP> binomial_heap_id;
this would be easy.
As soon as I have more time, I'll have a look into this. Thanks for the feedback!
from datastructures.
SEXP is just a pointer, you just need to make sure R GC doesn't remove the memory by preserving it.
Check R_PreserveObject and R_ReleaseObject.
Update: However..
These functions need to be used with care: because R_ReleaseObject() will perform a recursive search for the object to protect, if many SEXPs are inserted and later removed in the same order, this can cause performance regressions.
from datastructures.
Another possibility is to serialize the R objects to raw vectors via serialize(x, NULL)
from datastructures.
Oh wow, that's great! Thanks!
from datastructures.
One more alternative, you could also store the R objects in a queue using a pairlist if you think R_PreserveObject/R_ReleaseObject
is too heavy.
from datastructures.
Dear @randy3k,
the latest version of the package implements your suggestions. Thanks for the tips and feedback.
I will validate and test this for a couple of more days and then release to CRAN.
hm <- hashmap("integer")
keys <- 1:2
values <- list(
environment(),
data.frame(A=rbeta(3, .5, .5), B=rgamma(3, 1)))
hm[keys] <- values
hm[1L]
[[1]]
<environment: R_GlobalEnv>
Please let me know if I can close this issue.
from datastructures.
Related Issues (17)
- Examples dont work on `R CMD check`
- no examples/use cases HOT 1
- Change names HOT 3
- Benchmark file seems to not work (for me) HOT 2
- Is `insert` slow? HOT 2
- Stack is not type stable when inserting/popping list objects
- Convert entire stack/queue to list? HOT 2
- Adding has_key() and/or default values to hasmaps
- Package ‘datastructures’ was removed from the CRAN repository. HOT 1
- Insert a list of values under a key in heap HOT 2
- Add `as` methods for stacks, queues, etc. HOT 1
- decrease-key HOT 28
- Assignment does not overwrite
- remove an element from a hashmap HOT 2
- utils::head, utils::stack, base::get, base::remove get overwritten without replacement HOT 1
- `dequer` seems like relevant prior work in a very similar direction HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datastructures.