42 #define PQHEAP_MEM_DEBUG if (0)
50 static const int PAGESIZE = 1024;
66 static inline unsigned int heap_lchild(
unsigned int index)
71 static inline unsigned int heap_rchild(
unsigned int index)
77 static inline unsigned int heap_parent(
unsigned int index)
83 static inline unsigned int mymin(
unsigned int a,
unsigned int b)
85 return (a <=
b) ? a :
b;
108 unsigned int cur_elts;
111 unsigned int max_elts;
114 void heapify(
unsigned int root);
129 unsigned int fill(T *a,
unsigned int size);
132 inline bool full(
void);
135 inline bool empty(
void);
142 inline unsigned int size(
void)
const {
return cur_elts; };
145 inline bool min(T &elt);
158 inline bool insert(
const T &elt);
168 void set(
long i, T &elt);
175 for (
unsigned int i = 0; i < mymin(10, pq.cur_elts); i++) {
179 << pq.elements[i] <<
"]";
190 inline void heapstatus(
int d);
191 inline void heaptouch(
unsigned int pos);
192 unsigned int *numtouch;
201 elements =
new T[size];
202 cout <<
"pqheap_t1: register memory\n";
208 cerr <<
"could not allocate priority queue: insufficient memory..\n";
217 numtouch =
new unsigned int[size / PAGESIZE];
219 for (
int i = 0; i < size / PAGESIZE; i++) {
235 cerr <<
"Using slow build in pqheap_t1" << endl;
245 for (
int i = heap_parent(max_elts - 1); i >= 0; i--) {
257 cout << endl <<
"pagesize = " << PAGESIZE << endl;
258 cout <<
"max_elts = " << max_elts << endl;
259 unsigned int n = max_elts / PAGESIZE;
260 for (
unsigned int i = 0; i < n; i++) {
261 cout << form(
"PQTEMP %d\t%d", i, numtouch[i]) << endl;
281 for (i = 0; i < size; i++) {
299 return cur_elts == max_elts;
306 return cur_elts == 0;
336 cerr <<
"unguarded min failed" << endl;
361 numtouch[pos / PAGESIZE]++;
362 assert(numtouch[pos / PAGESIZE] > 0);
370 static int count = 0;
371 static int delta = 0;
376 if ((
count % 10000) == 0) {
377 cout << endl << form(
"PQHEAP %d\t%d", cur_elts, delta) << endl;
392 elements[0] = elements[--cur_elts];
414 if (!extract_min(elt)) {
420 if ((!
min(next_elt)) ||
421 !(next_elt.getPriority() == elt.getPriority())) {
425 extract_min(next_elt);
426 elt = elt + next_elt;
438 return extract_min(dummy);
451 for (ii = cur_elts++;
452 ii && (elements[heap_parent(ii)].getPriority() > elt.getPriority());
453 ii = heap_parent(ii)) {
454 elements[ii] = elements[heap_parent(ii)];
470 unsigned int min_index = root;
471 unsigned int lc = heap_lchild(root);
472 unsigned int rc = heap_rchild(root);
483 if ((lc < cur_elts) &&
484 ((elements[lc].getPriority()) < elements[min_index].getPriority())) {
487 if ((rc < cur_elts) &&
488 ((elements[rc].getPriority()) < elements[min_index].getPriority())) {
492 if (min_index != root) {
493 T tmp_q = elements[min_index];
495 elements[min_index] = elements[root];
496 elements[root] = tmp_q;
519 for (
unsigned int i = 0; i < cur_elts; i++) {
520 cout << elements[i].getPriority().field1() <<
",";
534 cout << a.getPriority().field1() <<
".." <<
b.getPriority().field1();
536 cout <<
" (" << cur_elts <<
")]";
pqheap_t1(unsigned int size)
bool extract_all_min(T &elt)
void delete_min_and_insert(const T &x)
unsigned int fill(T *a, unsigned int size)
unsigned int size(void) const
unsigned int num_elts(void)
friend ostream & operator<<(ostream &s, const pqheap_t1< T > &pq)
bool insert(const T &elt)
#define assert(condition)