1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2025-07-14 09:29:22 +00:00

Changement d'architecture de projet

This commit is contained in:
2016-12-28 18:49:26 +01:00
parent b2c6602cbc
commit 547bafb92f
11 changed files with 17 additions and 20 deletions

170
LibMatrix/CellElement.c Normal file
View File

@ -0,0 +1,170 @@
/***
--- CellElemFunc ---
---Created by : Naej Doree ---
***/
#include <stdio.h>
#include <stdlib.h>
#include <CellElement.h>
cellElement * CreateCellElem(){
cellElement * elem = NULL;
elem = (cellElement*) malloc(sizeof(cellElement));
if (elem == NULL){
return NULL;
}
printf("---Created cellElement---\n");
elem->nextCol = NULL;
elem->nextRow = NULL;
return elem;
}
bool is_leaf(cellElement* tree){
if (tree->nextCol == NULL && tree->nextRow == NULL){
return true;
} else {
return false;
}
}
int SetPositionIndex(cellElement* elem,int Col,int Row){
if (elem != NULL){
elem->colIndex = Col;
elem->rowIndex = Row;
} else {
return -1;
}
return 1;
}
int SetNextCol(cellElement* tree,cellElement* elem){
if (tree->nextCol == NULL){
tree->nextCol = elem;
}else{
return -2;
}
return 1;
}
int SetNextRow(cellElement* tree,cellElement* elem){
if (tree->nextRow == NULL){
tree->nextRow = elem;
}else{
return -2;
}
return 1;
}
int AddNextCol(cellElement* tree){
cellElement * elem = NULL;
printf("---insertNextCol---\n");
elem = CreateCellElem();
if (elem == NULL){
return -1;
}
return(SetNextCol(tree,elem));
}
int AddNextRow(cellElement* tree){
cellElement * elem = NULL;
printf("---insertNextRow---\n");
elem = CreateCellElem();
if (elem == NULL){
return -1;
}
return(SetNextRow(tree,elem));
}
void removeNextCol(cellElement* tree){
printf("---removeNextCol---\n");
if (tree->nextCol != NULL){
free(tree->nextCol);
tree->nextCol = NULL;
}
}
void removeNextRow(cellElement* tree){
printf("---removeNextRow---\n");
if (tree->nextRow != NULL){
free(tree->nextRow);
tree->nextRow = NULL;
}
}
void recursivePrint(cellElement * tree){
if (tree != NULL){
printf("Elem : x: %d y: %d \n",tree->colIndex,tree->rowIndex);
if (is_leaf(tree)){
printf("leaf\n");
} else {
if (tree->nextCol != NULL){
recursivePrint(tree->nextCol);
}
if (tree->nextRow != NULL){
recursivePrint(tree->nextRow);
}
}
}
}
void FreeCellElement(cellElement* element) {
printf("---FreeCellElement---\n");
if (element != NULL){
free(element);
}else{
printf("Cant free NULL");
}
element = NULL;
}
bool OR(bool a, bool b){
if (a || b){
return true;
} else {
return false;
}
}
bool AND(bool a, bool b){
if (a && b){
return true;
} else {
return false;
}
}
bool XOR(bool a, bool b){
if (!(a && b) && (a||b)){
return true;
} else {
return false;
}
}
bool ErrorToFalse(bool x){
if (x == ERROR){
return false;
}
return x;
}

179
LibMatrix/CellElement.h Normal file
View File

