Example #1
0
void heap_heapify_down(heap* h) {
    unsigned int pos = 0, 
                 newpos,
                 temp;
	node** graph_nodes;

	graph_nodes = &h->g->nodes;
	
    while(1) {
        if(HEAP_LEFT(pos) <= h->count) { 
            if(HEAP_RIGHT(pos) <= h->count) {
                newpos = (*h->compare)(h->g, h->nodes[HEAP_RIGHT(pos)], h->nodes[HEAP_LEFT(pos)]) < 0 ? HEAP_RIGHT(pos) : HEAP_LEFT(pos);
            } else {
                newpos = HEAP_LEFT(pos);
            }
        } else {
            break; 
        }
        
        if((*h->compare)(h->g, h->nodes[pos], h->nodes[newpos]) > 0) {
            temp = h->nodes[pos];
			(*graph_nodes)[temp].heap_index = newpos;
			(*graph_nodes)[h->nodes[newpos]].heap_index = pos;
            h->nodes[pos] = h->nodes[newpos];
            h->nodes[newpos] = temp;
        } else {
            break; /* A posição está correta*/
        }
    }
}
Example #2
0
static inline void
heap_sort_percolate_down(void *base, uint32 size, uint32 csize, uint32 index,
    sint32 (*compare)(const void *, const void *))
{
    uint32 i;
    void *tmp;
    uint32 child;

    assert_exit(sort_parameters_legal_p(base, size, csize, compare));
    assert_exit(index < size);

    tmp = memory_cache_allocate(csize);
    sort_cell_copy(tmp, base + index * csize, csize);

    i = index;
    while (HEAP_LEFT(i) < size) {
        child = HEAP_LEFT(i);
        if (child != size - 1
            && compare(base + child * csize, base + (child + 1) * csize) < 0) {
            child++;
        }

        if (compare(tmp, base + child * csize) < 0) {
            sort_cell_copy(base + i * csize, base + child * csize, csize);
        } else {
            break;
        }
        i = child;
    }

    sort_cell_copy(base + i * csize, tmp, csize);
    memory_cache_free(tmp);
}
Example #3
0
/*
 *	Remove the top element, or object.
 */
int fr_heap_extract(fr_heap_t *hp, void *data)
{
	int child, parent;
	int max;

	if (!hp || (hp->num_elements == 0)) return 0;

	max = hp->num_elements - 1;

	/*
	 *	Extract element.  Default is the first one.
	 */
	if (!data) {
		parent = 0;

	} else {		/* extract from the middle */
		if (!hp->offset) return 0;

		parent = *((int *)(((uint8_t *)data) + hp->offset));

		/*
		 *	Out of bounds.
		 */
		if (parent < 0 || parent >= hp->num_elements) return 0;
	}

	RESET_OFFSET(hp, parent);
	child = HEAP_LEFT(parent);
	while (child <= max) {
		/*
		 *	Maybe take the right child.
		 */
		if ((child != max) &&
		    (hp->cmp(hp->p[child + 1], hp->p[child]) < 0)) {
			child = child + 1;
		}
		hp->p[parent] = hp->p[child];
		SET_OFFSET(hp, parent);
		parent = child;
		child = HEAP_LEFT(child);
	}
	hp->num_elements--;

	/*
	 *	We didn't end up at the last element in the heap.
	 *	This element has to be re-inserted.
	 */
	if (parent != max) {
		/*
		 *	Fill hole with last entry and bubble up,
		 *	reusing the insert code
		 */
		hp->p[parent] = hp->p[max];
		return fr_heap_bubble(hp, parent);
	}

	return 1;
}
Example #4
0
void
heap_heapify(heap_t* h, int i)
{
    int l, r;
    int largest;
    double tmp;
    double* tmp_data; // FIXME: void*

    if (i < h->len/2) {
        l = HEAP_LEFT(i);
        r = HEAP_RIGHT(i);

        if((l < h->len) && (h->A[l] > h->A[i]))
            largest = l;
        else
            largest = i;

        if((r < h->len) && (h->A[r] > h->A[largest]))
            largest = r;

        if(largest != i)
        {
            tmp = h->A[i];
            tmp_data = h->data[i];
            h->A[i] = h->A[largest];
            h->data[i] = h->data[largest];
            h->A[largest] = tmp;
            h->data[largest] = tmp_data;
            heap_heapify(h,largest);
        }
    }
}
Example #5
0
/*
 * change object position and update references
 * XXX this one is never used!
 */
