00001 /* 00002 ** SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008) 00003 ** Copyright (C) [dates of first publication] Silicon Graphics, Inc. 00004 ** All Rights Reserved. 00005 ** 00006 ** Permission is hereby granted, free of charge, to any person obtaining a copy 00007 ** of this software and associated documentation files (the "Software"), to deal 00008 ** in the Software without restriction, including without limitation the rights 00009 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 00010 ** of the Software, and to permit persons to whom the Software is furnished to do so, 00011 ** subject to the following conditions: 00012 ** 00013 ** The above copyright notice including the dates of first publication and either this 00014 ** permission notice or a reference to http://oss.sgi.com/projects/FreeB/ shall be 00015 ** included in all copies or substantial portions of the Software. 00016 ** 00017 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00018 ** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 00019 ** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL SILICON GRAPHICS, INC. 00020 ** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 00021 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 00022 ** OR OTHER DEALINGS IN THE SOFTWARE. 00023 ** 00024 ** Except as contained in this notice, the name of Silicon Graphics, Inc. shall not 00025 ** be used in advertising or otherwise to promote the sale, use or other dealings in 00026 ** this Software without prior written authorization from Silicon Graphics, Inc. 00027 */ 00028 /* 00029 ** Author: Eric Veach, July 1994. 00030 */ 00031 00032 #ifndef PRIORITYQ_H 00033 #define PRIORITYQ_H 00034 00035 /* The basic operations are insertion of a new key (pqInsert), 00036 * and examination/extraction of a key whose value is minimum 00037 * (pqMinimum/pqExtractMin). Deletion is also allowed (pqDelete); 00038 * for this purpose pqInsert returns a "handle" which is supplied 00039 * as the argument. 00040 * 00041 * An initial heap may be created efficiently by calling pqInsert 00042 * repeatedly, then calling pqInit. In any case pqInit must be called 00043 * before any operations other than pqInsert are used. 00044 * 00045 * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key. 00046 * This may also be tested with pqIsEmpty. 00047 */ 00048 00049 /* Since we support deletion the data structure is a little more 00050 * complicated than an ordinary heap. "nodes" is the heap itself; 00051 * active nodes are stored in the range 1..pq->size. When the 00052 * heap exceeds its allocated size (pq->max), its size doubles. 00053 * The children of node i are nodes 2i and 2i+1. 00054 * 00055 * Each node stores an index into an array "handles". Each handle 00056 * stores a key, plus a pointer back to the node which currently 00057 * represents that key (ie. nodes[handles[i].node].handle == i). 00058 */ 00059 00060 typedef void *PQkey; 00061 typedef int PQhandle; 00062 typedef struct PriorityQHeap PriorityQHeap; 00063 00064 #define INV_HANDLE 0x0fffffff 00065 00066 typedef struct { PQhandle handle; } PQnode; 00067 typedef struct { PQkey key; PQhandle node; } PQhandleElem; 00068 00069 struct PriorityQHeap { 00070 00071 PQnode *nodes; 00072 PQhandleElem *handles; 00073 int size, max; 00074 PQhandle freeList; 00075 int initialized; 00076 00077 int (*leq)(PQkey key1, PQkey key2); 00078 }; 00079 00080 typedef struct PriorityQ PriorityQ; 00081 00082 struct PriorityQ { 00083 PriorityQHeap *heap; 00084 00085 PQkey *keys; 00086 PQkey **order; 00087 PQhandle size, max; 00088 int initialized; 00089 00090 int (*leq)(PQkey key1, PQkey key2); 00091 }; 00092 00093 PriorityQ *pqNewPriorityQ( TESSalloc* alloc, int size, int (*leq)(PQkey key1, PQkey key2) ); 00094 void pqDeletePriorityQ( TESSalloc* alloc, PriorityQ *pq ); 00095 00096 int pqInit( TESSalloc* alloc, PriorityQ *pq ); 00097 PQhandle pqInsert( TESSalloc* alloc, PriorityQ *pq, PQkey key ); 00098 PQkey pqExtractMin( PriorityQ *pq ); 00099 void pqDelete( PriorityQ *pq, PQhandle handle ); 00100 00101 PQkey pqMinimum( PriorityQ *pq ); 00102 int pqIsEmpty( PriorityQ *pq ); 00103 00104 #endif