1c2c66affSColin Finck /*
2c2c66affSColin Finck * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3c2c66affSColin Finck * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4c2c66affSColin Finck *
5c2c66affSColin Finck * Permission is hereby granted, free of charge, to any person obtaining a
6c2c66affSColin Finck * copy of this software and associated documentation files (the "Software"),
7c2c66affSColin Finck * to deal in the Software without restriction, including without limitation
8c2c66affSColin Finck * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9c2c66affSColin Finck * and/or sell copies of the Software, and to permit persons to whom the
10c2c66affSColin Finck * Software is furnished to do so, subject to the following conditions:
11c2c66affSColin Finck *
12c2c66affSColin Finck * The above copyright notice including the dates of first publication and
13c2c66affSColin Finck * either this permission notice or a reference to
14c2c66affSColin Finck * http://oss.sgi.com/projects/FreeB/
15c2c66affSColin Finck * shall be included in all copies or substantial portions of the Software.
16c2c66affSColin Finck *
17c2c66affSColin Finck * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18c2c66affSColin Finck * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19c2c66affSColin Finck * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20c2c66affSColin Finck * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21c2c66affSColin Finck * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22c2c66affSColin Finck * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23c2c66affSColin Finck * SOFTWARE.
24c2c66affSColin Finck *
25c2c66affSColin Finck * Except as contained in this notice, the name of Silicon Graphics, Inc.
26c2c66affSColin Finck * shall not be used in advertising or otherwise to promote the sale, use or
27c2c66affSColin Finck * other dealings in this Software without prior written authorization from
28c2c66affSColin Finck * Silicon Graphics, Inc.
29c2c66affSColin Finck */
30c2c66affSColin Finck /*
31c2c66affSColin Finck ** Author: Eric Veach, July 1994.
32c2c66affSColin Finck **
33c2c66affSColin Finck */
34c2c66affSColin Finck
35c2c66affSColin Finck #include "gluos.h"
36c2c66affSColin Finck //#include <stddef.h>
37c2c66affSColin Finck #include <assert.h>
38c2c66affSColin Finck //#include <limits.h> /* LONG_MAX */
39c2c66affSColin Finck #include "memalloc.h"
40c2c66affSColin Finck
41c2c66affSColin Finck /* Include all the code for the regular heap-based queue here. */
42c2c66affSColin Finck
43c2c66affSColin Finck #include "priorityq-heap.c"
44c2c66affSColin Finck
45c2c66affSColin Finck /* Now redefine all the function names to map to their "Sort" versions. */
46c2c66affSColin Finck
47c2c66affSColin Finck #include "priorityq-sort.h"
48c2c66affSColin Finck
49c2c66affSColin Finck /* really __gl_pqSortNewPriorityQ */
pqNewPriorityQ(int (* leq)(PQkey key1,PQkey key2))50c2c66affSColin Finck PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) )
51c2c66affSColin Finck {
52c2c66affSColin Finck PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
53c2c66affSColin Finck if (pq == NULL) return NULL;
54c2c66affSColin Finck
55c2c66affSColin Finck pq->heap = __gl_pqHeapNewPriorityQ( leq );
56c2c66affSColin Finck if (pq->heap == NULL) {
57c2c66affSColin Finck memFree(pq);
58c2c66affSColin Finck return NULL;
59c2c66affSColin Finck }
60c2c66affSColin Finck
61c2c66affSColin Finck pq->keys = (PQHeapKey *)memAlloc( INIT_SIZE * sizeof(pq->keys[0]) );
62c2c66affSColin Finck if (pq->keys == NULL) {
63c2c66affSColin Finck __gl_pqHeapDeletePriorityQ(pq->heap);
64c2c66affSColin Finck memFree(pq);
65c2c66affSColin Finck return NULL;
66c2c66affSColin Finck }
67c2c66affSColin Finck
68*695946a5SMasanori Ogino pq->order = NULL;
69c2c66affSColin Finck pq->size = 0;
70c2c66affSColin Finck pq->max = INIT_SIZE;
71c2c66affSColin Finck pq->initialized = FALSE;
72c2c66affSColin Finck pq->leq = leq;
73c2c66affSColin Finck return pq;
74c2c66affSColin Finck }
75c2c66affSColin Finck
76c2c66affSColin Finck /* really __gl_pqSortDeletePriorityQ */
pqDeletePriorityQ(PriorityQ * pq)77c2c66affSColin Finck void pqDeletePriorityQ( PriorityQ *pq )
78c2c66affSColin Finck {
79c2c66affSColin Finck assert(pq != NULL);
80c2c66affSColin Finck if (pq->heap != NULL) __gl_pqHeapDeletePriorityQ( pq->heap );
81c2c66affSColin Finck if (pq->order != NULL) memFree( pq->order );
82c2c66affSColin Finck if (pq->keys != NULL) memFree( pq->keys );
83c2c66affSColin Finck memFree( pq );
84c2c66affSColin Finck }
85c2c66affSColin Finck
86c2c66affSColin Finck
87c2c66affSColin Finck #define LT(x,y) (! LEQ(y,x))
88c2c66affSColin Finck #define GT(x,y) (! LEQ(x,y))
89c2c66affSColin Finck #define Swap(a,b) do{PQkey *tmp = *a; *a = *b; *b = tmp;}while(0)
90c2c66affSColin Finck
91c2c66affSColin Finck /* really __gl_pqSortInit */
pqInit(PriorityQ * pq)92c2c66affSColin Finck int pqInit( PriorityQ *pq )
93c2c66affSColin Finck {
94c2c66affSColin Finck PQkey **p, **r, **i, **j, *piv;
95c2c66affSColin Finck struct { PQkey **p, **r; } Stack[50], *top = Stack;
96c2c66affSColin Finck unsigned long seed = 2016473283;
97c2c66affSColin Finck
98c2c66affSColin Finck /* Create an array of indirect pointers to the keys, so that we
99c2c66affSColin Finck * the handles we have returned are still valid.
100c2c66affSColin Finck */
101c2c66affSColin Finck /*
102c2c66affSColin Finck pq->order = (PQHeapKey **)memAlloc( (size_t)
103c2c66affSColin Finck (pq->size * sizeof(pq->order[0])) );
104c2c66affSColin Finck */
105c2c66affSColin Finck pq->order = (PQHeapKey **)memAlloc( (size_t)
106c2c66affSColin Finck ((pq->size+1) * sizeof(pq->order[0])) );
107c2c66affSColin Finck /* the previous line is a patch to compensate for the fact that IBM */
108c2c66affSColin Finck /* machines return a null on a malloc of zero bytes (unlike SGI), */
109c2c66affSColin Finck /* so we have to put in this defense to guard against a memory */
110c2c66affSColin Finck /* fault four lines down. from fossum@austin.ibm.com. */
111c2c66affSColin Finck if (pq->order == NULL) return 0;
112c2c66affSColin Finck
113c2c66affSColin Finck p = pq->order;
114c2c66affSColin Finck r = p + pq->size - 1;
115c2c66affSColin Finck for( piv = pq->keys, i = p; i <= r; ++piv, ++i ) {
116c2c66affSColin Finck *i = piv;
117c2c66affSColin Finck }
118c2c66affSColin Finck
119c2c66affSColin Finck /* Sort the indirect pointers in descending order,
120c2c66affSColin Finck * using randomized Quicksort
121c2c66affSColin Finck */
122c2c66affSColin Finck top->p = p; top->r = r; ++top;
123c2c66affSColin Finck while( --top >= Stack ) {
124c2c66affSColin Finck p = top->p;
125c2c66affSColin Finck r = top->r;
126c2c66affSColin Finck while( r > p + 10 ) {
127c2c66affSColin Finck seed = seed * 1539415821 + 1;
128c2c66affSColin Finck i = p + seed % (r - p + 1);
129c2c66affSColin Finck piv = *i;
130c2c66affSColin Finck *i = *p;
131c2c66affSColin Finck *p = piv;
132c2c66affSColin Finck i = p - 1;
133c2c66affSColin Finck j = r + 1;
134c2c66affSColin Finck do {
135c2c66affSColin Finck do { ++i; } while( GT( **i, *piv ));
136c2c66affSColin Finck do { --j; } while( LT( **j, *piv ));
137c2c66affSColin Finck Swap( i, j );
138c2c66affSColin Finck } while( i < j );
139c2c66affSColin Finck Swap( i, j ); /* Undo last swap */
140c2c66affSColin Finck if( i - p < r - j ) {
141c2c66affSColin Finck top->p = j+1; top->r = r; ++top;
142c2c66affSColin Finck r = i-1;
143c2c66affSColin Finck } else {
144c2c66affSColin Finck top->p = p; top->r = i-1; ++top;
145c2c66affSColin Finck p = j+1;
146c2c66affSColin Finck }
147c2c66affSColin Finck }
148c2c66affSColin Finck /* Insertion sort small lists */
149c2c66affSColin Finck for( i = p+1; i <= r; ++i ) {
150c2c66affSColin Finck piv = *i;
151c2c66affSColin Finck for( j = i; j > p && LT( **(j-1), *piv ); --j ) {
152c2c66affSColin Finck *j = *(j-1);
153c2c66affSColin Finck }
154c2c66affSColin Finck *j = piv;
155c2c66affSColin Finck }
156c2c66affSColin Finck }
157c2c66affSColin Finck pq->max = pq->size;
158c2c66affSColin Finck pq->initialized = TRUE;
159c2c66affSColin Finck __gl_pqHeapInit( pq->heap ); /* always succeeds */
160c2c66affSColin Finck
161c2c66affSColin Finck #ifndef NDEBUG
162c2c66affSColin Finck p = pq->order;
163c2c66affSColin Finck r = p + pq->size - 1;
164c2c66affSColin Finck for( i = p; i < r; ++i ) {
165c2c66affSColin Finck assert( LEQ( **(i+1), **i ));
166c2c66affSColin Finck }
167c2c66affSColin Finck #endif
168c2c66affSColin Finck
169c2c66affSColin Finck return 1;
170c2c66affSColin Finck }
171c2c66affSColin Finck
172c2c66affSColin Finck /* really __gl_pqSortInsert */
173c2c66affSColin Finck /* returns LONG_MAX iff out of memory */
pqInsert(PriorityQ * pq,PQkey keyNew)174c2c66affSColin Finck PQhandle pqInsert( PriorityQ *pq, PQkey keyNew )
175c2c66affSColin Finck {
176c2c66affSColin Finck long curr;
177c2c66affSColin Finck
178c2c66affSColin Finck if( pq->initialized ) {
179c2c66affSColin Finck return __gl_pqHeapInsert( pq->heap, keyNew );
180c2c66affSColin Finck }
181c2c66affSColin Finck curr = pq->size;
182c2c66affSColin Finck if( ++ pq->size >= pq->max ) {
183c2c66affSColin Finck PQkey *saveKey= pq->keys;
184c2c66affSColin Finck
185c2c66affSColin Finck /* If the heap overflows, double its size. */
186c2c66affSColin Finck pq->max <<= 1;
187c2c66affSColin Finck pq->keys = (PQHeapKey *)memRealloc( pq->keys,
188c2c66affSColin Finck (size_t)
189c2c66affSColin Finck (pq->max * sizeof( pq->keys[0] )));
190c2c66affSColin Finck if (pq->keys == NULL) {
191c2c66affSColin Finck pq->keys = saveKey; /* restore ptr to free upon return */
192c2c66affSColin Finck return LONG_MAX;
193c2c66affSColin Finck }
194c2c66affSColin Finck }
195c2c66affSColin Finck assert(curr != LONG_MAX);
196c2c66affSColin Finck pq->keys[curr] = keyNew;
197c2c66affSColin Finck
198c2c66affSColin Finck /* Negative handles index the sorted array. */
199c2c66affSColin Finck return -(curr+1);
200c2c66affSColin Finck }
201c2c66affSColin Finck
202c2c66affSColin Finck /* really __gl_pqSortExtractMin */
pqExtractMin(PriorityQ * pq)203c2c66affSColin Finck PQkey pqExtractMin( PriorityQ *pq )
204c2c66affSColin Finck {
205c2c66affSColin Finck PQkey sortMin, heapMin;
206c2c66affSColin Finck
207c2c66affSColin Finck if( pq->size == 0 ) {
208c2c66affSColin Finck return __gl_pqHeapExtractMin( pq->heap );
209c2c66affSColin Finck }
210c2c66affSColin Finck sortMin = *(pq->order[pq->size-1]);
211c2c66affSColin Finck if( ! __gl_pqHeapIsEmpty( pq->heap )) {
212c2c66affSColin Finck heapMin = __gl_pqHeapMinimum( pq->heap );
213c2c66affSColin Finck if( LEQ( heapMin, sortMin )) {
214c2c66affSColin Finck return __gl_pqHeapExtractMin( pq->heap );
215c2c66affSColin Finck }
216c2c66affSColin Finck }
217c2c66affSColin Finck do {
218c2c66affSColin Finck -- pq->size;
219c2c66affSColin Finck } while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL );
220c2c66affSColin Finck return sortMin;
221c2c66affSColin Finck }
222c2c66affSColin Finck
223c2c66affSColin Finck /* really __gl_pqSortMinimum */
pqMinimum(PriorityQ * pq)224c2c66affSColin Finck PQkey pqMinimum( PriorityQ *pq )
225c2c66affSColin Finck {
226c2c66affSColin Finck PQkey sortMin, heapMin;
227c2c66affSColin Finck
228c2c66affSColin Finck if( pq->size == 0 ) {
229c2c66affSColin Finck return __gl_pqHeapMinimum( pq->heap );
230c2c66affSColin Finck }
231c2c66affSColin Finck sortMin = *(pq->order[pq->size-1]);
232c2c66affSColin Finck if( ! __gl_pqHeapIsEmpty( pq->heap )) {
233c2c66affSColin Finck heapMin = __gl_pqHeapMinimum( pq->heap );
234c2c66affSColin Finck if( LEQ( heapMin, sortMin )) {
235c2c66affSColin Finck return heapMin;
236c2c66affSColin Finck }
237c2c66affSColin Finck }
238c2c66affSColin Finck return sortMin;
239c2c66affSColin Finck }
240c2c66affSColin Finck
241c2c66affSColin Finck /* really __gl_pqSortIsEmpty */
pqIsEmpty(PriorityQ * pq)242c2c66affSColin Finck int pqIsEmpty( PriorityQ *pq )
243c2c66affSColin Finck {
244c2c66affSColin Finck return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap );
245c2c66affSColin Finck }
246c2c66affSColin Finck
247c2c66affSColin Finck /* really __gl_pqSortDelete */
pqDelete(PriorityQ * pq,PQhandle curr)248c2c66affSColin Finck void pqDelete( PriorityQ *pq, PQhandle curr )
249c2c66affSColin Finck {
250c2c66affSColin Finck if( curr >= 0 ) {
251c2c66affSColin Finck __gl_pqHeapDelete( pq->heap, curr );
252c2c66affSColin Finck return;
253c2c66affSColin Finck }
254c2c66affSColin Finck curr = -(curr+1);
255c2c66affSColin Finck assert( curr < pq->max && pq->keys[curr] != NULL );
256c2c66affSColin Finck
257c2c66affSColin Finck pq->keys[curr] = NULL;
258c2c66affSColin Finck while( pq->size > 0 && *(pq->order[pq->size-1]) == NULL ) {
259c2c66affSColin Finck -- pq->size;
260c2c66affSColin Finck }
261c2c66affSColin Finck }
262