graph kruskal(graph G, float (*pesoArco )(void *)) { int nNodes = graphCountNodes(G); graph GF = graphInit(nNodes, GRAPH_IS_NOT_ORIENTED); // e gli orientati?? uf_handler uf = uf_init(graphGetMaxNodes(G)); archInfo arco; coda allArcs=graphGetAllArchs(G); heap archHeap=heapInit(nNodes, pesoArco, HEAP_GET_MIN); while ((arco=codaGet(allArcs))!=NULL) { heapInsert(archHeap, arco); } int from, to; while ((arco= heapExtract(archHeap))!=NULL) { from=arco->fromNode; to= arco->toNode; if (uf_find(uf,from , to)) { uf_unionFind(uf, from, to); graphAddNode(GF, from, arco->fromInfo); graphAddNode(GF, to, arco->toInfo); graphAddArch(GF, from, to, arco->archInfo); } } return GF; }
int main() { int i; srand(time(NULL)); Heap * h = createHeap(100); for (i = 0; i < 100; ++i) { if (i > 20) { heapAdd(h, createHeapNode(FLT_MAX, i)); continue; } heapAdd(h, createHeapNode(rand() % 100, i)); } displayHeap(h); for (i = 0; i < 10; ++i) { HeapNode *tmp = heapExtractHead(h); fprintf(stdout, "\nAncien noeud : "); displayHeapNode(tmp); fprintf(stdout, "\n"); tmp->c = rand() * i % 100 + 100; fprintf(stdout, "Nouveau noeud : "); displayHeapNode(tmp); heapAdd(h, tmp); displayHeap(h); } HeapNode * tmp = heapExtract(h, 2); tmp->c = rand() % 100; heapAdd(h, tmp); displayHeap(h); freeHeap(h); return 0; }
void produce(unsigned int n, void *(*func)(void *)) { int maxHandle = 8, readyId=-1, nC = 0; Heap *wHeap = NULL, *restHeap = NULL; wHeap = initHeap(wHeap, loadComp, freeLoad); restHeap = initHeap(restHeap, loadComp, freeLoad); unsigned int minThreshold = maxHandle > n ? n : maxHandle; pthread_t thList[minThreshold]; pthread_attr_t attr; pthread_attr_init(&attr); #ifdef __linux__ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); #endif for (readyId=0; readyId < minThreshold; ++readyId) { unsigned int *iNew = (unsigned int *)malloc(sizeof(unsigned int)); *iNew = readyId; Load *l = createLoad(iNew); l->thId = readyId; addLoad(wHeap, l); pthread_create(thList + readyId, &attr, func, l); } int i, minFreeLoadCount = minThreshold >> 1; if (readyId < n) { heapifyFromHead(wHeap); i = readyId; int chillThId = -1; Load *wHead = NULL; while (i < n) { printf("\033[32mwHeap: "); printHeap(wHeap, printLoad); printf("\n\033[33mrHeap: "); printHeap(restHeap, printLoad); printf("\ni:%d n:%d chillThId: %d\033[00m\n", i, n, chillThId); if ((wHead = peek(wHeap)) != NULL) { printf("wHead: %d\n", wHead->id); void *data = NULL; int join = pthread_join(thList[wHead->thId], &data); printf("newJoin: %d\n", join); if (! join) { printf("joined: %d\n", wHead->thId); if (data != NULL) { printf("\033[36m\nRetr %s :%d\033[00m\n", (char *)data, wHead->thId); free(data); } chillThId = wHead->thId; printf("chillThId: %d\n", chillThId); #ifdef DEBUG printf("wHead->thId: %d\n", wHead->thId); #endif heapExtract(wHeap, (const void **)&wHead); wHead->id = getAvailableId(restHeap); addLoad(restHeap, wHead); printf("rHeap"); printHeap(restHeap, printLoad); wHead = NULL; } } if (getSize(wHeap) < minFreeLoadCount && peek(restHeap) != NULL) { #ifdef DEBUG printf("Peeked: %p\n", peek(restHeap)); printf("\nrestHeap\n"); #endif heapExtract(restHeap, (const void **)&wHead); if (wHead == NULL) continue; #ifdef DEBUG printf("wHead->thId:: %p\n", wHead); #endif wHead->thId = chillThId; *((int *)wHead->data) = i; addLoad(wHeap, wHead); int createStatus =\ pthread_create(thList + wHead->thId, &attr, func, wHead); printf("createdStatus: %d i: %d\n", createStatus, i); if (! createStatus) { ++i; } } } } while (! isEmpty(wHeap)) { Load *tmpHead = NULL; if (! heapExtract(wHeap, (const void **)&tmpHead) && tmpHead != NULL) { void *data = NULL; if (! pthread_join(thList[tmpHead->thId], &data)) { if (data != NULL) { printf("i: %d Joined msg: %s\n", i, (char *)data); free(data); } } } freeLoad(tmpHead); } destroyHeap(wHeap); destroyHeap(restHeap); }