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-27 00:24:27 +00:00
|
|
|
#include <time.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){
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-23 22:58:08 +00:00
|
|
|
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){
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-23 22:58:08 +00:00
|
|
|
if (Col->data->rowIndex == RowPos){
|
|
|
|
error ++;
|
|
|
|
} else if (Col->data->rowIndex > RowPos){
|
|
|
|
elem->nextRow = Col->data;
|
|
|
|
Col->data = elem;
|
2016-12-27 21:34:15 +00:00
|
|
|
} else {
|
2016-12-23 22:58:08 +00:00
|
|
|
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;
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-23 22:58:08 +00:00
|
|
|
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-26 19:44:39 +00:00
|
|
|
|
2016-12-23 22:58:08 +00:00
|
|
|
}
|
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-26 19:44:39 +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-26 19:44:39 +00:00
|
|
|
|
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;
|
2016-12-26 19:44:39 +00:00
|
|
|
bool b;
|
2016-12-24 14:18:19 +00:00
|
|
|
|
|
|
|
printf("\n---PRINT MATRIX---\n");
|
|
|
|
|
2016-12-26 19:44:39 +00:00
|
|
|
for (i=0;i<matrix.rowCount;i++){
|
2016-12-24 14:18:19 +00:00
|
|
|
printf("| ");
|
2016-12-26 19:44:39 +00:00
|
|
|
for (j=0;j<matrix.colCount;j++){
|
|
|
|
|
|
|
|
b = GetCellValue(matrix,j,i);
|
|
|
|
if (b == true){
|
2016-12-24 14:18:19 +00:00
|
|
|
printf("1 ");
|
2016-12-26 19:44:39 +00:00
|
|
|
}else if (b == false){
|
2016-12-24 14:18:19 +00:00
|
|
|
printf("0 ");
|
|
|
|
}else{
|
|
|
|
printf("X "); /* error out of bounds, should never happend*/
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
printf("|\n");
|
|
|
|
|
|
|
|
}
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-24 14:18:19 +00:00
|
|
|
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;
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
if (elem == NULL){
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elem->nextRow != NULL){
|
|
|
|
RecursiveFreeCol(matrix,elem->nextRow);
|
|
|
|
}
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
if (elem->nextRow == NULL){
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
Row = GetElementPos(matrix.rows,elem->rowIndex);
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FreeCellElement(elem);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; /* should never happend */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-24 15:07:50 +00:00
|
|
|
Matrix freeMatrix(Matrix matrix){
|
2016-12-27 21:26:06 +00:00
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
printf("\n---FREE MATRIX---\n");
|
2016-12-25 23:06:54 +00:00
|
|
|
|
2016-12-27 21:26:06 +00:00
|
|
|
for (i=0;i<matrix.rowCount;i++){
|
|
|
|
for (j=0;j<matrix.colCount;j++){
|
|
|
|
|
|
|
|
SetCellValue(matrix,j,i,false);
|
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
|
|
|
|
}
|
2016-12-27 21:26:06 +00:00
|
|
|
|
2016-12-25 23:06:54 +00:00
|
|
|
}
|
2016-12-27 21:26:06 +00:00
|
|
|
|
|
|
|
printf("---END OF MATRIX---\n\n");
|
2016-12-24 15:07:50 +00:00
|
|
|
FreeList(matrix.cols);
|
|
|
|
FreeList(matrix.rows);
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2016-12-27 22:03:37 +00:00
|
|
|
Matrix opMatrix(Matrix matrix1,Matrix matrix2,bool (operator)(bool, bool)){
|
2016-12-26 19:13:35 +00:00
|
|
|
Matrix SumMatrix = CreateMatrix();
|
|
|
|
int i =0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
if (matrix1.colCount == matrix2.colCount && matrix1.rowCount == matrix2.rowCount){
|
|
|
|
SumMatrix = SetMatrixDim(SumMatrix,matrix2.colCount,matrix1.rowCount);
|
|
|
|
for (i=0;i<SumMatrix.colCount;i++){
|
|
|
|
for (j=0;j<SumMatrix.rowCount;j++){
|
2016-12-27 22:03:37 +00:00
|
|
|
SetCellValue(SumMatrix,i,j,operator(GetCellValue(matrix1,i,j),GetCellValue(matrix2,i,j)));
|
2016-12-26 19:13:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
printf("\n- error : Matrices haven't the same size -\n");
|
|
|
|
|
|
|
|
SumMatrix.colCount = -1;
|
|
|
|
SumMatrix.rowCount = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SumMatrix;
|
|
|
|
|
|
|
|
}
|
2016-12-25 23:06:54 +00:00
|
|
|
|
2016-12-27 22:03:37 +00:00
|
|
|
Matrix sumMatrix(Matrix matrix1,Matrix matrix2){
|
2016-12-27 22:11:15 +00:00
|
|
|
return opMatrix(matrix1,matrix2,OR);
|
2016-12-27 22:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Matrix mulMatrix(Matrix matrix1,Matrix matrix2){
|
2016-12-27 22:11:15 +00:00
|
|
|
return opMatrix(matrix1,matrix2,AND);
|
2016-12-27 22:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-26 19:44:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-27 00:24:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
Matrix matrixFromRules(Matrix m, int n, int rules[]){
|
2016-12-28 01:38:48 +00:00
|
|
|
int i;
|
|
|
|
int j;
|
2016-12-28 15:28:07 +00:00
|
|
|
|
|
|
|
bool * bools = NULL;
|
2016-12-28 01:38:48 +00:00
|
|
|
Matrix result = CreateMatrix();
|
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
if (rules == NULL){
|
|
|
|
result.colCount = 0;
|
|
|
|
result.rowCount = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-28 01:38:48 +00:00
|
|
|
result.colCount = m.colCount;
|
|
|
|
result.rowCount = m.rowCount;
|
|
|
|
|
|
|
|
for (i=0; i < m.rowCount; i++){
|
|
|
|
for (j = 0; j < m.colCount; j++){
|
2016-12-28 15:28:07 +00:00
|
|
|
bools = GetFromRules(m, j, i, n, rules);
|
|
|
|
if (bools != NULL){
|
|
|
|
if (MXOR(n, bools)){
|
|
|
|
SetCellValue(result, j, i, true);
|
|
|
|
}
|
|
|
|
free(bools);
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool MXOR(int n, bool bools[]){
|
2016-12-28 01:38:48 +00:00
|
|
|
int i;
|
2016-12-28 15:28:07 +00:00
|
|
|
bool r = false;
|
|
|
|
for (i=0;i<n;i++){
|
|
|
|
r = XOR(r, bools[i]);
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
2016-12-28 15:28:07 +00:00
|
|
|
return r;
|
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool firstRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos, RowPos));
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool leftRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos));
|
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool rightRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos));
|
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool topRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos, RowPos -1));
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool bottomRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos, RowPos + 1));
|
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool top_leftRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos + 1));
|
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool top_rightRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos + 1));
|
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool bottom_leftRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos - 1));
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool bottom_rightRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos - 1));
|
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool * GetFromRules(Matrix m, int ColPos, int RowPos, int n, int rules[]){
|
|
|
|
bool * bools = (bool *)malloc(sizeof(bool) * n);
|
|
|
|
int i;
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
if (bools != NULL){
|
|
|
|
for (i=0;i<n;i++){
|
|
|
|
switch (rules[i]){
|
|
|
|
case 8:
|
|
|
|
bools[i] = topRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 128:
|
|
|
|
bools[i] = bottomRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
bools[i] = leftRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
bools[i] = rightRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
bools[i] = top_leftRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
bools[i] = top_rightRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 256:
|
|
|
|
bools[i] = bottom_leftRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 34:
|
|
|
|
bools[i] = bottom_rightRule(m, ColPos, RowPos);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
bools[i] = firstRule(m, ColPos, RowPos);
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
return bools;
|
|
|
|
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-24 15:07:50 +00:00
|
|
|
/* todos :
|
2016-12-26 19:13:35 +00:00
|
|
|
*mulMatrix
|
|
|
|
*
|
2016-12-24 15:07:50 +00:00
|
|
|
*chasser les bugs
|
|
|
|
*ecrire doc
|
|
|
|
*faire un print + opti pour que sli l'adapte avec sdl
|
2016-12-26 16:00:25 +00:00
|
|
|
*/
|