20-CS-122-001 Computer Science II Spring 2012
Priority Queue class

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev     Next     Lectures     pqueue.cc     pqueue.h

#include <stdlib.h>
#include "pqueue.h"

PQueue::PQueue (int s, BigInt *(*v)(void*), ostream &(*d)(ostream &, void*)){
   size = s;
   objects = new void*[size];
   tail = 0;
   valfn = v;
   dispfn = d;
}

PQueue::PQueue (BigInt *(*v)(void*), ostream &(*d)(ostream &, void*)) {
   size = 100000;
   objects = new void*[size];
   tail = 0;
   valfn = v;
   dispfn = d;
}

PQueue::PQueue (BigInt *(*v)(void*)) {
   size = 100000;
   objects = new void*[size];
   tail = 0;
   valfn = v;
   dispfn = NULL;
}

PQueue::~PQueue() { delete objects; }

bool PQueue::empty() { return tail == 0; }

void PQueue::insert(void *obj) {
   if (obj == NULL) return;
   int ptr = tail;
   objects[tail++] = obj;
   while ((ptr-1)/2 >= 0 && 
          valfn(objects[ptr])->lessThan(valfn(objects[(ptr-1)/2]))) {
      void *tobj = objects[ptr];
      objects[ptr] = objects[(ptr-1)/2];
      objects[(ptr-1)/2] = tobj;
      ptr = (ptr-1)/2;
   }
}

void *PQueue::remove () {
   if (tail == 0) return NULL;
   void *t = objects[0];
   objects[0] = objects[--tail];
   int ptr = 0;
   while (2*(ptr+1) <= tail) {
      void *p = objects[ptr];
      if (valfn(objects[(ptr+1)*2-1])->lessThan(valfn(objects[ptr]))) {
         if (valfn(objects[(ptr+1)*2-1])->lessThan(valfn(objects[(ptr+1)*2]))) {
            objects[ptr] = objects[(ptr+1)*2-1];
            objects[(ptr+1)*2-1] = p;
            ptr = (ptr+1)*2-1;
         } else {
            objects[ptr] = objects[(ptr+1)*2];
            objects[(ptr+1)*2] = p;
            ptr = (ptr+1)*2;
         }
      } else if (2*(ptr+1) < tail) {
         if (valfn(objects[(ptr+1)*2])->lessThan(valfn(objects[ptr]))) {
            objects[ptr] = objects[(ptr+1)*2];
            objects[(ptr+1)*2] = p;
            ptr = (ptr+1)*2;
         } else break;
      } else break;
   }
   return t;
}

void PQueue::printTree () {
   int blanks = 80/2, d=1, n=1;
   for (int k=0 ; k < tail ; k++) {
      if (d == 0) {
         n *= 2;
         d = n;
         blanks = 40/(n+1);
         cout << "\n";
         for (int i=0 ; i < 30/n ; i++) cout << " ";
      } 
      for (int i=0 ; i < blanks ; i++) cout << " ";
      cout << "[";
      if (dispfn == NULL)
         cout << valfn(objects[k]);
      else
         dispfn(cout, objects[k]);
      cout << "] ";
      d--;
   }
   cout << "\n";
}

ostream & operator<<(ostream & out, PQueue *pq) {
   int count = pq->tail;
   bool *isdone = new bool[count];
   for (int i=0 ; i < count ; i++) isdone[i] = false;
   for (int i=0 ; i < count ; i++) {
      BigInt *tmp = new BigInt("100000");
      int j = -1;
      for (int k=0 ; k < pq->tail ; k++) {
         if (isdone[k]) continue;
         if (pq->valfn(pq->objects[k])->lessThan(tmp)) {
            j = k;
            tmp = pq->valfn(pq->objects[k]);
         }
      }
      isdone[j] = true;
      out << "[";
      if (pq->dispfn == NULL) 
         out << pq->valfn(pq->objects[j]);
      else
         out << pq->dispfn(out, pq->objects[j]);
      out << "] ";
   }
   return out;
}