Merge pull request #8539 from random-internet-cat/population-op-plus

Convert operator+(Population, Population) into a hidden friend
This commit is contained in:
chriseth 2020-03-30 16:22:57 +02:00 committed by GitHub
commit ff35bf69de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 12 deletions

View File

@ -117,15 +117,21 @@ Population Population::crossover(PairSelection const& _selection, function<Cross
return Population(m_fitnessMetric, crossedIndividuals);
}
namespace solidity::phaser
{
Population operator+(Population _a, Population _b)
{
// 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).
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));
}
}
bool Population::operator==(Population const& _other) const
{
// We consider populations identical only if they share the same exact instance of the metric.

View File

@ -29,17 +29,6 @@
namespace solidity::phaser
{
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
{
class PairSelection;
class Selection;
@ -108,7 +97,8 @@ public:
Population select(Selection const& _selection) const;
Population mutate(Selection const& _selection, std::function<Mutation> _mutation) 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::vector<Individual> const& individuals() const { return m_individuals; }