}END_TEST START_TEST (test_add_more) { p_minheap m = minheap_init( 5 ); ck_assert_int_eq( 0, m->count ); ck_assert_int_eq( 5, m->alloc ); for( int i = 0; i < 20; i++ ) { elem_t e = new_elem( i, i, i ); minheap_add( m, &e ); } minheap_sort( m ); ck_assert_int_eq( 5, m->count ); ck_assert_int_eq( 5, m->alloc ); ck_assert_int_eq( 19, m->array[0].score ); ck_assert_int_eq( 19, m->array[0].query_id ); ck_assert_int_eq( 19, m->array[0].db_id ); ck_assert_int_eq( 18, m->array[1].score ); ck_assert_int_eq( 17, m->array[2].score ); ck_assert_int_eq( 16, m->array[3].score ); ck_assert_int_eq( 15, m->array[4].score ); minheap_exit( m ); }END_TEST
void add_to_minheap( p_minheap heap, uint8_t query_id, p_sdb_sequence db_seq, long score ) { elem_t e; e.query_id = query_id; e.db_id = db_seq->ID; e.db_frame = db_seq->frame; e.db_strand = db_seq->strand; e.score = score; #ifdef DBG_COLLECT_ALIGNED_DB_SEQUENCES dbg_add_aligned_sequence( db_seq->ID, query_id, score ); #endif /* * Alignments, with a score equal to the current lowest score in the heap are ignored */ minheap_add( heap, &e ); }
void exampleMinHeap() { // Use pooling for efficiency, if you don't want to use pooling // then comment out this line. pool_minheap(32); MinHeap* H = newMinHeap(31); minheap_add(H, 99, "99"); minheap_add(H, 45, "45"); minheap_add(H, 57, "57"); minheap_add(H, 12, "12"); minheap_add(H, 87, "87"); minheap_add(H, 42, "42"); minheap_add(H, 67, "67"); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); printf("Update 45 to 91\n"); minheap_update(H, 45, 91); minheap_set(H, 91, "91"); minheap_display(H, 2, &toString); printf("Update 91 to 1\n"); minheap_update(H, 91, 1); minheap_set(H, 1, "1"); minheap_display(H, 2, &toString); printf("Add 50\n"); minheap_add(H, 50, "50"); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); printf("Pop: '%s'\n", (char*)minheap_popMin(H)); minheap_display(H, 2, &toString); minheap_clear(H); printf("\n"); // Build a much bigger heap int total = 21; int keys[] = {0, 23, 3, 6, 41, 17, 21, 8, 9, 68, 2, 1, 34, 29, 38, 11, 15, 16, 45, 65, 39}; char* items[] = {"0", "23", "3", "6", "41", "17", "21", "8", "9", "68", "2", "1", "34", "29", "38", "11", "15", "16", "45", "65", "39"}; while (--total >= 0) minheap_add(H, keys[total], items[total]); minheap_display(H, 2, &toString); printf("Popping.. "); while (!minheap_isEmpty(H)) printf("%s ", (char*)minheap_popMin(H)); printf("\n"); minheap_free(H); // If you're not using pooling this can be commented out. This will // free all pooled nodes from memory. Always call this at the end // of using any List. unpool_minheap(); }