2018-06-04 22:26:48 +00:00
|
|
|
//
|
|
|
|
// Created by Antoine Bartuccio on 22/05/2018.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "List.h"
|
|
|
|
|
|
|
|
GETTER(Element, Element*, next)
|
|
|
|
GETTER(Element, Element*, previous)
|
|
|
|
GETTER(Element, void*, data)
|
|
|
|
|
2018-06-05 11:43:45 +00:00
|
|
|
SETTER(Element, Element*, previous)
|
|
|
|
SETTER(Element, Element*, next)
|
2018-06-04 22:26:48 +00:00
|
|
|
|
2018-06-05 23:15:23 +00:00
|
|
|
void _free__Element(THIS(Element)){
|
2018-06-05 11:43:45 +00:00
|
|
|
List * list = this->list;
|
|
|
|
if (list != NULL) {
|
|
|
|
if (list->head == this && list->tail == this) {
|
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
|
|
|
} else if (list->head == this) {
|
|
|
|
list->head = this->next;
|
|
|
|
this->previous = NULL;
|
|
|
|
} else if (list->tail == this) {
|
|
|
|
list->tail = this->previous;
|
|
|
|
this->previous->next = NULL;
|
|
|
|
} else {
|
|
|
|
this->next->previous = this->previous;
|
|
|
|
this->previous->next = this->next;
|
|
|
|
}
|
|
|
|
list->size --;
|
|
|
|
}
|
|
|
|
if (this->data != NULL)
|
2018-06-06 18:29:02 +00:00
|
|
|
this->data_free(this->data);
|
2018-06-04 22:26:48 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2018-06-05 23:15:23 +00:00
|
|
|
Element *_init_Element(void *data, size_t size, List *list){
|
2018-06-04 22:26:48 +00:00
|
|
|
Element *el = (Element*) malloc_or_die(sizeof(Element));
|
2018-06-05 13:52:48 +00:00
|
|
|
void *new_data = NULL;
|
|
|
|
|
|
|
|
if (data != NULL) {
|
|
|
|
new_data = malloc_or_die(size);
|
|
|
|
memcpy(new_data, data, size);
|
|
|
|
}
|
2018-06-04 22:26:48 +00:00
|
|
|
|
|
|
|
el->list = list;
|
2018-06-05 13:52:48 +00:00
|
|
|
el->data = new_data;
|
2018-06-04 22:26:48 +00:00
|
|
|
el->previous = NULL;
|
|
|
|
el->next = NULL;
|
2018-06-06 18:29:02 +00:00
|
|
|
el->data_free = free;
|
2018-06-04 22:26:48 +00:00
|
|
|
|
2018-06-05 13:52:48 +00:00
|
|
|
if (el->list != NULL)
|
|
|
|
el->list->size ++;
|
|
|
|
|
2018-06-05 11:43:45 +00:00
|
|
|
LINK_ALL(Element, el,
|
|
|
|
get_next,
|
|
|
|
get_previous,
|
|
|
|
get_data,
|
|
|
|
set_next,
|
|
|
|
set_previous
|
|
|
|
)
|
2018-06-04 22:26:48 +00:00
|
|
|
|
|
|
|
return el;
|
|
|
|
}
|