Пример #1
0
bool Heap::insert(int value) {
    if (p == Maxsize)
        return false;
    root[p++] = value;
    siftup(p-1);
    return true;
}
Пример #2
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;
}
Пример #3
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;
}
Пример #4
0
void dheap::remove(item i) {
// Remove item i from heap. Name remove is used since delete is C++ keyword.
	int j = h[n--];
	     if (i != j && kvec[j] <= kvec[i]) siftup(j,pos[i]);
	else if (i != j && kvec[j] >  kvec[i]) siftdown(j,pos[i]);
	pos[i] = Null;
}
Пример #5
0
void
heap_heapify(struct heap *h)
{
	for (size_t i = ptrvec_size(h->vec)/2; i-- > 0; ) {
		siftup(h, i);
	}
}
Пример #6
0
void test005()
{
    int arr[] = {2, 9, 1, 5, 4, 3};
    int i = 0;
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
    quickSort(arr, 6);
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
    siftdown(arr, 1, 6);
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
    siftup(arr , 2, 6);
    for(i=0;i<5;i++)
    {
        printf("%d ", arr[i]);
    }
    printf("%d\n", arr[i]);
}
Пример #7
0
bool 
runtime·deltimer ( Timer *t ) 
{ 
int32 i; 
#line 156 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
i = t->i; 
USED ( i ) ; 
#line 159 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
runtime·lock ( &timers ) ; 
#line 164 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
i = t->i; 
if ( i < 0 || i >= timers.len || timers.t[i] != t ) { 
runtime·unlock ( &timers ) ; 
return false; 
} 
#line 170 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
timers.len--; 
if ( i == timers.len ) { 
timers.t[i] = nil; 
} else { 
timers.t[i] = timers.t[timers.len]; 
timers.t[timers.len] = nil; 
timers.t[i]->i = i; 
siftup ( i ) ; 
siftdown ( i ) ; 
} 
if ( debug ) 
dumptimers ( "deltimer" ) ; 
runtime·unlock ( &timers ) ; 
return true; 
} 
Пример #8
0
void Heap::remove(int oldval)
{
  int i=p[oldval];
  a[i]=a[h--];
  p[a[i]]=i;
  siftdown(i);
  siftup(i);
}
Пример #9
0
static void
R_heapsort(int low, int up, double *window, int *outlist, int *nrlist,
	 int print_level)
{
    int l, u;

    l = (up/2) + 1;
    u = up;
    while(l > low) {
	l--;
	siftup(l, u, window, outlist, nrlist, print_level);
    }
    while(u > low) {
	swap(l, u, window, outlist, nrlist, print_level);
	u--;
	siftup(l, u, window, outlist, nrlist, print_level);
    }
}
Пример #10
0
    // Siftup() takes a node that has just been added to the heap
    // and compares it to its parent. It swaps values if necessary
    // and recurses on the parent node until reaching the root.
    void siftup(int *node) {
        // base case
        if(node == root() || size() == 0) return;

        int *parent = rootOf(node);
        if(*node < *parent) swapValues(node, parent);

        // recursion
        return siftup(parent);
    }
Пример #11
0
void *
heap_replace(struct heap *h, void *item)
{
	if (ptrvec_size(h->vec) < 1)
		return (NULL);
	void *returnitem = ptrvec_value(h->vec, 0);
	ptrvec_data(h->vec)[0] = item;
	siftup(h, 0);
	return (returnitem);
}
Пример #12
0
 // buildheap takes the number of elements to place
 // in the heap and a method for heaping it.
 // A value of 1 in method will use siftup().
 // A value of 0 passed through method only add the elements.
 // Heapify() must be manually called after if this method
 // is choosen.
 //
 // For Min_heap, buildHeap() places the largest element at
 // the bottom of the tree, so we get worst-case time for
 // testing
 void buildHeap(int elements, int method){
     if(method == 0) {
         for(int i=elements;i>0;--i) push(i);
     }
     if(method == 1){
         for(int i=elements;i>0;--i) {
             push(i);
             siftup(end());
         }
     }
 }
