/* Generic LIST (1994) */ /* */ /* Author : Marie Aimar */ /* File : list.c */ /* */ #include #include #include "generic.h" #include "list.h" /*-------------------------------*/ t_genlist list_of_family; struct struct_list { generic_type element; t_list next; }; struct struct_double_list { generic_type element; t_double_list same_list; t_double_list next; double nb; }; struct struct_genlist { t_list head_of_list; }; struct struct_double_genlist { t_double_list head_of_list; }; t_genlist create_genlist(); void add_in_list( t_genlist,generic_type); generic_type find_in_list(t_genlist , int cmp_function(generic_type,generic_type), generic_type ); void apply_to_all(t_genlist , void (*fnct_to_apply)()); t_genlist create_genlist() { t_genlist new = (t_genlist)malloc(sizeof(struct struct_genlist)); new->head_of_list = NULL; return new; } void add_in_list(t_genlist genlist, generic_type element_to_add) { t_list *pt_list; for (pt_list = &(genlist -> head_of_list); (*pt_list); pt_list = &(*pt_list)->next) ; (*pt_list) = malloc(sizeof(struct struct_list)); (*pt_list) -> element = element_to_add; (*pt_list) -> next = NULL; } generic_type find_in_list(t_genlist genlist, int cmp_function(generic_type,generic_type), generic_type element_to_compare) { /* -- require : dictionary and elemnt != NULL -- ensure : returns the item if found or NULL if it does not exist */ t_list current = genlist->head_of_list; while (current) { if (cmp_function(element_to_compare,current->element) == 0) return current->element ; current = current->next; } return NULL; } void apply_to_all(t_genlist genlist, void (*fnct_to_apply)()) { t_list *pt_list ; for (pt_list=&(genlist->head_of_list); (*pt_list); pt_list=&(*pt_list)->next) fnct_to_apply((*pt_list)->element); } generic_type get_next(t_genlist genlist) { t_list pt_list ; generic_type elem; pt_list = genlist->head_of_list; genlist->head_of_list =pt_list->next; elem = pt_list->element; return elem; } void rank_in_list(t_double_genlist genlist,void (*value_fct)()) { double val ; t_double_list list = genlist->head_of_list; t_double_list same; double rank=1.0 ; while(list) { if (list->nb==1.0) { value_fct(list->element,rank); rank++; } else { val = (rank +rank+(list->nb-1))/2.0; value_fct(list->element,val); same=list->same_list; while (same) { /*printf("rang idem %f",val);*/ value_fct(same->element,val); same=same->same_list; } rank+=list->nb; } list=list->next; } } void insert_in_list(t_genlist genlist, int cmp_function(generic_type,generic_type), generic_type element_to_insert) { /* -- comment : it is inserted only if doesn't exist -- if the element already exists it exits -- require : genlist and element != NULL */ t_list current=genlist->head_of_list; t_list temp, previous=NULL; int comparaison_result; temp = (t_list)malloc(sizeof(struct struct_list)); temp->next = NULL; temp->element = element_to_insert; while ((current) && ((comparaison_result = cmp_function(current->element, element_to_insert)) != 0)) { previous= current; current=current->next; } if ( (current) && (comparaison_result ==0)) /* element already exists in the list */ { return; } if (!previous) /*it's the first one */ genlist->head_of_list = temp; else previous->next = temp; temp->next = current; return; } void insert_in_ordered_list(t_double_genlist genlist, int cmp_function(generic_type,generic_type), generic_type element_to_insert) { /* -- comment : it is inserted only if doesn't exist -- if the element already exists it exits -- require : genlist and element != NULL */ t_double_list same,s_previous, current=genlist->head_of_list; t_double_list temp, previous= NULL; int comparaison_result; s_previous=NULL; same =NULL; temp = (t_double_list)malloc(sizeof(struct struct_double_list)); temp->next = NULL; temp->same_list =NULL; temp->element = element_to_insert; temp->nb=1; while ((current) && ((comparaison_result = cmp_function(current->element, element_to_insert)) < 0)) { previous= current; current=current->next; comparaison_result = -1; } if ((current) && (comparaison_result == 0)) { /* element already exists in the list */ same=current->same_list; while (same) { s_previous=same; same=same->same_list ; } if (s_previous) s_previous->same_list = temp; else { current->same_list =temp; } current->nb++; return; } if (!previous) /*it's the first one */ { genlist->head_of_list= temp; } else previous->next = temp; temp->next =current; } void compute_all(t_genlist genlist,void (*fct_compute)()) { t_list list =genlist->head_of_list; t_list second_list =NULL; while (list) { second_list=list->next; while (second_list) { fct_compute(list->element,second_list->element); second_list=second_list->next; } list=list->next; } } t_double_genlist create_double_genlist() { t_double_genlist new = (t_double_genlist)malloc(sizeof(struct struct_double_genlist)); new->head_of_list = NULL; return new; }