/* * @NAME: collection_list_removeByPointer2 * @DESC: Elimina un elemento de la lista segun a donde apunte data */ bool collection_list_removeByPointer2( t_list *list, void *data, void (*data_destroyer)(void*) ){ //void *data = NULL; t_link_element *aux_element = NULL; if( list->head == NULL) return false; sem_wait( &list->semaforo ); if(list->head->data==data){ aux_element = list->head; list->head = list->head->next; }else{ t_link_element *element = list->head; while((element->next!=NULL)&&(element->next->data!=data)){ element=element->next; } if(element->next==NULL){ sem_post( &list->semaforo ); return false; } aux_element = element->next; element->next = element->next->next; } list->elements_count--; free(aux_element); data_destroyer(data); sem_post( &list->semaforo ); return true; }
static void dictionary_destroy_element(t_dictionary *self, t_hash_element *element, void(*data_destroyer)(void*)) { if (data_destroyer != NULL) { data_destroyer(element->data); } free(element->key); free(element); }
void dictionary_remove_and_destroy(t_dictionary *self, char *key, void(*data_destroyer)(void*)) { void *data = dictionary_remove_element(self, key); if( data != NULL) { self->elements_amount--; data_destroyer(data); } }
/* * @NAME: collection_list_removeByContent * @DESC: Elimina un elemento de la lista segun su contenido */ bool collection_list_removeByContent( t_list *list, void *p_data, void (*data_destroyer)(void*) ){ //void *data = NULL; t_link_element *aux_element = NULL; char *data; data=p_data; if( list->head == NULL) return false; sem_wait( &list->semaforo ); char *data_aux; data_aux=list->head->data; if(strcmp(data,data_aux)==0){ aux_element = list->head; p_data = list->head->data; list->head = list->head->next; }else{ t_link_element *element = list->head; //Comparo el contenido como si fueran caracteres.. :S data_aux=element->next->data; while((element->next!=NULL)&&(strcmp(data,data_aux)!=0)){ element=element->next; data_aux=element->next->data; } if(element->next==NULL){ sem_post( &list->semaforo ); return false; } p_data = element->next->data; aux_element = element->next; element->next = element->next->next; } list->elements_count--; free(aux_element); data_destroyer(p_data); sem_post( &list->semaforo ); return true; }