1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-29 23:04:15 +00:00

Merge branch 'NaejBranch' into 'master'

Naej branch

See merge request !10
This commit is contained in:
Antoine Bartuccio 2016-12-26 15:44:02 +00:00
commit ed0aac6021
6 changed files with 407 additions and 34 deletions

View File

@ -16,7 +16,6 @@ cellElement * CreateCellElem(){
printf("---Created cellElement---\n"); printf("---Created cellElement---\n");
elem->value = true;
elem->nextCol = NULL; elem->nextCol = NULL;
elem->nextRow = NULL; elem->nextRow = NULL;
@ -127,8 +126,14 @@ void recursivePrint(cellElement * tree){
} }
void FreeCellElement(cellElement* element) { void FreeCellElement(cellElement* element) {
printf("---FreeCellElement---\n");
if (element != NULL){ if (element != NULL){
free(element); free(element);
}else{
printf("Cant free NULL");
} }
element = NULL; element = NULL;
} }

View File

@ -9,7 +9,8 @@
typedef enum Bool{ typedef enum Bool{
true = 1, true = 1,
false = 0 false = 0,
ERROR = -1
} bool; } bool;
@ -31,8 +32,6 @@ struct cellElement {
int colIndex; int colIndex;
int rowIndex; int rowIndex;
bool value;
struct cellElement * nextCol; struct cellElement * nextCol;
struct cellElement * nextRow; struct cellElement * nextRow;
@ -87,7 +86,7 @@ void removeNextRow(cellElement* tree);
/*---FreeCellElem--- /*---FreeCellElem---
*Allocates a cellElement and returns it *Frees a cellElement and returns it
* *
*@element : pointer on the allocated cellElement *@element : pointer on the allocated cellElement
* *

View File

@ -176,11 +176,11 @@ int DeleteListContent(List *list){
while (current != NULL){ while (current != NULL){
toDelete = current; toDelete = current;
current = current->next; current = current->next;
/*
if (toDelete->data != NULL){ if (toDelete->data != NULL){
FreeCellElement(toDelete->data); FreeCellElement(toDelete->data);
} }
*/
free(toDelete); free(toDelete);
} }
list->head = NULL; list->head = NULL;

