3 #ifndef tools_glutess_priorityq
4 #define tools_glutess_priorityq
17 #define PQkey PQHeapKey
18 #define PQhandle PQHeapHandle
19 #define PriorityQ PriorityQHeap
21 #define pqNewPriorityQ(leq) __gl_pqHeapNewPriorityQ(leq)
22 #define pqDeletePriorityQ(pq) __gl_pqHeapDeletePriorityQ(pq)
37 #define pqInit(pq) __gl_pqHeapInit(pq)
38 #define pqInsert(pq,key) __gl_pqHeapInsert(pq,key)
39 #define pqMinimum(pq) __gl_pqHeapMinimum(pq)
40 #define pqExtractMin(pq) __gl_pqHeapExtractMin(pq)
41 #define pqDelete(pq,handle) __gl_pqHeapDelete(pq,handle)
42 #define pqIsEmpty(pq) __gl_pqHeapIsEmpty(pq)
71 #define __gl_pqHeapMinimum(pq) ((pq)->handles[(pq)->nodes[1].handle].key)
72 #define __gl_pqHeapIsEmpty(pq) ((pq)->size == 0)
78 static const long s_value = 32;
84 #define LEQ(x,y) VertLeq((GLUvertex *)x, (GLUvertex *)y)
90 if (pq == NULL)
return NULL;
95 if (pq->
nodes == NULL) {
135 if( child < pq->
size &&
LEQ( h[n[child+1].handle].key,
136 h[n[child].handle].key )) {
140 assert(child <= pq->max);
143 if( child > pq->
size ||
LEQ( h[hCurr].key, h[hChild].key )) {
145 h[hCurr].
node = curr;
149 h[hChild].
node = curr;
165 hParent = n[parent].
handle;
166 if( parent == 0 ||
LEQ( h[hParent].key, h[hCurr].key )) {
168 h[hCurr].
node = curr;
172 h[hParent].
node = curr;
184 for( i = pq->
size; i >= 1; --i ) {
198 if( (curr*2) > pq->
max ) {
206 ((pq->
max + 1) *
sizeof( pq->
nodes[0] )));
207 if (pq->
nodes == NULL) {
208 pq->
nodes = saveNodes;
235 assert(free != LONG_MAX);
255 if( -- pq->
size > 0 ) {
269 assert( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL );
271 curr = h[hCurr].
node;
275 if( curr <= -- pq->
size ) {
276 if( curr <= 1 ||
LEQ( h[n[curr>>1].handle].key, h[n[curr].handle].key )) {
295 #undef pqNewPriorityQ
296 #undef pqDeletePriorityQ
306 #define PQkey PQSortKey
307 #define PQhandle PQSortHandle
308 #define PriorityQ PriorityQSort
310 #define pqNewPriorityQ(leq) __gl_pqSortNewPriorityQ(leq)
311 #define pqDeletePriorityQ(pq) __gl_pqSortDeletePriorityQ(pq)
326 #define pqInit(pq) __gl_pqSortInit(pq)
327 #define pqInsert(pq,key) __gl_pqSortInsert(pq,key)
328 #define pqMinimum(pq) __gl_pqSortMinimum(pq)
329 #define pqExtractMin(pq) __gl_pqSortExtractMin(pq)
330 #define pqDelete(pq,handle) __gl_pqSortDelete(pq,handle)
331 #define pqIsEmpty(pq) __gl_pqSortIsEmpty(pq)
345 typedef PQHeapKey
PQkey;
362 if (pq == NULL)
return NULL;
365 if (pq->
heap == NULL) {
371 if (pq->
keys == NULL) {
395 #define LT(x,y) (! LEQ(y,x))
396 #define GT(x,y) (! LEQ(x,y))
398 #define pq_Swap(a,b) do{PQkey *tmp = *a; *a = *b; *b = tmp;} while(false)
403 PQkey **p, **r, **i, **j, *piv;
404 struct {
PQkey **p, **r; } Stack[50], *
top = Stack;
405 unsigned long seed = 2016473283;
420 if (pq->
order == NULL)
return 0;
423 r = p + pq->
size - 1;
424 for( piv = pq->
keys, i = p; i <= r; ++piv, ++i ) {
432 while( --
top >= Stack ) {
435 while( r > p + 10 ) {
436 seed = seed * 1539415821 + 1;
437 i = p + seed % (r - p + 1);
444 do { ++i; }
while(
GT( **i, *piv ));
445 do { --j; }
while(
LT( **j, *piv ));
449 if( i - p < r - j ) {
458 for( i = p+1; i <= r; ++i ) {
460 for( j = i; j > p &&
LT( **(j-1), *piv ); --j ) {
472 r = p + pq->
size - 1;
473 for( i = p; i < r; ++i ) {
474 assert(
LEQ( **(i+1), **i ));
498 (pq->
max *
sizeof( pq->
keys[0] )));
499 if (pq->
keys == NULL) {
504 assert(curr != LONG_MAX);
505 pq->
keys[curr] = keyNew;
514 PQkey sortMin, heapMin;
516 if( pq->
size == 0 ) {
522 if(
LEQ( heapMin, sortMin )) {
535 PQkey sortMin, heapMin;
537 if( pq->
size == 0 ) {
543 if(
LEQ( heapMin, sortMin )) {
564 assert( curr < pq->max && pq->
keys[curr] != NULL );
566 pq->
keys[curr] = NULL;