int main() { struct priority_queue *q; q = priority_queue_init(char_cmp); assert(q != NULL); assert(priority_queue_size(q) == 0); priority_queue_add(q, (void *) 'a'); assert(priority_queue_size(q) == 1); assert((char) priority_queue_get(q) == 'a'); priority_queue_add(q, (void *) 'b'); assert(priority_queue_size(q) == 2); assert((char) priority_queue_get(q) == 'a'); priority_queue_add(q, (void *) 'c'); assert(priority_queue_size(q) == 3); assert((char) priority_queue_pop(q) == 'a'); assert(priority_queue_size(q) == 2); assert((char) priority_queue_pop(q) == 'b'); assert((char) priority_queue_pop(q) == 'c'); assert(priority_queue_pop(q) == NULL); priority_queue_destory(q); TEST_OK("priority queue test passed..."); }
static int enqueue_next_best_state(diagnostic_problem problem, priority_queue opened, gotcha_node current, const_tv_term_list_list systems) { register unsigned int ix; current->depth += 1; ix = current->depth - 1; while (current->offsets[ix] < systems->arr[ix]->sz) { if (is_consistent(problem, current, systems)) { update_cardinality(problem, current); if (!priority_queue_push(opened, current)) { return 0; } increase_int_counter("pushed"); maximize_int_counter("max", priority_queue_size(opened)); return 1; } current->offsets[ix] += 1; increase_int_counter("inconsistent"); } gotcha_node_free(current); return 1; }
static int enqueue_sibling(diagnostic_problem problem, priority_queue opened, gotcha_node current, const_tv_term_list_list systems) { register unsigned int ix; gotcha_node sibling; if (current->depth == 0) { return 1; } ix = current->depth - 1; if (NULL == (sibling = gotcha_node_copy(current, systems->sz))) { return 0; } while (sibling->offsets[ix] < systems->arr[ix]->sz - 1) { sibling->offsets[ix] += 1; if (is_consistent(problem, sibling, systems)) { update_cardinality(problem, sibling); if (!priority_queue_push(opened, sibling)) { gotcha_node_free(sibling); return 0; } increase_int_counter("pushed"); maximize_int_counter("max", priority_queue_size(opened)); return 1; } increase_int_counter("inconsistent"); } gotcha_node_free(sibling); return 1; }
void dump_elements (HeapPriorityQueue *queue) { int i; unsigned int size = priority_queue_size(queue); for (i = 1; i <= size; ++i) { log("%u ", queue->elements[i].priority); } log("\n"); }
END_TEST START_TEST(test_priority_queue_initialization){ priority_queue_t* pq; priority_queue_initialize(&pq,int_constructor,int_destructor,int_comparator); ck_assert(pq->size == 0); ck_assert(pq->data == NULL); ck_assert(pq->capacity == 0); ck_assert(priority_queue_empty(pq)); ck_assert(priority_queue_size(pq)==0); priority_queue_delete(&pq); }
END_TEST START_TEST(test_priority_queue_crud){ priority_queue_t* pq; int* v = callocx(N,sizeof(int)); priority_queue_initialize(&pq,int_constructor,int_destructor,int_comparator); int i; for(i=0;i<N;i++){ v[i] = rand(); priority_queue_push(pq,&v[i]); } qsort(v,N,sizeof(int),int_comparator); ck_assert(priority_queue_size(pq)==N); for(i=0; !priority_queue_empty(pq); i++){ ck_assert(*(int*) priority_queue_front(pq) == v[i]); priority_queue_pop(pq); } free(v); priority_queue_delete(&pq); }
static void recursive_insert(lm_trie_t *trie, ngram_raw_t **raw_ngrams, uint32 *counts, int order) { uint32 unigram_idx = 0; uint32 *words; float *probs; const uint32 unigram_count = (uint32)counts[0]; priority_queue_t *ngrams = priority_queue_create(order, &ngram_ord_comparator); ngram_raw_ord_t *ngram; uint32 *raw_ngrams_ptr; int i; words = (uint32 *)ckd_calloc(order, sizeof(*words)); //for blanks catching probs = (float *)ckd_calloc(order - 1, sizeof(*probs)); //for blanks prob generating ngram = (ngram_raw_ord_t *)ckd_calloc(1, sizeof(*ngram)); ngram->order = 1; ngram->instance.words = &unigram_idx; priority_queue_add(ngrams, ngram); raw_ngrams_ptr = (uint32 *)ckd_calloc(order - 1, sizeof(*raw_ngrams_ptr)); for (i = 2; i <= order; ++i) { ngram_raw_ord_t *tmp_ngram = (ngram_raw_ord_t *)ckd_calloc(1, sizeof(*tmp_ngram)); tmp_ngram->order = i; raw_ngrams_ptr[i-2] = 0; tmp_ngram->instance = raw_ngrams[i - 2][raw_ngrams_ptr[i-2]]; priority_queue_add(ngrams, tmp_ngram); } for (;;) { ngram_raw_ord_t *top = (ngram_raw_ord_t *)priority_queue_poll(ngrams); if (top->order == 1) { trie->unigrams[unigram_idx].next = unigram_next(trie, order); words[0] = unigram_idx; probs[0] = trie->unigrams[unigram_idx].prob; if (++unigram_idx == unigram_count + 1) { ckd_free(top); break; } priority_queue_add(ngrams, top); } else { for (i = 0; i < top->order - 1; i++) { if (words[i] != top->instance.words[i]) { //need to insert dummy suffixes to make ngram of higher order reachable int j; assert(i > 0); //unigrams are not pruned without removing ngrams that contains them for (j = i; j < top->order - 1; j++) { middle_t *middle = &trie->middle_begin[j - 1]; bitarr_address_t address = middle_insert(middle, top->instance.words[j], j + 1, order); //calculate prob for blank float calc_prob = probs[j - 1] + trie->unigrams[top->instance.words[j]].bo; probs[j] = calc_prob; lm_trie_quant_mwrite(trie->quant, address, j - 1, calc_prob, 0.0f); } } } memcpy(words, top->instance.words, top->order * sizeof(*words)); if (top->order == order) { float *weights = top->instance.weights; bitarr_address_t address = longest_insert(trie->longest, top->instance.words[top->order - 1]); lm_trie_quant_lwrite(trie->quant, address, weights[0]); } else { float *weights = top->instance.weights; middle_t *middle = &trie->middle_begin[top->order - 2]; bitarr_address_t address = middle_insert(middle, top->instance.words[top->order - 1], top->order, order); //write prob and backoff probs[top->order - 1] = weights[0]; lm_trie_quant_mwrite(trie->quant, address, top->order - 2, weights[0], weights[1]); } raw_ngrams_ptr[top->order - 2]++; if (raw_ngrams_ptr[top->order - 2] < counts[top->order - 1]) { top->instance = raw_ngrams[top->order-2][raw_ngrams_ptr[top->order - 2]]; priority_queue_add(ngrams, top); } else { ckd_free(top); } } } assert(priority_queue_size(ngrams) == 0); priority_queue_free(ngrams, NULL); ckd_free(raw_ngrams_ptr); ckd_free(words); ckd_free(probs); }