/* vector.cc */ #include "vector.h" template Vector::Vector () { arraySize = 0; spaceAllocated = 0; arrayElems = NULL; } template Vector::Vector (Vector &a) { int i = 0; spaceAllocated = a.arraySize; arraySize = a.arraySize; if (arraySize > 0) { arrayElems = new Elemtype [arraySize]; for (i = 0; i < arraySize; i++) { arrayElems[i] = a.arrayElems[i]; } } else { arrayElems = NULL; } } template Vector::Vector (int sz, Elemtype e) { if (sz < 0) { cerr << "Can't make vector with less than 0 cells." << endl; abort (); } else { arraySize = sz; spaceAllocated = sz; arrayElems = new Elemtype [sz]; if (!arrayElems) { cerr << "Insufficient memory." << endl; abort (); } for (int i = 0; i < sz; i++) { arrayElems[i] = e; } } } template Vector::~Vector () { if (spaceAllocated > 0) { if (arrayElems) { delete [] arrayElems; } } } template int Vector::size () { return arraySize; } template Elemtype & Vector::operator [] (int i) { /* more detailed error messages than assert will give */ if (i < 0) { cerr << "Attempt to access array element " << i << " using []"; cerr << endl; } else if (i >= arraySize) { cerr << "Attempt to access array element " << i << " using [] "; cerr << "but size is only " << arraySize << endl; } /* more detailed error messages than assert will give */ assert (i>=0); assert (i < arraySize); return arrayElems[i]; } template void Vector::addh (Elemtype e) { if (arraySize == spaceAllocated) { // need more space int oldSpaceAllocated = spaceAllocated; spaceAllocated = (spaceAllocated+2)/2 + spaceAllocated; Elemtype *newvals = new Elemtype [spaceAllocated]; /* more detailed error messages than assert will give */ if (newvals == NULL) { cerr << "When addh'ing to an array, ran out of memory." << endl; cerr << " Have you deleted objects created with new?" << endl; } /* more detailed error messages than assert will give */ assert(newvals != NULL); for (int i = 0; i < arraySize; i++) { newvals[i] = arrayElems[i]; } if (oldSpaceAllocated > 0) { delete [] arrayElems; } arrayElems = newvals; } /* more detailed error messages than assert will give */ if (arraySize >= spaceAllocated) { cerr << "Internal error in the Vector class. " << endl; cerr << " Please notify your instructor. " << endl; } /* more detailed error messages than assert will give */ assert(arraySize < spaceAllocated); arrayElems[arraySize] = e; arraySize++; } template Elemtype Vector::remh () { /* more detailed error messages than assert will give */ if (arraySize <= 0) { cerr << "Attempt to remh from an empty array. " << endl; } /* more detailed error messages than assert will give */ assert(arraySize > 0); arraySize--; return arrayElems[arraySize]; } template Vector & Vector::operator = (Vector &a) { if (this == &a) { return *this; } int i = 0; if ((spaceAllocated < a.arraySize)) { if (spaceAllocated > 0) { delete [] arrayElems; } arrayElems = new Elemtype [a.arraySize]; spaceAllocated = a.arraySize; } arraySize = a.arraySize; for (i = 0; i < arraySize; i++) { arrayElems[i] = a.arrayElems[i]; } return *this; } template bool operator == (Vector &a1, Vector &a2) { if (&a1 == &a2) { return true; } else if (a1.size() != a2.size()) { return false; } else { for (int i = 0; i < a1.size(); i++) { if (a1[i] == a2[i]) { continue; } else { return false; } } return true; } } template // Postcondition: prints vector v on the standard output steam. ostream & operator << (ostream &os, Vector &v) { // Replace the following line with your solution to part 3 here os << endl; // leave the following line in your solution !! return os; }