1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-16 18:13:18 +00:00
LO27/LibAutomaton/matrix.c

456 lines
8.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <matrix.h>
#include <time.h>
Matrix applyRules (Matrix matrix,int Rules, int N){
int power = 2;
int i = 0;
if (Rules <= 0){
return matrix;
} else {
while (Rules%power == 0 ){
power*=2;
}
for (i=0;i<N;i++){
printf("Apply rule %d \n",Rules%power);
/*Replace it by the implementation of the rules*/
}
applyRules(matrix,Rules - Rules%power,N);
}
return matrix;
}
Matrix CreateMatrix(){
Matrix matrix;
matrix.colCount = 0;
matrix.rowCount = 0;
matrix.cols = CreateList();
matrix.rows = CreateList();
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;
bool b;
printf("\n---PRINT MATRIX---\n");
for (i=0;i<matrix.rowCount;i++){
printf("| ");
for (j=0;j<matrix.colCount;j++){
b = GetCellValue(matrix,j,i);
if (b == true){
printf("1 ");
}else if (b == 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;
}
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);
}
Matrix newMatrix(BooleanMatrix bmatrix){
Matrix m = CreateMatrix();
int i;
int j;
m.colCount = bmatrix.cols;
m.rowCount = bmatrix.rows;
for (i=0; i < m.rowCount ; i++){
for (j=0; j < m.colCount; j++){
if (bmatrix.data[i][j]){
SetCellValue(m, j, i, true);
}
}
}
return m;
}
BooleanMatrix CreateBooleanMatrix(int cols, int rows){
BooleanMatrix matrix;
int i;
matrix.rows = rows;
matrix.cols = cols;
matrix.data = (bool**)malloc(matrix.rows * sizeof(bool*));
if (matrix.data != NULL){
for (i=0; i < matrix.rows; i++){
matrix.data[i] = (bool*)malloc(matrix.cols * sizeof(bool));
}
}
return matrix;
}
BooleanMatrix RandomizeBooleanMatrix(BooleanMatrix matrix){
int i;
int j;
int r;
srand(time(NULL));
for (i=0; i < matrix.rows; i++){
for (j=0; j < matrix.cols; j++){
r = rand() % 2;
if (r == 1){
matrix.data[i][j] = true;
} else {
matrix.data[i][j] = false;
}
}
}
return matrix;
}
void FreeBooleanMatrix(BooleanMatrix matrix){
int i;
for (i=0; i < matrix.rows; i++){
free(matrix.data[i]);
}
free(matrix.data);
}
/* todos :
*finir le freeMatrix
*chasser les bugs
*ecrire doc
*faire un print + opti pour que sli l'adapte avec sdl
*/