diff --git a/test/yulPhaser/Population.cpp b/test/yulPhaser/Population.cpp index bb2f4cc05..be90382c7 100644 --- a/test/yulPhaser/Population.cpp +++ b/test/yulPhaser/Population.cpp @@ -261,6 +261,15 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, Po } } +BOOST_FIXTURE_TEST_CASE(plus_operator_should_add_two_populations, PopulationFixture) +{ + BOOST_CHECK_EQUAL( + Population(m_program, {Chromosome("ac"), Chromosome("cx")}) + + Population(m_program, {Chromosome("g"), Chromosome("h"), Chromosome("iI")}), + Population(m_program, {Chromosome("ac"), Chromosome("cx"), Chromosome("g"), Chromosome("h"), Chromosome("iI")}) + ); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() diff --git a/tools/yulPhaser/Population.cpp b/tools/yulPhaser/Population.cpp index 890e13b43..e8a302683 100644 --- a/tools/yulPhaser/Population.cpp +++ b/tools/yulPhaser/Population.cpp @@ -19,6 +19,7 @@ #include +#include #include #include @@ -117,6 +118,13 @@ void Population::run(optional _numRounds, ostream& _outputStream) } } +Population operator+(Population _a, Population _b) +{ + assert(toString(_a.m_program) == toString(_b.m_program)); + + return Population(_a.m_program, move(_a.m_individuals) + move(_b.m_individuals)); +} + ostream& phaser::operator<<(ostream& _stream, Population const& _population) { auto individual = _population.m_individuals.begin(); diff --git a/tools/yulPhaser/Population.h b/tools/yulPhaser/Population.h index 43f8643e1..7171a7045 100644 --- a/tools/yulPhaser/Population.h +++ b/tools/yulPhaser/Population.h @@ -28,6 +28,17 @@ 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 +{ + /** * Information describing the state of an individual member of the population during the course * of the genetic algorithm. @@ -74,6 +85,7 @@ public: ); void run(std::optional _numRounds, std::ostream& _outputStream); + friend Population (::operator+)(Population _a, Population _b); std::vector const& individuals() const { return m_individuals; }