#include "List.h" /* Private */ template void List::resize() { length <<= 1; T* temp = new T[length]; for (int i = 0; i < count; i++) temp[i] = elements[i]; delete[] elements; elements = temp; } template void List::free() { delete[] elements; } template void List::copyFrom(const List& other) { elements = new T[other.length]; for (int i = 0; i < other.count; i++) elements[i] = other.elements[i]; length = other.length; count = other.count; } /* Public */ template List::List(const T* elements, unsigned elementsCount) : List() { for (unsigned i = 0; i < elementsCount; i++) add(elements[i]); } template void List::add(const T& element) { if (length == count) resize(); elements[count++] = element; } template bool List::removeAt(unsigned index) { if (index >= count) return false; delete elements[index]; for (int i = index; i < count; i++) elements[i] = elements[i + 1]; elements[count--] = nullptr; } template T& List::operator[](unsigned index) { return elements[index]; } template const T& List::operator[](unsigned index) const { return elements[index]; } template std::istream& List::read(std::istream& istr) { istr.read((char*)&length, sizeof(length)); istr.read((char*)&count, sizeof(count)); delete[] elements; elements = new T[length]; for (int i = 0; i < count; i++) elements[i].read(istr); // disgusting return istr; } template std::ostream& List::write(std::ostream& ostr) const { ostr.write((const char*)&length, sizeof(length)); ostr.write((const char*)&count, sizeof(count)); for (int i = 0; i < count; i++) elements[i].write(ostr); // disgusting return ostr; } template unsigned List::get_length() const { return length; } template unsigned List::get_count() const { return count; } // Rule of 4 template List::List() { length = 8; count = 0; elements = new T[length]; } template List& List::operator=(const List& other) { if (this != &other) { free(); copyFrom(other); } return *this; } template List::List(const List& other) { copyFrom(other); } template List::~List() { free(); } // Move semantics template List::List(List&& other) { length = other.length; count = other.count; elements = other.elements; other.elements = nullptr; } template List& List::operator=(List&& other) { if (this != &other) { free(); length = other.length; count = other.count; elements = other.elements; other.elements = nullptr; } return *this; }