static void test_push_existing() { size_t i, j; int top; struct pqueue pq; int elt; for (i = 0; i < count; i++) { pqueue_init_copy(&pq, &pqueue); elt = elts[i]; pqueue_push(&pq, &elt); assert_int_equal(pqueue_count(&pq), count + 1); for (j = 0; j < count + 1; j++) { top = *(int *)pqueue_top(&pq); pqueue_pop(&pq); if (j <= i) { assert_int_equal(top, elts[j]); } else { assert_int_equal(top, elts[j - 1]); } } pqueue_destroy(&pq); } }
static void test_push_max_plus_one() { int max = (count ? elts[0] : 0); int max_plus_one = max + 1; pqueue_push(&pqueue, &max_plus_one); assert_int_equal(pqueue_count(&pqueue), count + 1); assert_int_equal(*(int *)pqueue_top(&pqueue), max + 1); }
static void test_push_max() { int max = elts[0]; int elt = max; pqueue_push(&pqueue, &elt); assert_int_equal(pqueue_count(&pqueue), count + 1); assert_int_equal(*(int *)pqueue_top(&pqueue), max); }
static void test_push_max_minus_one() { int max = elts[0]; int max_minus_one = max - 1; pqueue_push(&pqueue, &max_minus_one); assert_int_equal(pqueue_count(&pqueue), count + 1); assert_int_equal(*(int *)pqueue_top(&pqueue), max); }
static void test_push_min() { int min = elts[count - 1]; int elt = min; pqueue_push(&pqueue, &elt); assert_int_equal(pqueue_count(&pqueue), count + 1); assert_int_equal(*(int *)pqueue_top(&pqueue), elts[0]); }
static void test_push_min_minus_one() { int min = elts[count - 1]; int min_minus_one = min - 1; pqueue_push(&pqueue, &min_minus_one); assert_int_equal(pqueue_count(&pqueue), count + 1); assert_int_equal(*(int *)pqueue_top(&pqueue), elts[0]); }
static void singleton_setup() { static int singleton_elts[] = { 1234 }; void *ptr; pqueue_init(&pqueue, sizeof(int), compar, NULL); elts = singleton_elts; count = 1; pqueue_push(&pqueue, elts); }
static void unsorted7_setup() { static int sorted7_elts[] = { 7, 6, 5, 4, 3, 2, 1 }; int unsorted7_elts[] = { 2, 1, 3, 4, 7, 6, 5 }; size_t i; pqueue_init(&pqueue, sizeof(int), compar, NULL); elts = sorted7_elts; count = 7; for (i = 0; i < count; i++) pqueue_push(&pqueue, &unsorted7_elts[i]); }
static void sorted5_setup() { static int sorted5_elts[] = { 5, 4, 3, 2, 1 }; size_t i; pqueue_init(&pqueue, sizeof(int), compar, NULL); elts = sorted5_elts; count = 5; for (i = 0; i < count; i++) pqueue_push(&pqueue, &elts[i]); }
static inline void sweep_line_insert (sweep_line_t *sweep, rectangle_t *rectangle) { if (sweep->insert) sweep->insert->prev = &rectangle->right; rectangle->right.next = sweep->insert; rectangle->right.prev = &rectangle->left; rectangle->left.next = &rectangle->right; rectangle->left.prev = NULL; sweep->insert = &rectangle->left; if (rectangle->left.x < sweep->insert_x) sweep->insert_x = rectangle->left.x; pqueue_push (sweep, rectangle); }
/* Attempts to get a packet from the upper layer and add it to the queue. Return &packet if the packet was added, NULL if there is no more data. */ Packet* add_packet(PQueue* queue, int seqn) { Packet* packet = pqueue_push(queue); int cnt = get_data(packet->buffer, DATASIZE); /* If the newly pushed packet could be filled with data, we fill in the header. Otherwise we pop it back out and give up. */ if (cnt != NET_EOF) { assert(cnt > 0); packet->nbuffer = cnt; packet->seqn = seqn; return packet; } else { pqueue_poptail(queue); return NULL; } }
int main(int argc, char *argv) { APIRET rc = NO_ERROR; PVOID pAlloc = NULL; PPQUEUE heap = NULL; int iRet = 0; EXCEPTIONREGISTRATIONRECORD xcpthand = { 0, &exception_handler }; int i = 0; srand(time(NULL)); /* * The whole idea with this sample is to demonstrate how to allocate memory * 'as needed'. The best way to do this is to use an exception handler */ rc = DosSetExceptionHandler(&xcpthand); /* * Allocate a very large buffer. This will fit approx 524280 nodes * which is way more than you'll need. */ rc = DosAllocMem(&pAlloc, 16*1024*1024, PAG_READ | PAG_WRITE); if(rc == NO_ERROR) { heap = pAlloc; } /* * Add some nodes with random priorities */ if(heap) { NODE tmpNode = { 0 }; PNODE node = NULL; puts("Add nodes"); for(i = 0; i < 100; i++) { tmpNode.priority = rand(); printf("Added node with priority: %u\n", tmpNode.priority); pqueue_push(heap, &tmpNode); } /* * Actually, it's neater to use the nodes-counter.. */ puts("\n\nEmpty heap"); while((node = pqueue_pop2(heap)) != NULL) { printf("Priority: %u\n", node->priority); } } /* * Free heap buffer */ if(heap) { DosFreeMem(heap); } /* * All registered exception handlers must be deregistered */ DosUnsetExceptionHandler(&xcpthand); return iRet; }