lib/quickHeap.c File Reference

#include "common.h"
#include "quickHeap.h"

Include dependency graph for quickHeap.c:

Go to the source code of this file.

Functions

quickHeapnewQuickHeap (int initSize, int(*compare)(const void *elem1, const void *elem2))
void freeQuickHeap (struct quickHeap **pH)
static void expandQuickHeap (struct quickHeap *h)
void addToQuickHeap (struct quickHeap *h, void *elem)
static void quickHeapBalanceWithChildren (struct quickHeap *h, int n)
void quickHeapTopChanged (struct quickHeap *h)
boolean quickHeapEmpty (struct quickHeap *h)
static void removeFromQuickHeapN (struct quickHeap *h, int n)
static int findElemInQuickHeap (struct quickHeap *h, void *elem)
boolean removeFromQuickHeapByElem (struct quickHeap *h, void *elem)
void * peekQuickHeapTop (struct quickHeap *h)
void * removeQuickHeapTop (struct quickHeap *h)

Variables

static char const rcsid [] = "$Id: quickHeap.c,v 1.1 2005/10/21 05:36:08 galt Exp $"


Function Documentation

void addToQuickHeap ( struct quickHeap h,
void *  elem 
)

Definition at line 66 of file quickHeap.c.

References quickHeap::compareFunc, expandQuickHeap(), quickHeap::heap, quickHeap::heapCount, and quickHeap::heapMax.

00068 {
00069 int n, p;   /* node, parent */
00070 if (h->heapCount >= h->heapMax)
00071     expandQuickHeap(h);
00072 /* preserve heap property as it grows */
00073 n = h->heapCount;
00074 h->heap[h->heapCount++] = elem;
00075 p = (n-1)/2;
00076 while (n > 0 && h->compareFunc(h->heap[p], h->heap[n]) < 0)
00077     {
00078     void *temp = h->heap[p];
00079     h->heap[p] = h->heap[n];
00080     h->heap[n] = temp;
00081     n = p;
00082     p = (n-1)/2;
00083     }
00084 }

Here is the call graph for this function:

static void expandQuickHeap ( struct quickHeap h  )  [static]

Definition at line 58 of file quickHeap.c.

References ExpandArray, quickHeap::heap, and quickHeap::heapMax.

Referenced by addToQuickHeap().

00060 {
00061 int newSize = h->heapMax * 2;
00062 ExpandArray(h->heap, h->heapMax, newSize);
00063 h->heapMax = newSize;
00064 }

Here is the caller graph for this function:

static int findElemInQuickHeap ( struct quickHeap h,
void *  elem 
) [static]

Definition at line 150 of file quickHeap.c.

References quickHeap::heap, and quickHeap::heapCount.

Referenced by removeFromQuickHeapByElem().

00153 {
00154 int n = -1;
00155 while (++n < h->heapCount)
00156     {
00157     if (h->heap[n] == elem)
00158         return n;
00159     }
00160 return -1;    
00161 }

Here is the caller graph for this function:

void freeQuickHeap ( struct quickHeap **  pH  ) 

Definition at line 50 of file quickHeap.c.

References freez(), and quickHeap::heap.

00052 {
00053 struct quickHeap *h = *pH;
00054 freez(&h->heap);
00055 freez(pH);
00056 }

Here is the call graph for this function:

struct quickHeap* newQuickHeap ( int  initSize,
int(*)(const void *elem1, const void *elem2)  compare 
) [read]

Definition at line 33 of file quickHeap.c.

References AllocN, AllocVar, quickHeap::compareFunc, errAbort(), quickHeap::heap, quickHeap::heapCount, and quickHeap::heapMax.

00037 {
00038 struct quickHeap *h;
00039 if (initSize < 1)
00040     errAbort("invalid initial size for newQuickHeap: %d",initSize);
00041 AllocVar(h);
00042 h->heapCount=0;
00043 h->heapMax = initSize;
00044 h->heap = AllocN(void *,h->heapMax);
00045 h->compareFunc = compare;
00046 return h;
00047 }

Here is the call graph for this function:

void* peekQuickHeapTop ( struct quickHeap h  ) 

Definition at line 176 of file quickHeap.c.

References quickHeap::heap, and quickHeap::heapCount.

00178 {
00179 if (h->heapCount < 1) 
00180     return NULL;
00181 return h->heap[0];    
00182 }

static void quickHeapBalanceWithChildren ( struct quickHeap h,
int  n 
) [static]