@ -0,0 +1,179 @@
#ifndef CELLELMNT_H
#define CELLELMNT_H
/*---bool---
*@true : 1
*@false : 0
*/
typedef enum Bool{
true = 1,
false = 0,
ERROR = -1
} bool;
/*---cellElement---
*A cell of the matrix
*
*@colIndex : index (int) of the column of this cell
*@rowIndex : index (int) of the row of this cell
*
*@value : a boolean that is the content of the cell
*
*@nextCol : pointer on the next cellElement in the same column
*@nextRow : pointer on the next cellElement in the same row
*
*/
struct cellElement {
int colIndex;
int rowIndex;
struct cellElement * nextCol;
struct cellElement * nextRow;
};
typedef struct cellElement cellElement;
/*---CreateCellElem---
*Allocates a cellElement and returns it
*
*@return : pointer on the allocated cellElement
*
*/
cellElement * CreateCellElem();
/*---AddNextCol---
*Allocates a cellElement and sets it as the NextCol of the tree
*
*@tree : pointer on an allocated cellElement
*
*@return ; error codes
*
*/
int AddNextCol(cellElement* tree);
/*---AddNextRow---
*Allocates a cellElement and sets it as the NextRow of the tree
*
*@tree : a pointer on an allocated cellElement
*
*@return : error codes
*
*/
int AddNextRow(cellElement* tree);
/*---removeNextCol---
*Free the nextCol cellElement of the tree
*
*@tree : a pointer on an allocated cellElement
*
*/
void removeNextCol(cellElement* tree);
/*---removeNextRow---
*Free the nextRow cellElement of the tree
*
*@tree : a pointer on an allocated cellElement
*
*/
void removeNextRow(cellElement* tree);
/*---FreeCellElem---
*Frees a cellElement and returns it
*
*@element : pointer on the allocated cellElement
*
*/
void FreeCellElement(cellElement* element);
/*---is_leaf---
*Checks is the tree is a leaf
*
*@tree : a pointer on an allocated cellElement
*
*@return : a bool
*
*/
bool is_leaf(cellElement* tree);
/*---SetPositionIndex---
*Allows you to set the colIndex and the rowIndex of the cellElement elem
*
*@elem : a pointer on an allocated cellElement
*@Col : the value for colIndex
*@Row : the value for rowIndex
*
*@return : error codes
*
*/
int SetPositionIndex(cellElement* elem,int Col,int Row);
/*---SetNextCol---
*Allows you to set the nextCol of the cellElement tree
*
*@tree : a pointer on the element you want to set
*@elem : a pointer on a cellElement
*
*@return : error codes
*
*/
int SetNextCol(cellElement* tree,cellElement* elem);
/*---SetNextRow---
*Allows you to set the nextRow of the cellElement elem
*
*@tree : a pointer on the element you want to set
*@elem : a pointer on a cellElement
*
*@return : error codes
*
*/
int SetNextRow(cellElement* tree,cellElement* elem);
void recursivePrint(cellElement * tree);
/**
* Apply OR operator to a bool a and a bool b
* @param a a bool
* @param b a bool
* @return result a or b
*/
bool OR(bool a, bool b);
/**
* Apply AND operator to a bool a and a bool b
* @param a a bool
* @param b a bool
* @return result a and b
*/
bool AND(bool a, bool b);
/**
* Apply XOR operator to a bool a and a bool b
* @param a a bool
* @param b a bool
* @return result a and b
*/
bool XOR(bool a, bool b);
/**
* Return false if the bool is ERROR
* @param x a bool
* @return result a bool
*/
bool ErrorToFalse(bool x);
#endif

29
LibMatrix/Makefile Normal file
View File

@ -0,0 +1,29 @@
CC=gcc
CFLAGS=-Wall -Werror -pedantic -fpic -g -std=c89
LIBSDIR=-L/usr/lib -L../Libs
INCLUDEDIR=-I/usr/include -I.
#Library variables
LIBTARGET=libMatrix.so
LIBSOURCE=list CellElement matrix
LIBSOURCECFILE=$(LIBSOURCE:=.c)
LIBSOURCEOFILE=$(LIBSOURCE:=.o)
#Generating the library binary
$(LIBTARGET): $(LIBSOURCEOFILE)
@echo "\n Generating the library binary"
$(CC) $(CFLAGS) -shared $(LIBSOURCEOFILE) -o ../Libs/$(LIBTARGET)
#Generating object files
.c.o:
@echo "\n Generating " $@ " from " $<
$(CC) $(CFLAGS) $(INCLUDEDIR) -c -o $@ $<
#Cleaning
clean:
@echo "\n Cleaning"
rm -rf *.o *.exe *.so

346
LibMatrix/list.c Normal file
View File

