2016-12-13 14:55:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-12-13 14:12:55 +00:00
|
|
|
#include <matrix.h>
|
2016-12-13 14:55:42 +00:00
|
|
|
|
|
|
|
Matrix applyRules (Matrix matrix,int Rules, int N){
|
2016-12-13 14:12:55 +00:00
|
|
|
int power = 2;
|
2016-12-13 14:55:42 +00:00
|
|
|
int i = 0;
|
|
|
|
if (Rules <= 0){
|
|
|
|
return matrix;
|
|
|
|
} else {
|
2016-12-13 14:12:55 +00:00
|
|
|
while (Rules%power == 0 ){
|
|
|
|
power*=2;
|
2016-12-13 14:55:42 +00:00
|
|
|
}
|
|
|
|
for (i=0;i<N;i++){
|
2016-12-13 14:12:55 +00:00
|
|
|
printf("Apply rule %d \n",Rules%power);
|
2016-12-13 14:55:42 +00:00
|
|
|
/*Replace it by the implementation of the rules*/
|
2016-12-13 14:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applyRules(matrix,Rules - Rules%power,N);
|
2016-12-13 14:55:42 +00:00
|
|
|
}
|
|
|
|
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-23 22:58:08 +00:00
|
|
|
|
2016-12-24 14:18:19 +00:00
|
|
|
Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows){
|
2016-12-24 12:53:04 +00:00
|
|
|
matrix.colCount = nbCols;
|
|
|
|
matrix.rowCount = nbRows;
|
2016-12-24 14:18:19 +00:00
|
|
|
return matrix;
|
2016-12-24 12:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
|
2016-12-23 22:58:08 +00:00
|
|
|
ListElement * Row = NULL;
|
|
|
|
ListElement * Col = NULL;
|
|
|
|
int error = 0;
|
|
|
|
cellElement * elem = NULL;
|
|
|
|
cellElement * tmp = NULL;
|
|
|
|
|
2016-12-24 14:18:19 +00:00
|
|
|
if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos ){
|
2016-12-24 12:53:04 +00:00
|
|
|
return ERROR;
|
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-12-24 14:18:19 +00:00
|
|
|
if (tmp->nextCol == NULL || tmp->nextCol->colIndex > ColPos){
|
2016-12-23 22:58:08 +00:00
|
|
|
elem->nextCol = tmp->nextCol;
|
|
|
|
tmp->nextCol = elem;
|
2016-12-24 14:18:19 +00:00
|
|
|
}else {
|
2016-12-23 22:58:08 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-12-24 14:18:19 +00:00
|
|
|
if (tmp->nextRow == NULL || tmp->nextRow->rowIndex > RowPos){
|
2016-12-23 22:58:08 +00:00
|
|
|
elem->nextRow = tmp->nextRow;
|
|
|
|
tmp->nextRow = elem;
|
2016-12-24 14:18:19 +00:00
|
|
|
}else {
|
2016-12-23 22:58:08 +00:00
|
|
|
error ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}else {
|
|
|
|
push(matrix.cols,elem);
|
|
|
|
matrix.cols->tail->pos = ColPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (error != 0){
|
2016-12-24 15:07:50 +00:00
|
|
|
FreeCellElement(elem);
|
2016-12-24 12:53:04 +00:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
2016-12-23 22:58:08 +00:00
|
|
|
}
|
2016-12-24 12:53:04 +00:00
|
|
|
|
2016-12-23 22:58:08 +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;
|
|
|
|
|
2016-12-24 14:18:19 +00:00
|
|
|
while (elem != NULL && elem->colIndex != ColPos){
|
2016-12-23 22:58:08 +00:00
|
|
|
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);
|
2016-12-23 22:58:08 +00:00
|
|
|
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 */
|
2016-12-23 22:58:08 +00:00
|
|
|
} else {
|
|
|
|
tmp->nextCol = elem->nextCol;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-12-24 12:39:14 +00:00
|
|
|
if (Row->data == NULL){
|
|
|
|
RemoveElementPos(matrix.rows,RowPos);
|
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
Col = GetElementPos(matrix.cols,ColPos);
|
2016-12-24 12:39:14 +00:00
|
|
|
if (Col == NULL){
|
2016-12-23 22:58:08 +00:00
|
|
|
return -2;
|
|
|
|
}
|
2016-12-24 12:39:14 +00:00
|
|
|
if (Col->data == NULL){
|
|
|
|
RemoveElementPos(matrix.cols,ColPos);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
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 */
|
2016-12-23 22:58:08 +00:00
|
|
|
} else {
|
|
|
|
tmp->nextRow = elem->nextRow;
|
2016-12-24 12:39:14 +00:00
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
}
|
2016-12-24 12:39:14 +00:00
|
|
|
if (Col->data == NULL){
|
|
|
|
RemoveElementPos(matrix.cols,ColPos);
|
|
|
|
}
|
|
|
|
|
2016-12-23 22:58:08 +00:00
|
|
|
FreeCellElement(elem);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetCellValue(Matrix matrix, int ColPos, int RowPos){
|
2016-12-24 14:18:19 +00:00
|
|
|
if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos){
|
2016-12-24 12:53:04 +00:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-23 22:58:08 +00:00
|
|
|
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){
|
2016-12-23 22:58:08 +00:00
|
|
|
if (value == true){
|
2016-12-24 12:53:04 +00:00
|
|
|
return CreateMatrixElem(matrix,ColPos,RowPos);
|
2016-12-23 22:58:08 +00:00
|
|
|
}else{
|
2016-12-24 12:53:04 +00:00
|
|
|
if ( SupprMatrixElem(matrix,ColPos,RowPos) >= 0 ){
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-24 14:18:19 +00:00
|
|
|
|
|
|
|
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-24 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-24 15:07:50 +00:00
|
|
|
Matrix freeMatrix(Matrix matrix){
|
2016-12-25 23:06:54 +00:00
|
|
|
ListElement * current = NULL;
|
|
|
|
|
2016-12-24 15:07:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-12-24 15:07:50 +00:00
|
|
|
FreeList(matrix.cols);
|
|
|
|
FreeList(matrix.rows);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
|
|
|
|
|
2016-12-24 15:07:50 +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
|
|
|
*/
|