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