LO27/LibMatrix/CellElement.c

176 lines
3.0 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.
*
*/
2016-12-10 01:28:10 +00:00
#include <stdio.h>
#include <stdlib.h>
2016-12-10 04:04:13 +00:00
#include <CellElement.h>
2016-12-10 01:28:10 +00:00
2017-01-01 23:37:47 +00:00
cellElement * createCellElem(){
2016-12-10 20:11:46 +00:00
cellElement * elem = NULL;
elem = (cellElement*) malloc(sizeof(cellElement));
if (elem == NULL){
return NULL;
}
elem->nextCol = NULL;
elem->nextRow = NULL;
2016-12-10 01:28:10 +00:00
return elem;
2016-12-10 20:11:46 +00:00
}
2017-01-01 23:37:47 +00:00
bool isLeaf(cellElement* tree){
2016-12-11 01:32:51 +00:00
if (tree->nextCol == NULL && tree->nextRow == NULL){
return true;
} else {
return false;
}
}
2017-01-01 23:37:47 +00:00
int setPositionIndex(cellElement* elem,int Col,int Row){
2016-12-11 02:09:57 +00:00
if (elem != NULL){
elem->colIndex = Col;
elem->rowIndex = Row;
} else {
return -1;
}
return 1;
}
2017-01-01 23:37:47 +00:00
int setNextCol(cellElement* tree,cellElement* elem){
2016-12-11 02:09:57 +00:00
if (tree->nextCol == NULL){
tree->nextCol = elem;
}else{
return -2;
}
return 1;
}
2017-01-01 23:37:47 +00:00
int setNextRow(cellElement* tree,cellElement* elem){
2016-12-11 02:09:57 +00:00
if (tree->nextRow == NULL){
tree->nextRow = elem;
}else{
return -2;
}
return 1;
}
2017-01-01 23:37:47 +00:00
int addNextCol(cellElement* tree){
2016-12-10 20:11:46 +00:00
cellElement * elem = NULL;
2016-12-11 02:25:21 +00:00
2017-01-01 23:37:47 +00:00
elem = createCellElem();
2016-12-10 01:28:10 +00:00
2016-12-10 20:11:46 +00:00
if (elem == NULL){
return -1;
}
2017-01-01 23:37:47 +00:00
return(setNextCol(tree,elem));
2016-12-11 02:25:21 +00:00
2016-12-10 20:11:46 +00:00
}
2017-01-01 23:37:47 +00:00
int addNextRow(cellElement* tree){
2016-12-10 20:11:46 +00:00
cellElement * elem = NULL;
2016-12-11 02:25:21 +00:00
2017-01-01 23:37:47 +00:00
elem = createCellElem();
2016-12-10 01:28:10 +00:00
2016-12-10 20:11:46 +00:00
if (elem == NULL){
return -1;
}
2017-01-01 23:37:47 +00:00
return(setNextRow(tree,elem));
2016-12-11 02:25:21 +00:00
2016-12-10 20:11:46 +00:00
}
void removeNextCol(cellElement* tree){
if (tree->nextCol != NULL){
2016-12-11 01:32:51 +00:00
free(tree->nextCol);
2016-12-10 20:11:46 +00:00
tree->nextCol = NULL;
}
}
void removeNextRow(cellElement* tree){
if (tree->nextRow != NULL){
2016-12-11 01:32:51 +00:00
free(tree->nextRow);
2016-12-10 20:11:46 +00:00
tree->nextRow = NULL;
}
}
2016-12-11 03:01:27 +00:00
2016-12-10 20:11:46 +00:00
void recursivePrint(cellElement * tree){
if (tree != NULL){
printf("Elem : x: %d y: %d \n",tree->colIndex,tree->rowIndex);
2017-01-01 23:37:47 +00:00
if (isLeaf(tree)){
2016-12-10 20:11:46 +00:00
printf("leaf\n");
2016-12-11 01:32:51 +00:00
} else {
if (tree->nextCol != NULL){
recursivePrint(tree->nextCol);
}
if (tree->nextRow != NULL){
recursivePrint(tree->nextRow);
}
2016-12-10 20:11:46 +00:00
}
}
}
2017-01-01 23:37:47 +00:00
void freeCellElement(cellElement* element) {
2016-12-10 01:28:10 +00:00
if (element != NULL){
free(element);
}else{
printf("Cant free NULL");
2016-12-10 01:28:10 +00:00
}
2016-12-10 23:33:02 +00:00
element = NULL;
2016-12-10 04:04:13 +00:00
}
2016-12-25 23:06:54 +00:00
2016-12-26 17:16:37 +00:00
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;
}
}
2016-12-28 01:38:48 +00:00
bool XOR(bool a, bool b){
if (!(a && b) && (a||b)){
return true;
} else {
return false;
}
}
2017-01-01 23:37:47 +00:00
bool errorToFalse(bool x){
2016-12-28 01:38:48 +00:00
if (x == ERROR){
return false;
}
return x;
}