Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / glu / sgi / libtess / priorityq-heap.c
1 /*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30 /*
31 ** Author: Eric Veach, July 1994.
32 **
33 */
34
35 #include <stddef.h>
36 #include <assert.h>
37 #include "priorityq-heap.h"
38 #include "memalloc.h"
39
40 #define INIT_SIZE 32
41
42 #define TRUE 1
43 #define FALSE 0
44
45 #ifdef FOR_TRITE_TEST_PROGRAM
46 #define LEQ(x,y) (*pq->leq)(x,y)
47 #else
48 /* Violates modularity, but a little faster */
49 #include "geom.h"
50 #define LEQ(x,y) VertLeq((GLUvertex *)x, (GLUvertex *)y)
51 #endif
52
53 /* really __gl_pqHeapNewPriorityQ */
54 PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
55 {
56 PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
57 if (pq == NULL) return NULL;
58
59 pq->size = 0;
60 pq->max = INIT_SIZE;
61 pq->nodes = (PQnode *)memAlloc( (INIT_SIZE + 1) * sizeof(pq->nodes[0]) );
62 if (pq->nodes == NULL) {
63 memFree(pq);
64 return NULL;
65 }
66
67 pq->handles = (PQhandleElem *)memAlloc( (INIT_SIZE + 1) * sizeof(pq->handles[0]) );
68 if (pq->handles == NULL) {
69 memFree(pq->nodes);
70 memFree(pq);
71 return NULL;
72 }
73
74 pq->initialized = FALSE;
75 pq->freeList = 0;
76 pq->leq = leq;
77
78 pq->nodes[1].handle = 1; /* so that Minimum() returns NULL */
79 pq->handles[1].key = NULL;
80 return pq;
81 }
82
83 /* really __gl_pqHeapDeletePriorityQ */
84 void pqDeletePriorityQ( PriorityQ *pq )
85 {
86 memFree( pq->handles );
87 memFree( pq->nodes );
88 memFree( pq );
89 }
90
91
92 static void FloatDown( PriorityQ *pq, long curr )
93 {
94 PQnode *n = pq->nodes;
95 PQhandleElem *h = pq->handles;
96 PQhandle hCurr, hChild;
97 long child;
98
99 hCurr = n[curr].handle;
100 for( ;; ) {
101 child = curr << 1;
102 if( child < pq->size && LEQ( h[n[child+1].handle].key,
103 h[n[child].handle].key )) {
104 ++child;
105 }
106
107 assert(child <= pq->max);
108
109 hChild = n[child].handle;
110 if( child > pq->size || LEQ( h[hCurr].key, h[hChild].key )) {
111 n[curr].handle = hCurr;
112 h[hCurr].node = curr;
113 break;
114 }
115 n[curr].handle = hChild;
116 h[hChild].node = curr;
117 curr = child;
118 }
119 }
120
121
122 static void FloatUp( PriorityQ *pq, long curr )
123 {
124 PQnode *n = pq->nodes;
125 PQhandleElem *h = pq->handles;
126 PQhandle hCurr, hParent;
127 long parent;
128
129 hCurr = n[curr].handle;
130 for( ;; ) {
131 parent = curr >> 1;
132 hParent = n[parent].handle;
133 if( parent == 0 || LEQ( h[hParent].key, h[hCurr].key )) {
134 n[curr].handle = hCurr;
135 h[hCurr].node = curr;
136 break;
137 }
138 n[curr].handle = hParent;
139 h[hParent].node = curr;
140 curr = parent;
141 }
142 }
143
144 /* really __gl_pqHeapInit */
145 void pqInit( PriorityQ *pq )
146 {
147 long i;
148
149 /* This method of building a heap is O(n), rather than O(n lg n). */
150
151 for( i = pq->size; i >= 1; --i ) {
152 FloatDown( pq, i );
153 }
154 pq->initialized = TRUE;
155 }
156
157 /* really __gl_pqHeapInsert */
158 /* returns LONG_MAX iff out of memory */
159 PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
160 {
161 long curr;
162 PQhandle free;
163
164 curr = ++ pq->size;
165 if( (curr*2) > pq->max ) {
166 PQnode *saveNodes= pq->nodes;
167 PQhandleElem *saveHandles= pq->handles;
168
169 /* If the heap overflows, double its size. */
170 pq->max <<= 1;
171 pq->nodes = (PQnode *)memRealloc( pq->nodes,
172 (size_t)
173 ((pq->max + 1) * sizeof( pq->nodes[0] )));
174 if (pq->nodes == NULL) {
175 pq->nodes = saveNodes; /* restore ptr to free upon return */
176 return LONG_MAX;
177 }
178 pq->handles = (PQhandleElem *)memRealloc( pq->handles,
179 (size_t)
180 ((pq->max + 1) *
181 sizeof( pq->handles[0] )));
182 if (pq->handles == NULL) {
183 pq->handles = saveHandles; /* restore ptr to free upon return */
184 return LONG_MAX;
185 }
186 }
187
188 if( pq->freeList == 0 ) {
189 free = curr;
190 } else {
191 free = pq->freeList;
192 pq->freeList = pq->handles[free].node;
193 }
194
195 pq->nodes[curr].handle = free;
196 pq->handles[free].node = curr;
197 pq->handles[free].key = keyNew;
198
199 if( pq->initialized ) {
200 FloatUp( pq, curr );
201 }
202 assert(free != LONG_MAX);
203 return free;
204 }
205
206 /* really __gl_pqHeapExtractMin */
207 PQkey pqExtractMin( PriorityQ *pq )
208 {
209 PQnode *n = pq->nodes;
210 PQhandleElem *h = pq->handles;
211 PQhandle hMin = n[1].handle;
212 PQkey min = h[hMin].key;
213
214 if( pq->size > 0 ) {
215 n[1].handle = n[pq->size].handle;
216 h[n[1].handle].node = 1;
217
218 h[hMin].key = NULL;
219 h[hMin].node = pq->freeList;
220 pq->freeList = hMin;
221
222 if( -- pq->size > 0 ) {
223 FloatDown( pq, 1 );
224 }
225 }
226 return min;
227 }
228
229 /* really __gl_pqHeapDelete */
230 void pqDelete( PriorityQ *pq, PQhandle hCurr )
231 {
232 PQnode *n = pq->nodes;
233 PQhandleElem *h = pq->handles;
234 long curr;
235
236 assert( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL );
237
238 curr = h[hCurr].node;
239 n[curr].handle = n[pq->size].handle;
240 h[n[curr].handle].node = curr;
241
242 if( curr <= -- pq->size ) {
243 if( curr <= 1 || LEQ( h[n[curr>>1].handle].key, h[n[curr].handle].key )) {
244 FloatDown( pq, curr );
245 } else {
246 FloatUp( pq, curr );
247 }
248 }
249 h[hCurr].key = NULL;
250 h[hCurr].node = pq->freeList;
251 pq->freeList = hCurr;
252 }