Warning, /include/Geant4/tools/glutess/priorityq is written in an unsupported language. File is not indexed.
0001 // see license file for original license.
0002
0003 #ifndef tools_glutess_priorityq
0004 #define tools_glutess_priorityq
0005
0006 #include <climits> /* LONG_MAX */
0007 #include "memalloc"
0008
0009 /* Include all the code for the regular heap-based queue here. */
0010
0011 /////////////////////////////////////////////////////////////////
0012 //#include "priorityq-heap.ic"
0013 //#include "priorityq-heap"
0014
0015 /* Use #define's so that another heap implementation can use this one */
0016
0017 #define PQkey PQHeapKey
0018 #define PQhandle PQHeapHandle
0019 #define PriorityQ PriorityQHeap
0020
0021 #define pqNewPriorityQ(leq) __gl_pqHeapNewPriorityQ(leq)
0022 #define pqDeletePriorityQ(pq) __gl_pqHeapDeletePriorityQ(pq)
0023
0024 /* The basic operations are insertion of a new key (pqInsert),
0025 * and examination/extraction of a key whose value is minimum
0026 * (pqMinimum/pqExtractMin). Deletion is also allowed (pqDelete);
0027 * for this purpose pqInsert returns a "handle" which is supplied
0028 * as the argument.
0029 *
0030 * An initial heap may be created efficiently by calling pqInsert
0031 * repeatedly, then calling pqInit. In any case pqInit must be called
0032 * before any operations other than pqInsert are used.
0033 *
0034 * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key.
0035 * This may also be tested with pqIsEmpty.
0036 */
0037 #define pqInit(pq) __gl_pqHeapInit(pq)
0038 #define pqInsert(pq,key) __gl_pqHeapInsert(pq,key)
0039 #define pqMinimum(pq) __gl_pqHeapMinimum(pq)
0040 #define pqExtractMin(pq) __gl_pqHeapExtractMin(pq)
0041 #define pqDelete(pq,handle) __gl_pqHeapDelete(pq,handle)
0042 #define pqIsEmpty(pq) __gl_pqHeapIsEmpty(pq)
0043
0044 /* Since we support deletion the data structure is a little more
0045 * complicated than an ordinary heap. "nodes" is the heap itself;
0046 * active nodes are stored in the range 1..pq->size. When the
0047 * heap exceeds its allocated size (pq->max), its size doubles.
0048 * The children of node i are nodes 2i and 2i+1.
0049 *
0050 * Each node stores an index into an array "handles". Each handle
0051 * stores a key, plus a pointer back to the node which currently
0052 * represents that key (ie. nodes[handles[i].node].handle == i).
0053 */
0054
0055 typedef void *PQkey;
0056 typedef long PQhandle;
0057 typedef struct PriorityQ PriorityQ;
0058
0059 typedef struct { PQhandle handle; } PQnode;
0060 typedef struct { PQkey key; PQhandle node; } PQhandleElem;
0061
0062 struct PriorityQ {
0063 PQnode *nodes;
0064 PQhandleElem *handles;
0065 long size, max;
0066 PQhandle freeList;
0067 int initialized;
0068 int (*leq)(PQkey key1, PQkey key2);
0069 };
0070
0071 #define __gl_pqHeapMinimum(pq) ((pq)->handles[(pq)->nodes[1].handle].key)
0072 #define __gl_pqHeapIsEmpty(pq) ((pq)->size == 0)
0073
0074 /////////////////////////////////////////////////////////////////
0075 /////////////////////////////////////////////////////////////////
0076 //#define INIT_SIZE 32
0077 inline long INIT_SIZE() {
0078 static const long s_value = 32;
0079 return s_value;
0080 }
0081
0082 /* Violates modularity, but a little faster */
0083 #include "geom"
0084 #define LEQ(x,y) VertLeq((GLUvertex *)x, (GLUvertex *)y)
0085
0086 /* really __gl_pqHeapNewPriorityQ */
0087 inline PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
0088 {
0089 PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
0090 if (pq == NULL) return NULL;
0091
0092 pq->size = 0;
0093 pq->max = INIT_SIZE();
0094 pq->nodes = (PQnode *)memAlloc( (INIT_SIZE() + 1) * sizeof(pq->nodes[0]) );
0095 if (pq->nodes == NULL) {
0096 memFree(pq);
0097 return NULL;
0098 }
0099
0100 pq->handles = (PQhandleElem *)memAlloc( (INIT_SIZE() + 1) * sizeof(pq->handles[0]) );
0101 if (pq->handles == NULL) {
0102 memFree(pq->nodes);
0103 memFree(pq);
0104 return NULL;
0105 }
0106
0107 pq->initialized = TOOLS_GLU_FALSE;
0108 pq->freeList = 0;
0109 pq->leq = leq;
0110
0111 pq->nodes[1].handle = 1; /* so that Minimum() returns NULL */
0112 pq->handles[1].key = NULL;
0113 return pq;
0114 }
0115
0116 /* really __gl_pqHeapDeletePriorityQ */
0117 inline void pqDeletePriorityQ( PriorityQ *pq )
0118 {
0119 memFree( pq->handles );
0120 memFree( pq->nodes );
0121 memFree( pq );
0122 }
0123
0124
0125 inline/*static*/ void static_FloatDown( PriorityQ *pq, long curr )
0126 {
0127 PQnode *n = pq->nodes;
0128 PQhandleElem *h = pq->handles;
0129 PQhandle hCurr, hChild;
0130 long child;
0131
0132 hCurr = n[curr].handle;
0133 for( ;; ) {
0134 child = curr << 1;
0135 if( child < pq->size && LEQ( h[n[child+1].handle].key,
0136 h[n[child].handle].key )) {
0137 ++child;
0138 }
0139
0140 assert(child <= pq->max);
0141
0142 hChild = n[child].handle;
0143 if( child > pq->size || LEQ( h[hCurr].key, h[hChild].key )) {
0144 n[curr].handle = hCurr;
0145 h[hCurr].node = curr;
0146 break;
0147 }
0148 n[curr].handle = hChild;
0149 h[hChild].node = curr;
0150 curr = child;
0151 }
0152 }
0153
0154
0155 inline/*static*/ void static_FloatUp( PriorityQ *pq, long curr )
0156 {
0157 PQnode *n = pq->nodes;
0158 PQhandleElem *h = pq->handles;
0159 PQhandle hCurr, hParent;
0160 long parent;
0161
0162 hCurr = n[curr].handle;
0163 for( ;; ) {
0164 parent = curr >> 1;
0165 hParent = n[parent].handle;
0166 if( parent == 0 || LEQ( h[hParent].key, h[hCurr].key )) {
0167 n[curr].handle = hCurr;
0168 h[hCurr].node = curr;
0169 break;
0170 }
0171 n[curr].handle = hParent;
0172 h[hParent].node = curr;
0173 curr = parent;
0174 }
0175 }
0176
0177 /* really __gl_pqHeapInit */
0178 inline void pqInit( PriorityQ *pq )
0179 {
0180 long i;
0181
0182 /* This method of building a heap is O(n), rather than O(n lg n). */
0183
0184 for( i = pq->size; i >= 1; --i ) {
0185 static_FloatDown( pq, i );
0186 }
0187 pq->initialized = TOOLS_GLU_TRUE;
0188 }
0189
0190 /* really __gl_pqHeapInsert */
0191 /* returns LONG_MAX iff out of memory */
0192 inline PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
0193 {
0194 long curr;
0195 PQhandle free;
0196
0197 curr = ++ pq->size;
0198 if( (curr*2) > pq->max ) {
0199 PQnode *saveNodes= pq->nodes;
0200 PQhandleElem *saveHandles= pq->handles;
0201
0202 /* If the heap overflows, double its size. */
0203 pq->max <<= 1;
0204 pq->nodes = (PQnode *)memRealloc( pq->nodes,
0205 (size_t)
0206 ((pq->max + 1) * sizeof( pq->nodes[0] )));
0207 if (pq->nodes == NULL) {
0208 pq->nodes = saveNodes; /* restore ptr to free upon return */
0209 return LONG_MAX;
0210 }
0211 pq->handles = (PQhandleElem *)memRealloc( pq->handles,
0212 (size_t)
0213 ((pq->max + 1) *
0214 sizeof( pq->handles[0] )));
0215 if (pq->handles == NULL) {
0216 pq->handles = saveHandles; /* restore ptr to free upon return */
0217 return LONG_MAX;
0218 }
0219 }
0220
0221 if( pq->freeList == 0 ) {
0222 free = curr;
0223 } else {
0224 free = pq->freeList;
0225 pq->freeList = pq->handles[free].node;
0226 }
0227
0228 pq->nodes[curr].handle = free;
0229 pq->handles[free].node = curr;
0230 pq->handles[free].key = keyNew;
0231
0232 if( pq->initialized ) {
0233 static_FloatUp( pq, curr );
0234 }
0235 assert(free != LONG_MAX);
0236 return free;
0237 }
0238
0239 /* really __gl_pqHeapExtractMin */
0240 inline PQkey pqExtractMin( PriorityQ *pq )
0241 {
0242 PQnode *n = pq->nodes;
0243 PQhandleElem *h = pq->handles;
0244 PQhandle hMin = n[1].handle;
0245 PQkey min = h[hMin].key;
0246
0247 if( pq->size > 0 ) {
0248 n[1].handle = n[pq->size].handle;
0249 h[n[1].handle].node = 1;
0250
0251 h[hMin].key = NULL;
0252 h[hMin].node = pq->freeList;
0253 pq->freeList = hMin;
0254
0255 if( -- pq->size > 0 ) {
0256 static_FloatDown( pq, 1 );
0257 }
0258 }
0259 return min;
0260 }
0261
0262 /* really __gl_pqHeapDelete */
0263 inline void pqDelete( PriorityQ *pq, PQhandle hCurr )
0264 {
0265 PQnode *n = pq->nodes;
0266 PQhandleElem *h = pq->handles;
0267 long curr;
0268
0269 assert( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL );
0270
0271 curr = h[hCurr].node;
0272 n[curr].handle = n[pq->size].handle;
0273 h[n[curr].handle].node = curr;
0274
0275 if( curr <= -- pq->size ) {
0276 if( curr <= 1 || LEQ( h[n[curr>>1].handle].key, h[n[curr].handle].key )) {
0277 static_FloatDown( pq, curr );
0278 } else {
0279 static_FloatUp( pq, curr );
0280 }
0281 }
0282 h[hCurr].key = NULL;
0283 h[hCurr].node = pq->freeList;
0284 pq->freeList = hCurr;
0285 }
0286
0287 /* Now redefine all the function names to map to their "Sort" versions. */
0288
0289 /////////////////////////////////////////////////////////////////
0290 //#include "priorityq-sort"
0291
0292 #undef PQkey
0293 #undef PQhandle
0294 #undef PriorityQ
0295 #undef pqNewPriorityQ
0296 #undef pqDeletePriorityQ
0297 #undef pqInit
0298 #undef pqInsert
0299 #undef pqMinimum
0300 #undef pqExtractMin
0301 #undef pqDelete
0302 #undef pqIsEmpty
0303
0304 /* Use #define's so that another heap implementation can use this one */
0305
0306 #define PQkey PQSortKey
0307 #define PQhandle PQSortHandle
0308 #define PriorityQ PriorityQSort
0309
0310 #define pqNewPriorityQ(leq) __gl_pqSortNewPriorityQ(leq)
0311 #define pqDeletePriorityQ(pq) __gl_pqSortDeletePriorityQ(pq)
0312
0313 /* The basic operations are insertion of a new key (pqInsert),
0314 * and examination/extraction of a key whose value is minimum
0315 * (pqMinimum/pqExtractMin). Deletion is also allowed (pqDelete);
0316 * for this purpose pqInsert returns a "handle" which is supplied
0317 * as the argument.
0318 *
0319 * An initial heap may be created efficiently by calling pqInsert
0320 * repeatedly, then calling pqInit. In any case pqInit must be called
0321 * before any operations other than pqInsert are used.
0322 *
0323 * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key.
0324 * This may also be tested with pqIsEmpty.
0325 */
0326 #define pqInit(pq) __gl_pqSortInit(pq)
0327 #define pqInsert(pq,key) __gl_pqSortInsert(pq,key)
0328 #define pqMinimum(pq) __gl_pqSortMinimum(pq)
0329 #define pqExtractMin(pq) __gl_pqSortExtractMin(pq)
0330 #define pqDelete(pq,handle) __gl_pqSortDelete(pq,handle)
0331 #define pqIsEmpty(pq) __gl_pqSortIsEmpty(pq)
0332
0333
0334 /* Since we support deletion the data structure is a little more
0335 * complicated than an ordinary heap. "nodes" is the heap itself;
0336 * active nodes are stored in the range 1..pq->size. When the
0337 * heap exceeds its allocated size (pq->max), its size doubles.
0338 * The children of node i are nodes 2i and 2i+1.
0339 *
0340 * Each node stores an index into an array "handles". Each handle
0341 * stores a key, plus a pointer back to the node which currently
0342 * represents that key (ie. nodes[handles[i].node].handle == i).
0343 */
0344
0345 typedef PQHeapKey PQkey;
0346 typedef PQHeapHandle PQhandle;
0347 typedef struct PriorityQ PriorityQ;
0348
0349 struct PriorityQ {
0350 PriorityQHeap *heap;
0351 PQkey *keys;
0352 PQkey **order;
0353 PQhandle size, max;
0354 int initialized;
0355 int (*leq)(PQkey key1, PQkey key2);
0356 };
0357
0358 /* really __gl_pqSortNewPriorityQ */
0359 inline PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
0360 {
0361 PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
0362 if (pq == NULL) return NULL;
0363
0364 pq->heap = __gl_pqHeapNewPriorityQ( leq );
0365 if (pq->heap == NULL) {
0366 memFree(pq);
0367 return NULL;
0368 }
0369
0370 pq->keys = (PQHeapKey *)memAlloc( INIT_SIZE() * sizeof(pq->keys[0]) );
0371 if (pq->keys == NULL) {
0372 __gl_pqHeapDeletePriorityQ(pq->heap);
0373 memFree(pq);
0374 return NULL;
0375 }
0376
0377 pq->size = 0;
0378 pq->max = INIT_SIZE();
0379 pq->initialized = TOOLS_GLU_FALSE;
0380 pq->leq = leq;
0381 return pq;
0382 }
0383
0384 /* really __gl_pqSortDeletePriorityQ */
0385 inline void pqDeletePriorityQ( PriorityQ *pq )
0386 {
0387 assert(pq != NULL);
0388 if (pq->heap != NULL) __gl_pqHeapDeletePriorityQ( pq->heap );
0389 if (pq->order != NULL) memFree( pq->order );
0390 if (pq->keys != NULL) memFree( pq->keys );
0391 memFree( pq );
0392 }
0393
0394
0395 #define LT(x,y) (! LEQ(y,x))
0396 #define GT(x,y) (! LEQ(x,y))
0397 //#define pq_Swap(a,b) if(1){PQkey *tmp = *a; *a = *b; *b = tmp;}else
0398 #define pq_Swap(a,b) do{PQkey *tmp = *a; *a = *b; *b = tmp;} while(false)
0399
0400 /* really __gl_pqSortInit */
0401 inline int pqInit( PriorityQ *pq )
0402 {
0403 PQkey **p, **r, **i, **j, *piv;
0404 struct { PQkey **p, **r; } Stack[50], *top = Stack;
0405 unsigned long seed = 2016473283;
0406
0407 /* Create an array of indirect pointers to the keys, so that we
0408 * the handles we have returned are still valid.
0409 */
0410 /*
0411 pq->order = (PQHeapKey **)memAlloc( (size_t)
0412 (pq->size * sizeof(pq->order[0])) );
0413 */
0414 pq->order = (PQHeapKey **)memAlloc( (size_t)
0415 ((pq->size+1) * sizeof(pq->order[0])) );
0416 /* the previous line is a patch to compensate for the fact that IBM */
0417 /* machines return a null on a malloc of zero bytes (unlike SGI), */
0418 /* so we have to put in this defense to guard against a memory */
0419 /* fault four lines down. from fossum@austin.ibm.com. */
0420 if (pq->order == NULL) return 0;
0421
0422 p = pq->order;
0423 r = p + pq->size - 1;
0424 for( piv = pq->keys, i = p; i <= r; ++piv, ++i ) {
0425 *i = piv;
0426 }
0427
0428 /* Sort the indirect pointers in descending order,
0429 * using randomized Quicksort
0430 */
0431 top->p = p; top->r = r; ++top;
0432 while( --top >= Stack ) {
0433 p = top->p;
0434 r = top->r;
0435 while( r > p + 10 ) {
0436 seed = seed * 1539415821 + 1;
0437 i = p + seed % (r - p + 1);
0438 piv = *i;
0439 *i = *p;
0440 *p = piv;
0441 i = p - 1;
0442 j = r + 1;
0443 do {
0444 do { ++i; } while( GT( **i, *piv ));
0445 do { --j; } while( LT( **j, *piv ));
0446 pq_Swap( i, j );
0447 } while( i < j );
0448 pq_Swap( i, j ); /* Undo last swap */
0449 if( i - p < r - j ) {
0450 top->p = j+1; top->r = r; ++top;
0451 r = i-1;
0452 } else {
0453 top->p = p; top->r = i-1; ++top;
0454 p = j+1;
0455 }
0456 }
0457 /* Insertion sort small lists */
0458 for( i = p+1; i <= r; ++i ) {
0459 piv = *i;
0460 for( j = i; j > p && LT( **(j-1), *piv ); --j ) {
0461 *j = *(j-1);
0462 }
0463 *j = piv;
0464 }
0465 }
0466 pq->max = pq->size;
0467 pq->initialized = TOOLS_GLU_TRUE;
0468 __gl_pqHeapInit( pq->heap ); /* always succeeds */
0469
0470 #ifndef NDEBUG
0471 p = pq->order;
0472 r = p + pq->size - 1;
0473 for( i = p; i < r; ++i ) {
0474 assert( LEQ( **(i+1), **i ));
0475 }
0476 #endif
0477
0478 return 1;
0479 }
0480
0481 /* really __gl_pqSortInsert */
0482 /* returns LONG_MAX iff out of memory */
0483 inline PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
0484 {
0485 long curr;
0486
0487 if( pq->initialized ) {
0488 return __gl_pqHeapInsert( pq->heap, keyNew );
0489 }
0490 curr = pq->size;
0491 if( ++ pq->size >= pq->max ) {
0492 PQkey *saveKey= pq->keys;
0493
0494 /* If the heap overflows, double its size. */
0495 pq->max <<= 1;
0496 pq->keys = (PQHeapKey *)memRealloc( pq->keys,
0497 (size_t)
0498 (pq->max * sizeof( pq->keys[0] )));
0499 if (pq->keys == NULL) {
0500 pq->keys = saveKey; /* restore ptr to free upon return */
0501 return LONG_MAX;
0502 }
0503 }
0504 assert(curr != LONG_MAX);
0505 pq->keys[curr] = keyNew;
0506
0507 /* Negative handles index the sorted array. */
0508 return -(curr+1);
0509 }
0510
0511 /* really __gl_pqSortExtractMin */
0512 inline PQkey pqExtractMin( PriorityQ *pq )
0513 {
0514 PQkey sortMin, heapMin;
0515
0516 if( pq->size == 0 ) {
0517 return __gl_pqHeapExtractMin( pq->heap );
0518 }
0519 sortMin = *(pq->order[pq->size-1]);
0520 if( ! __gl_pqHeapIsEmpty( pq->heap )) {
0521 heapMin = __gl_pqHeapMinimum( pq->heap );
0522 if( LEQ( heapMin, sortMin )) {
0523 return __gl_pqHeapExtractMin( pq->heap );
0524 }
0525 }
0526 do {
0527 -- pq->size;
0528 } while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL );
0529 return sortMin;
0530 }
0531
0532 /* really __gl_pqSortMinimum */
0533 inline PQkey pqMinimum( PriorityQ *pq )
0534 {
0535 PQkey sortMin, heapMin;
0536
0537 if( pq->size == 0 ) {
0538 return __gl_pqHeapMinimum( pq->heap );
0539 }
0540 sortMin = *(pq->order[pq->size-1]);
0541 if( ! __gl_pqHeapIsEmpty( pq->heap )) {
0542 heapMin = __gl_pqHeapMinimum( pq->heap );
0543 if( LEQ( heapMin, sortMin )) {
0544 return heapMin;
0545 }
0546 }
0547 return sortMin;
0548 }
0549
0550 /* really __gl_pqSortIsEmpty */
0551 inline int pqIsEmpty( PriorityQ *pq )
0552 {
0553 return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap );
0554 }
0555
0556 /* really __gl_pqSortDelete */
0557 inline void pqDelete( PriorityQ *pq, PQhandle curr )
0558 {
0559 if( curr >= 0 ) {
0560 __gl_pqHeapDelete( pq->heap, curr );
0561 return;
0562 }
0563 curr = -(curr+1);
0564 assert( curr < pq->max && pq->keys[curr] != NULL );
0565
0566 pq->keys[curr] = NULL;
0567 while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) {
0568 -- pq->size;
0569 }
0570 }
0571
0572 #endif