Пример #13
0
status heap_insert(heap * const p_H,
                   generic_ptr const data)
{
    if (p_H -> next_element == p_H -> heap_size
            &&
            increment_heap(p_H) == ERROR)
        return ERROR;

    p_H -> base[p_H -> next_element] = data;
    if (siftup(p_H, p_H -> next_element) == ERROR)
        return ERROR;

    p_H -> next_element++;
    return OK;
}//heap_insert
Пример #14
0
void *
heap_pop(struct heap *h)
{
	if (ptrvec_size(h->vec) < 1)
		return (NULL);
	void *returnitem;
	void *lastelt = ptrvec_value(h->vec, ptrvec_size(h->vec) - 1);
	ptrvec_clip(h->vec, ptrvec_size(h->vec) - 1);
	if (ptrvec_size(h->vec) > 0) {
		returnitem = ptrvec_value(h->vec, 0);
		ptrvec_data(h->vec)[0] = lastelt;
		siftup(h, 0);
	} else {
		returnitem = lastelt;
	}
	return (returnitem);
}
Пример #15
0
void *
heapremove(Heap *h, int k)
{
    void *x;

    if (k >= h->len) {
        return 0;
    }

    x = h->data[k];
    h->len--;
    set(h, k, h->data[h->len]);
    siftdown(h, k);
    siftup(h, k);
    h->rec(x, -1);
    return x;
}
Пример #16
0
status heap_delete(heap * const p_H,
                   size_t const index,
                   generic_ptr * const p_data)
{
    if (index >= p_H -> next_element)
        return ERROR;

    *p_data = p_H -> base[index];
    p_H -> next_element --;

    p_H -> base[index] = p_H -> base [p_H -> next_element];

    return (siftup(p_H, index) == ERROR ||
            siftdown(p_H, index) == ERROR) ?
           ERROR :
           OK;
}//heap_delete
Пример #17
0
static status siftup(heap * const p_H,
                     size_t const index)
{
    if (index == 0)
        return OK;

    {
        size_t parent;
        if (parent_index(index, &parent) == ERROR)
            return ERROR;

        if ((*(p_H -> p_cmp_f))(p_H -> base[index], p_H -> base[parent]) >= 0)
            return OK;

        swap(p_H, index, parent);
        return siftup(p_H, parent);
    }
}
int insert_heap(int num, struct heap **p)
{
  struct heap *q;

  if ((*p)->sz == (*p)->maxsz)
  {
    q = realloc(*p, HLISTSZ((*p)->maxsz + INCRHSZ));
    if( q == NULL)
    {
      return(-1);
    }
    q->maxsz += INCRHSZ;
    *p = q;
  }
  (*p)->item[(*p)->sz++] = num;
  siftup(*p,(*p)->sz -1);
  return(0);
}
Пример #19
0
int main(){
    int i,t;
    num = 0;
    
    scanf("%d", &t);
    while(!feof(stdin)){
		input[num] = t;
		list[num] = num;
		data[num] = malloc(sizeof(int) * t);
		
		num ++;
		scanf("%d", &t);
	}
    
    qsort(list, num, sizeof(int), MyComp);
    
    
    
    pos = 1;
    perm[0] = 1;
    
    for(i=0;i<num;i++){
    	while(pos<input[list[i]]){		
			perm[pos] = perm[pos-1];
			perm[pos-1] = pos + 1;
			siftup(pos-1);
			pos ++;		
		}
		memcpy(data[list[i]], perm, sizeof(int) * pos);
	}
	int *ptr;
	for(i=0;i<num;i++){
		ptr = data[i];
		printf("%d", ptr[0]);
		for(t=1;t<input[i];t++){
			printf(" %d", ptr[t]);
		}
		printf("\n");
	}
    return 0;
}
Пример #20
0
static void 
addtimer ( Timer *t ) 
{ 
int32 n; 
Timer **nt; 
#line 110 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
if ( t->when < 0 ) 
t->when = ( 1LL<<63 ) -1; 
#line 113 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
if ( timers.len >= timers.cap ) { 
#line 115 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
n = 16; 
if ( n <= timers.cap ) 
n = timers.cap*3 / 2; 
nt = runtime·malloc ( n*sizeof nt[0] ) ; 
runtime·memmove ( nt , timers.t , timers.len*sizeof nt[0] ) ; 
runtime·free ( timers.t ) ; 
timers.t = nt; 
timers.cap = n; 
} 
t->i = timers.len++; 
timers.t[t->i] = t; 
siftup ( t->i ) ; 
if ( t->i == 0 ) { 
#line 129 "/home/14/ren/source/golang/go/src/pkg/runtime/time.goc"
if ( timers.sleeping ) { 
timers.sleeping = false; 
runtime·notewakeup ( &timers.waitnote ) ; 
} 
if ( timers.rescheduling ) { 
timers.rescheduling = false; 
runtime·ready ( timers.timerproc ) ; 
} 
} 
if ( timers.timerproc == nil ) { 
timers.timerproc = runtime·newproc1 ( &timerprocv , nil , 0 , 0 , addtimer ) ; 
timers.timerproc->issystem = true; 
} 
if ( debug ) 
dumptimers ( "addtimer" ) ; 
} 
Пример #21
0
static int heapinsert( table_t *t, entry_t *item )
{
	entry_t *heap = t->heap;

	/*** Still room for an entry? ***/
	if (t->size < t->heapsize) {
		memcpy( &(heap[t->size]), item, sizeof(entry_t));
		siftup( t, t->size );
		t->size++;		
		return 0;
	}

	/*** Worse than the worst performer? ***/
	if ( LESS(*item, heap[0]) ) {
		return 0;
	}

	/*** Insert into heap and reheap ***/
	memcpy( &(heap[0]), item, sizeof(entry_t));
	siftdown( t, t->size, 0 );
	return 0;
}
Пример #22
0
void heapAdd(Tree** heap, int n, Tree* node) {
    heap[n] = node;
    siftup(heap, n);
}
Пример #23
0
void dheap::insert(item i, keytyp k) {
// Add i to heap.
	kvec[i] = k; n++; siftup(i,n);
}
Пример #24
0
void dheap::changekey(item i, keytyp k) {
// Change the key of i and restore heap order.
	keytyp ki = kvec[i]; kvec[i] = k;
	     if (k < ki) siftup(i,pos[i]);
	else if (k > ki) siftdown(i,pos[i]);
}
Пример #25
0
void insHeap(int site, int t) {/*insert new node*/
    Heap[site].v = t;
    siftup(site);
}
Пример #26
0
void Heap::insert(int newval)
{
  a[++h]=newval;
  p[a[h]]=h;
  siftup(h);
}