#include #include #include #include "ArrayT.h" using namespace std; ArrayT::ArrayT(){ data = nullptr; // probably not required. size = 0; } void CopyData(const unique_ptr & src, unique_ptr & dest, size_t size) { for(size_t i = 0; i < size; i++) { dest[i] = src[i]; } return; } ArrayT::ArrayT(const ArrayT & other){ size = other.size; // data = new int[size] data = make_unique(size); CopyData(other.data, data, size); return; } ArrayT & ArrayT::operator =(const ArrayT & other){ if (this != &other) { size = other.size; // delete [] data // data = new int[size]; //data.reset(); // data = new int[size] data = make_unique(size); // a_single_data = make_unique CopyData(other.data, data, size); } return *this; } size_t ArrayT::Size() const { return size; } void ArrayT::PushBack(int i){ auto tmp = make_unique(size+1); CopyData(data, tmp, size); //data.reset(); data = move(tmp); data[size] = i; size++; return; } void ArrayT::PopBack(){ if (size > 0) { size--; auto tmp = make_unique(size); CopyData(data,tmp,size); data.swap(tmp); // tmp will go out of scope deleteing the old memory when it does. } else { throw length_error("Invalid length in pop back"); } return; } int ArrayT::Back()const{ if (size > 0) { return data[size-1]; } else { throw length_error("Invalid length in back"); } } int & ArrayT::operator[](size_t i) { return data[i]; } const int & ArrayT::operator[](size_t i) const { return data[i]; } int & ArrayT::At(size_t i){ if (i < size ) { return data[i]; } else { throw length_error("Invalid index in at"); } } const int & ArrayT::At(size_t i) const { if (i < size ) { return data[i]; } else { throw length_error("Invalid index in at"); } }