Exemple #1
0
int heap_insert(Heap *heap, const void *data) {
    void *temp;
    int ipos, ppos;

    /* Allocate storage for node */
    if ((temp = (void **) realloc(heap->tree, (heap_size(heap) + 1) * sizeof(void *))) == NULL)
        return -1;

    heap->tree = temp;

    /* Insert node after last node */
    heap->tree[heap_size(heap)] = (void *) data;

    /* Heapify tree by pushing contents of new node upward */
    ipos = heap_size(heap);
    ppos = heap_parent(ipos);
    while (ipos > 0 && heap->compare(heap->tree[ppos], heap->tree[ipos]) < 1) {
        /* Swap contents of current node and parent */
        temp = heap->tree[ppos];
        heap->tree[ppos] = heap->tree[ipos];
        heap->tree[ipos] = temp;

        /* Move up one level in tree to continue heapifying */
        ipos = ppos;
        ppos = heap_parent(ipos);
    }

    /* Adjust size of heap to account for inserted node */
    heap->size++;
    return 0;
}
Exemple #2
0
int heap_insert(Heap *heap, const void *data)
{
        void *tmp;
        int ipos, ppos;

        tmp = (void **)realloc(heap->tree, (heap->size + 1) * sizeof(void *));
        if (tmp) {
                heap->tree = tmp;
        } else {
                return -1;
        }

        heap->tree[heap->size] = (void *)data;

        ipos = heap->size;
        ppos = heap_parent(ipos);

        while (ipos && heap->compare(heap->tree[ppos], heap->tree[ipos]) < 0) {
                tmp = heap->tree[ppos];
                heap->tree[ppos] = heap->tree[ipos];
                heap->tree[ipos] = tmp;

                ipos = ppos;
                ppos = heap_parent(ipos);
        }

        heap->size++;

        return 0;
}
Exemple #3
0
static void sift_up(binary_heap *heap, int index) {
    int parent = heap_parent(index);
    while(heap->cmp(heap->heap[index], heap->heap[parent])) {
        SWAP(heap->heap[index],heap->heap[parent]);
        index = parent;
        parent = heap_parent(index);
    }
}
Exemple #4
0
static void
float_up(heap_context ctx, int i, void *elt) {
	int p;

	for ( p = heap_parent(i); 
	      i > 1 && ctx->higher_priority(elt, ctx->heap[p]);
	      i = p, p = heap_parent(i) ) {
		ctx->heap[i] = ctx->heap[p];
		if (ctx->index != NULL)
			(ctx->index)(ctx->heap[i], i);
	}
	ctx->heap[i] = elt;
	if (ctx->index != NULL)
		(ctx->index)(ctx->heap[i], i);
}
Exemple #5
0
 void heapify(T* array, const int count) {
   int end = count - 1;
   int start = heap_parent(end);
   while (start >= 0) {
     sift_down(array, start, end);
     --start;
   }
 }  // end of heapify
