7#ifndef __GRAPH_H_IMPL__
8#define __GRAPH_H_IMPL__
16template<
typename T,
typename UIdx>
24template<
typename T,
typename UIdx>
30template<
typename T,
typename UIdx>
39template<
typename T,
typename UIdx>
49template<
typename T,
typename UIdx>
51 if(fileNamePng ==
"")
return false;
52 std::string fileTxt(
"output.dot");
54 std::cerr <<
"Graph<T, UIdx>::savePng : can't create file '" << fileTxt <<
"'" << std::endl;
57 std::string commande(
"dot -Tpng -o " + fileNamePng +
" " + fileTxt +
" && rm " + fileTxt);
58 if(system(commande.c_str()) != 0){
59 std::cerr <<
"Graph<T, UIdx>::savePng : can't create png file '" << fileNamePng <<
"'" << std::endl;
68template<
typename T,
typename UIdx>
70 if(fileName ==
""){
return false;}
72 fs.open(fileName.c_str());
74 std::cerr <<
"Graph<T, UIdx>::saveDot : can't open file '" << fileName <<
"'" << std::endl;
85template<
typename T,
typename UIdx>
94template<
typename T,
typename UIdx>
97 body +=
"digraph G{\n";
100 body += it->second.getDotDefinition();
104 const std::string & parentNodeName(itNode->second.getDotName());
105 const std::list<UIdx> & listChild = itNode->second.getListChild();
106 for(
typename std::list<UIdx>::const_iterator it(listChild.begin()); it != listChild.end(); ++it){
108 if(childNode == NULL){
continue;}
109 const std::string & childNodeName(childNode->
getDotName());
110 body +=
"\t" + parentNodeName +
" -> " + childNodeName +
"[color=\"red\"];\n";
122template<
typename T,
typename UIdx>
125 while(
getNode(nodeIndex) != NULL){
139template<
typename T,
typename UIdx>
151template<
typename T,
typename UIdx>
168template<
typename T,
typename UIdx>
178template<
typename T,
typename UIdx>
181 it->second.setIsUpdated(
false);
186template<
typename T,
typename UIdx>
190template<
typename T,
typename UIdx>
194template<
typename T,
typename UIdx>
198template<
typename T,
typename UIdx>
209template<
typename T,
typename UIdx>
212 if(indexNode == NULL){
return;}
215 if(listParent.size() != 0lu){
217 for(
typename std::list<UIdx>::iterator it(listParent.begin()); it != listParent.end(); ++it){
219 if(parentNode == NULL){
continue;}
227 std::list<UIdx> & listChild = indexNode->
getListChild();
228 if(listChild.size() != 0lu){
230 for(
typename std::list<UIdx>::iterator it(listChild.begin()); it != listChild.end(); ++it){
232 if(childNode == NULL){
continue;}
239 if(reconnectParentToChildren && listParent.size() != 0lu && listChild.size() != 0lu){
240 for(
typename std::list<UIdx>::iterator it(listParent.begin()); it != listParent.end(); ++it){
249template<
typename T,
typename UIdx>
254 if(itNode->second.isStart()){
257 if(itNode->second.isEnd()){
267template<
typename T,
typename UIdx>
270 if(parentNode == NULL){
return;}
272 if(childNode == NULL){
return;}
281template<
typename T,
typename UIdx>
284 if(parentNode == NULL){
return;}
285 for(
typename std::list<UIdx>::const_iterator it(listChildren.begin()); it != listChildren.end(); ++it){
288 if(childNode == NULL){
continue;}
298template<
typename T,
typename UIdx>
301 if(parentNode == NULL){
return;}
302 for(
typename std::vector<UIdx>::const_iterator it(vecChildren.begin()); it != vecChildren.end(); ++it){
305 if(childNode == NULL){
continue;}
315template<
typename T,
typename UIdx>
317 typename std::map<UIdx, Node<T, UIdx> >::const_iterator it(
p_mapNode.find(index));
319 return &(it->second);
329template<
typename T,
typename UIdx>
331 typename std::map<UIdx, Node<T, UIdx> >::iterator it(
p_mapNode.find(index));
333 return &(it->second);
342template<
typename T,
typename UIdx>
345 if(!itNode->second.getIsUpdated()){
346 return &(itNode->second);
355template<
typename T,
typename UIdx>
358 if(!itNode->second.getIsUpdated()){
359 return &(itNode->second);
369template<
typename T,
typename UIdx>
371 typename std::map<UIdx, Node<T, UIdx> >::iterator it(
p_mapNode.find(index));
378template<
typename T,
typename UIdx>
386template<
typename T,
typename UIdx>
394template<
typename T,
typename UIdx>
403template<
typename T,
typename UIdx>
405 if(listNode.size() == 0lu){
408 std::map<UIdx, bool> mapListChildren;
409 for(
typename std::list<UIdx>::iterator itNode(listNode.begin()); itNode != listNode.end(); ++itNode){
412 const std::list<UIdx> & listChildren = node->
getListChild();
413 for(
typename std::list<UIdx>::const_iterator it(listChildren.begin()); it != listChildren.end(); ++it){
414 mapListChildren[*it] =
true;
418 std::list<UIdx> listNodeOut;
419 for(
typename std::map<UIdx, bool>::iterator it(mapListChildren.begin()); it != mapListChildren.end(); ++it){
420 listNodeOut.push_back(it->first);
422 listNode = listNodeOut;
424 return listNode.size() != 0lu;
431template<
typename T,
typename UIdx>
433 if(listNode.size() == 0lu){
436 std::map<UIdx, bool> mapListParent;
437 for(
typename std::list<UIdx>::iterator itNode(listNode.begin()); itNode != listNode.end(); ++itNode){
441 for(
typename std::list<UIdx>::const_iterator it(listParent.begin()); it != listParent.end(); ++it){
442 mapListParent[*it] =
true;
446 std::list<UIdx> listNodeOut;
447 for(
typename std::map<UIdx, bool>::iterator it(mapListParent.begin()); it != mapListParent.end(); ++it){
448 listNodeOut.push_back(it->first);
450 listNode = listNodeOut;
452 return listNode.size() != 0lu;
459template<
typename T,
typename UIdx>
462 for(
typename std::list<UIdx>::const_iterator itNode(listNode.begin()); itNode != listNode.end() && b; ++itNode){
475template<
typename T,
typename UIdx>
477 if(listNode.size() == 0lu){
481 std::map<UIdx, bool> mapListChildren;
482 for(
typename std::list<UIdx>::iterator itNode(listNode.begin()); itNode != listNode.end(); ++itNode){
487 for(
typename std::list<UIdx>::iterator it(listChildren.begin()); it != listChildren.end(); ++it){
489 if(nodeChild != NULL){
493 mapListChildren[*it] =
true;
501 std::list<UIdx> listNodeOut;
502 for(
typename std::map<UIdx, bool>::iterator it(mapListChildren.begin()); it != mapListChildren.end(); ++it){
503 listNodeOut.push_back(it->first);
505 listNode = listNodeOut;
507 return listNode.size() != 0lu;
514template<
typename T,
typename UIdx>
516 if(listNode.size() == 0lu){
520 std::map<UIdx, bool> mapListParent;
521 for(
typename std::list<UIdx>::iterator itNode(listNode.begin()); itNode != listNode.end(); ++itNode){
526 for(
typename std::list<UIdx>::iterator it(listParent.begin()); it != listParent.end(); ++it){
528 if(nodeParent != NULL){
532 mapListParent[*it] =
true;
540 std::list<UIdx> listNodeOut;
541 for(
typename std::map<UIdx, bool>::iterator it(mapListParent.begin()); it != mapListParent.end(); ++it){
542 listNodeOut.push_back(it->first);
544 listNode = listNodeOut;
546 return listNode.size() != 0lu;
552template<
typename T,
typename UIdx>
560template<
typename T,
typename UIdx>
UIdx createNode(const T &data, const std::string &name)
Create a node and get its index.
void initialisationGraph()
Initialisation function of the class Graph.
bool savePng(const std::string &fileNamePng) const
Save a png file of the graph.
void updateFirstLastNode()
Update the first and last Node of the Graph.
bool isListNodeUdpated(const std::list< UIdx > &listNode) const
Check if the list of given nodes are all updated or not.
bool iterateParent(std::list< UIdx > &listNode) const
Iterate from children to parents.
bool saveDot(const std::string &fileName) const
Save the Graph in a dot file.
void removeNode(UIdx index, bool reconnectParentToChildren=false)
Remove a Node from the Graph.
size_t size() const
Get the number of nodes inside the graph.
bool iterateChildrenCheckUpdate(std::list< UIdx > &listNode)
Iterate from parents to chidren (all node are iterated only once)
Graph()
Default constructeur of Graph.
const Node< T, UIdx > * getNode(UIdx index) const
Get a Node by pointer.
Node< T, UIdx > * createNodePtr(const T &data, const std::string &name)
Create a node and get its pointer.
const std::list< UIdx > & getListFirstNode() const
Get the list of the first nodes (without parent)
Graph & operator=(const Graph< T, UIdx > &other)
Definition of equal operator of Graph.
bool iterateChildren(std::list< UIdx > &listNode) const
Iterate from parents to chidren.
std::map< UIdx, Node< T, UIdx > > p_mapNode
Map of the Node.
bool isNodeExist(UIdx index) const
Say if the node at index does exit.
const std::list< UIdx > & getListLastNode() const
Get the list of the last nodes (without child)
std::list< UIdx > p_listFirstNode
List of the first Node (without parent)
std::list< UIdx > p_listLastNode
List of the last Node (without child)
std::string toDot() const
Convert the graph to dot language.
void copyGraph(const Graph< T, UIdx > &other)
Copy function of Graph.
void clearMapNode()
Clear the map of Node.
const Node< T, UIdx > * getFirstNotUpdatedNode() const
Get the first node which is not udpated (getIsUpdated() == false)
void clearIsUpdated()
Clear the isUpdated attriburte of the Node.
void connectNode(UIdx parent, UIdx child)
Connect a parent to its child.
void clearFirstNode()
Clear the list of first Node.
bool iterateParentCheckUpdate(std::list< UIdx > &listNode)
Iterate from children to parents (all node are iterated only once)
void clearLastNode()
Clear the list of last Node.
virtual ~Graph()
Destructeur of Graph.
const std::list< UIdx > & getListParent() const
Get the list of parents of the Node.
void setIsUpdated(bool isUpdated)
Say if the node is updated.
std::string getDotName() const
Get the dot name of the current Node.
void removeParent(UIdx parent)
Remove connection with parent.
void addChild(UIdx child)
Add a child to the current Node.
const std::list< UIdx > & getListChild() const
Get the list of children of the Node.
void setIndex(UIdx index)
Set the index of the Node.
void addParent(UIdx parent)
Add a parent to the current Node.
bool getIsUpdated() const
Say if the node is updated.
void removeChild(UIdx child)
Remove connection with child.
void listindex_remove(std::list< UIdx > &listIndex, UIdx index)
Remove index from listIndex.