} T_END_TEST T_TEST(t_pqueue_alt_insert) { struct pqueue* q = pqueue_create(comp); int i; int num = 10; T_ASSERT(q); for (i = 0; i < num; i++) { T_ASSERT(0 == pqueue_insert(q, (void*)((i % 2) ? i : num - i - (num % 2 ? 1 : 2)))); } T_ASSERT(!pqueue_is_empty(q)); i = 0; while (!pqueue_is_empty(q)) { T_ASSERT(i == (int)pqueue_peek(q)); T_ASSERT(i == (int)pqueue_pop(q)); i++; } T_ASSERT(i == num); pqueue_destroy(q); } T_END_TEST
struct scheduler * create_scheduler(JSContext *cx) { struct scheduler *scheduler = malloc(sizeof(struct scheduler)); scheduler->queue = pqueue_create(); scheduler->cx = cx; return scheduler; }
int main (int argc, char *argv[]) { int i; struct pqueue *pqueue; pqueue = pqueue_create(0, compare, NULL); if (pqueue == NULL) { return -1; } for (i = 1; i < argc; i++) { pqueue_insert(pqueue, argv[i]); } printf("%s ", (char *) pqueue_pop(pqueue)); printf("%s ", (char *) pqueue_pop(pqueue)); printf("%s ", (char *) pqueue_pop(pqueue)); pqueue_destroy(pqueue); return 0; }
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; }
} T_END_TEST T_TEST(t_pqueue_duplicate) { struct pqueue* q = pqueue_create(comp); void* v = (void*)0xdeadbeef; T_ASSERT(q); T_ASSERT(0 == pqueue_insert(q, v)); T_ASSERT(0 == pqueue_insert(q, v)); T_ASSERT(!pqueue_is_empty(q)); T_ASSERT(v == pqueue_peek(q)); T_ASSERT(v == pqueue_pop(q)); T_ASSERT(!pqueue_is_empty(q)); T_ASSERT(v == pqueue_peek(q)); T_ASSERT(v == pqueue_pop(q)); T_ASSERT(pqueue_is_empty(q)); pqueue_destroy(q); } T_END_TEST
/* Interactively put/extract queue items. */ int main(int argc, char *const argv[]) { int rc = 0, cap = 0; long lval = 0; queue_t q = 0; char input[MAX_INPUT] = "\0", *p = NULL; FILE *fp = stdin; if (argc < 2) { fprintf(stderr, "Usage: %s capacity\n", argv[0]); return 1; } cap = atoi(argv[1]); if (cap < MIN_CAP || cap > MAX_CAP) { fprintf(stderr, "%s: invalid capacity: %s, stay within %d..%d\n", argv[0], argv[1], MIN_CAP, MAX_CAP); return 1; } q = pqueue_create((size_t)cap); if (-1 == q) { perror("pqueue_create"); return 1; } setvbuf(stdout, (char*)NULL, _IOLBF, 0); printf("Q(%d/%d) ready\n", cap, (int)pqueue_count(q)); for (;;) { if (NULL == fgets(input, sizeof(input)-1, fp)) break; if (NULL != (p = strrchr(input, '\n'))) *p = 0; if (NULL != (p = strrchr(input, '\r'))) *p = 0; if (0 == strcasecmp(input, "get")) { errno = 0; lval = (long) pqueue_get(q); if (-1 == lval && errno) { perror("pqueue_get"); continue; } printf("Q(%d/%d) >> %ld\n", cap, (int)pqueue_count(q), lval); } else if (0 == strcasecmp("dump", input) || 0 == strcasecmp("list", input)) { pqueue_dump(q, stdout); } else { lval = atol(input); if (0 == lval && strcmp("0", input)) { printf("Invalid input, try again\n"); continue; } rc = pqueue_put(q, (void*)lval); if (rc) { perror("pqueue_put"); continue; } printf("Q(%d/%d) << %ld\n", cap, (int)pqueue_count(q), lval); } } pqueue_free(q); printf("%s: exiting with rc=%d\n", argv[0], rc); return rc; }
/* 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); }
} T_END_TEST T_TEST(t_pqueue_destroy_empty) { struct pqueue* q = pqueue_create(comp); pqueue_destroy(q); } T_END_TEST