75 #define MY_LOG_DEBUG_ID(x)
96 snprintf(str,
sizeof(str),
"BasicMinMaxHeap: allocate %ld\n",
97 (
long)((
size + 1) *
sizeof(T)));
128 void insert(
const T &elt);
130 bool min(T &elt)
const;
132 bool max(T &elt)
const;
150 for (i = 1; i <= pq.
size(); i++) {
151 s <<
" " << pq.
get(i);
161 long log2(
long n)
const;
162 int isOnMaxLevel(
HeapIndex i)
const {
return (log2(i) % 2); };
163 int isOnMinLevel(
HeapIndex i)
const {
return !isOnMaxLevel(i); };
167 int hasRightChild(
HeapIndex i)
const {
return (rightChild(i) <=
size()); };
170 return ((*c = rightChild(i)) <=
size());
176 return (2 * i) <=
size();
255 if (hasRightChild(i) && (leftChildValue(i) > rightChildValue(i))) {
256 return rightChild(i);
269 if (hasRightChild(i) && (leftChildValue(i) < rightChildValue(i))) {
270 return rightChild(i);
289 if (hasChildren(p)) {
290 q = smallestChild(p);
297 if (hasRightChild(i, &p)) {
299 if (hasChildren(p)) {
300 q = smallestChild(p);
305 if (A[p] < A[minpos])
322 if (hasChildren(p)) {
330 if (hasRightChild(i, &p)) {
332 if (hasChildren(p)) {
338 if (A[p] > A[maxpos])
363 if (!hasChildren(i)) {
367 m = smallestChildGrandchild(i);
368 if (isGrandchildOf(i, m)) {
371 if (A[m] > A[parent(m)]) {
400 if (!hasChildren(i)) {
405 m = largestChildGrandchild(i);
406 if (isGrandchildOf(i, m)) {
409 if (A[m] < A[parent(m)]) {
433 if (isOnMinLevel(i)) {
448 if (isOnMinLevel(i)) {
449 if (m && (A[i] > A[m])) {
458 if (m && (A[i] < A[m])) {
475 while (m && (A[i] < A[m])) {
490 while (m && (A[i] > A[m])) {
504 ostrstream *ostr =
new ostrstream();
507 for (i = 1; i <= size(); i++) {
508 *ostr <<
" " << A[i];
509 if (ostr->pcount() > 70) {
510 cout << ostr->str() << endl;
512 ostr =
new ostrstream();
513 *ostr <<
"[" << i <<
"]";
516 cout << ostr->str() << endl;
525 for (
unsigned int i = 1; i <= size(); i++) {
526 cout << A[i].getPriority() <<
",";
540 cout << a.getPriority() <<
".." <<
b.getPriority();
542 cout <<
" (" << size() <<
")]";
552 A = allocateHeap(maxsize);
556 if (lastindex == maxsize)
559 XXX cerr <<
"insert: " << elt << endl;
593 if (!extract_min(elt)) {
599 if ((!
min(next_elt)) ||
600 !(next_elt.getPriority() == elt.getPriority())) {
604 extract_min(next_elt);
605 elt = elt + next_elt;
623 if (hasChildren(1)) {
662 if (hasChildren(1)) {
699 p = (T *)LargeMemory::alloc(
sizeof(T) * (n + 1));
738 XXX cerr << i <<
": " << val << endl;
739 if (val.getPriority() < prev.getPriority()) {
741 cerr <<
"n=" << n << endl;
742 cerr <<
"val=" << val << endl;
743 cerr <<
"prev=" << prev << endl;
744 cerr <<
"looks like minmaxheap.min is broken!!" << endl;
749 ok = extract_min(val);
766 dup = allocateHeap(maxsize);
792 fprintf(stderr,
"MinMaxHeap::grow: not implemented\n");
807 assert(this->size() == 0);
808 for (i = 0; !full() && i < n; i++) {
809 this->insert(arr[i]);
812 assert(i == this->maxsize);
820 #define MMHEAP_INITIAL_SIZE 1024
839 assert(this->maxsize > 0);
843 this->A = this->allocateHeap(this->maxsize);
845 assert(this->maxsize > n);
static T * allocateHeap(HeapIndex n)
bool extract_all_min(T &elt)
BasicMinMaxHeap(HeapIndex size)
virtual ~BasicMinMaxHeap(void)
static void freeHeap(T *)
friend ostream & operator<<(ostream &s, const BasicMinMaxHeap< T > &pq)
void insert(const T &elt)
HeapIndex fill(T *arr, HeapIndex n)
MinMaxHeap(HeapIndex size)
HeapIndex get_maxsize() const
virtual ~UnboundedMinMaxHeap()
UnboundedMinMaxHeap(HeapIndex size)
#define assert(condition)
#define MY_LOG_DEBUG_ID(x)
#define MMHEAP_INITIAL_SIZE