53 if (pq == NULL)
return NULL;
55 pq->
heap = __gl_pqHeapNewPriorityQ( leq );
56 if (pq->
heap == NULL) {
62 if (pq->
keys == NULL) {
63 __gl_pqHeapDeletePriorityQ(pq->
heap);
80 if (pq->
heap != NULL) __gl_pqHeapDeletePriorityQ( pq->
heap );
87#define LT(x,y) (! LEQ(y,x))
88#define GT(x,y) (! LEQ(x,y))
89#define Swap(a,b) do{PQkey *tmp = *a; *a = *b; *b = tmp;}while(0)
94 PQkey **p, **
r, **i, **j, *piv;
95 struct {
PQkey **p, **
r; } Stack[50], *top = Stack;
96 unsigned long seed = 2016473283;
111 if (pq->
order == NULL)
return 0;
114 r = p + pq->
size - 1;
115 for( piv = pq->
keys, i = p; i <=
r; ++piv, ++i ) {
122 top->p = p; top->r =
r; ++top;
123 while( --top >= Stack ) {
126 while(
r > p + 10 ) {
127 seed = seed * 1539415821 + 1;
128 i = p + seed % (
r - p + 1);
135 do { ++i; }
while(
GT( **i, *piv ));
136 do { --j; }
while(
LT( **j, *piv ));
140 if( i - p <
r - j ) {
141 top->p = j+1; top->r =
r; ++top;
144 top->p = p; top->r = i-1; ++top;
149 for( i = p+1; i <=
r; ++i ) {
151 for( j = i; j > p &&
LT( **(j-1), *piv ); --j ) {
159 __gl_pqHeapInit( pq->
heap );
163 r = p + pq->
size - 1;
164 for( i = p; i <
r; ++i ) {
165 assert(
LEQ( **(i+1), **i ));
179 return __gl_pqHeapInsert( pq->
heap, keyNew );
189 (pq->
max *
sizeof( pq->
keys[0] )));
190 if (pq->
keys == NULL) {
195 assert(curr != LONG_MAX);
196 pq->
keys[curr] = keyNew;
205 PQkey sortMin, heapMin;
207 if( pq->
size == 0 ) {
208 return __gl_pqHeapExtractMin( pq->
heap );
213 if(
LEQ( heapMin, sortMin )) {
214 return __gl_pqHeapExtractMin( pq->
heap );
226 PQkey sortMin, heapMin;
228 if( pq->
size == 0 ) {
234 if(
LEQ( heapMin, sortMin )) {
251 __gl_pqHeapDelete( pq->
heap, curr );
255 assert( curr < pq->max && pq->
keys[curr] != NULL );
257 pq->
keys[curr] = NULL;
#define __gl_pqHeapMinimum(pq)
#define __gl_pqHeapIsEmpty(pq)
PQkey pqMinimum(PriorityQ *pq)
void pqDeletePriorityQ(PriorityQ *pq)
void pqDelete(PriorityQ *pq, PQhandle curr)
int pqInit(PriorityQ *pq)
int pqIsEmpty(PriorityQ *pq)
PriorityQ * pqNewPriorityQ(int(*leq)(PQkey key1, PQkey key2))
PQkey pqExtractMin(PriorityQ *pq)
PQhandle pqInsert(PriorityQ *pq, PQkey keyNew)
int(* leq)(PQkey key1, PQkey key2)