1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-06-13 19:51:54 +00:00
LO27/LibAutomaton/matrix.c

225 lines
4.3 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;
}
2016-12-24 12:53:04 +00:00
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;
2016-12-24 12:53:04 +00:00
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->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);
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->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);
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){
return -3; /* WTF ?? */
} 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){
return -4; /* WTF ?? */
} 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){
2016-12-24 12:53:04 +00:00
if (matrix.colCount < ColPos || matrix.rowCount < RowPos){
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;
}
}
}
/* todos:
*print matrice
*/