Пример #1
0
int queue_insert_multiple_noorder(void) {
  // This function exercises the insert bubbling code paths

  priority_queue_t queue = priority_queue_create(realloc, 0);
  if (!queue) return 1;

  if (priority_queue_insert(queue, (void*)34567, 3) != 0) return 1;
  if (priority_queue_insert(queue, (void*)12345, 1) != 0) return 1;
  if (priority_queue_insert(queue, (void*)45678, 4) != 0) return 1;
  if (priority_queue_insert(queue, (void*)23456, 2) != 0) return 1;
  if (priority_queue_insert(queue, (void*)56789, 5) != 0) return 1;


  if (priority_queue_get_obj(queue) != (void*)12345) return 1;
  if (priority_queue_get_obj(queue) != (void*)23456) return 1;
  if (priority_queue_get_obj(queue) != (void*)34567) return 1;
  if (priority_queue_get_obj(queue) != (void*)45678) return 1;
  if (priority_queue_get_obj(queue) != (void*)56789) return 1;

  if (priority_queue_peek_pri(queue) != -1) return 1;

  priority_queue_free(queue, free);

  return 0;
}
Пример #2
0
int queue_insert_multiple_inorder(void) {
  // This function happens to exercise all of the remove bubbling code paths
  // (no children, 1 child, 2 children)

  priority_queue_t queue = priority_queue_create(realloc, 0);
  if (!queue) return 1;

  if (priority_queue_insert(queue, (void*)12345, 1) != 0) return 1;
  if (priority_queue_insert(queue, (void*)23456, 2) != 0) return 1;
  if (priority_queue_insert(queue, (void*)34567, 3) != 0) return 1;
  if (priority_queue_insert(queue, (void*)45678, 4) != 0) return 1;
  if (priority_queue_insert(queue, (void*)56789, 5) != 0) return 1;


  if (priority_queue_get_obj(queue) != (void*)12345) return 1;
  if (priority_queue_get_obj(queue) != (void*)23456) return 1;
  if (priority_queue_get_obj(queue) != (void*)34567) return 1;
  if (priority_queue_get_obj(queue) != (void*)45678) return 1;
  if (priority_queue_get_obj(queue) != (void*)56789) return 1;

  if (priority_queue_peek_pri(queue) != -1) return 1;

  priority_queue_free(queue, free);

  return 0;
}
Пример #3
0
level *level_create(unsigned n, unsigned p, unsigned max_k)
{
	//make sure max_k is sane
	if(max_k < 2 || max_k > (n - 1))
		return NULL;
	level *ret = malloc(sizeof(level));
	ret->n = n;
	ret->p = p;
	ret->max_k = max_k;
	ret->min_m = n - 1;
	ret->num_m = (n * max_k / 2) - ret->min_m + 1;
	
	ret->sets = malloc(ret->num_m * sizeof(hash_set*));
	ret->queues = malloc(ret->num_m * sizeof(priority_queue*));
	
	for(int i = 0; i < ret->num_m; i++)
	{
		ret->sets[i] = hash_set_create(P * 3 / 2, nauty_hash, nauty_compare,
									   nauty_delete);
		ret->queues[i] = priority_queue_create(graph_compare_gt,
											   graph_delete);
	}
	
	return ret;
}
Пример #4
0
char *test_insert()
{
	priority_queue_p queue = priority_queue_create(sizeof(int), compare_intp);	

	priority_queue_insert(queue, test_data(10));
	mu_assert(*(int*)vector_get(queue->vector, 0) == 10, "should have set first item");	

	priority_queue_free(queue);
	return NULL;
}
Пример #5
0
char *test_create()
{
	priority_queue_p queue = priority_queue_create(sizeof(int), compare_intp);	
	
	mu_assert(queue != NULL, "should have created queue");
	mu_assert(queue->vector != NULL, "should have created vector");
	mu_assert(queue->comparator != NULL, "should have set comparator");

	priority_queue_free(queue);
	return NULL;
}
Пример #6
0
int queue_insert_one(void) {
  priority_queue_t queue = priority_queue_create(realloc, 0);
  if (!queue) return 1;

  if (priority_queue_insert(queue, (void*)12345, 1) != 0) return 1;

  if (priority_queue_peek_pri(queue) != 1) return 1;
  if (priority_queue_peek_obj(queue) != (void*)12345) return 1;

  priority_queue_free(queue, free);

  return 0;
}
Пример #7
0
/* TODO do random inserts and deletes */
int main(void)
{
    float numbers[8] = {0.5f, 1.0f, 0.1f, 10.0f, 5.0f, 0.3f, 0.0f, 42.0f};
    int ordered[8] = {6, 2, 5, 0, 1, 4, 3, 7};

    struct PriorityQueue *queue = priority_queue_create();
    
    for (int i = 0; i < 8; ++i)
        priority_queue_insert(queue, i, numbers[i]);

    for (int i = 0; priority_queue_length(queue); ++i) {
        int id;
        float value;
        priority_queue_minimum(queue, &id, &value);
        assert(ordered[i] == id);
        assert(numbers[ordered[i]] == value);

        priority_queue_extract_min(queue);
    }

    /* worst case test */
    for (int i = 10000000; i >= 0; --i)
        priority_queue_insert(queue, 10000000 - i, i);
    
    /* should be sorted */
    int previous = -1;
    for (int i = 0; i < 10000000; ++i) {
        int id;
        float value;
        priority_queue_minimum(queue, &id, &value);
        priority_queue_extract_min(queue);

        if (previous > value)
            printf("%g[%d] ", value, id);
        previous = value;
    }
    printf("\n"); 

    priority_queue_destroy(queue);

    return 0;
}
Пример #8
0
int queue_insert_resize(void) {
  priority_queue_t queue = priority_queue_create(realloc, 1);
  if (!queue) return 1;

  if (priority_queue_insert(queue, (void*)34567, 3) != 0) return 1;
  if (priority_queue_insert(queue, (void*)12345, 1) != 0) return 1;
  if (priority_queue_insert(queue, (void*)45678, 4) != 0) return 1;
  if (priority_queue_insert(queue, (void*)23456, 2) != 0) return 1;
  if (priority_queue_insert(queue, (void*)56789, 5) != 0) return 1;


  if (priority_queue_get_obj(queue) != (void*)12345) return 1;
  if (priority_queue_get_obj(queue) != (void*)23456) return 1;
  if (priority_queue_get_obj(queue) != (void*)34567) return 1;
  if (priority_queue_get_obj(queue) != (void*)45678) return 1;
  if (priority_queue_get_obj(queue) != (void*)56789) return 1;

  if (priority_queue_peek_pri(queue) != -1) return 1;

  priority_queue_free(queue, free);

  return 0;
}
Пример #9
0
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);
}