LO27/LibMatrix/CellElement.c

176 lines
3.0 KiB
C

/*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <CellElement.h>
cellElement * createCellElem(){
cellElement * elem = NULL;
elem = (cellElement*) malloc(sizeof(cellElement));
if (elem == NULL){
return NULL;
}
elem->nextCol = NULL;
elem->nextRow = NULL;
return elem;
}
bool isLeaf(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;
elem = createCellElem();
if (elem == NULL){
return -1;
}
return(setNextCol(tree,elem));
}
int addNextRow(cellElement* tree){
cellElement * elem = NULL;
elem = createCellElem();
if (elem == NULL){
return -1;
}
return(setNextRow(tree,elem));
}
void removeNextCol(cellElement* tree){
if (tree->nextCol != NULL){
free(tree->nextCol);
tree->nextCol = NULL;
}
}
void removeNextRow(cellElement* tree){
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 (isLeaf(tree)){
printf("leaf\n");
} else {
if (tree->nextCol != NULL){
recursivePrint(tree->nextCol);
}
if (tree->nextRow != NULL){
recursivePrint(tree->nextRow);
}
}
}
}
void freeCellElement(cellElement* element) {
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;
}