Skip to content
Snippets Groups Projects
Commit 019c430f authored by Amaury Johnen's avatar Amaury Johnen
Browse files

use of a vector for storing actions

parent 9e099d36
No related branches found
No related tags found
No related merge requests found
...@@ -101,6 +101,20 @@ int otherParity(int a) ...@@ -101,6 +101,20 @@ int otherParity(int a)
return a + 1; return a + 1;
} }
namespace std
{
template <>
void swap(Rec2DData::Action& a0, Rec2DData::Action& a1)
{
int pos0 = a0.position;
a0.position = a1.position;
a1.position = pos0;
const Rec2DAction *ra0 = a0.action;
a0.action = a1.action;
a1.action = ra0;
}
}
/** Recombine2D **/ /** Recombine2D **/
/*******************/ /*******************/
Recombine2D::Recombine2D(GFace *gf) : _gf(gf), _strategy(0), _numChange(0) Recombine2D::Recombine2D(GFace *gf) : _gf(gf), _strategy(0), _numChange(0)
...@@ -561,6 +575,11 @@ void Recombine2D::add(MTriangle *t) ...@@ -561,6 +575,11 @@ void Recombine2D::add(MTriangle *t)
/** Rec2DData **/ /** Rec2DData **/
/*****************/ /*****************/
bool Rec2DData::gterAction::operator()(Action *ra1, Action *ra2) const
{
return *((Rec2DAction*)ra2->action) < *((Rec2DAction*)ra1->action);
}
Rec2DData::Rec2DData() Rec2DData::Rec2DData()
{ {
if (Rec2DData::_current != NULL) { if (Rec2DData::_current != NULL) {
...@@ -617,6 +636,16 @@ void Rec2DData::add(const Rec2DElement *rel) ...@@ -617,6 +636,16 @@ void Rec2DData::add(const Rec2DElement *rel)
#endif #endif
} }
void Rec2DData::add(const Rec2DAction *ra)
{
if (ra->_dataAction) {
Msg::Error("[Rec2DData] action already there");
return;
}
_current->_actions.push_back(new Action(ra, _current->_actions.size()));
((Rec2DAction*)ra)->_dataAction = _current->_actions.back();
}
void Rec2DData::rmv(const Rec2DEdge *re) void Rec2DData::rmv(const Rec2DEdge *re)
{ {
if (re->_pos < 0) { if (re->_pos < 0) {
...@@ -680,18 +709,17 @@ void Rec2DData::rmv(const Rec2DElement *rel) ...@@ -680,18 +709,17 @@ void Rec2DData::rmv(const Rec2DElement *rel)
void Rec2DData::rmv(const Rec2DAction *ra) void Rec2DData::rmv(const Rec2DAction *ra)
{ {
std::list<Rec2DAction*>::iterator it = _current->_actions.begin(); if (!ra->_dataAction) {
while (it != _current->_actions.end()) { Msg::Error("[Rec2DData] action not there");
if (*it == ra) {
it = _current->_actions.erase(it);
return;
} }
else else {
++it; int pos = ((Action*)ra->_dataAction)->position;
((Rec2DAction*)ra)->_dataAction = NULL;
delete _current->_actions[pos];
_current->_actions[pos] = _current->_actions.back();
_current->_actions[pos]->position = pos;
_current->_actions.pop_back();
} }
static int a = -1;
if (++a < 1)
Msg::Error("[Rec2DData] removing too much action");
} }
void Rec2DData::printState() const void Rec2DData::printState() const
...@@ -773,9 +801,10 @@ void Rec2DData::checkEntities() ...@@ -773,9 +801,10 @@ void Rec2DData::checkEntities()
return; return;
} }
} }
std::list<Rec2DAction*>::iterator ita = _current->_actions.begin(); for (unsigned int i = 0; i < _current->_actions.size(); ++i) {
for (; ita != _current->_actions.end(); ++ita) { if (_current->_actions[i]->action->getDataAction() != _current->_actions[i] ||
if (!(*ita)->checkCoherence()) { _current->_actions[i]->position != (int)i ||
!_current->_actions[i]->action->checkCoherence() ) {
Msg::Error("Incoherence action"); Msg::Error("Incoherence action");
crash(); crash();
return; return;
...@@ -787,13 +816,15 @@ void Rec2DData::printActions() const ...@@ -787,13 +816,15 @@ void Rec2DData::printActions() const
{ {
std::map<int, std::vector<double> > data; std::map<int, std::vector<double> > data;
std::list<Rec2DAction*>::const_iterator it = _actions.begin(); for (unsigned int i = 0; i < _actions.size(); ++i) {
it = _actions.begin();
for (; it != _actions.end(); ++it) {
std::vector<Rec2DElement*> tri; std::vector<Rec2DElement*> tri;
(*it)->getElements(tri); _actions[i]->action->getElements(tri);
Msg::Info("action %d (%d, %d) -> reward %g", *it, tri[0]->getNum(), tri[1]->getNum(), (*it)->getReward()); Msg::Info("action %d (%d, %d) -> reward %g",
//Msg::Info("action %d -> reward %g", *it, (*it)->getReward()); _actions[i]->action,
tri[0]->getNum(),
tri[1]->getNum(),
((Rec2DAction*)_actions[i]->action)->getReward());
//Msg::Info("action %d -> reward %g", *it, _actions[i]->getReward());
data[tri[0]->getNum()].resize(1); data[tri[0]->getNum()].resize(1);
data[tri[1]->getNum()].resize(1); data[tri[1]->getNum()].resize(1);
//data[tri[0]->getNum()][0] = (*it)->getReward(); //data[tri[0]->getNum()][0] = (*it)->getReward();
...@@ -935,11 +966,11 @@ Rec2DAction* Rec2DData::getBestAction() ...@@ -935,11 +966,11 @@ Rec2DAction* Rec2DData::getBestAction()
{ {
static int a = -1; static int a = -1;
if (++a < 1) Msg::Warning("FIXME implement better compute qual for collapse"); if (++a < 1) Msg::Warning("FIXME implement better compute qual for collapse");
std::list<Rec2DAction*> actions = _current->_actions; if (_current->_actions.size() == 0)
if (actions.size() == 0)
return NULL; return NULL;
return *std::max_element(actions.begin(), Action *ac = *std::max_element(_current->_actions.begin(),
actions.end(), lessRec2DAction()); _current->_actions.end(), gterAction());
return (Rec2DAction*)ac->action;
} }
Rec2DAction* Rec2DData::getRandomAction() Rec2DAction* Rec2DData::getRandomAction()
...@@ -947,20 +978,15 @@ Rec2DAction* Rec2DData::getRandomAction() ...@@ -947,20 +978,15 @@ Rec2DAction* Rec2DData::getRandomAction()
if (_current->_actions.size() == 0) if (_current->_actions.size() == 0)
return NULL; return NULL;
int index = rand() % (int)_current->_actions.size(); int index = rand() % (int)_current->_actions.size();
std::list<Rec2DAction*>::iterator it = _current->_actions.begin(); return (Rec2DAction*)_current->_actions[index]->action;
static int a = -1;
if (++a < 1) Msg::Warning("FIXME what type is size of list ?");
for (int i = 0; i < index; ++i) ++it;
return *it;
} }
void Rec2DData::checkObsolete() void Rec2DData::checkObsolete()
{ {
std::vector<Rec2DAction*> obsoletes; std::vector<Rec2DAction*> obsoletes;
std::list<Rec2DAction*>::iterator it = _current->_actions.begin(); for (unsigned int i = 0; i < _current->_actions.size(); ++i) {
for (; it != _current->_actions.end(); ++it) { if (_current->_actions[i]->action->isObsolete())
if ((*it)->isObsolete()) obsoletes.push_back((Rec2DAction*)_current->_actions[i]->action);
obsoletes.push_back(*it);
} }
for (unsigned int i = 0; i < obsoletes.size(); ++i) { for (unsigned int i = 0; i < obsoletes.size(); ++i) {
...@@ -1524,7 +1550,7 @@ bool gterRec2DAction::operator()(Rec2DAction *ra1, Rec2DAction *ra2) const ...@@ -1524,7 +1550,7 @@ bool gterRec2DAction::operator()(Rec2DAction *ra1, Rec2DAction *ra2) const
} }
Rec2DAction::Rec2DAction() Rec2DAction::Rec2DAction()
: _globQualIfExecuted(.0), _lastUpdate(-2), _numPointing(0) : _globQualIfExecuted(.0), _lastUpdate(-2), _numPointing(0), _dataAction(NULL)
{ {
} }
...@@ -3678,7 +3704,7 @@ bool Rec2DNode::makeChanges() ...@@ -3678,7 +3704,7 @@ bool Rec2DNode::makeChanges()
return false; return false;
_dataChange = Rec2DData::getNewDataChange(); _dataChange = Rec2DData::getNewDataChange();
#ifdef REC2D_DRAW // draw state at origin #ifdef REC2D_DRAW // draw state at origin
double time = Cpu(); //double time = Cpu();
_ra->color(0, 0, 200); _ra->color(0, 0, 200);
CTX::instance()->mesh.changed = ENT_ALL; CTX::instance()->mesh.changed = ENT_ALL;
drawContext::global()->draw(); drawContext::global()->draw();
......
...@@ -46,7 +46,6 @@ struct gterRec2DNode { ...@@ -46,7 +46,6 @@ struct gterRec2DNode {
struct moreRec2DNode { struct moreRec2DNode {
bool operator()(Rec2DNode*, Rec2DNode*) const; bool operator()(Rec2DNode*, Rec2DNode*) const;
}; };
// //
class Recombine2D { class Recombine2D {
private : private :
...@@ -89,6 +88,20 @@ class Recombine2D { ...@@ -89,6 +88,20 @@ class Recombine2D {
}; };
class Rec2DData { class Rec2DData {
private :
class Action {
public :
const Rec2DAction *action;
int position;
Action(const Rec2DAction *ra, unsigned int pos)
: action((Rec2DAction*)ra), position((int)pos) {}
};
template<class T>
friend void std::swap(T&, T&);
struct gterAction {
bool operator()(Action*, Action*) const;
};
private : private :
int _numEdge, _numVert; int _numEdge, _numVert;
long double _valEdge, _valVert; long double _valEdge, _valVert;
...@@ -99,7 +112,7 @@ class Rec2DData { ...@@ -99,7 +112,7 @@ class Rec2DData {
std::vector<Rec2DVertex*> _vertices; std::vector<Rec2DVertex*> _vertices;
std::vector<Rec2DElement*> _elements; std::vector<Rec2DElement*> _elements;
std::list<Rec2DAction*> _actions; std::vector<Action*> _actions;
std::vector<Rec2DAction*> _hiddenActions; std::vector<Rec2DAction*> _hiddenActions;
std::vector<Rec2DNode*> _endNodes; std::vector<Rec2DNode*> _endNodes;
std::vector<Rec2DDataChange*> _changes; std::vector<Rec2DDataChange*> _changes;
...@@ -116,7 +129,7 @@ class Rec2DData { ...@@ -116,7 +129,7 @@ class Rec2DData {
void printState() const; void printState() const;
void printActions() const; void printActions() const;
static void printAction() {_current->printActions();} static void printAction() {_current->printActions();}
void sortActions() {_actions.sort(lessRec2DAction());} //void sortActions() {sort(_actions.begin(), _actions.end(), gterAction());}
void drawTriangles(double shiftx, double shifty) const; void drawTriangles(double shiftx, double shifty) const;
void drawElements(double shiftx, double shifty) const; void drawElements(double shiftx, double shifty) const;
void drawChanges(double shiftx, double shifty) const; void drawChanges(double shiftx, double shifty) const;
...@@ -159,7 +172,7 @@ class Rec2DData { ...@@ -159,7 +172,7 @@ class Rec2DData {
static inline double getValVert() {return (double)_current->_valVert;} static inline double getValVert() {return (double)_current->_valVert;}
static Rec2DAction* getBestAction(); static Rec2DAction* getBestAction();
static Rec2DAction* getRandomAction(); static Rec2DAction* getRandomAction();
static inline bool hasAction() {return !_current->_actions.empty();} static inline bool hasAction() {return _current->_actions.size();}
static void checkObsolete(); static void checkObsolete();
typedef std::vector<Rec2DEdge*>::iterator iter_re; typedef std::vector<Rec2DEdge*>::iterator iter_re;
...@@ -175,9 +188,7 @@ class Rec2DData { ...@@ -175,9 +188,7 @@ class Rec2DData {
static void add(const Rec2DEdge*); static void add(const Rec2DEdge*);
static void add(const Rec2DVertex*); static void add(const Rec2DVertex*);
static void add(const Rec2DElement*); static void add(const Rec2DElement*);
static inline void add(const Rec2DAction *ra) { static void add(const Rec2DAction*);
_current->_actions.push_back((Rec2DAction*)ra);
}
static inline void addHidden(const Rec2DAction *ra) { static inline void addHidden(const Rec2DAction *ra) {
_current->_hiddenActions.push_back((Rec2DAction*)ra); _current->_hiddenActions.push_back((Rec2DAction*)ra);
} }
...@@ -289,6 +300,10 @@ class Rec2DAction { ...@@ -289,6 +300,10 @@ class Rec2DAction {
double _globQualIfExecuted; double _globQualIfExecuted;
int _lastUpdate, _numPointing; int _lastUpdate, _numPointing;
void *_dataAction; // Rec2DData::Action*
friend void Rec2DData::add(const Rec2DAction*);
friend void Rec2DData::rmv(const Rec2DAction*);
public : public :
Rec2DAction(); Rec2DAction();
virtual ~Rec2DAction() {Msg::Error("deleting %d", this);} virtual ~Rec2DAction() {Msg::Error("deleting %d", this);}
...@@ -298,6 +313,7 @@ class Rec2DAction { ...@@ -298,6 +313,7 @@ class Rec2DAction {
bool operator<(Rec2DAction&); bool operator<(Rec2DAction&);
double getReward(); double getReward();
inline void *getDataAction() const {return _dataAction;}
virtual void color(int, int, int) const = 0; virtual void color(int, int, int) const = 0;
virtual void apply(std::vector<Rec2DVertex*> &newPar) = 0; virtual void apply(std::vector<Rec2DVertex*> &newPar) = 0;
virtual void apply(Rec2DDataChange*, std::vector<Rec2DAction*>*&) const = 0; virtual void apply(Rec2DDataChange*, std::vector<Rec2DAction*>*&) const = 0;
...@@ -679,7 +695,6 @@ class Rec2DNode { ...@@ -679,7 +695,6 @@ class Rec2DNode {
inline double getGlobQual() const {return _globalQuality;} inline double getGlobQual() const {return _globalQuality;}
inline int getNumTri() const {return _remainingTri;} inline int getNumTri() const {return _remainingTri;}
}; };
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment