mirror of
https://gitlab.com/klmp200/LO27.git
synced 2024-11-16 04:43:21 +00:00
176 lines
3.0 KiB
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 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;
|
|
|
|
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 (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) {
|
|
|
|
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;
|
|
}
|