From 6d3043902cb9f981d08dcd6dee3f031c754e26a3 Mon Sep 17 00:00:00 2001 From: Naej Date: Fri, 23 Dec 2016 23:58:08 +0100 Subject: [PATCH] =?UTF-8?q?Avanc=C3=A9=20et=20presque=20fini=20la=20gestio?= =?UTF-8?q?n=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