1f220fa62Smrg/* 2f220fa62Smrg * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008) 3f220fa62Smrg * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved. 4f220fa62Smrg * 5f220fa62Smrg * Permission is hereby granted, free of charge, to any person obtaining a 6f220fa62Smrg * copy of this software and associated documentation files (the "Software"), 7f220fa62Smrg * to deal in the Software without restriction, including without limitation 8f220fa62Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9f220fa62Smrg * and/or sell copies of the Software, and to permit persons to whom the 10f220fa62Smrg * Software is furnished to do so, subject to the following conditions: 11f220fa62Smrg * 12f220fa62Smrg * The above copyright notice including the dates of first publication and 13f220fa62Smrg * either this permission notice or a reference to 14f220fa62Smrg * http://oss.sgi.com/projects/FreeB/ 15f220fa62Smrg * shall be included in all copies or substantial portions of the Software. 16f220fa62Smrg * 17f220fa62Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18f220fa62Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19f220fa62Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20f220fa62Smrg * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21f220fa62Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 22f220fa62Smrg * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23f220fa62Smrg * SOFTWARE. 24f220fa62Smrg * 25f220fa62Smrg * Except as contained in this notice, the name of Silicon Graphics, Inc. 26f220fa62Smrg * shall not be used in advertising or otherwise to promote the sale, use or 27f220fa62Smrg * other dealings in this Software without prior written authorization from 28f220fa62Smrg * Silicon Graphics, Inc. 29f220fa62Smrg */ 30f220fa62Smrg/* 31f220fa62Smrg** Author: Eric Veach, July 1994. 32f220fa62Smrg** 33f220fa62Smrg*/ 34f220fa62Smrg 35f220fa62Smrg#ifndef __priorityq_sort_h_ 36f220fa62Smrg#define __priorityq_sort_h_ 37f220fa62Smrg 38f220fa62Smrg#include "priorityq-heap.h" 39f220fa62Smrg 40f220fa62Smrg#undef PQkey 41f220fa62Smrg#undef PQhandle 42f220fa62Smrg#undef PriorityQ 43f220fa62Smrg#undef pqNewPriorityQ 44f220fa62Smrg#undef pqDeletePriorityQ 45f220fa62Smrg#undef pqInit 46f220fa62Smrg#undef pqInsert 47f220fa62Smrg#undef pqMinimum 48f220fa62Smrg#undef pqExtractMin 49f220fa62Smrg#undef pqDelete 50f220fa62Smrg#undef pqIsEmpty 51f220fa62Smrg 52f220fa62Smrg/* Use #define's so that another heap implementation can use this one */ 53f220fa62Smrg 54f220fa62Smrg#define PQkey PQSortKey 55f220fa62Smrg#define PQhandle PQSortHandle 56f220fa62Smrg#define PriorityQ PriorityQSort 57f220fa62Smrg 58f220fa62Smrg#define pqNewPriorityQ(leq) __gl_pqSortNewPriorityQ(leq) 59f220fa62Smrg#define pqDeletePriorityQ(pq) __gl_pqSortDeletePriorityQ(pq) 60f220fa62Smrg 61f220fa62Smrg/* The basic operations are insertion of a new key (pqInsert), 62f220fa62Smrg * and examination/extraction of a key whose value is minimum 63f220fa62Smrg * (pqMinimum/pqExtractMin). Deletion is also allowed (pqDelete); 64f220fa62Smrg * for this purpose pqInsert returns a "handle" which is supplied 65f220fa62Smrg * as the argument. 66f220fa62Smrg * 67f220fa62Smrg * An initial heap may be created efficiently by calling pqInsert 68f220fa62Smrg * repeatedly, then calling pqInit. In any case pqInit must be called 69f220fa62Smrg * before any operations other than pqInsert are used. 70f220fa62Smrg * 71f220fa62Smrg * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key. 72f220fa62Smrg * This may also be tested with pqIsEmpty. 73f220fa62Smrg */ 74f220fa62Smrg#define pqInit(pq) __gl_pqSortInit(pq) 75f220fa62Smrg#define pqInsert(pq,key) __gl_pqSortInsert(pq,key) 76f220fa62Smrg#define pqMinimum(pq) __gl_pqSortMinimum(pq) 77f220fa62Smrg#define pqExtractMin(pq) __gl_pqSortExtractMin(pq) 78f220fa62Smrg#define pqDelete(pq,handle) __gl_pqSortDelete(pq,handle) 79f220fa62Smrg#define pqIsEmpty(pq) __gl_pqSortIsEmpty(pq) 80f220fa62Smrg 81f220fa62Smrg 82f220fa62Smrg/* Since we support deletion the data structure is a little more 83f220fa62Smrg * complicated than an ordinary heap. "nodes" is the heap itself; 84f220fa62Smrg * active nodes are stored in the range 1..pq->size. When the 85f220fa62Smrg * heap exceeds its allocated size (pq->max), its size doubles. 86f220fa62Smrg * The children of node i are nodes 2i and 2i+1. 87f220fa62Smrg * 88f220fa62Smrg * Each node stores an index into an array "handles". Each handle 89f220fa62Smrg * stores a key, plus a pointer back to the node which currently 90f220fa62Smrg * represents that key (ie. nodes[handles[i].node].handle == i). 91f220fa62Smrg */ 92f220fa62Smrg 93f220fa62Smrgtypedef PQHeapKey PQkey; 94f220fa62Smrgtypedef PQHeapHandle PQhandle; 95f220fa62Smrgtypedef struct PriorityQ PriorityQ; 96f220fa62Smrg 97f220fa62Smrgstruct PriorityQ { 98f220fa62Smrg PriorityQHeap *heap; 99f220fa62Smrg PQkey *keys; 100f220fa62Smrg PQkey **order; 101f220fa62Smrg PQhandle size, max; 102f220fa62Smrg int initialized; 103f220fa62Smrg int (*leq)(PQkey key1, PQkey key2); 104f220fa62Smrg}; 105f220fa62Smrg 106f220fa62SmrgPriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) ); 107f220fa62Smrgvoid pqDeletePriorityQ( PriorityQ *pq ); 108f220fa62Smrg 109f220fa62Smrgint pqInit( PriorityQ *pq ); 110f220fa62SmrgPQhandle pqInsert( PriorityQ *pq, PQkey key ); 111f220fa62SmrgPQkey pqExtractMin( PriorityQ *pq ); 112f220fa62Smrgvoid pqDelete( PriorityQ *pq, PQhandle handle ); 113f220fa62Smrg 114f220fa62SmrgPQkey pqMinimum( PriorityQ *pq ); 115f220fa62Smrgint pqIsEmpty( PriorityQ *pq ); 116f220fa62Smrg 117f220fa62Smrg#endif 118