Convert operator+(Population, Population) into a hidden friend

This commit is contained in:
Jason Cobb 2020-03-25 23:46:26 -04:00
parent 28c0bc5929
commit e16c0c4133
No known key found for this signature in database
GPG Key ID: 2A3F6A6DCA1E8DED
2 changed files with 8 additions and 5 deletions

View File

@ -118,15 +118,21 @@ Population Population::crossover(PairSelection const& _selection, function<Cross
return Population(m_fitnessMetric, crossedIndividuals); return Population(m_fitnessMetric, crossedIndividuals);
} }
namespace solidity::phaser
{
Population operator+(Population _a, Population _b) Population operator+(Population _a, Population _b)
{ {
// This operator is meant to be used only with populations sharing the same metric (and, to make // This operator is meant to be used only with populations sharing the same metric (and, to make
// things simple, "the same" here means the same exact object in memory). // things simple, "the same" here means the same exact object in memory).
assert(_a.m_fitnessMetric == _b.m_fitnessMetric); assert(_a.m_fitnessMetric == _b.m_fitnessMetric);
using ::operator+; // Import the std::vector concat operator from CommonData.h
return Population(_a.m_fitnessMetric, move(_a.m_individuals) + move(_b.m_individuals)); return Population(_a.m_fitnessMetric, move(_a.m_individuals) + move(_b.m_individuals));
} }
}
bool Population::operator==(Population const& _other) const bool Population::operator==(Population const& _other) const
{ {
// We consider populations identical only if they share the same exact instance of the metric. // We consider populations identical only if they share the same exact instance of the metric.

View File

@ -33,10 +33,6 @@ class Population;
} }
// This operator+ must be declared in the global namespace. Otherwise it would shadow global
// operator+ overloads from CommonData.h (e.g. the one for vector) in the namespace it was declared in.
solidity::phaser::Population operator+(solidity::phaser::Population _a, solidity::phaser::Population _b);
namespace solidity::phaser namespace solidity::phaser
{ {
@ -108,7 +104,8 @@ public:
Population select(Selection const& _selection) const; Population select(Selection const& _selection) const;
Population mutate(Selection const& _selection, std::function<Mutation> _mutation) const; Population mutate(Selection const& _selection, std::function<Mutation> _mutation) const;
Population crossover(PairSelection const& _selection, std::function<Crossover> _crossover) const; Population crossover(PairSelection const& _selection, std::function<Crossover> _crossover) const;
friend Population (::operator+)(Population _a, Population _b);
friend Population operator+(Population _a, Population _b);
std::shared_ptr<FitnessMetric> fitnessMetric() { return m_fitnessMetric; } std::shared_ptr<FitnessMetric> fitnessMetric() { return m_fitnessMetric; }
std::vector<Individual> const& individuals() const { return m_individuals; } std::vector<Individual> const& individuals() const { return m_individuals; }