Stellarium  0.90.0
priorityq-heap.h
1 /*
2  * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3  * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4  *
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:
11  *
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.
16  *
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
23  * SOFTWARE.
24  *
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.
29  */
30 /*
31 ** Author: Eric Veach, July 1994.
32 **
33 */
34 
35 #ifndef __priorityq_heap_h_
36 #define __priorityq_heap_h_
37 
38 /* Use #define's so that another heap implementation can use this one */
39 
40 #define PQkey PQHeapKey
41 #define PQhandle PQHeapHandle
42 #define PriorityQ PriorityQHeap
43 
44 #define pqNewPriorityQ(leq) __gl_pqHeapNewPriorityQ(leq)
45 #define pqDeletePriorityQ(pq) __gl_pqHeapDeletePriorityQ(pq)
46 
47 /* The basic operations are insertion of a new key (pqInsert),
48  * and examination/extraction of a key whose value is minimum
49  * (pqMinimum/pqExtractMin). Deletion is also allowed (pqDelete);
50  * for this purpose pqInsert returns a "handle" which is supplied
51  * as the argument.
52  *
53  * An initial heap may be created efficiently by calling pqInsert
54  * repeatedly, then calling pqInit. In any case pqInit must be called
55  * before any operations other than pqInsert are used.
56  *
57  * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key.
58  * This may also be tested with pqIsEmpty.
59  */
60 #define pqInit(pq) __gl_pqHeapInit(pq)
61 #define pqInsert(pq,key) __gl_pqHeapInsert(pq,key)
62 #define pqMinimum(pq) __gl_pqHeapMinimum(pq)
63 #define pqExtractMin(pq) __gl_pqHeapExtractMin(pq)
64 #define pqDelete(pq,handle) __gl_pqHeapDelete(pq,handle)
65 #define pqIsEmpty(pq) __gl_pqHeapIsEmpty(pq)
66 
67 
68 /* Since we support deletion the data structure is a little more
69  * complicated than an ordinary heap. "nodes" is the heap itself;
70  * active nodes are stored in the range 1..pq->size. When the
71  * heap exceeds its allocated size (pq->max), its size doubles.
72  * The children of node i are nodes 2i and 2i+1.
73  *
74  * Each node stores an index into an array "handles". Each handle
75  * stores a key, plus a pointer back to the node which currently
76  * represents that key (ie. nodes[handles[i].node].handle == i).
77  */
78 
79 typedef void* PQkey;
80 typedef long PQhandle;
81 typedef struct PriorityQ PriorityQ;
82 
83 typedef struct
84 {
85  PQhandle handle;
86 } PQnode;
87 
88 typedef struct
89 {
90  PQkey key;
91  PQhandle node;
92 } PQhandleElem;
93 
94 struct PriorityQ
95 {
96  PQnode* nodes;
97  PQhandleElem* handles;
98  long size;
99  long max;
100  PQhandle freeList;
101  int initialized;
102  int (*leq)(PQkey key1, PQkey key2);
103 };
104 
105 PriorityQ* pqNewPriorityQ(int (*leq)(PQkey key1, PQkey key2));
106 void pqDeletePriorityQ(PriorityQ* pq);
107 
108 void pqInit(PriorityQ* pq);
109 PQhandle pqInsert(PriorityQ* pq, PQkey key);
110 PQkey pqExtractMin(PriorityQ* pq);
111 void pqDelete(PriorityQ* pq, PQhandle handle);
112 
113 #define __gl_pqHeapMinimum(pq) ((pq)->handles[(pq)->nodes[1].handle].key)
114 #define __gl_pqHeapIsEmpty(pq) ((pq)->size==0)
115 
116 #endif /* __priorityq_heap_h_ */