Skip to content

Commit

Permalink
setting names to const ref in prebuilt models
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Jun 21, 2024
1 parent f26bffb commit 26d9590
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 158 deletions.
148 changes: 69 additions & 79 deletions epiworld.hpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion include/epiworld/agent-events-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ inline void default_add_entity(Event<TSeq> & a, Model<TSeq> *)
if (p->get_n_entities() > e->size()) // Slower search through the agent
{
for (size_t i = 0u; i < e->size(); ++i)
if(e->operator[](i)->get_id() == p->get_id())
if(static_cast<int>(e->operator[](i)) == p->get_id())
throw std::logic_error("An entity cannot be reassigned to an agent.");
}
else // Slower search through the entity
Expand Down
8 changes: 3 additions & 5 deletions include/epiworld/entity-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class Entity {
friend void default_add_entity<TSeq>(Event<TSeq> & a, Model<TSeq> * m);
friend void default_rm_entity<TSeq>(Event<TSeq> & a, Model<TSeq> * m);
private:

Model<TSeq> * model;

int id = -1;
std::vector< size_t > agents; ///< Vector of agents
Expand Down Expand Up @@ -82,7 +80,7 @@ class Entity {

void add_agent(Agent<TSeq> & p, Model<TSeq> * model);
void add_agent(Agent<TSeq> * p, Model<TSeq> * model);
void rm_agent(size_t idx);
void rm_agent(size_t idx, Model<TSeq> * model);
size_t size() const noexcept;
void set_location(std::vector< epiworld_double > loc);
std::vector< epiworld_double > & get_location();
Expand All @@ -93,7 +91,7 @@ class Entity {
typename std::vector< Agent<TSeq> * >::const_iterator begin() const;
typename std::vector< Agent<TSeq> * >::const_iterator end() const;

Agent<TSeq> * operator[](size_t i);
size_t operator[](size_t i);

int get_id() const noexcept;
const std::string & get_name() const noexcept;
Expand All @@ -115,7 +113,7 @@ class Entity {
* The idea is to have a flexible way of distributing agents among entities.
*/
void distribute();
void distribute(Model<TSeq> * model);

std::vector< size_t > & get_agents();

Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/entity-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ inline void Entity<TSeq>::add_agent(
}

template<typename TSeq>
inline void Entity<TSeq>::rm_agent(size_t idx)
inline void Entity<TSeq>::rm_agent(size_t idx, Model<TSeq> * model)
{
if (idx >= n_agents)
throw std::out_of_range(
Expand Down Expand Up @@ -89,15 +89,15 @@ inline typename std::vector< Agent<TSeq> * >::const_iterator Entity<TSeq>::end()
}

template<typename TSeq>
inline Agent<TSeq> * Entity<TSeq>::operator[](size_t i)
size_t Entity<TSeq>::operator[](size_t i)
{
if (n_agents <= i)
throw std::logic_error(
"There are not that many agents in this entity. " +
std::to_string(n_agents) + " <= " + std::to_string(i)
);

return &model->get_agents()[i];
return i;
}

template<typename TSeq>
Expand Down Expand Up @@ -227,7 +227,7 @@ inline bool Entity<TSeq>::operator==(const Entity<TSeq> & other) const
}

template<typename TSeq>
inline void Entity<TSeq>::distribute()
inline void Entity<TSeq>::distribute(Model<TSeq> * model)
{

if (dist_fun)
Expand Down
10 changes: 1 addition & 9 deletions include/epiworld/model-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,6 @@ inline Model<TSeq>::Model(const Model<TSeq> & model) :
for (auto & p : population_backup)
p.model = this;

for (auto & e : entities)
e.model = this;

if (entities_backup.size() != 0u)
for (auto & e : entities_backup)
e.model = this;

// Pointing to the right place. This needs
// to be done afterwards since the state zero is set as a function
// of the population.
Expand Down Expand Up @@ -776,7 +769,7 @@ inline void Model<TSeq>::dist_entities()
for (auto & entity: entities)
{

entity.distribute();
entity.distribute(this);

// Apply the events
events_run();
Expand Down Expand Up @@ -960,7 +953,6 @@ template<typename TSeq>
inline void Model<TSeq>::add_entity(Entity<TSeq> e)
{

e.model = this;
e.id = entities.size();
entities.push_back(e);

Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/diffnet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ModelDiffNet : public epiworld::Model<TSeq>

ModelDiffNet(
ModelDiffNet<TSeq> & model,
std::string innovation_name,
const std::string & innovation_name,
epiworld_double prevalence,
epiworld_double prob_adopt,
bool normalize_exposure = true,
Expand All @@ -31,7 +31,7 @@ class ModelDiffNet : public epiworld::Model<TSeq>
);

ModelDiffNet(
std::string innovation_name,
const std::string & innovation_name,
epiworld_double prevalence,
epiworld_double prob_adopt,
bool normalize_exposure = true,
Expand All @@ -52,7 +52,7 @@ class ModelDiffNet : public epiworld::Model<TSeq>
template<typename TSeq>
inline ModelDiffNet<TSeq>::ModelDiffNet(
ModelDiffNet<TSeq> & model,
std::string innovation_name,
const std::string & innovation_name,
epiworld_double prevalence,
epiworld_double prob_adopt,
bool normalize_exposure,
Expand Down Expand Up @@ -180,7 +180,7 @@ inline ModelDiffNet<TSeq>::ModelDiffNet(

template<typename TSeq>
inline ModelDiffNet<TSeq>::ModelDiffNet(
std::string innovation_name,
const std::string & innovation_name,
epiworld_double prevalence,
epiworld_double prob_adopt,
bool normalize_exposure,
Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/seir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class ModelSEIR : public epiworld::Model<TSeq>

ModelSEIR(
ModelSEIR<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
epiworld_double recovery_rate
);

ModelSEIR(
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
Expand Down Expand Up @@ -84,7 +84,7 @@ class ModelSEIR : public epiworld::Model<TSeq>
template<typename TSeq>
inline ModelSEIR<TSeq>::ModelSEIR(
ModelSEIR<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
Expand Down Expand Up @@ -122,7 +122,7 @@ inline ModelSEIR<TSeq>::ModelSEIR(

template<typename TSeq>
inline ModelSEIR<TSeq>::ModelSEIR(
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/seirconnected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ModelSEIRCONN : public epiworld::Model<TSeq>

ModelSEIRCONN(
ModelSEIRCONN<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand All @@ -30,7 +30,7 @@ class ModelSEIRCONN : public epiworld::Model<TSeq>
);

ModelSEIRCONN(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -136,7 +136,7 @@ inline Model<TSeq> * ModelSEIRCONN<TSeq>::clone_ptr()
template<typename TSeq>
inline ModelSEIRCONN<TSeq>::ModelSEIRCONN(
ModelSEIRCONN<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -348,7 +348,7 @@ inline ModelSEIRCONN<TSeq>::ModelSEIRCONN(

template<typename TSeq>
inline ModelSEIRCONN<TSeq>::ModelSEIRCONN(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/seird.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ModelSEIRD : public epiworld::Model<TSeq>
*/
ModelSEIRD(
ModelSEIRD<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
Expand All @@ -50,7 +50,7 @@ class ModelSEIRD : public epiworld::Model<TSeq>
* @param death_rate Death rate of the disease.
*/
ModelSEIRD(
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
Expand Down Expand Up @@ -145,7 +145,7 @@ class ModelSEIRD : public epiworld::Model<TSeq>
template<typename TSeq>
inline ModelSEIRD<TSeq>::ModelSEIRD(
ModelSEIRD<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
Expand Down Expand Up @@ -187,7 +187,7 @@ inline ModelSEIRD<TSeq>::ModelSEIRD(

template<typename TSeq>
inline ModelSEIRD<TSeq>::ModelSEIRD(
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double avg_incubation_days,
Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/seirdconnected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ModelSEIRDCONN : public epiworld::Model<TSeq>

ModelSEIRDCONN(
ModelSEIRDCONN<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand All @@ -31,7 +31,7 @@ class ModelSEIRDCONN : public epiworld::Model<TSeq>
);

ModelSEIRDCONN(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -142,7 +142,7 @@ inline Model<TSeq> * ModelSEIRDCONN<TSeq>::clone_ptr()
template<typename TSeq>
inline ModelSEIRDCONN<TSeq>::ModelSEIRDCONN(
ModelSEIRDCONN<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -367,7 +367,7 @@ inline ModelSEIRDCONN<TSeq>::ModelSEIRDCONN(

template<typename TSeq>
inline ModelSEIRDCONN<TSeq>::ModelSEIRDCONN(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/seirmixing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ModelSEIRMixing : public epiworld::Model<TSeq>
*/
ModelSEIRMixing(
ModelSEIRMixing<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand All @@ -71,7 +71,7 @@ class ModelSEIRMixing : public epiworld::Model<TSeq>
* @param contact_matrix The contact matrix between entities in the model.
*/
ModelSEIRMixing(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -281,7 +281,7 @@ inline Model<TSeq> * ModelSEIRMixing<TSeq>::clone_ptr()
template<typename TSeq>
inline ModelSEIRMixing<TSeq>::ModelSEIRMixing(
ModelSEIRMixing<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -477,7 +477,7 @@ inline ModelSEIRMixing<TSeq>::ModelSEIRMixing(

template<typename TSeq>
inline ModelSEIRMixing<TSeq>::ModelSEIRMixing(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/sir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class ModelSIR : public epiworld::Model<TSeq>

ModelSIR(
ModelSIR<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double recovery_rate
);

ModelSIR(
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double recovery_rate
Expand All @@ -47,7 +47,7 @@ class ModelSIR : public epiworld::Model<TSeq>
template<typename TSeq>
inline ModelSIR<TSeq>::ModelSIR(
ModelSIR<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double recovery_rate
Expand Down Expand Up @@ -80,7 +80,7 @@ inline ModelSIR<TSeq>::ModelSIR(

template<typename TSeq>
inline ModelSIR<TSeq>::ModelSIR(
std::string vname,
const std::string & vname,
epiworld_double prevalence,
epiworld_double transmission_rate,
epiworld_double recovery_rate
Expand Down
8 changes: 4 additions & 4 deletions include/epiworld/models/sirconnected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ModelSIRCONN : public epiworld::Model<TSeq>

ModelSIRCONN(
ModelSIRCONN<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand All @@ -30,7 +30,7 @@ class ModelSIRCONN : public epiworld::Model<TSeq>
);

ModelSIRCONN(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -143,7 +143,7 @@ inline Model<TSeq> * ModelSIRCONN<TSeq>::clone_ptr()
template<typename TSeq>
inline ModelSIRCONN<TSeq>::ModelSIRCONN(
ModelSIRCONN<TSeq> & model,
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down Expand Up @@ -324,7 +324,7 @@ inline ModelSIRCONN<TSeq>::ModelSIRCONN(

template<typename TSeq>
inline ModelSIRCONN<TSeq>::ModelSIRCONN(
std::string vname,
const std::string & vname,
epiworld_fast_uint n,
epiworld_double prevalence,
epiworld_double contact_rate,
Expand Down
Loading

0 comments on commit 26d9590

Please sign in to comment.