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

New project acrh

This commit is contained in:
2016-12-11 01:53:18 +01:00
parent 53c05a0ca5
commit 24e3bb4b90
7 changed files with 8 additions and 42 deletions

116
LibAutomaton/CellElement.c Normal file
View File

@ -0,0 +1,116 @@
/***
--- 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->value = true;
elem->nextCol = NULL;
elem->nextRow = NULL;
return elem;
}
int AddNextCol(cellElement* tree){
cellElement * elem = NULL;
elem = (cellElement*) malloc(sizeof(cellElement));
if (elem == NULL){
return -1;
}
printf("---insertNextCol---\n");
elem->value = true;
elem->nextCol = NULL;
elem->nextRow = NULL;
if (tree->nextCol == NULL){
tree->nextCol = elem;
}else{
return -2;
}
return 1;
}
int AddNextRow(cellElement* tree){
cellElement * elem = NULL;
elem = (cellElement*) malloc(sizeof(cellElement));
if (elem == NULL){
return -1;
}
printf("---insertNextRow---\n");
elem->value = true;
elem->nextRow = NULL;
elem->nextRow = NULL;
if (tree->nextRow == NULL){
tree->nextRow = elem;
}else{
return -2;
}
return 1;
}
void removeNextCol(cellElement* tree){
printf("---removeNextCol---\n");
if (tree->nextCol != NULL){
cellElement * elem = tree->nextCol;
free(elem);
elem = NULL;
tree->nextCol = NULL;
}
}
void removeNextRow(cellElement* tree){
printf("---removeNextRow---\n");
if (tree->nextRow != NULL){
cellElement* elem = tree->nextRow;
free(elem);
elem =NULL;
tree->nextRow = NULL;
}
}
void recursivePrint(cellElement * tree){
if (tree != NULL){
printf("Elem : x: %d y: %d \n",tree->colIndex,tree->rowIndex);
if (tree->nextCol != NULL){
recursivePrint(tree->nextCol);
}
if (tree->nextRow != NULL){
recursivePrint(tree->nextRow);
}
if (tree->nextCol == NULL && tree->nextCol == NULL){
printf("leaf\n");
}
}
}
void FreeCellElement(cellElement* element) {
if (element != NULL){
free(element);
}
element = NULL;
}

View File

@ -0,0 +1,95 @@
#ifndef CELLELMNT_H
#define CELLELMNT_H
/*---bool---
*@true : 1
*@false : 0
*/
typedef enum Bool{
true = 1,
false = 0
} bool;
/*---cellElement---
*Pointer on 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;
bool value;
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 : an allocated cellElement
*
*/
int AddNextCol(cellElement* tree);
/*---AddNextRow---
*Allocates a cellElement and sets it as the NextRow of the tree
*
*@tree : an allocated cellElement
*
*/
int AddNextRow(cellElement* tree);
/*---removeNextCol---
*Free the nextCol cellElement of the tree
*
*@tree : an allocated cellElement
*
*/
void removeNextCol(cellElement* tree);
/*---removeNextRow---
*Free the nextRow cellElement of the tree
*
*@tree : an allocated cellElement
*
*/
void removeNextRow(cellElement* tree);
void recursivePrint(cellElement * tree);
/*---FreeCellElem---
*Allocates a cellElement and returns it
*
*@return : pointer on the allocated cellElement
*
*/
void FreeCellElement(cellElement* element);
#endif

29
LibAutomaton/Makefile Normal file
View File

@ -0,0 +1,29 @@
CC=gcc
CFLAGS=-Wall -Werror -pedantic -fpic -g
LIBSDIR=-L/usr/lib -L../Libs
INCLUDEDIR=-I/usr/include -I.
#Library variables
LIBTARGET=libAutomaton.so
LIBSOURCE=list CellElement
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

206
LibAutomaton/list.c Normal file
View File

@ -0,0 +1,206 @@
/*********************************************************************************
* 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;
/* 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->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;
}

99
LibAutomaton/list.h Normal file
View File

@ -0,0 +1,99 @@
#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;
};
typedef struct List {
ListElement *head;
ListElement *tail;
int size;
} List;
/*
* Create a new list
* @return 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
* @param size size of the data
* @return 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
* @param size size of the data
* @return 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 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 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 position of the element
* @return status of the operation
*/
int RemoveElement(List *list, int nb);
/*
* Delete the first element of the list
* @param list as a pointer
* @return status of the operation
*/
int shift(List *list);
/*
* Delete the last element of the list
* @param list as a pointer
* @return status of the operation
*/
int pop(List *list);
/*
* Delete every elements in a list
* @param list as a pointer
* @return status of the operation
*/
int DeleteListContent(List *list);
/*
* Free a list
* @param list as a pointer
* @return status of the operation
*/
int FreeList(List *list);
#endif /* LIST_H */