Erase-remove idiom in Island and Bimap to avoid invalid iterators (Nils Mangelsen)

This commit is contained in:
quemy 2013-02-07 22:48:41 +01:00
commit d41cbbe781
3 changed files with 13 additions and 12 deletions

View file

@ -49,9 +49,9 @@ std::map<B,A> paradiseo::smp::Bimap<A,B>::getLeft() const
template<class A, class B>
void paradiseo::smp::Bimap<A,B>::removeFromRight(const A& a)
{
for(auto& it : leftAssociation)
if(it->second == a)
leftAssociation.erase(it);
leftAssociation.erase(remove(leftAssociation.begin(), leftAssociation.end(),
[&](std::pair<B,A>& i) -> bool { return i->second == a; }),
leftAssociation.end());
rightAssociation.erase(a);
}
@ -59,9 +59,9 @@ void paradiseo::smp::Bimap<A,B>::removeFromRight(const A& a)
template<class A, class B>
void paradiseo::smp::Bimap<A,B>::removeFromLeft(const B& b)
{
for(auto& it : rightAssociation)
if(it->second == b)
rightAssociation.erase(it);
rightAssociation.erase(remove(rightAssociation.begin(), rightAssociation.end(),
[&](std::pair<A,B>& i) -> bool { return i->second == b; }),
rightAssociation.end());
leftAssociation.erase(b);
}

View file

@ -116,14 +116,14 @@ void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::send(eoSelect<EOT>& _select)
eoPop<bEOT> baseMigPop;
for(auto& indi : migPop)
baseMigPop.push_back(std::move(convertToBase(indi)));
//std::cout << "On envoie de l'île : " << migPop << std::endl;
// Delete delivered messages
for(auto it = sentMessages.begin(); it != sentMessages.end(); it++)
if(!it->joinable())
sentMessages.erase(it);
sentMessages.erase(std::remove_if(sentMessages.begin(), sentMessages.end(),
[&](std::thread& i) -> bool
{ return !i.joinable(); }
),
sentMessages.end());
sentMessages.push_back(std::thread(&IslandModel<bEOT>::update, model, std::move(baseMigPop), this));
}
}

View file

@ -35,6 +35,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#include <utility>
#include <atomic>
#include <type_traits>
#include <algorithm>
#include <eoEvalFunc.h>
#include <eoSelect.h>