Exemple #6
0
/* Insert */
void PQ_insert(PQueue* pq, elemType elt) {
  
  unsigned int ii;
  assert(pq && pq->elements); 
 
  PQ_DEBUG {printf("PQ_insert: "); printElem(elt); printf("\n"); fflush(stdout);}
  if (pq->cursize==pq->maxsize) {
    PQ_grow(pq);
  }
  assert(pq->cursize < pq->maxsize);
  for (ii = pq->cursize++;
       ii && (compare_element(pq->elements[heap_parent(ii)], elt) > 0);
       ii = heap_parent(ii)) {
    pq->elements[ii] = pq->elements[heap_parent(ii)];
  }
  pq->elements[ii] = elt;
}                                       
Exemple #7
0
static void
float_up(isc_heap_t *heap, unsigned int i, void *elt) {
	unsigned int p;

	for (p = heap_parent(i) ;
	     i > 1 && heap->compare(elt, heap->array[p]) ;
	     i = p, p = heap_parent(i)) {
		heap->array[i] = heap->array[p];
		if (heap->index != NULL)
			(heap->index)(heap->array[i], i);
	}
	heap->array[i] = elt;
	if (heap->index != NULL)
		(heap->index)(heap->array[i], i);

	INSIST(HEAPCONDITION(i));
}
Exemple #8
0
//Alan's reheapify function will reheapify node n
void reheapify(Heap *heap, struct node *n) {
    void *temp;

    int ipos = n->ipos, ppos = heap_parent(ipos);

    while (ipos > 0 && heap->compare(heap->tree[ppos], heap->tree[ipos]) < 0) {

        temp = heap->tree[ppos];
        heap->tree[ppos] = heap->tree[ipos];
        ((struct node *)(heap->tree[ppos]))->ipos = ipos;
        heap->tree[ipos] = temp;
        ((struct node *)(heap->tree[ipos]))->ipos = ppos;

        ipos = ppos;
        ppos = heap_parent(ipos);
    }
}
Exemple #9
0
int heap_insert(Heap *heap, const void *data)
{
        void *temp;

        int ipos, ppos;

        /*
         * Allocate storage for node
         */
        if ((temp = (void **)realloc(heap->tree, (heap_size(heap) + 1) *
                                                       sizeof(void *))) == NULL)
                return -1;

        heap->tree = temp;

        /*
         * Insert node at end
         */
        heap->tree[heap_size(heap)] = (void *)data;

        /*
         * Heapify by swapping node upwards as necessary
         */
        ipos = heap_size(heap);
        ppos = heap_parent(ipos);
        while (ipos > 0 &&
                        heap->compare(heap->tree[ppos], heap->tree[ipos]) < 0) {
                temp = heap->tree[ppos];
                heap->tree[ppos] = heap->tree[ipos];
                heap->tree[ipos] = temp;

                ipos = ppos;
                ppos = heap_parent(ipos);
        }

        /*
         * Update heap stats
         */
        heap->size++;

        return 0;
}
Exemple #10
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 #11
0
int agile_heap_insert(agile_heap* heap, const void* data) {
	void* temp;
	int ipos;
	int ppos;
	if ((temp=(void**)realloc(heap->tree, (agile_heap_size(heap)+1)*sizeof(void*)))==NULL) return -1;
	heap->tree = temp;
	heap->tree[agile_heap_size(heap)] = (void*)data;
	ipos = agile_heap_size(heap);
	ppos = heap_parent(ipos);
	while (ipos > 0 && heap->compare(heap->tree[ppos],heap->tree[ipos]) < 0) {
		temp = heap->tree[ppos];
		heap->tree[ppos] = heap->tree[ipos];
		heap->tree[ipos] = temp;

		ipos = ppos;
		ppos = heap_parent(ipos);
	}
	heap->size += 1;
	return 0;
}
Exemple #12
0
void
heap_increase_priority(heap_t *heap,
                       heap_item_t* item,
                       double new_priority,
                       double (*compute_priority)(heap_item_t*),
                       void (*set_priority)(heap_item_t*,double)) {
  // assumes that priority and node mass are one and the same
  double new_mass;
  if (new_priority < compute_priority(item)) {
    fprintf(stderr,"New priority is smaller than current priority.\n");
    return;
  }

  new_mass = new_priority - item->node_mass;
  
  set_priority(item,new_priority);
  
  
  item->node_mass = new_priority;
  item->subtree_mass += new_mass;
  
  while (item->index > 0 && compute_priority(heap_parent(heap, item)) < compute_priority(item)) {
    heap_parent(heap,item)->subtree_mass += new_mass;
    heap_exchange(heap, item, heap_parent(heap, item));
    item = heap_parent(heap, item);
  }

  while (item->index > 0) {
    heap_parent(heap,item)->subtree_mass += new_mass;
    item = heap_parent(heap, item);
  }
}
Exemple #13
0
void
heap_insert(heap_t *heap,
            node_t *node,
            double (*compute_mass) (node_t*),
            double (*compute_priority) (heap_item_t*)) {
  heap_item_t *item;
  //heap_item_t *parent_item;
  double node_mass;
  //uint64_t parent_index;
  
  item = make_heap_item(node, compute_mass, compute_priority);
  item->index = heap->n_nodes;
  
  // ensure that the heap can accommodate a new item
  if (heap->n_nodes == heap->n_alloced) {
    heap->items = realloc(heap->items, 2*heap->n_alloced * sizeof(heap_item_t*));
    if(!heap->items) {
      fprintf(stderr,"Could not reallocate heap (%llu -> %llu)!\n",heap->n_alloced,2*heap->n_alloced);
      return;
    }
    heap->n_alloced = 2*heap->n_alloced;
  }
  
  heap->items[item->index] = item;
  node_mass = item->node_mass;
  //parent_index = UINT64_MAX;
  //parent_item = item;
  //while (heap_parent(heap, parent_item)->index < parent_index) {
  while (heap_parent(heap, item) != item) {
    //parent_item = heap_parent(heap, parent_item);
    //parent_index = parent_item->index;
    //parent_item->subtree_mass += item->node_mass;
    item = heap_parent(heap, item);
    item->subtree_mass += node_mass;
  }

  heap->n_nodes++;
  heap->total_mass += node_mass;
}
Exemple #14
0
/* heap_insert */
int heap_insert(Heap *heap, const void *data) {
	void *temp;
	int ipos;
	int ppos;

	/* Allocate storage for the node. */
	if ((temp = (void **)realloc
		 (heap->tree, (heap_size(heap) + 1) * sizeof(void *))
		 ) == NULL) {
		return -1;
	} else {
		heap->tree = temp;
	}

	/* Insert the node after the last node. */
	heap->tree[heap_size(heap)] = (void *)data;

	/* Heapify the tree by pushing the contents of the new node upward. */
	ipos = heap_size(heap);
	ppos = heap_parent(ipos);

	while (ipos > 0
		   && heap->compare(heap->tree[ppos], heap->tree[ipos]) < 0) {
		/* Swap the contents of the current node and its parent. */
		temp = heap->tree[ppos];
		heap->tree[ppos] = heap->tree[ipos];
		heap->tree[ipos] = temp;

		/* Move up one level in the tree to continue heapifying. */
		ipos = ppos;
		ppos = heap_parent(ipos);
	}

	/* Adjust the size of the heap to account for the inserted node. */
	heap->size++;

	return 0;
}
Exemple #15
0
void  heap_percolate_up(heap_t *heap, int index) {
  int i = index;
  int p;
  for (;;) {
    p = heap_parent(i);
    if (p < 0) { return; }
    if (heap->compare(array_get(&heap->items, i), array_get(&heap->items, p)) < 0) {
      array_swap(&heap->items, i, p);
      i = p;
    } else {
      return;
    }
  }
}
Exemple #16
0
void heap_insert(collision_heap* heap, collision_data *collision){
    heap->length += 1;
    if(heap->length > heap->allocated){
        heap->allocated *= 2;
        heap->heap = (collision_data*)realloc(heap->heap, sizeof(collision_data) * (heap->allocated + 1));
    }
    int i = heap->length;
    while(i > 1 && heap->heap[heap_parent(i)].time > collision->time){
        heap->heap[i] = heap->heap[heap_parent(i)];
        i = heap_parent(i);
    }
    heap->heap[i] = *collision;
    /**
        Keeping who > with order for movables
        */
    if(heap->heap[i].with_movable){
        if(heap->heap[i].with < heap->heap[i].who){
            short int temp = heap->heap[i].who;
            heap->heap[i].who = heap->heap[i].with;
            heap->heap[i].with = temp;
        }
    }
}
Exemple #17
0
int heap_insert( heap_t *heap_in, void *data_in )
{
	int i,j,step = heap_in->step;
	byte_t *start = (byte_t*) heap_in->start;
	if( heap_in->alloc < heap_in->size + 1 )
		heap_grow( heap_in, HEAP_SIZE_INC );
	i = heap_in->size;
	while( i > 0 )
	{
		j = heap_parent( i );
		if( heap_in->cmp( start + j * step, data_in ) == 1 )
			break;
		bcopy( start + j * step, start + i * step, step );
		i = j; /* Take a step up the tree toward the root */
	}
	bcopy( data_in, start + i * step, step );
	++heap_in->size;
	return 0;
}
Exemple #18
0
/* Enqueue an item in the heap.
 *
 * Args:
 *   heap: A binary heap in which stores indexs into seqs.
 *   n: Fixed maximum size of the heap.
 *   m: Current size of the heap.
 *   seqs: Sequences.
 *   idx: Index to enqueue.
 *   cmp: Comparison function.
 *
 */
void heap_push(size_t* heap, size_t n, size_t* m, seq_t** seqs,
               int (*cmp)(const void*, const void*), size_t idx)
{
    if (*m >= n) return;
    size_t tmp, j, i = (*m)++;
    heap[i] = idx;

    /* percolate up */
    while (i > 0) {
        j = heap_parent(i);
        if (cmp(seqs[heap[i]], seqs[heap[j]]) < 0) {
            tmp = heap[i];
            heap[i] = heap[j];
            heap[j] = tmp;
            i = j;
        }
        else break;
    }
}
Exemple #19
0
void minHeapDecreaseKey(int current_index, Heap *heap){
	while(current_index > 0 && compare(vector_get(heap_parent(current_index), heap->vector), vector_get(current_index, heap->vector), heap) > 0){
		vector_swap(current_index, heap_parent(current_index), heap->vector);
      	current_index = heap_parent(current_index);
	}
}
Exemple #20
0
static void heapify(binary_heap *heap) {
    long start;
    for(start=heap_parent((heap->len-1)); start>=0; start--) {
        sift_down(heap, start, heap->len);
    }
}