Remove CVS keywords.
[mesa.git] / src / glu / sgi / libtess / priorityq.c
1 /*
2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17 **
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
23 **
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
33 **
34 */
35 /*
36 ** Author: Eric Veach, July 1994.
37 **
38 */
39
40 #include "gluos.h"
41 #include <stddef.h>
42 #include <assert.h>
43 #include <limits.h> /* LONG_MAX */
44 #include "memalloc.h"
45
46 /* Include all the code for the regular heap-based queue here. */
47
48 #include "priorityq-heap.c"
49
50 /* Now redefine all the function names to map to their "Sort" versions. */
51
52 #include "priorityq-sort.h"
53
54 /* really __gl_pqSortNewPriorityQ */
55 PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
56 {
57 PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
58 if (pq == NULL) return NULL;
59
60 pq->heap = __gl_pqHeapNewPriorityQ( leq );
61 if (pq->heap == NULL) {
62 memFree(pq);
63 return NULL;
64 }
65
66 pq->keys = (PQHeapKey *)memAlloc( INIT_SIZE * sizeof(pq->keys[0]) );
67 if (pq->keys == NULL) {
68 __gl_pqHeapDeletePriorityQ(pq->heap);
69 memFree(pq);
70 return NULL;
71 }
72
73 pq->size = 0;
74 pq->max = INIT_SIZE;
75 pq->initialized = FALSE;
76 pq->leq = leq;
77 return pq;
78 }
79
80 /* really __gl_pqSortDeletePriorityQ */
81 void pqDeletePriorityQ( PriorityQ *pq )
82 {
83 assert(pq != NULL);
84 if (pq->heap != NULL) __gl_pqHeapDeletePriorityQ( pq->heap );
85 if (pq->order != NULL) memFree( pq->order );
86 if (pq->keys != NULL) memFree( pq->keys );
87 memFree( pq );
88 }
89
90
91 #define LT(x,y) (! LEQ(y,x))
92 #define GT(x,y) (! LEQ(x,y))
93 #define Swap(a,b) if(1){PQkey *tmp = *a; *a = *b; *b = tmp;}else
94
95 /* really __gl_pqSortInit */
96 int pqInit( PriorityQ *pq )
97 {
98 PQkey **p, **r, **i, **j, *piv;
99 struct { PQkey **p, **r; } Stack[50], *top = Stack;
100 unsigned long seed = 2016473283;
101
102 /* Create an array of indirect pointers to the keys, so that we
103 * the handles we have returned are still valid.
104 */
105 /*
106 pq->order = (PQHeapKey **)memAlloc( (size_t)
107 (pq->size * sizeof(pq->order[0])) );
108 */
109 pq->order = (PQHeapKey **)memAlloc( (size_t)
110 ((pq->size+1) * sizeof(pq->order[0])) );
111 /* the previous line is a patch to compensate for the fact that IBM */
112 /* machines return a null on a malloc of zero bytes (unlike SGI), */
113 /* so we have to put in this defense to guard against a memory */
114 /* fault four lines down. from fossum@austin.ibm.com. */
115 if (pq->order == NULL) return 0;
116
117 p = pq->order;
118 r = p + pq->size - 1;
119 for( piv = pq->keys, i = p; i <= r; ++piv, ++i ) {
120 *i = piv;
121 }
122
123 /* Sort the indirect pointers in descending order,
124 * using randomized Quicksort
125 */
126 top->p = p; top->r = r; ++top;
127 while( --top >= Stack ) {
128 p = top->p;
129 r = top->r;
130 while( r > p + 10 ) {
131 seed = seed * 1539415821 + 1;
132 i = p + seed % (r - p + 1);
133 piv = *i;
134 *i = *p;
135 *p = piv;
136 i = p - 1;
137 j = r + 1;
138 do {
139 do { ++i; } while( GT( **i, *piv ));
140 do { --j; } while( LT( **j, *piv ));
141 Swap( i, j );
142 } while( i < j );
143 Swap( i, j ); /* Undo last swap */
144 if( i - p < r - j ) {
145 top->p = j+1; top->r = r; ++top;
146 r = i-1;
147 } else {
148 top->p = p; top->r = i-1; ++top;
149 p = j+1;
150 }
151 }
152 /* Insertion sort small lists */
153 for( i = p+1; i <= r; ++i ) {
154 piv = *i;
155 for( j = i; j > p && LT( **(j-1), *piv ); --j ) {
156 *j = *(j-1);
157 }
158 *j = piv;
159 }
160 }
161 pq->max = pq->size;
162 pq->initialized = TRUE;
163 __gl_pqHeapInit( pq->heap ); /* always succeeds */
164
165 #ifndef NDEBUG
166 p = pq->order;
167 r = p + pq->size - 1;
168 for( i = p; i < r; ++i ) {
169 assert( LEQ( **(i+1), **i ));
170 }
171 #endif
172
173 return 1;
174 }
175
176 /* really __gl_pqSortInsert */
177 /* returns LONG_MAX iff out of memory */
178 PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
179 {
180 long curr;
181
182 if( pq->initialized ) {
183 return __gl_pqHeapInsert( pq->heap, keyNew );
184 }
185 curr = pq->size;
186 if( ++ pq->size >= pq->max ) {
187 PQkey *saveKey= pq->keys;
188
189 /* If the heap overflows, double its size. */
190 pq->max <<= 1;
191 pq->keys = (PQHeapKey *)memRealloc( pq->keys,
192 (size_t)
193 (pq->max * sizeof( pq->keys[0] )));
194 if (pq->keys == NULL) {
195 pq->keys = saveKey; /* restore ptr to free upon return */
196 return LONG_MAX;
197 }
198 }
199 assert(curr != LONG_MAX);
200 pq->keys[curr] = keyNew;
201
202 /* Negative handles index the sorted array. */
203 return -(curr+1);
204 }
205
206 /* really __gl_pqSortExtractMin */
207 PQkey pqExtractMin( PriorityQ *pq )
208 {
209 PQkey sortMin, heapMin;
210
211 if( pq->size == 0 ) {
212 return __gl_pqHeapExtractMin( pq->heap );
213 }
214 sortMin = *(pq->order[pq->size-1]);
215 if( ! __gl_pqHeapIsEmpty( pq->heap )) {
216 heapMin = __gl_pqHeapMinimum( pq->heap );
217 if( LEQ( heapMin, sortMin )) {
218 return __gl_pqHeapExtractMin( pq->heap );
219 }
220 }
221 do {
222 -- pq->size;
223 } while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL );
224 return sortMin;
225 }
226
227 /* really __gl_pqSortMinimum */
228 PQkey pqMinimum( PriorityQ *pq )
229 {
230 PQkey sortMin, heapMin;
231
232 if( pq->size == 0 ) {
233 return __gl_pqHeapMinimum( pq->heap );
234 }
235 sortMin = *(pq->order[pq->size-1]);
236 if( ! __gl_pqHeapIsEmpty( pq->heap )) {
237 heapMin = __gl_pqHeapMinimum( pq->heap );
238 if( LEQ( heapMin, sortMin )) {
239 return heapMin;
240 }
241 }
242 return sortMin;
243 }
244
245 /* really __gl_pqSortIsEmpty */
246 int pqIsEmpty( PriorityQ *pq )
247 {
248 return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap );
249 }
250
251 /* really __gl_pqSortDelete */
252 void pqDelete( PriorityQ *pq, PQhandle curr )
253 {
254 if( curr >= 0 ) {
255 __gl_pqHeapDelete( pq->heap, curr );
256 return;
257 }
258 curr = -(curr+1);
259 assert( curr < pq->max && pq->keys[curr] != NULL );
260
261 pq->keys[curr] = NULL;
262 while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) {
263 -- pq->size;
264 }
265 }