Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenpriorityq.c
Go to the documentation of this file.
00001 /* 00002 ** License Applicability. Except to the extent portions of this file are 00003 ** made subject to an alternative license as permitted in the SGI Free 00004 ** Software License B, Version 1.1 (the "License"), the contents of this 00005 ** file are subject only to the provisions of the License. You may not use 00006 ** this file except in compliance with the License. You may obtain a copy 00007 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600 00008 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at: 00009 ** 00010 ** http://oss.sgi.com/projects/FreeB 00011 ** 00012 ** Note that, as provided in the License, the Software is distributed on an 00013 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS 00014 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND 00015 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A 00016 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT. 00017 ** 00018 ** Original Code. The Original Code is: OpenGL Sample Implementation, 00019 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics, 00020 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc. 00021 ** Copyright in any portions created by third parties is as indicated 00022 ** elsewhere herein. All Rights Reserved. 00023 ** 00024 ** Additional Notice Provisions: The application programming interfaces 00025 ** established by SGI in conjunction with the Original Code are The 00026 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released 00027 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version 00028 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X 00029 ** Window System(R) (Version 1.3), released October 19, 1998. This software 00030 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation 00031 ** published by SGI, but has not been independently verified as being 00032 ** compliant with the OpenGL(R) version 1.2.1 Specification. 00033 ** 00034 */ 00035 /* 00036 ** Author: Eric Veach, July 1994. 00037 ** 00038 ** $Date: 2007-10-19 23:21:45 +0000 (Fri, 19 Oct 2007) $ $Revision: 1.1 $ 00039 ** $Header: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/glu32/libtess/priorityq.c,v 1.1 2004/02/02 16:39:15 navaraf Exp $ 00040 */ 00041 00042 #include "gluos.h" 00043 #include <stddef.h> 00044 #include <assert.h> 00045 #include <limits.h> /* LONG_MAX */ 00046 #include "memalloc.h" 00047 00048 /* Include all the code for the regular heap-based queue here. */ 00049 00050 #include "priorityq-heap.c" 00051 00052 /* Now redefine all the function names to map to their "Sort" versions. */ 00053 00054 #include "priorityq-sort.h" 00055 00056 /* really __gl_pqSortNewPriorityQ */ 00057 PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) ) 00058 { 00059 PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ )); 00060 if (pq == NULL) return NULL; 00061 00062 pq->heap = __gl_pqHeapNewPriorityQ( leq ); 00063 if (pq->heap == NULL) { 00064 memFree(pq); 00065 return NULL; 00066 } 00067 00068 pq->keys = (PQHeapKey *)memAlloc( INIT_SIZE * sizeof(pq->keys[0]) ); 00069 if (pq->keys == NULL) { 00070 __gl_pqHeapDeletePriorityQ(pq->heap); 00071 memFree(pq); 00072 return NULL; 00073 } 00074 00075 pq->size = 0; 00076 pq->max = INIT_SIZE; 00077 pq->initialized = FALSE; 00078 pq->leq = leq; 00079 return pq; 00080 } 00081 00082 /* really __gl_pqSortDeletePriorityQ */ 00083 void pqDeletePriorityQ( PriorityQ *pq ) 00084 { 00085 assert(pq != NULL); 00086 if (pq->heap != NULL) __gl_pqHeapDeletePriorityQ( pq->heap ); 00087 if (pq->order != NULL) memFree( pq->order ); 00088 if (pq->keys != NULL) memFree( pq->keys ); 00089 memFree( pq ); 00090 } 00091 00092 00093 #define LT(x,y) (! LEQ(y,x)) 00094 #define GT(x,y) (! LEQ(x,y)) 00095 #define Swap(a,b) if(1){PQkey *tmp = *a; *a = *b; *b = tmp;}else 00096 00097 /* really __gl_pqSortInit */ 00098 int pqInit( PriorityQ *pq ) 00099 { 00100 PQkey **p, **r, **i, **j, *piv; 00101 struct { PQkey **p, **r; } Stack[50], *top = Stack; 00102 unsigned long seed = 2016473283; 00103 00104 /* Create an array of indirect pointers to the keys, so that we 00105 * the handles we have returned are still valid. 00106 */ 00107 /* 00108 pq->order = (PQHeapKey **)memAlloc( (size_t) 00109 (pq->size * sizeof(pq->order[0])) ); 00110 */ 00111 pq->order = (PQHeapKey **)memAlloc( (size_t) 00112 ((pq->size+1) * sizeof(pq->order[0])) ); 00113 /* the previous line is a patch to compensate for the fact that IBM */ 00114 /* machines return a null on a malloc of zero bytes (unlike SGI), */ 00115 /* so we have to put in this defense to guard against a memory */ 00116 /* fault four lines down. from fossum@austin.ibm.com. */ 00117 if (pq->order == NULL) return 0; 00118 00119 p = pq->order; 00120 r = p + pq->size - 1; 00121 for( piv = pq->keys, i = p; i <= r; ++piv, ++i ) { 00122 *i = piv; 00123 } 00124 00125 /* Sort the indirect pointers in descending order, 00126 * using randomized Quicksort 00127 */ 00128 top->p = p; top->r = r; ++top; 00129 while( --top >= Stack ) { 00130 p = top->p; 00131 r = top->r; 00132 while( r > p + 10 ) { 00133 seed = seed * 1539415821 + 1; 00134 i = p + seed % (r - p + 1); 00135 piv = *i; 00136 *i = *p; 00137 *p = piv; 00138 i = p - 1; 00139 j = r + 1; 00140 do { 00141 do { ++i; } while( GT( **i, *piv )); 00142 do { --j; } while( LT( **j, *piv )); 00143 Swap( i, j ); 00144 } while( i < j ); 00145 Swap( i, j ); /* Undo last swap */ 00146 if( i - p < r - j ) { 00147 top->p = j+1; top->r = r; ++top; 00148 r = i-1; 00149 } else { 00150 top->p = p; top->r = i-1; ++top; 00151 p = j+1; 00152 } 00153 } 00154 /* Insertion sort small lists */ 00155 for( i = p+1; i <= r; ++i ) { 00156 piv = *i; 00157 for( j = i; j > p && LT( **(j-1), *piv ); --j ) { 00158 *j = *(j-1); 00159 } 00160 *j = piv; 00161 } 00162 } 00163 pq->max = pq->size; 00164 pq->initialized = TRUE; 00165 __gl_pqHeapInit( pq->heap ); /* always succeeds */ 00166 00167 #ifndef NDEBUG 00168 p = pq->order; 00169 r = p + pq->size - 1; 00170 for( i = p; i < r; ++i ) { 00171 assert( LEQ( **(i+1), **i )); 00172 } 00173 #endif 00174 00175 return 1; 00176 } 00177 00178 /* really __gl_pqSortInsert */ 00179 /* returns LONG_MAX iff out of memory */ 00180 PQhandle pqInsert( PriorityQ *pq, PQkey keyNew ) 00181 { 00182 long curr; 00183 00184 if( pq->initialized ) { 00185 return __gl_pqHeapInsert( pq->heap, keyNew ); 00186 } 00187 curr = pq->size; 00188 if( ++ pq->size >= pq->max ) { 00189 PQkey *saveKey= pq->keys; 00190 00191 /* If the heap overflows, double its size. */ 00192 pq->max <<= 1; 00193 pq->keys = (PQHeapKey *)memRealloc( pq->keys, 00194 (size_t) 00195 (pq->max * sizeof( pq->keys[0] ))); 00196 if (pq->keys == NULL) { 00197 pq->keys = saveKey; /* restore ptr to free upon return */ 00198 return LONG_MAX; 00199 } 00200 } 00201 assert(curr != LONG_MAX); 00202 pq->keys[curr] = keyNew; 00203 00204 /* Negative handles index the sorted array. */ 00205 return -(curr+1); 00206 } 00207 00208 /* really __gl_pqSortExtractMin */ 00209 PQkey pqExtractMin( PriorityQ *pq ) 00210 { 00211 PQkey sortMin, heapMin; 00212 00213 if( pq->size == 0 ) { 00214 return __gl_pqHeapExtractMin( pq->heap ); 00215 } 00216 sortMin = *(pq->order[pq->size-1]); 00217 if( ! __gl_pqHeapIsEmpty( pq->heap )) { 00218 heapMin = __gl_pqHeapMinimum( pq->heap ); 00219 if( LEQ( heapMin, sortMin )) { 00220 return __gl_pqHeapExtractMin( pq->heap ); 00221 } 00222 } 00223 do { 00224 -- pq->size; 00225 } while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ); 00226 return sortMin; 00227 } 00228 00229 /* really __gl_pqSortMinimum */ 00230 PQkey pqMinimum( PriorityQ *pq ) 00231 { 00232 PQkey sortMin, heapMin; 00233 00234 if( pq->size == 0 ) { 00235 return __gl_pqHeapMinimum( pq->heap ); 00236 } 00237 sortMin = *(pq->order[pq->size-1]); 00238 if( ! __gl_pqHeapIsEmpty( pq->heap )) { 00239 heapMin = __gl_pqHeapMinimum( pq->heap ); 00240 if( LEQ( heapMin, sortMin )) { 00241 return heapMin; 00242 } 00243 } 00244 return sortMin; 00245 } 00246 00247 /* really __gl_pqSortIsEmpty */ 00248 int pqIsEmpty( PriorityQ *pq ) 00249 { 00250 return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap ); 00251 } 00252 00253 /* really __gl_pqSortDelete */ 00254 void pqDelete( PriorityQ *pq, PQhandle curr ) 00255 { 00256 if( curr >= 0 ) { 00257 __gl_pqHeapDelete( pq->heap, curr ); 00258 return; 00259 } 00260 curr = -(curr+1); 00261 assert( curr < pq->max && pq->keys[curr] != NULL ); 00262 00263 pq->keys[curr] = NULL; 00264 while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) { 00265 -- pq->size; 00266 } 00267 } Generated on Fri May 25 2012 04:21:58 for ReactOS by
1.7.6.1
|