g4tools  5.4.0
Classes | Macros | Typedefs | Functions
priorityq File Reference
#include <climits>
#include "memalloc"
#include "geom"
Include dependency graph for priorityq:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PQnode
 
struct  PQhandleElem
 
struct  PriorityQ
 
struct  PriorityQ
 

Macros

#define tools_glutess_priorityq
 
#define PQkey   PQHeapKey
 
#define PQhandle   PQHeapHandle
 
#define PriorityQ   PriorityQHeap
 
#define pqNewPriorityQ(leq)   __gl_pqHeapNewPriorityQ(leq)
 
#define pqDeletePriorityQ(pq)   __gl_pqHeapDeletePriorityQ(pq)
 
#define pqInit(pq)   __gl_pqHeapInit(pq)
 
#define pqInsert(pq, key)   __gl_pqHeapInsert(pq,key)
 
#define pqMinimum(pq)   __gl_pqHeapMinimum(pq)
 
#define pqExtractMin(pq)   __gl_pqHeapExtractMin(pq)
 
#define pqDelete(pq, handle)   __gl_pqHeapDelete(pq,handle)
 
#define pqIsEmpty(pq)   __gl_pqHeapIsEmpty(pq)
 
#define __gl_pqHeapMinimum(pq)   ((pq)->handles[(pq)->nodes[1].handle].key)
 
#define __gl_pqHeapIsEmpty(pq)   ((pq)->size == 0)
 
#define LEQ(x, y)   VertLeq((GLUvertex *)x, (GLUvertex *)y)
 
#define PQkey   PQSortKey
 
#define PQhandle   PQSortHandle
 
#define PriorityQ   PriorityQSort
 
#define pqNewPriorityQ(leq)   __gl_pqSortNewPriorityQ(leq)
 
#define pqDeletePriorityQ(pq)   __gl_pqSortDeletePriorityQ(pq)
 
#define pqInit(pq)   __gl_pqSortInit(pq)
 
#define pqInsert(pq, key)   __gl_pqSortInsert(pq,key)
 
#define pqMinimum(pq)   __gl_pqSortMinimum(pq)
 
#define pqExtractMin(pq)   __gl_pqSortExtractMin(pq)
 
#define pqDelete(pq, handle)   __gl_pqSortDelete(pq,handle)
 
#define pqIsEmpty(pq)   __gl_pqSortIsEmpty(pq)
 
#define LT(x, y)   (! LEQ(y,x))
 
#define GT(x, y)   (! LEQ(x,y))
 
#define pq_Swap(a, b)   do{PQkey *tmp = *a; *a = *b; *b = tmp;} while(false)
 

Typedefs

typedef void * PQkey
 
typedef long PQhandle
 
typedef struct PriorityQ PriorityQ
 

Functions

long INIT_SIZE ()
 
PriorityQpqNewPriorityQ (int(*leq)(PQkey key1, PQkey key2))
 
void pqDeletePriorityQ (PriorityQ *pq)
 
inline void static_FloatDown (PriorityQ *pq, long curr)
 
inline void static_FloatUp (PriorityQ *pq, long curr)
 
void pqInit (PriorityQ *pq)
 
PQhandle pqInsert (PriorityQ *pq, PQkey keyNew)
 
PQkey pqExtractMin (PriorityQ *pq)
 
void pqDelete (PriorityQ *pq, PQhandle hCurr)
 
PQkey pqMinimum (PriorityQ *pq)
 
int pqIsEmpty (PriorityQ *pq)
 

Macro Definition Documentation

◆ __gl_pqHeapIsEmpty

#define __gl_pqHeapIsEmpty (   pq)    ((pq)->size == 0)

Definition at line 72 of file priorityq.

◆ __gl_pqHeapMinimum

#define __gl_pqHeapMinimum (   pq)    ((pq)->handles[(pq)->nodes[1].handle].key)

Definition at line 71 of file priorityq.

◆ GT

#define GT (   x,
 
)    (! LEQ(x,y))

Definition at line 396 of file priorityq.

◆ LEQ

#define LEQ (   x,
 
)    VertLeq((GLUvertex *)x, (GLUvertex *)y)

Definition at line 84 of file priorityq.

◆ LT

#define LT (   x,
 
)    (! LEQ(y,x))

Definition at line 395 of file priorityq.

◆ pq_Swap

#define pq_Swap (   a,
 
)    do{PQkey *tmp = *a; *a = *b; *b = tmp;} while(false)

Definition at line 398 of file priorityq.

◆ pqDelete [1/2]

#define pqDelete (   pq,
  handle 
)    __gl_pqHeapDelete(pq,handle)

