Open bartoszukm opened 9 years ago
for []
. you may overload the [.classname
operator in R.
> x <- structure("test", class="testclass")
> "[.testclass" <- function(x, i)
+ stringi::stri_sub(x, i, length=1)
> x[1]
[1] "t"
> x[2]
[1] "e"
replacement version:
> "[<-.testclass" <- function(x, i, value) {
+ stringi::stri_sub(x, i, length=1) <- value
+ x
+ }
> x[1] <- ":P"
> x
[1] ":Pest"
vector of any R objects == list (in Rcpp, this will be std::vector<RObject>
.
Why do we use SEXP in stack and queue? Maybe we can also use RObject in stack and queue? Is it any difference?
On 20.03.2015 09:38, Maciej Bartoszuk wrote:
Why do we use SEXP in stack and queue? Maybe we can also use RObject in stack and queue? Is it any difference?
RObject is a thin wrapper around an underlying SEXP. When storing data is concerned only , SEXP is better (more lightweight)
Ok, I committed vector class in 1196208a9afd8718ffa269eec8f47db8516a27c4 .
Unfortunately, there are some issues in getting/setting one element, like at() or operator[].
Firstly, I tried as you said
"[.Vector" <- function(vec, i)
{
vector_at(vec,i)
}
"[.Vector" <- function(vec, i, value)
{
vector_at(vec,i) = value #it's not working alone either, because R do not know about C++ references
}
Unfortunately, R is detecting that it is an external pointer and forbid subsetting:
mv = vector_create(4)
mv[1]=5
mv[1]
Error in mv[1] = 5 : object of type 'externalptr' is not subsettable Error in mv[1] : object of type 'externalptr' is not subsettable
After that I tried to make something like that:
"vector_at<-" <- function(vec, i, value) {
print(vec)
print(i)
print(value)
vector_set_at(vec,i,value)
print(vec)
print(i)
print(value)
}
What is interesting, in this function everything is working fine, but... after function ends and returning to main code, which called "vector_at<-"... vector changes to random number, like NULL or 5.
Do you have any ideas?
For now, one can use vector_set_at(vec,i,value) and vector_at(vec,i) for setting and getting item at index i.
Try setting the class attribute by callling the class() function
Marek Gagolewski, PhD
Sent from Type Mail
On Mar 20, 2015, 11:30, at 11:30, Maciej Bartoszuk notifications@github.com wrote:
Ok, I committed vector class in 1196208a9afd8718ffa269eec8f47db8516a27c4 .
Unfortunately, there are some issues in getting/setting one element, like at() or operator[].
Firstly, I tried as you said
"[.Vector" <- function(vec, i) { vector_at(vec,i) } "[.Vector" <- function(vec, i, value) { vector_at(vec,i) = value #it's not working alone either, because R do not know about C++ references }
Unfortunately, R is detecting that it is an external pointer and forbid subsetting:
mv = vector_create(4) mv[1]=5 mv[1]
Error in mv[1] = 5 : object of type 'externalptr' is not subsettable Error in mv[1] : object of type 'externalptr' is not subsettable
After that I tried to make something like that:
"vector_at<-" <- function(vec, i, value) { print(vec) print(i) print(value) vector_set_at(vec,i,value) print(vec) print(i) print(value) }
What is interesting, in this function everything is working fine, but... after function end and returning to main code, which called "vector_at<-"... vector changes to random number, like NULL or 5.
Do you have any ideas?
For now, one can use vector_set_at(vec,i,value) and vector_at(vec,i) for setting and getting item at index i.
Reply to this email directly or view it on GitHub: https://github.com/Rexamine/DataStructures/issues/2#issuecomment-83982521
We should implement a vector class, using std:vector from C++, which would allow adding new items to a vector with amortized constant complexity (it would solve a "dynamically extending vector" problem).
The main question is: should it be a vector of any R objects (it would be like an R list), or maybe one type of objects (more like an R vector)?
And is there any way to implement operator [], like vector[5]? Or the only way is to make a function get_element(vector, 5) and set_element(vector, 5)?