From 86f7ef1a9bb77b288f15dcb408651e2916da7c24 Mon Sep 17 00:00:00 2001 From: Matthew-Supernaw-NOAA Date: Mon, 15 Apr 2024 21:14:57 -0400 Subject: [PATCH] added resize --- .../include/interface/rcpp/rcpp_interface.hpp | 3 ++- .../rcpp/rcpp_objects/rcpp_interface_base.hpp | 21 +++++++++++++++++++ test/testv.R | 12 +++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/inst/include/interface/rcpp/rcpp_interface.hpp b/inst/include/interface/rcpp/rcpp_interface.hpp index 3b52e89..61bec8b 100644 --- a/inst/include/interface/rcpp/rcpp_interface.hpp +++ b/inst/include/interface/rcpp/rcpp_interface.hpp @@ -76,7 +76,8 @@ RCPP_MODULE(growth) { .constructor() .constructor() .method("at", &VariableVector::at) - .method("size", &VariableVector::size); + .method("size", &VariableVector::size) + .method("resize", &VariableVector::resize); Rcpp::class_("Population") .constructor() .field("ages", &PopulationInterface::ages) diff --git a/inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp b/inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp index e51d281..0013ea4 100644 --- a/inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp +++ b/inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp @@ -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; diff --git a/test/testv.R b/test/testv.R index 3dbba2d..d7b6169 100644 --- a/test/testv.R +++ b/test/testv.R @@ -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) +}