123c9d899e902792ae94221ee1ba87c7a8db0522
[mesa.git] / src / glu / sgi / libtess / priorityq-sort.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 ** $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
34 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libtess/priorityq-sort.h,v 1.1 2001/03/17 00:25:41 brianp Exp $
35 */
36
37 #ifndef __priorityq_sort_h_
38 #define __priorityq_sort_h_
39
40 #include "priorityq-heap.h"
41
42 #undef PQkey
43 #undef PQhandle
44 #undef PriorityQ
45 #undef pqNewPriorityQ
46 #undef pqDeletePriorityQ
47 #undef pqInit
48 #undef pqInsert
49 #undef pqMinimum
50 #undef pqExtractMin
51 #undef pqDelete
52 #undef pqIsEmpty
53
54 /* Use #define's so that another heap implementation can use this one */
55
56 #define PQkey PQSortKey
57 #define PQhandle PQSortHandle
58 #define PriorityQ PriorityQSort
59
60 #define pqNewPriorityQ(leq) __gl_pqSortNewPriorityQ(leq)
61 #define pqDeletePriorityQ(pq) __gl_pqSortDeletePriorityQ(pq)
62
63 /* The basic operations are insertion of a new key (pqInsert),
64 * and examination/extraction of a key whose value is minimum
65 * (pqMinimum/pqExtractMin). Deletion is also allowed (pqDelete);
66 * for this purpose pqInsert returns a "handle" which is supplied
67 * as the argument.
68 *
69 * An initial heap may be created efficiently by calling pqInsert
70 * repeatedly, then calling pqInit. In any case pqInit must be called
71 * before any operations other than pqInsert are used.
72 *
73 * If the heap is empty, pqMinimum/pqExtractMin will return a NULL key.
74 * This may also be tested with pqIsEmpty.
75 */
76 #define pqInit(pq) __gl_pqSortInit(pq)
77 #define pqInsert(pq,key) __gl_pqSortInsert(pq,key)
78 #define pqMinimum(pq) __gl_pqSortMinimum(pq)
79 #define pqExtractMin(pq) __gl_pqSortExtractMin(pq)
80 #define pqDelete(pq,handle) __gl_pqSortDelete(pq,handle)
81 #define pqIsEmpty(pq) __gl_pqSortIsEmpty(pq)
82
83
84 /* Since we support deletion the data structure is a little more
85 * complicated than an ordinary heap. "nodes" is the heap itself;
86 * active nodes are stored in the range 1..pq->size. When the
87 * heap exceeds its allocated size (pq->max), its size doubles.
88 * The children of node i are nodes 2i and 2i+1.
89 *
90 * Each node stores an index into an array "handles". Each handle
91 * stores a key, plus a pointer back to the node which currently
92 * represents that key (ie. nodes[handles[i].node].handle == i).
93 */
94
95 typedef PQHeapKey PQkey;
96 typedef PQHeapHandle PQhandle;
97 typedef struct PriorityQ PriorityQ;
98
99 struct PriorityQ {
100 PriorityQHeap *heap;
101 PQkey *keys;
102 PQkey **order;
103 PQhandle size, max;
104 int initialized;
105 int (*leq)(PQkey key1, PQkey key2);
106 };
107
108 PriorityQ *pqNewPriorityQ( int (*leq)(PQkey key1, PQkey key2) );
109 void pqDeletePriorityQ( PriorityQ *pq );
110
111 int pqInit( PriorityQ *pq );
112 PQhandle pqInsert( PriorityQ *pq, PQkey key );
113 PQkey pqExtractMin( PriorityQ *pq );
114 void pqDelete( PriorityQ *pq, PQhandle handle );
115
116 PQkey pqMinimum( PriorityQ *pq );
117 int pqIsEmpty( PriorityQ *pq );
118
119 #endif