Definition at line 330 of file priorityq.

◆ pqDelete [2/2]

#define pqDelete (   pq,
  handle 
)    __gl_pqSortDelete(pq,handle)

Definition at line 330 of file priorityq.

◆ pqDeletePriorityQ [1/2]

#define pqDeletePriorityQ (   pq)    __gl_pqHeapDeletePriorityQ(pq)

Definition at line 311 of file priorityq.

◆ pqDeletePriorityQ [2/2]

#define pqDeletePriorityQ (   pq)    __gl_pqSortDeletePriorityQ(pq)

Definition at line 311 of file priorityq.

◆ pqExtractMin [1/2]

#define pqExtractMin (   pq)    __gl_pqHeapExtractMin(pq)

Definition at line 329 of file priorityq.

◆ pqExtractMin [2/2]

#define pqExtractMin (   pq)    __gl_pqSortExtractMin(pq)

Definition at line 329 of file priorityq.

◆ PQhandle [1/2]

typedef PQHeapHandle PQhandle   PQHeapHandle

Definition at line 307 of file priorityq.

◆ PQhandle [2/2]

#define PQhandle   PQSortHandle

Definition at line 307 of file priorityq.

◆ pqInit [1/2]

#define pqInit (   pq)    __gl_pqHeapInit(pq)

Definition at line 326 of file priorityq.

◆ pqInit [2/2]

#define pqInit (   pq)    __gl_pqSortInit(pq)

Definition at line 326 of file priorityq.

◆ pqInsert [1/2]

#define pqInsert (   pq,
  key 
)    __gl_pqHeapInsert(pq,key)

Definition at line 327 of file priorityq.

◆ pqInsert [2/2]

#define pqInsert (   pq,
  key 
)    __gl_pqSortInsert(pq,key)

Definition at line 327 of file priorityq.

◆ pqIsEmpty [1/2]

#define pqIsEmpty (   pq)    __gl_pqHeapIsEmpty(pq)

Definition at line 331 of file priorityq.

◆ pqIsEmpty [2/2]

#define pqIsEmpty (   pq)    __gl_pqSortIsEmpty(pq)

Definition at line 331 of file priorityq.

◆ PQkey [1/2]

typedef PQHeapKey PQkey   PQHeapKey

Definition at line 306 of file priorityq.

◆ PQkey [2/2]

#define PQkey   PQSortKey

Definition at line 306 of file priorityq.

◆ pqMinimum [1/2]

#define pqMinimum (   pq)    __gl_pqHeapMinimum(pq)

Definition at line 328 of file priorityq.

◆ pqMinimum [2/2]

#define pqMinimum (   pq)    __gl_pqSortMinimum(pq)

Definition at line 328 of file priorityq.

◆ pqNewPriorityQ [1/2]

#define pqNewPriorityQ (   leq)    __gl_pqHeapNewPriorityQ(leq)

Definition at line 310 of file priorityq.

◆ pqNewPriorityQ [2/2]

#define pqNewPriorityQ (   leq)    __gl_pqSortNewPriorityQ(leq)

Definition at line 310 of file priorityq.

◆ PriorityQ [1/2]

typedef struct PriorityQ PriorityQ   PriorityQHeap

Definition at line 308 of file priorityq.

◆ PriorityQ [2/2]

#define PriorityQ   PriorityQSort

Definition at line 308 of file priorityq.

◆ tools_glutess_priorityq

#define tools_glutess_priorityq

Definition at line 4 of file priorityq.

Typedef Documentation

◆ PQhandle

typedef PQHeapHandle PQhandle

Definition at line 56 of file priorityq.

◆ PQkey

typedef PQHeapKey PQkey

Definition at line 55 of file priorityq.

◆ PriorityQ

typedef struct PriorityQ PriorityQ

Definition at line 56 of file priorityq.

Function Documentation

◆ INIT_SIZE()

long INIT_SIZE ( )
inline

Definition at line 77 of file priorityq.

77  {
78  static const long s_value = 32;
79  return s_value;
80 }

◆ pqDelete()

void pqDelete ( PriorityQ pq,
PQhandle  hCurr 
)
inline

Definition at line 263 of file priorityq.

264 {
265  PQnode *n = pq->nodes;
266  PQhandleElem *h = pq->handles;
267  long curr;
268 
269  assert( hCurr >= 1 && hCurr <= pq->max && h[hCurr].key != NULL );
270 
271  curr = h[hCurr].node;
272  n[curr].handle = n[pq->size].handle;
273  h[n[curr].handle].node = curr;
274 
275  if( curr <= -- pq->size ) {
276  if( curr <= 1 || LEQ( h[n[curr>>1].handle].key, h[n[curr].handle].key )) {
277  static_FloatDown( pq, curr );
278  } else {
279  static_FloatUp( pq, curr );
280  }
281  }
282  h[hCurr].key = NULL;
283  h[hCurr].node = pq->freeList;
284  pq->freeList = hCurr;
285 }

