f46bbb017c0e522539c0c0d9ff2180728efaf7ed
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
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:
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.
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
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.
31 ** Author: Eric Veach, July 1994.
33 ** $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
34 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libtess/priorityq-heap.c,v 1.1 2001/03/17 00:25:41 brianp Exp $
39 #include "priorityq-heap.h"
47 #ifdef FOR_TRITE_TEST_PROGRAM
48 #define LEQ(x,y) (*pq->leq)(x,y)
50 /* Violates modularity, but a little faster */
52 #define LEQ(x,y) VertLeq((GLUvertex *)x, (GLUvertex *)y)
55 /* really __gl_pqHeapNewPriorityQ */
56 PriorityQ
*pqNewPriorityQ( int (*leq
)(PQkey key1
, PQkey key2
) )
58 PriorityQ
*pq
= (PriorityQ
*)memAlloc( sizeof( PriorityQ
));
59 if (pq
== NULL
) return NULL
;
63 pq
->nodes
= (PQnode
*)memAlloc( (INIT_SIZE
+ 1) * sizeof(pq
->nodes
[0]) );
64 if (pq
->nodes
== NULL
) {
69 pq
->handles
= (PQhandleElem
*)memAlloc( (INIT_SIZE
+ 1) * sizeof(pq
->handles
[0]) );
70 if (pq
->handles
== NULL
) {
76 pq
->initialized
= FALSE
;
80 pq
->nodes
[1].handle
= 1; /* so that Minimum() returns NULL */
81 pq
->handles
[1].key
= NULL
;
85 /* really __gl_pqHeapDeletePriorityQ */
86 void pqDeletePriorityQ( PriorityQ
*pq
)
88 memFree( pq
->handles
);
94 static void FloatDown( PriorityQ
*pq
, long curr
)
96 PQnode
*n
= pq
->nodes
;
97 PQhandleElem
*h
= pq
->handles
;
98 PQhandle hCurr
, hChild
;
101 hCurr
= n
[curr
].handle
;
104 if( child
< pq
->size
&& LEQ( h
[n
[child
+1].handle
].key
,
105 h
[n
[child
].handle
].key
)) {
109 assert(child
<= pq
->max
);
111 hChild
= n
[child
].handle
;
112 if( child
> pq
->size
|| LEQ( h
[hCurr
].key
, h
[hChild
].key
)) {
113 n
[curr
].handle
= hCurr
;
114 h
[hCurr
].node
= curr
;
117 n
[curr
].handle
= hChild
;
118 h
[hChild
].node
= curr
;
124 static void FloatUp( PriorityQ
*pq
, long curr
)
126 PQnode
*n
= pq
->nodes
;
127 PQhandleElem
*h
= pq
->handles
;
128 PQhandle hCurr
, hParent
;
131 hCurr
= n
[curr
].handle
;
134 hParent
= n
[parent
].handle
;
135 if( parent
== 0 || LEQ( h
[hParent
].key
, h
[hCurr
].key
)) {
136 n
[curr
].handle
= hCurr
;
137 h
[hCurr
].node
= curr
;
140 n
[curr
].handle
= hParent
;
141 h
[hParent
].node
= curr
;
146 /* really __gl_pqHeapInit */
147 void pqInit( PriorityQ
*pq
)
151 /* This method of building a heap is O(n), rather than O(n lg n). */
153 for( i
= pq
->size
; i
>= 1; --i
) {
156 pq
->initialized
= TRUE
;
159 /* really __gl_pqHeapInsert */
160 /* returns LONG_MAX iff out of memory */
161 PQhandle
pqInsert( PriorityQ
*pq
, PQkey keyNew
)
167 if( (curr
*2) > pq
->max
) {
168 PQnode
*saveNodes
= pq
->nodes
;
169 PQhandleElem
*saveHandles
= pq
->handles
;
171 /* If the heap overflows, double its size. */
173 pq
->nodes
= (PQnode
*)memRealloc( pq
->nodes
,
175 ((pq
->max
+ 1) * sizeof( pq
->nodes
[0] )));
176 if (pq
->nodes
== NULL
) {
177 pq
->nodes
= saveNodes
; /* restore ptr to free upon return */
180 pq
->handles
= (PQhandleElem
*)memRealloc( pq
->handles
,
183 sizeof( pq
->handles
[0] )));
184 if (pq
->handles
== NULL
) {
185 pq
->handles
= saveHandles
; /* restore ptr to free upon return */
190 if( pq
->freeList
== 0 ) {
194 pq
->freeList
= pq
->handles
[free
].node
;
197 pq
->nodes
[curr
].handle
= free
;
198 pq
->handles
[free
].node
= curr
;
199 pq
->handles
[free
].key
= keyNew
;
201 if( pq
->initialized
) {
204 assert(free
!= LONG_MAX
);
208 /* really __gl_pqHeapExtractMin */
209 PQkey
pqExtractMin( PriorityQ
*pq
)
211 PQnode
*n
= pq
->nodes
;
212 PQhandleElem
*h
= pq
->handles
;
213 PQhandle hMin
= n
[1].handle
;
214 PQkey min
= h
[hMin
].key
;
217 n
[1].handle
= n
[pq
->size
].handle
;
218 h
[n
[1].handle
].node
= 1;
221 h
[hMin
].node
= pq
->freeList
;
224 if( -- pq
->size
> 0 ) {
231 /* really __gl_pqHeapDelete */
232 void pqDelete( PriorityQ
*pq
, PQhandle hCurr
)
234 PQnode
*n
= pq
->nodes
;
235 PQhandleElem
*h
= pq
->handles
;
238 assert( hCurr
>= 1 && hCurr
<= pq
->max
&& h
[hCurr
].key
!= NULL
);
240 curr
= h
[hCurr
].node
;
241 n
[curr
].handle
= n
[pq
->size
].handle
;
242 h
[n
[curr
].handle
].node
= curr
;
244 if( curr
<= -- pq
->size
) {
245 if( curr
<= 1 || LEQ( h
[n
[curr
>>1].handle
].key
, h
[n
[curr
].handle
].key
)) {
246 FloatDown( pq
, curr
);
252 h
[hCurr
].node
= pq
->freeList
;
253 pq
->freeList
= hCurr
;