@ -0,0 +1,346 @@
/*********************************************************************************
* File Name : list.c
* Created By : Bartuccio Antoine
* Creation Date : [2016-10-18 13:53]
* Last Modified : [2016-11-08 15:00]
* Description :
**********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <CellElement.h>
#include <list.h>
#define SUCCESS 0
#define FAILURE 1
List * CreateList() {
List *list = malloc(sizeof(*list));
if(list != NULL){
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
return list;
}
int unshift(List *list, cellElement *data){
int ok = SUCCESS;
/* Create a new element */
ListElement *newElement = malloc(sizeof(*newElement));
if (list != NULL && newElement != NULL){
newElement->data = data;
newElement->pos = -1;
/* Insert the element at the begining of the list */
newElement->previous = NULL;
if (list->head != NULL){
list->head->previous = newElement;
} else {
list->tail = newElement;
}
newElement->next = list->head;
list->head = newElement;
list->size = list->size + 1;
} else {
if (newElement != NULL){
free(newElement);
}
ok = FAILURE;
}
return ok;
}
int push(List *list, cellElement *data){
int ok = SUCCESS;
ListElement *newElement = malloc(sizeof(*newElement));
if(list != NULL && newElement != NULL){
newElement->data = data;
newElement->pos = -1;
newElement->next = NULL;
if (list->tail == NULL){
list->tail = newElement;
list->head = newElement;
newElement->previous = NULL;
} else {
newElement->previous = list->tail;
list->tail->next = newElement;
list->tail = newElement;
}
list->size = list->size + 1;
} else {
if (newElement != NULL){
free(newElement);
}
ok = FAILURE;
}
return ok;
}
ListElement * GetElement(List *list, int nb){
ListElement *current = NULL;
int i;
if (list != NULL && (nb < list->size || -nb < list->size)){
if (nb == list->size -1 || nb == -1){
current = list->tail;
} else if (nb == 0){
current = list->head;
} else if (nb <= (list->size - 1)/2 && nb > 0){
i = 0;
current = list->head;
while(i<nb){
current = current->next;
i++;
}
} else {
i = list->size - 1;
if (nb < 0){
nb = list->size + nb -1;
}
while(i>nb){
current = current->previous;
i = i - 1;
}
}
}
return current;
}
int PopPtnList(List *list, ListElement *element){
int ok = SUCCESS;
if (list != NULL && element != NULL){
if (list->head == element && list->tail == element){
list->head = NULL;
list->tail = NULL;
} else if (list->head == element){
list->head = element->next;
element->previous = NULL;
} else if (list->tail == element){
list->tail = element->previous;
element->previous->next = NULL;
} else {
element->next->previous = element->previous;
element->previous->next = element->next;
}
if (element->data != NULL){
FreeCellElement(element->data);
}
free(element);
list->size = list->size - 1;
} else {
ok = FAILURE;
}
return ok;
}
int RemoveElement(List *list, int nb){
int ok = SUCCESS;
ListElement *toDelete = GetElement(list, nb);
if (toDelete != NULL){
ok = PopPtnList(list, toDelete);
} else {
ok = FAILURE;
}
return ok;
}
int shift(List *list){
return RemoveElement(list, 0);
}
int pop(List *list){
return RemoveElement(list, -1);
}
int DeleteListContent(List *list){
int ok = SUCCESS;
ListElement * current = NULL;
ListElement * toDelete = NULL;
if (list != NULL){
current = list->head;
while (current != NULL){
toDelete = current;
current = current->next;
/*
if (toDelete->data != NULL){
FreeCellElement(toDelete->data);
}
*/
free(toDelete);
}
list->head = NULL;
list->tail = NULL;
list->size = 0;
} else {
ok = FAILURE;
}
return ok;
}
int FreeList(List *list){
int ok = SUCCESS;
if (list != NULL){
ok = DeleteListContent(list);
if (ok == SUCCESS){
free(list);
}
} else {
ok = FAILURE;
}
return ok;
}
ListElement * GetElementPos(List *list, int pos){
ListElement * el = list->head;
while (el != NULL && el->pos != pos){
el = el->next;
}
return el;
}
int RemoveElementPos(List *list, int pos){
int ok = SUCCESS;
ListElement *toDelete = GetElementPos(list, pos);
if (toDelete != NULL){
ok = PopPtnList(list, toDelete);
} else {
ok = FAILURE;
}
return ok;
}
int InsertBeforeElement(List *list, ListElement *eli, ListElement *elp){
int ok = SUCCESS;
if (list != NULL){
if (elp->previous == NULL){
eli->next = elp;
eli->previous = NULL;
elp->previous = eli;
} else {
eli->next = elp;
eli->previous = elp->previous;
elp->previous->next = eli;
elp->previous = eli;
}
list->size = list->size + 1;
} else {
ok = FAILURE;
}
return ok;
}
int InsertAfterElement(List *list, ListElement *eli, ListElement *elb){
int ok = SUCCESS;
if (list != NULL){
if (elb->next == NULL){
eli->next = NULL;
eli->previous = elb;
elb->next = eli;
} else {
eli->previous = elb;
eli->next=elb->next;
elb->next->previous = eli;
elb->next = eli;
}
list->size = list->size + 1;
} else {
ok = FAILURE;
}
return ok;
}
int InsertBefore(List *list, cellElement *data, int nb){
int ok = SUCCESS;
ListElement *newElement = NULL;
ListElement *eli = GetElement(list, nb);
if (eli != NULL){
newElement = malloc(sizeof(*newElement));
if (newElement != NULL){
newElement->pos = -1;
newElement->data = data;
ok = InsertBeforeElement(list, newElement, eli);
} else {
ok = FAILURE;
}
} else {
ok = FAILURE;
}
return ok;
}
int InsertAfter(List *list, cellElement *data, int nb){
int ok = SUCCESS;
ListElement *newElement = NULL;
ListElement *elb = GetElement(list, nb);
if (elb != NULL){
newElement = malloc(sizeof(*newElement));
if (newElement != NULL){
newElement->pos = -1;
newElement->data = data;
ok = InsertAfterElement(list, newElement, elb);
} else {
ok = FAILURE;
}
} else {
ok = FAILURE;
}
return ok;
}
int InsertBeforePos(List *list, cellElement *data, int pos){
int ok = SUCCESS;
ListElement *newElement = NULL;
ListElement *eli = GetElementPos(list, pos);
if (eli != NULL){
newElement = malloc(sizeof(*newElement));
if (newElement != NULL){
newElement->pos = -1;
newElement->data = data;
ok = InsertBeforeElement(list, newElement, eli);
} else {
ok = FAILURE;
}
} else {
ok = FAILURE;
}
return ok;
}
int InsertAfterPos(List *list, cellElement *data, int pos){
int ok = SUCCESS;
ListElement *newElement = NULL;
ListElement *elb = GetElementPos(list, pos);
if (elb != NULL){
newElement = malloc(sizeof(*newElement));
if (newElement != NULL){
newElement->pos = -1;
newElement->data = data;
ok = InsertAfterElement(list, newElement, elb);
} else {
ok = FAILURE;
}
} else {
ok = FAILURE;
}
return ok;
}

