#include "slotlist.h" #include Slot::Slot(int sz) : size(sz), usage(0) { contents = new char[sz]; for(int i = 0; i < sz; i++) { contents[i] = ' '; } } char Slot::operator[](int i) { assert(i < size); return contents[i]; } void Slot::reset() { for(int i = 0; i < usage; i++) { contents[i] = ' '; } usage = 0; } int Slot::insertChar(char ch) { if(usage == size) { return 0; } contents[usage++] = ch; return usage; } Slotlist::Slotlist(int sls, int sz) : maxSlotUsage(0), slots(sls) { data = new Slot*[slots]; for(int i = 0; i < slots; i++) { data[i] = new Slot(sz); } } ostream& Slotlist::printTop(ostream& os, int leftMargin) { char *margBuf = new char[leftMargin + 1]; int i, j; // initialize margBuf; for(i = 0; i < leftMargin; i++) { margBuf[i] = ' '; } margBuf[i] = '\0'; // print out all the slots for(i = maxSlotUsage-1; i >= 0; i--) { // print out each row of the slots os << margBuf << " "; for(j = 0; j < slots; j++) { // print out one element of each slot os << (*data[j])[i] << ' '; } os << endl; } // print a blank line os << endl; // print out the numbers of the columns os << margBuf << " "; for(i = 0; i < slots; i++) { os << i << ' '; } os << endl; // print out a horzontal line os << margBuf << " "; for(i = 0; i < slots; i++) { os << "--"; } os << '-' << endl; return os; } ostream& Slotlist::printLeftRow(int row, ostream& os, int leftMargin) { for(int i = leftMargin-1; i >= 0; i--) { os << (*data[row])[i]; } return os; } ostream& Slotlist::printRightRow(int row, ostream& os) { for(int i = 0; i < data[row]->usage; i++) { os << (*data[row])[i]; } return os; } ostream& Slotlist::printBottom(ostream& os, int leftMargin) { char *margBuf = new char[leftMargin + 1]; int i, j; // initialize margBuf; for(i = 0; i < leftMargin; i++) { margBuf[i] = ' '; } margBuf[i] = '\0'; // print out a horzontal line os << margBuf << " "; for(i = 0; i < slots; i++) { os << "--"; } os << '-' << endl; // print out the numbers of the columns os << margBuf << " "; for(i = 0; i < slots; i++) { os << i << ' '; } os << endl; // print a blank line os << endl; // print out all the slots for(i = 0; i < maxSlotUsage; i++) { // print out each row of the slots os << margBuf << " "; for(j = 0; j < slots; j++) { // print out one element of each slot os << (*data[j])[i] << ' '; } os << endl; } return os; } void Slotlist::reset() { maxSlotUsage = 0; for(int i = 0; i < slots; i++) { data[i]->reset(); } } bool Slotlist::insertChar(char ch, int i) { if(i < 0 || i >= slots) { return false; } int newUsage = data[i]->insertChar(ch); if(newUsage == 0) { // overflowed return false; } if(newUsage > maxSlotUsage) { maxSlotUsage = newUsage; } return true; } int Slotlist::max() { return maxSlotUsage; }