Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > PriorityQueue
changeset 11:0de14128ff68 MC_shared tip
comments added for Stefan, and formatting changes
author | Sean <seanhalle@yahoo.com> |
---|---|
date | Thu, 17 May 2012 20:35:19 +0200 |
parents | ee94893d2843 |
children | |
files | PriorityQueue.c |
diffstat | 1 files changed, 51 insertions(+), 37 deletions(-) [+] |
line diff
1.1 --- a/PriorityQueue.c Mon May 14 16:24:26 2012 -0700 1.2 +++ b/PriorityQueue.c Thu May 17 20:35:19 2012 +0200 1.3 @@ -11,6 +11,7 @@ 1.4 1.5 #include "PriorityQueue.h" 1.6 1.7 +//Stefan: nice use of macros : ) 1.8 #define left(i) 2*i 1.9 #define right(i) 2*i+1 1.10 #define parent(i) i/2 1.11 @@ -27,16 +28,19 @@ 1.12 1.13 } 1.14 1.15 -bool increaseKey (PrioQueueStruc *Q, int i, int key) { 1.16 - if (key < Q->data[i].key) return false; // increasing not possible 1.17 +bool 1.18 +increaseKey( PrioQueueStruc *Q, int i, int key ) 1.19 + { 1.20 + if( key < Q->data[i].key ) 1.21 + return false; // increasing not possible 1.22 1.23 - Q->data[i].key= key; 1.24 - while (i>0 && Q->data[parent(i)].key < Q->data[i].key) { 1.25 - swap(Q,i,parent(i)); 1.26 - i= parent(i); 1.27 - } 1.28 - return true; 1.29 -} 1.30 + Q->data[i].key= key; 1.31 + while( i > 0 && Q->data[parent(i)].key < Q->data[i].key ) 1.32 + { swap( Q, i, parent(i) ); 1.33 + i = parent(i); 1.34 + } 1.35 + return true; 1.36 + } 1.37 1.38 void heapify (PrioQueueStruc *Q, int i) { 1.39 int largest; 1.40 @@ -52,32 +56,40 @@ 1.41 } 1.42 } 1.43 1.44 -void enlargePrioQ (PrioQueueStruc *Q) { 1.45 - size_t oldSize, newSize; 1.46 - heapNode *oldData,*newData; 1.47 +void 1.48 +enlargePrioQ( PrioQueueStruc *Q ) 1.49 + { size_t oldSize, newSize; 1.50 + heapNode *oldData,*newData; 1.51 1.52 - oldSize= Q->size; 1.53 - newSize= 2*oldSize; 1.54 + oldSize = Q->size; 1.55 + newSize = 2*oldSize; 1.56 1.57 - Q->maxSize= newSize; 1.58 - newData= VMS_PI__malloc(Q->maxSize*sizeof(heapNode)); 1.59 - oldData= Q->data; 1.60 + Q->maxSize = newSize; 1.61 + newData = VMS_PI__malloc( Q->maxSize * sizeof(heapNode) ); 1.62 + oldData = Q->data; 1.63 1.64 - memcpy(newData,oldData,Q->maxSize*sizeof(heapNode)); 1.65 - Q->data= newData; 1.66 + //Stefan: should this be Q->maxSize, or oldSize? Copying old data into 1.67 + // new array, so oldSize is the amount of data.. does the data need to 1.68 + // be re-arranged to make sure it still has heap invariant within the new 1.69 + // size? 1.70 + memcpy( newData, oldData, Q->maxSize * sizeof(heapNode) ); 1.71 + Q->data = newData; 1.72 1.73 - VMS_PI__free(oldData); 1.74 -} 1.75 + VMS_PI__free(oldData); 1.76 + } 1.77 1.78 1.79 -PrioQueueStruc* makePrioQ () { 1.80 - PrioQueueStruc *retQ; 1.81 - retQ= VMS_int__malloc(sizeof(PrioQueueStruc)); 1.82 - retQ->maxSize= 1024; 1.83 - retQ->data= VMS_int__malloc(retQ->maxSize*sizeof(heapNode)); 1.84 - retQ->size= 0; 1.85 - return retQ; 1.86 -} 1.87 +PrioQueueStruc * 1.88 +makePrioQ() 1.89 + { PrioQueueStruc *newQ; 1.90 + 1.91 + newQ= VMS_int__malloc( sizeof(PrioQueueStruc) ); 1.92 + newQ->maxSize= 1024; 1.93 + newQ->data= VMS_int__malloc( newQ->maxSize * sizeof(heapNode) ); 1.94 + newQ->size= 0; 1.95 + 1.96 + return newQ; 1.97 + } 1.98 1.99 void* getFirstPrioQ (PrioQueueStruc *Q) { 1.100 return Q->data[0].val; 1.101 @@ -93,17 +105,19 @@ 1.102 heapify(Q,0); 1.103 } 1.104 return max; 1.105 -} 1.106 + } 1.107 1.108 -bool insertPrioQ (void *val, int key, PrioQueueStruc *Q) { 1.109 - if (Q->size == Q->maxSize) enlargePrioQ(Q); 1.110 +bool 1.111 +insertPrioQ (void *val, int key, PrioQueueStruc *Q) 1.112 + { 1.113 + if( Q->size == Q->maxSize ) enlargePrioQ(Q); 1.114 1.115 - Q->size++; 1.116 - Q->data[Q->size-1].key= INT_MIN; 1.117 - Q->data[Q->size-1].val= val; 1.118 + Q->size++; 1.119 + Q->data[Q->size-1].key= INT_MIN; 1.120 + Q->data[Q->size-1].val= val; 1.121 1.122 - return increaseKey(Q,Q->size-1,key); 1.123 -} 1.124 + return increaseKey(Q, Q->size-1, key); 1.125 + } 1.126 1.127 void printPrioQ (PrioQueueStruc *Q) { 1.128 int i;