1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-19 15:43:19 +00:00
LO27/LibAutomaton/matrix.c

321 lines
6.2 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
2016-12-13 14:12:55 +00:00
#include <matrix.h>
Matrix applyRules (Matrix matrix,int Rules, int N){
2016-12-13 14:12:55 +00:00
int power = 2;
int i = 0;
if (Rules <= 0){
return matrix;
} else {
2016-12-13 14:12:55 +00:00
while (Rules%power == 0 ){
power*=2;
}
for (i=0;i<N;i++){
2016-12-13 14:12:55 +00:00
printf("Apply rule %d \n",Rules%power);
/*Replace it by the implementation of the rules*/
2016-12-13 14:12:55 +00:00
}
applyRules(matrix,Rules - Rules%power,N);
}
return matrix;
}
2016-12-13 16:28:13 +00:00
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){
2016-12-24 12:53:04 +00:00
matrix.colCount = nbCols;
matrix.rowCount = nbRows;
return matrix;
2016-12-24 12:53:04 +00:00
}
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 ){
2016-12-24 12:53:04 +00:00
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);
2016-12-24 12:53:04 +00:00
return true;
}else{
return false;
}
2016-12-24 12:53:04 +00:00
}
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);
2016-12-24 12:39:14 +00:00
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){
2016-12-25 23:06:54 +00:00
return -3; /* should never happend */
} else {
tmp->nextCol = elem->nextCol;
}
}
2016-12-24 12:39:14 +00:00
if (Row->data == NULL){
RemoveElementPos(matrix.rows,RowPos);
}
Col = GetElementPos(matrix.cols,ColPos);
2016-12-24 12:39:14 +00:00
if (Col == NULL){
return -2;
}
2016-12-24 12:39:14 +00:00
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){
2016-12-25 23:06:54 +00:00
return -4; /* should never happend */
} else {
tmp->nextRow = elem->nextRow;
2016-12-24 12:39:14 +00:00
}
}
2016-12-24 12:39:14 +00:00
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){
2016-12-24 12:53:04 +00:00
return ERROR;
}
if (FindMatrixElem(matrix,ColPos,RowPos) == NULL){
return false;
}
return true;
}
2016-12-24 12:53:04 +00:00
bool SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){
if (value == true){
2016-12-24 12:53:04 +00:00
return CreateMatrixElem(matrix,ColPos,RowPos);
}else{
2016-12-24 12:53:04 +00:00
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");
}
2016-12-25 23:06:54 +00:00
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){
2016-12-25 23:06:54 +00:00
ListElement * current = NULL;
matrix.colCount = 0;
matrix.rowCount = 0;
/*il faut free les cellElements car FreeList ne peut pas le faire*/
2016-12-25 23:06:54 +00:00
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;
}
2016-12-25 23:06:54 +00:00
/* todos :
*finir le freeMatrix
*chasser les bugs
*ecrire doc
*faire un print + opti pour que sli l'adapte avec sdl
2016-12-26 16:00:25 +00:00
*/