#ifndef _VECTOR #define _VECTOR template class Vector { T* objects; unsigned objectsCount; unsigned objectsAlloc; void free(); void copyFrom(const Vector& other); void resize(); public: Vector(); ~Vector(); Vector(const Vector& other); Vector& operator=(const Vector& other); Vector(Vector&& other); Vector& operator=(Vector&& other); unsigned Count() const; void PushBack(const T& object); T& operator[](unsigned index); const T& operator[](unsigned index) const; Vector& operator+=(const Vector& rhs); }; template Vector operator+(const Vector& lhs, const Vector& rhs); template void Vector::free() { delete[] objects; } template void Vector::copyFrom(const Vector& other) { objectsCount = other.objectsCount; objectsAlloc = other.objectsAlloc; objects = new T[objectsAlloc]; for (unsigned i = 0; i < objectsCount; ++i) objects[i] = other.objects[i]; } template void Vector::resize() { objectsAlloc = objectsAlloc == 0 ? 8 : objectsAlloc * 2; T* objectsLarger = new T[objectsAlloc]; for (unsigned i = 0; i < objectsCount; ++i) objectsLarger[i] = objects[i]; delete[] objects; objects = objectsLarger; } template Vector::Vector() : objects(), objectsAlloc(), objectsCount() { } template Vector::~Vector() { free(); } template Vector::Vector(const Vector& other) { copyFrom(other); } template Vector& Vector::operator=(const Vector& other) { if (this != &other) { free(); copyFrom(other); } return *this; } template Vector::Vector(Vector&& other) { objects = other.objects; objectsCount = other.objectsCount; objectsAlloc = other.objectsAlloc; other.objects = nullptr; other.objectsCount = other.objectsAlloc = 0; } template Vector& Vector::operator=(Vector&& other) { if (this != &other) { free(); objects = other.objects; objectsCount = other.objectsCount; objectsAlloc = other.objectsAlloc; other.objects = nullptr; other.objectsCount = other.objectsAlloc = 0; } return *this; } template unsigned Vector::Count() const { return objectsCount; } template void Vector::PushBack(const T& object) { if (objectsCount >= objectsAlloc) resize(); objects[objectsCount++] = object; } template T& Vector::operator[](unsigned index) { return objects[index]; } template const T& Vector::operator[](unsigned index) const { return objects[index]; } template Vector& Vector::operator+=(const Vector& rhs) { for (unsigned i = 0; i < rhs.objectsCount; ++i) PushBack(rhs.objects[i]); return *this; } template Vector operator+(const Vector& lhs, const Vector& rhs) { Vector val(lhs); val += rhs; return val; } #endif /* _VECTOR */