View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <CellElement.h>
#include <matrix.h> #include <matrix.h>
Matrix applyRules (Matrix matrix,int Rules, int N){ Matrix applyRules (Matrix matrix,int Rules, int N){
@ -30,3 +29,292 @@ Matrix CreateMatrix(){
matrix.rows = CreateList(); matrix.rows = CreateList();
return matrix; return matrix;
} }
Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows){
matrix.colCount = nbCols;
matrix.rowCount = nbRows;
return matrix;
}
bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
ListElement * Row = NULL;
ListElement * Col = NULL;
int error = 0;
cellElement * elem = NULL;
cellElement * tmp = NULL;
if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos ){
return ERROR;
}
elem = CreateCellElem();
SetPositionIndex(elem,ColPos,RowPos);
Row = GetElementPos(matrix.rows,RowPos);
if (Row != NULL && Row->data != NULL){
if (Row->data->colIndex == ColPos){
error ++;
} else if (Row->data->colIndex > ColPos){
elem->nextCol = Row->data;
Row->data = elem;
} else {
tmp = Row->data;
while (tmp->nextCol != NULL && tmp->nextCol->colIndex < ColPos){
tmp=tmp->nextCol;
}
if (tmp->nextCol == NULL || tmp->nextCol->colIndex > ColPos){
elem->nextCol = tmp->nextCol;
tmp->nextCol = elem;
}else {
error ++;
}
}
}else {
push(matrix.rows,elem);
matrix.rows->tail->pos = RowPos;
}
Col = GetElementPos(matrix.cols,ColPos);
if (Col != NULL && Col->data != NULL){
if (Col->data->rowIndex == RowPos){
error ++;
} else if (Col->data->rowIndex > RowPos){
elem->nextRow = Col->data;
Col->data = elem;
} else {
tmp = Col->data;
while (tmp->nextRow != NULL && tmp->nextRow->rowIndex < RowPos){
tmp=tmp->nextRow;
}
if (tmp->nextRow == NULL || tmp->nextRow->rowIndex > RowPos){
elem->nextRow = tmp->nextRow;
tmp->nextRow = elem;
}else {
error ++;
}
}
}else {
push(matrix.cols,elem);
matrix.cols->tail->pos = ColPos;
}
if (error != 0){
FreeCellElement(elem);
return true;
}else{
return false;
}
}
cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos){
ListElement * Row = NULL;
cellElement * elem = NULL;
Row = GetElementPos(matrix.rows,RowPos);
if (Row == NULL){
return NULL;
}
elem = Row->data;
while (elem != NULL && elem->colIndex != ColPos){
elem = elem->nextCol;
}
return elem;
}
int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){
cellElement * elem = NULL;
cellElement * tmp = NULL;
ListElement * Row = NULL;
ListElement * Col = NULL;
elem = FindMatrixElem(matrix,ColPos,RowPos);
if (elem == NULL){
return 0;
}
Row = GetElementPos(matrix.rows,RowPos);
if (Row == NULL){
return -1;
}
if (Row->data == NULL){
RemoveElementPos(matrix.rows,RowPos);
return -1;
}
if (Row->data->colIndex == ColPos){
Row->data = elem->nextCol;
} else {
tmp = Row->data;
while (tmp->nextCol != NULL && tmp->nextCol != elem){
tmp = tmp->nextCol;
}
if (tmp->nextCol == NULL){
return -3; /* should never happend */
} else {
tmp->nextCol = elem->nextCol;
}
}
if (Row->data == NULL){
RemoveElementPos(matrix.rows,RowPos);
}
Col = GetElementPos(matrix.cols,ColPos);
if (Col == NULL){
return -2;
}
if (Col->data == NULL){
RemoveElementPos(matrix.cols,ColPos);
return -1;
}
if (Col->data->rowIndex == RowPos){
Col->data = elem->nextRow;
} else {
tmp = Col->data;
while (tmp->nextRow != NULL && tmp->nextRow != elem){
tmp = tmp->nextRow;
}
if (tmp->nextRow == NULL){
return -4; /* should never happend */
} else {
tmp->nextRow = elem->nextRow;
}
}
if (Col->data == NULL){
RemoveElementPos(matrix.cols,ColPos);
}
FreeCellElement(elem);
return 1;
}
bool GetCellValue(Matrix matrix, int ColPos, int RowPos){
if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos){
return ERROR;
}
if (FindMatrixElem(matrix,ColPos,RowPos) == NULL){
return false;
}
return true;
}
bool SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){
if (value == true){
return CreateMatrixElem(matrix,ColPos,RowPos);
}else{
if ( SupprMatrixElem(matrix,ColPos,RowPos) >= 0 ){
return true;
}else{
return false;
}
}
}
void BasicPrintMatrix(Matrix matrix){
/* Non optimisé : debug fx */
int i = 0;
int j = 0;
printf("\n---PRINT MATRIX---\n");
for (i=0;i<matrix.colCount;i++){
printf("| ");
for (j=0;j<matrix.rowCount;j++){
if (GetCellValue(matrix,i,j) == true){
printf("1 ");
}else if (GetCellValue(matrix,i,j) == false){
printf("0 ");
}else{
printf("X "); /* error out of bounds, should never happend*/
}
}
printf("|\n");
}
printf("---END OF MATRIX---\n\n");
}
bool RecursiveFreeCol(Matrix matrix, cellElement * elem){
cellElement * tmp = NULL;
ListElement * Row = NULL;
if (elem == NULL){
return ERROR;
}
if (elem->nextRow != NULL){
RecursiveFreeCol(matrix,elem->nextRow);
}
if (elem->nextRow == NULL){
Row = GetElementPos(matrix.rows,elem->rowIndex);
if (Row == NULL || Row->data == NULL ){
return ERROR; /*should never happend*/
}
if (Row->data->colIndex == elem->colIndex){
Row->data = NULL;
} else {
tmp = Row->data;
while (tmp->nextCol != NULL && tmp->nextCol != elem){
tmp = tmp->nextCol;
}
if (tmp->nextCol == NULL){
return ERROR; /* should never happend */
} else {
tmp->nextCol = elem->nextCol;
}
}
FreeCellElement(elem);
return true;
}
return false; /* should never happend */
}
Matrix freeMatrix(Matrix matrix){
ListElement * current = NULL;
matrix.colCount = 0;
matrix.rowCount = 0;
/*il faut free les cellElements car FreeList ne peut pas le faire*/
if (matrix.cols != NULL){
current= matrix.cols->head;
while (current != NULL){
RecursiveFreeCol(matrix,current->data);
current = current->next;
}
}
FreeList(matrix.cols);
FreeList(matrix.rows);
return matrix;
}
/* todos :
*finir le freeMatrix
*chasser les bugs
*ecrire doc
*faire un print + opti pour que sli l'adapte avec sdl
*/

