Actual source code: ex29.c
petsc-3.13.6 2020-09-29
1: static char help[] = "Tests PetscHeapCreate()\n\n";
3: #include <petscsys.h>
4: #include <petscviewer.h>
6: int main(int argc,char **args)
7: {
9: PetscHeap h;
10: PetscInt id,val,cnt,*values;
12: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
13: PetscHeapCreate(9,&h);
14: PetscHeapAdd(h,0,100);
15: PetscHeapAdd(h,1,19);
16: PetscHeapAdd(h,2,36);
17: PetscHeapAdd(h,3,17);
18: PetscHeapAdd(h,4,3);
19: PetscHeapAdd(h,5,25);
20: PetscHeapAdd(h,6,1);
21: PetscHeapAdd(h,8,2);
22: PetscHeapAdd(h,9,7);
23: PetscPrintf(PETSC_COMM_SELF,"Initial heap:\n");
24: PetscHeapView(h,NULL);
26: PetscHeapPop(h,&id,&val);
27: PetscHeapStash(h,id,val+10);
28: PetscHeapPop(h,&id,&val);
29: PetscHeapStash(h,id,val+10);
30: PetscPrintf(PETSC_COMM_SELF,"Pop two items, increment, and place in stash:\n");
31: PetscHeapView(h,NULL);
33: PetscHeapUnstash(h);
34: PetscPrintf(PETSC_COMM_SELF,"After unpacking the stash:\n");
35: PetscHeapView(h,NULL);
37: PetscMalloc1(9,&values);
38: PetscHeapPop(h,&id,&val);
39: cnt = 0;
40: while (id >= 0) {
41: values[cnt++] = val;
42: PetscHeapPop(h,&id,&val);
43: }
44: PetscPrintf(PETSC_COMM_SELF,"Sorted values:\n");
45: PetscIntView(cnt,values,PETSC_VIEWER_STDOUT_SELF);
46: PetscFree(values);
47: PetscHeapDestroy(&h);
48: PetscFinalize();
49: return ierr;
50: }
55: /*TEST
57: test:
59: TEST*/