/* * @NAME: collection_map * @DESC: Igual a la funcion map de haskell */ void *collection_map( t_list* list, void *(*closure)(void*, void*),void *arg){ t_list *listaAux=collection_list_create(); t_link_element *element = list->head; sem_wait( &list->semaforo ); while( element != NULL ){ void *newData=malloc(sizeof(*closure)); newData=closure(element->data, arg); collection_list_add(listaAux,newData); element = element->next; } sem_post( &list->semaforo ); return(listaAux); }
/* * @NAME: collection_filter * @DESC: Igual a la funcion filter de haskell */ void *collection_filter( t_list* list, char (*closure)(void*, void*),void *arg){ t_list *listaAux=collection_list_create(); t_link_element *element = list->head; sem_wait( &list->semaforo ); while( element != NULL ){ if(closure(element->data, arg)){ void *newData=malloc(sizeof(element->data)); memcpy(newData,element->data,sizeof(element->data)); //newData=element->data; collection_list_add(listaAux,newData); } element = element->next; } sem_post( &list->semaforo ); return(listaAux); }
/* * @NAME: collection_take * @DESC: Toma n elementos */ void *collection_take( t_list* list, unsigned int n){ t_list *listaAux=collection_list_create(); t_link_element *element = list->head; unsigned int i=0; sem_wait( &list->semaforo ); while(( element != NULL) && (i<n)){ void *newData=malloc(sizeof(*element)); //memcpy(newData,element->data,sizeof(element->data)); newData=element->data; collection_list_add(listaAux,newData); element = element->next; i++; } sem_post( &list->semaforo ); return(listaAux); }
/* * @NAME: collection_map2 * @DESC: Igual a la funcion map de haskell pero con argumentos variables */ void *collection_map2( t_list* list, void *(*closure)(void*, ...),...){ va_list args_list; va_start(args_list,*closure); t_list *listaAux=collection_list_create(); t_link_element *element = list->head; sem_wait( &list->semaforo ); while( element != NULL ){ void *newData=malloc(sizeof(*closure)); //newData=closure(element->data, args_list); memcpy(newData,closure(element->data, args_list),sizeof(*closure)); collection_list_add(listaAux,newData); element = element->next; } sem_post( &list->semaforo ); return(listaAux); }
/* * @NAME: collection_filter2 * @DESC: Igual a la funcion filter de haskell con argumentos variables */ void *collection_filter2( t_list* list, char (*closure)(void*, ...),...){ va_list args_list; va_start(args_list,*closure); t_list *listaAux=collection_list_create(); t_link_element *element = list->head; sem_wait( &list->semaforo ); while( element != NULL ){ if(closure(element->data, args_list)){ //unsigned long *a; //a=element->data; void *newData=malloc(sizeof(element->data)); memcpy(newData,element->data,sizeof(element->data));//Si pasara pos donde apuntan punteros seria mas liviano //printf("Añado el sector ** %ld ** a la lista\n",*a); //newData=element->data; collection_list_add(listaAux,newData); } element = element->next; } sem_post( &list->semaforo ); return(listaAux); }
gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist) { struct stat st; gboolean valid; if (must_exist) { valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode)); } else { valid = TRUE; st.st_size = 0; st.st_mtime = 0; } if (valid) { CollectInfo *ci; ci = collection_info_new_if_not_exists(cd, &st, fd); if (!ci) return FALSE; DEBUG_3("add to collection: %s", fd->path); cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE); cd->changed = TRUE; if (!sorted || cd->sort_method == SORT_NONE) { collection_window_add(collection_window_find(cd), ci); } else { collection_window_insert(collection_window_find(cd), ci); } } return valid; }