42#define PQHEAP_MEM_DEBUG if (0)
66static inline unsigned int heap_lchild(
unsigned int index)
71static inline unsigned int heap_rchild(
unsigned int index)
77static inline unsigned int heap_parent(
unsigned int index)
83static 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] <<
"]";
201 elements =
new T[size];
202 cout <<
"pqheap_t1: register memory\n";
208 cerr <<
"could not allocate priority queue: insufficient memory..\n";
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++) {
281 for (i = 0; i < size; i++) {
299 return cur_elts == max_elts;
306 return cur_elts == 0;
336 cerr <<
"unguarded min failed" << endl;
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)) {
421 !(
next_elt.getPriority() == elt.getPriority())) {
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)];
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())) {
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)
bool insert(const T &elt)
friend ostream & operator<<(ostream &s, const pqheap_t1< T > &pq)
#define assert(condition)