diff --git a/src/Simulation.cpp b/src/Simulation.cpp index ea8ccfd..3f9a20b 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -72,19 +72,13 @@ SimulationData::BasicMetrics Simulation::progressByOneYear( { ++yearMetrics.deaths_; it = individuals_.erase(it); - continue; } - - if (shouldHaveOffspring(individual)) + else { - for (int i{0}; i < params_.offspring_.count_; ++i) - individuals_.emplace_front(individual.offspring( - *generator_, params_.mutations_.added_)); - yearMetrics.births_ += params_.offspring_.count_; + handleOffspring(individual, yearMetrics); + individual.ageByOneYear(); + ++it; } - - individual.ageByOneYear(); - ++it; } return yearMetrics; @@ -231,3 +225,16 @@ int Simulation::getLivesOnYearStart( return basicMetrics[static_cast(year - 1)].getLivingAtEnd(); } + +void Simulation::handleOffspring(const Individual& individual, + BasicMetrics& yearMetrics) +{ + if (!shouldHaveOffspring(individual)) + return; + + for (int i = 0; i < params_.offspring_.count_; ++i) + individuals_.emplace_front( + individual.offspring(*generator_, params_.mutations_.added_)); + + yearMetrics.births_ += params_.offspring_.count_; +} diff --git a/src/Simulation.h b/src/Simulation.h index 168bf38..f6aa039 100644 --- a/src/Simulation.h +++ b/src/Simulation.h @@ -59,6 +59,9 @@ class Simulation int getLivesOnYearStart( int year, const std::vector& basicMetrics) const; + void handleOffspring(const Individual& individual, + BasicMetrics& yearMetrics); + std::list individuals_; config::Params params_;