# HG changeset patch # User Sean # Date 1337279719 -7200 # Node ID 0de14128ff68b25602854a57dcede889d0529509 # Parent ee94893d28432f306a082548d747c03105cbb653 comments added for Stefan, and formatting changes diff -r ee94893d2843 -r 0de14128ff68 PriorityQueue.c --- a/PriorityQueue.c Mon May 14 16:24:26 2012 -0700 +++ b/PriorityQueue.c Thu May 17 20:35:19 2012 +0200 @@ -11,6 +11,7 @@ #include "PriorityQueue.h" +//Stefan: nice use of macros : ) #define left(i) 2*i #define right(i) 2*i+1 #define parent(i) i/2 @@ -27,16 +28,19 @@ } -bool increaseKey (PrioQueueStruc *Q, int i, int key) { - if (key < Q->data[i].key) return false; // increasing not possible +bool +increaseKey( PrioQueueStruc *Q, int i, int key ) + { + if( key < Q->data[i].key ) + return false; // increasing not possible - Q->data[i].key= key; - while (i>0 && Q->data[parent(i)].key < Q->data[i].key) { - swap(Q,i,parent(i)); - i= parent(i); - } - return true; -} + Q->data[i].key= key; + while( i > 0 && Q->data[parent(i)].key < Q->data[i].key ) + { swap( Q, i, parent(i) ); + i = parent(i); + } + return true; + } void heapify (PrioQueueStruc *Q, int i) { int largest; @@ -52,32 +56,40 @@ } } -void enlargePrioQ (PrioQueueStruc *Q) { - size_t oldSize, newSize; - heapNode *oldData,*newData; +void +enlargePrioQ( PrioQueueStruc *Q ) + { size_t oldSize, newSize; + heapNode *oldData,*newData; - oldSize= Q->size; - newSize= 2*oldSize; + oldSize = Q->size; + newSize = 2*oldSize; - Q->maxSize= newSize; - newData= VMS_PI__malloc(Q->maxSize*sizeof(heapNode)); - oldData= Q->data; + Q->maxSize = newSize; + newData = VMS_PI__malloc( Q->maxSize * sizeof(heapNode) ); + oldData = Q->data; - memcpy(newData,oldData,Q->maxSize*sizeof(heapNode)); - Q->data= newData; + //Stefan: should this be Q->maxSize, or oldSize? Copying old data into + // new array, so oldSize is the amount of data.. does the data need to + // be re-arranged to make sure it still has heap invariant within the new + // size? + memcpy( newData, oldData, Q->maxSize * sizeof(heapNode) ); + Q->data = newData; - VMS_PI__free(oldData); -} + VMS_PI__free(oldData); + } -PrioQueueStruc* makePrioQ () { - PrioQueueStruc *retQ; - retQ= VMS_int__malloc(sizeof(PrioQueueStruc)); - retQ->maxSize= 1024; - retQ->data= VMS_int__malloc(retQ->maxSize*sizeof(heapNode)); - retQ->size= 0; - return retQ; -} +PrioQueueStruc * +makePrioQ() + { PrioQueueStruc *newQ; + + newQ= VMS_int__malloc( sizeof(PrioQueueStruc) ); + newQ->maxSize= 1024; + newQ->data= VMS_int__malloc( newQ->maxSize * sizeof(heapNode) ); + newQ->size= 0; + + return newQ; + } void* getFirstPrioQ (PrioQueueStruc *Q) { return Q->data[0].val; @@ -93,17 +105,19 @@ heapify(Q,0); } return max; -} + } -bool insertPrioQ (void *val, int key, PrioQueueStruc *Q) { - if (Q->size == Q->maxSize) enlargePrioQ(Q); +bool +insertPrioQ (void *val, int key, PrioQueueStruc *Q) + { + if( Q->size == Q->maxSize ) enlargePrioQ(Q); - Q->size++; - Q->data[Q->size-1].key= INT_MIN; - Q->data[Q->size-1].val= val; + Q->size++; + Q->data[Q->size-1].key= INT_MIN; + Q->data[Q->size-1].val= val; - return increaseKey(Q,Q->size-1,key); -} + return increaseKey(Q, Q->size-1, key); + } void printPrioQ (PrioQueueStruc *Q) { int i;