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_heap_h_
36f220fa62Smrg#define __priorityq_heap_h_
37f220fa62Smrg
38f220fa62Smrg/* Use #define's so that another heap implementation can use this one */
39f220fa62Smrg
40f220fa62Smrg#define PQkey			PQHeapKey
41f220fa62Smrg#define PQhandle		PQHeapHandle
42f220fa62Smrg#define PriorityQ		PriorityQHeap
43f220fa62Smrg
44f220fa62Smrg#define pqNewPriorityQ(leq)	__gl_pqHeapNewPriorityQ(leq)
45f220fa62Smrg#define pqDeletePriorityQ(pq)	__gl_pqHeapDeletePriorityQ(pq)
46f220fa62Smrg
47f220fa62Smrg/* The basic operations are insertion of a new key (pqInsert),
48f220fa62Smrg * and examination/extraction of a key whose value is minimum
49f220fa62Smrg * (pqMinimum/pqExtractMin).  Deletion is also allowed (pqDelete);
50f220fa62Smrg * for this purpose pqInsert returns a "handle" which is supplied
51f220fa62Smrg * as the argument.
52f220fa62Smrg *
53f220fa62Smrg * An initial heap may be created efficiently by calling pqInsert
54f220fa62Smrg * repeatedly, then calling pqInit.  In any case pqInit must be called
55f220fa62Smrg * before any operations other than pqInsert are used.
56f220fa62Smrg *
57f220fa62Smrg * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key.
58f220fa62Smrg * This may also be tested with pqIsEmpty.
59f220fa62Smrg */
60f220fa62Smrg#define pqInit(pq)		__gl_pqHeapInit(pq)
61f220fa62Smrg#define pqInsert(pq,key)	__gl_pqHeapInsert(pq,key)
62f220fa62Smrg#define pqMinimum(pq)		__gl_pqHeapMinimum(pq)
63f220fa62Smrg#define pqExtractMin(pq)	__gl_pqHeapExtractMin(pq)
64f220fa62Smrg#define pqDelete(pq,handle)	__gl_pqHeapDelete(pq,handle)
65f220fa62Smrg#define pqIsEmpty(pq)		__gl_pqHeapIsEmpty(pq)
66f220fa62Smrg
67f220fa62Smrg
68f220fa62Smrg/* Since we support deletion the data structure is a little more
69f220fa62Smrg * complicated than an ordinary heap.  "nodes" is the heap itself;
70f220fa62Smrg * active nodes are stored in the range 1..pq->size.  When the
71f220fa62Smrg * heap exceeds its allocated size (pq->max), its size doubles.
72f220fa62Smrg * The children of node i are nodes 2i and 2i+1.
73f220fa62Smrg *
74f220fa62Smrg * Each node stores an index into an array "handles".  Each handle
75f220fa62Smrg * stores a key, plus a pointer back to the node which currently
76f220fa62Smrg * represents that key (ie. nodes[handles[i].node].handle == i).
77f220fa62Smrg */
78f220fa62Smrg
79f220fa62Smrgtypedef void *PQkey;
80f220fa62Smrgtypedef long PQhandle;
81f220fa62Smrgtypedef struct PriorityQ PriorityQ;
82f220fa62Smrg
83f220fa62Smrgtypedef struct { PQhandle handle; } PQnode;
84f220fa62Smrgtypedef struct { PQkey key; PQhandle node; } PQhandleElem;
85f220fa62Smrg
86f220fa62Smrgstruct PriorityQ {
87f220fa62Smrg  PQnode	*nodes;
88f220fa62Smrg  PQhandleElem	*handles;
89f220fa62Smrg  long		size, max;
90f220fa62Smrg  PQhandle	freeList;
91f220fa62Smrg  int		initialized;
92f220fa62Smrg  int		(*leq)(PQkey key1, PQkey key2);
93f220fa62Smrg};
94f220fa62Smrg
95f220fa62SmrgPriorityQ	*pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) );
96f220fa62Smrgvoid		pqDeletePriorityQ( PriorityQ *pq );
97f220fa62Smrg
98f220fa62Smrgvoid		pqInit( PriorityQ *pq );
99f220fa62SmrgPQhandle	pqInsert( PriorityQ *pq, PQkey key );
100f220fa62SmrgPQkey		pqExtractMin( PriorityQ *pq );
101f220fa62Smrgvoid		pqDelete( PriorityQ *pq, PQhandle handle );
102f220fa62Smrg
103f220fa62Smrg
104f220fa62Smrg#define __gl_pqHeapMinimum(pq)	((pq)->handles[(pq)->nodes[1].handle].key)
105f220fa62Smrg#define __gl_pqHeapIsEmpty(pq)	((pq)->size == 0)
106f220fa62Smrg
107f220fa62Smrg#endif
108