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.
38 #include "priorityq-heap.h"
46 #ifdef FOR_TRITE_TEST_PROGRAM
47 #define LEQ(x,y) (*pq->leq)(x,y)
49 /* Violates modularity, but a little faster */
51 #define LEQ(x,y) VertLeq((GLUvertex *)x, (GLUvertex *)y)
54 /* really __gl_pqHeapNewPriorityQ */
55 PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
57 PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
58 if (pq == NULL) return NULL;
62 pq->nodes = (PQnode *)memAlloc( (INIT_SIZE + 1) * sizeof(pq->nodes[0]) );
63 if (pq->nodes == NULL) {
68 pq->handles = (PQhandleElem *)memAlloc( (INIT_SIZE + 1) * sizeof(pq->handles[0]) );
69 if (pq->handles == NULL) {
75 pq->initialized = FALSE;
79 pq->nodes[1].handle = 1; /* so that Minimum() returns NULL */
80 pq->handles[1].key = NULL;
84 /* really __gl_pqHeapDeletePriorityQ */
85 void pqDeletePriorityQ( PriorityQ *pq )
87 memFree( pq->handles );
93 static void FloatDown( PriorityQ *pq, long curr )
95 PQnode *n = pq->nodes;
96 PQhandleElem *h = pq->handles;
97 PQhandle hCurr, hChild;
100 hCurr = n[curr].handle;
103 if( child < pq->size && LEQ( h[n[child+1].handle].key,
104 h[n[child].handle].key )) {
108 assert(child <= pq->max);
110 hChild = n[child].handle;
111 if( child > pq->size || LEQ( h[hCurr].key, h[hChild].key )) {
112 n[curr].handle = hCurr;
113 h[hCurr].node = curr;
116 n[curr].handle = hChild;
117 h[hChild].node = curr;
123 static void FloatUp( PriorityQ *pq, long curr )
125 PQnode *n = pq->nodes;
126 PQhandleElem *h = pq->handles;
127 PQhandle hCurr, hParent;
130 hCurr = n[curr].handle;
133 hParent = n[parent].handle;
134 if( parent == 0 || LEQ( h[hParent].key, h[hCurr].key )) {
135 n[curr].handle = hCurr;
136 h[hCurr].node = curr;
139 n[curr].handle = hParent;
140 h[hParent].node = curr;
145 /* really __gl_pqHeapInit */
146 void pqInit( PriorityQ *pq )
150 /* This method of building a heap is O(n), rather than O(n lg n). */
152 for( i = pq->size; i >= 1; --i ) {
155 pq->initialized = TRUE;
158 /* really __gl_pqHeapInsert */
159 /* returns LONG_MAX iff out of memory */
160 PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
166 if( (curr*2) > pq->max ) {
167 PQnode *saveNodes= pq->nodes;
168 PQhandleElem *saveHandles= pq->handles;
170 /* If the heap overflows, double its size. */
172 pq->nodes = (PQnode *)memRealloc( pq->nodes,
174 ((pq->max + 1) * sizeof( pq->nodes[0] )));
175 if (pq->nodes == NULL) {
176 pq->nodes = saveNodes; /* restore ptr to free upon return */
179 pq->handles = (PQhandleElem *)memRealloc( pq->handles,
182 sizeof( pq->handles[0] )));
183 if (pq->handles == NULL) {
184 pq->handles = saveHandles; /* restore ptr to free upon return */
189 if( pq->freeList == 0 ) {
193 pq->freeList = pq->handles[free].node;
196 pq->nodes[curr].handle = free;
197 pq->handles[free].node = curr;
198 pq->handles[free].key = keyNew;
200 if( pq->initialized ) {
203 assert(free != LONG_MAX);
207 /* really __gl_pqHeapExtractMin */
208 PQkey pqExtractMin( PriorityQ *pq )
210 PQnode *n = pq->nodes;
211 PQhandleElem *h = pq->handles;
212 PQhandle hMin = n[1].handle;
213 PQkey min = h[hMin].key;
216 n[1].handle = n[pq->size].handle;
217 h[n[1].handle].node = 1;
220 h[hMin].node = pq->freeList;
223 if( -- pq->size > 0 ) {
230 /* really __gl_pqHeapDelete */
231 void pqDelete( PriorityQ *pq, PQhandle hCurr )
233 PQnode *n = pq->nodes;
234 PQhandleElem *h = pq->handles;
237 assert( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL );
239 curr = h[hCurr].node;
240 n[curr].handle = n[pq->size].handle;
241 h[n[curr].handle].node = curr;
243 if( curr <= -- pq->size ) {
244 if( curr <= 1 || LEQ( h[n[curr>>1].handle].key, h[n[curr].handle].key )) {
245 FloatDown( pq, curr );
251 h[hCurr].node = pq->freeList;
252 pq->freeList = hCurr;