diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index dad465a..40893f1 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -51,7 +51,7 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ Row = GetElementPos(matrix.rows,RowPos); if (Row != NULL && Row->data != NULL){ - + if (Row->data->colIndex == ColPos){ error ++; } else if (Row->data->colIndex > ColPos){ @@ -77,7 +77,7 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ Col = GetElementPos(matrix.cols,ColPos); if (Col != NULL && Col->data != NULL){ - + if (Col->data->rowIndex == RowPos){ error ++; } else if (Col->data->rowIndex > RowPos){ @@ -134,7 +134,7 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ ListElement * Row = NULL; ListElement * Col = NULL; - + elem = FindMatrixElem(matrix,ColPos,RowPos); if (elem == NULL){ return 0; @@ -161,7 +161,7 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ } else { tmp->nextCol = elem->nextCol; } - + } if (Row->data == NULL){ RemoveElementPos(matrix.rows,RowPos); @@ -187,12 +187,12 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){ return -4; /* should never happend */ } else { tmp->nextRow = elem->nextRow; - } + } } if (Col->data == NULL){ RemoveElementPos(matrix.cols,ColPos); } - + FreeCellElement(elem); return 1; @@ -225,16 +225,18 @@ void BasicPrintMatrix(Matrix matrix){ /* Non optimisé : debug fx */ int i = 0; int j = 0; + bool b; printf("\n---PRINT MATRIX---\n"); - for (i=0;inextRow != 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*/ } @@ -280,7 +282,7 @@ bool RecursiveFreeCol(Matrix matrix, cellElement * elem){ } else { tmp->nextCol = elem->nextCol; } - + } FreeCellElement(elem); @@ -311,6 +313,75 @@ Matrix freeMatrix(Matrix matrix){ } +Matrix colSequenceOnMatrix(Matrix m, bool (operator)(bool, bool)){ + bool a; + bool b; + int i; + int j; + Matrix newM = CreateMatrix(); + + + newM.rowCount = m.rowCount; + if (m.colCount <= 1){ + newM.colCount = 0; + return newM; + } + newM.colCount = m.colCount - 1; + + for (i=0;i < m.colCount - 1;i++){ + for (j=0;j < m.rowCount;j++){ + a = GetCellValue(m, i, j); + b = GetCellValue(m, i + 1, j); + if (operator(a, b)){ + SetCellValue(newM, i, j, true); + } + } + } + return newM; +} + +Matrix rowSequenceOnMatrix(Matrix m, bool (operator)(bool, bool)){ + bool a; + bool b; + int i; + int j; + Matrix newM = CreateMatrix(); + + newM.colCount = m.colCount; + if (m.rowCount <= 1){ + newM.rowCount = 0; + return newM; + } + newM.rowCount = m.rowCount - 1; + + for (i=0; i < m.colCount;i++){ + for (j=0;j < m.rowCount - 1;j++){ + a = GetCellValue(m, i, j); + b = GetCellValue(m, i, j + 1); + if (operator(a, b)){ + SetCellValue(newM, i, j, true); + } + } + } + return newM; +} + +Matrix andColSequenceOnMatrix(Matrix m){ + return colSequenceOnMatrix(m, AND); +} + +Matrix orColSequenceOnMatrix(Matrix m){ + return colSequenceOnMatrix(m, OR); +} + +Matrix andRowSequenceOnMatrix(Matrix m){ + return rowSequenceOnMatrix(m, AND); +} + +Matrix orRowSequenceOnMatrix(Matrix m){ + return rowSequenceOnMatrix(m, OR); +} + /* todos : *finir le freeMatrix *chasser les bugs diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index cabe29c..289fa70 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -120,7 +120,7 @@ Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows); /** *Basically print the Matrix in the standard output * -*@param matrix the Matrix +*@param matrix the Matrix * *@return void * @@ -131,6 +131,61 @@ bool RecursiveFreeCol(Matrix matrix, cellElement * elem); Matrix freeMatrix(Matrix matrix); +/** +* Given an input matrix with M rows and N columns, computes a matrix with +* M rows and N-1 columns were each element corresponds to the operator boolean operation +* between two successive horizontal elements in the input matrix +* @param m a Matrix +* @param operator a function +* @return matrix the copmuted matrix +*/ +Matrix colSequenceOnMatrix(Matrix m, bool (operator)(bool, bool)); + +/** +* Given an input matrix with M rows and N columns, computes a matrix with +* M-1 rows and N columns were each element corresponds to the operator boolean operation +* between two succesive vertical elements in the input matrix +* @param m a Matrix +* @param operator a function +* @return matrix the copmuted matrix +*/ +Matrix rowSequenceOnMatrix(Matrix m, bool (operator)(bool, bool)); + +/** +* Given an input matrix with M rows and N columns, computes a matrix with +* M rows and N-1 columns were each element corresponds to the AND boolean operation +* between two successive horizontal elements in the input matrix +* @param m a Matrix +* @return matrix the copmuted matrix +*/ +Matrix andColSequenceOnMatrix(Matrix m); + +/** +* Given an input matrix with M rows and N columns, computes a matrix with +* M rows and N-1 columns were each element corresponds to the OR boolean operation +* between two successive horizontal elements in the input matrix +* @param m a Matrix +* @return matrix the copmuted matrix +*/ +Matrix orColSequenceOnMatrix(Matrix m); + +/** +* Given an input matrix with M rows and N columns, computes a matrix with +* M-1 rows and N columns were each element corresponds to the AND boolean operation +* between two succesive vertical elements in the input matrix +* @param m a Matrix +* @return matrix the copmuted matrix +*/ +Matrix andRowSequenceOnMatrix(Matrix m); + +/** +* Given an input matrix with M rows and N columns, computes a matrix with +* M-1 rows and N columns were each element corresponds to the OR boolean operation +* between two succesive vertical elements in the input matrix +* @param m a Matrix +* @return matrix the copmuted matrix +*/ +Matrix orRowSequenceOnMatrix(Matrix m); #endif diff --git a/main.c b/main.c index d5d983f..32c6445 100644 --- a/main.c +++ b/main.c @@ -12,6 +12,7 @@ int main(int argc, char **argv){ Matrix matrix = CreateMatrix(); + Matrix m2; int Rule = 256; int N = 1; applyRules(matrix,Rule,N); @@ -26,7 +27,14 @@ int main(int argc, char **argv){ SetCellValue(matrix,0,0,false); SetCellValue(matrix,0,1,false); - BasicPrintMatrix(matrix); + BasicPrintMatrix(matrix); + + m2 = colSequenceOnMatrix(matrix, OR); + BasicPrintMatrix(m2); + freeMatrix(m2); + m2 = rowSequenceOnMatrix(matrix, AND); + BasicPrintMatrix(m2); + freeMatrix(m2); freeMatrix(matrix); return 0;