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