diff --git a/R/RcppExports.R b/R/RcppExports.R index 815e6deb..b73c7820 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -81,6 +81,10 @@ categorical_variable_get_size_of <- function(variable, values) { .Call(`_individual_categorical_variable_get_size_of`, variable, values) } +categorical_variable_get_categories <- function(variable) { + .Call(`_individual_categorical_variable_get_categories`, variable) +} + categorical_variable_queue_update_vector <- function(variable, value, index) { invisible(.Call(`_individual_categorical_variable_queue_update_vector`, variable, value, index)) } diff --git a/R/categorical_variable.R b/R/categorical_variable.R index 6e789301..9f1e3f5b 100644 --- a/R/categorical_variable.R +++ b/R/categorical_variable.R @@ -31,6 +31,14 @@ CategoricalVariable <- R6::R6Class( categorical_variable_get_size_of(self$.variable, values) }, + #' @description return a character vector of possible values. + #' Note that the order of the returned vector may not be the same order + #' that was given when the variable was intitialized, due to the underlying + #' unordered storage type. + get_categories = function() { + categorical_variable_get_categories(self$.variable) + }, + #' @description queue an update for this variable #' @param values the new values #' @param index the indices of individuals whose value will be updated diff --git a/man/CategoricalVariable.Rd b/man/CategoricalVariable.Rd index 922d80af..dc327fb8 100644 --- a/man/CategoricalVariable.Rd +++ b/man/CategoricalVariable.Rd @@ -16,6 +16,7 @@ if possible becuase certain operations will be faster. \item \href{#method-new}{\code{CategoricalVariable$new()}} \item \href{#method-get_index_of}{\code{CategoricalVariable$get_index_of()}} \item \href{#method-get_size_of}{\code{CategoricalVariable$get_size_of()}} +\item \href{#method-get_categories}{\code{CategoricalVariable$get_categories()}} \item \href{#method-queue_update}{\code{CategoricalVariable$queue_update()}} \item \href{#method-.update}{\code{CategoricalVariable$.update()}} \item \href{#method-clone}{\code{CategoricalVariable$clone()}} @@ -74,6 +75,19 @@ return the number of individuals with the given \code{values} } \if{html}{\out{}} } +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-get_categories}{}}} +\subsection{Method \code{get_categories()}}{ +return a character vector of possible values. +Note that the order of the returned vector may not be the same order +that was given when the variable was intitialized, due to the underlying +unordered storage type. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{CategoricalVariable$get_categories()}\if{html}{\out{
}} +} + } \if{html}{\out{
}} \if{html}{\out{}} diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index cdb2332d..5dbb0271 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -235,6 +235,17 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// categorical_variable_get_categories +std::vector categorical_variable_get_categories(Rcpp::XPtr variable); +RcppExport SEXP _individual_categorical_variable_get_categories(SEXP variableSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Rcpp::XPtr >::type variable(variableSEXP); + rcpp_result_gen = Rcpp::wrap(categorical_variable_get_categories(variable)); + return rcpp_result_gen; +END_RCPP +} // categorical_variable_queue_update_vector void categorical_variable_queue_update_vector(Rcpp::XPtr variable, const std::string& value, std::vector& index); RcppExport SEXP _individual_categorical_variable_queue_update_vector(SEXP variableSEXP, SEXP valueSEXP, SEXP indexSEXP) { @@ -814,6 +825,7 @@ static const R_CallMethodDef CallEntries[] = { {"_individual_categorical_variable_queue_update", (DL_FUNC) &_individual_categorical_variable_queue_update, 3}, {"_individual_categorical_variable_get_index_of", (DL_FUNC) &_individual_categorical_variable_get_index_of, 2}, {"_individual_categorical_variable_get_size_of", (DL_FUNC) &_individual_categorical_variable_get_size_of, 2}, + {"_individual_categorical_variable_get_categories", (DL_FUNC) &_individual_categorical_variable_get_categories, 1}, {"_individual_categorical_variable_queue_update_vector", (DL_FUNC) &_individual_categorical_variable_queue_update_vector, 3}, {"_individual_categorical_variable_update", (DL_FUNC) &_individual_categorical_variable_update, 1}, {"_individual_dummy", (DL_FUNC) &_individual_dummy, 0}, diff --git a/src/categorical_variable.cpp b/src/categorical_variable.cpp index 272ba139..4dbb0270 100644 --- a/src/categorical_variable.cpp +++ b/src/categorical_variable.cpp @@ -48,6 +48,18 @@ int categorical_variable_get_size_of( return variable->get_size_of(values); } +//[[Rcpp::export]] +std::vector categorical_variable_get_categories( + Rcpp::XPtr variable + ) { + std::vector categories; + categories.reserve(variable->indices.size()); + for (const auto& it : variable->indices) { + categories.emplace_back(it.first); + } + return categories; +} + //[[Rcpp::export]] void categorical_variable_queue_update_vector( Rcpp::XPtr variable, diff --git a/src/prefab.cpp b/src/prefab.cpp index 53ff78d1..6b8d2e6b 100644 --- a/src/prefab.cpp +++ b/src/prefab.cpp @@ -25,7 +25,7 @@ Rcpp::XPtr fixed_probability_multinomial_process_internal( // make pointer to lambda function and return XPtr to R return Rcpp::XPtr( - new process_t([variable,source_state,destination_states,rate,cdf](size_t t){ + new process_t([variable,source_state,destination_states,rate,cdf](size_t t){ // sample leavers individual_index_t leaving_individuals(variable->get_index_of(source_state)); @@ -73,7 +73,7 @@ Rcpp::XPtr multi_probability_multinomial_process_internal( // make pointer to lambda function and return XPtr to R return Rcpp::XPtr( - new process_t([variable,source_state,destination_states,rate_variable,cdf](size_t t){ + new process_t([variable,source_state,destination_states,rate_variable,cdf](size_t t){ // sample leavers with their unique prob individual_index_t leaving_individuals(variable->get_index_of(source_state)); @@ -83,7 +83,7 @@ Rcpp::XPtr multi_probability_multinomial_process_internal( // empty bitsets to put them (their destinations) std::vector destination_individuals; size_t n = destination_states.size(); - for(size_t i=0; i multi_probability_bernoulli_process_internal( // make pointer to lambda function and return XPtr to R return Rcpp::XPtr( - new process_t([variable,rate_variable,from,to](size_t t){ + new process_t([variable,rate_variable,from,to](size_t t){ // sample leavers with their unique prob individual_index_t leaving_individuals(variable->get_index_of(from)); diff --git a/tests/testthat/test-variables.R b/tests/testthat/test-variables.R index 052cd745..55631551 100644 --- a/tests/testthat/test-variables.R +++ b/tests/testthat/test-variables.R @@ -40,6 +40,12 @@ test_that("getting the size of CategoricalVariable category which does not exist expect_error(state$get_size_of('X')) }) +test_that("can retrieve categories of CategoricalVariable", { + values <- c("S","E","I","R") + var <- CategoricalVariable$new(categories = values,initial_values = rep(values,2)) + expect_setequal(categorical_variable_get_categories(var$.variable), values) +}) + test_that("getting variables works", { size <- 10 sequence <- DoubleVariable$new(seq_len(size))