2016-12-29 15:09:39 +00:00
|
|
|
/*
|
|
|
|
* This is a cellular automaton library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016-2017 Antoine BARTUCCIO, Jean POREE DE RIDDER
|
|
|
|
*
|
|
|
|
* Licensed under the MIT License,(the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* hhttps://opensource.org/licenses/MIT
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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-29 00:40:50 +00:00
|
|
|
int RulesMatrix[9];
|
2016-12-13 14:55:42 +00:00
|
|
|
int i = 0;
|
2016-12-29 00:40:50 +00:00
|
|
|
int power = 2;
|
|
|
|
int sum = 0;
|
|
|
|
int j = 0;
|
2016-12-29 02:14:07 +00:00
|
|
|
Matrix tempMatrix1;
|
|
|
|
Matrix tempMatrix2;
|
2016-12-29 00:40:50 +00:00
|
|
|
|
|
|
|
if (Rules <= 0 || N < 1){
|
2016-12-13 14:55:42 +00:00
|
|
|
return matrix;
|
2016-12-29 02:14:07 +00:00
|
|
|
}
|
2016-12-29 00:40:50 +00:00
|
|
|
|
|
|
|
while(power<=512){
|
2016-12-29 02:14:07 +00:00
|
|
|
|
2016-12-29 00:40:50 +00:00
|
|
|
RulesMatrix[i] = Rules%power - sum;
|
|
|
|
sum = Rules%power;
|
|
|
|
|
|
|
|
if (RulesMatrix[i]!=0){
|
|
|
|
i++;
|
2016-12-13 14:12:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 00:40:50 +00:00
|
|
|
power*=2;
|
2016-12-13 14:55:42 +00:00
|
|
|
}
|
2016-12-29 00:40:50 +00:00
|
|
|
|
2016-12-29 02:14:07 +00:00
|
|
|
tempMatrix1 = matrixFromRules(matrix, i, RulesMatrix);
|
2016-12-29 00:40:50 +00:00
|
|
|
|
2016-12-29 02:14:07 +00:00
|
|
|
for (j=1;j<N;j++){
|
|
|
|
tempMatrix2 = matrixFromRules(tempMatrix1,i, RulesMatrix);
|
|
|
|
freeMatrix(tempMatrix1);
|
|
|
|
|
2016-12-29 14:38:13 +00:00
|
|
|
tempMatrix1 = tempMatrix2;
|
2016-12-29 00:40:50 +00:00
|
|
|
}
|
2016-12-29 02:14:07 +00:00
|
|
|
|
|
|
|
return tempMatrix1;
|
2016-12-29 00:40:50 +00:00
|
|
|
|
2016-12-13 14:55:42 +00:00
|
|
|
}
|
2016-12-13 16:28:13 +00:00
|
|
|
|
2016-12-29 00:40:50 +00:00
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
Matrix createMatrix(){
|
2016-12-13 16:28:13 +00:00
|
|
|
Matrix matrix;
|
|
|
|
matrix.colCount = 0;
|
|
|
|
matrix.rowCount = 0;
|
2017-01-01 15:35:34 +00:00
|
|
|
matrix.cols = createList();
|
|
|
|
matrix.rows = createList();
|
2016-12-13 16:28:13 +00:00
|
|
|
return matrix;
|
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
|
2017-01-01 15:43:40 +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
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +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);
|
|
|
|
|
2017-01-01 15:35:34 +00:00
|
|
|
Row = getElementPos(matrix.rows,RowPos);
|
2016-12-23 22:58:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:35:34 +00:00
|
|
|
Col = getElementPos(matrix.cols,ColPos);
|
2016-12-23 22:58:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
cellElement * findMatrixElem(Matrix matrix, int ColPos, int RowPos){
|
2016-12-23 22:58:08 +00:00
|
|
|
ListElement * Row = NULL;
|
|
|
|
cellElement * elem = NULL;
|
|
|
|
|
2017-01-01 15:35:34 +00:00
|
|
|
Row = getElementPos(matrix.rows,RowPos);
|
2016-12-23 22:58:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
int deleteMatrixElem(Matrix matrix, int ColPos, int RowPos){
|
2016-12-23 22:58:08 +00:00
|
|
|
cellElement * elem = NULL;
|
|
|
|
cellElement * tmp = NULL;
|
|
|
|
|
|
|
|
ListElement * Row = NULL;
|
|
|
|
ListElement * Col = NULL;
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
elem = findMatrixElem(matrix,ColPos,RowPos);
|
2016-12-23 22:58:08 +00:00
|
|
|
if (elem == NULL){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:35:34 +00:00
|
|
|
Row = getElementPos(matrix.rows,RowPos);
|
2016-12-24 12:39:14 +00:00
|
|
|
if (Row == NULL){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (Row->data == NULL){
|
2017-01-01 15:35:34 +00:00
|
|
|
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){
|
2017-01-01 15:35:34 +00:00
|
|
|
removeElementPos(matrix.rows,RowPos);
|
2016-12-24 12:39:14 +00:00
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
|
|
|
|
|
2017-01-01 15:35:34 +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){
|
2017-01-01 15:35:34 +00:00
|
|
|
removeElementPos(matrix.cols,ColPos);
|
2016-12-24 12:39:14 +00:00
|
|
|
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){
|
2017-01-01 15:35:34 +00:00
|
|
|
removeElementPos(matrix.cols,ColPos);
|
2016-12-24 12:39:14 +00:00
|
|
|
}
|
2016-12-26 19:44:39 +00:00
|
|
|
|
2016-12-23 22:58:08 +00:00
|
|
|
FreeCellElement(elem);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
if (findMatrixElem(matrix,ColPos,RowPos) == NULL){
|
2016-12-23 22:58:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
bool setCellValue(Matrix matrix, int ColPos, int RowPos,bool value){
|
2016-12-23 22:58:08 +00:00
|
|
|
if (value == true){
|
2017-01-01 15:43:40 +00:00
|
|
|
return createMatrixElem(matrix,ColPos,RowPos);
|
2016-12-23 22:58:08 +00:00
|
|
|
}else{
|
2017-01-01 15:43:40 +00:00
|
|
|
if ( deleteMatrixElem(matrix,ColPos,RowPos) >= 0 ){
|
2016-12-24 12:53:04 +00:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-23 22:58:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-24 14:18:19 +00:00
|
|
|
|
2016-12-30 18:49:55 +00:00
|
|
|
void printMatrix(Matrix matrix){
|
2016-12-24 14:18:19 +00:00
|
|
|
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++){
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
b = getCellValue(matrix,j,i);
|
2016-12-26 19:44:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Matrix freeMatrix(Matrix matrix){
|
2016-12-27 21:26:06 +00:00
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
for (i=0;i<matrix.rowCount;i++){
|
|
|
|
for (j=0;j<matrix.colCount;j++){
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
setCellValue(matrix,j,i,false);
|
2016-12-27 21:26:06 +00:00
|
|
|
|
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
|
|
|
|
2017-01-01 15:35:34 +00:00
|
|
|
freeList(matrix.cols);
|
|
|
|
freeList(matrix.rows);
|
2016-12-24 15:07:50 +00:00
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
2016-12-27 22:03:37 +00:00
|
|
|
Matrix opMatrix(Matrix matrix1,Matrix matrix2,bool (operator)(bool, bool)){
|
2017-01-01 15:43:40 +00:00
|
|
|
Matrix SumMatrix = createMatrix();
|
2016-12-26 19:13:35 +00:00
|
|
|
int i =0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
if (matrix1.colCount == matrix2.colCount && matrix1.rowCount == matrix2.rowCount){
|
2017-01-01 15:43:40 +00:00
|
|
|
SumMatrix = setMatrixDim(SumMatrix,matrix2.colCount,matrix1.rowCount);
|
2016-12-26 19:13:35 +00:00
|
|
|
for (i=0;i<SumMatrix.colCount;i++){
|
|
|
|
for (j=0;j<SumMatrix.rowCount;j++){
|
2017-01-01 15:43:40 +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;
|
2017-01-01 15:43:40 +00:00
|
|
|
Matrix newM = createMatrix();
|
2016-12-26 19:44:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
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++){
|
2017-01-01 15:43:40 +00:00
|
|
|
a = getCellValue(m, i, j);
|
|
|
|
b = getCellValue(m, i + 1, j);
|
2016-12-26 19:44:39 +00:00
|
|
|
if (operator(a, b)){
|
2017-01-01 15:43:40 +00:00
|
|
|
setCellValue(newM, i, j, true);
|
2016-12-26 19:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newM;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix rowSequenceOnMatrix(Matrix m, bool (operator)(bool, bool)){
|
|
|
|
bool a;
|
|
|
|
bool b;
|
|
|
|
int i;
|
|
|
|
int j;
|
2017-01-01 15:43:40 +00:00
|
|
|
Matrix newM = createMatrix();
|
2016-12-26 19:44:39 +00:00
|
|
|
|
|
|
|
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++){
|
2017-01-01 15:43:40 +00:00
|
|
|
a = getCellValue(m, i, j);
|
|
|
|
b = getCellValue(m, i, j + 1);
|
2016-12-26 19:44:39 +00:00
|
|
|
if (operator(a, b)){
|
2017-01-01 15:43:40 +00:00
|
|
|
setCellValue(newM, i, j, true);
|
2016-12-26 19:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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){
|
2017-01-01 15:43:40 +00:00
|
|
|
Matrix m = createMatrix();
|
2016-12-27 00:24:27 +00:00
|
|
|
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]){
|
2017-01-01 15:43:40 +00:00
|
|
|
setCellValue(m, j, i, true);
|
2016-12-27 00:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
BooleanMatrix createBooleanMatrix(int cols, int rows){
|
2016-12-27 00:24:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
BooleanMatrix randomizeBooleanMatrix(BooleanMatrix matrix){
|
2016-12-27 00:24:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
void freeBooleanMatrix(BooleanMatrix matrix){
|
2016-12-27 00:24:27 +00:00
|
|
|
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;
|
2017-01-01 15:43:40 +00:00
|
|
|
Matrix result = createMatrix();
|
2016-12-28 01:38:48 +00:00
|
|
|
|
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++){
|
2017-01-01 15:43:40 +00:00
|
|
|
bools = getFromRules(m, j, i, n, rules);
|
2016-12-28 15:28:07 +00:00
|
|
|
if (bools != NULL){
|
|
|
|
if (MXOR(n, bools)){
|
2017-01-01 15:43:40 +00:00
|
|
|
setCellValue(result, j, i, true);
|
2016-12-28 15:28:07 +00:00
|
|
|
}
|
|
|
|
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){
|
2017-01-01 15:43:40 +00:00
|
|
|
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){
|
2017-01-01 15:43:40 +00:00
|
|
|
return ErrorToFalse(getCellValue(m, ColPos + 1, RowPos));
|
2016-12-28 15:28:07 +00:00
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool rightRule(Matrix m, int ColPos, int RowPos){
|
2017-01-01 15:43:40 +00:00
|
|
|
return ErrorToFalse(getCellValue(m, ColPos - 1, RowPos));
|
2016-12-28 15:28:07 +00:00
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2016-12-28 15:28:07 +00:00
|
|
|
bool topRule(Matrix m, int ColPos, int RowPos){
|
2017-01-01 15:43:40 +00:00
|
|
|
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){
|
2017-01-01 15:43:40 +00:00
|
|
|
return ErrorToFalse(getCellValue(m, ColPos, RowPos - 1));
|
2016-12-28 15:28:07 +00:00
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
bool topLeftRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(getCellValue(m, ColPos + 1, RowPos + 1));
|
2016-12-28 15:28:07 +00:00
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
bool topRightRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(getCellValue(m, ColPos - 1, RowPos + 1));
|
2016-12-28 15:28:07 +00:00
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
bool bottomLeftRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(getCellValue(m, ColPos + 1, RowPos - 1));
|
2016-12-28 01:38:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
bool bottomRightRule(Matrix m, int ColPos, int RowPos){
|
|
|
|
return ErrorToFalse(getCellValue(m, ColPos - 1, RowPos - 1));
|
2016-12-28 15:28:07 +00:00
|
|
|
}
|
2016-12-28 01:38:48 +00:00
|
|
|
|
2017-01-01 15:43:40 +00:00
|
|
|
bool * getFromRules(Matrix m, int ColPos, int RowPos, int n, int rules[]){
|
2016-12-28 15:28:07 +00:00
|
|
|
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:
|
2017-01-01 15:43:40 +00:00
|
|
|
bools[i] = topLeftRule(m, ColPos, RowPos);
|
2016-12-28 15:28:07 +00:00
|
|
|
break;
|
|
|
|
case 16:
|
2017-01-01 15:43:40 +00:00
|
|
|
bools[i] = topRightRule(m, ColPos, RowPos);
|
2016-12-28 15:28:07 +00:00
|
|
|
break;
|
|
|
|
case 256:
|
2017-01-01 15:43:40 +00:00
|
|
|
bools[i] = bottomLeftRule(m, ColPos, RowPos);
|
2016-12-28 15:28:07 +00:00
|
|
|
break;
|
|
|
|
case 34:
|
2017-01-01 15:43:40 +00:00
|
|
|
bools[i] = bottomRightRule(m, ColPos, RowPos);
|
2016-12-28 15:28:07 +00:00
|
|
|
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-29 14:25:02 +00:00
|
|
|
bool isMatrixEmpty(Matrix matrix){
|
|
|
|
if (matrix.colCount < 1 || matrix.rowCount < 1){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (matrix.cols->size == 0 || matrix.rows->size == 0){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isMatrixSquare(Matrix matrix){
|
|
|
|
if (matrix.colCount == matrix.rowCount){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isColumnEmpty(Matrix matrix,int nb){
|
|
|
|
ListElement * Col = NULL;
|
|
|
|
|
|
|
|
if (matrix.colCount < 1 || matrix.rowCount < 1){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (matrix.cols->size == 0 || matrix.rows->size == 0){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-01 15:35:34 +00:00
|
|
|
Col = getElementPos(matrix.cols,nb);
|
2016-12-29 14:25:02 +00:00
|
|
|
if (Col == NULL || Col->data == NULL){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isRowEmpty(Matrix matrix,int nb){
|
|
|
|
ListElement * Row = NULL;
|
|
|
|
|
|
|
|
if (matrix.colCount < 1 || matrix.rowCount < 1){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (matrix.cols->size == 0 || matrix.rows->size == 0){
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-01 15:35:34 +00:00
|
|
|
Row = getElementPos(matrix.rows,nb);
|
2016-12-29 14:25:02 +00:00
|
|
|
if (Row == NULL || Row->data == NULL){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equalsMatrix(Matrix m1, Matrix m2){
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
if (m1.colCount == m2.colCount && m1.rowCount == m2.rowCount){
|
|
|
|
for (i=0;i<m2.colCount;i++){
|
|
|
|
for (j=0;j<m1.rowCount;j++){
|
2017-01-01 15:43:40 +00:00
|
|
|
if (getCellValue(m1,i,j)!=getCellValue(m2,i,j)){
|
2016-12-29 14:25:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-01 23:52:39 +00:00
|
|
|
|
|
|
|
void setRowToTrue(Matrix matrix, int RowNb){
|
|
|
|
int i =0;
|
|
|
|
for (i=0;i<m2.colCount;i++){
|
|
|
|
setCellValue(matrix,i,RowNb);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void setColToTrue(Matrix matrix, int ColNb){
|
|
|
|
int i =0;
|
|
|
|
for (i=0;i<m2.rowCount;i++){
|
|
|
|
setCellValue(matrix,ColNb,i);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void createSquare(Matrix matrix){
|
|
|
|
setRowToTrue(matrix,rowCount/4);
|
|
|
|
setRowToTrue(matrix,rowCount*3/4);
|
|
|
|
setColToTrue(matrix,colCount/4);
|
|
|
|
setColToTrue(matrix,colCount*3/4);
|
|
|
|
}
|