Commit c88f6827 authored by Pierre Aubert's avatar Pierre Aubert
Browse files

Add method to remove Node and clear graph

parent dd08346c
......@@ -11,7 +11,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
phoenix_create_find_header(PhoenixGraph Graph.h "")
phoenix_create_find(PhoenixGraph phoenix_graph Graph.h "")
add_subdirectory(src)
......
......@@ -6,5 +6,5 @@ add_definitions(-DCMAKE_SOURCE_DIR="${CMAKE_SOURCE_DIR}")
add_subdirectory(TEST_DOT)
add_subdirectory(TEST_DOT_LOOP)
add_subdirectory(TEST_DOT_TOTAL_LOOP)
add_subdirectory(TEST_DOT_REMOVE)
......@@ -2,6 +2,7 @@ project(Phoenix)
add_executable(test_graph_to_dot main.cpp)
target_link_libraries(test_graph_to_dot phoenix_graph)
add_test(NAME TestGraphToDot
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_graph_to_dot)
......
......@@ -2,6 +2,7 @@ project(Phoenix)
add_executable(test_graph_to_dot_loop main.cpp)
target_link_libraries(test_graph_to_dot_loop phoenix_graph)
add_test(NAME TestGraphToDotLoop
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_graph_to_dot_loop)
......
project(Phoenix)
add_executable(test_graph_to_dot_remove main.cpp)
target_link_libraries(test_graph_to_dot_remove phoenix_graph)
add_test(NAME TestGraphToDotRemove
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_graph_to_dot_remove)
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#include "Graph.h"
///Test the graph conversion to dot
void testGraphToDot(){
Graph<bool> graph;
long nodeA = graph.createNode(true, "a");
long nodeB = graph.createNode(true, "b");
long nodeC = graph.createNode(true, "c");
long nodeD = graph.createNode(true, "d");
long nodeE = graph.createNode(true, "e");
long nodeRemove = graph.createNode(true, "to be removed");
graph.connectNode(nodeA, nodeC);
graph.connectNode(nodeB, nodeC);
graph.connectNode(nodeC, nodeE);
graph.connectNode(nodeD, nodeE);
graph.connectNode(nodeB, nodeRemove);
graph.connectNode(nodeRemove, nodeE);
graph.savePng("testGraphBase.png");
graph.removeNode(nodeRemove);
graph.savePng("testGraphRemove.png");
}
int main(int argc, char** argv){
testGraphToDot();
return 0;
}
......@@ -2,6 +2,7 @@ project(Phoenix)
add_executable(test_graph_to_dot_total_loop main.cpp)
target_link_libraries(test_graph_to_dot_total_loop phoenix_graph)
add_test(NAME TestGraphToDotTotalLoop
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_graph_to_dot_total_loop)
......
project(Phoenix)
cmake_minimum_required(VERSION 3.0)
file(GLOB phoenix_graph_SRC "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
add_library(phoenix_graph SHARED ${phoenix_graph_SRC})
install(TARGETS phoenix_graph LIBRARY DESTINATION ${LIBRARY_DIRECTORY} ARCHIVE DESTINATION ${LIBRARY_DIRECTORY})
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
install(FILES ${headers} DESTINATION include/PhoenixGraph)
......@@ -29,6 +29,14 @@ class Graph{
Node<T> * createNodePtr(const T & data, const std::string & name = "");
void clearIsUpdated();
void clearMapNode();
void clearFirstNode();
void clearLastNode();
void clearAll();
void removeNode(long index);
void updateFirstLastNode();
void connectNode(long parent, long child);
void connectNode(long parent, const ListIndex & listChildren);
......
......@@ -150,6 +150,78 @@ void Graph<T>::clearIsUpdated(){
}
}
///Clear the map of Node
template<typename T>
void Graph<T>::clearMapNode(){p_mapNode.clear();}
///Clear the list of first Node
template<typename T>
void Graph<T>::clearFirstNode(){p_listFirstNode.clear();}
///Clear the list of last Node
template<typename T>
void Graph<T>::clearLastNode(){p_listLastNode.clear();}
//Clear all Node
template<typename T>
void Graph<T>::clearAll(){
clearFirstNode();
clearLastNode();
clearMapNode();
}
///Remove a Node from the Graph
/** @param index : index of the Node to be removed
*/
template<typename T>
void Graph<T>::removeNode(long index){
Node<T> * indexNode = getNode(index);
if(indexNode == NULL){return;} //If the Node does not exist, we stop
//Get the parents of the current Node
ListIndex & listParent = indexNode->getListParent();
if(listParent.size() != 0lu){
//Tell them they do not have child anymore
for(ListIndex::iterator it(listParent.begin()); it != listParent.end(); ++it){
Node<T> * parentNode = getNode(*it);
if(parentNode == NULL){continue;}
parentNode->removeChild(index);
}
}else{
//Let's remove the current Node from first Node
listindex_remove(p_listFirstNode, index);
}
//Get the children of the current Node
ListIndex & listChild = indexNode->getListChild();
if(listChild.size() != 0lu){
//Tell them they do not have parent anymore
for(ListIndex::iterator it(listChild.begin()); it != listChild.end(); ++it){
Node<T> * childNode = getNode(*it);
if(childNode == NULL){continue;}
childNode->removeParent(index);
}
}else{
//Let's remove the current Node from last Node
listindex_remove(p_listLastNode, index);
}
//Now, let's remove the current Node
p_mapNode.erase(index);
}
///Update the first and last Node of the Graph
template<typename T>
void Graph<T>::updateFirstLastNode(){
clearFirstNode();
clearLastNode();
for(typename std::map<long, Node<T> >::iterator itNode(p_mapNode.begin()); itNode != p_mapNode.end(); ++itNode){
if(itNode->second.isStart()){ //If the Node does not have any parent, it is a first Node
p_listFirstNode.push_back(itNode->first);
}
if(itNode->second.isEnd()){ //If the Node does not have any child, it is a last Node
p_listLastNode.push_back(itNode->first);
}
}
}
///Connect a parent to its child
/** @param parent : index of the parent Node
* @param child : index of the child Node
......
......@@ -7,11 +7,7 @@
#ifndef __NODE_H__
#define __NODE_H__
#include <string>
#include <list>
///List of index to be used in the graph
typedef std::list<long> ListIndex;
#include "list_index_utils.h"
///@brief Node of a Graph
template<typename T>
......@@ -26,6 +22,9 @@ class Node{
void addChild(long child);
void addParent(long parent);
void removeChild(long child);
void removeParent(long parent);
void setListChild(const ListIndex & listChild);
void setListParent(const ListIndex & listParent);
void setIndex(long index);
......
......@@ -81,6 +81,22 @@ void Node<T>::addParent(long parent){
p_listParent.push_back(parent);
}
///Remove connection with child
/** @param child : index of the child to be removed
*/
template<typename T>
void Node<T>::removeChild(long child){
listindex_remove(p_listChild, child);
}
///Remove connection with parent
/** @param parent : index of the parent to be removed
*/
template<typename T>
void Node<T>::removeParent(long parent){
listindex_remove(p_listParent, parent);
}
///Set the list of children of the Node
/** @param listChild : list of children of the Node
*/
......
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#include "list_index_utils.h"
///Remove index from listIndex
/** @param[out] listIndex : list of index to be modified
* @param index : index to be removed from list of index
*/
void listindex_remove(ListIndex & listIndex, long index){
if(listIndex.size() == 0lu){return;}
ListIndex::iterator it(listIndex.begin());
while(it != listIndex.end()){
if(*it == index){
it = listIndex.erase(it);
}else{
++it;
}
}
}
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#ifndef __LIST_INDEX_UTILS_H__
#define __LIST_INDEX_UTILS_H__
#include <string>
#include <list>
///List of index to be used in the graph
typedef std::list<long> ListIndex;
void listindex_remove(ListIndex & listIndex, long index);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment