Exemple #1
0
int insert(struct heap* h, int t) {
  if(h->n >= HEAP_SIZE + 1)
         return -1;
  h->arr[h->n + 1] = t;
  bubble_up(h, h->n + 1);
  return h->n++;
}
Exemple #2
0
int
pqueue_insert(pqueue_t *q, void *d)
{
    void *tmp;
    size_t i;
    size_t newsize;

    if (!q) return 1;

    /* allocate more memory if necessary */
    if (q->size >= q->avail) {
        newsize = q->size + q->step;
        if (!(tmp = realloc(q->d, sizeof(void *) * newsize)))
            return 1;
        q->d = tmp;
        q->avail = newsize;
    }

    /* insert item */
    i = q->size++;
    q->d[i] = d;
    bubble_up(q, i);

    return 0;
}
Exemple #3
0
int git_pqueue_insert(git_pqueue *q, void *d)
{
	void *tmp;
	size_t i;
	size_t newsize;

	if (!q) return 1;

	/* allocate more memory if necessary */
	if (q->size >= q->avail) {
		newsize = q->size + q->step;
		if ((tmp = git__realloc(q->d, sizeof(void *) * newsize)) == NULL)
			return GIT_ENOMEM;

		q->d = tmp;
		q->avail = newsize;
	}

	/* insert item */
	i = q->size++;
	q->d[i] = d;
	bubble_up(q, i);

	return GIT_SUCCESS;
}
Exemple #4
0
job
pq_remove(pq q, job j)
{
    uint64_t id;
    unsigned int pri;

    if (j->heap_index >= q->used) return NULL;
    if (q->heap[j->heap_index] != j) return NULL;

    id = j->id;
    j->id = 0;
    pri = j->pri;
    j->pri = 0;

    bubble_up(q, j->heap_index);

    j->id = id;
    j->pri = pri;

    /* can't happen */
    if (q->heap[0] != j) return NULL;

    delete_min(q);

    return j;
}
Exemple #5
0
 void Heap<T, Compare>::push(const T& element)
 {
   size_type old_size = BaseList<T>::size();
   BaseList<T>::prepare_size(BaseList<T>::size() + 1);
   BaseList<T>::create(old_size, element);
   bubble_up(old_size);
 }
    /** reheap re-enforces the heap property for a node that was modified
     *  externally. Logarithmic performance for a node anywhere in the tree.
     */
    void reheap(T node)
    {
        size_t heap_idx = get_heap_index(node);
        bubble_down(bubble_up(heap_idx));
#if CHECK_HEAP_CONSISTENCY
        validate_heap_state();
#endif
    }
Exemple #7
0
void heap_push(heap *h, int x)
{
        if (h->size == h->cap) {
                heap_dbl(h);
        }
        h->buffer[h->size++]=x;
        bubble_up(h, h->size-1);
}
    void bubble_up(int index) {
	if (index == 0) return;
	int parent_index = get_parent_index(index);
	if (heapArray[index] < heapArray[parent_index]) {
	    std::swap(heapArray[index], heapArray[parent_index]);
	    bubble_up(parent_index);
	}
    }
Exemple #9
0
void bubble_up(struct heap* h, int idx) {
  int parent = get_parent(idx);
  if(parent < 0) return;
  if(h->arr[parent] > h->arr[idx]) {
    swap(&h->arr[parent], &h->arr[idx]);
    bubble_up(h, parent);
  }
}
Exemple #10
0
Fichier : heap.c Projet : kcyeu/xcb
/* Return 0 if success, otherwise -1 is returned */
int heap_push(heap_t heap, void *elem) {
	if (heap == NULL || elem == NULL)
		return -1;
	if (heap->curr == heap->avail && grow_heap(heap))
		return -1;
	heap_set(heap, ++heap->curr, elem);
	bubble_up(heap, heap->curr);
	return 0;
}
Exemple #11
0
 void Heap<T, Compare>::push_all(InputIterator begin, InputIterator end)
 {
   size_type i = BaseList<T>::size();
   BaseList<T>::prepare_size(BaseList<T>::size() + (end - begin));
   for (; begin != end; ++begin, ++i) {
     BaseList<T>::create(i, *begin);
     bubble_up(i);
   }
 }
Exemple #12
0
void Minmax::insert(int val){
	if(size == max_size){
		//std::cout << "Heap is full, cannot insert\n\n";
	}
	else{
		heap[size]=val;;
		bubble_up(size);
		//std::cout << "Inserted " << val << " into the heap.\n\n";
	}
}
Exemple #13
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 #14
0
int queue_add(Queue *q, Data data, int priority) {
    int nextIndex = q->nextIndex;
    if(q->nextIndex>q->maxSize) {
        perror("Queue overflow");
        exit(1);
    }
    q->array[nextIndex] = new_item(q, data,priority);
    q->nextIndex = nextIndex+1;
    bubble_up(q,nextIndex);
    return q->array[nextIndex].uid;
}
Exemple #15
0
/**
 * @brief remove element
 * from queue
 *
 * @param[out] q
 * @param[in] d
 *
 * @return 
 */
int pqueue_remove(pqueue_t *q, void *d)
{
    size_t posn = q->getpos(d);
    q->d[posn] = q->d[--q->size];
    if (q->cmppri(q->getpri(d), q->getpri(q->d[posn])))
        bubble_up(q, posn);
    else
        percolate_down(q, posn);

    return 0;
}
Exemple #16
0
void bubble_up(HeapPriorityQueue *queue, unsigned int pos) {
    if (pos == 0 || pos == 1) {
        return;
    }
    PriorityQueueElement *us = &queue->elements[pos];
    PriorityQueueElement *parent = &queue->elements[pos/2];
    if (parent->priority > us->priority) {
        swap(us, parent);
        bubble_up(queue, pos/2);
    }
}
Exemple #17
0
void heap_arr_insert (int *head, int x, size_t *heap_size, size_t *arr_size) {
  if (*heap_size == *arr_size) {
    *arr_size *= 2;
    realloc(head, sizeof(int) * (*arr_size));
    fill_arr(head, *heap_size, *arr_size);
  }
  size_t adj_heap_size = (*heap_size)++;
  head[adj_heap_size] = x;
  bubble_up(head, adj_heap_size);
  return;
}
Exemple #18
0
static void
bubble_up(pq q, unsigned int k)
{
    int p;

    if (k == 0) return;
    p = PARENT(k);
    if (cmp(q, p, k) <= 0) return;
    swap(q, k, p);
    bubble_up(q, p);
}
Exemple #19
0
static void bubble_up(nghttp2_pq *pq, size_t index)
{
  if(index == 0) {
    return;
  } else {
    size_t parent = (index-1)/2;
    if(pq->compar(pq->q[parent], pq->q[index]) > 0) {
      swap(pq, parent, index);
      bubble_up(pq, parent);
    }
  }
}
// sub functions: bubble_up, bubble_down
// takes index of heap element and recursively swaps it up the tree to proper position
void priority_queue::bubble_up(int index)
{
    int parent = index/2;
    // done if at top node value or greater or equal to parent
    if(index == 1 || min_heap[index].value >= min_heap[parent].value)
    {
        return;
    }
    // swap node with parent, and continue
    swap(index, parent);
    bubble_up(parent);
}
Exemple #21
0
int
pqueue_remove(pqueue_t *q, void *d)
{
	unsigned int posn = q->getpos(d);
	q->d[posn] = q->d[--q->size];
	if (q->cmppri(q->getpri(d), q->getpri(q->d[posn]))) {
		bubble_up(q, posn);
	} else {
		percolate_down(q, posn);
	}

	return 0;
}
Exemple #22
0
void bubble_up(Queue* q,int bubbleThis){
    Item this = q->array[bubbleThis];
    int pid = parentId(bubbleThis);

    // base case root el
    if(pid==0) return;
    Item parent = q->array[pid];
    // if parent is larger prior then swap
    if(this.priority<parent.priority){
        swap(q,bubbleThis, pid);
        bubble_up(q, pid);
    }
}
Exemple #23
0
void bubble_up(int node, int *heap){
	if (node == 0) {
		return;
	}
	int temp;
	int parent = (node-1)/2;
	if (heap[node] > heap[parent]) {
		temp = heap[node];
		heap[node] = heap[parent];
		heap[parent] = temp;
		bubble_up (parent, heap);
	}
}
Exemple #24
0
void pqueue_change_priority(pqueue_t *q,
                       pqueue_pri_t new_pri,
                       void *d)
{
    size_t posn;
    pqueue_pri_t old_pri = q->getpri(d);

    q->setpri(d, new_pri);
    posn = q->getpos(d);
    if (q->cmppri(old_pri, new_pri))
        bubble_up(q, posn);
    else
        percolate_down(q, posn);
}
Exemple #25
0
void
pqueue_change_priority(pqueue_t *q, pqueue_pri_t new_pri, void *d)
{
	unsigned int posn;
	pqueue_pri_t old_pri = q->getpri(d);

	q->setpri(d, new_pri);
	posn = q->getpos(d);
	if (q->cmppri(old_pri, new_pri)) {
		bubble_up(q, posn);
	} else {
		percolate_down(q, posn);
	}
}
int heap_insert(DIST h[], int v,int d, int tam){
    int size;
    tam++;
    size = tamanho_vetor;//sizeof(h)/sizeof(DIST);
    if(tam == size){
       h = (DIST*)realloc(h,size*sizeof(DIST)); /////dobra o tamanho de h;
       tamanho_vetor+=tamanho_vetor;
    }
    printf("SIZE %d ------> tamanho %d\n",size,tam);
    h[tam].v = v;
    h[tam].d = d;
    bubble_up(h,tam);
    return tam;
}
Exemple #27
0
int spdylay_pq_push(spdylay_pq *pq, void *item)
{
  if(pq->capacity <= pq->length) {
    void *nq = realloc(pq->q, pq->capacity*2);
    if(nq == NULL) {
      return SPDYLAY_ERR_NOMEM;
    }
    pq->capacity *= 2;
    pq->q = nq;
  }
  pq->q[pq->length] = item;
  ++pq->length;
  bubble_up(pq, pq->length-1);
  return 0;
}
Exemple #28
0
int
pq_give(pq q, job j)
{
    int k;

    if (q->used >= q->cap) pq_grow(q);
    if (q->used >= q->cap) return 0;

    k = q->used++;
    q->heap[k] = j;
    j->heap_index = k;
    bubble_up(q, k);

    return 1;
}
Exemple #29
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;
}
// main functions: insert, pop 
void priority_queue::insert(node cell)
{
    if(heap_size == 128)
    {
        // error for full min_heap
        Serial.print("heap is full!");
    }
    // put new node in
    min_heap[heap_size + 1] = cell;
    // record this in heap map
    heap_map[cell.state]=heap_size+1;
    // swap new node up to correct position, increment size
    bubble_up(heap_size+1);
    heap_size++;
}