1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-11-10 19:03:21 +00:00

Les lists font des trucs à peut près

J'ai rajouté le friendly aussi, comme ne C++
This commit is contained in:
Antoine Bartuccio 2018-06-05 13:43:45 +02:00
parent 3232ac1ec6
commit a72587d430
Signed by: klmp200
GPG Key ID: E7245548C53F904B
5 changed files with 87 additions and 20 deletions

View File

@ -9,15 +9,29 @@ GETTER(Element, Element*, next)
GETTER(Element, Element*, previous) GETTER(Element, Element*, previous)
GETTER(Element, void*, data) GETTER(Element, void*, data)
void set_previous_Element(THIS(Element), Element *previous){ SETTER(Element, Element*, previous)
this->previous = previous; SETTER(Element, Element*, next)
}
void set_next_Element(THIS(Element), Element *next){
this->next = next;
}
void $_free__Element(THIS(Element)){ void $_free__Element(THIS(Element)){
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)
free(this->data);
free(this); free(this);
} }
@ -31,7 +45,13 @@ Element *_init_element(void *data, size_t size, List *list){
el->previous = NULL; el->previous = NULL;
el->next = NULL; el->next = NULL;
LINK_ALL(Element, el, get_next, get_previous, get_data, set_next, set_previous) LINK_ALL(Element, el,
get_next,
get_previous,
get_data,
set_next,
set_previous
)
return el; return el;
} }

View File

@ -15,16 +15,21 @@ struct o_Element {
PRIVATE struct o_Element *next; PRIVATE struct o_Element *next;
PRIVATE struct o_Element *previous; PRIVATE struct o_Element *previous;
PUBLIC struct o_Element *(*get_next)(O_THIS(Element)); PUBLIC struct o_Element *(*get_next)(_THIS(Element));
PUBLIC struct o_Element *(*get_previous)(O_THIS(Element)); PUBLIC struct o_Element *(*get_previous)(_THIS(Element));
PUBLIC void *(*get_data)(O_THIS(Element)); PUBLIC void *(*get_data)(_THIS(Element));
PUBLIC void (*set_previous)(O_THIS(Element), struct o_Element *previous); PUBLIC void (*set_previous)(_THIS(Element), struct o_Element *previous);
PUBLIC void (*set_next)(O_THIS(Element), struct o_Element *next); PUBLIC void (*set_next)(_THIS(Element), struct o_Element *next);
PUBLIC void (*$_free_)(O_THIS(Element)); DESTRUCTOR(Element);
}; };
FRIENDLY(list, List)
FRIENDLY(data, List)
FRIENDLY(next, List)
FRIENDLY(previous, List)
Element *$_init_Element(void *data, size_t size, List *list); Element *$_init_Element(void *data, size_t size, List *list);
#endif //LO41_ELEMENT_H #endif //LO41_ELEMENT_H

View File

@ -8,16 +8,44 @@ GETTER(List, Element*, tail)
GETTER(List, int, size) GETTER(List, int, size)
void $_free__List(THIS(List)){ void $_free__List(THIS(List)){
this->clear(this);
free(this); free(this);
} }
void clear_List(THIS(List)){
Element * el = this->head;
Element * el_tmp;
while (el != NULL){
el->list = NULL;
el_tmp = el;
el = el_tmp->next;
DELETE(el_tmp);
}
this->size = 0;
}
void remove_head_List(THIS(List)){
DELETE(this->head);
}
void remove_tail_List(THIS(List)){
DELETE(this->tail);
}
List *$_init_List(){ List *$_init_List(){
List *l = (List*) malloc_or_die(sizeof(List)); List *l = (List*) malloc_or_die(sizeof(List));
l->size = 0; l->size = 0;
l->head = NULL; l->head = NULL;
l->tail = NULL; l->tail = NULL;
LINK_ALL(List, l, get_head, get_tail, get_size) LINK_ALL(List, l,
get_head,
get_tail,
get_size,
clear,
remove_head,
remove_tail
)
return l; return l;
} }

View File

@ -18,12 +18,21 @@ struct o_List {
PRIVATE Element *tail; PRIVATE Element *tail;
PRIVATE int size; PRIVATE int size;
PUBLIC Element *(*get_head)(O_THIS(List)); PUBLIC Element *(*get_head)(_THIS(List));
PUBLIC Element *(*get_tail)(O_THIS(List)); PUBLIC Element *(*get_tail)(_THIS(List));
PUBLIC int (*get_size)(O_THIS(List)); PUBLIC int (*get_size)(_THIS(List));
PUBLIC void (*$_free_)(O_THIS(List)); PUBLIC void (*clear)(_THIS(List));
PUBLIC void (*remove_head)(_THIS(List));
PUBLIC void (*remove_tail)(_THIS(List));
DESTRUCTOR(List);
}; };
FRIENDLY(head, Element)
FRIENDLY(tail, Element)
FRIENDLY(size, Element)
List *$_init_List(); List *$_init_List();
#endif //LO41_LIST_H #endif //LO41_LIST_H

View File

@ -10,8 +10,9 @@
#define NEW(type, ...) $_init_##type(__VA_ARGS__) #define NEW(type, ...) $_init_##type(__VA_ARGS__)
#define DELETE(obj) obj->$_free_(obj) #define DELETE(obj) obj->$_free_(obj)
#define DESTRUCTOR(type) void (*$_free_)(struct o_##type *this)
#define THIS(type) type *this #define THIS(type) type *this
#define O_THIS(type) struct o_##type *this #define _THIS(type) struct o_##type *this
#define CRASH(message) perror(message) #define CRASH(message) perror(message)
#define SYNCHRONIZE #define SYNCHRONIZE
@ -19,9 +20,13 @@
#define PUBLIC #define PUBLIC
#define PROTECTED #define PROTECTED
#define FRIENDLY(property, obj)
#define LINK(type, obj, method) obj->method = method##_##type; #define LINK(type, obj, method) obj->method = method##_##type;
#define GETTER(obj_type, variable_type, variable) variable_type get_##variable##_##obj_type(obj_type *this){\ #define GETTER(obj_type, variable_type, variable) variable_type get_##variable##_##obj_type(obj_type *this){\
return this->variable; } return this->variable; }
#define SETTER(obj_type, variable_type, variable) void set_##variable##_##obj_type(obj_type *this, variable_type var){\
this->variable = var; }
void * malloc_or_die(size_t size); void * malloc_or_die(size_t size);