Skip to content

Commit

Permalink
added copy constructor and changed FIMSRcppInterfaceBase::fims_interf…
Browse files Browse the repository at this point in the history
…ace_objects to map
  • Loading branch information
msupernaw committed Jan 2, 2025
1 parent f0d4e76 commit a4fc7e3
Show file tree
Hide file tree
Showing 10 changed files with 1,057 additions and 874 deletions.
256 changes: 142 additions & 114 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,47 @@
*
*/
class DataInterfaceBase : public FIMSRcppInterfaceBase {
public:
Rcpp::NumericVector observed_data; /**< The data */
static uint32_t id_g; /**< static id of the DataInterfaceBase object */
uint32_t id; /**< local id of the DataInterfaceBase object */
// live objects in C++ are objects that have been created and live in memory
static std::map<uint32_t, DataInterfaceBase*>
live_objects; /**< map associating the ids of DataInterfaceBase to
public:
Rcpp::NumericVector observed_data; /**< The data */
static uint32_t id_g; /**< static id of the DataInterfaceBase object */
uint32_t id; /**< local id of the DataInterfaceBase object */
// live objects in C++ are objects that have been created and live in memory
static std::map<uint32_t, DataInterfaceBase*>
live_objects; /**< map associating the ids of DataInterfaceBase to
the objects */

/** @brief constructor
*/
DataInterfaceBase() {
this->id = DataInterfaceBase::id_g++;
/* Create instance of map: key is id and value is pointer to
DataInterfaceBase */
DataInterfaceBase::live_objects[this->id] = this;
FIMSRcppInterfaceBase::fims_interface_objects.push_back(this);
}

/** @brief destructor
*/
virtual ~DataInterfaceBase() {}

/** @brief get the ID of the interface base object
*/
virtual uint32_t get_id() { return this->id; }

/**@brief add_to_fims_tmb dummy method
*
*/
virtual bool add_to_fims_tmb() { return true; };
/** @brief constructor
*/
DataInterfaceBase() {
this->id = DataInterfaceBase::id_g++;
/* Create instance of map: key is id and value is pointer to
DataInterfaceBase */
DataInterfaceBase::live_objects[this->id] = this;
FIMSRcppInterfaceBase::fims_interface_objects[this->id] = this;
}

DataInterfaceBase(const DataInterfaceBase& other) :
observed_data(other.observed_data), id(other.id) {
FIMSRcppInterfaceBase::fims_interface_objects[this->id] = this;
}

/** @brief destructor
*/
virtual ~DataInterfaceBase() {
}

/** @brief get the ID of the interface base object
*/
virtual uint32_t get_id() {
return this->id;
}

/**@brief add_to_fims_tmb dummy method
*
*/
virtual bool add_to_fims_tmb() {
return true;
};
};
uint32_t DataInterfaceBase::id_g = 1;
std::map<uint32_t, DataInterfaceBase*> DataInterfaceBase::live_objects;
Expand All @@ -59,63 +69,71 @@ std::map<uint32_t, DataInterfaceBase*> DataInterfaceBase::live_objects;
* acomp <- new(AgeComp)
*/
class AgeCompDataInterface : public DataInterfaceBase {
public:
int amax; /**< first dimension of the data */
int ymax; /**< second dimension of the data */
Rcpp::NumericVector age_comp_data; /**<the age composition data*/

/**
* @brief constructor
*/
AgeCompDataInterface(int ymax = 0, int amax = 0) : DataInterfaceBase() {
this->amax = amax;
this->ymax = ymax;
}

/**
* @brief destructor
*/
virtual ~AgeCompDataInterface() {}

/** @brief get the ID of the interface base object
*/
virtual uint32_t get_id() { return this->id; }
public:
int amax; /**< first dimension of the data */
int ymax; /**< second dimension of the data */
Rcpp::NumericVector age_comp_data; /**<the age composition data*/

/**
* @brief constructor
*/
AgeCompDataInterface(int ymax = 0, int amax = 0) : DataInterfaceBase() {
this->amax = amax;
this->ymax = ymax;
}

AgeCompDataInterface(const AgeCompDataInterface& other) :
DataInterfaceBase(other), amax(other.amax), ymax(other.ymax), age_comp_data(other.age_comp_data) {
}

#ifdef TMB_MODEL

template <typename Type>
bool add_to_fims_tmb_internal() {
std::shared_ptr<fims_data_object::DataObject<Type>> age_comp_data =
std::make_shared<fims_data_object::DataObject<Type>>(this->ymax,
this->amax);
/**
* @brief destructor
*/
virtual ~AgeCompDataInterface() {
}

age_comp_data->id = this->id;
for (int y = 0; y < ymax; y++) {
for (int a = 0; a < amax; a++) {
int i_age_year = y * amax + a;
age_comp_data->at(y, a) = this->age_comp_data[i_age_year];
}
/** @brief get the ID of the interface base object
*/
virtual uint32_t get_id() {
return this->id;
}

std::shared_ptr<fims_info::Information<Type>> info =
fims_info::Information<Type>::GetInstance();
#ifdef TMB_MODEL

info->data_objects[this->id] = age_comp_data;
template <typename Type>
bool add_to_fims_tmb_internal() {
std::shared_ptr<fims_data_object::DataObject < Type>> age_comp_data =
std::make_shared<fims_data_object::DataObject < Type >> (this->ymax,
this->amax);

return true;
}
age_comp_data->id = this->id;
for (int y = 0; y < ymax; y++) {
for (int a = 0; a < amax; a++) {
int i_age_year = y * amax + a;
age_comp_data->at(y, a) = this->age_comp_data[i_age_year];
}
}

/**
* @brief adds parameters to the model
*/
virtual bool add_to_fims_tmb() {
this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
this->add_to_fims_tmb_internal<TMB_FIMS_FIRST_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_SECOND_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_THIRD_ORDER>();
std::shared_ptr<fims_info::Information < Type>> info =
fims_info::Information<Type>::GetInstance();

return true;
}
info->data_objects[this->id] = age_comp_data;

return true;
}

/**
* @brief adds parameters to the model
*/
virtual bool add_to_fims_tmb() {
this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
this->add_to_fims_tmb_internal<TMB_FIMS_FIRST_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_SECOND_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_THIRD_ORDER>();

return true;
}

#endif
};
Expand All @@ -126,55 +144,65 @@ class AgeCompDataInterface : public DataInterfaceBase {
* fleet <- new(Index)
*/
class IndexDataInterface : public DataInterfaceBase {
public:
int ymax; /**< second dimension of the data */
Rcpp::NumericVector index_data; /**<the age composition data*/
public:
int ymax; /**< second dimension of the data */
Rcpp::NumericVector index_data; /**<the age composition data*/

/**
* @brief constructor
*/
IndexDataInterface(int ymax = 0) : DataInterfaceBase() {
this->ymax = ymax;
}

/**
* @brief constructor
*/
IndexDataInterface(int ymax = 0) : DataInterfaceBase() { this->ymax = ymax; }
IndexDataInterface(const IndexDataInterface& other) :
DataInterfaceBase(other), ymax(other.ymax), index_data(other.index_data) {
}

/**
* @brief destructor
*/
virtual ~IndexDataInterface() {}

/**
* @brief destructor
*/
virtual ~IndexDataInterface() {
}

/** @brief get the ID of the interface base object
*/
virtual uint32_t get_id() { return this->id; }
/** @brief get the ID of the interface base object
*/
virtual uint32_t get_id() {
return this->id;
}

#ifdef TMB_MODEL

template <typename Type>
bool add_to_fims_tmb_internal() {
std::shared_ptr<fims_data_object::DataObject<Type>> data =
std::make_shared<fims_data_object::DataObject<Type>>(this->ymax);
template <typename Type>
bool add_to_fims_tmb_internal() {
std::shared_ptr<fims_data_object::DataObject < Type>> data =
std::make_shared<fims_data_object::DataObject < Type >> (this->ymax);

data->id = this->id;
data->id = this->id;

for (int y = 0; y < ymax; y++) {
data->at(y) = this->index_data[y];
}
for (int y = 0; y < ymax; y++) {
data->at(y) = this->index_data[y];
}

std::shared_ptr<fims_info::Information<Type>> info =
fims_info::Information<Type>::GetInstance();
std::shared_ptr<fims_info::Information < Type>> info =
fims_info::Information<Type>::GetInstance();

info->data_objects[this->id] = data;
return true;
}
info->data_objects[this->id] = data;
return true;
}

/**
*@brief function to add to TMB
*/
virtual bool add_to_fims_tmb() {
this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
this->add_to_fims_tmb_internal<TMB_FIMS_FIRST_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_SECOND_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_THIRD_ORDER>();
/**
*@brief function to add to TMB
*/
virtual bool add_to_fims_tmb() {
this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
this->add_to_fims_tmb_internal<TMB_FIMS_FIRST_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_SECOND_ORDER>();
this->add_to_fims_tmb_internal<TMB_FIMS_THIRD_ORDER>();

return true;
}
return true;
}

#endif
};
Expand Down
Loading

0 comments on commit a4fc7e3

Please sign in to comment.