Skip to content

Commit

Permalink
Adding clone method
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Dec 3, 2023
1 parent 34cccfd commit 7e638ac
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export(add_virus_n)
export(agents_from_edgelist)
export(agents_smallworld)
export(change_state)
export(clone_model)
export(get_agents)
export(get_agents_data_ncols)
export(get_agents_states)
Expand Down
4 changes: 4 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ initial_states_cpp <- function(model, proportions) {
.Call(`_epiworldR_initial_states_cpp`, model, proportions)
}

clone_model_cpp <- function(model) {
.Call(`_epiworldR_clone_model_cpp`, model)
}

tool_cpp <- function(name, susceptibility_reduction, transmission_reduction, recovery_enhancer, death_reduction) {
.Call(`_epiworldR_tool_cpp`, name, susceptibility_reduction, transmission_reduction, recovery_enhancer, death_reduction)
}
Expand Down
15 changes: 15 additions & 0 deletions R/model-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,18 @@ initial_states <- function(model, proportions) {

}

#' @rdname epiworld-methods
#' @export
#' @details `epiworld_model` objects are pointers to an underlying C++ class
#' in `epiworld`. To generate a copy of a model, use `clone_model`, otherwise,
#' the assignment operator will only copy the pointer.
#' @return
#' - `clone_model` returns a copy of the model.
clone_model <- function(model) {
stopifnot_model(model)
structure(
clone_model_cpp(model),
class = class(model)
)
}

11 changes: 11 additions & 0 deletions man/epiworld-methods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ extern "C" SEXP _epiworldR_initial_states_cpp(SEXP model, SEXP proportions) {
return cpp11::as_sexp(initial_states_cpp(cpp11::as_cpp<cpp11::decay_t<SEXP>>(model), cpp11::as_cpp<cpp11::decay_t<cpp11::doubles>>(proportions)));
END_CPP11
}
// model.cpp
SEXP clone_model_cpp(const SEXP & model);
extern "C" SEXP _epiworldR_clone_model_cpp(SEXP model) {
BEGIN_CPP11
return cpp11::as_sexp(clone_model_cpp(cpp11::as_cpp<cpp11::decay_t<const SEXP &>>(model)));
END_CPP11
}
// tool.cpp
SEXP tool_cpp(std::string name, double susceptibility_reduction, double transmission_reduction, double recovery_enhancer, double death_reduction);
extern "C" SEXP _epiworldR_tool_cpp(SEXP name, SEXP susceptibility_reduction, SEXP transmission_reduction, SEXP recovery_enhancer, SEXP death_reduction) {
Expand Down Expand Up @@ -787,6 +794,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_epiworldR_agents_from_edgelist_cpp", (DL_FUNC) &_epiworldR_agents_from_edgelist_cpp, 5},
{"_epiworldR_agents_smallworld_cpp", (DL_FUNC) &_epiworldR_agents_smallworld_cpp, 5},
{"_epiworldR_change_state_cpp", (DL_FUNC) &_epiworldR_change_state_cpp, 4},
{"_epiworldR_clone_model_cpp", (DL_FUNC) &_epiworldR_clone_model_cpp, 1},
{"_epiworldR_get_agent_cpp", (DL_FUNC) &_epiworldR_get_agent_cpp, 2},
{"_epiworldR_get_agents_cpp", (DL_FUNC) &_epiworldR_get_agents_cpp, 1},
{"_epiworldR_get_agents_data_ncols_cpp", (DL_FUNC) &_epiworldR_get_agents_data_ncols_cpp, 1},
Expand Down
13 changes: 13 additions & 0 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,16 @@ SEXP initial_states_cpp(SEXP model, cpp11::doubles proportions) {
return model;

}

// Function for cloning a model
[[cpp11::register]]
SEXP clone_model_cpp(const SEXP & model) {

external_pointer<const Model<>> modelptr(model);

return external_pointer<Model<>>(
new Model<>(*modelptr)
);

}

0 comments on commit 7e638ac

Please sign in to comment.