From a49a12762795fc2b3940e01b330bb368924b9188 Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Tue, 2 Jun 2020 15:00:33 +0200 Subject: [PATCH] yul-phaser: Use ptrdiff_t for iterator arithmetic --- test/yulPhaser/Mutations.cpp | 4 ++-- tools/yulPhaser/Mutations.cpp | 20 ++++++++++---------- tools/yulPhaser/PairSelections.cpp | 15 +++++++++------ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/test/yulPhaser/Mutations.cpp b/test/yulPhaser/Mutations.cpp index 27e57be08..86e9992f9 100644 --- a/test/yulPhaser/Mutations.cpp +++ b/test/yulPhaser/Mutations.cpp @@ -118,7 +118,7 @@ BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_before_first_position BOOST_TEST(mutatedChromosome.length() > chromosome.length()); vector suffix( - mutatedChromosome.optimisationSteps().end() - chromosome.length(), + mutatedChromosome.optimisationSteps().end() - static_cast(chromosome.length()), mutatedChromosome.optimisationSteps().end() ); BOOST_TEST(suffix == chromosome.optimisationSteps()); @@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_after_last_position) vector prefix( mutatedChromosome.optimisationSteps().begin(), - mutatedChromosome.optimisationSteps().begin() + chromosome.length() + mutatedChromosome.optimisationSteps().begin() + static_cast(chromosome.length()) ); BOOST_TEST(prefix == chromosome.optimisationSteps()); } diff --git a/tools/yulPhaser/Mutations.cpp b/tools/yulPhaser/Mutations.cpp index 2b988e622..f2876e395 100644 --- a/tools/yulPhaser/Mutations.cpp +++ b/tools/yulPhaser/Mutations.cpp @@ -126,12 +126,12 @@ ChromosomePair fixedPointSwap( return { Chromosome( - vector(begin1, begin1 + _crossoverPoint) + - vector(begin2 + _crossoverPoint, end2) + vector(begin1, begin1 + static_cast(_crossoverPoint)) + + vector(begin2 + static_cast(_crossoverPoint), end2) ), Chromosome( - vector(begin2, begin2 + _crossoverPoint) + - vector(begin1 + _crossoverPoint, end1) + vector(begin2, begin2 + static_cast(_crossoverPoint)) + + vector(begin1 + static_cast(_crossoverPoint), end1) ), }; } @@ -196,8 +196,8 @@ ChromosomePair fixedTwoPointSwap( assert(_crossoverPoint2 <= _chromosome1.length()); assert(_crossoverPoint2 <= _chromosome2.length()); - size_t lowPoint = min(_crossoverPoint1, _crossoverPoint2); - size_t highPoint = max(_crossoverPoint1, _crossoverPoint2); + auto lowPoint = static_cast(min(_crossoverPoint1, _crossoverPoint2)); + auto highPoint = static_cast(max(_crossoverPoint1, _crossoverPoint2)); auto begin1 = _chromosome1.optimisationSteps().begin(); auto begin2 = _chromosome2.optimisationSteps().begin(); @@ -282,17 +282,17 @@ ChromosomePair uniformSwap(Chromosome const& _chromosome1, Chromosome const& _ch if (_chromosome1.length() > minLength) { if (swapTail) - steps2.insert(steps2.end(), begin1 + minLength, end1); + steps2.insert(steps2.end(), begin1 + static_cast(minLength), end1); else - steps1.insert(steps1.end(), begin1 + minLength, end1); + steps1.insert(steps1.end(), begin1 + static_cast(minLength), end1); } if (_chromosome2.length() > minLength) { if (swapTail) - steps1.insert(steps1.end(), begin2 + minLength, end2); + steps1.insert(steps1.end(), begin2 + static_cast(minLength), end2); else - steps2.insert(steps2.end(), begin2 + minLength, end2); + steps2.insert(steps2.end(), begin2 + static_cast(minLength), end2); } return {Chromosome(steps1), Chromosome(steps2)}; diff --git a/tools/yulPhaser/PairSelections.cpp b/tools/yulPhaser/PairSelections.cpp index 7e744ff70..8934c3e17 100644 --- a/tools/yulPhaser/PairSelections.cpp +++ b/tools/yulPhaser/PairSelections.cpp @@ -30,7 +30,7 @@ vector> RandomPairSelection::materialise(size_t _poolSize) if (_poolSize < 2) return {}; - size_t count = static_cast(round(_poolSize * m_selectionSize)); + auto count = static_cast(round(_poolSize * m_selectionSize)); vector> selection; for (size_t i = 0; i < count; ++i) @@ -64,7 +64,10 @@ vector> PairsFromRandomSubset::materialise(size_t _poolSiz } while (selectedIndices.size() % 2 != 0); } else - selectedIndices.erase(selectedIndices.begin() + SimulationRNG::uniformInt(0, selectedIndices.size() - 1)); + selectedIndices.erase( + selectedIndices.begin() + + static_cast(SimulationRNG::uniformInt(0, selectedIndices.size() - 1)) + ); } assert(selectedIndices.size() % 2 == 0); @@ -73,14 +76,14 @@ vector> PairsFromRandomSubset::materialise(size_t _poolSiz { size_t position1 = SimulationRNG::uniformInt(0, selectedIndices.size() - 1); size_t value1 = selectedIndices[position1]; - selectedIndices.erase(selectedIndices.begin() + position1); + selectedIndices.erase(selectedIndices.begin() + static_cast(position1)); size_t position2 = SimulationRNG::uniformInt(0, selectedIndices.size() - 1); size_t value2 = selectedIndices[position2]; - selectedIndices.erase(selectedIndices.begin() + position2); + selectedIndices.erase(selectedIndices.begin() + static_cast(position2)); - selectedPairs.push_back({value1, value2}); + selectedPairs.emplace_back(value1, value2); } - assert(selectedIndices.size() == 0); + assert(selectedIndices.empty()); return selectedPairs; }