167
LibMatrix/list.h Normal file
View File

@ -0,0 +1,167 @@
#ifndef LIST_H
#define LIST_H
#include <CellElement.h>
#define SUCCESS 0
#define FAILURE 1
typedef struct ListElement ListElement;
struct ListElement {
cellElement *data;
ListElement *next;
ListElement *previous;
int pos;
};
typedef struct List {
ListElement *head;
ListElement *tail;
int size;
} List;
/**
* Create a new list
* @return List a pointer of list
*/
List * CreateList();
/**
* Insert an element at the begining of a list
* @param list pointer of a list
* @param data any type of data
* @return int status of the operation
*/
int unshift(List* list, cellElement* data);
/**
* Insert an element at the end of a list
* @param list pointer of a list
* @param data any type of data
* @return int status of the operation
*/
int push(List* list, cellElement* data);
/**
* Get an element in a given list
* @param list as a pointer
* @param nb the number of the element (can be negative)
* @return List an element
*/
ListElement * GetElement(List *list, int nb);
/**
* Delete an element with a pointer of element in the list
* @param list as a pointer
* @param element of the list as a pointer
* @return int status of the operation
*/
int PopPtnList(List *list, ListElement *element);
/**
* Delete an element with a position in the list
* @param list as a pointer
* @param nb position of the element
* @return int status of the operation
*/
int RemoveElement(List *list, int nb);
/**
* Delete the first element of the list
* @param list as a pointer
* @return int status of the operation
*/
int shift(List *list);
/**
* Delete the last element of the list
* @param list as a pointer
* @return int status of the operation
*/
int pop(List *list);
/**
* Delete every elements in a list
* @param list as a pointer
* @return int status of the operation
*/
int DeleteListContent(List *list);
/**
* Free a list
* @param list as a pointer
* @return int status of the operation
*/
int FreeList(List *list);
/**
* Find the first element with the given pos value
* @param list as a pointer
* @param pos the pos value to find
* @return ListElement the found element can return NULL
*/
ListElement * GetElementPos(List *list, int pos);
/**
* Delete the first element of a list with the given pos
* @param list as a pointer
* @param pos pos value of the element
* @return int status of the operation
*/
int RemoveElementPos(List *list, int pos);
/**
* Insert an element in a list before the given element
* @param list as a pointer
* @param eli an element to insert
* @param elp the previous element in the list
* @return int status of the operation
*/
int InsertBeforeElement(List *list, ListElement *eli, ListElement *elp);
/**
* Insert an element in a list after the given element
* @param list as a pointer
* @param eli an element to insert
* @param elb the before element in the list
* @return int status of the operation
*/
int InsertAfterElement(List *list, ListElement *eli, ListElement *elb);
/**
* Insert an element in a list before the given position
* @param list as a pointer
* @param data cellElement to insert
* @param nb the position in list to find
* @return int status of the operation
*/
int InsertBefore(List *list, cellElement *data, int nb);
/**
* Insert an element in a list after the given position
* @param list as a pointer
* @param data cellElement to insert
* @param nb the position in list to find
* @return int status of the operation
*/
int InsertAfter(List *list, cellElement *data, int nb);
/**
* Insert an element in a list before the first element with the given pos
* @param list as a pointer
* @param data cellElement to insert
* @param pos the first pos in list to find
* @return int status of the operation
*/
int InsertBeforePos(List *list, cellElement *data, int pos);
/**
* Insert an element in a list after the first element with the given pos
* @param list as a pointer
* @param data cellElement to insert
* @param pos the first pos in list to find
* @return int status of the operation
*/
int InsertAfterPos(List *list, cellElement *data, int pos);
#endif /* LIST_H */

