Skip to content

Commit

Permalink
added resize
Browse files Browse the repository at this point in the history
  • Loading branch information
msupernaw committed Apr 16, 2024
1 parent d929ad8 commit 86f7ef1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ RCPP_MODULE(growth) {
.constructor()
.constructor<size_t>()
.method("at", &VariableVector::at)
.method("size", &VariableVector::size);
.method("size", &VariableVector::size)
.method("resize", &VariableVector::resize);
Rcpp::class_<PopulationInterface>("Population")
.constructor()
.field("ages", &PopulationInterface::ages)
Expand Down
21 changes: 21 additions & 0 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ class VariableVector{
return this->vec_m.size();
}

void resize(size_t size){
size_t n = this->vec_m.size();

if(size > n){
int m = size - n;

for(size_t i = 0; i < m; i++){
Variable v;
this->vec_m.push_back(Rcpp::wrap(v));
}
}else if(n > size){
int m = size;
Rcpp::List l(m);
for(size_t i = 0; i < m; i++){
l[i] = this->vec_m[i];
}
this->vec_m = l;
}

}


};
uint32_t VariableVector::id_g = 0;
Expand Down
12 changes: 12 additions & 0 deletions test/testv.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ v[1]$value
for( i in 1:length(v)){
print(v[i]$id)
}

v$resize(5)

for( i in 1:length(v)){
print(v[i]$id)
}

v$resize(10)

for( i in 1:length(v)){
print(v[i]$id)
}

0 comments on commit 86f7ef1

Please sign in to comment.