Exemple #1
0
Fichier : heap.c Projet : kcyeu/xcb
static unsigned bubble_up(heap_t heap, unsigned long i) {
	while (i > 1 && heap->cmp(heap_get(heap, parent_node(i)), heap_get(heap, i)) < 0) {
		heap_swap(heap, parent_node(i), i);
		i = parent_node(i);
	}
	return i;
}
Exemple #2
0
static inline void max_heapify(struct ast_heap *h, int i)
{
	for (;;) {
		int l = left_node(i);
		int r = right_node(i);
		int max;

		if (l <= h->cur_len && h->cmp_fn(heap_get(h, l), heap_get(h, i)) > 0) {
			max = l;
		} else {
			max = i;
		}

		if (r <= h->cur_len && h->cmp_fn(heap_get(h, r), heap_get(h, max)) > 0) {
			max = r;
		}

		if (max == i) {
			break;
		}

		heap_swap(h, i, max);

		i = max;
	}
}
Exemple #3
0
Fichier : heap.c Projet : kcyeu/xcb
static void heap_swap(heap_t heap, unsigned long i, unsigned long j) {
	void *tmp;

	tmp = heap_get(heap, i);
	heap_set(heap, i, heap_get(heap, j));
	heap_set(heap, j, tmp);
}
Exemple #4
0
static inline void heap_swap(struct ast_heap *h, int i, int j)
{
	void *tmp;

	tmp = heap_get(h, i);
	heap_set(h, i, heap_get(h, j));
	heap_set(h, j, tmp);
}
Exemple #5
0
static int bubble_up(struct ast_heap *h, int i)
{
	while (i > 1 && h->cmp_fn(heap_get(h, parent_node(i)), heap_get(h, i)) < 0) {
		heap_swap(h, i, parent_node(i));
		i = parent_node(i);
	}

	return i;
}
Exemple #6
0
Fichier : heap.c Projet : kcyeu/xcb
static void *_heap_remove(heap_t heap, unsigned long i) {
	void *ret;

	if (i > heap->curr)
		return NULL;
	ret = heap_get(heap, i);
	heap_set(heap, i, heap_get(heap, heap->curr--));
	i = bubble_up(heap, i);
	max_heapify(heap, i);
	return ret;
}
Exemple #7
0
Fichier : heap.c Projet : kcyeu/xcb
static void max_heapify(heap_t heap, unsigned long i) {
	for (;;) {
		unsigned long l = left_node(i), r = right_node(i), max;

		max = l <= heap->curr && heap->cmp(heap_get(heap, l), heap_get(heap, i)) > 0
			? l : i;
		if (r <= heap->curr && heap->cmp(heap_get(heap, r), heap_get(heap, max)) > 0)
			max = r;
		if (max == i)
			break;
		heap_swap(heap, i, max);
		i = max;
	}
}
Exemple #8
0
static void *_ast_heap_remove(struct ast_heap *h, unsigned int index)
{
	void *ret;

	if (!index || index > h->cur_len) {
		return NULL;
	}

	ret = heap_get(h, index);
	heap_set(h, index, heap_get(h, (h->cur_len)--));
	index = bubble_up(h, index);
	max_heapify(h, index);

	return ret;
}
Exemple #9
0
Fichier : heap.c Projet : kcyeu/xcb
void *heap_peek(heap_t heap, unsigned long index) {
	if (heap == NULL)
		return NULL;
	if (heap->curr == 0 || index == 0 || index > heap->curr)
		return NULL;
	return heap_get(heap, index);
}
Exemple #10
0
void test_heapify(void) {
  size_t i;
  /* initialize data */
  int *array = zmalloc(sizeof(int) * LOTS_OF_INTS);
  int sum = 0;
  for (i = 0; i < LOTS_OF_INTS; i++) {
    array[i] = random() % 123456789;
    sum += array[i];
  }

  /* construct a heap from our random array */
  heap_t *h = heap_heapify(array, LOTS_OF_INTS, sizeof(int),
                           NULL, (cmp_func_t)intcmp);

  assert(heap_size(h) == LOTS_OF_INTS);
  int ctrlsum = 0, maxctrl, *e;
  /* peek head */
  if (heap_size(h)) maxctrl = *(int*)heap_get(h, 0);
  while (heap_size(h)) {
    e = heap_pop(h); ctrlsum += *e;
    /* check that elements are in heap order */
    assert(*e <= maxctrl); maxctrl = *e;
  }
  assert(ctrlsum == sum);
  heap_destroy(h);
  free(array);
}
Exemple #11
0
/* get the best cell from the heap */
static void TCOD_path_get_cell(TCOD_path_data_t *path, int *x, int *y, float *distance) {
	uint32 offset = heap_get(path,path->heap);
	*x=(offset % path->w);
	*y=(offset / path->w);
	*distance=path->grid[offset];
//printf ("get cell : %d %d %g\n",*x,*y,*distance);
}
Exemple #12
0
void generate_test_heap(void) {
  size_t n = 0, i, j;
  int sum = 0, *e=NULL;
  heap_t *h = heap_init(free, (cmp_func_t)intcmp);

  for (i = 0; i < 1000; i++) {
    switch (random() % 3) {
      case 0:
      case 2:
        e = zmalloc(sizeof(int)); *e = random() % 123456789;
        n++; sum += *e;
        heap_insert(h, e);
        break;
      case 1:
        e = heap_pop(h);
        if (e) { n--; sum -= *e; free(e); }
        break;
    }

    /* check propper auto resize */
    assert(heap_capacity(h) >= heap_size(h));
    for (j = 0; j < heap_size(h); j++) {
      size_t left = heap_child_l(j);
      size_t right = heap_child_r(j);
      size_t greatest = heap_greatest_child(h, j);

      /* check for heap order is preserved and greatest child is correct */
      if (heap_size(h) > right) {
        assert(*(int*)heap_get(h, j) >= *(int*)heap_get(h, right));
        assert(*(int*)heap_get(h, greatest) >= *(int*)heap_get(h, right));
      }
      if (heap_size(h) > left) {
        assert(*(int*)heap_get(h, j) >= *(int*)heap_get(h, left));
        assert(*(int*)heap_get(h, greatest) >= *(int*)heap_get(h, left));
      }
    }
  }

  assert(heap_size(h) == n);
  int ctrlsum = 0, maxctrl;
  if (heap_size(h)) maxctrl = *(int*)heap_get(h, 0);
  while (heap_size(h)) {
    e = heap_pop(h); ctrlsum += *e;
    /* check that elements are in heap order */
    assert(*e <= maxctrl); maxctrl = *e;
    free(e);
  }
  assert(ctrlsum == sum);

  heap_destroy(h);
}
Exemple #13
0
void *ast_heap_peek(struct ast_heap *h, unsigned int index)
{
	if (!h->cur_len || !index || index > h->cur_len) {
		return NULL;
	}

	return heap_get(h, index);
}
Exemple #14
0
// will use vector_set rather than remove and insert for effieciency reasons.
VALUE_TYPE extractMinHeapMinimum(Heap *heap){
	HeapNode* top = vector_get(0, heap->vector);
	vector_set(0, heap_get(heap->size-1, heap), heap->vector);
	vector_removeIndex(heap->size-1, heap->vector);
	heap->size--;
	heap_minHeapify(0, heap);
	return top->value;
}
Exemple #15
0
static void __test_head_add_gt(struct heap *h, uint32_t v) {
    if( !heap_full(h) ) {
        heap_add(h, &v);
    } else {
        uint32_t *min = heap_get(h);
        if( *min < v ) {
            heap_pop(h);
            heap_add(h, &v);
        }
    }
}
Exemple #16
0
void heap_bubble_up(Heap* heap, int index) {
  if(index == 0) {
    return;
  }

  int compare = heap->comparator(heap_get(heap, index), heap_parent(heap, index));
  if(compare <= 0) {
    heap_swap(heap, index, heap_parent_index(index));
    heap_bubble_up(heap, heap_parent_index(index));
  }
}
Exemple #17
0
void test_heap_test_4(void) {

    size_t memsize = heap_mem_size(1, sizeof(struct cw));

    fprintf(stderr, "memsize: %ld\n", memsize);

    char heap_mem[heap_mem_size(1, sizeof(struct cw))];
    struct heap *h = heap_create( heap_mem
                                , sizeof(heap_mem)
                                , sizeof(struct cw)
                                , __cw_leq
                                , __cw_cpy );

    fprintf(stderr, "heap: %p\n", h);

    fprintf(stdout, "# heap_size: %ld\n", heap_size(h));

    struct cw cats[] = { {  1, 1 }
                       , {  2, 1 }
                       , {  1, 2 }
                       , {  3, 1 }
                       , { 12, 3 }
                       , {  5, 1 }
                       , { 31, 2 }
                       , {  6, 2 }
                       , {  7, 1 }
                       , {  7, 1 }
                       , { 10, 5 }
                       };

    fprintf(stdout, "\n");


    size_t i = 0;
    for(; i < sizeof(cats)/sizeof(cats[0]); i++ ) {
        fprintf(stdout, "# {%d, %d}\n", cats[i].cat, cats[i].weight);
        if( heap_full(h) ) {
            struct cw *min = heap_get(h);
            if( __cw_leq(min, &cats[i]) ) {
                heap_pop(h);
            }
        }
        heap_add(h, &cats[i]);
    }

    fprintf(stdout, "\nheap_items %ld\n", heap_items(h));

    fprintf(stdout, "\n");
    while( !heap_empty(h) ) {
        struct cw *c = heap_pop(h);
        fprintf(stdout, "# {%d, %d}\n", c->cat, c->weight);
    }
}
Exemple #18
0
void heap_bubble_down(Heap* heap, int index) {
  if(heap_left_child_index(index) <= heap_last_index(heap)) {
    int left_compare = heap->comparator(heap_get(heap, index), heap_left_child(heap, index));
    if(1 == left_compare) {
      heap_swap(heap, index, heap_left_child_index(index));
      heap_bubble_down(heap, heap_left_child_index(index));
      return;
    } 
  }
  
  if(heap_right_child_index(index) <= heap_last_index(heap)) {
    int right_compare = heap->comparator(heap_get(heap, index), heap_right_child(heap, index));
    if(1 == right_compare) {
      heap_swap(heap, index, heap_right_child_index(index));
      heap_bubble_down(heap, heap_right_child_index(index));
      return;
    }
  }

  return;
}
Exemple #19
0
int hb_write(buffer_t * hb, void * data, int priority){
    assert(hb->type == HEAP_BUFFER);
    if(rb_available(hb) > 0){
        heap_node_t * n = (heap_node_t*)heap_get(hb, hb->used);
        n->priority = priority;
        memmove(n->value, data, hb->size - sizeof(int));
        int i = hb->used;
        hb->used++;
        while(i > 0){
            int j = (i - 1)/2;
            heap_node_t * pn = (heap_node_t*)heap_get(hb, j);
            if(n->priority > pn->priority){
                __hb_swap(hb, n, pn);
                i = j;
                n = pn;
            }else
                break;
        }
        return 1;
    }
    return 0;
}
Exemple #20
0
int ast_heap_push(struct ast_heap *h, void *elm)
#endif
{
	int i;

	if (h->cur_len == h->avail_len && grow_heap(h
#ifdef MALLOC_DEBUG
		, file, lineno, func
#endif
		)) {
		return -1;
	}

	heap_set(h, ++(h->cur_len), elm);

	for (i = h->cur_len;
			i > 1 && h->cmp_fn(heap_get(h, parent_node(i)), heap_get(h, i)) < 0;
			i = parent_node(i)) {
		heap_swap(h, i, parent_node(i));
	}

	return 0;
}
Exemple #21
0
int ast_heap_verify(struct ast_heap *h)
{
	unsigned int i;

	for (i = 1; i <= (h->cur_len / 2); i++) {
		int l = left_node(i);
		int r = right_node(i);

		if (l <= h->cur_len) {
			if (h->cmp_fn(heap_get(h, i), heap_get(h, l)) < 0) {
				return -1;
			}
		}

		if (r <= h->cur_len) {
			if (h->cmp_fn(heap_get(h, i), heap_get(h, r)) < 0) {
				return -1;
			}
		}
	}

	return 0;
}
Exemple #22
0
void add_dictionary_entry(){
  char *verb;
  word_t words;
  verb = (stack_pop(&data_stack)).char_ptr;
  words = stack_pop(&data_stack);
  //printf("adding %s\n",verb);
  int l = strlen(verb);
  char* chars = (char *)heap_get(l+1);
  strcpy(chars,verb);
  int index = hash(verb) % 100;
  while(dictionary[index].verb != NULL && strcmp(dictionary[index].verb,verb) ){
    index++;
    if(index == 100) index = 0;
  }
  dictionary[index].verb = chars;
  dictionary[index].words = words.ptr;
}
Exemple #23
0
int hb_take(buffer_t * hb, void * data){
    assert(hb->type == HEAP_BUFFER);
    if(rb_has_next(hb) > 0){
        heap_node_t * n = (heap_node_t*)heap_get(hb, 0);
        memmove(data, n->value, hb->size - sizeof(int));
        hb->used--;
        heap_node_t * ln = (heap_node_t*)heap_get(hb, hb->used);
        __hb_swap(hb, ln, n);
        unsigned int i = 0;
        while(1){
            unsigned int j = 2*i + 1;
            unsigned int k = 2*i + 2;
            heap_node_t * sn;
            if(j < hb->used && k < hb->used){
                int pj, pk;
                pj = *(int*)heap_get(hb, j);
                pk = *(int*)heap_get(hb, k);
                if(pj > pk){
                    sn = (heap_node_t*)heap_get(hb, j);
                    i = j;
                }else{
                    sn = (heap_node_t*)heap_get(hb, k);
                    i = k;
                }
            }else if(j < hb->used){
                sn = (heap_node_t*)heap_get(hb, j);
                i = j;
            }else if (k < hb->used){
                sn = (heap_node_t*)heap_get(hb, k);
                i = k;
            }else
                break;
            if(n->priority < sn->priority){
                __hb_swap(hb, n, sn); 
                n = sn;
            }else
                break;
        }
        return 1;
    }
    return 0;
}
Exemple #24
0
int heap_next (char  **key, 
               DESCR **data)
{
    int 
        rc = HEAP_OK;
    char
        *file_name = NULL,
        *temp_key;

    ASSERT (key);

    if ((! file_list)
    ||  (! file_info))
        return HEAP_DATA_NOT_FOUND;

    if (file_info == (FILEINFO *) file_list)
      {
        free_dir_list (file_list);
        file_list = NULL;
        file_info = NULL;

        return HEAP_DATA_NOT_FOUND;
      }

    file_name = file_info-> dir. file_name;
    file_info = file_info-> next;

    temp_key = mem_strdup (file_name);
    
    rc = heap_get (temp_key, data);

    if (rc)
        mem_free (temp_key);
    else
        *key = temp_key;
    
    return rc;
}
Exemple #25
0
void* heap_parent(Heap* heap, unsigned int index) {
  return heap_get(heap, heap_parent_index(index));
}
Exemple #26
0
/* get the best cell from the heap */
static void TCOD_path_get_cell(TCOD_path_data_t *path, int *x, int *y, float *distance) {
	uint32 offset = heap_get(path,path->heap);
	*x=(offset % path->w);
	*y=(offset / path->w);
	*distance=path->grid[offset];
}
Exemple #27
0
void* heap_min(Heap* heap) {
  return heap_get(heap, 0);
}
Exemple #28
0
void* heap_right_child(Heap* heap, unsigned int index) {
  return heap_get(heap, heap_right_child_index(index));
}
Exemple #29
0
void* heap_last(Heap* heap) {
  return heap_get(heap, heap_last_index(heap));
}
Exemple #30
0
void heap_swap(Heap* heap, int index0, int index1) {
  void* temp = heap_get(heap, index0);
  heap_set(heap, index0, heap_get(heap, index1));
  heap_set(heap, index1, temp);
}