/* ArrayVector.cpp Written by Matthew Fisher See ArrayVector.h for a full definition of the Vector class. */ #ifndef __VECTOR_CPP #define __VECTOR_CPP #include "ArrayVector.h" template Vector::Vector() { Size = 0; Data = NULL; } template Vector::Vector(UINT _Size) { Size = _Size; Data = new type[Size]; } template Vector::~Vector() { FreeMemory(); } template void Vector::FreeMemory() { Size = 0; if(Data) delete[] Data; Data = NULL; } /*template type& Vector::operator [] (int k) { if(k < 0 || k >= Size) //make sure k is in bounds { //normally I set a breakpoint here so I can see the call stack cout << "Out of bounds Vector access. k = " << k << ", Size = " << Size << endl; exit(1); } return Data[k]; } template const type& Vector::operator [] (int k) const { if(k < 0 || k >= Size) //make sure k is in bounds { //normally I set a breakpoint here so I can see the call stack cout << "Out of bounds Vector access. k = " << k << ", Size = " << Size << endl; exit(1); } return Data[k]; }*/ template void Vector::Allocate(UINT _Size) { FreeMemory(); Size = _Size; Data = new type[Size]; if(!Data) { cout << "Bad malloc" << endl; exit(1); //I've never actually seen this occur, but it's always possible to run out of memory. } } template void Vector::ReSize(UINT _Size) { if(Size == 0) Allocate(_Size); //ReSize is equivalent to Allocate if Size is 0. else { UINT ElementsToCopy = Size; type *Storage = new type[Size]; //create intermediate storage (not necessary if I rewrote this function...) for(UINT i=0; i Vector::Vector(const Vector &V) { Size = 0; Data = NULL; Allocate(V.Size); for(UINT i=0;i Vector& Vector::operator = (const Vector &V) { Allocate(V.Size); for(UINT i=0;i void Vector:: PushEnd(const type &t) { ReSize(Size+1); Data[Size-1] = t; } template void Vector:: PopEnd() { ReSize(Size-1); } #endif