Commit 7e67be1e authored by Kevin Coulomb's avatar Kevin Coulomb

Ajout de templates pour la creation de l'arbre binaire. Les tests pour

verifier le template n'ont pas encore été realisé (possibilité 
d'erreurs)
parent 618d4633
......@@ -19,6 +19,8 @@
/* For OpenGL text rendering */
#include <GL/glut.h>
/* These paths corresponding to the path in the resource file ('vite.qrc' in the 'bin' folder) */
/*!
......@@ -64,6 +66,8 @@
exit(EXIT_FAILURE); \
}
/* Define the unity of time */
typedef double Times;
......
#ifndef BINARYTREE
#define BINARYTREE
#include <iostream>
#include "Node.hpp"
#include "Interval.hpp"
template <E>
class BinaryTree{
Node <E> * _current;
/*
*
*\brief To visit the tree
*
*/
Node <E> *_father;
Node <E> *_left_child;
Node <E> *_right_child;
public :
/*
*
*\fn BinaryTree<E> ()
*\brief Default constructor
*
*/
BinaryTree<E>(){
_father = NULL;
_left_child = NULL;
_right_child = NULL;
_current = new Node<E>();
}
/*
*
*\fn set_current_node( Node * event )
*\brief Set the current node
*
*/
void set_current_node( Node <E> * node ){
this->_current = node;
}
/*
*
*\fn get_event()
*\brief Return the event field
*
*/
Node <E> *get_event(){
return _current;
}
/*
*
*\fn set_father( Node * )
*\brief Set the father's node
*
*/
void set_father( Node <E> * father ){
this->_father = father;
}
/*
*
*\fn get_father()
*\brief Returns the father
*
*/
Node <E> * get_father(){
return _father;
}
/*
*
*\fn set_left_child( Node * child )
*\brief Set only the left child to child
*
*/
void set_left_child( Node <E> * child ){
this->_left_child = child;
}
/*
*
*\fn set_right_child( Node * child )
*\brief Set only the right child to child
*
*/
void set_right_child( Node <E> * child ){
this->_right_child = child;
}
/*
*
*\fn get_left_child()
*\brief Return only the left child
*
*/
Node <E> * get_left_child(){
return _left_child;
}
/*
*
*\fn get_right_child()
*\brief Return only the right child
*
*/
Node <E> * get_right_child(){
return _right_child;
}
/*
*
*\fn set_children ( Node * leftChild , Node * rightChild)
*\brief Calling setleft and set right, set the 2 children
*
*/
void set_children ( Node <E> * leftChild , Node <E> * rightChild){
this->_left_child = leftChild;
this->_right_child = rightChild;
}
/*
*
*\fn get_children()
*\brief Return a pointer to a structure that contain respectively the left then the right child
*
*/
Node <E> * get_children(){
Node<E> * res[2];
res[0] = get_left_child();
res[1] = get_right_child();
return res;
}
};
#endif
#include <iostream>
template <E>
class Data{
std::string name;
std::string description;
E element;
}
#include "Interval.hpp"
Interval::Interval(){
leftSide = 0;
rightSide = 100;
interval = new Interval[4];
}
void Interval::setLeft( time left ){
leftSide = left;
}
void Interval::setRight( time right ){
rightSide = right;
}
time Interval::getLeft (){
return leftSide;
}
time Interval::getRight (){
return rightSide;
}
#ifndef INTERVAL
#define INTERVAL
#include "../../interface/ressource.hpp"
class Interval{
times leftSide;
times rightSide;
public :
Interval();
void setLeft( times left );
void setRight( times right );
times getLeft () const;
times getRight () const;
};
#endif
PP = g++ -Wall -c
all : BinaryTree.o Interval.o Node.o TreeBuilder.o TreeUser.o
clean :
rm *~ *.o
BinaryTree.o : BinaryTree.hpp
$(PP) BinaryTree.hpp -o BinaryTree.o
Interval.o : Interval.cpp
$(PP) Interval.cpp -o Interval.o
Node.o : Node.hpp
$(PP) Node.hpp -o Node.o
TreeBuilder.o : TreeBuilder.hpp
$(PP) TreeBuilder.hpp -o TreeBuilder.o
TreeUser.o : TreeUser.hpp
$(PP) TreeUser.hpp -o TreeUser.o
\ No newline at end of file
/*
*
* \brief File that contains the definition of a Node in the binary tree that contain the events
*\author COULOMB Kevin
*\date 03/09
*
*/
#ifndef NODEEVENT
#define NODEEVENT
#include <iostream>
//#include "../../../svn/svn_forge/vite/source/src/trace/values/"
#include "Interval.hpp"
#include "Data.hpp"
template <E>
class Node{
/*
*
*\brief Used in the structure to know whether it is the beginning or the event of a state
*
*/
static const int BEGINNING=0;
static const int END=1;
/*
*
*\brief The moment when the event happen
*
*/
Date _happen;
/*
*
*\brief Keeps the intervalls of it's children that have conflicts
*
*/
Interval * _interval;
/*
*
*\brief To know whether it is the beginning or the end of an event
*
*/
int _typeofevent;
/*
*
*\brief Contains the information of the event that will be used when more information aboit a state are required :
* Name, type if it is a state, ...
*
*/
Data <E> * _caracteristic_of_event;
public :
/*
*
*\fn Node()
*\brief Default constructor
*
*/
Node(){
_happen = 0;
_interval = new Interval();
_caracteristic_of_event = new Data<E>();
}
/*
*
*\fn ~Node()
*\brief Destructor
*
*/
~Node(){
delete _interval;
}
/*
*
*\fn setTypeOfEvent(int TYPE)
*\brief Set the type of event to BEGINNING or END
*
*/
void setTypeOfEvent(int TYPE){
_typeofevent = TYPE;
}
/*
*
*\fn getTypeOfEvent()
*\brief Get the type of event to BEGINNING or END
*
*/
int getTypeOfEvent(){
return _typeofevent;
}
/*
*
*\fn getCaracteristic( Data* data )
*\brief Get all the caracteristic of the data
*
*/
Data <E> * getCaracteristic(){
return _data;
}
/*
*
*\fn getName()
*\brief Returns only the name of the container in the data
*
*/
std::string getName(){
return _data.getName();
}
/*
*
*\fn getDescription()
*\brief Returns only the description of the data
*
*/
std::string getDescription(){
return _data.getDescription();
}
/*
*
*\fn getDate()
*\brief Return the date when the event happens
*
*/
Date * getDate(){
return _happen;
}
/*
*
*\fn setDate(Date * date)
*\fn Set happen parameter to date
*
*/
void setDate( Date * date){
_happen = * date;
}
};
#endif
#ifndef TREEBUILDER
#define TREEBUILDER
/*
*
*\brief This class is used in order to build a binary tree from a list
*\author Kevin COULOMB
*\date March 09
*
*/
#include "BinaryTree.hpp"
#inclue "Node.hpp"
template <E>
class TreeBuilder(){
std::list <E> _list;
BinaryTree <E> *_tree;
/*
*
*\brief Assuming there are N items in the list
*
*
*/
int _size;
/*
*
*\brief TableNode is a private class defined at the end of this one
*
*
*/
TableNode<E> *_temporary;// = new TableNode[size];
/*
*
*\brief The position where we have to begin in the table (avoid testing again if the first box is known to be locked)
*
*/
int _begining;
public :
/*
*
*\fn TreeBuilder()
*\brief Default constructor
*
*/
TreeBuilder(){
_size = 1;
_list = NULL;
_tree = NULL;
_beginning = 0;
_temporary = new TableNode[size];
}
/*
*
*\fn inc_beginning()
*\brief Increment of 1 the value of beginning
*
*/
void inc_beginning(){
this->_beginning ++;
}
/*
*
*\fn set_beginning ( int )
*\brief Set the beginning to 'value'
*
*/
void set_beginning(int value){
this->_beginning = value;
}
/*
*
*\fn get_beginning()
*\brief Return where to begin in the table
*
*/
int get_beginning(){
return _beginning;
}
/*
*
*\fn set_size( int )
*\brief Set the size of the table
*
*/
void set_size(int size){
this->_size = size;
}
/*
*
*\fn get_size()
*\brief Return the size of the table
*
*/
int get_size(){
return this->_size;
}
/*
*
*\fn clear_tree()
*\brief Clear the memory linked to the binary tree
*
*/
void clear_tree(){
BinaryTree<E> * copy = tree->getRoot();
copy->clear_tree();
}
/*
*
*\fn set_list(std::list *)
*\brief set the list that must be used to build the binary tree
*
*/
void set_list( list <E *> list){
this->_list = list;
}
/*
*
*\fn delete_list()
*\brief Function called to clean the memory because the list is useless once the tree is built
*
*/
void delete_list(){
std::list<E> * next;
while(_list){
if(_list->has_next()){
next = _list->next();
}
else
next = NULL;
delete _list;
_list = next;
}
}
/*
*
*\fn build_tree()
*\brief Function that uses the list to build the tree
*
*/
void build_tree(){
BinaryTree <E> * tmp;
int pos = 0;
int cpt;
int cptlimit=0;
BinaryTree <E> * father;
BinaryTree <E> * leftChild;
BinaryTree <E> * rightChild;
// Attention a la methode
if(_list.current() != NULL){
int size_list = _size;
this->_beginning = 0;
// Number of element = N = 2^(n-1)+m
int n = get_power(size_list);
int m = get_rest(n-1,size_list);
int limit = 2 * m -1;
this->_temporary = new TableNode[n];
// voir si go beginning est correct
this->_list = go_beginning();
while (this->_list->has_next() ){
// create a node with the current element of the list
tmp = create_binary_tree(_list->get_current());
// Getting the first position free where we can insert the node in the table
pos = get_first_freebox();
// Set the node in the table
set_node(tmp.get_current_node(),pos);
// To look if there is links that can already be put
tmp = _temporary[pos];
// If it is a leaf
if(pos == _beginning){
tmp->set_children(NULL,NULL);
}
else{
// Previous node = left child
leftChild = _temporary[pos-1];
leftChild->set_father(tmp.get_current_node());
tmp->set_left_child(leftChild->get_current_node());
// Removing the previous nodes in the table
for(cpt=_beginning;cpt<pos;cpt++){
_temporary[cpt].set_free();
_temporary[cpt].set_node(NULL);
}
}//end else
if(_temporary[pos+1].is_busy()){
rightChild = tmp;
tmp = _temporary[pos+1];
rightChild->set_father(tmp.get_current_node());
tmp->set_right_child(rightChild.get_current_node());
}
if ( cptlimit == limit ){
_temporary[0].set_locked();
}
cptlimit ++;
// Attention a la methode get_next
this->_list = this->_list->get_next();
}//endwhile
// Treating the last Node and filling the tree
tmp = create_binary_tree(_list->get_current());
pos = get_first_freebox();
set_node(tmp.get_current_node(),pos);
tmp = _temporary[pos];
// If it is a leaf
if(pos == _beginning){
tmp->set_children(NULL,NULL);
}
else{
// Previous node = left child
leftChild = _temporary[pos-1];
leftChild->set_father(tmp.get_current_node());
tmp->set_left_child(leftChild.get_current_node());
// Removing the previous nodes in the table
for(cpt=_beginning;cpt<pos;cpt++){
_temporary[cpt].set_free();
_temporary[cpt].set_node(NULL);
}
}//end else
if(_temporary[pos+1].is_busy()){
rightChild = tmp;
tmp = _temporary[pos+1].get_node();
rightChild->set_father(tmp);
tmp->set_right_child(rightChild);
}
this->_tree = tmp;
}
else{
this->_beginning = -1;
this->_tree = new BinaryTree();
}
}
private :
/*
*
*\fn get_rest(int power, int size)
*\brief Return the rest of size minus 2^power
*
*/
int get_rest(int power, int size){
return (size_list - pow(2,n));
}
/*
*
*\fn get_power(int size)
*\brief Calculate and return the number j to have 2^j<size<2^(j+1)
*
*/
int get_power(int size){
int tmp = 0;
while ( pow(2,tmp) < size ){