From 6d3043902cb9f981d08dcd6dee3f031c754e26a3 Mon Sep 17 00:00:00 2001 From: Naej Date: Fri, 23 Dec 2016 23:58:08 +0100 Subject: [PATCH 1/8] =?UTF-8?q?Avanc=C3=A9=20et=20presque=20fini=20la=20ge?= =?UTF-8?q?stion=20des=20gaudMatrices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LibAutomaton/CellElement.h | 2 +- LibAutomaton/matrix.c | 163 ++++++++++++++++++++++++++++++++++++- LibAutomaton/matrix.h | 35 ++++++++ 3 files changed, 198 insertions(+), 2 deletions(-) diff --git a/LibAutomaton/CellElement.h b/LibAutomaton/CellElement.h index c354609..d9557ce 100644 --- a/LibAutomaton/CellElement.h +++ b/LibAutomaton/CellElement.h @@ -87,7 +87,7 @@ void removeNextRow(cellElement* tree); /*---FreeCellElem--- -*Allocates a cellElement and returns it +*Frees a cellElement and returns it * *@element : pointer on the allocated cellElement * diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index a25ba21..6bb6480 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -1,6 +1,5 @@ #include #include -#include #include Matrix applyRules (Matrix matrix,int Rules, int N){ @@ -30,3 +29,165 @@ Matrix CreateMatrix(){ matrix.rows = CreateList(); return matrix; } + +void CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ + ListElement * Row = NULL; + ListElement * Col = NULL; + int error = 0; + cellElement * elem = NULL; + cellElement * tmp = NULL; + + 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->colIndex > ColPos){ + elem->nextCol = tmp->nextCol; + tmp->nextCol = elem; + }else if (tmp->colIndex == ColPos){ + 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->rowIndex > RowPos){ + elem->nextRow = tmp->nextRow; + tmp->nextRow = elem; + }else if (tmp->rowIndex == RowPos){ + error ++; + } + } + + }else { + push(matrix.cols,elem); + matrix.cols->tail->pos = ColPos; + } + + + if (error != 0){ + free(elem); + } +} + +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->colIndex != ColPos && elem != NULL){ + 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 || Row->data == NULL){ + 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; /* WTF ?? */ + } else { + tmp->nextCol = elem->nextCol; + } + + } + + + Col = GetElementPos(matrix.cols,ColPos); + if (Col == NULL|| Col->data == NULL){ + return -2; + } + 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; /* WTF ?? */ + } else { + tmp->nextRow = elem->nextRow; + } + + } + + FreeCellElement(elem); + return 1; + +} + +bool GetCellValue(Matrix matrix, int ColPos, int RowPos){ + if (FindMatrixElem(matrix,ColPos,RowPos) == NULL){ + return false; + } + return true; +} + +void SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){ + if (value == true){ + CreateMatrixElem(matrix,ColPos,RowPos); + }else{ + SupprMatrixElem(matrix,ColPos,RowPos); + } + +} +/* todos: +*gerer bornes matrice +*print matrice +*/ \ No newline at end of file diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index b3618e7..1916bce 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -37,7 +37,42 @@ typedef struct Matrix { */ Matrix applyRules(Matrix matrix,int Rules, int N); +/** +*Create a void Matrix +* +*@return a matrix +* +*/ 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 void +* +*/ +void CreateMatrixElem(Matrix matrix, int ColPos, int RowPos); + +int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos); + +void SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value); + +bool GetCellValue(Matrix matrix, int ColPos, int RowPos); #endif From 0a6c7524e0efbc9f277733410ce0f994ec59dfb2 Mon Sep 17 00:00:00 2001 From: Naej Date: Sat, 24 Dec 2016 13:39:14 +0100 Subject: [PATCH 2/8] fix suppr --- LibAutomaton/matrix.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index 6bb6480..6896a18 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -128,7 +128,11 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ } Row = GetElementPos(matrix.rows,RowPos); - if (Row == NULL || Row->data == NULL){ + if (Row == NULL){ + return -1; + } + if (Row->data == NULL){ + RemoveElementPos(matrix.rows,RowPos); return -1; } @@ -146,12 +150,19 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ } } + if (Row->data == NULL){ + RemoveElementPos(matrix.rows,RowPos); + } Col = GetElementPos(matrix.cols,ColPos); - if (Col == NULL|| Col->data == NULL){ + 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 { @@ -163,10 +174,12 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ return -4; /* WTF ?? */ } else { tmp->nextRow = elem->nextRow; - } - + } } - + if (Col->data == NULL){ + RemoveElementPos(matrix.cols,ColPos); + } + FreeCellElement(elem); return 1; From 8e72e571793386bc29b478c59316894d6d06ec86 Mon Sep 17 00:00:00 2001 From: Naej Date: Sat, 24 Dec 2016 13:53:04 +0100 Subject: [PATCH 3/8] ajout gestion bornes matrice --- LibAutomaton/CellElement.h | 3 ++- LibAutomaton/matrix.c | 29 ++++++++++++++++++++++++----- LibAutomaton/matrix.h | 7 +++++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/LibAutomaton/CellElement.h b/LibAutomaton/CellElement.h index d9557ce..60b27e9 100644 --- a/LibAutomaton/CellElement.h +++ b/LibAutomaton/CellElement.h @@ -9,7 +9,8 @@ typedef enum Bool{ true = 1, - false = 0 + false = 0, + ERROR = -1 } bool; diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index 6896a18..6a584aa 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -30,13 +30,21 @@ Matrix CreateMatrix(){ return matrix; } -void CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ +void SetMatrixDim(Matrix matrix,int nbCols,int nbRows){ + matrix.colCount = nbCols; + matrix.rowCount = nbRows; +} + +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); @@ -95,7 +103,11 @@ void CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ if (error != 0){ free(elem); + return true; + }else{ + return false; } + } cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos){ @@ -186,21 +198,28 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ } 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; } -void SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){ +bool SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){ if (value == true){ - CreateMatrixElem(matrix,ColPos,RowPos); + return CreateMatrixElem(matrix,ColPos,RowPos); }else{ - SupprMatrixElem(matrix,ColPos,RowPos); + if ( SupprMatrixElem(matrix,ColPos,RowPos) >= 0 ){ + return true; + }else{ + return false; + } } } /* todos: -*gerer bornes matrice *print matrice */ \ No newline at end of file diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index 1916bce..bfdd828 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -67,12 +67,15 @@ cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos); *@return void * */ -void CreateMatrixElem(Matrix matrix, int ColPos, int RowPos); +bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos); int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos); -void SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value); +bool SetCellValue(Matrix matrix, int ColPos, int RowPos, bool value); bool GetCellValue(Matrix matrix, int ColPos, int RowPos); + +void SetMatrixDim(Matrix matrix,int nbCols,int nbRows); + #endif From db8322f504c8aba25d4c2b6ecdf1fee00e7d981a Mon Sep 17 00:00:00 2001 From: Naej Date: Sat, 24 Dec 2016 15:18:19 +0100 Subject: [PATCH 4/8] =?UTF-8?q?ajout=C3=A9=20print=20et=20corrig=C3=A9=20p?= =?UTF-8?q?lein=20de=20fautes,=20reste=20une=20fuite=20m=C3=A9moire=20:=20?= =?UTF-8?q?on=20peut=20cr=C3=A9er=20deux=20fois=20la=20meme=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LibAutomaton/CellElement.c | 3 +++ LibAutomaton/matrix.c | 48 ++++++++++++++++++++++++++++---------- LibAutomaton/matrix.h | 4 +++- main.c | 36 +++++++--------------------- 4 files changed, 51 insertions(+), 40 deletions(-) diff --git a/LibAutomaton/CellElement.c b/LibAutomaton/CellElement.c index 6dc9f88..165425f 100644 --- a/LibAutomaton/CellElement.c +++ b/LibAutomaton/CellElement.c @@ -127,6 +127,9 @@ void recursivePrint(cellElement * tree){ } void FreeCellElement(cellElement* element) { + + printf("---FreeCellElement---\n"); + if (element != NULL){ free(element); } diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index 6a584aa..3b4477a 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -30,9 +30,10 @@ Matrix CreateMatrix(){ return matrix; } -void SetMatrixDim(Matrix matrix,int nbCols,int nbRows){ +Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows){ matrix.colCount = nbCols; matrix.rowCount = nbRows; + return matrix; } bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ @@ -42,7 +43,7 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ cellElement * elem = NULL; cellElement * tmp = NULL; - if (matrix.colCount < ColPos || matrix.rowCount < RowPos){ + if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos ){ return ERROR; } elem = CreateCellElem(); @@ -61,10 +62,10 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ while (tmp->nextCol != NULL && tmp->nextCol->colIndex < ColPos){ tmp=tmp->nextCol; } - if (tmp->nextCol->colIndex > ColPos){ + if (tmp->nextCol == NULL || tmp->nextCol->colIndex > ColPos){ elem->nextCol = tmp->nextCol; tmp->nextCol = elem; - }else if (tmp->colIndex == ColPos){ + }else { error ++; } } @@ -87,10 +88,10 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ while (tmp->nextRow != NULL && tmp->nextRow->rowIndex < RowPos){ tmp=tmp->nextRow; } - if (tmp->nextRow->rowIndex > RowPos){ + if (tmp->nextRow == NULL || tmp->nextRow->rowIndex > RowPos){ elem->nextRow = tmp->nextRow; tmp->nextRow = elem; - }else if (tmp->rowIndex == RowPos){ + }else { error ++; } } @@ -120,7 +121,7 @@ cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos){ } elem = Row->data; - while (elem->colIndex != ColPos && elem != NULL){ + while (elem != NULL && elem->colIndex != ColPos){ elem = elem->nextCol; } @@ -198,7 +199,7 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ } bool GetCellValue(Matrix matrix, int ColPos, int RowPos){ - if (matrix.colCount < ColPos || matrix.rowCount < RowPos){ + if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos){ return ERROR; } @@ -218,8 +219,31 @@ bool SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){ return false; } } - } -/* todos: -*print matrice -*/ \ No newline at end of file + +void BasicPrintMatrix(Matrix matrix){ +/* Non optimisé : debug fx */ + int i = 0; + int j = 0; + + printf("\n---PRINT MATRIX---\n"); + + for (i=0;inextRow,1,2); - - AddNextCol(tree); - SetPositionIndex(tree->nextCol,1,3); - - SetNextRow(tree->nextCol,tree->nextRow); - - recursivePrint(tree); - - - removeNextRow(tree); - removeNextCol(tree); - - recursivePrint(tree); -*/ + SetCellValue(matrix,0,0,false); + SetCellValue(matrix,0,0,false); + BasicPrintMatrix(matrix); return 0; } From 6c6bd307f8876ef7861a59ac0d860cfac414ced2 Mon Sep 17 00:00:00 2001 From: Naej Date: Sat, 24 Dec 2016 16:07:50 +0100 Subject: [PATCH 5/8] =?UTF-8?q?en=20fait=20il=20n'y=20avait=20pas=20de=20b?= =?UTF-8?q?ug=20sur=20le=20point=20abord=C3=A9=20ds=20le=20commit=20preced?= =?UTF-8?q?ent,=20commence=20free=20matrix,=20necessite=20une=20mise=20au?= =?UTF-8?q?=20pt=20avec=20sli?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LibAutomaton/CellElement.c | 3 ++- LibAutomaton/CellElement.h | 2 -- LibAutomaton/matrix.c | 20 ++++++++++++++++++-- LibAutomaton/matrix.h | 2 ++ main.c | 12 +++++++++--- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/LibAutomaton/CellElement.c b/LibAutomaton/CellElement.c index 165425f..96fc5c8 100644 --- a/LibAutomaton/CellElement.c +++ b/LibAutomaton/CellElement.c @@ -16,7 +16,6 @@ cellElement * CreateCellElem(){ printf("---Created cellElement---\n"); - elem->value = true; elem->nextCol = NULL; elem->nextRow = NULL; @@ -132,6 +131,8 @@ void FreeCellElement(cellElement* element) { if (element != NULL){ free(element); + }else{ + printf("Cant free NULL"); } element = NULL; } diff --git a/LibAutomaton/CellElement.h b/LibAutomaton/CellElement.h index 60b27e9..01c44f7 100644 --- a/LibAutomaton/CellElement.h +++ b/LibAutomaton/CellElement.h @@ -32,8 +32,6 @@ struct cellElement { int colIndex; int rowIndex; - bool value; - struct cellElement * nextCol; struct cellElement * nextRow; diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index 3b4477a..a0b9154 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -103,7 +103,7 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ if (error != 0){ - free(elem); + FreeCellElement(elem); return true; }else{ return false; @@ -246,4 +246,20 @@ void BasicPrintMatrix(Matrix matrix){ } printf("---END OF MATRIX---\n\n"); -} \ No newline at end of file +} + +Matrix freeMatrix(Matrix matrix){ + matrix.colCount = 0; + matrix.rowCount = 0; + /*il faut free les cellElements car FreeList ne peut pas le faire*/ + 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 +*/ \ No newline at end of file diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index c3a38bc..797f5c8 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -79,5 +79,7 @@ Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows); void BasicPrintMatrix(Matrix matrix); +Matrix freeMatrix(Matrix matrix); + #endif diff --git a/main.c b/main.c index bd8e0e6..6973351 100644 --- a/main.c +++ b/main.c @@ -15,17 +15,23 @@ int main(int argc, char **argv){ int Rule = 256; int N = 1; applyRules(matrix,Rule,N); - matrix = SetMatrixDim(matrix,1,2); + matrix = SetMatrixDim(matrix,3,3); BasicPrintMatrix(matrix); SetCellValue(matrix,0,0,true); - SetCellValue(matrix,0,0,true); + SetCellValue(matrix,0,1,true); + SetCellValue(matrix,0,2,true); BasicPrintMatrix(matrix); SetCellValue(matrix,0,0,false); - SetCellValue(matrix,0,0,false); + SetCellValue(matrix,0,1,false); BasicPrintMatrix(matrix); + + freeMatrix(matrix); return 0; } +/* todo +*modifier DeleteListContent avec sli +*/ \ No newline at end of file From f30e968e0a880c4eacd80419db726d057f16fbc1 Mon Sep 17 00:00:00 2001 From: Naej Date: Sat, 24 Dec 2016 16:41:06 +0100 Subject: [PATCH 6/8] added some doc --- LibAutomaton/matrix.h | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index 797f5c8..92aca29 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -64,17 +64,48 @@ cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos); *@param ColPos an int indicating the column of the cell *@param RowPos an int indicating the row of the cell * -*@return void +*@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); + Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows); void BasicPrintMatrix(Matrix matrix); From 22b653db679859e71893f1c347483cab6c134558 Mon Sep 17 00:00:00 2001 From: Naej Date: Mon, 26 Dec 2016 00:06:54 +0100 Subject: [PATCH 7/8] fini freematrix :) --- LibAutomaton/CellElement.c | 1 + LibAutomaton/list.c | 4 +-- LibAutomaton/matrix.c | 59 ++++++++++++++++++++++++++++++++++++-- LibAutomaton/matrix.h | 3 ++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/LibAutomaton/CellElement.c b/LibAutomaton/CellElement.c index 96fc5c8..e0fbfa1 100644 --- a/LibAutomaton/CellElement.c +++ b/LibAutomaton/CellElement.c @@ -136,3 +136,4 @@ void FreeCellElement(cellElement* element) { } element = NULL; } + diff --git a/LibAutomaton/list.c b/LibAutomaton/list.c index 8b8a7e1..e44b428 100644 --- a/LibAutomaton/list.c +++ b/LibAutomaton/list.c @@ -176,11 +176,11 @@ int DeleteListContent(List *list){ while (current != NULL){ toDelete = current; current = current->next; - +/* if (toDelete->data != NULL){ FreeCellElement(toDelete->data); } - +*/ free(toDelete); } list->head = NULL; diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index a0b9154..620ec8f 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -157,7 +157,7 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ tmp = tmp->nextCol; } if (tmp->nextCol == NULL){ - return -3; /* WTF ?? */ + return -3; /* should never happend */ } else { tmp->nextCol = elem->nextCol; } @@ -184,7 +184,7 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ tmp = tmp->nextRow; } if (tmp->nextRow == NULL){ - return -4; /* WTF ?? */ + return -4; /* should never happend */ } else { tmp->nextRow = elem->nextRow; } @@ -248,15 +248,70 @@ void BasicPrintMatrix(Matrix matrix){ 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 diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index 92aca29..b1cbef5 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -110,7 +110,10 @@ Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows); void BasicPrintMatrix(Matrix matrix); +bool RecursiveFreeCol(Matrix matrix, cellElement * elem); + Matrix freeMatrix(Matrix matrix); + #endif From cdd5dfc859d44ba640cdd03f205b6eb07c613f24 Mon Sep 17 00:00:00 2001 From: Naej Date: Mon, 26 Dec 2016 00:13:06 +0100 Subject: [PATCH 8/8] added some doc --- LibAutomaton/matrix.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index b1cbef5..cabe29c 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -105,9 +105,26 @@ bool SetCellValue(Matrix matrix, int ColPos, int RowPos, bool value); */ 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);