600
LibMatrix/matrix.c Normal file
View File

@ -0,0 +1,600 @@
#include <stdio.h>
#include <stdlib.h>
#include <matrix.h>
#include <time.h>
Matrix applyRules (Matrix matrix,int Rules, int N){
int power = 2;
int i = 0;
if (Rules <= 0){
return matrix;
} else {
while (Rules%power == 0 ){
power*=2;
}
for (i=0;i<N;i++){
printf("Apply rule %d \n",Rules%power);
/*Replace it by the implementation of the rules*/
}
applyRules(matrix,Rules - Rules%power,N);
}
return matrix;
}
Matrix CreateMatrix(){
Matrix matrix;
matrix.colCount = 0;
matrix.rowCount = 0;
matrix.cols = CreateList();
matrix.rows = CreateList();
return matrix;
}
Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows){
matrix.colCount = nbCols;
matrix.rowCount = nbRows;
return matrix;
}
bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
ListElement * Row = NULL;
ListElement * Col = NULL;
int error = 0;
cellElement * elem = NULL;
cellElement * tmp = NULL;
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 == NULL || tmp->nextCol->colIndex > ColPos){
elem->nextCol = tmp->nextCol;
tmp->nextCol = elem;
}else {
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 == NULL || tmp->nextRow->rowIndex > RowPos){
elem->nextRow = tmp->nextRow;
tmp->nextRow = elem;
}else {
error ++;
}
}
}else {
push(matrix.cols,elem);
matrix.cols->tail->pos = ColPos;
}
if (error != 0){
FreeCellElement(elem);
return true;
}else{
return false;
}
}
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 != NULL && elem->colIndex != ColPos){
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);
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; /* should never happend */
} else {
tmp->nextCol = elem->nextCol;
}
}
if (Row->data == NULL){
RemoveElementPos(matrix.rows,RowPos);
}
Col = GetElementPos(matrix.cols,ColPos);
if (Col == NULL){
return -2;
}
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; /* should never happend */
} else {
tmp->nextRow = elem->nextRow;
}
}
if (Col->data == NULL){
RemoveElementPos(matrix.cols,ColPos);
}
FreeCellElement(elem);
return 1;
}
bool GetCellValue(Matrix matrix, int ColPos, int RowPos){
if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos){
return ERROR;
}
if (FindMatrixElem(matrix,ColPos,RowPos) == NULL){
return false;
}
return true;
}
bool SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){
if (value == true){
return CreateMatrixElem(matrix,ColPos,RowPos);
}else{
if ( SupprMatrixElem(matrix,ColPos,RowPos) >= 0 ){
return true;
}else{
return false;
}
}
}
void BasicPrintMatrix(Matrix matrix){
/* Non optimisé : debug fx */
int i = 0;
int j = 0;
bool b;
printf("\n---PRINT MATRIX---\n");
for (i=0;i<matrix.rowCount;i++){
printf("| ");
for (j=0;j<matrix.colCount;j++){
b = GetCellValue(matrix,j,i);
if (b == true){
printf("1 ");
}else if (b == false){
printf("0 ");
}else{
printf("X "); /* error out of bounds, should never happend*/
}
}
printf("|\n");
}
printf("---END OF MATRIX---\n\n");
}
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 */
}
Matrix freeMatrix(Matrix matrix){
int i = 0;
int j = 0;
printf("\n---FREE MATRIX---\n");
for (i=0;i<matrix.rowCount;i++){
for (j=0;j<matrix.colCount;j++){
SetCellValue(matrix,j,i,false);
}
}
printf("---END OF MATRIX---\n\n");
FreeList(matrix.cols);
FreeList(matrix.rows);
return matrix;
}
Matrix sumMatrix(Matrix matrix1,Matrix matrix2){
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++){
SetCellValue(SumMatrix,i,j,GetCellValue(matrix1,i,j)+GetCellValue(matrix2,i,j));
}
}
}else{
printf("\n- error : Matrices haven't the same size -\n");
SumMatrix.colCount = -1;
SumMatrix.rowCount = -1;
}
return SumMatrix;
}
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);
}
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);
}
Matrix matrixFromRules(Matrix m, int n, int rules[]){
int i;
int j;
bool * bools = NULL;
Matrix result = CreateMatrix();
if (rules == NULL){
result.colCount = 0;
result.rowCount = 0;
return result;
}
result.colCount = m.colCount;
result.rowCount = m.rowCount;
for (i=0; i < m.rowCount; i++){
for (j = 0; j < m.colCount; j++){
bools = GetFromRules(m, j, i, n, rules);
if (bools != NULL){
if (MXOR(n, bools)){
SetCellValue(result, j, i, true);
}
free(bools);
}
}
}
return result;
}
bool MXOR(int n, bool bools[]){
int i;
bool r = false;
for (i=0;i<n;i++){
r = XOR(r, bools[i]);
}
return r;
}
bool firstRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos, RowPos));
}
bool leftRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos));
}
bool rightRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos));
}
bool topRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos, RowPos -1));
}
bool bottomRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos, RowPos + 1));
}
bool top_leftRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos + 1));
}
bool top_rightRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos + 1));
}
bool bottom_leftRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos + 1, RowPos - 1));
}
bool bottom_rightRule(Matrix m, int ColPos, int RowPos){
return ErrorToFalse(GetCellValue(m, ColPos - 1, RowPos - 1));
}
bool * GetFromRules(Matrix m, int ColPos, int RowPos, int n, int rules[]){
bool * bools = (bool *)malloc(sizeof(bool) * n);
int i;
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);
}
}
}
return bools;
}
/* todos :
*mulMatrix
*
*chasser les bugs
*ecrire doc
*faire un print + opti pour que sli l'adapte avec sdl
*/

