#include #include "WordArrayT.h" using namespace std; WordArrayT::WordArrayT(const WordArrayT & other) { if (other.data != nullptr) { capacity = other.capacity; size = other.size; data = new DataT[capacity]; for(size_t i = 0; i < size; ++i) { data[i] = other.data[i]; } } } WordArrayT::~WordArrayT() { if (data != nullptr) { delete [] data; } } WordArrayT & WordArrayT::operator =(const WordArrayT & other){ // do a deep copy if (this != &other) { capacity = other.capacity; size = other.size; if (other.data == nullptr) { data = nullptr; } else { if (data != nullptr) { delete [] data; } data = new DataT [capacity]; for(size_t i = 0; i < size; ++i) { data[i] = other.data[i]; } } } return *this; } bool WordArrayT::PushBack(DataT newWord){ bool good { true}; if (data == nullptr) { data = new DataT[3]; capacity = 3; } else if (size >= capacity) { capacity *= 2; DataT * tmp {new DataT[capacity]}; for(size_t i =0; i < size; ++i) { tmp[i] = data[i]; } delete [] data; data = tmp; } data[size] = newWord; ++size; return good; } DataT & WordArrayT::operator[](size_t index){ return data[index]; } const DataT & WordArrayT::operator[](size_t index) const{ return data[index]; } DataT & WordArrayT::at(size_t index){ if (data != nullptr and index < size) { return data[index]; } cerr << "Error: Out of bounds index " << index << endl; return data[0]; } const DataT & WordArrayT::at(size_t index) const{ if (data != nullptr and index < size) { return data[index]; } cerr << "Error: Out of bounds index " << index << endl; return data[0]; } size_t WordArrayT::Capacity() const{ return capacity; } size_t WordArrayT::Size() const{ return size; }