View File

@ -37,7 +37,100 @@ typedef struct Matrix {
*/ */
Matrix applyRules(Matrix matrix,int Rules, int N); Matrix applyRules(Matrix matrix,int Rules, int N);
/**
*Create a void Matrix
*
*@return a matrix
*
*/
Matrix CreateMatrix(); Matrix CreateMatrix();
/**
*Find and return the cell in the given matrix
*
*@param matrix the Matrix where we search
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return a cellElement
*
*/
cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos);
/**
*Create the cell in the given matrix
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return a bool (error code)
*
*/
bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos);
/**
*Delete the cell in the given matrix
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return an error code (int)
*
*/
int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos);
/**
*Delete or create the cell in the given matrix to fit the value
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return an error code (bool)
*
*/
bool SetCellValue(Matrix matrix, int ColPos, int RowPos, bool value);
/**
*Checks out the value of the cell in the given matrix
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return the value (bool)
*
*/
bool GetCellValue(Matrix matrix, int ColPos, int RowPos);
/**
*Set the number of columns and rows of the matrix and returns it
*
*@param matrix the Matrix
*@param nbCols an int indicating the number of columns
*@param nbRows an int indicating the number of rows
*
*@return the matrix
*
*/
Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows);
/**
*Basically print the Matrix in the standard output
*
*@param matrix the Matrix
*
*@return void
*
*/
void BasicPrintMatrix(Matrix matrix);
bool RecursiveFreeCol(Matrix matrix, cellElement * elem);
Matrix freeMatrix(Matrix matrix);
#endif #endif

40
main.c
View File

@ -12,38 +12,26 @@
int main(int argc, char **argv){ int main(int argc, char **argv){
Matrix matrix = CreateMatrix(); Matrix matrix = CreateMatrix();
int Rule = 170; int Rule = 256;
int N = 1; int N = 1;
applyRules(matrix,Rule,N); applyRules(matrix,Rule,N);
/* matrix = SetMatrixDim(matrix,3,3);
Matrix matrix;
cellElement * tree = NULL;
List * cols = NULL;
List * rows = NULL;
cols = CreateList(); BasicPrintMatrix(matrix);
rows = CreateList();
SetCellValue(matrix,0,0,true);
SetCellValue(matrix,0,1,true);
SetCellValue(matrix,0,2,true);
BasicPrintMatrix(matrix);
tree = CreateCellElem(); SetCellValue(matrix,0,0,false);
SetPositionIndex(tree,1,1); SetCellValue(matrix,0,1,false);
BasicPrintMatrix(matrix);
AddNextRow(tree); freeMatrix(matrix);
SetPositionIndex(tree->nextRow,1,2);
AddNextCol(tree);
SetPositionIndex(tree->nextCol,1,3);
SetNextRow(tree->nextCol,tree->nextRow);
recursivePrint(tree);
removeNextRow(tree);
removeNextCol(tree);
recursivePrint(tree);
*/
return 0; return 0;
} }
/* todo
*modifier DeleteListContent avec sli
*/