Definition at line 87 of file quickHeap.c.

References compareFunc, errAbort(), quickHeap::heap, quickHeap::heapCount, and TRUE.

Referenced by quickHeapTopChanged(), and removeFromQuickHeapN().

00089 {
00090 int c1, c2, hc;   /* child1, child2, heapCount */
00091 if (n < 0 || n >= h->heapCount)
00092     errAbort("attempt to quickHeapBalanceWithChildren with invalid value "
00093                 "for n: %d, heapCount=%d", n, h->heapCount);
00094 /* balance heap - swap node with biggest child until both children are 
00095  * either less or equal or hit end of heap (no more children) */
00096 hc = h->heapCount;
00097 c1 = 2*n+1;
00098 c2 = 2*n+2;
00099 while (TRUE)
00100     {
00101     void *temp = NULL;
00102     int bestc = n;
00103     if (c1 < hc && h->compareFunc(h->heap[c1], h->heap[bestc]) > 0)
00104         bestc = c1;
00105     if (c2 < hc && h->compareFunc(h->heap[c2], h->heap[bestc]) > 0)
00106         bestc = c2;
00107     if (bestc == n)
00108         break;
00109     temp = h->heap[bestc];
00110     h->heap[bestc] = h->heap[n];
00111     h->heap[n] = temp;
00112     n = bestc;
00113     c1 = 2*n+1;
00114     c2 = 2*n+2;
00115     }
00116 }

Here is the call graph for this function:

Here is the caller graph for this function:

boolean quickHeapEmpty ( struct quickHeap h  ) 

Definition at line 127 of file quickHeap.c.

References quickHeap::heapCount.

00129 {
00130 return h->heapCount < 1;
00131 }

void quickHeapTopChanged ( struct quickHeap h  ) 

Definition at line 119 of file quickHeap.c.

References quickHeapBalanceWithChildren().

00121 {
00122 /* balance heap - swap node with biggest child until both children are 
00123  * either less or equal or hit end of heap (no more children) */
00124 quickHeapBalanceWithChildren(h,0);
00125 }

Here is the call graph for this function:

boolean removeFromQuickHeapByElem ( struct quickHeap h,
void *  elem 
)

Definition at line 164 of file quickHeap.c.

References FALSE, findElemInQuickHeap(), removeFromQuickHeapN(), and TRUE.

00168 {
00169 int n = findElemInQuickHeap(h, elem);
00170 if (n < 0) 
00171     return FALSE;
00172 removeFromQuickHeapN(h,n);
00173 return TRUE;
00174 }

Here is the call graph for this function:

static void removeFromQuickHeapN ( struct quickHeap h,
int  n 
) [static]

Definition at line 133 of file quickHeap.c.

References errAbort(), quickHeap::heap, quickHeap::heapCount, and quickHeapBalanceWithChildren().

Referenced by removeFromQuickHeapByElem(), and removeQuickHeapTop().

00137 {
00138 if (n < 0 || n >= h->heapCount)
00139     errAbort("attempt to removeFromQuickHeapN with invalid value "
00140                 "for n: %d, heapCount=%d", n, h->heapCount);
00141 h->heap[n] = h->heap[--h->heapCount]; /* wipes out n by moving tail to it */
00142 h->heap[h->heapCount] = NULL; /* not needed but clean */
00143 /* balance heap - swap node with biggest child until both children are 
00144  * either less or equal or hit end of heap (no more children) */
00145 if (n < h->heapCount)  /* i.e. true for all but when n was the tail */
00146     quickHeapBalanceWithChildren(h,n);
00147 }

Here is the call graph for this function:

Here is the caller graph for this function:

void* removeQuickHeapTop ( struct quickHeap h  ) 

Definition at line 184 of file quickHeap.c.

References quickHeap::heap, quickHeap::heapCount, and removeFromQuickHeapN().

00188 {
00189 void *result = h->heap[0]; 
00190 /* this is ok, but depends on removeFromQuickHeapN NULLing as it empties */   
00191 if (h->heapCount > 0) 
00192     {
00193     removeFromQuickHeapN(h,0);
00194     }
00195 return result;
00196 }

Here is the call graph for this function:


Variable Documentation

char const rcsid[] = "$Id: quickHeap.c,v 1.1 2005/10/21 05:36:08 galt Exp $" [static]

Definition at line 30 of file quickHeap.c.


Generated on Tue Dec 25 20:14:14 2007 for blat by  doxygen 1.5.2