int slist_append(slist_t *list,void *Value) { slist_node_t *temp_p=NULL; if (Value == NULL) { // if no have Value return 0; } temp_p=(slist_node_t *)calloc(1,sizeof(slist_node_t)); if(temp_p==NULL) { return -1; } slist_data(temp_p) = Value; slist_next(temp_p)=NULL; if (slist_head(list)==NULL) { slist_head(list) = temp_p; slist_tail(list) = temp_p; slist_size(list)++; return 0; } slist_next(slist_tail(list)) = temp_p; slist_tail(list) = temp_p; slist_size(list)++; return 0; }
slist slist_insert(slist lista, int ind, int dato) { int lon = slist_length(lista); int i; slist nodoAux = slist_create(); slist nodoNuevo = slist_create(); slist listaAux = lista; if( ind == 0) { lista = slist_preppend(lista, dato); return lista; } if( ind == lon) { lista = slist_append(lista, dato); return lista; } if( ind > 0 && ind < lon) { nodoNuevo = slist_append(nodoNuevo, dato); for(i = 0; i < ind - 1; lista = slist_next(lista), i++) ; nodoAux = slist_next(lista); slist_next(lista) = nodoNuevo; slist_next(nodoNuevo) = nodoAux; } return listaAux; }
int slist_prepend(slist_t *list,void *Value) { if (Value == NULL) { // if no have Value return 0; } slist_node_t *temp_p; temp_p=(slist_node_t *)calloc(1,sizeof(slist_node_t)); if(temp_p==NULL) { return -1; } slist_data(temp_p)=Value; temp_p->next = NULL; if (slist_head(list)==NULL) // the list is empty { slist_head(list) = temp_p; slist_tail(list) = temp_p; slist_next(temp_p) = NULL; slist_size(list) = 1; return 0; } slist_next(temp_p) = slist_head(list); slist_head(list) = temp_p; if (slist_tail(list)==NULL) { slist_tail(list)=temp_p; } slist_size(list)++; return 0; }
slist slist_remove(slist lista, int ind) { int lon = slist_length(lista); slist listaAux = lista; slist nodoAux; int i; if(ind < 0 || ind >= lon) return lista; if(ind == 0) { nodoAux = lista; lista = slist_next(lista); free(nodoAux); return lista; } else { for( i = 0; i < ind - 1; lista = slist_next(lista), i++) ; nodoAux = slist_next(lista); slist_next(lista) = slist_next(nodoAux); free(nodoAux); } return listaAux; }
slist slist_concat(slist l1, slist l2) { slist listaNueva = slist_create(); for( ; l1 != slist_empty(); l1 = slist_next(l1)) listaNueva = slist_append(listaNueva, slist_data(l1)); for( ; l2 != slist_empty(); l2 = slist_next(l2)) listaNueva = slist_append(listaNueva, slist_data(l2)); return listaNueva; }
static int _ls_do_files_short(Prefs * prefs, char const * directory, SList * files) { char * cols; char * p; size_t len = 0; size_t lencur; size_t lenmax = 0; size_t colnb = 0; size_t i = 0; size_t j = 0; char c; SList cur; if(((*prefs & LS_PREFS_1) == 0) && (cols = getenv("COLUMNS")) != NULL && *cols != '\0' && (len = strtol(cols, &p, 10)) > 0 && *p == '\0') { for(cur = *files; cur != NULL; slist_next(&cur)) { lencur = strlen(slist_data(&cur)); lenmax = max(lenmax, lencur); } if(*prefs & LS_PREFS_F) lenmax++; if(lenmax > 0) colnb = len / ++lenmax; } for(cur = *files; cur != NULL; slist_next(&cur)) { p = slist_data(&cur); j = strlen(p); fwrite(p, sizeof(char), j, stdout); if((*prefs & LS_PREFS_F) && (c = _short_file_mode(prefs, directory, p))) { fputc(c, stdout); j++; } if(i + 1 < colnb) { for(i++; j < lenmax; j++) fputc(' ', stdout); continue; } fputc('\n', stdout); i = 0; } if(i != 0) fputc('\n', stdout); return 0; }
slist slist_preppend(slist lista, int dato) { slist nodoNuevo = malloc(sizeof(slist)); slist_data(nodoNuevo) = dato; slist_next(nodoNuevo) = lista; return nodoNuevo; }
int slist_length(slist lista) { int longitud = 0; for( ; lista != slist_empty(); lista = slist_next(lista) ) longitud++; return longitud; }
END_TEST START_TEST(test_network_nodes) { Edge *e1 = NULL, *e2 = NULL, *e3 = NULL; SList *nodes = NULL; Node ntmp = 0; e1 = edge_create(1, 2, 1, 0); e2 = edge_create(3, 4, 1, 0); e3 = edge_create(5, 6, 1, 0); net = network_create(); network_add_edge(net, e1); network_add_edge(net, e2); network_add_edge(net, e3); nodes = network_nodes(net); fail_unless(nodes != NULL); fail_unless(slist_length(nodes) == 6); while(nodes != NULL){ ntmp = *((Node *) slist_head_data(nodes)); fail_unless((1 <= ntmp) && (ntmp <= 6)); nodes = slist_next(nodes); } network_destroy(net); }
static void do_user_summary_output(slist *sptr) { const snode *sn; if (sptr->cnt == 0) { printf("<no events of interest were found>\n\n"); return; } slist_first(sptr); sn=slist_get_cur(sptr); while (sn) { long uid; char name[64]; if (sn->str[0] == '-' || isdigit(sn->str[0])) { uid = strtol(sn->str, NULL, 10); printf("%u ", sn->hits); safe_print_string(aulookup_uid(uid, name, sizeof(name)), 1); } else { printf("%u ", sn->hits); safe_print_string(sn->str, 1); } sn=slist_next(sptr); } }
static int _ls_do_files_long(Prefs * prefs, char const * directory, SList * files) { SList cur; char * file = NULL; char * p; int (* _stat)(const char * filename, struct stat * buf) = lstat; struct stat st; size_t len; if(*prefs & LS_PREFS_H || *prefs & LS_PREFS_L) _stat = stat; for(cur = *files; cur != NULL; slist_next(&cur)) { p = slist_data(&cur); if(directory != NULL) { len = strlen(directory) + 1 + strlen(p) + 1; if((p = realloc(file, len)) == NULL) { _ls_error("malloc", 0); continue; } file = p; p = slist_data(&cur); snprintf(file, len, "%s/%s", directory, p); } if(_stat(directory == NULL ? p : file, &st) != 0) _ls_error(file, 0); else _long_print(prefs, file != NULL ? file : p, p, &st); } free(file); return 0; }
END_TEST /* Testeo de funcionalidad */ START_TEST(test_network_get_edges) { Edge *e1 = NULL, *e2 = NULL, *e3 = NULL, *etmp = NULL; SList *el = NULL, *tmp = NULL; Node ntmp = 0; e1 = edge_create(0, 1, 1, 0); e2 = edge_create(0, 2, 1, 0); e3 = edge_create(2, 3, 1, 0); net = network_create(); network_add_edge(net, e1); network_add_edge(net, e2); network_add_edge(net, e3); el = network_get_edges(net, 0); fail_unless(!slist_is_empty(el)); tmp = el; while(tmp != NULL){ etmp = (Edge *) slist_nth_data(tmp, 0); ntmp = *edge_get_second(etmp); fail_unless(ntmp == 1 || ntmp == 2); tmp = slist_next(tmp); } network_destroy(net); }
END_TEST START_TEST(test_network_get_neightbours) { Edge *e1 = NULL, *e2 = NULL, *e3 = NULL; Node *ntmp = NULL; SList *el = NULL, *tmp = NULL; e1 = edge_create(2, 3, 1, 0); e2 = edge_create(1, 4, 1, 0); e3 = edge_create(2, 1, 1, 0); net = network_create(); network_add_edge(net, e1); network_add_edge(net, e2); network_add_edge(net, e3); el = network_neighbours(net, 2); fail_unless(!slist_is_empty(el)); tmp = el; while(tmp != NULL){ ntmp = (Node *) slist_nth_data(tmp, 0); fail_unless(*ntmp == 3 || *ntmp == 1); tmp = slist_next(tmp); } slist_free(el); network_destroy(net); }
SList * clonar(SList *l) { SList *ret = NULL; for(; l != NULL; l = slist_next(l)) ret = slist_append(ret, slist_data(l)); return ret; }
bool slist_has_next(SList list) { if(list == slist_empty()) return FALSE; if(slist_next(list) != NULL) return TRUE; return FALSE; }
slist slist_intersec(slist l1, slist l2) { slist listaNueva = slist_create(); slist l2Aux = l2; for( ; l1 != slist_empty(); l1 = slist_next(l1)) { for( ; l2 != slist_empty(); l2 = slist_next(l2)) { if( slist_data(l1) == slist_data(l2)) { if(slist_contain(listaNueva, slist_data(l1)) == FALSE) listaNueva = slist_append(listaNueva, slist_data(l1)); } } l2 = l2Aux; } return listaNueva; }
bool slist_contain(slist lista, const int elem) { for( ; lista != slist_empty(); lista = slist_next(lista)) { if(elem == slist_data(lista)) return TRUE; } return FALSE; }
SList slist_interect_custom(SList list1, SList list2, CompareFunc func, size_t size) { SList aux_list1 = list1; SList aux_list2 = list2; SList new_list = slist_create(); while (aux_list1 != slist_empty()) { while(aux_list2 != slist_empty()) { if(func(slist_data(aux_list1), slist_data(aux_list2)) && !slist_contain(new_list, slist_data(aux_list1), func)) new_list = slist_append(new_list, slist_data(aux_list1), size); aux_list2 = slist_next(aux_list2); } aux_list2 = list2; aux_list1 = slist_next(aux_list1); } return new_list; }
int slist_index(slist lista, const int elem) { int index = 0; for( ; lista != slist_empty(); lista = slist_next(lista), index++) { if(slist_data(lista) == elem) return index; } return -1; }
void slist_destroy(slist lista) { slist nodoAux; for( ; lista != slist_empty(); ) { nodoAux = lista; lista = slist_next(lista); free(nodoAux); } }
SList slist_concat(SList list1, SList list2, size_t size) { SList new_list = slist_create(); SList aux_list1 = list1; SList aux_list2 = list2; while(aux_list1 != slist_empty()) { new_list = slist_append(new_list, slist_data(aux_list1), size); aux_list1 = slist_next(aux_list1); } while(aux_list2 != slist_empty()) { new_list = slist_append(new_list, slist_data(aux_list2), size); aux_list2 = slist_next(aux_list2); } return new_list; }
slist slist_intersec_custom(slist l1, slist l2, bool (*f)(int, int)) { slist listaNueva = slist_empty(); slist l2Aux = l2; for( ; l1 != slist_empty(); l1 = slist_next(l1)) { for( ; l2 != slist_empty(); l2 = slist_next(l2)) { if ((slist_data(l1) == slist_data(l2)) && f(slist_data(l1), slist_data(l2))) { if(slist_contain(listaNueva, slist_data(l1)) == FALSE) listaNueva = slist_append(listaNueva, slist_data(l1)); } } l2 = l2Aux; } return listaNueva; }
/* Para testing de memoria */ void network_memory_test(void){ /* Codigo que deberia correr sin memory leaks */ Edge *e1 = NULL, *e2 = NULL, *e3 = NULL, *e4 = NULL, *etmp = NULL; Node *ntmp = NULL; SList *el = NULL, *nl = NULL, *tmp = NULL; e1 = edge_create(0, 1, 1, 0); e2 = edge_create(0, 2, 4, 0); e3 = edge_create(2, 3, 6, 0); e4 = edge_create(2, 0, 0, 0); net = network_create(); network_add_edge(net, e1); network_add_edge(net, e2); network_add_edge(net, e3); network_add_edge(net, e4); el = network_get_edges(net, 0); tmp = el; while(tmp != NULL){ etmp = (Edge *) slist_nth_data(tmp, 0); edge_pprint(etmp); tmp = slist_next(tmp); } nl = network_neighbours(net, 2); printf("Vecinos del nodo: 2\n"); tmp = nl; while(tmp != NULL){ ntmp = (Node *) slist_nth_data(tmp, 0); tmp = slist_next(tmp); printf(" %d\n", *ntmp); } slist_free(nl); network_destroy(net); }
unsigned int slist_length(SList list) { SList aux_list = list; unsigned int length = 0; while (aux_list != slist_empty()) { ++length; aux_list = slist_next(aux_list); } return length; }
bool slist_contain(SList list, void *data, CompareFunc func) { SList aux_list = list; while(aux_list != slist_empty()) { if(func(slist_data(aux_list), data) == TRUE) return TRUE; aux_list = slist_next(aux_list); } return FALSE; }
slist slist_append(slist lista, int dato) { slist nodoNuevo = malloc(sizeof(slist)); slist aux = lista; slist_data(nodoNuevo) = dato; slist_next(nodoNuevo) = slist_empty(); if(lista == slist_empty()) { return nodoNuevo; } else { for( ; slist_next(lista) != slist_empty(); lista = slist_next(lista)) ; slist_next(lista) = nodoNuevo; } return aux; }
/* slist_find -- Trouve le premier élément de la liste égal à item * (d'après le prédicat pred) et positionne le pointeur de liste dessus. * Renvoie un pointeur sur son son contenu. * Si l'élément n'a pas pu ^etre trouvé, renvoie NULL. * Complexité: O(longueur(list)*C(pred)) */ void *slist_find(SList *list, void *item, int (*pred)(void *, void *)) { assert((list != NULL) && (pred != NULL)); if (slist_empty(list)) return NULL; for (slist_reset(list); list->current != NULL; slist_next(list)) if (pred(slist_current(list), item)) break; if (list->current != NULL) return list->current->item; /* Trouvé */ else return NULL; /* Pas trouvé */ }
SList slist_remove(SList list, unsigned int pos, RemoveFunc remove) { SList list_aux = list; if(pos == 0) { list = slist_next(list); //remove(&slist_data(list_aux)); //free(list_aux->data); free(list_aux); } else { int i; for( i = 0; i < slist_length(list); i++) { if(i == pos - 1) { SList node_aux = slist_next(list_aux); slist_next(list_aux) = slist_next(slist_next(list_aux)); //remove(&slist_data(node_aux)); free(node_aux); } list_aux = slist_next(list_aux); } } return list; }
int print_list(void) { int cnt = 0; slist_first(&s); do { snode *cur = slist_get_cur(&s); if (cur) { cnt++; printf("%s\n", cur->str); } } while (slist_next(&s)); return cnt; }
SList slist_insert(SList list, void *data, size_t size, unsigned int pos) { if( pos == 0) list = slist_preppend(list, data, size); if( pos > 0 && pos <= slist_length(list)) { int i; SList aux_list = list; SList new_node = slist_create(); new_node = slist_preppend(new_node, data, size); for(i = 0; i <= pos; i++) { if( i == pos - 1) { slist_next(new_node) = slist_next(aux_list); slist_next(aux_list) = new_node; } aux_list = slist_next(aux_list); } } return list; }