◆ pqDeletePriorityQ()

void pqDeletePriorityQ ( PriorityQ pq)
inline

Definition at line 117 of file priorityq.

118 {
119  memFree( pq->handles );
120  memFree( pq->nodes );
121  memFree( pq );
122 }

◆ pqExtractMin()

PQkey pqExtractMin ( PriorityQ pq)
inline

Definition at line 240 of file priorityq.

241 {
242  PQnode *n = pq->nodes;
243  PQhandleElem *h = pq->handles;
244  PQhandle hMin = n[1].handle;
245  PQkey min = h[hMin].key;
246 
247  if( pq->size > 0 ) {
248  n[1].handle = n[pq->size].handle;
249  h[n[1].handle].node = 1;
250 
251  h[hMin].key = NULL;
252  h[hMin].node = pq->freeList;
253  pq->freeList = hMin;
254 
255  if( -- pq->size > 0 ) {
256  static_FloatDown( pq, 1 );
257  }
258  }
259  return min;
260 }

◆ pqInit()

int pqInit ( PriorityQ pq)
inline

Definition at line 178 of file priorityq.

179 {
180  long i;
181 
182  /* This method of building a heap is O(n), rather than O(n lg n). */
183 
184  for( i = pq->size; i >= 1; --i ) {
185  static_FloatDown( pq, i );
186  }
188 }

◆ pqInsert()

PQhandle pqInsert ( PriorityQ pq,
PQkey  keyNew 
)
inline

Definition at line 192 of file priorityq.

193 {
194  long curr;
195  PQhandle free;
196 
197  curr = ++ pq->size;
198  if( (curr*2) > pq->max ) {
199  PQnode *saveNodes= pq->nodes;
200  PQhandleElem *saveHandles= pq->handles;
201 
202  /* If the heap overflows, double its size. */
203  pq->max <<= 1;
204  pq->nodes = (PQnode *)memRealloc( pq->nodes,
205  (size_t)
206  ((pq->max + 1) * sizeof( pq->nodes[0] )));
207  if (pq->nodes == NULL) {
208  pq->nodes = saveNodes; /* restore ptr to free upon return */
209  return LONG_MAX;
210  }
211  pq->handles = (PQhandleElem *)memRealloc( pq->handles,
212  (size_t)
213  ((pq->max + 1) *
214  sizeof( pq->handles[0] )));
215  if (pq->handles == NULL) {
216  pq->handles = saveHandles; /* restore ptr to free upon return */
217  return LONG_MAX;
218  }
219  }
220 
221  if( pq->freeList == 0 ) {
222  free = curr;
223  } else {
224  free = pq->freeList;
225  pq->freeList = pq->handles[free].node;
226  }
227 
228  pq->nodes[curr].handle = free;
229  pq->handles[free].node = curr;
230  pq->handles[free].key = keyNew;
231 
232  if( pq->initialized ) {
233  static_FloatUp( pq, curr );
234  }
235  assert(free != LONG_MAX);
236  return free;
237 }

◆ pqIsEmpty()

int pqIsEmpty ( PriorityQ pq)
inline

Definition at line 551 of file priorityq.

552 {
553  return (pq->size == 0) && __gl_pqHeapIsEmpty( pq->heap );
554 }

◆ pqMinimum()

PQkey pqMinimum ( PriorityQ pq)
inline

Definition at line 533 of file priorityq.

534 {
535  PQkey sortMin, heapMin;
536 
537  if( pq->size == 0 ) {
538  return __gl_pqHeapMinimum( pq->heap );
539  }
540  sortMin = *(pq->order[pq->size-1]);
541  if( ! __gl_pqHeapIsEmpty( pq->heap )) {
542  heapMin = __gl_pqHeapMinimum( pq->heap );
543  if( LEQ( heapMin, sortMin )) {
544  return heapMin;
545  }
546  }
547  return sortMin;
548 }

◆ pqNewPriorityQ()

PriorityQ * pqNewPriorityQ ( int(*)(PQkey key1, PQkey key2)  leq)
inline

Definition at line 87 of file priorityq.

