1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-16 15:43:20 +00:00
LO27/LibMatrix/matrix.h

499 lines
11 KiB
C
Raw Normal View History

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.
*
*/
#ifndef MTRXGAUD_H
#define MTRXGAUD_H
#include <CellElement.h>
#include <list.h>
2016-12-31 19:19:49 +00:00
/**
* Abstract type that describe a boolean matrix
*
2016-12-31 19:19:49 +00:00
* @param colCount is the number of columns of the matrix
* @param rowIndex is the number of rows of the matrix
*
2016-12-31 19:19:49 +00:00
* @param rows is pointer on the first row that contains a true value
* @param cols is pointer on the first col that contains a true value
*
2017-01-01 15:43:40 +00:00
* @see createMatrix to allocate one
2016-12-31 19:19:49 +00:00
* @see freeMatrix to free one
*/
typedef struct Matrix {
int colCount;
int rowCount;
List *cols;
List *rows;
}Matrix;
/**
* Abstract type for a matrix of booleans
2016-12-31 19:19:49 +00:00
*
* @param rows number of rows of the matrix
* @param cols numbers of columns of the matrix
* @param data the matrix
2016-12-31 19:19:49 +00:00
*
2017-01-01 15:43:40 +00:00
* @see createBooleanMatrix to create one
*/
typedef struct {
int rows;
int cols;
bool **data;
} BooleanMatrix;
/**
* Function creating an empty boolean matrix
2016-12-31 19:19:49 +00:00
*
* @param cols number of cols
* @param rows number of rows
2016-12-31 19:19:49 +00:00
*
* @return booleanmatrix a new matrix
*/
2017-01-01 15:43:40 +00:00
BooleanMatrix createBooleanMatrix(int cols, int rows);
/**
* Randomize a BooleanMatrix
2016-12-31 19:19:49 +00:00
*
* @param matrix a BooleanMatrix
2016-12-31 19:19:49 +00:00
*
* @return booleanmatrix the processed matrix
*/
2017-01-01 15:43:40 +00:00
BooleanMatrix randomizeBooleanMatrix(BooleanMatrix matrix);
/**
* Free a BooleanMatrix
2016-12-31 19:19:49 +00:00
*
* @param matrix a BooleanMatrix
2016-12-31 19:19:49 +00:00
*
* @return void
*/
2017-01-01 15:43:40 +00:00
void freeBooleanMatrix(BooleanMatrix matrix);
/**
* Create a Matrix from its array-based representation
2016-12-31 19:19:49 +00:00
*
* @param bmatrix BooleanMatrix
2016-12-31 19:19:49 +00:00
*
* @return m Matrix
*/
Matrix newMatrix(BooleanMatrix bmatrix);
/*---applyRules---
2016-12-31 19:19:49 +00:00
* A function that allows you to apply some rules n times on the matrix and returns it
*
2016-12-31 19:19:49 +00:00
* @param matrix is a matrix on whitch you would apply the rules
* @param Rules is an integer describing the rules
* @param N is the number of time the rules will be applied
*
2016-12-31 19:19:49 +00:00
* @return Matrix
*/
2016-12-13 16:28:13 +00:00
Matrix applyRules(Matrix matrix,int Rules, int N);
/**
2016-12-31 19:19:49 +00:00
* Create a void Matrix
*
2016-12-31 19:19:49 +00:00
* @return a matrix
*
*/
2017-01-01 15:43:40 +00:00
Matrix createMatrix();
/**
2016-12-31 19:19:49 +00:00
* Find and return the cell in the given matrix
*
2016-12-31 19:19:49 +00:00
* @param matrix the Matrix where we search
* @param ColPos an int indicating the column of the cell
* @param RowPos an int indicating the row of the cell
*
2016-12-31 19:19:49 +00:00
* @return a cellElement
*
*/
2017-01-01 15:43:40 +00:00
cellElement * findMatrixElem(Matrix matrix, int ColPos, int RowPos);
/**
2016-12-31 19:19:49 +00:00
* Create the cell in the given matrix
*
2016-12-31 19:19:49 +00:00
* @param matrix the Matrix
* @param ColPos an int indicating the column of the cell
* @param RowPos an int indicating the row of the cell
*
2016-12-31 19:19:49 +00:00
* @return a bool (error code)
*
*/
2017-01-01 15:43:40 +00:00
bool createMatrixElem(Matrix matrix, int ColPos, int RowPos);
2016-12-24 15:41:06 +00:00
/**
2016-12-31 19:19:49 +00:00
* Delete the cell in the given matrix
2016-12-24 15:41:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @param matrix the Matrix
* @param ColPos an int indicating the column of the cell
* @param RowPos an int indicating the row of the cell
2016-12-24 15:41:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @return an error code (int)
2016-12-24 15:41:06 +00:00
*
*/
2017-01-01 15:43:40 +00:00
int deleteMatrixElem(Matrix matrix, int ColPos, int RowPos);
2016-12-24 15:41:06 +00:00
/**
2016-12-31 19:19:49 +00:00
* Delete or create the cell in the given matrix to fit the value
2016-12-24 15:41:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @param matrix the Matrix
* @param ColPos an int indicating the column of the cell
* @param RowPos an int indicating the row of the cell
2016-12-24 15:41:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @return an error code (bool)
2016-12-24 15:41:06 +00:00
*
*/
2017-01-01 15:43:40 +00:00
bool setCellValue(Matrix matrix, int ColPos, int RowPos, bool value);
2016-12-24 15:41:06 +00:00
/**
2016-12-31 19:19:49 +00:00
* Checks out the value of the cell in the given matrix
2016-12-24 15:41:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @param matrix the Matrix
* @param ColPos an int indicating the column of the cell
* @param RowPos an int indicating the row of the cell
2016-12-24 15:41:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @return the value (bool)
2016-12-24 15:41:06 +00:00
*
*/
2017-01-01 15:43:40 +00:00
bool getCellValue(Matrix matrix, int ColPos, int RowPos);
2016-12-24 12:53:04 +00:00
2016-12-25 23:13:06 +00:00
/**
2016-12-31 19:19:49 +00:00
* Set the number of columns and rows of the matrix and returns it
2016-12-25 23:13:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @param matrix the Matrix
* @param nbCols an int indicating the number of columns
* @param nbRows an int indicating the number of rows
2016-12-25 23:13:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @return the matrix
2016-12-25 23:13:06 +00:00
*
*/
2017-01-01 15:43:40 +00:00
Matrix setMatrixDim(Matrix matrix,int nbCols,int nbRows);
2016-12-25 23:13:06 +00:00
/**
2016-12-31 19:19:49 +00:00
* Basically print the Matrix in the standard output
2016-12-25 23:13:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @param matrix the Matrix
2016-12-25 23:13:06 +00:00
*
2016-12-31 19:19:49 +00:00
* @return void
2016-12-25 23:13:06 +00:00
*
*/
void printMatrix(Matrix matrix);
2016-12-24 12:53:04 +00:00
2016-12-31 19:19:49 +00:00
/**
* Allows you to free a Matrix
*
* @param matrix is the Matrix you want to free
*
* @return Matrix non allocated
*/
Matrix freeMatrix(Matrix matrix);
2016-12-26 19:44:39 +00:00
/**
* Given an input matrix with M rows and N columns, computes a matrix with
* M rows and N-1 columns were each element corresponds to the operator boolean operation
* between two successive horizontal elements in the input matrix
2016-12-31 19:19:49 +00:00
*
2016-12-26 19:44:39 +00:00
* @param m a Matrix
* @param operator a function
2016-12-31 19:19:49 +00:00
*
2016-12-28 16:22:02 +00:00
* @return Matrix the copmuted matrix
2016-12-26 19:44:39 +00:00
*/
Matrix colSequenceOnMatrix(Matrix m, bool (operator)(bool, bool));
/**
* Given an input matrix with M rows and N columns, computes a matrix with
* M-1 rows and N columns were each element corresponds to the operator boolean operation
* between two succesive vertical elements in the input matrix
2016-12-31 19:19:49 +00:00
*
2016-12-26 19:44:39 +00:00
* @param m a Matrix
* @param operator a function
2016-12-31 19:19:49 +00:00
*
2016-12-28 16:22:02 +00:00
* @return Matrix the copmuted matrix
2016-12-26 19:44:39 +00:00
*/
Matrix rowSequenceOnMatrix(Matrix m, bool (operator)(bool, bool));
/**
* Given an input matrix with M rows and N columns, computes a matrix with
* M rows and N-1 columns were each element corresponds to the AND boolean operation
* between two successive horizontal elements in the input matrix
2016-12-31 19:19:49 +00:00
*
2016-12-26 19:44:39 +00:00
* @param m a Matrix
2016-12-31 19:19:49 +00:00
*
2016-12-28 16:22:02 +00:00
* @return Matrix the copmuted matrix
2016-12-26 19:44:39 +00:00
*/
Matrix andColSequenceOnMatrix(Matrix m);
/**
* Given an input matrix with M rows and N columns, computes a matrix with
* M rows and N-1 columns were each element corresponds to the OR boolean operation
* between two successive horizontal elements in the input matrix
2016-12-31 19:19:49 +00:00
*
2016-12-26 19:44:39 +00:00
* @param m a Matrix
2016-12-31 19:19:49 +00:00
*
2016-12-28 16:22:02 +00:00
* @return Matrix the copmuted matrix
2016-12-26 19:44:39 +00:00
*/
Matrix orColSequenceOnMatrix(Matrix m);
/**
* Given an input matrix with M rows and N columns, computes a matrix with
* M-1 rows and N columns were each element corresponds to the AND boolean operation
* between two succesive vertical elements in the input matrix
2016-12-31 19:19:49 +00:00
*
2016-12-26 19:44:39 +00:00
* @param m a Matrix
2016-12-31 19:19:49 +00:00
*
2016-12-28 16:22:02 +00:00
* @return Matrix the copmuted matrix
2016-12-26 19:44:39 +00:00
*/
Matrix andRowSequenceOnMatrix(Matrix m);
/**
* Given an input matrix with M rows and N columns, computes a matrix with
* M-1 rows and N columns were each element corresponds to the OR boolean operation
* between two succesive vertical elements in the input matrix
2016-12-31 19:19:49 +00:00
*
2016-12-26 19:44:39 +00:00
* @param m a Matrix
2016-12-31 19:19:49 +00:00
*
2016-12-28 16:22:02 +00:00
* @return Matrix the copmuted matrix
2016-12-26 19:44:39 +00:00
*/
Matrix orRowSequenceOnMatrix(Matrix m);
2016-12-28 01:38:48 +00:00
/**
2016-12-28 15:28:07 +00:00
* Apply a given set of rules
2016-12-31 19:19:49 +00:00
*
2016-12-28 01:38:48 +00:00
* @param m a matrix
2016-12-28 15:28:07 +00:00
* @param n the number of rules
* @param rules an array of rules
2016-12-31 19:19:49 +00:00
*
2016-12-28 16:22:02 +00:00
* @return Matrix a new modified matrix
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
/**
2016-12-28 15:28:07 +00:00
* Apply XOR to a given number of bools
2016-12-31 19:19:49 +00:00
*
* @param n the number of bools
2016-12-28 15:28:07 +00:00
* @param bools an array of bool
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the processed bool
*/
bool MXOR(int n, bool bools[]);
/**
* Get a cell with first rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
*/
bool firstRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with right rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
*/
bool rightRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with left rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
*/
bool leftRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with top rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
*/
bool topRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with bottom rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
*/
bool bottomRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with top_left rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 01:38:48 +00:00
* @param m a matrix
2016-12-28 15:28:07 +00:00
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
2016-12-28 01:38:48 +00:00
*/
2017-01-01 15:43:40 +00:00
bool topLeftRule(Matrix m, int ColPos, int RowPos);
2016-12-28 01:38:48 +00:00
/**
2016-12-28 15:28:07 +00:00
* Get a cell with top_right rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 01:38:48 +00:00
* @param m a matrix
2016-12-28 15:28:07 +00:00
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
2016-12-28 01:38:48 +00:00
*/
2017-01-01 15:43:40 +00:00
bool topRightRule(Matrix m, int ColPos, int 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
* Get a cell with bottom_left rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 01:38:48 +00:00
* @param m a matrix
2016-12-28 15:28:07 +00:00
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
2016-12-28 01:38:48 +00:00
*/
2017-01-01 15:43:40 +00:00
bool bottomLeftRule(Matrix m, int ColPos, int RowPos);
2016-12-28 01:38:48 +00:00
/**
2016-12-28 15:28:07 +00:00
* Get a cell with bottom_right rule
2016-12-31 19:19:49 +00:00
*
2016-12-28 01:38:48 +00:00
* @param m a matrix
2016-12-28 15:28:07 +00:00
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool the value of the cell got by the rule
*/
2017-01-01 15:43:40 +00:00
bool bottomRightRule(Matrix m, int ColPos, int RowPos);
2016-12-28 15:28:07 +00:00
/**
* Get a list of bool from a given set of rules
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @param m a Matrix
* @param ColPos colon position of the point
* @param RowPos colon position of the point
* @param n number of rules
* @param rules[] an array of rules
2016-12-31 19:19:49 +00:00
*
2016-12-28 15:28:07 +00:00
* @return bool[] an array of bool
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 01:38:48 +00:00
2016-12-31 19:19:49 +00:00
/**
* Allows you to use boolean operators between two matrices
2016-12-29 14:25:02 +00:00
*
2016-12-31 19:19:49 +00:00
* @param matrix1 the first matrix you want to operate on
* @param matrix2 the second matrix you want to operate on
* @param operator the function operator you want to use
2016-12-29 14:25:02 +00:00
*
2016-12-31 19:19:49 +00:00
* @return Matrix
*/
Matrix opMatrix(Matrix matrix1,Matrix matrix2,bool (operator)(bool, bool));
/**
* Allows you to add two Matrices
2016-12-29 14:25:02 +00:00
*
2016-12-31 19:19:49 +00:00
* @param matrix1 the first matrix you want to add
* @param matrix2 the second matrix you want to add
2016-12-29 14:25:02 +00:00
*
2016-12-31 19:19:49 +00:00
* @return Matrix the sum
2016-12-29 14:25:02 +00:00
*/
2016-12-26 19:13:35 +00:00
Matrix sumMatrix(Matrix matrix1,Matrix matrix2);
2016-12-31 19:19:49 +00:00
/**
* Allows you to multiply two Matrices (with the AND operator)
*
* @param matrix1 the first matrix you want to multiply
* @param matrix2 the second matrix you want to multiply
*
* @return Matrix the product
*/
2016-12-27 22:11:15 +00:00
Matrix mulMatrix(Matrix matrix1,Matrix matrix2);
2016-12-31 19:19:49 +00:00
/**
* Check out if the matrix is Empty
*
* @param matrix the matrix you want to check
*
* @return bool (true/false)
*/
2016-12-29 14:25:02 +00:00
bool isMatrixEmpty(Matrix matrix);
2016-12-31 19:19:49 +00:00
/**
* Check out if the matrix is square
*
* @param matrix the matrix you want to check
*
* @return bool (true/false)
*/
2016-12-29 14:25:02 +00:00
bool isMatrixSquare(Matrix matrix);
2016-12-31 19:19:49 +00:00
/**
* Check out if the Matrices are equals
*
* @param m1 Matrices
* @param m2 Matrices
*
* @return bool (true/false)
*/
2016-12-29 14:25:02 +00:00
bool equalsMatrix(Matrix m1, Matrix m2);
2016-12-25 23:06:54 +00:00
2016-12-31 19:19:49 +00:00
/**
* Check out if the column is empty
*
* @param matrix the Matrix where the column is in
* @param the number of the column
*
* @return bool (true/false)
*/
bool isColumnEmpty(Matrix matrix,int nb);
/**
* Check out if the row is empty
*
* @param matrix the Matrix where the row is in
* @param the number of the row
*
* @return bool (true/false)
*/
bool isRowEmpty(Matrix matrix,int nb);
#endif
2016-12-13 14:12:55 +00:00