static void
heap_move(struct dn_heap *h, uint64_t new_key, void *object)
{
    int temp, i, max = h->elements-1;
    struct dn_heap_entry *p, buf;

    if (h->ofs <= 0)
        panic("cannot move items on this heap");
    p = h->p;	/* shortcut */

    i = *((int *)((char *)object + h->ofs));
    if (DN_KEY_LT(new_key, p[i].key) ) { /* must move up */
        p[i].key = new_key;
        for (; i>0 &&
                DN_KEY_LT(new_key, p[(temp = HEAP_FATHER(i))].key);
                i = temp ) { /* bubble up */
            HEAP_SWAP(p[i], p[temp], buf);
            SET_OFFSET(h, i);
        }
    } else {		/* must move down */
        p[i].key = new_key;
        while ( (temp = HEAP_LEFT(i)) <= max ) {
            /* found left child */
            if (temp != max &&
                    DN_KEY_LT(p[temp+1].key, p[temp].key))
                temp++; /* select child with min key */
            if (DN_KEY_LT(>p[temp].key, new_key)) {
                /* go down */
                HEAP_SWAP(p[i], p[temp], buf);
                SET_OFFSET(h, i);
            } else
                break;
            i = temp;
        }
    }
Example #6
0
static void reheap_down(pj_timer_heap_t *ht, pj_timer_entry *moved_node,
                        size_t slot, size_t child)
{
    PJ_CHECK_STACK();

    // Restore the heap property after a deletion.
    
    while (child < ht->cur_size)
    {
	// Choose the smaller of the two children.
	if (child + 1 < ht->cur_size
	    && PJ_TIME_VAL_LT(ht->heap[child + 1]->_timer_value, ht->heap[child]->_timer_value))
	    child++;
	
	// Perform a <copy> if the child has a larger timeout value than
	// the <moved_node>.
	if (PJ_TIME_VAL_LT(ht->heap[child]->_timer_value, moved_node->_timer_value))
        {
	    copy_node( ht, slot, ht->heap[child]);
	    slot = child;
	    child = HEAP_LEFT(child);
        }
	else
	    // We've found our location in the heap.
	    break;
    }
    
    copy_node( ht, slot, moved_node);
}
Example #7
0
/**
 * @brief CSLR MAX-HEAPIFY
 *
 * This routine creates a max-heap for the subtree rooted at index i. The
 * index is 0 based.
 *
 * @param[in] h The heap to operate on
 * @param[in] i The (0-based) subtree index
 * @return HEAP_ERR_E
 */
HEAP_ERR_E heap_max_heapify (HEAP_T* h, unsigned long i)
{
   unsigned long l = HEAP_LEFT(i);
   unsigned long r = HEAP_RIGHT(i);
   unsigned long largest;

   //fprintf (stdout, "i=%lu; l=%lu; r=%lu\n", i, l, r);
   if (h->type == DS_HEAP_MIN)
      return HEAP_ERR_WRONG_TYPE;
   
   if (l < h->heap_size && (HEAP_KEY(h, l) > HEAP_KEY(h, i)))
      largest = l;
   else
      largest = i;
   
   if (r < h->heap_size && (HEAP_KEY(h, r) > HEAP_KEY(h, largest)))
      largest = r;

   if (largest != i)
   {
      HEAP_SWAP_NODES(i,largest);
      heap_max_heapify (h, largest);
   }
   /* todo: set was_heapified to true */
   return HEAP_ERR_OK;
}
static void
heapify(plane_t *h, uint32 index, uint32 size) {

	uint32 c;
	plane_t tmp;
	for (c = HEAP_LEFT(index); c < (size-1); 
			index = c, c = HEAP_LEFT(index)) {

		if (h[c].plane.score > h[c+1].plane.score)
			c++;

		if (h[index].plane.score > h[c].plane.score) {
			HEAP_SWAP(h[index], h[c]);
		}
		else {
			return;
		}
	}
	if (c == (size-1) && h[index].plane.score > h[c].plane.score) {
		HEAP_SWAP(h[index], h[c]);
	}
}
Example #9
0
/**
 * @brief graphviz description plugin for heap ds.
 *
 * @param[in] h The heap to operate in
 * @param[in] filename The file to write the graphviz description to.
 * @return HEAP_ERR_E
 */
HEAP_ERR_E heap_graphviz_description
(
HEAP_T* h,
char* filename
)
{
   FILE* fp;
   unsigned long idx = 0;
   unsigned long weight;
   if (NULL == filename)
   {
      fp = stderr;
   }
   else
   {
      fp = fopen (filename, "w");
      if (NULL == fp)
         return HEAP_ERR_ERR;
   }
   
   fprintf (fp, "graph G {\n");
   fprintf (fp, "node [shape = circle, style=filled, color=\"sienna2\"];\n");
   fprintf (fp, "size=\"12,8\"\n");

   weight = h->heap_size;
   if (HEAP_LEFT(idx) < h->heap_size)
      fprintf (fp, "\t%lu -- %lu [headlabel=\"%lu\", weight=%lu];\n ",
               HEAP_KEY(h, idx),
               HEAP_KEY(h, HEAP_LEFT(idx)),
               HEAP_LEFT(idx),
               weight);
   if (HEAP_RIGHT(idx) < h->heap_size)
      fprintf (fp, "\t%lu -- %lu [headlabel=\"%lu\", weight=%lu];\n",
               HEAP_KEY(h, idx),
               HEAP_KEY(h, HEAP_RIGHT(idx)),
               HEAP_RIGHT(idx),
               weight);
   
   for (idx = 1; idx < h->heap_size; idx++)
   {
      if (HEAP_LEFT(idx) < h->heap_size)
         fprintf (fp, "\t%lu -- %lu [headlabel = \"%lu\"]; \n ",
                  HEAP_KEY(h, idx),
                  HEAP_KEY(h, HEAP_LEFT(idx)),
                  HEAP_LEFT(idx));
      if (HEAP_RIGHT(idx) < h->heap_size)
         fprintf (fp, "\t%lu -- %lu [headlabel = \"%lu\"];\n",
                  HEAP_KEY(h, idx),
                  HEAP_KEY(h, HEAP_RIGHT(idx)),
                  HEAP_RIGHT(idx));
   }
   
   fprintf (fp, "}\n");

   if (NULL != filename)
      fclose (fp);
   
   return HEAP_ERR_OK;
}
Example #10
0
void heap_dump(const heap* h, const unsigned int from) {
    if(from == 0) {
        printf("digraph g {\nnode [shape = record,height=.1];\n");
    }
    if(from < h->count) {
        printf("node%d[ label = \"<f0> | <f1> %s | <f2>\"];\n", from, h->g->nodes[h->nodes[from]].value);

        if(HEAP_LEFT(from) < h->count) {
            printf("\"node%d\":f0 -> \"node%d\":f1\n", from, HEAP_LEFT(from));
            heap_dump(h, HEAP_LEFT(from));
        }
    
        if(HEAP_RIGHT(from) < h->count) {
            printf("\"node%d\":f2 -> \"node%d\":f1\n", from, HEAP_RIGHT(from));
            heap_dump(h, HEAP_RIGHT(from));
        }
    }
    
    if(from == 0) { 
        printf("}\n");
    }
}
Example #11
0
/*
 * remove top element from heap, or obj if obj != NULL
 */
void
heap_extract(struct dn_heap *h, void *obj)
{
    int child, father, max = h->elements - 1;

    if (max < 0) {
        printf("--- %s: empty heap 0x%p\n", __FUNCTION__, h);
        return;
    }
    if (obj == NULL)
        father = 0; /* default: move up smallest child */
    else { /* extract specific element, index is at offset */
        if (h->ofs <= 0)
            panic("%s: extract from middle not set on %p\n",
                  __FUNCTION__, h);
        father = *((int *)((char *)obj + h->ofs));
        if (father < 0 || father >= h->elements) {
            panic("%s: father %d out of bound 0..%d\n",
                  __FUNCTION__, father, h->elements);
        }
    }
    /*
     * below, father is the index of the empty element, which
     * we replace at each step with the smallest child until we
     * reach the bottom level.
     */
    // XXX why removing RESET_OFFSET increases runtime by 10% ?
    RESET_OFFSET(h, father);
    while ( (child = HEAP_LEFT(father)) <= max ) {
        if (child != max &&
                DN_KEY_LT(h->p[child+1].key, h->p[child].key) )
            child++; /* take right child, otherwise left */
        h->p[father] = h->p[child];
        SET_OFFSET(h, father);
        father = child;
    }
    h->elements--;
    if (father != max) {
        /*
         * Fill hole with last entry and bubble up,
         * reusing the insert code
         */
        h->p[father] = h->p[max];
        heap_insert(h, father, NULL);
    }
}
Example #12
0
static void max_heap_fix_down(int *t, int n, int p)
{
	while (1)
	{
		int i = p;
		int left = HEAP_LEFT(i);
		int right = HEAP_RIGHT(i);
		if (left < n && t[i] < t[left])
			i = left;
		if (right < n && t[i] < t[right])
			i = right;
		if (i == p)
			break;
		swap_places(t, p, i);
		p = i;
	}
}
Example #13
0
void heap_max_heapify(int *v, int size, int i)
{
	int l, r;
	int largest;

	l = HEAP_LEFT(i);
	r = HEAP_RIGHT(i);

	if (l < size && v[l] > v[i])
		largest = l;
	else
		largest = i;
	if (r < size && v[r] > v[largest])
		largest = r;
	if (largest != i) {
		swap(v, i, largest);
		heap_max_heapify(v, size, largest);
	}
}
Example #14
0
static void BLI_heap_down(Heap *heap, int i)
{
	while (1) {
		int size = heap->size, smallest;
		int l = HEAP_LEFT(i);
		int r = HEAP_RIGHT(i);

		smallest = ((l < size) && HEAP_COMPARE(heap->tree[l], heap->tree[i]))? l: i;

		if ((r < size) && HEAP_COMPARE(heap->tree[r], heap->tree[smallest]))
			smallest = r;
		
		if (smallest == i)
			break;

		HEAP_SWAP(heap, i, smallest);
		i = smallest;
	}
}
Example #15
0
static void
heap_heapify(struct heap_list *h, int i)
{
	int		l, r, largest;
	void	**p;

restart:
	l = HEAP_LEFT(i);
	r = HEAP_RIGHT(i);
	p = h->heap_data;

	if (l < h->heap_keys && (h->heap_comp_fun(p[l], p[i]) > 0))
		largest = l;
	else
		largest = i;
	if (r < h->heap_keys && h->heap_comp_fun(p[r], p[largest]) > 0)
		largest = r;
	if (largest != i) {
		heap_exchange(h, i, largest);
		i = largest;
		goto restart;
	}
}
Example #16
0
static pj_timer_entry * remove_node( pj_timer_heap_t *ht, size_t slot)
{
    pj_timer_entry *removed_node = ht->heap[slot];
    
    // Return this timer id to the freelist.
    push_freelist( ht, removed_node->_timer_id );
    
    // Decrement the size of the heap by one since we're removing the
    // "slot"th node.
    ht->cur_size--;
    
    // Set the ID
    removed_node->_timer_id = -1;

    // Only try to reheapify if we're not deleting the last entry.
    
    if (slot < ht->cur_size)
    {
	int parent;
	pj_timer_entry *moved_node = ht->heap[ht->cur_size];
	
	// Move the end node to the location being removed and update
	// the corresponding slot in the parallel <timer_ids> array.
	copy_node( ht, slot, moved_node);
	
	// If the <moved_node->time_value_> is great than or equal its
	// parent it needs be moved down the heap.
	parent = HEAP_PARENT (slot);
	
	if (PJ_TIME_VAL_GTE(moved_node->_timer_value, ht->heap[parent]->_timer_value))
	    reheap_down( ht, moved_node, slot, HEAP_LEFT(slot));
	else
	    reheap_up( ht, moved_node, slot, parent);
    }
    
    return removed_node;
}
Example #17
0
/**
 * @brief CSLR MIN-HEAPIFY
 *
 * This routine creates a min-heap for the subtree rooted at index i. The
 * index is 0 based.
 *
 * @param[in] h The heap to operate on
 * @param[in] i The (0-based) subtree index
 * @return HEAP_ERR_E
 */
HEAP_ERR_E heap_min_heapify (HEAP_T* h, unsigned long i)
{
   unsigned long l = HEAP_LEFT(i);
   unsigned long r = HEAP_RIGHT(i);
   unsigned long smallest;

   if (h->type == DS_HEAP_MAX)
      return HEAP_ERR_WRONG_TYPE;
   
   if (l < h->heap_size && (HEAP_KEY(h, l) < HEAP_KEY(h, i)))
      smallest = l;
       else
      smallest = i;
   
   if (r < h->heap_size && (HEAP_KEY(h, r) < HEAP_KEY(h, smallest)))
      smallest = r;

   if (smallest != i)
   {
      HEAP_SWAP_NODES(i,smallest);
      heap_min_heapify (h, smallest);
   }
   return HEAP_ERR_OK;
}