void merge(int num_threads, int length, pivot_data *before_merge, int* merged){ //printf("In merge num_threads %d\n", num_threads); int i; for(i=0; i<num_threads; i++){ before_merge[i].ctr=0; //printf("i %d length %d\n", i, before_merge[i].length); } //printf("length %d\n", length); int ctr = 0; pivot_data* dq; PQueue *pq = pqueue_new(heap_compare_pivots, num_threads); for(i=0; i<num_threads; i++){ pqueue_enqueue(pq, &before_merge[i]); //printf("Enqueue %d\n", before_merge[i].pivots[0]); } while(pq->size != 0){ dq = (pivot_data*)pqueue_dequeue(pq); //printf("Dequeue %d\n", dq->pivots[dq->ctr]); merged[ctr++] = dq->pivots[dq->ctr++]; if(dq->ctr != dq->length){ pqueue_enqueue(pq, dq); //printf("Enqueue %d\n", dq->pivots[dq->ctr]); } } pqueue_delete(pq); aligned_free(before_merge); }
int main(int argc, char ** argv) { int nElements, i, q, j; struct { char (*compare) (pq_element, pq_element); uint8_t size; uint8_t n_elements; pq_element heap[10]; } pqt; nElements = argc -1; pqueue_init(&pqt, 10, compare); for (j=0; j < 10; j++) { pqt.heap[j] = -1; } for (i=0; i < nElements; i++) { q = atoi(argv[i+1]); printf("%d %d\n", q, pqueue_enqueue(&pqt, q)); for (j=0; j < 10; j++) { printf("%d ", pqt.heap[j]); } printf("\n"); } for (i=0; i < nElements; i++) { printf("%d\n", pqueue_dequeue(&pqt)); } for (j=0; j < 10; j++) { printf("%d ", pqt.heap[j]); } printf("\n"); }
void mylib_barrier_calculate_pivots (struct barrier_node*b, int num_threads) { pthread_mutex_lock(&(b -> count_lock)); b -> count ++; if (b -> count == num_threads) { b -> count = 0; //This code should be executed by only one thread. int *merged_samples = (int*)malloc(sizeof(int)*num_threads*num_threads); int i = 0 , k= 0; PQueue*heap = pqueue_new(compare_heap_key,num_threads); for(;i<num_threads;++i) { if(samples_arr[i].count > 0 ) { pqueue_enqueue(heap,samples_arr + i); } } struct sample_size*ptr; while((ptr = pqueue_dequeue(heap)) != NULL) { merged_samples[k++] = ptr->addr[ptr->current_index++]; if(ptr->current_index < ptr->count) { pqueue_enqueue(heap,ptr); } } if(DEBUG_1) { puts("In merged samples"); for(i=0;i<k;++i) { printf("%d ",merged_samples[i]); } } //Find the p-1 pivots pivots = (int*)malloc(sizeof(int) * (num_threads - 1)); int pby2 = num_threads/2 - 1; for(i = 1;i < num_threads;++i) { int index = i*num_threads + pby2; if(index >= k) { break; } //Store unique pivots only. if(!total_pivots ||( pivots[total_pivots - 1] != merged_samples[index])) { pivots[total_pivots++] = merged_samples[index]; } } //free(merged_samples); pthread_cond_broadcast(&(b -> ok_to_proceed)); } else while (pthread_cond_wait(&(b -> ok_to_proceed), &(b -> count_lock)) != 0); pthread_mutex_unlock(&(b -> count_lock)); }
graph_t kruskal(graph_t graph) { graph_t result = graph_empty(graph_vertices_count(graph)); unsigned int L, R, num_edges = graph_edges_count(graph); vertex_t l = NULL, r = NULL; pqueue_t Q = pqueue_empty(num_edges); union_find_t C = union_find_create(graph_vertices_count(graph)); edge_t E = NULL, *edges = graph_edges(graph); for (unsigned int i = 0; i < num_edges; i++) { pqueue_enqueue(Q, edges[i]); } free(edges); edges = NULL; while (!pqueue_is_empty(Q) && union_find_count(C) > 1) { E = edge_copy(pqueue_fst(Q)); l = edge_left_vertex(E); r = edge_right_vertex(E); L = union_find_find(C, vertex_label(l)); R = union_find_find(C, vertex_label(r)); if (L != R) { union_find_union(C, L, R); E = edge_set_primary(E, true); } else { E = edge_set_primary(E, false); } result = graph_add_edge(result, E); pqueue_dequeue(Q); } while (!pqueue_is_empty(Q)) { E = edge_copy(pqueue_fst(Q)); pqueue_dequeue(Q); E = edge_set_primary(E, false); result = graph_add_edge(result, E); } Q = pqueue_destroy(Q); C = union_find_destroy(C); return result; }
int main() { unsigned int maxSize = 10; pqueue_t pq = pqueue_empty(maxSize); bool exit = false; char *option = NULL; unsigned int *u = calloc(1,sizeof(unsigned int)); unsigned int v; do { option = print_menu(); switch(*option) { case ADD: printf("\nPor favor ingrese el nodo: "); if(!pqueue_is_full(pq)) { scanf("%u",&v); *u = v; pqueue_enqueue(pq, *u); printf("\nExito.\n"); } else { printf("La cola esta llena\n"); } break; case SHOW: if(!pqueue_is_empty(pq)) { *u = pqueue_fst(pq); printf("\nEl maximo es: %u\n",*u); } else { printf("La cola está vacia\n"); } break; case POP: if(!pqueue_is_empty(pq)) { pqueue_dequeue(pq); printf("\nSe elimino correctamente\n"); } break; case EXIT: exit = true; break; } free(option); option = NULL; } while(!exit); pq = pqueue_destroy(pq); }
/* * Checks all available alarms and runs their handlers */ void check_alarms() { alarm_id a; alarm_t alarm; // Keep checking queue until we find an alarm that should not execute while (pqueue_peek(alarm_pqueue, &a) == 0) { alarm = (alarm_t) a; if (time_ticks * PERIOD >= alarm->time) { // Executes the alarm pqueue_dequeue(alarm_pqueue, &a); alarm = (alarm_t) a; alarm->func(alarm->arg); free(alarm); // Frees alarm after execution } else { break; } } }
int main() { pqueue_t* q = pqueue_create(); if (q == NULL) { fprintf(stderr, "Failed to create a pqueue.\n"); return EXIT_FAILURE; } size_t n = 100000; interval_bound_t* xs = malloc(n * sizeof(interval_bound_t)); memset(xs, 0, n * sizeof(interval_bound_t)); size_t i; for (i = 0; i < n; ++i) { xs[i].start_min = 100000.0 * ((double) rand() / (double) RAND_MAX); xs[i].end_max = xs[i].start_min + 1000.0 * ((double) rand() / (double) RAND_MAX); xs[i].x_max = (double) rand() / (double) RAND_MAX; pqueue_enqueue(q, &xs[i]); } sort_interval_bounds_asc(xs, n); interval_bound_t x; for (i = 0; i < n; ++i) { if (!pqueue_dequeue(q, &x)) { fprintf(stderr, "Pqueue was prematurely empty.\n"); return EXIT_FAILURE; } if (memcmp(&xs[i], &x, sizeof(interval_bound_t)) != 0) { fprintf(stderr, "Pop number %zu was incorrect.\n", i); return EXIT_FAILURE; } } pqueue_free(q); return EXIT_SUCCESS; }
/** * Solve given instance of the knapsack problem using a branch and bound * approach. * TODO: the solution presented below represents a "rough" attempt and is not * in anyway optimized. * TODO: solution in terms of which items are picked along the optimal path * could easily be recovered by associating with each node a bitvector * encoding the decision made along the tree in order to arrive at that * particular node (e.g., for the node encoding that we did not take * the first item, but took the second and third items, the node's bit vector * would be equal to 011) */ static char * solve_knapsack_instance_bb(int n, int K, Item *items) { PQueue *pq; Node *u, *v; Item tmp; int maxvalue; char *sol = malloc(128 * sizeof(char)); pq = pqueue_init(n, node_get_bound); v = malloc(sizeof(Item)); if (!v) allocation_error(); v->level = -1; v->value = 0; v->weight = 0; pqueue_enqueue(pq, (void *) v); /* While priority queue is not empty ... */ while (pq->nElements > 1) { pqueue_dequeue(pq, (void **) &v, NULL); v->bound = bound(n, K, items, v); DEBUG_PRINT("maxvalue: %d\t v->bound: %f", maxvalue, v->bound); if (v->bound > maxvalue) { /* Set u to be child that includes next item. */ /* Better solution: preallocate and dynamically grow * array of Items to be used as nodes in search tree */ u = malloc(sizeof(Node)); if (!u) allocation_error(); u->level = v->level + 1; u->weight = v->weight + items[u->level].weight; u->value = v->value + items[u->level].value; if (u->weight <= K && u->value > maxvalue) maxvalue = u->value; u->bound = bound(n, K, items, u); if (u->bound > maxvalue) pqueue_enqueue(pq, (void *) u); else free(u); /* Set u to be child that does not include next item */ u = malloc(sizeof(Node)); if (!u) allocation_error(); u->level = v->level + 1; u->weight = v->weight; u->value = v->value; u->bound = bound(n, K, items, u); if (u->bound > maxvalue) pqueue_enqueue(pq, (void *) u); else free(u); free(v); } } pqueue_free(pq); sprintf(sol, "%d\n", maxvalue); return sol; }
END_TEST START_TEST(test_pqueue_dequeue) { PriorityQueue *pq = pqueue_new(); ck_assert_msg(pq != NULL, "Priority queue should not be NULL."); TreeNode *t = tree_new(); t->type = LEAF; t->freq.v = 10; t->freq.c = 'c'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 1); t = tree_new(); t->type = LEAF; t->freq.v = 15; t->freq.c = 'x'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 2); t = tree_new(); t->type = LEAF; t->freq.v = 9; t->freq.c = 'q'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 3); t = tree_new(); t->type = LEAF; t->freq.v = 999; t->freq.c = 'a'; t->left = NULL; t->right = NULL; pqueue_enqueue(pq, t); ck_assert_int_eq(pqueue_size(pq), 4); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'q'); ck_assert_int_eq(pqueue_size(pq), 3); free(t); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'c'); ck_assert_int_eq(pqueue_size(pq), 2); free(t); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'x'); ck_assert_int_eq(pqueue_size(pq), 1); free(t); t = pqueue_dequeue(pq); ck_assert_int_eq(t->freq.c, 'a'); ck_assert_int_eq(pqueue_size(pq), 0); free(t); pqueue_free(pq); }
int main(){ setup(); puts("5-item enqueue"); pqueue_enqueue(pq, u[5]); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); puts("9-item enqueue"); pqueue_enqueue(pq, u[9]); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); puts("3-item enqueue"); pqueue_enqueue(pq, u[3]); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); puts("0-item enqueue"); pqueue_enqueue(pq, u[0]); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); puts("8-item enqueue"); pqueue_enqueue(pq, u[8]); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); void *data = NULL; puts("dequeue"); pqueue_dequeue(pq, &data); print(data); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); puts("dequeue"); pqueue_dequeue(pq, &data); print(data); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); puts("6-item enqueue"); pqueue_enqueue(pq, u[6]); puts(""); puts("PriorityQueue: "); for(int i=0; i<pqueue_len(pq); i++){ print(pq->nodes[i]); } puts(""); enddown(); return 0; }
graph_t kruskal(graph_t graph) { /* Computes a MST of the given graph. * * This function returns a copy of the input graph in which * only the edges of the MST are marked as primary. The * remaining edges are marked as secondary. * * The input graph does not change. * */ graph_t mst; union_find_t uf; pqueue_t pq; edge_t *edges; edge_t e; unsigned int left, right, n, m; /* Inicialización */ n = graph_vertices_count(graph); m = graph_edges_count(graph); mst = graph_empty(n); uf = union_find_create(n); pq = pqueue_empty(m); /* Llenar `pq` */ edges = graph_edges(graph); for (unsigned int j = 0; j < m; j++) { pqueue_enqueue(pq, edges[j]); } /* Ahora las aristas están en `pq` */ free(edges); edges = NULL; /* Principal */ while (!pqueue_is_empty(pq) && union_find_count(uf) > 1) { e = edge_copy(pqueue_fst(pq)); left = vertex_label(edge_left_vertex(e)); right = vertex_label(edge_right_vertex(e)); left = union_find_find(uf, left); right = union_find_find(uf, right); if (!union_find_connected(uf, left, right)) { e = edge_set_primary(e, true); union_find_union(uf, left, right); } else { e = edge_set_primary(e, false); } mst = graph_add_edge(mst, e); pqueue_dequeue(pq); } /* Agregar aristas restantes como secundarias */ while (!pqueue_is_empty(pq)) { e = edge_copy(pqueue_fst(pq)); e = edge_set_primary(e, false); mst = graph_add_edge(mst, e); pqueue_dequeue(pq); } /* Destroy */ uf = union_find_destroy(uf); pq = pqueue_destroy(pq); return (mst); }
/* RFC2740 3.8.1. Calculating the shortest path tree for an area */ void ospf6_spf_calculation(u_int32_t router_id, struct ospf6_route_table * result_table, struct ospf6_area * oa, unsigned int hostnum) { struct pqueue * candidate_list; struct ospf6_vertex * root, * v, * w; int i; int size; caddr_t lsdesc; struct ospf6_lsa * lsa; if(IS_OSPF6_SIBLING_DEBUG_SPF) { zlog_debug("Starting spf calculation..."); } /* initialize */ candidate_list = pqueue_create(); candidate_list->cmp = ospf6_vertex_cmp; ospf6_spf_table_finish (result_table); /* Install the calculating router itself as the root of the SPF tree */ /* construct root vertex */ lsa = ospf6_lsdb_lookup (htons (OSPF6_LSTYPE_ROUTER), htonl (0), router_id, oa->lsdb); if (lsa == NULL) { if(IS_OSPF6_SIBLING_DEBUG_SPF) { zlog_debug("Looking for type %d from lsdb %p with router id %d", htons(OSPF6_LSTYPE_ROUTER), oa->lsdb, router_id); zlog_debug("No router LSAs present, quiting spf calculation"); } return; } root = ospf6_vertex_create (lsa); root->area = oa; root->cost = 0; root->hops = 0; root->nexthop[0].ifindex = 0; /* loopbak I/F is better ... */ inet_pton (AF_INET6, "::1", &root->nexthop[0].address); /* Actually insert root to the candidate-list as the only candidate */ pqueue_enqueue (root, candidate_list); /* Iterate until candidate-list becomes empty */ while (candidate_list->size) { /* get closest candidate from priority queue */ v = pqueue_dequeue (candidate_list); /* installing may result in merging or rejecting of the vertex */ if (ospf6_spf_install (v, result_table, hostnum) < 0) continue; /* For each LS description in the just-added vertex V's LSA */ size = (VERTEX_IS_TYPE (ROUTER, v) ? sizeof (struct ospf6_router_lsdesc) : sizeof (struct ospf6_network_lsdesc)); for (lsdesc = OSPF6_LSA_HEADER_END (v->lsa->header) + 4; lsdesc + size <= OSPF6_LSA_END (v->lsa->header); lsdesc += size) { lsa = ospf6_lsdesc_lsa (lsdesc, v); if (lsa == NULL) continue; if (! ospf6_lsdesc_backlink (lsa, lsdesc, v)) continue; w = ospf6_vertex_create (lsa); w->area = oa; w->parent = v; if (VERTEX_IS_TYPE (ROUTER, v)) { w->cost = v->cost + ROUTER_LSDESC_GET_METRIC (lsdesc); w->hops = v->hops + (VERTEX_IS_TYPE (NETWORK, w) ? 0 : 1); } else /* NETWORK */ { w->cost = v->cost; w->hops = v->hops + 1; } /* nexthop calculation */ if (w->hops == 0) w->nexthop[0].ifindex = ROUTER_LSDESC_GET_IFID (lsdesc); else if (w->hops == 1 && v->hops == 0) ospf6_nexthop_calc (w, v, lsdesc); else { for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) && i < OSPF6_MULTI_PATH_LIMIT; i++) ospf6_nexthop_copy (&w->nexthop[i], &v->nexthop[i]); } /* add new candidate to the candidate_list */ if (IS_OSPF6_SIBLING_DEBUG_SPF) zlog_debug (" New candidate: %s hops %d cost %d", w->name, w->hops, w->cost); pqueue_enqueue (w, candidate_list); } } pqueue_delete (candidate_list); }