88 {
89  PriorityQ *pq = (PriorityQ *)memAlloc( sizeof( PriorityQ ));
90  if (pq == NULL) return NULL;
91 
92  pq->size = 0;
93  pq->max = INIT_SIZE();
94  pq->nodes = (PQnode *)memAlloc( (INIT_SIZE() + 1) * sizeof(pq->nodes[0]) );
95  if (pq->nodes == NULL) {
96  memFree(pq);
97  return NULL;
98  }
99 
100  pq->handles = (PQhandleElem *)memAlloc( (INIT_SIZE() + 1) * sizeof(pq->handles[0]) );
101  if (pq->handles == NULL) {
102  memFree(pq->nodes);
103  memFree(pq);
104  return NULL;
105  }
106 
108  pq->freeList = 0;
109  pq->leq = leq;
110 
111  pq->nodes[1].handle = 1; /* so that Minimum() returns NULL */
112  pq->handles[1].key = NULL;
113  return pq;
114 }

◆ static_FloatDown()

inline void static_FloatDown ( PriorityQ pq,
long  curr 
)

Definition at line 125 of file priorityq.

126 {
127  PQnode *n = pq->nodes;
128  PQhandleElem *h = pq->handles;
129  PQhandle hCurr, hChild;
130  long child;
131 
132  hCurr = n[curr].handle;
133  for( ;; ) {
134  child = curr << 1;
135  if( child < pq->size && LEQ( h[n[child+1].handle].key,
136  h[n[child].handle].key )) {
137  ++child;
138  }
139 
140  assert(child <= pq->max);
141 
142  hChild = n[child].handle;
143  if( child > pq->size || LEQ( h[hCurr].key, h[hChild].key )) {
144  n[curr].handle = hCurr;
145  h[hCurr].node = curr;
146  break;
147  }
148  n[curr].handle = hChild;
149  h[hChild].node = curr;
150  curr = child;
151  }
152 }

◆ static_FloatUp()

inline void static_FloatUp ( PriorityQ pq,
long  curr 
)

Definition at line 155 of file priorityq.

156 {
157  PQnode *n = pq->nodes;
158  PQhandleElem *h = pq->handles;
159  PQhandle hCurr, hParent;
160  long parent;
161 
162  hCurr = n[curr].handle;
163  for( ;; ) {
164  parent = curr >> 1;
165  hParent = n[parent].handle;
166  if( parent == 0 || LEQ( h[hParent].key, h[hCurr].key )) {
167  n[curr].handle = hCurr;
168  h[hCurr].node = curr;
169  break;
170  }
171  n[curr].handle = hParent;
172  h[hParent].node = curr;
173  curr = parent;
174  }
175 }
__gl_pqHeapIsEmpty
#define __gl_pqHeapIsEmpty(pq)
Definition: priorityq:72
memFree
#define memFree
Definition: memalloc:42
LEQ
#define LEQ(x, y)
Definition: priorityq:84
PQnode::handle
PQhandle handle
Definition: priorityq:59
PriorityQ::size
long size
Definition: priorityq:65
PriorityQ::nodes
PQnode * nodes
Definition: priorityq:63
PQhandle
long PQhandle
Definition: priorityq:56
PQhandleElem::node
PQhandle node
Definition: priorityq:60
PriorityQ::max
long max
Definition: priorityq:65
PriorityQ::handles
PQhandleElem * handles
Definition: priorityq:64
memAlloc
#define memAlloc
Definition: memalloc:40
memRealloc
#define memRealloc
Definition: memalloc:41
PQkey
void * PQkey
Definition: priorityq:55
__gl_pqHeapMinimum
#define __gl_pqHeapMinimum(pq)
Definition: priorityq:71
PQhandleElem::key
PQkey key
Definition: priorityq:60
TOOLS_GLU_TRUE
#define TOOLS_GLU_TRUE
Definition: _glu:82
static_FloatUp
inline void static_FloatUp(PriorityQ *pq, long curr)
Definition: priorityq:155
PQnode
Definition: priorityq:59
INIT_SIZE
long INIT_SIZE()
Definition: priorityq:77
TOOLS_GLU_FALSE
#define TOOLS_GLU_FALSE
Definition: _glu:81
static_FloatDown
inline void static_FloatDown(PriorityQ *pq, long curr)
Definition: priorityq:125
tools::file::size
bool size(const std::string &a_file, long &a_size)
Definition: fsize:13
PriorityQ::freeList
PQhandle freeList
Definition: priorityq:66
PriorityQ::leq
int(* leq)(PQkey key1, PQkey key2)
Definition: priorityq:68
PriorityQ::initialized
int initialized
Definition: priorityq:67
PQhandleElem
Definition: priorityq:60
PriorityQ::heap
PriorityQHeap * heap
Definition: priorityq:350
PriorityQ
Definition: priorityq:62
PriorityQ::order
PQkey ** order
Definition: priorityq:352