347
LibMatrix/matrix.h Normal file
View File

@ -0,0 +1,347 @@
#ifndef MTRXGAUD_H
#define MTRXGAUD_H
#include <CellElement.h>
#include <list.h>
/*---Matrix---
*Abstract type that describe a boolean matrix
*
*@colCount : the number of columns of the matrix
*@rowIndex : the number of rows of the matrix
*
*@rows : pointer on the first row that contains a true value
*@cols : pointer on the first col that contains a true value
*
*/
typedef struct Matrix {
int colCount;
int rowCount;
List *cols;
List *rows;
}Matrix;
/**
* Abstract type for a matrix of booleans
* @param rows number of rows of the matrix
* @param cols numbers of columns of the matrix
* @param data the matrix
* @see CreateBooleanMatrix to create one
*/
typedef struct {
int rows;
int cols;
bool **data;
} BooleanMatrix;
/**
* Function creating an empty boolean matrix
* @param cols number of cols
* @param rows number of rows
* @return booleanmatrix a new matrix
*/
BooleanMatrix CreateBooleanMatrix(int cols, int rows);
/**
* Randomize a BooleanMatrix
* @param matrix a BooleanMatrix
* @return booleanmatrix the processed matrix
*/
BooleanMatrix RandomizeBooleanMatrix(BooleanMatrix matrix);
/**
* Free a BooleanMatrix
* @param matrix a BooleanMatrix
* @return
*/
void FreeBooleanMatrix(BooleanMatrix matrix);
/**
* Create a Matrix from its array-based representation
* @param bmatrix BooleanMatrix
* @return m Matrix
*/
Matrix newMatrix(BooleanMatrix bmatrix);
/*---applyRules---
*A function tha allows you to apply some rules n times on the matrix and returns it
*
*@matrix : A matrix on whitch you would apply the rules
*
*@Rules : Integer describing the rules
*
*@N : number of time the rules will be applied
*
*/
Matrix applyRules(Matrix matrix,int Rules, int N);
/**
*Create a void Matrix
*
*@return a matrix
*
*/
Matrix CreateMatrix();
/**
*Find and return the cell in the given matrix
*
*@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
*
*@return a cellElement
*
*/
cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos);
/**
*Create the cell in the given matrix
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return a bool (error code)
*
*/
bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos);
/**
*Delete the cell in the given matrix
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return an error code (int)
*
*/
int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos);
/**
*Delete or create the cell in the given matrix to fit the value
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return an error code (bool)
*
*/
bool SetCellValue(Matrix matrix, int ColPos, int RowPos, bool value);
/**
*Checks out the value of the cell in the given matrix
*
*@param matrix the Matrix
*@param ColPos an int indicating the column of the cell
*@param RowPos an int indicating the row of the cell
*
*@return the value (bool)
*
*/
bool GetCellValue(Matrix matrix, int ColPos, int RowPos);
/**
*Set the number of columns and rows of the matrix and returns it
*
*@param matrix the Matrix
*@param nbCols an int indicating the number of columns
*@param nbRows an int indicating the number of rows
*
*@return the matrix
*
*/
Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows);
/**
*Basically print the Matrix in the standard output
*
*@param matrix the Matrix
*
*@return void
*
*/
void BasicPrintMatrix(Matrix matrix);
bool RecursiveFreeCol(Matrix matrix, cellElement * elem);
Matrix freeMatrix(Matrix matrix);
/**
* 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
* @param m a Matrix
* @param operator a function
* @return Matrix the copmuted matrix
*/
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
* @param m a Matrix
* @param operator a function
* @return Matrix the copmuted matrix
*/
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
* @param m a Matrix
* @return Matrix the copmuted matrix
*/
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
* @param m a Matrix
* @return Matrix the copmuted matrix
*/
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
* @param m a Matrix
* @return Matrix the copmuted matrix
*/
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
* @param m a Matrix
* @return Matrix the copmuted matrix
*/
Matrix orRowSequenceOnMatrix(Matrix m);
/**
* Apply a given set of rules
* @param m a matrix
* @param n the number of rules
* @param rules an array of rules
* @return Matrix a new modified matrix
*/
Matrix matrixFromRules(Matrix m, int n, int rules[]);
/**
* Apply XOR to a given number of bools
* @param n the number of bool
* @param bools an array of bool
* @return bool the processed bool
*/
bool MXOR(int n, bool bools[]);
/**
* Get a cell with first rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @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
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @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
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @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
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @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
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @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
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool top_leftRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with top_right rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool top_rightRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with bottom_left rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool bottom_leftRule(Matrix m, int ColPos, int RowPos);
/**
* Get a cell with bottom_right rule
* @param m a matrix
* @param ColPos collumn pos of the current cell
* @param RowPos row pos of the current cell
* @return bool the value of the cell got by the rule
*/
bool bottom_rightRule(Matrix m, int ColPos, int RowPos);
/**
* Get a list of bool from a given set of rules
* @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
* @return bool[] an array of bool
*/
bool * GetFromRules(Matrix m, int ColPos, int RowPos, int n, int rules[]);
Matrix sumMatrix(Matrix matrix1,Matrix matrix2);
#endif