Example #1
0
int
heap_insert(struct dn_heap *h, uint64_t key1, void *p)
{
    int son = h->elements;

    //log("%s key %llu p %p\n", __FUNCTION__, key1, p);
    if (p == NULL) { /* data already there, set starting point */
        son = key1;
    } else { /* insert new element at the end, possibly resize */
        son = h->elements;
        if (son == h->size) /* need resize... */
            // XXX expand by 16 or so
            if (heap_resize(h, h->elements+16) )
                return 1; /* failure... */
        h->p[son].object = p;
        h->p[son].key = key1;
        h->elements++;
    }
    /* make sure that son >= father along the path */
    while (son > 0) {
        int father = HEAP_FATHER(son);
        struct dn_heap_entry tmp;

        if (DN_KEY_LT( h->p[father].key, h->p[son].key ) )
            break; /* found right position */
        /* son smaller than father, swap and repeat */
        HEAP_SWAP(h->p[son], h->p[father], tmp);
        SET_OFFSET(h, son);
        son = father;
    }
    SET_OFFSET(h, son);
    return 0;
}
/* note that heap_free does not deallocate anything referenced by the items */
static void heap_free(heap *h)
{
     h->n = 0;
     heap_resize(h, 0);
     h->fdim = 0;
     free(h->ee);
}
static int heap_push(heap *h, heap_item hi)
{
     int insert;
     unsigned i, fdim = h->fdim;

     for (i = 0; i < fdim; ++i) {
	  h->ee[i].val += hi.ee[i].val;
	  h->ee[i].err += hi.ee[i].err;
     }
     insert = h->n;
     if (++(h->n) > h->nalloc) {
	  heap_resize(h, h->n * 2);
	  if (!h->items) return FAILURE;
     }

     while (insert) {
	  int parent = (insert - 1) / 2;
	  if (KEY(hi) <= KEY(h->items[parent]))
	       break;
	  h->items[insert] = h->items[parent];
	  insert = parent;
     }
     h->items[insert] = hi;
     return SUCCESS;
}
Example #4
0
/*
 * Function: int bh_push( Bh *h, void *in )
 * Description: push one item into bh
 * Input:  h: the bh object
 *         in: the item
 * Output: none
 * Return: int:
 *			0: succeed
 *		   -1: failed
 * Others: none
 */
int bh_push( Bh *h, void *in )
{
	int				rc = 0;
	unsigned int	he;

	if( ( rc = heap_resize( h ) ) )
	{
		goto DONE;
	}


	/*
	   root is 1, not 0, to do the same shift operation to get parent
	   for left and right children
	 */
	h->heap_end++;
	he			   = h->heap_end;
	h->heap[he]	   = in;

	siftup( h );

	bh_info( h );

DONE:
	return rc;
}
Example #5
0
int cp_heap_push(cp_heap *h, void *in)
{
    int rc = 0;
    unsigned int he;

    if ((rc = cp_heap_txlock(h))) return rc;

    he = h->heap_end;

#ifdef _HEAP_TRACE
    DEBUGMSG("cp_heap_push: %p\n", in);
    heap_info(h);
#endif
    if ((rc = heap_resize(h))) goto DONE;
    if ((h->mode & COLLECTION_MODE_COPY) && h->copy)
        h->heap[he] = (*h->copy)(in);
    else
        h->heap[he] = in;
    siftup(h);
#ifdef _HEAP_TRACE
    DEBUGMSG("cp_heap_push end\n");
    heap_info(h);
#endif
DONE:
    cp_heap_txunlock(h);
    return rc;
}
Example #6
0
int
heap_init(struct dn_heap *h, int size, int ofs)
{
    if (heap_resize(h, size))
        return 1;
    h->elements = 0;
    h->ofs = ofs;
    return 0;
}
Example #7
0
void
heap_insert(heap_t h, void *elem)
{
	if (!elem)
		return;

	if (h->next >= h->treesz)
		heap_resize(h, h->treesz*2);
	
	h->tree[h->next] = elem;
	h->count++;
	upheap(h, h->next++);
}
Example #8
0
void heap_insert(Heap* heap, void* value) {
  if(heap_capacity(heap) * HEAP_LOAD_FACTOR <= heap_size(heap)) {
    heap_resize(heap);
  }

  unsigned int next_index = heap_last_index(heap) + 1;

  heap_set(heap, next_index, value);

  heap->size++;
  
  heap_bubble_up(heap, next_index);
}
static heap heap_alloc(size_t nalloc, unsigned fdim)
{
     heap h;
     unsigned i;
     h.n = 0;
     h.nalloc = 0;
     h.items = 0;
     h.fdim = fdim;
     h.ee = (esterr *) malloc(sizeof(esterr) * fdim);
     if (h.ee) {
	  for (i = 0; i < fdim; ++i) h.ee[i].val = h.ee[i].err = 0;
	  heap_resize(&h, nalloc);
     }
     return h;
}
Example #10
0
int
heap_insert(heap_context ctx, void *elt) {
	int i;

	if (ctx == NULL || elt == NULL) {
		errno = EINVAL;
		return (-1);
	}

	i = ++ctx->heap_size;
	if (ctx->heap_size >= ctx->array_size && heap_resize(ctx) < 0)
		return (-1);
	
	float_up(ctx, i, elt);

	return (0);
}