diff --git a/list.c b/list.c new file mode 100644 index 0000000000000000000000000000000000000000..a3e4fa916734d760b9ba9f42aeef5c07819fd36e --- /dev/null +++ b/list.c @@ -0,0 +1,270 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include "rank.h" +#include "list.h" +#include "job.h" +#include "outils.h" +#include "io.h" + +list_t * new_list(void) { + list_t * L = calloc(1, sizeof(list_t)); + assert(L); + L->head = NULL; + L->tail = NULL; + L->numelm = 0; + return L; +} + + +elmlist_t * new_list_elm(void * data) { + elmlist_t * E = calloc(1, sizeof(elmlist_t)); + assert(E); + E->data = data; + E->pred = NULL; + E->suc = NULL; + return E; +} + +void del_list(list_t ** ptrL, void (*ptrf)(void *)) { + assert(ptrL && *ptrL); + list_t * list = *ptrL; + elmlist_t * E = list->head; + while (E != NULL) { + elmlist_t * temp = E; + E = get_suc(temp); + if (ptrf) { + ptrf(get_data(temp)); + } + free(temp); + } + free(*ptrL); + *ptrL = NULL; +} + +bool is_empty(list_t * L) { + assert(L); + return L->numelm == 0; +} + +elmlist_t * get_head(list_t * L) { + assert(L); + return L->head; +} + +elmlist_t * get_tail(list_t * L) { + assert(L); + return L->tail; +} + +int get_numelm(list_t * L) { + assert(L); + return L->numelm; +} + +void view_list(list_t * L, void (*ptr_view)(void *)) { + assert(L && ptr_view); + for (elmlist_t * E = L->head; E != NULL; E = get_suc(E)) { + ptr_view(get_data(E)); + } +} + +void clean(list_t * L) { + assert(L); + elmlist_t * curr = L->head; + while (curr != NULL) { + elmlist_t * temp = curr; + curr = get_suc(temp); + free(temp); + } + L->head = NULL; + L->tail = NULL; + L->numelm = 0; +} + +void cons(list_t * L, void * data) { + assert(L); + elmlist_t * E = new_list_elm(data); + set_suc(E, L->head); + if (L->head != NULL) { + set_pred(L->head, E); + } + L->head = E; + if (L->tail == NULL) { + L->tail = E; + } + L->numelm++; +} + +void queue(list_t * L, void * data) { + assert(L); + elmlist_t * E = new_list_elm(data); + if (is_empty(L)) { + L->head = E; + L->tail = E; + } else { + set_suc(L->tail, E); + set_pred(E, L->tail); + L->tail = E; + } + L->numelm++; +} + +void insert_after(list_t * L, void * data, elmlist_t * elm) { + if (elm == NULL) { + printf("VALEUR NULL"); + return; +} + elmlist_t * E = new_list_elm(data); + set_suc(E, get_suc(elm)); + set_pred(E, elm); + if (get_suc(elm) != NULL) { + set_pred(get_suc(elm), E); + } else { + L->tail = E; + } + set_suc(elm, E); + L->numelm++; +} + +void ordered_insert(list_t * L, void * data, int (*cmpFct)(void*, void*)) { + assert(L && cmpFct); + if (is_empty(L) || cmpFct(get_data(L->head), data) > 0) { + cons(L, data); + } else { + elmlist_t * current = L->head; + while (get_suc(current) != NULL && cmpFct(get_data(get_suc(current)), data) <= 0) { + current = get_suc(current); + } + insert_after(L, data, current); + } +} + +void take_out(list_t * L, void * D) { + assert(L); + elmlist_t * current = L->head; + while (current != NULL) { + if (get_data(current) == D) { + elmlist_t * p = get_pred(current); + elmlist_t * s = get_suc(current); + if (p!= NULL) set_suc(p, s); + else L->head = s; + if (s!= NULL) set_pred(s, p); + else L->tail = p; + free(current); + L->numelm--; + return; + } + current = get_suc(current); + } +} + +static void partition(list_t * L, elmlist_t * pivot, list_t * val_inf, list_t * val_sup, int (*cmpFct)(void*, void*)) { + elmlist_t * cur = L->head; + while (cur != NULL) { + if (cmpFct(get_data(cur), get_data(pivot)) < 0) { + queue(val_inf, get_data(cur)); + } else { + queue(val_sup, get_data(cur)); + } + cur = get_suc(cur); + } +} + +void quick_sort(list_t * L, int (*cmpFct)(void*, void*)) { + assert(L && cmpFct); + if (L->numelm <= 1) return; + + elmlist_t * pivot = get_head(L); + L->head = get_suc(pivot); + set_pred(pivot, NULL); + set_suc(pivot, NULL); + + list_t * val_inf_pivot = new_list(); + list_t * val_sup_pivot = new_list(); + + partition(L, pivot, val_inf_pivot, val_sup_pivot, cmpFct); + + clean(L); + + if (is_empty(val_inf_pivot)) { + L->head = pivot; + L->tail = pivot; + L->numelm = 1; + } else { + quick_sort(val_inf_pivot, cmpFct); + L->head = val_inf_pivot->head; + L->tail = val_inf_pivot->tail; + L->numelm = val_inf_pivot->numelm; + set_pred(pivot, L->tail); + set_suc(L->tail, pivot); + L->tail = pivot; + L->numelm++; + } + + if (!is_empty(val_sup_pivot)) { + quick_sort(val_sup_pivot, cmpFct); + set_pred(val_sup_pivot->head, pivot); + set_suc(pivot, val_sup_pivot->head); + L->tail = val_sup_pivot->tail; + L->numelm += val_sup_pivot->numelm; + } + + free(val_inf_pivot); + free(val_sup_pivot); +} + +void find(list_t * L, void ** ptrKey, int (*cmpFct)(void*, void*), void (*delFct)(void*)) { + assert(L && ptrKey && cmpFct); + elmlist_t * curr = L->head; + while (curr != NULL) { + if (cmpFct(get_data(curr), *ptrKey) == 0) { + if (delFct) { + delFct(ptrKey); + } + *ptrKey = get_data(curr); + return; + } + curr = get_suc(curr); + } +} + +elmlist_t * get_suc(elmlist_t * E) { + assert(E); + return E->suc; +} +elmlist_t * get_pred(elmlist_t * E) { + assert(E); + return E->pred; +} + +void * get_data(elmlist_t * E) { + assert(E); + return E->data; +} + +void set_suc(elmlist_t * E, elmlist_t * suc) { + assert(E); + E->suc = suc; +} + +void set_pred(elmlist_t * E, elmlist_t * pred) { + assert(E); + E->pred = pred; +} + +void set_data(elmlist_t * E, void * data) { + assert(E); + E->data = data; +} + +bool is_in_list(list_t *L, void *key, int (*cmpFct)(void*, void*)) { + assert(L && cmpFct); + for (elmlist_t *E = L->head; E != NULL; E = E->suc) { + if (cmpFct(get_data(E), key) == 0) { + return true; + } + } + return false; +} \ No newline at end of file